






from __future__ import annotations

import inspect
import unittest

import numpy as np

from transformers import DeiTConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
     import tensorflow as tf

     from transformers import (
         TFDeiTForImageClassification,
         TFDeiTForImageClassificationWithTeacher,
         TFDeiTForMaskedImageModeling,
         TFDeiTModel,
     )
     from transformers.models.deit.modeling_tf_deit import TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
     from PIL import Image

     from transformers import DeiTImageProcessor




class 			snake_case__   :

       '''simple docstring'''



       def __init__(							self			,						a__			,						a__=13			,						a__=30			,						a__=2			,						a__=3			,						a__=True			,						a__=True			,						a__=32			,						a__=2			,						a__=4			,						a__=37			,						a__="gelu"			,						a__=0.1			,						a__=0.1			,						a__=10			,						a__=0.02			,						a__=3			,						a__=None			,						a__=2			,						) ->      str:






            '''simple docstring'''





            __snake_case			:List[Any]   =						parent
            __snake_case			:Dict   =						batch_size
            __snake_case			:List[str]   =						image_size
            __snake_case			:Dict   =						patch_size
            __snake_case			:List[Any]   =						num_channels
            __snake_case			:Optional[int]   =						is_training
            __snake_case			:Tuple   =						use_labels
            __snake_case			:Any   =						hidden_size
            __snake_case			:Optional[Any]   =						num_hidden_layers
            __snake_case			:Optional[Any]   =						num_attention_heads
            __snake_case			:Any   =						intermediate_size
            __snake_case			:Dict   =						hidden_act
            __snake_case			:str   =						hidden_dropout_prob
            __snake_case			:Optional[Any]   =						attention_probs_dropout_prob
            __snake_case			:Optional[Any]   =						type_sequence_label_size
            __snake_case			:Union[str, Any]   =						initializer_range
            __snake_case			:List[str]   =						scope
            __snake_case			:Dict   =						encoder_stride

            # in DeiT, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distilation tokens)
            __snake_case			:List[Any]   =						(image_size // patch_size) ** 2
            __snake_case			:Any   =						num_patches + 2



       def 					__lowercase						(							self    ) ->      List[str]:






            '''simple docstring'''





            __snake_case			:Tuple   =						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

            __snake_case			:Union[str, Any]   =						None
            if self.use_labels:
                 __snake_case			:str   =						ids_tensor([self.batch_size]			,						self.type_sequence_label_size    )

            __snake_case			:List[Any]   =						self.get_config()

            return config, pixel_values, labels



       def 					__lowercase						(							self    ) ->      List[Any]:






            '''simple docstring'''





            return DeiTConfig(
                image_size=self.image_size			,						patch_size=self.patch_size			,						num_channels=self.num_channels			,						hidden_size=self.hidden_size			,						num_hidden_layers=self.num_hidden_layers			,						num_attention_heads=self.num_attention_heads			,						intermediate_size=self.intermediate_size			,						hidden_act=self.hidden_act			,						hidden_dropout_prob=self.hidden_dropout_prob			,						attention_probs_dropout_prob=self.attention_probs_dropout_prob			,						is_decoder=a__			,						initializer_range=self.initializer_range			,						encoder_stride=self.encoder_stride			,						)



       def 					__lowercase						(							self			,						a__			,						a__			,						a__    ) ->      Tuple:






            '''simple docstring'''





            __snake_case			:Tuple   =						TFDeiTModel(config=a__    )
            __snake_case			:Any   =						model(a__    )
            self.parent.assertEqual(result.last_hidden_state.shape			,						(self.batch_size, self.seq_length, self.hidden_size)    )



       def 					__lowercase						(							self			,						a__			,						a__			,						a__    ) ->      Any:






            '''simple docstring'''





            __snake_case			:Any   =						TFDeiTForMaskedImageModeling(config=a__    )
            __snake_case			:List[str]   =						model(a__    )
            self.parent.assertEqual(
                result.reconstruction.shape			,						(self.batch_size, self.num_channels, self.image_size, self.image_size)    )

            # test greyscale images
            __snake_case			:List[str]   =						1
            __snake_case			:int   =						TFDeiTForMaskedImageModeling(a__    )

            __snake_case			:Any   =						floats_tensor([self.batch_size, 1, self.image_size, self.image_size]    )
            __snake_case			:List[Any]   =						model(a__    )
            self.parent.assertEqual(result.reconstruction.shape			,						(self.batch_size, 1, self.image_size, self.image_size)    )



       def 					__lowercase						(							self			,						a__			,						a__			,						a__    ) ->      Dict:






            '''simple docstring'''





            __snake_case			:Any   =						self.type_sequence_label_size
            __snake_case			:str   =						TFDeiTForImageClassification(a__    )
            __snake_case			:List[str]   =						model(a__			,						labels=a__    )
            self.parent.assertEqual(result.logits.shape			,						(self.batch_size, self.type_sequence_label_size)    )

            # test greyscale images
            __snake_case			:Tuple   =						1
            __snake_case			:List[str]   =						TFDeiTForImageClassification(a__    )

            __snake_case			:Tuple   =						floats_tensor([self.batch_size, 1, self.image_size, self.image_size]    )
            __snake_case			:Optional[Any]   =						model(a__			,						labels=a__    )
            self.parent.assertEqual(result.logits.shape			,						(self.batch_size, self.type_sequence_label_size)    )



       def 					__lowercase						(							self    ) ->      List[str]:






            '''simple docstring'''





            __snake_case			:Dict   =						self.prepare_config_and_inputs()
            __snake_case			:str   =						config_and_inputs
            __snake_case			:List[Any]   =						{"""pixel_values""": pixel_values}
            return config, inputs_dict




@require_tf
class 			snake_case__   (							lowercase_	,    lowercase_	,    unittest.TestCase):

       '''simple docstring'''
       lowerCamelCase							:				Dict 				= (
           (
               TFDeiTModel,
               TFDeiTForImageClassification,
               TFDeiTForImageClassificationWithTeacher,
               TFDeiTForMaskedImageModeling,
           )
           if is_tf_available()
           else ()
       )
       lowerCamelCase							:				Optional[int] 				= (
           {
               "feature-extraction": TFDeiTModel,
               "image-classification": (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher),
           }
           if is_tf_available()
           else {}
       )

       lowerCamelCase							:				Union[str, Any] 				= False
       lowerCamelCase							:				List[Any] 				= False
       lowerCamelCase							:				Optional[int] 				= False
       lowerCamelCase							:				List[Any] 				= False



       def 					__lowercase						(							self    ) ->      Any:






            '''simple docstring'''





            __snake_case			:Optional[int]   =						TFDeiTModelTester(self    )
            __snake_case			:Dict   =						ConfigTester(self			,						config_class=a__			,						has_text_modality=a__			,						hidden_size=37    )



       def 					__lowercase						(							self    ) ->      Any:






            '''simple docstring'''





            self.config_tester.run_common_tests()



       @unittest.skip(reason="""DeiT does not use inputs_embeds"""    )
       def 					__lowercase						(							self    ) ->      str:






            '''simple docstring'''





            pass



       def 					__lowercase						(							self    ) ->      Optional[int]:






            '''simple docstring'''





            __snake_case			:Tuple   =						self.model_tester.prepare_config_and_inputs_for_common()

            for model_class in self.all_model_classes:
                 __snake_case			:List[str]   =						model_class(a__    )
                 self.assertIsInstance(model.get_input_embeddings()			,						(tf.keras.layers.Layer)    )
                 __snake_case			:str   =						model.get_output_embeddings()
                 self.assertTrue(x is None or isinstance(a__			,						tf.keras.layers.Dense    )    )



       def 					__lowercase						(							self    ) ->      str:






            '''simple docstring'''





            __snake_case			:str   =						self.model_tester.prepare_config_and_inputs_for_common()

            for model_class in self.all_model_classes:
                 __snake_case			:Tuple   =						model_class(a__    )
                 __snake_case			:Optional[int]   =						inspect.signature(model.call    )
                 # signature.parameters is an OrderedDict => so arg_names order is deterministic
                 __snake_case			:Any   =						[*signature.parameters.keys()]

                 __snake_case			:Any   =						["""pixel_values"""]
                 self.assertListEqual(arg_names[:1]			,						a__    )



       def 					__lowercase						(							self    ) ->      str:






            '''simple docstring'''





            __snake_case			:List[Any]   =						self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*a__    )



       def 					__lowercase						(							self    ) ->      Optional[Any]:






            '''simple docstring'''





            __snake_case			:Optional[int]   =						self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_masked_image_modeling(*a__    )



       def 					__lowercase						(							self    ) ->      Dict:






            '''simple docstring'''





            __snake_case			:List[str]   =						self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_image_classification(*a__    )



       def 					__lowercase						(							self			,						a__			,						a__			,						a__=False    ) ->      Any:






            '''simple docstring'''





            __snake_case			:Tuple   =						super()._prepare_for_class(a__			,						a__			,						return_labels=a__    )

            if return_labels:
                 if "labels" in inputs_dict and "labels" not in inspect.signature(model_class.call    ).parameters:
                      del inputs_dict["labels"]

            return inputs_dict



       @slow
       def 					__lowercase						(							self    ) ->      List[str]:






            '''simple docstring'''





            for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 __snake_case			:Union[str, Any]   =						TFDeiTModel.from_pretrained(a__    )
                 self.assertIsNotNone(a__    )






def   UpperCamelCase  (       ):




     '''simple docstring'''






     __snake_case			:Dict   =						Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""  )
     return image




@require_tf
@require_vision
class 			snake_case__   (							unittest.TestCase):

       '''simple docstring'''



       @cached_property
       def 					__lowercase						(							self    ) ->      Optional[Any]:






            '''simple docstring'''





            return (
                DeiTImageProcessor.from_pretrained("""facebook/deit-base-distilled-patch16-224"""    )
                if is_vision_available()
                else None
            )



       @slow
       def 					__lowercase						(							self    ) ->      str:






            '''simple docstring'''





            __snake_case			:Any   =						TFDeiTForImageClassificationWithTeacher.from_pretrained("""facebook/deit-base-distilled-patch16-224"""    )

            __snake_case			:Dict   =						self.default_image_processor
            __snake_case			:List[Any]   =						prepare_img()
            __snake_case			:Optional[Any]   =						image_processor(images=a__			,						return_tensors="""tf"""    )

            # forward pass
            __snake_case			:Optional[int]   =						model(**a__    )

            # verify the logits
            __snake_case			:Tuple   =						tf.TensorShape((1, 10_00)    )
            self.assertEqual(outputs.logits.shape			,						a__    )

            __snake_case			:Tuple   =						tf.constant([-1.02_66, 0.19_12, -1.28_61]    )

            self.assertTrue(np.allclose(outputs.logits[0, :3]			,						a__			,						atol=1e-4    )    )










import json
import os
import unittest

from transformers.models.gptsan_japanese.tokenization_gptsan_japanese import (
    VOCAB_FILES_NAMES,
    GPTSanJapaneseTokenizer,
)
from transformers.testing_utils import require_tokenizers, slow

from ...test_tokenization_common import TokenizerTesterMixin




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

       '''simple docstring'''
       lowerCamelCase							:				Optional[int] 				= GPTSanJapaneseTokenizer
       lowerCamelCase							:				Any 				= False
       lowerCamelCase							:				Optional[Any] 				= {"do_clean_text": False, "add_prefix_space": False}



       def 					__lowercase						(							self    ) ->      Any:






            '''simple docstring'''





            super().setUp()

            # fmt: off
            __snake_case			:str          =						["""こん""", """こんに""", """にちは""", """ばんは""", """世界,㔺界""", """、""", """。""", """<BR>""", """<SP>""", """<TAB>""", """<URL>""", """<EMAIL>""", """<TEL>""", """<DATE>""", """<PRICE>""", """<BLOCK>""", """<KIGOU>""", """<U2000U2BFF>""", """<|emoji1|>""", """<unk>""", """<|bagoftoken|>""", """<|endoftext|>"""]
            # fmt: on
            __snake_case			:Optional[Any]          =						{"""emoji""": {"""\ud83d\ude00""": """<|emoji1|>"""}, """emoji_inv""": {"""<|emoji1|>""": """\ud83d\ude00"""}}  # 😀
            __snake_case			:Optional[int]          =						{"""unk_token""": """<unk>"""}

            __snake_case			:Optional[Any]          =						os.path.join(self.tmpdirname			,						VOCAB_FILES_NAMES["""vocab_file"""]    )
            __snake_case			:List[str]          =						os.path.join(self.tmpdirname			,						VOCAB_FILES_NAMES["""emoji_file"""]    )
            with open(self.vocab_file			,						"""w"""			,						encoding="""utf-8"""    ) as vocab_writer:
                 vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens]    )    )
            with open(self.emoji_file			,						"""w"""    ) as emoji_writer:
                 emoji_writer.write(json.dumps(a__    )    )



       def 					__lowercase						(							self			,						**a__    ) ->      Optional[Any]:






            '''simple docstring'''





            kwargs.update(self.special_tokens_map    )
            return GPTSanJapaneseTokenizer.from_pretrained(self.tmpdirname			,						**a__    )



       def 					__lowercase						(							self			,						a__    ) ->      List[str]:






            '''simple docstring'''





            __snake_case			:Tuple          =						"""こんにちは、世界。 \nこんばんは、㔺界。😀"""
            __snake_case			:Union[str, Any]          =						"""こんにちは、世界。 \nこんばんは、世界。😀"""
            return input_text, output_text



       def 					__lowercase						(							self			,						a__    ) ->      List[str]:






            '''simple docstring'''





            __snake_case       , __snake_case			:Dict          =						self.get_input_output_texts(a__    )
            __snake_case			:Dict          =						tokenizer.encode(a__			,						add_special_tokens=a__    )
            __snake_case			:int          =						tokenizer.decode(a__			,						clean_up_tokenization_spaces=a__    )
            return text, ids



       def 					__lowercase						(							self    ) ->      Optional[int]:






            '''simple docstring'''





            pass  # TODO add if relevant



       def 					__lowercase						(							self    ) ->      Optional[Any]:






            '''simple docstring'''





            pass  # TODO add if relevant



       def 					__lowercase						(							self    ) ->      Union[str, Any]:






            '''simple docstring'''





            pass  # TODO add if relevant



       def 					__lowercase						(							self    ) ->      str:






            '''simple docstring'''





            __snake_case			:Optional[int]          =						self.get_tokenizer()

            # Testing tokenization
            __snake_case			:int          =						"""こんにちは、世界。　こんばんは、㔺界。"""
            __snake_case			:Dict          =						["""こん""", """にちは""", """、""", """世界""", """。""", """<SP>""", """こん""", """ばんは""", """、""", """㔺界""", """。"""]
            __snake_case			:Optional[Any]          =						tokenizer.tokenize(a__    )
            self.assertListEqual(a__			,						a__    )

            # Testing conversion to ids without special tokens
            __snake_case			:List[str]          =						[0, 2, 5, 4, 6, 8, 0, 3, 5, 4, 6]
            __snake_case			:Any          =						tokenizer.convert_tokens_to_ids(a__    )
            self.assertListEqual(a__			,						a__    )

            # Testing conversion to ids with special tokens
            __snake_case			:Tuple          =						tokens + [tokenizer.unk_token]
            __snake_case			:Optional[int]          =						[0, 2, 5, 4, 6, 8, 0, 3, 5, 4, 6, 19]
            __snake_case			:List[Any]          =						tokenizer.convert_tokens_to_ids(a__    )
            self.assertListEqual(a__			,						a__    )



       def 					__lowercase						(							self    ) ->      List[str]:






            '''simple docstring'''





            __snake_case			:Tuple          =						self.get_tokenizer()

            # Testing tokenization
            __snake_case			:Optional[Any]          =						"""こんにちは、<|bagoftoken|>世界。こんばんは、<|bagoftoken|>㔺界。"""
            __snake_case			:Tuple          =						"""こんにちは、、、、世界。こんばんは、、、、世界。"""
            __snake_case			:str          =						tokenizer.encode(a__    )
            __snake_case			:Optional[int]          =						tokenizer.decode(a__    )
            self.assertEqual(a__			,						a__    )



       @slow
       def 					__lowercase						(							self    ) ->      List[Any]:






            '''simple docstring'''





            __snake_case			:List[str]          =						self.tokenizer_class.from_pretrained("""Tanrei/GPTSAN-japanese"""    )

            # Testing tokenization
            __snake_case			:str          =						"""こんにちは、世界。"""
            __snake_case			:List[str]          =						"""こんばんは、㔺界。😀"""
            __snake_case			:Dict          =						"""こんにちは、世界。こんばんは、世界。😀"""
            __snake_case			:int          =						tokenizer.encode(prefix_text + input_text    )
            __snake_case			:Optional[Any]          =						tokenizer.encode(""""""			,						prefix_text=prefix_text + input_text    )
            __snake_case			:Any          =						tokenizer.encode(a__			,						prefix_text=a__    )
            __snake_case			:Optional[Any]          =						tokenizer.decode(a__    )
            __snake_case			:Optional[Any]          =						tokenizer.decode(a__    )
            __snake_case			:Optional[Any]          =						tokenizer.decode(a__    )
            self.assertEqual(a__			,						a__    )
            self.assertEqual(a__			,						a__    )
            self.assertEqual(a__			,						a__    )



       @slow
       def 					__lowercase						(							self    ) ->      Any:






            '''simple docstring'''





            __snake_case			:Optional[Any]          =						self.tokenizer_class.from_pretrained("""Tanrei/GPTSAN-japanese"""    )

            # Testing tokenization
            __snake_case			:List[Any]          =						"""こんにちは、世界。"""
            __snake_case			:Dict          =						"""こんばんは、㔺界。😀"""

            __snake_case			:Optional[int]          =						len(tokenizer.encode(a__    )    ) - 2
            __snake_case			:Union[str, Any]          =						len(tokenizer.encode(a__    )    ) - 2

            __snake_case			:Union[str, Any]          =						[1] + [0] * (len_prefix + len_text + 1)
            __snake_case			:Optional[Any]          =						[1] * (len_prefix + len_text + 1) + [0]
            __snake_case			:Union[str, Any]          =						[1] + [1] * (len_prefix) + [0] * (len_text + 1)

            __snake_case			:int          =						tokenizer(prefix_text + input_text    ).token_type_ids
            __snake_case			:List[Any]          =						tokenizer(""""""			,						prefix_text=prefix_text + input_text    ).token_type_ids
            __snake_case			:int          =						tokenizer(a__			,						prefix_text=a__    ).token_type_ids
            self.assertListEqual(a__			,						a__    )
            self.assertListEqual(a__			,						a__    )
            self.assertListEqual(a__			,						a__    )



       @slow
       def 					__lowercase						(							self    ) ->      Optional[int]:






            '''simple docstring'''





            __snake_case			:Optional[Any]          =						self.tokenizer_class.from_pretrained("""Tanrei/GPTSAN-japanese"""    )

            __snake_case			:Tuple          =						tokenizer.encode("""あンいワ"""    )
            __snake_case			:List[str]          =						tokenizer.encode(""""""			,						prefix_text="""あンいワ"""    )
            __snake_case			:int          =						tokenizer.encode("""いワ"""			,						prefix_text="""あン"""    )

            self.assertEqual(tokenizer.decode(a__    )			,						tokenizer.decode(a__    )    )
            self.assertEqual(tokenizer.decode(a__    )			,						tokenizer.decode(a__    )    )
            self.assertNotEqual(a__			,						a__    )
            self.assertNotEqual(a__			,						a__    )
            self.assertEqual(x_token_a[1]			,						x_token_a[-1]    )  # SEG token
            self.assertEqual(x_token_a[1]			,						x_token_a[3]    )  # SEG token



       @slow
       def 					__lowercase						(							self    ) ->      Union[str, Any]:






            '''simple docstring'''





            __snake_case			:List[str]          =						self.tokenizer_class.from_pretrained("""Tanrei/GPTSAN-japanese"""    )

            __snake_case			:int          =						[["""武田信玄""", """は、"""], ["""織田信長""", """の配下の、"""]]
            __snake_case			:Optional[int]          =						tokenizer(a__			,						padding=a__    )
            __snake_case			:int          =						tokenizer.batch_encode_plus(a__			,						padding=a__    )

            # fmt: off
            __snake_case			:Tuple          =						[[3_59_93, 86_40, 2_59_48, 3_59_98, 3_06_47, 3_56_75, 3_59_99, 3_59_99], [3_59_93, 1_03_82, 98_68, 3_59_98, 3_06_46, 94_59, 3_06_46, 3_56_75]]
            __snake_case			:Any          =						[[1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0, 0, 0]]
            __snake_case			:int          =						[[1, 1, 1, 1, 1, 1, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1]]
            # fmt: on
            self.assertListEqual(x_token.input_ids			,						a__    )
            self.assertListEqual(x_token.token_type_ids			,						a__    )
            self.assertListEqual(x_token.attention_mask			,						a__    )
            self.assertListEqual(x_token_a.input_ids			,						a__    )
            self.assertListEqual(x_token_a.token_type_ids			,						a__    )
            self.assertListEqual(x_token_a.attention_mask			,						a__    )



       def 					__lowercase						(							self    ) ->      List[str]:






            '''simple docstring'''





            pass



       def 					__lowercase						(							self    ) ->      Optional[int]:






            '''simple docstring'''





            pass


