test_tokenization_lxmert.py 3.07 KB
Newer Older
1
# coding=utf-8
Sylvain Gugger's avatar
Sylvain Gugger committed
2
# Copyright 2018 LXMERT Authors, The Hugging Face Team.
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#
# 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.


import os
import unittest

20
from transformers import LxmertTokenizer, LxmertTokenizerFast
Sylvain Gugger's avatar
Sylvain Gugger committed
21
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES
22
from transformers.testing_utils import require_tokenizers
23

Yih-Dar's avatar
Yih-Dar committed
24
from ...test_tokenization_common import TokenizerTesterMixin
25
26


27
@require_tokenizers
28
class LxmertTokenizationTest(TokenizerTesterMixin, unittest.TestCase):
29
    from_pretrained_id = "unc-nlp/lxmert-base-uncased"
30
    tokenizer_class = LxmertTokenizer
31
32
33
    rust_tokenizer_class = LxmertTokenizerFast
    test_rust_tokenizer = True
    space_between_special_tokens = True
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

    def setUp(self):
        super().setUp()

        vocab_tokens = [
            "[UNK]",
            "[CLS]",
            "[SEP]",
            "want",
            "##want",
            "##ed",
            "wa",
            "un",
            "runn",
            "##ing",
            ",",
            "low",
            "lowest",
        ]
        self.vocab_file = os.path.join(self.tmpdirname, VOCAB_FILES_NAMES["vocab_file"])
        with open(self.vocab_file, "w", encoding="utf-8") as vocab_writer:
            vocab_writer.write("".join([x + "\n" for x in vocab_tokens]))

    def get_input_output_texts(self, tokenizer):
Arthur's avatar
Arthur committed
58
        input_text = "UNwant\u00e9d,running"
59
60
61
62
63
64
        output_text = "unwanted, running"
        return input_text, output_text

    def test_full_tokenizer(self):
        tokenizer = self.tokenizer_class(self.vocab_file)

Arthur's avatar
Arthur committed
65
        tokens = tokenizer.tokenize("UNwant\u00e9d,running")
66
67
        self.assertListEqual(tokens, ["un", "##want", "##ed", ",", "runn", "##ing"])
        self.assertListEqual(tokenizer.convert_tokens_to_ids(tokens), [7, 4, 5, 10, 8, 9])
68
69
70

    def test_rust_and_python_full_tokenizers(self):
        if not self.test_rust_tokenizer:
amyeroberts's avatar
amyeroberts committed
71
            self.skipTest(reason="test_rust_tokenizer is set to False")
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89

        tokenizer = self.get_tokenizer()
        rust_tokenizer = self.get_rust_tokenizer()

        sequence = "I was born in 92000, and this is fals茅."

        tokens = tokenizer.tokenize(sequence)
        rust_tokens = rust_tokenizer.tokenize(sequence)
        self.assertListEqual(tokens, rust_tokens)

        ids = tokenizer.encode(sequence, add_special_tokens=False)
        rust_ids = rust_tokenizer.encode(sequence, add_special_tokens=False)
        self.assertListEqual(ids, rust_ids)

        rust_tokenizer = self.get_rust_tokenizer()
        ids = tokenizer.encode(sequence)
        rust_ids = rust_tokenizer.encode(sequence)
        self.assertListEqual(ids, rust_ids)