



"""simple docstring"""







import os
import unittest

from transformers import RealmTokenizerFast
from transformers.models.bert.tokenization_bert import (
    VOCAB_FILES_NAMES,
    BasicTokenizer,
    WordpieceTokenizer,
    _is_control,
    _is_punctuation,
    _is_whitespace,
)
from transformers.models.realm.tokenization_realm import RealmTokenizer
from transformers.testing_utils import require_tokenizers, slow

from ...test_tokenization_common import TokenizerTesterMixin, filter_non_english







@require_tokenizers
class  UpperCamelCase__  (      lowercase_,   unittest.TestCase    ):




      """simple docstring"""




      _SCREAMING_SNAKE_CASE    							=	RealmTokenizer
      _SCREAMING_SNAKE_CASE    							=	RealmTokenizerFast
      _SCREAMING_SNAKE_CASE    							=	True
      _SCREAMING_SNAKE_CASE    							=	True
      _SCREAMING_SNAKE_CASE    							=	filter_non_english



      def 	SCREAMING_SNAKE_CASE__					( self							: List[Any]			):
        super().setUp()

        lowerCAmelCase_  :							Any								=					[
            '[UNK]',
            '[CLS]',
            '[SEP]',
            '[PAD]',
            '[MASK]',
            'want',
            '##want',
            '##ed',
            'wa',
            'un',
            'runn',
            '##ing',
            ',',
            'low',
            'lowest',
        ]
        lowerCAmelCase_  :							Optional[Any]								=					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							: Optional[int]       ,     SCREAMING_SNAKE_CASE_							: Tuple			):
        lowerCAmelCase_  :							Optional[int]								=					'UNwant\u00E9d,running'
        lowerCAmelCase_  :							Optional[int]								=					'unwanted, running'
        return input_text, output_text



      def 	SCREAMING_SNAKE_CASE__					( self							: List[Any]			):
        lowerCAmelCase_  :							Any								=					self.tokenizer_class(self.vocab_file			)

        lowerCAmelCase_  :							List[Any]								=					tokenizer.tokenize('UNwant\u00E9d,running'			)
        self.assertListEqual(SCREAMING_SNAKE_CASE_       ,     ['un', '##want', '##ed', ',', 'runn', '##ing']			)
        self.assertListEqual(tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE_			)       ,     [9, 6, 7, 1_2, 1_0, 1_1]			)



      def 	SCREAMING_SNAKE_CASE__					( self							: int			):
        if not self.test_rust_tokenizer:
          return

        lowerCAmelCase_  :							Any								=					self.get_tokenizer()
        lowerCAmelCase_  :							List[str]								=					self.get_rust_tokenizer()

        lowerCAmelCase_  :							List[Any]								=					'UNwant\u00E9d,running'

        lowerCAmelCase_  :							List[str]								=					tokenizer.tokenize(SCREAMING_SNAKE_CASE_			)
        lowerCAmelCase_  :							Optional[int]								=					rust_tokenizer.tokenize(SCREAMING_SNAKE_CASE_			)
        self.assertListEqual(SCREAMING_SNAKE_CASE_       ,     SCREAMING_SNAKE_CASE_			)

        lowerCAmelCase_  :							List[Any]								=					tokenizer.encode(SCREAMING_SNAKE_CASE_       ,     add_special_tokens=SCREAMING_SNAKE_CASE_			)
        lowerCAmelCase_  :							int								=					rust_tokenizer.encode(SCREAMING_SNAKE_CASE_       ,     add_special_tokens=SCREAMING_SNAKE_CASE_			)
        self.assertListEqual(SCREAMING_SNAKE_CASE_       ,     SCREAMING_SNAKE_CASE_			)

        lowerCAmelCase_  :							int								=					self.get_rust_tokenizer()
        lowerCAmelCase_  :							str								=					tokenizer.encode(SCREAMING_SNAKE_CASE_			)
        lowerCAmelCase_  :							Optional[int]								=					rust_tokenizer.encode(SCREAMING_SNAKE_CASE_			)
        self.assertListEqual(SCREAMING_SNAKE_CASE_       ,     SCREAMING_SNAKE_CASE_			)

        # With lower casing
        lowerCAmelCase_  :							List[str]								=					self.get_tokenizer(do_lower_case=SCREAMING_SNAKE_CASE_			)
        lowerCAmelCase_  :							Dict								=					self.get_rust_tokenizer(do_lower_case=SCREAMING_SNAKE_CASE_			)

        lowerCAmelCase_  :							List[str]								=					'UNwant\u00E9d,running'

        lowerCAmelCase_  :							str								=					tokenizer.tokenize(SCREAMING_SNAKE_CASE_			)
        lowerCAmelCase_  :							Optional[int]								=					rust_tokenizer.tokenize(SCREAMING_SNAKE_CASE_			)
        self.assertListEqual(SCREAMING_SNAKE_CASE_       ,     SCREAMING_SNAKE_CASE_			)

        lowerCAmelCase_  :							Tuple								=					tokenizer.encode(SCREAMING_SNAKE_CASE_       ,     add_special_tokens=SCREAMING_SNAKE_CASE_			)
        lowerCAmelCase_  :							List[str]								=					rust_tokenizer.encode(SCREAMING_SNAKE_CASE_       ,     add_special_tokens=SCREAMING_SNAKE_CASE_			)
        self.assertListEqual(SCREAMING_SNAKE_CASE_       ,     SCREAMING_SNAKE_CASE_			)

        lowerCAmelCase_  :							Dict								=					self.get_rust_tokenizer()
        lowerCAmelCase_  :							List[str]								=					tokenizer.encode(SCREAMING_SNAKE_CASE_			)
        lowerCAmelCase_  :							Union[str, Any]								=					rust_tokenizer.encode(SCREAMING_SNAKE_CASE_			)
        self.assertListEqual(SCREAMING_SNAKE_CASE_       ,     SCREAMING_SNAKE_CASE_			)



      def 	SCREAMING_SNAKE_CASE__					( self							: Optional[Any]			):
        lowerCAmelCase_  :							Optional[int]								=					BasicTokenizer()

        self.assertListEqual(tokenizer.tokenize('ah\u535A\u63A8zz'			)       ,     ['ah', '\u535A', '\u63A8', 'zz']			)



      def 	SCREAMING_SNAKE_CASE__					( self							: str			):
        lowerCAmelCase_  :							int								=					BasicTokenizer(do_lower_case=SCREAMING_SNAKE_CASE_			)

        self.assertListEqual(
            tokenizer.tokenize(' \tHeLLo!how  \n Are yoU?  '			)       ,     ['hello', '!', 'how', 'are', 'you', '?']			)
        self.assertListEqual(tokenizer.tokenize('H\u00E9llo'			)       ,     ['hello']			)



      def 	SCREAMING_SNAKE_CASE__					( self							: Optional[Any]			):
        lowerCAmelCase_  :							Optional[int]								=					BasicTokenizer(do_lower_case=SCREAMING_SNAKE_CASE_       ,     strip_accents=SCREAMING_SNAKE_CASE_			)

        self.assertListEqual(
            tokenizer.tokenize(' \tHäLLo!how  \n Are yoU?  '			)       ,     ['hällo', '!', 'how', 'are', 'you', '?']			)
        self.assertListEqual(tokenizer.tokenize('H\u00E9llo'			)       ,     ['h\u00E9llo']			)



      def 	SCREAMING_SNAKE_CASE__					( self							: Optional[Any]			):
        lowerCAmelCase_  :							int								=					BasicTokenizer(do_lower_case=SCREAMING_SNAKE_CASE_       ,     strip_accents=SCREAMING_SNAKE_CASE_			)

        self.assertListEqual(
            tokenizer.tokenize(' \tHäLLo!how  \n Are yoU?  '			)       ,     ['hallo', '!', 'how', 'are', 'you', '?']			)
        self.assertListEqual(tokenizer.tokenize('H\u00E9llo'			)       ,     ['hello']			)



      def 	SCREAMING_SNAKE_CASE__					( self							: Dict			):
        lowerCAmelCase_  :							str								=					BasicTokenizer(do_lower_case=SCREAMING_SNAKE_CASE_			)

        self.assertListEqual(
            tokenizer.tokenize(' \tHäLLo!how  \n Are yoU?  '			)       ,     ['hallo', '!', 'how', 'are', 'you', '?']			)
        self.assertListEqual(tokenizer.tokenize('H\u00E9llo'			)       ,     ['hello']			)



      def 	SCREAMING_SNAKE_CASE__					( self							: List[Any]			):
        lowerCAmelCase_  :							int								=					BasicTokenizer(do_lower_case=SCREAMING_SNAKE_CASE_			)

        self.assertListEqual(
            tokenizer.tokenize(' \tHeLLo!how  \n Are yoU?  '			)       ,     ['HeLLo', '!', 'how', 'Are', 'yoU', '?']			)



      def 	SCREAMING_SNAKE_CASE__					( self							: Dict			):
        lowerCAmelCase_  :							Union[str, Any]								=					BasicTokenizer(do_lower_case=SCREAMING_SNAKE_CASE_       ,     strip_accents=SCREAMING_SNAKE_CASE_			)

        self.assertListEqual(
            tokenizer.tokenize(' \tHäLLo!how  \n Are yoU?  '			)       ,     ['HäLLo', '!', 'how', 'Are', 'yoU', '?']			)



      def 	SCREAMING_SNAKE_CASE__					( self							: Any			):
        lowerCAmelCase_  :							Optional[Any]								=					BasicTokenizer(do_lower_case=SCREAMING_SNAKE_CASE_       ,     strip_accents=SCREAMING_SNAKE_CASE_			)

        self.assertListEqual(
            tokenizer.tokenize(' \tHäLLo!how  \n Are yoU?  '			)       ,     ['HaLLo', '!', 'how', 'Are', 'yoU', '?']			)



      def 	SCREAMING_SNAKE_CASE__					( self							: str			):
        lowerCAmelCase_  :							Any								=					BasicTokenizer(do_lower_case=SCREAMING_SNAKE_CASE_       ,     never_split=['[UNK]']			)

        self.assertListEqual(
            tokenizer.tokenize(' \tHeLLo!how  \n Are yoU? [UNK]'			)       ,     ['HeLLo', '!', 'how', 'Are', 'yoU', '?', '[UNK]']			)



      def 	SCREAMING_SNAKE_CASE__					( self							: str			):
        lowerCAmelCase_  :							str								=					['[UNK]', '[CLS]', '[SEP]', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing']

        lowerCAmelCase_  :							Any								=					{}
        for i, token in enumerate(SCREAMING_SNAKE_CASE_			):
          lowerCAmelCase_  :							Any								=					i
        lowerCAmelCase_  :							Dict								=					WordpieceTokenizer(vocab=SCREAMING_SNAKE_CASE_       ,     unk_token='[UNK]'			)

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

        self.assertListEqual(tokenizer.tokenize('unwanted running'			)       ,     ['un', '##want', '##ed', 'runn', '##ing']			)

        self.assertListEqual(tokenizer.tokenize('unwantedX running'			)       ,     ['[UNK]', 'runn', '##ing']			)



      def 	SCREAMING_SNAKE_CASE__					( self							: Optional[Any]			):
        self.assertTrue(_is_whitespace(' '			)			)
        self.assertTrue(_is_whitespace('\t'			)			)
        self.assertTrue(_is_whitespace('\r'			)			)
        self.assertTrue(_is_whitespace('\n'			)			)
        self.assertTrue(_is_whitespace('\u00A0'			)			)

        self.assertFalse(_is_whitespace('A'			)			)
        self.assertFalse(_is_whitespace('-'			)			)



      def 	SCREAMING_SNAKE_CASE__					( self							: List[Any]			):
        self.assertTrue(_is_control('\u0005'			)			)

        self.assertFalse(_is_control('A'			)			)
        self.assertFalse(_is_control(' '			)			)
        self.assertFalse(_is_control('\t'			)			)
        self.assertFalse(_is_control('\r'			)			)



      def 	SCREAMING_SNAKE_CASE__					( self							: Tuple			):
        self.assertTrue(_is_punctuation('-'			)			)
        self.assertTrue(_is_punctuation('$'			)			)
        self.assertTrue(_is_punctuation('`'			)			)
        self.assertTrue(_is_punctuation('.'			)			)

        self.assertFalse(_is_punctuation('A'			)			)
        self.assertFalse(_is_punctuation(' '			)			)



      def 	SCREAMING_SNAKE_CASE__					( self							: int			):
        lowerCAmelCase_  :							Dict								=					self.get_tokenizer()

        # Example taken from the issue https://github.com/huggingface/tokenizers/issues/340
        self.assertListEqual([tokenizer.tokenize(SCREAMING_SNAKE_CASE_			) for t in ['Test', '\xad', 'test']]       ,     [['[UNK]'], [], ['[UNK]']]			)

        if self.test_rust_tokenizer:
          lowerCAmelCase_  :							int								=					self.get_rust_tokenizer()
          self.assertListEqual(
              [rust_tokenizer.tokenize(SCREAMING_SNAKE_CASE_			) for t in ['Test', '\xad', 'test']]       ,     [['[UNK]'], [], ['[UNK]']]			)



      @slow
      def 	SCREAMING_SNAKE_CASE__					( self							: Any			):
        lowerCAmelCase_  :							str								=					self.tokenizer_class.from_pretrained('bert-base-uncased'			)

        lowerCAmelCase_  :							Union[str, Any]								=					tokenizer.encode('sequence builders'       ,     add_special_tokens=SCREAMING_SNAKE_CASE_			)
        lowerCAmelCase_  :							Union[str, Any]								=					tokenizer.encode('multi-sequence build'       ,     add_special_tokens=SCREAMING_SNAKE_CASE_			)

        lowerCAmelCase_  :							str								=					tokenizer.build_inputs_with_special_tokens(SCREAMING_SNAKE_CASE_			)
        lowerCAmelCase_  :							Dict								=					tokenizer.build_inputs_with_special_tokens(SCREAMING_SNAKE_CASE_       ,     SCREAMING_SNAKE_CASE_			)

        assert encoded_sentence == [1_0_1] + text + [1_0_2]
        assert encoded_pair == [1_0_1] + text + [1_0_2] + text_a + [1_0_2]



      def 	SCREAMING_SNAKE_CASE__					( self							: List[Any]			):
        for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
          with self.subTest(F"{tokenizer.__class__.__name__} ({pretrained_name})"			):
            lowerCAmelCase_  :							Union[str, Any]								=					self.rust_tokenizer_class.from_pretrained(SCREAMING_SNAKE_CASE_       ,     **SCREAMING_SNAKE_CASE_			)

            lowerCAmelCase_  :							List[Any]								=					F"A, naïve {tokenizer_r.mask_token} AllenNLP sentence."
            lowerCAmelCase_  :							Optional[Any]								=					tokenizer_r.encode_plus(
                SCREAMING_SNAKE_CASE_       ,     return_attention_mask=SCREAMING_SNAKE_CASE_       ,     return_token_type_ids=SCREAMING_SNAKE_CASE_       ,     return_offsets_mapping=SCREAMING_SNAKE_CASE_       ,     add_special_tokens=SCREAMING_SNAKE_CASE_       ,     )

            lowerCAmelCase_  :							Tuple								=					tokenizer_r.do_lower_case if hasattr(SCREAMING_SNAKE_CASE_       ,     'do_lower_case'			) else False
            lowerCAmelCase_  :							List[str]								=					(
                [
                    ((0, 0), tokenizer_r.cls_token),
                    ((0, 1), 'A'),
                    ((1, 2), ','),
                    ((3, 5), 'na'),
                    ((5, 6), '##ï'),
                    ((6, 8), '##ve'),
                    ((9, 1_5), tokenizer_r.mask_token),
                    ((1_6, 2_1), 'Allen'),
                    ((2_1, 2_3), '##NL'),
                    ((2_3, 2_4), '##P'),
                    ((2_5, 3_3), 'sentence'),
                    ((3_3, 3_4), '.'),
                    ((0, 0), tokenizer_r.sep_token),
                ]
                if not do_lower_case
                else [
                    ((0, 0), tokenizer_r.cls_token),
                    ((0, 1), 'a'),
                    ((1, 2), ','),
                    ((3, 8), 'naive'),
                    ((9, 1_5), tokenizer_r.mask_token),
                    ((1_6, 2_1), 'allen'),
                    ((2_1, 2_3), '##nl'),
                    ((2_3, 2_4), '##p'),
                    ((2_5, 3_3), 'sentence'),
                    ((3_3, 3_4), '.'),
                    ((0, 0), tokenizer_r.sep_token),
                ]
            )

            self.assertEqual(
                [e[1] for e in expected_results]       ,     tokenizer_r.convert_ids_to_tokens(tokens['input_ids']			)			)
            self.assertEqual([e[0] for e in expected_results]       ,     tokens['offset_mapping']			)





      @slow
      def 	SCREAMING_SNAKE_CASE__					( self							: Dict			):
        for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
          with self.subTest(F"{tokenizer.__class__.__name__} ({pretrained_name})"			):
            lowerCAmelCase_  :							Optional[Any]								=					self.rust_tokenizer_class.from_pretrained(SCREAMING_SNAKE_CASE_       ,     **SCREAMING_SNAKE_CASE_			)
            lowerCAmelCase_  :							Any								=					self.tokenizer_class.from_pretrained(SCREAMING_SNAKE_CASE_       ,     **SCREAMING_SNAKE_CASE_			)
            lowerCAmelCase_  :							Optional[int]								=					[['Hello world!', 'Nice to meet you!'], ['The cute cat.', 'The adorable dog.']]

            lowerCAmelCase_  :							str								=					tokenizer_r.batch_encode_candidates(SCREAMING_SNAKE_CASE_       ,     max_length=1_0       ,     return_tensors='np'			)
            lowerCAmelCase_  :							str								=					tokenizer_p.batch_encode_candidates(SCREAMING_SNAKE_CASE_       ,     max_length=1_0       ,     return_tensors='np'			)

            lowerCAmelCase_  :							Optional[int]								=					(2, 2, 1_0)

            self.assertEqual(encoded_sentence_r['input_ids'].shape       ,     SCREAMING_SNAKE_CASE_			)
            self.assertEqual(encoded_sentence_r['attention_mask'].shape       ,     SCREAMING_SNAKE_CASE_			)
            self.assertEqual(encoded_sentence_r['token_type_ids'].shape       ,     SCREAMING_SNAKE_CASE_			)

            self.assertEqual(encoded_sentence_p['input_ids'].shape       ,     SCREAMING_SNAKE_CASE_			)
            self.assertEqual(encoded_sentence_p['attention_mask'].shape       ,     SCREAMING_SNAKE_CASE_			)
            self.assertEqual(encoded_sentence_p['token_type_ids'].shape       ,     SCREAMING_SNAKE_CASE_			)










"""simple docstring"""







import inspect

from diffusers.utils import is_flax_available
from diffusers.utils.testing_utils import require_flax


if is_flax_available():
     import jax







@require_flax
class  UpperCamelCase__  :




      """simple docstring"""




      def 	SCREAMING_SNAKE_CASE__					( self							: Optional[int]			):
        lowerCAmelCase_		,lowerCAmelCase_  :							Any								=					self.prepare_init_args_and_inputs_for_common()

        lowerCAmelCase_  :							List[str]								=					self.model_class(**SCREAMING_SNAKE_CASE_			)
        lowerCAmelCase_  :							Any								=					model.init(inputs_dict['prng_key']       ,     inputs_dict['sample']			)
        jax.lax.stop_gradient(SCREAMING_SNAKE_CASE_			)

        lowerCAmelCase_  :							Tuple								=					model.apply(SCREAMING_SNAKE_CASE_       ,     inputs_dict['sample']			)

        if isinstance(SCREAMING_SNAKE_CASE_       ,     SCREAMING_SNAKE_CASE_			):
          lowerCAmelCase_  :							str								=					output.sample

        self.assertIsNotNone(SCREAMING_SNAKE_CASE_			)
        lowerCAmelCase_  :							Any								=					inputs_dict['sample'].shape
        self.assertEqual(output.shape       ,     SCREAMING_SNAKE_CASE_       ,     'Input and output shapes do not match'			)



      def 	SCREAMING_SNAKE_CASE__					( self							: List[str]			):
        lowerCAmelCase_		,lowerCAmelCase_  :							Union[str, Any]								=					self.prepare_init_args_and_inputs_for_common()

        lowerCAmelCase_  :							Tuple								=					1_6
        lowerCAmelCase_  :							Dict								=					(1_6, 3_2)

        lowerCAmelCase_  :							Any								=					self.model_class(**SCREAMING_SNAKE_CASE_			)
        lowerCAmelCase_  :							Optional[Any]								=					model.init(inputs_dict['prng_key']       ,     inputs_dict['sample']			)
        jax.lax.stop_gradient(SCREAMING_SNAKE_CASE_			)

        lowerCAmelCase_  :							Optional[Any]								=					model.apply(SCREAMING_SNAKE_CASE_       ,     inputs_dict['sample']			)

        if isinstance(SCREAMING_SNAKE_CASE_       ,     SCREAMING_SNAKE_CASE_			):
          lowerCAmelCase_  :							Dict								=					output.sample

        self.assertIsNotNone(SCREAMING_SNAKE_CASE_			)
        lowerCAmelCase_  :							Optional[Any]								=					inputs_dict['sample'].shape
        self.assertEqual(output.shape       ,     SCREAMING_SNAKE_CASE_       ,     'Input and output shapes do not match'			)





      def 	SCREAMING_SNAKE_CASE__					( self							: List[Any]			):
        lowerCAmelCase_  :							List[str]								=					'kwargs' in inspect.signature(self.model_class.__init__			).parameters
        lowerCAmelCase_  :							Optional[Any]								=					len(self.model_class._deprecated_kwargs			) > 0

        if has_kwarg_in_model_class and not has_deprecated_kwarg:
          raise ValueError(
              F"{self.model_class} has `**kwargs` in its __init__ method but has not defined any deprecated kwargs"
              ' under the `_deprecated_kwargs` class attribute. Make sure to either remove `**kwargs` if there are'
              ' no deprecated arguments or add the deprecated argument with `_deprecated_kwargs ='
              ' [<deprecated_argument>]`'			)

        if not has_kwarg_in_model_class and has_deprecated_kwarg:
          raise ValueError(
              F"{self.model_class} doesn't have `**kwargs` in its __init__ method but has defined deprecated kwargs"
              ' under the `_deprecated_kwargs` class attribute. Make sure to either add the `**kwargs` argument to'
              F" {self.model_class}.__init__ if there are deprecated arguments or remove the deprecated argument"
              ' from `_deprecated_kwargs = [<deprecated_argument>]`'			)





