test_tokenization_utils.py 1.68 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# coding=utf-8
# Copyright 2018 HuggingFace Inc..
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Aymeric Augustin's avatar
Aymeric Augustin committed
15

16
17

import unittest
Aymeric Augustin's avatar
Aymeric Augustin committed
18

19
import six
20

21
22
from transformers import PreTrainedTokenizer
from transformers.tokenization_gpt2 import GPT2Tokenizer
23

24
25
26
from .utils import slow


27
class TokenizerUtilsTest(unittest.TestCase):
28
29
30
31
32
    def check_tokenizer_from_pretrained(self, tokenizer_class):
        s3_models = list(tokenizer_class.max_model_input_sizes.keys())
        for model_name in s3_models[:1]:
            tokenizer = tokenizer_class.from_pretrained(model_name)
            self.assertIsNotNone(tokenizer)
33
            self.assertIsInstance(tokenizer, tokenizer_class)
34
35
            self.assertIsInstance(tokenizer, PreTrainedTokenizer)

36
37
            for special_tok in tokenizer.all_special_tokens:
                if six.PY2:
38
                    self.assertIsInstance(special_tok, unicode)  # noqa: F821
39
40
41
42
43
                else:
                    self.assertIsInstance(special_tok, str)
                special_tok_id = tokenizer.convert_tokens_to_ids(special_tok)
                self.assertIsInstance(special_tok_id, int)

44
    @slow
45
46
    def test_pretrained_tokenizers(self):
        self.check_tokenizer_from_pretrained(GPT2Tokenizer)