transformers

Форк
0
/
test_modeling_camembert.py 
56 строк · 2.0 Кб
1
# coding=utf-8
2
# Copyright 2020 The HuggingFace Team. All rights reserved.
3
#
4
# Licensed under the Apache License, Version 2.0 (the "License");
5
# you may not use this file except in compliance with the License.
6
# You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15

16
import unittest
17

18
from transformers import is_torch_available
19
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device
20

21

22
if is_torch_available():
23
    import torch
24

25
    from transformers import CamembertModel
26

27

28
@require_torch
29
@require_sentencepiece
30
@require_tokenizers
31
class CamembertModelIntegrationTest(unittest.TestCase):
32
    @slow
33
    def test_output_embeds_base_model(self):
34
        model = CamembertModel.from_pretrained("almanach/camembert-base")
35
        model.to(torch_device)
36

37
        input_ids = torch.tensor(
38
            [[5, 121, 11, 660, 16, 730, 25543, 110, 83, 6]],
39
            device=torch_device,
40
            dtype=torch.long,
41
        )  # J'aime le camembert !
42
        with torch.no_grad():
43
            output = model(input_ids)["last_hidden_state"]
44
        expected_shape = torch.Size((1, 10, 768))
45
        self.assertEqual(output.shape, expected_shape)
46
        # compare the actual values for a slice.
47
        expected_slice = torch.tensor(
48
            [[[-0.0254, 0.0235, 0.1027], [0.0606, -0.1811, -0.0418], [-0.1561, -0.1127, 0.2687]]],
49
            device=torch_device,
50
            dtype=torch.float,
51
        )
52
        # camembert = torch.hub.load('pytorch/fairseq', 'camembert.v0')
53
        # camembert.eval()
54
        # expected_slice = roberta.model.forward(input_ids)[0][:, :3, :3].detach()
55

56
        self.assertTrue(torch.allclose(output[:, :3, :3], expected_slice, atol=1e-4))
57

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.