transformers
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
16import unittest
17
18from transformers import is_torch_available
19from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device
20
21
22if is_torch_available():
23import torch
24
25from transformers import CamembertModel
26
27
28@require_torch
29@require_sentencepiece
30@require_tokenizers
31class CamembertModelIntegrationTest(unittest.TestCase):
32@slow
33def test_output_embeds_base_model(self):
34model = CamembertModel.from_pretrained("almanach/camembert-base")
35model.to(torch_device)
36
37input_ids = torch.tensor(
38[[5, 121, 11, 660, 16, 730, 25543, 110, 83, 6]],
39device=torch_device,
40dtype=torch.long,
41) # J'aime le camembert !
42with torch.no_grad():
43output = model(input_ids)["last_hidden_state"]
44expected_shape = torch.Size((1, 10, 768))
45self.assertEqual(output.shape, expected_shape)
46# compare the actual values for a slice.
47expected_slice = torch.tensor(
48[[[-0.0254, 0.0235, 0.1027], [0.0606, -0.1811, -0.0418], [-0.1561, -0.1127, 0.2687]]],
49device=torch_device,
50dtype=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
56self.assertTrue(torch.allclose(output[:, :3, :3], expected_slice, atol=1e-4))
57