test_tokenization_utils.py 1.54 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
20
from transformers import PreTrainedTokenizer
from transformers.tokenization_gpt2 import GPT2Tokenizer
21

22
23
24
from .utils import slow


25
class TokenizerUtilsTest(unittest.TestCase):
26
27
28
29
30
    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)
31
            self.assertIsInstance(tokenizer, tokenizer_class)
32
33
            self.assertIsInstance(tokenizer, PreTrainedTokenizer)

34
            for special_tok in tokenizer.all_special_tokens:
Aymeric Augustin's avatar
Aymeric Augustin committed
35
                self.assertIsInstance(special_tok, str)
36
37
38
                special_tok_id = tokenizer.convert_tokens_to_ids(special_tok)
                self.assertIsInstance(special_tok_id, int)

39
    @slow
40
41
    def test_pretrained_tokenizers(self):
        self.check_tokenizer_from_pretrained(GPT2Tokenizer)