


import os
import pickle
import unittest

from transformers import AutoTokenizer
from transformers.models.bert.tokenization_bert import BertTokenizer
from transformers.models.bert_japanese.tokenization_bert_japanese import (
    VOCAB_FILES_NAMES,
    BertJapaneseTokenizer,
    CharacterTokenizer,
    JumanppTokenizer,
    MecabTokenizer,
    SudachiTokenizer,
    WordpieceTokenizer,
)
from transformers.testing_utils import custom_tokenizers, require_jumanpp, require_sudachi

from ...test_tokenization_common import TokenizerTesterMixin
@custom_tokenizers
class  SCREAMING_SNAKE_CASE__  (				lowercase__						,    unittest.TestCase			):
      snake_case__				:     int        =						BertJapaneseTokenizer
      snake_case__				:     Optional[int]        =						False
      snake_case__				:     Union[str, Any]        =						True
      def        SCREAMING_SNAKE_CASE							(       self				:       Tuple     )		->			List[str]:
             super().setUp()

             a_					:		Optional[Any]							=  [
                 '[UNK]',
                 '[CLS]',
                 '[SEP]',
                 'こんにちは',
                 'こん',
                 'にちは',
                 'ばんは',
                 '##こん',
                 '##にちは',
                 '##ばんは',
                 '世界',
                 '##世界',
                 '、',
                 '##、',
                 '。',
                 '##。',
             ]

             a_					:		Dict							=  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        SCREAMING_SNAKE_CASE							(       self				:       str     ,							SCREAMING_SNAKE_CASE__				:       List[Any]     )		->			Optional[int]:
             a_					:		Optional[int]							=  'こんにちは、世界。 \nこんばんは、世界。'
             a_					:		Optional[Any]							=  'こんにちは 、 世界 。 こんばんは 、 世界 。'
             return input_text, output_text
      def        SCREAMING_SNAKE_CASE							(       self				:       Dict     ,							SCREAMING_SNAKE_CASE__				:       List[str]     )		->			int:
             a_							,		a_					:		int							=  self.get_input_output_texts(SCREAMING_SNAKE_CASE__     )
             a_					:		List[Any]							=  tokenizer.encode(SCREAMING_SNAKE_CASE__     ,							add_special_tokens=SCREAMING_SNAKE_CASE__     )
             a_					:		Tuple							=  tokenizer.decode(SCREAMING_SNAKE_CASE__     ,							clean_up_tokenization_spaces=SCREAMING_SNAKE_CASE__     )
             return text, ids
      def        SCREAMING_SNAKE_CASE							(       self				:       Optional[int]     )		->			Dict:
             pass  # TODO add if relevant
      def        SCREAMING_SNAKE_CASE							(       self				:       List[str]     )		->			Tuple:
             pass  # TODO add if relevant
      def        SCREAMING_SNAKE_CASE							(       self				:       List[Any]     )		->			Optional[Any]:
             pass  # TODO add if relevant
      def        SCREAMING_SNAKE_CASE							(       self				:       Any     )		->			Any:
             a_					:		Union[str, Any]							=  self.tokenizer_class(self.vocab_file     )

             a_					:		str							=  tokenizer.tokenize('こんにちは、世界。\nこんばんは、世界。'     )
             self.assertListEqual(SCREAMING_SNAKE_CASE__     ,							['こんにちは', '、', '世界', '。', 'こん', '##ばんは', '、', '世界', '。']     )
             self.assertListEqual(tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE__     )     ,							[3, 1_2, 1_0, 1_4, 4, 9, 1_2, 1_0, 1_4]     )
      def        SCREAMING_SNAKE_CASE							(       self				:       Optional[int]     )		->			List[str]:
             a_					:		str							=  self.tokenizer_class(self.vocab_file     ,							word_tokenizer_type='mecab'     )
             self.assertIsNotNone(SCREAMING_SNAKE_CASE__     )

             a_					:		str							=  'こんにちは、世界。\nこんばんは、世界。'
             a_					:		List[Any]							=  tokenizer.tokenize(SCREAMING_SNAKE_CASE__     )
             self.assertListEqual(SCREAMING_SNAKE_CASE__     ,							['こんにちは', '、', '世界', '。', 'こん', '##ばんは', '、', '世界', '。']     )
             self.assertListEqual(tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE__     )     ,							[3, 1_2, 1_0, 1_4, 4, 9, 1_2, 1_0, 1_4]     )

             a_					:		Optional[int]							=  os.path.join(self.tmpdirname     ,							'tokenizer.bin'     )
             with open(SCREAMING_SNAKE_CASE__     ,							'wb'     ) as handle:
                    pickle.dump(SCREAMING_SNAKE_CASE__     ,							SCREAMING_SNAKE_CASE__     )

             with open(SCREAMING_SNAKE_CASE__     ,							'rb'     ) as handle:
                    a_					:		Union[str, Any]							=  pickle.load(SCREAMING_SNAKE_CASE__     )

             a_					:		List[Any]							=  tokenizer_new.tokenize(SCREAMING_SNAKE_CASE__     )

             self.assertListEqual(SCREAMING_SNAKE_CASE__     ,							SCREAMING_SNAKE_CASE__     )
      def        SCREAMING_SNAKE_CASE							(       self				:       Tuple     )		->			Union[str, Any]:
             a_					:		List[str]							=  MecabTokenizer(mecab_dic='ipadic'     )

             self.assertListEqual(
                 tokenizer.tokenize(' \tｱｯﾌﾟﾙストアでiPhone８ が  \n 発売された　。  '     )     ,							['アップルストア', 'で', 'iPhone', '8', 'が', '発売', 'さ', 'れ', 'た', '。']     ,							)
      def        SCREAMING_SNAKE_CASE							(       self				:       Union[str, Any]     )		->			List[Any]:
             try:
                    a_					:		List[str]							=  MecabTokenizer(mecab_dic='unidic_lite'     )
             except ModuleNotFoundError:
                    return

             self.assertListEqual(
                 tokenizer.tokenize(' \tｱｯﾌﾟﾙストアでiPhone８ が  \n 発売された　。  '     )     ,							['アップル', 'ストア', 'で', 'iPhone', '8', 'が', '発売', 'さ', 'れ', 'た', '。']     ,							)
      def        SCREAMING_SNAKE_CASE							(       self				:       Tuple     )		->			Optional[int]:
             try:
                    a_					:		Union[str, Any]							=  MecabTokenizer(mecab_dic='unidic'     )
             except ModuleNotFoundError:
                    return

             self.assertListEqual(
                 tokenizer.tokenize(' \tｱｯﾌﾟﾙストアでiPhone８ が  \n 発売された　。  '     )     ,							['アップル', 'ストア', 'で', 'iPhone', '8', 'が', '発売', 'さ', 'れ', 'た', '。']     ,							)
      def        SCREAMING_SNAKE_CASE							(       self				:       str     )		->			Any:
             a_					:		Optional[Any]							=  MecabTokenizer(do_lower_case=SCREAMING_SNAKE_CASE__     ,							mecab_dic='ipadic'     )

             self.assertListEqual(
                 tokenizer.tokenize(' \tｱｯﾌﾟﾙストアでiPhone８ が  \n 発売された　。  '     )     ,							['アップルストア', 'で', 'iphone', '8', 'が', '発売', 'さ', 'れ', 'た', '。']     ,							)
      def        SCREAMING_SNAKE_CASE							(       self				:       Optional[int]     )		->			Union[str, Any]:
             try:
                    a_					:		Any							=  MecabTokenizer(
                        do_lower_case=SCREAMING_SNAKE_CASE__     ,							normalize_text=SCREAMING_SNAKE_CASE__     ,							mecab_option='-d /usr/local/lib/mecab/dic/jumandic'     )
             except RuntimeError:
                    # if dict doesn't exist in the system, previous code raises this error.
                    return

             self.assertListEqual(
                 tokenizer.tokenize(' \tｱｯﾌﾟﾙストアでiPhone８ が  \n 発売された　。  '     )     ,							['ｱｯﾌﾟﾙストア', 'で', 'iPhone', '８', 'が', '発売', 'さ', 'れた', '\u3000', '。']     ,							)
      def        SCREAMING_SNAKE_CASE							(       self				:       List[str]     )		->			Any:
             a_					:		int							=  MecabTokenizer(normalize_text=SCREAMING_SNAKE_CASE__     ,							mecab_dic='ipadic'     )

             self.assertListEqual(
                 tokenizer.tokenize(' \tｱｯﾌﾟﾙストアでiPhone８ が  \n 発売された　。  '     )     ,							['ｱｯﾌﾟﾙストア', 'で', 'iPhone', '８', 'が', '発売', 'さ', 'れ', 'た', '　', '。']     ,							)
      @require_sudachi
      def        SCREAMING_SNAKE_CASE							(       self				:       Optional[int]     )		->			str:
             a_					:		List[str]							=  self.tokenizer_class(self.vocab_file     ,							word_tokenizer_type='sudachi'     )
             self.assertIsNotNone(SCREAMING_SNAKE_CASE__     )

             a_					:		List[str]							=  'こんにちは、世界。\nこんばんは、世界。'
             a_					:		Dict							=  tokenizer.tokenize(SCREAMING_SNAKE_CASE__     )
             self.assertListEqual(SCREAMING_SNAKE_CASE__     ,							['こんにちは', '、', '世界', '。', 'こん', '##ばんは', '、', '世界', '。']     )
             self.assertListEqual(tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE__     )     ,							[3, 1_2, 1_0, 1_4, 4, 9, 1_2, 1_0, 1_4]     )

             a_					:		int							=  os.path.join(self.tmpdirname     ,							'tokenizer.bin'     )
             with open(SCREAMING_SNAKE_CASE__     ,							'wb'     ) as handle:
                    pickle.dump(SCREAMING_SNAKE_CASE__     ,							SCREAMING_SNAKE_CASE__     )

             with open(SCREAMING_SNAKE_CASE__     ,							'rb'     ) as handle:
                    a_					:		Union[str, Any]							=  pickle.load(SCREAMING_SNAKE_CASE__     )

             a_					:		Optional[Any]							=  tokenizer_new.tokenize(SCREAMING_SNAKE_CASE__     )

             self.assertListEqual(SCREAMING_SNAKE_CASE__     ,							SCREAMING_SNAKE_CASE__     )
      @require_sudachi
      def        SCREAMING_SNAKE_CASE							(       self				:       int     )		->			List[str]:
             a_					:		List[str]							=  SudachiTokenizer(sudachi_dict_type='core'     )

             self.assertListEqual(
                 tokenizer.tokenize(' \tｱｯﾌﾟﾙストアでiPhone８ が  \n 発売された　。  '     )     ,							[' ', '\t', 'アップル', 'ストア', 'で', 'iPhone', '8', ' ', 'が', ' ', ' ', '\n ', '発売', 'さ', 'れ', 'た', ' ', '。', ' ', ' ']     ,							)
      @require_sudachi
      def        SCREAMING_SNAKE_CASE							(       self				:       Tuple     )		->			Union[str, Any]:
             a_					:		List[Any]							=  SudachiTokenizer(sudachi_dict_type='core'     ,							sudachi_split_mode='A'     )

             self.assertListEqual(tokenizer.tokenize('外国人参政権'     )     ,							['外国', '人', '参政', '権']     )
      @require_sudachi
      def        SCREAMING_SNAKE_CASE							(       self				:       Union[str, Any]     )		->			Tuple:
             a_					:		Tuple							=  SudachiTokenizer(sudachi_dict_type='core'     ,							sudachi_split_mode='B'     )

             self.assertListEqual(tokenizer.tokenize('外国人参政権'     )     ,							['外国人', '参政権']     )
      @require_sudachi
      def        SCREAMING_SNAKE_CASE							(       self				:       str     )		->			List[str]:
             a_					:		Union[str, Any]							=  SudachiTokenizer(sudachi_dict_type='core'     ,							sudachi_split_mode='C'     )

             self.assertListEqual(tokenizer.tokenize('外国人参政権'     )     ,							['外国人参政権']     )
      @require_sudachi
      def        SCREAMING_SNAKE_CASE							(       self				:       List[Any]     )		->			Union[str, Any]:
             a_					:		Dict							=  SudachiTokenizer(do_lower_case=SCREAMING_SNAKE_CASE__     ,							sudachi_dict_type='core'     )

             self.assertListEqual(
                 tokenizer.tokenize(' \tｱｯﾌﾟﾙストアでiPhone８ が  \n 発売された　。  '     )     ,							[' ', '\t', 'アップル', 'ストア', 'で', 'iphone', '8', ' ', 'が', ' ', ' ', '\n ', '発売', 'さ', 'れ', 'た', ' ', '。', ' ', ' ']     ,							)
      @require_sudachi
      def        SCREAMING_SNAKE_CASE							(       self				:       Union[str, Any]     )		->			Optional[Any]:
             a_					:		Any							=  SudachiTokenizer(normalize_text=SCREAMING_SNAKE_CASE__     ,							sudachi_dict_type='core'     )

             self.assertListEqual(
                 tokenizer.tokenize(' \tｱｯﾌﾟﾙストアでiPhone８ が  \n 発売された　。  '     )     ,							[' ', '\t', 'ｱｯﾌﾟﾙ', 'ストア', 'で', 'iPhone', '８', ' ', 'が', ' ', ' ', '\n ', '発売', 'さ', 'れ', 'た', '\u3000', '。', ' ', ' ']     ,							)
      @require_sudachi
      def        SCREAMING_SNAKE_CASE							(       self				:       Tuple     )		->			List[str]:
             a_					:		int							=  SudachiTokenizer(trim_whitespace=SCREAMING_SNAKE_CASE__     ,							sudachi_dict_type='core'     )

             self.assertListEqual(
                 tokenizer.tokenize(' \tｱｯﾌﾟﾙストアでiPhone８ が  \n 発売された　。  '     )     ,							['アップル', 'ストア', 'で', 'iPhone', '8', 'が', '発売', 'さ', 'れ', 'た', '。']     ,							)
      @require_jumanpp
      def        SCREAMING_SNAKE_CASE							(       self				:       Tuple     )		->			Optional[Any]:
             a_					:		Tuple							=  self.tokenizer_class(self.vocab_file     ,							word_tokenizer_type='jumanpp'     )
             self.assertIsNotNone(SCREAMING_SNAKE_CASE__     )

             a_					:		Any							=  'こんにちは、世界。\nこんばんは、世界。'
             a_					:		List[Any]							=  tokenizer.tokenize(SCREAMING_SNAKE_CASE__     )
             self.assertListEqual(SCREAMING_SNAKE_CASE__     ,							['こんにちは', '、', '世界', '。', 'こん', '##ばんは', '、', '世界', '。']     )
             self.assertListEqual(tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE__     )     ,							[3, 1_2, 1_0, 1_4, 4, 9, 1_2, 1_0, 1_4]     )

             a_					:		Optional[int]							=  os.path.join(self.tmpdirname     ,							'tokenizer.bin'     )
             with open(SCREAMING_SNAKE_CASE__     ,							'wb'     ) as handle:
                    pickle.dump(SCREAMING_SNAKE_CASE__     ,							SCREAMING_SNAKE_CASE__     )

             with open(SCREAMING_SNAKE_CASE__     ,							'rb'     ) as handle:
                    a_					:		Optional[Any]							=  pickle.load(SCREAMING_SNAKE_CASE__     )

             a_					:		Tuple							=  tokenizer_new.tokenize(SCREAMING_SNAKE_CASE__     )

             self.assertListEqual(SCREAMING_SNAKE_CASE__     ,							SCREAMING_SNAKE_CASE__     )
      @require_jumanpp
      def        SCREAMING_SNAKE_CASE							(       self				:       Union[str, Any]     )		->			Optional[int]:
             a_					:		Optional[Any]							=  JumanppTokenizer()

             self.assertListEqual(
                 tokenizer.tokenize(' \tｱｯﾌﾟﾙストアでiPhone８ が  \n 発売された　。  '     )     ,							['アップル', 'ストア', 'で', 'iPhone', '8', '\u3000', 'が', '\u3000', '\u3000', '\u3000', '発売', 'さ', 'れた', '\u3000', '。']     ,							)
      @require_jumanpp
      def        SCREAMING_SNAKE_CASE							(       self				:       Any     )		->			Optional[Any]:
             a_					:		Union[str, Any]							=  JumanppTokenizer(do_lower_case=SCREAMING_SNAKE_CASE__     )

             self.assertListEqual(
                 tokenizer.tokenize(' \tｱｯﾌﾟﾙストアでiPhone８ が  \n 発売された　。  '     )     ,							['アップル', 'ストア', 'で', 'iphone', '8', '\u3000', 'が', '\u3000', '\u3000', '\u3000', '発売', 'さ', 'れた', '\u3000', '。']     ,							)
      @require_jumanpp
      def        SCREAMING_SNAKE_CASE							(       self				:       List[Any]     )		->			List[Any]:
             a_					:		Optional[int]							=  JumanppTokenizer(normalize_text=SCREAMING_SNAKE_CASE__     )

             self.assertListEqual(
                 tokenizer.tokenize(' \tｱｯﾌﾟﾙストアでiPhone８ が  \n 発売された　。  '     )     ,							['ｱ', 'ｯ', 'ﾌ', 'ﾟ', 'ﾙ', 'ストア', 'で', 'iPhone', '８', '\u3000', 'が', '\u3000', '\u3000', '\u3000', '発売', 'さ', 'れた', '\u3000', '。']     ,							)
      @require_jumanpp
      def        SCREAMING_SNAKE_CASE							(       self				:       Any     )		->			int:
             a_					:		Dict							=  JumanppTokenizer(trim_whitespace=SCREAMING_SNAKE_CASE__     )

             self.assertListEqual(
                 tokenizer.tokenize(' \tｱｯﾌﾟﾙストアでiPhone８ が  \n 発売された　。  '     )     ,							['アップル', 'ストア', 'で', 'iPhone', '8', 'が', '発売', 'さ', 'れた', '。']     ,							)
      @require_jumanpp
      def        SCREAMING_SNAKE_CASE							(       self				:       Any     )		->			List[str]:
             a_					:		str							=  JumanppTokenizer()

             self.assertListEqual(
                 tokenizer.tokenize('ありがとうございますm(_ _)ｍ見つけるのが大変です。'     )     ,							['ありがとう', 'ございます', 'm(_ _)m', '見つける', 'の', 'が', '大変です', '。']     ,							)
      def        SCREAMING_SNAKE_CASE							(       self				:       Dict     )		->			Any:
             a_					:		Tuple							=  ['[UNK]', '[CLS]', '[SEP]', 'こんにちは', 'こん', 'にちは', 'ばんは', '##こん', '##にちは', '##ばんは']

             a_					:		List[Any]							=  {}
             for i, token in enumerate(SCREAMING_SNAKE_CASE__     ):
                    a_					:		List[str]							=  i
             a_					:		Dict							=  WordpieceTokenizer(vocab=SCREAMING_SNAKE_CASE__     ,							unk_token='[UNK]'     )

             self.assertListEqual(tokenizer.tokenize(''     )     ,							[]     )

             self.assertListEqual(tokenizer.tokenize('こんにちは'     )     ,							['こんにちは']     )

             self.assertListEqual(tokenizer.tokenize('こんばんは'     )     ,							['こん', '##ばんは']     )

             self.assertListEqual(tokenizer.tokenize('こんばんは こんばんにちは こんにちは'     )     ,							['こん', '##ばんは', '[UNK]', 'こんにちは']     )
      def        SCREAMING_SNAKE_CASE							(       self				:       Union[str, Any]     )		->			List[str]:
             a_					:		List[str]							=  BertJapaneseTokenizer.from_pretrained('nlp-waseda/roberta-base-japanese-with-auto-jumanpp'     )
             a_					:		List[str]							=  tokenizer.subword_tokenizer

             a_					:		Optional[int]							=  subword_tokenizer.tokenize('国境 の 長い トンネル を 抜ける と 雪国 であった 。'     )
             self.assertListEqual(SCREAMING_SNAKE_CASE__     ,							['▁国境', '▁の', '▁長い', '▁トンネル', '▁を', '▁抜ける', '▁と', '▁雪', '国', '▁であった', '▁。']     )

             a_					:		Optional[int]							=  subword_tokenizer.tokenize('こんばんは こんばん にち は こんにちは'     )
             self.assertListEqual(SCREAMING_SNAKE_CASE__     ,							['▁こん', 'ばん', 'は', '▁こん', 'ばん', '▁に', 'ち', '▁は', '▁こんにちは']     )

      def        SCREAMING_SNAKE_CASE							(       self				:       Tuple     )		->			Tuple:
             a_					:		Optional[Any]							=  self.tokenizer_class.from_pretrained('cl-tohoku/bert-base-japanese'     )

             a_					:		int							=  tokenizer.encode('ありがとう。'     ,							add_special_tokens=SCREAMING_SNAKE_CASE__     )
             a_					:		Dict							=  tokenizer.encode('どういたしまして。'     ,							add_special_tokens=SCREAMING_SNAKE_CASE__     )

             a_					:		int							=  tokenizer.build_inputs_with_special_tokens(SCREAMING_SNAKE_CASE__     )
             a_					:		List[Any]							=  tokenizer.build_inputs_with_special_tokens(SCREAMING_SNAKE_CASE__     ,							SCREAMING_SNAKE_CASE__     )

             # 2 is for "[CLS]", 3 is for "[SEP]"
             assert encoded_sentence == [2] + text + [3]
             assert encoded_pair == [2] + text + [3] + text_a + [3]
@custom_tokenizers
class  SCREAMING_SNAKE_CASE__  (				lowercase__						,    unittest.TestCase			):
      snake_case__				:     Optional[int]        =						BertJapaneseTokenizer
      snake_case__				:     str        =						False
      def        SCREAMING_SNAKE_CASE							(       self				:       str     )		->			Any:
             super().setUp()

             a_					:		int							=  ['[UNK]', '[CLS]', '[SEP]', 'こ', 'ん', 'に', 'ち', 'は', 'ば', '世', '界', '、', '。']

             a_					:		str							=  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        SCREAMING_SNAKE_CASE							(       self				:       str     ,							**SCREAMING_SNAKE_CASE__				:       Optional[int]     )		->			Tuple:
             return BertJapaneseTokenizer.from_pretrained(self.tmpdirname     ,							subword_tokenizer_type='character'     ,							**SCREAMING_SNAKE_CASE__     )
      def        SCREAMING_SNAKE_CASE							(       self				:       Dict     ,							SCREAMING_SNAKE_CASE__				:       List[str]     )		->			List[str]:
             a_					:		List[Any]							=  'こんにちは、世界。 \nこんばんは、世界。'
             a_					:		Optional[Any]							=  'こ ん に ち は 、 世 界 。 こ ん ば ん は 、 世 界 。'
             return input_text, output_text
      def        SCREAMING_SNAKE_CASE							(       self				:       Any     )		->			List[str]:
             pass  # TODO add if relevant
      def        SCREAMING_SNAKE_CASE							(       self				:       List[Any]     )		->			List[str]:
             pass  # TODO add if relevant
      def        SCREAMING_SNAKE_CASE							(       self				:       Optional[Any]     )		->			Tuple:
             pass  # TODO add if relevant
      def        SCREAMING_SNAKE_CASE							(       self				:       Optional[Any]     )		->			Dict:
             a_					:		Optional[int]							=  self.tokenizer_class(self.vocab_file     ,							subword_tokenizer_type='character'     )

             a_					:		Any							=  tokenizer.tokenize('こんにちは、世界。 \nこんばんは、世界。'     )
             self.assertListEqual(
                 SCREAMING_SNAKE_CASE__     ,							['こ', 'ん', 'に', 'ち', 'は', '、', '世', '界', '。', 'こ', 'ん', 'ば', 'ん', 'は', '、', '世', '界', '。']     )
             self.assertListEqual(
                 tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE__     )     ,							[3, 4, 5, 6, 7, 1_1, 9, 1_0, 1_2, 3, 4, 8, 4, 7, 1_1, 9, 1_0, 1_2]     )
      def        SCREAMING_SNAKE_CASE							(       self				:       str     )		->			Optional[Any]:
             a_					:		Dict							=  ['[UNK]', '[CLS]', '[SEP]', 'こ', 'ん', 'に', 'ち', 'は', 'ば', '世', '界', '、', '。']

             a_					:		Dict							=  {}
             for i, token in enumerate(SCREAMING_SNAKE_CASE__     ):
                    a_					:		str							=  i
             a_					:		Optional[Any]							=  CharacterTokenizer(vocab=SCREAMING_SNAKE_CASE__     ,							unk_token='[UNK]'     )

             self.assertListEqual(tokenizer.tokenize(''     )     ,							[]     )

             self.assertListEqual(tokenizer.tokenize('こんにちは'     )     ,							['こ', 'ん', 'に', 'ち', 'は']     )

             self.assertListEqual(tokenizer.tokenize('こんにちほ'     )     ,							['こ', 'ん', 'に', 'ち', '[UNK]']     )

      def        SCREAMING_SNAKE_CASE							(       self				:       str     )		->			Optional[Any]:
             a_					:		Union[str, Any]							=  self.tokenizer_class.from_pretrained('cl-tohoku/bert-base-japanese-char'     )

             a_					:		Optional[Any]							=  tokenizer.encode('ありがとう。'     ,							add_special_tokens=SCREAMING_SNAKE_CASE__     )
             a_					:		List[Any]							=  tokenizer.encode('どういたしまして。'     ,							add_special_tokens=SCREAMING_SNAKE_CASE__     )

             a_					:		Union[str, Any]							=  tokenizer.build_inputs_with_special_tokens(SCREAMING_SNAKE_CASE__     )
             a_					:		Union[str, Any]							=  tokenizer.build_inputs_with_special_tokens(SCREAMING_SNAKE_CASE__     ,							SCREAMING_SNAKE_CASE__     )

             # 2 is for "[CLS]", 3 is for "[SEP]"
             assert encoded_sentence == [2] + text + [3]
             assert encoded_pair == [2] + text + [3] + text_a + [3]
@custom_tokenizers
class  SCREAMING_SNAKE_CASE__  (				unittest.TestCase			):
      def        SCREAMING_SNAKE_CASE							(       self				:       str     )		->			List[str]:
             a_					:		int							=  'cl-tohoku/bert-base-japanese'
             a_					:		Optional[Any]							=  AutoTokenizer.from_pretrained(SCREAMING_SNAKE_CASE__     )
             self.assertIsInstance(SCREAMING_SNAKE_CASE__     ,							SCREAMING_SNAKE_CASE__     )
class  SCREAMING_SNAKE_CASE__  (				unittest.TestCase			):
      def        SCREAMING_SNAKE_CASE							(       self				:       str     )		->			Optional[Any]:
             a_					:		List[str]							=  'cl-tohoku/bert-base-japanese'
             with self.assertLogs('transformers'     ,							level='WARNING'     ) as cm:
                    BertTokenizer.from_pretrained(SCREAMING_SNAKE_CASE__     )
                    self.assertTrue(
                        cm.records[0].message.startswith(
                            'The tokenizer class you load from this checkpoint is not the same type as the class this function'
                            ' is called from.'     )     )
             a_					:		Tuple							=  'bert-base-cased'
             with self.assertLogs('transformers'     ,							level='WARNING'     ) as cm:
                    BertJapaneseTokenizer.from_pretrained(SCREAMING_SNAKE_CASE__     )
                    self.assertTrue(
                        cm.records[0].message.startswith(
                            'The tokenizer class you load from this checkpoint is not the same type as the class this function'
                            ' is called from.'     )     )






import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
   import torch

   from transformers import (
       OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
       OpenAIGPTConfig,
       OpenAIGPTDoubleHeadsModel,
       OpenAIGPTForSequenceClassification,
       OpenAIGPTLMHeadModel,
       OpenAIGPTModel,
   )
class  SCREAMING_SNAKE_CASE__  :
      def __init__(       self				:       List[str]     ,							SCREAMING_SNAKE_CASE__				:       Dict     ,							SCREAMING_SNAKE_CASE__				:       Tuple=1_3     ,							SCREAMING_SNAKE_CASE__				:       str=7     ,							SCREAMING_SNAKE_CASE__				:       List[str]=True     ,							SCREAMING_SNAKE_CASE__				:       Optional[Any]=True     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]=True     ,							SCREAMING_SNAKE_CASE__				:       Optional[Any]=9_9     ,							SCREAMING_SNAKE_CASE__				:       Optional[Any]=3_2     ,							SCREAMING_SNAKE_CASE__				:       List[str]=5     ,							SCREAMING_SNAKE_CASE__				:       List[Any]=4     ,							SCREAMING_SNAKE_CASE__				:       Tuple=3_7     ,							SCREAMING_SNAKE_CASE__				:       Any="gelu"     ,							SCREAMING_SNAKE_CASE__				:       Dict=0.1     ,							SCREAMING_SNAKE_CASE__				:       Dict=0.1     ,							SCREAMING_SNAKE_CASE__				:       int=5_1_2     ,							SCREAMING_SNAKE_CASE__				:       int=1_6     ,							SCREAMING_SNAKE_CASE__				:       Optional[int]=2     ,							SCREAMING_SNAKE_CASE__				:       Any=0.02     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]=3     ,							SCREAMING_SNAKE_CASE__				:       Optional[Any]=4     ,							SCREAMING_SNAKE_CASE__				:       Optional[int]=None     ,							)		->			Any:
             a_					:		Tuple							=  parent
             a_					:		int							=  batch_size
             a_					:		Tuple							=  seq_length
             a_					:		List[Any]							=  is_training
             a_					:		List[str]							=  use_token_type_ids
             a_					:		Dict							=  use_labels
             a_					:		Any							=  vocab_size
             a_					:		List[str]							=  hidden_size
             a_					:		Tuple							=  num_hidden_layers
             a_					:		List[Any]							=  num_attention_heads
             a_					:		Dict							=  intermediate_size
             a_					:		Any							=  hidden_act
             a_					:		List[str]							=  hidden_dropout_prob
             a_					:		Tuple							=  attention_probs_dropout_prob
             a_					:		Optional[Any]							=  max_position_embeddings
             a_					:		List[Any]							=  type_vocab_size
             a_					:		int							=  type_sequence_label_size
             a_					:		List[Any]							=  initializer_range
             a_					:		List[str]							=  num_labels
             a_					:		Union[str, Any]							=  num_choices
             a_					:		str							=  scope
             a_					:		Tuple							=  self.vocab_size - 1
      def        SCREAMING_SNAKE_CASE							(       self				:       Union[str, Any]     )		->			Any:
             a_					:		Optional[int]							=  ids_tensor([self.batch_size, self.seq_length]     ,							self.vocab_size     )

             a_					:		Any							=  None
             if self.use_token_type_ids:
                    a_					:		Tuple							=  ids_tensor([self.batch_size, self.seq_length]     ,							self.type_vocab_size     )

             a_					:		List[Any]							=  None
             a_					:		Union[str, Any]							=  None
             a_					:		List[Any]							=  None
             if self.use_labels:
                    a_					:		Tuple							=  ids_tensor([self.batch_size]     ,							self.type_sequence_label_size     )
                    a_					:		Any							=  ids_tensor([self.batch_size, self.seq_length]     ,							self.num_labels     )
                    a_					:		List[Any]							=  ids_tensor([self.batch_size]     ,							self.num_choices     )

             a_					:		Union[str, Any]							=  OpenAIGPTConfig(
                 vocab_size=self.vocab_size     ,							n_embd=self.hidden_size     ,							n_layer=self.num_hidden_layers     ,							n_head=self.num_attention_heads     ,							n_positions=self.max_position_embeddings     ,							pad_token_id=self.pad_token_id     ,							)

             a_					:		List[str]							=  ids_tensor([self.num_hidden_layers, self.num_attention_heads]     ,							2     )

             return (
                 config,
                 input_ids,
                 head_mask,
                 token_type_ids,
                 sequence_labels,
                 token_labels,
                 choice_labels,
             )
      def        SCREAMING_SNAKE_CASE							(       self				:       List[str]     ,							SCREAMING_SNAKE_CASE__				:       Any     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]     ,							SCREAMING_SNAKE_CASE__				:       Any     ,							SCREAMING_SNAKE_CASE__				:       List[str]     ,							*SCREAMING_SNAKE_CASE__				:       Tuple     )		->			Union[str, Any]:
             a_					:		Dict							=  OpenAIGPTModel(config=SCREAMING_SNAKE_CASE__     )
             model.to(SCREAMING_SNAKE_CASE__     )
             model.eval()

             a_					:		str							=  model(SCREAMING_SNAKE_CASE__     ,							token_type_ids=SCREAMING_SNAKE_CASE__     ,							head_mask=SCREAMING_SNAKE_CASE__     )
             a_					:		Dict							=  model(SCREAMING_SNAKE_CASE__     ,							token_type_ids=SCREAMING_SNAKE_CASE__     )
             a_					:		Dict							=  model(SCREAMING_SNAKE_CASE__     )

             self.parent.assertEqual(result.last_hidden_state.shape     ,							(self.batch_size, self.seq_length, self.hidden_size)     )
      def        SCREAMING_SNAKE_CASE							(       self				:       Optional[int]     ,							SCREAMING_SNAKE_CASE__				:       List[str]     ,							SCREAMING_SNAKE_CASE__				:       Tuple     ,							SCREAMING_SNAKE_CASE__				:       Tuple     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]     ,							*SCREAMING_SNAKE_CASE__				:       Optional[int]     )		->			Any:
             a_					:		str							=  OpenAIGPTLMHeadModel(SCREAMING_SNAKE_CASE__     )
             model.to(SCREAMING_SNAKE_CASE__     )
             model.eval()

             a_					:		Optional[int]							=  model(SCREAMING_SNAKE_CASE__     ,							token_type_ids=SCREAMING_SNAKE_CASE__     ,							labels=SCREAMING_SNAKE_CASE__     )
             self.parent.assertEqual(result.loss.shape     ,							()     )
             self.parent.assertEqual(result.logits.shape     ,							(self.batch_size, self.seq_length, self.vocab_size)     )
      def        SCREAMING_SNAKE_CASE							(       self				:       Optional[Any]     ,							SCREAMING_SNAKE_CASE__				:       Optional[Any]     ,							SCREAMING_SNAKE_CASE__				:       int     ,							SCREAMING_SNAKE_CASE__				:       Optional[int]     ,							SCREAMING_SNAKE_CASE__				:       Optional[Any]     ,							*SCREAMING_SNAKE_CASE__				:       Union[str, Any]     )		->			Dict:
             a_					:		int							=  OpenAIGPTDoubleHeadsModel(SCREAMING_SNAKE_CASE__     )
             model.to(SCREAMING_SNAKE_CASE__     )
             model.eval()

             a_					:		str							=  model(SCREAMING_SNAKE_CASE__     ,							token_type_ids=SCREAMING_SNAKE_CASE__     ,							labels=SCREAMING_SNAKE_CASE__     )
             self.parent.assertEqual(result.loss.shape     ,							()     )
             self.parent.assertEqual(result.logits.shape     ,							(self.batch_size, self.seq_length, self.vocab_size)     )
      def        SCREAMING_SNAKE_CASE							(       self				:       Tuple     ,							SCREAMING_SNAKE_CASE__				:       Dict     ,							SCREAMING_SNAKE_CASE__				:       Dict     ,							SCREAMING_SNAKE_CASE__				:       List[Any]     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]     ,							*SCREAMING_SNAKE_CASE__				:       str     )		->			List[str]:
             a_					:		Any							=  self.num_labels
             a_					:		Dict							=  OpenAIGPTForSequenceClassification(SCREAMING_SNAKE_CASE__     )
             model.to(SCREAMING_SNAKE_CASE__     )
             model.eval()

             a_					:		Dict							=  ids_tensor([self.batch_size]     ,							self.type_sequence_label_size     )
             a_					:		Any							=  model(SCREAMING_SNAKE_CASE__     ,							token_type_ids=SCREAMING_SNAKE_CASE__     ,							labels=SCREAMING_SNAKE_CASE__     )
             self.parent.assertEqual(result.logits.shape     ,							(self.batch_size, self.num_labels)     )

      def        SCREAMING_SNAKE_CASE							(       self				:       Union[str, Any]     )		->			Tuple:
             a_					:		Optional[Any]							=  self.prepare_config_and_inputs()
             (
                 (
                 a_
             )							,		(
                 a_
             )							,		(
                 a_
             )							,		(
                 a_
             )							,		(
                 a_
             )							,		(
                 a_
             )							,		(
                 a_
             )							,		
             )					:		Optional[Any]							=  config_and_inputs
             a_					:		Optional[int]							=  {
                 'input_ids': input_ids,
                 'token_type_ids': token_type_ids,
                 'head_mask': head_mask,
             }

             return config, inputs_dict
@require_torch
class  SCREAMING_SNAKE_CASE__  (				lowercase__						,    lowercase__						,    lowercase__						,    unittest.TestCase			):
      snake_case__				:     Tuple        =						(
          (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
          if is_torch_available()
          else ()
      )
      snake_case__				:     List[str]        =						(
          (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
      )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
      snake_case__				:     Dict        =						(
          {
              '''feature-extraction''': OpenAIGPTModel,
              '''text-classification''': OpenAIGPTForSequenceClassification,
              '''text-generation''': OpenAIGPTLMHeadModel,
              '''zero-shot''': OpenAIGPTForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      def        SCREAMING_SNAKE_CASE							(       self				:       List[str]     ,							SCREAMING_SNAKE_CASE__				:       Tuple     ,							SCREAMING_SNAKE_CASE__				:       Optional[Any]     ,							SCREAMING_SNAKE_CASE__				:       Tuple     ,							SCREAMING_SNAKE_CASE__				:       Dict     ,							SCREAMING_SNAKE_CASE__				:       List[str]     )		->			Dict:
             if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests":
                    # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers.
                    # `OpenAIGPTConfig` was never used in pipeline tests, either because of a missing checkpoint or because a
                    # tiny config could not be created.
                    return True

             return False
      def        SCREAMING_SNAKE_CASE							(       self				:       int     ,							SCREAMING_SNAKE_CASE__				:       Dict     ,							SCREAMING_SNAKE_CASE__				:       Dict     ,							SCREAMING_SNAKE_CASE__				:       Any=False     )		->			List[str]:
             a_					:		str							=  super()._prepare_for_class(SCREAMING_SNAKE_CASE__     ,							SCREAMING_SNAKE_CASE__     ,							return_labels=SCREAMING_SNAKE_CASE__     )

             if return_labels:
                    if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
                           a_					:		Optional[Any]							=  torch.zeros(
                               (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length)     ,							dtype=torch.long     ,							device=SCREAMING_SNAKE_CASE__     ,							)
                           a_					:		str							=  inputs_dict['labels']
                           a_					:		Optional[int]							=  inputs_dict['labels']
                           a_					:		Optional[int]							=  torch.zeros(
                               (self.model_tester.batch_size, self.model_tester.num_choices)     ,							dtype=torch.long     ,							device=SCREAMING_SNAKE_CASE__     ,							)
                           a_					:		Union[str, Any]							=  torch.zeros(
                               self.model_tester.batch_size     ,							dtype=torch.long     ,							device=SCREAMING_SNAKE_CASE__     )
             return inputs_dict
      def        SCREAMING_SNAKE_CASE							(       self				:       str     )		->			List[Any]:
             a_					:		str							=  OpenAIGPTModelTester(self     )
             a_					:		int							=  ConfigTester(self     ,							config_class=SCREAMING_SNAKE_CASE__     ,							n_embd=3_7     )
      def        SCREAMING_SNAKE_CASE							(       self				:       Tuple     )		->			Tuple:
             self.config_tester.run_common_tests()
      def        SCREAMING_SNAKE_CASE							(       self				:       List[Any]     )		->			Tuple:
             a_					:		Dict							=  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_openai_gpt_model(*SCREAMING_SNAKE_CASE__     )
      def        SCREAMING_SNAKE_CASE							(       self				:       Optional[int]     )		->			Tuple:
             a_					:		Any							=  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_lm_head_model(*SCREAMING_SNAKE_CASE__     )
      def        SCREAMING_SNAKE_CASE							(       self				:       Any     )		->			Optional[Any]:
             a_					:		Optional[int]							=  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_double_lm_head_model(*SCREAMING_SNAKE_CASE__     )
      def        SCREAMING_SNAKE_CASE							(       self				:       Optional[Any]     )		->			Optional[Any]:
             a_					:		int							=  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*SCREAMING_SNAKE_CASE__     )

      @slow
      def        SCREAMING_SNAKE_CASE							(       self				:       List[str]     )		->			str:
             for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                    a_					:		str							=  OpenAIGPTModel.from_pretrained(SCREAMING_SNAKE_CASE__     )
                    self.assertIsNotNone(SCREAMING_SNAKE_CASE__     )
@require_torch
class  SCREAMING_SNAKE_CASE__  (				unittest.TestCase			):
      @slow
      def        SCREAMING_SNAKE_CASE							(       self				:       Dict     )		->			int:
             a_					:		Dict							=  OpenAIGPTLMHeadModel.from_pretrained('openai-gpt'     )
             model.to(SCREAMING_SNAKE_CASE__     )
             a_					:		List[Any]							=  torch.tensor([[4_8_1, 4_7_3_5, 5_4_4]]     ,							dtype=torch.long     ,							device=SCREAMING_SNAKE_CASE__     )  # the president is
             a_					:		Tuple							=  [
                 4_8_1,
                 4_7_3_5,
                 5_4_4,
                 2_4_6,
                 9_6_3,
                 8_7_0,
                 7_6_2,
                 2_3_9,
                 2_4_4,
                 4_0_4_7_7,
                 2_4_4,
                 2_4_9,
                 7_1_9,
                 8_8_1,
                 4_8_7,
                 5_4_4,
                 2_4_0,
                 2_4_4,
                 6_0_3,
                 4_8_1,
             ]  # the president is a very good man. " \n " i\'m sure he is, " said the

             a_					:		Dict							=  model.generate(SCREAMING_SNAKE_CASE__     ,							do_sample=SCREAMING_SNAKE_CASE__     )
             self.assertListEqual(output_ids[0].tolist()     ,							SCREAMING_SNAKE_CASE__     )


