


'''simple docstring'''
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 					__lowerCAmelCase   :

						"""simple docstring"""






						def __init__(  self							:      List[str] ,  lowerCAmelCase							:      Tuple ,  lowerCAmelCase							:      Any=13 ,  lowerCAmelCase							:      Optional[int]=30 ,  lowerCAmelCase							:      List[str]=2 ,  lowerCAmelCase							:      Optional[Any]=3 ,  lowerCAmelCase							:      str=True ,  lowerCAmelCase							:      Optional[int]=True ,  lowerCAmelCase							:      List[Any]=32 ,  lowerCAmelCase							:      Tuple=2 ,  lowerCAmelCase							:      Optional[int]=4 ,  lowerCAmelCase							:      int=37 ,  lowerCAmelCase							:      Tuple="gelu" ,  lowerCAmelCase							:      str=0.1 ,  lowerCAmelCase							:      List[str]=0.1 ,  lowerCAmelCase							:      Union[str, Any]=10 ,  lowerCAmelCase							:      Union[str, Any]=0.0_2 ,  lowerCAmelCase							:      Union[str, Any]=3 ,  lowerCAmelCase							:      str=None ,  lowerCAmelCase							:      str=2 ,  ):
													A_			    =		parent
													A_			    =		batch_size
													A_			    =		image_size
													A_			    =		patch_size
													A_			    =		num_channels
													A_			    =		is_training
													A_			    =		use_labels
													A_			    =		hidden_size
													A_			    =		num_hidden_layers
													A_			    =		num_attention_heads
													A_			    =		intermediate_size
													A_			    =		hidden_act
													A_			    =		hidden_dropout_prob
													A_			    =		attention_probs_dropout_prob
													A_			    =		type_sequence_label_size
													A_			    =		initializer_range
													A_			    =		scope
													A_			    =		encoder_stride

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






						def 				_UpperCAmelCase       (  self							:      Dict	):
													A_			    =		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

													A_			    =		None
													if self.use_labels:
																				A_			    =		ids_tensor([self.batch_size] ,  self.type_sequence_label_size	)

													A_			    =		self.get_config()

													return config, pixel_values, labels






						def 				_UpperCAmelCase       (  self							:      Dict	):
													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=SCREAMING_SNAKE_CASE_ ,  initializer_range=self.initializer_range ,  encoder_stride=self.encoder_stride ,  )






						def 				_UpperCAmelCase       (  self							:      Union[str, Any] ,  lowerCAmelCase							:      List[Any] ,  lowerCAmelCase							:      Union[str, Any] ,  lowerCAmelCase							:      int	):
													A_			    =		TFDeiTModel(config=SCREAMING_SNAKE_CASE_	)
													A_			    =		model(SCREAMING_SNAKE_CASE_	)
													self.parent.assertEqual(result.last_hidden_state.shape ,  (self.batch_size, self.seq_length, self.hidden_size)	)






						def 				_UpperCAmelCase       (  self							:      Union[str, Any] ,  lowerCAmelCase							:      Optional[Any] ,  lowerCAmelCase							:      Any ,  lowerCAmelCase							:      Optional[Any]	):
													A_			    =		TFDeiTForMaskedImageModeling(config=SCREAMING_SNAKE_CASE_	)
													A_			    =		model(SCREAMING_SNAKE_CASE_	)
													self.parent.assertEqual(
													    result.reconstruction.shape ,  (self.batch_size, self.num_channels, self.image_size, self.image_size)	)

													# test greyscale images
													A_			    =		1
													A_			    =		TFDeiTForMaskedImageModeling(SCREAMING_SNAKE_CASE_	)

													A_			    =		floats_tensor([self.batch_size, 1, self.image_size, self.image_size]	)
													A_			    =		model(SCREAMING_SNAKE_CASE_	)
													self.parent.assertEqual(result.reconstruction.shape ,  (self.batch_size, 1, self.image_size, self.image_size)	)






						def 				_UpperCAmelCase       (  self							:      str ,  lowerCAmelCase							:      Tuple ,  lowerCAmelCase							:      List[str] ,  lowerCAmelCase							:      Union[str, Any]	):
													A_			    =		self.type_sequence_label_size
													A_			    =		TFDeiTForImageClassification(SCREAMING_SNAKE_CASE_	)
													A_			    =		model(SCREAMING_SNAKE_CASE_ ,  labels=SCREAMING_SNAKE_CASE_	)
													self.parent.assertEqual(result.logits.shape ,  (self.batch_size, self.type_sequence_label_size)	)

													# test greyscale images
													A_			    =		1
													A_			    =		TFDeiTForImageClassification(SCREAMING_SNAKE_CASE_	)

													A_			    =		floats_tensor([self.batch_size, 1, self.image_size, self.image_size]	)
													A_			    =		model(SCREAMING_SNAKE_CASE_ ,  labels=SCREAMING_SNAKE_CASE_	)
													self.parent.assertEqual(result.logits.shape ,  (self.batch_size, self.type_sequence_label_size)	)






						def 				_UpperCAmelCase       (  self							:      Optional[int]	):
													A_			    =		self.prepare_config_and_inputs()
													A_			    =		config_and_inputs
													A_			    =		{'pixel_values': pixel_values}
													return config, inputs_dict






@require_tf
class 					__lowerCAmelCase   (   _UpperCAmelCase				,			_UpperCAmelCase				,			unittest.TestCase						):

						"""simple docstring"""






						_UpperCAmelCase		:       Any   						=(
						    (
						        TFDeiTModel,
						        TFDeiTForImageClassification,
						        TFDeiTForImageClassificationWithTeacher,
						        TFDeiTForMaskedImageModeling,
						    )
						    if is_tf_available()
						    else ()
						)
						_UpperCAmelCase		:       List[str]   						=(
						    {
						        'feature-extraction': TFDeiTModel,
						        'image-classification': (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher),
						    }
						    if is_tf_available()
						    else {}
						)

						_UpperCAmelCase		:       Optional[int]   						=False
						_UpperCAmelCase		:       List[Any]   						=False
						_UpperCAmelCase		:       List[str]   						=False
						_UpperCAmelCase		:       Any   						=False






						def 				_UpperCAmelCase       (  self							:      Optional[int]	):
													A_			    =		TFDeiTModelTester(self	)
													A_			    =		ConfigTester(self ,  config_class=SCREAMING_SNAKE_CASE_ ,  has_text_modality=SCREAMING_SNAKE_CASE_ ,  hidden_size=37	)






						def 				_UpperCAmelCase       (  self							:      List[Any]	):
													self.config_tester.run_common_tests()






						@unittest.skip(reason="DeiT does not use inputs_embeds"	)
						def 				_UpperCAmelCase       (  self							:      Optional[Any]	):
													pass






						def 				_UpperCAmelCase       (  self							:      Union[str, Any]	):
													A_			    =		self.model_tester.prepare_config_and_inputs_for_common()

													for model_class in self.all_model_classes:
																				A_			    =		model_class(SCREAMING_SNAKE_CASE_	)
																				self.assertIsInstance(model.get_input_embeddings() ,  (tf.keras.layers.Layer)	)
																				A_			    =		model.get_output_embeddings()
																				self.assertTrue(x is None or isinstance(SCREAMING_SNAKE_CASE_ ,  tf.keras.layers.Dense	)	)






						def 				_UpperCAmelCase       (  self							:      Any	):
													A_			    =		self.model_tester.prepare_config_and_inputs_for_common()

													for model_class in self.all_model_classes:
																				A_			    =		model_class(SCREAMING_SNAKE_CASE_	)
																				A_			    =		inspect.signature(model.call	)
																				# signature.parameters is an OrderedDict => so arg_names order is deterministic
																				A_			    =		[*signature.parameters.keys()]

																				A_			    =		['pixel_values']
																				self.assertListEqual(arg_names[:1] ,  SCREAMING_SNAKE_CASE_	)






						def 				_UpperCAmelCase       (  self							:      Tuple	):
													A_			    =		self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_	)






						def 				_UpperCAmelCase       (  self							:      Optional[int]	):
													A_			    =		self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_masked_image_modeling(*SCREAMING_SNAKE_CASE_	)






						def 				_UpperCAmelCase       (  self							:      Dict	):
													A_			    =		self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE_	)






						def 				_UpperCAmelCase       (  self							:      List[Any] ,  lowerCAmelCase							:      Optional[Any] ,  lowerCAmelCase							:      Dict ,  lowerCAmelCase							:      Tuple=False	):
													A_			    =		super()._prepare_for_class(SCREAMING_SNAKE_CASE_ ,  SCREAMING_SNAKE_CASE_ ,  return_labels=SCREAMING_SNAKE_CASE_	)

													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 				_UpperCAmelCase       (  self							:      Any	):
													for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																				A_			    =		TFDeiTModel.from_pretrained(SCREAMING_SNAKE_CASE_	)
																				self.assertIsNotNone(SCREAMING_SNAKE_CASE_	)







def       a_				(	):
							A_			    =		Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" )
							return image







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

						"""simple docstring"""






						@cached_property
						def 				_UpperCAmelCase       (  self							:      Union[str, Any]	):
													return (
													    DeiTImageProcessor.from_pretrained("facebook/deit-base-distilled-patch16-224"	)
													    if is_vision_available()
													    else None
													)






						@slow
						def 				_UpperCAmelCase       (  self							:      Union[str, Any]	):
													A_			    =		TFDeiTForImageClassificationWithTeacher.from_pretrained("facebook/deit-base-distilled-patch16-224"	)

													A_			    =		self.default_image_processor
													A_			    =		prepare_img()
													A_			    =		image_processor(images=SCREAMING_SNAKE_CASE_ ,  return_tensors="tf"	)

													# forward pass
													A_			    =		model(**SCREAMING_SNAKE_CASE_	)

													# verify the logits
													A_			    =		tf.TensorShape((1, 10_00)	)
													self.assertEqual(outputs.logits.shape ,  SCREAMING_SNAKE_CASE_	)

													A_			    =		tf.constant([-1.0_2_6_6, 0.1_9_1_2, -1.2_8_6_1]	)

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









'''simple docstring'''



from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


if is_sentencepiece_available():
      from ..ta.tokenization_ta import TaTokenizer
else:
      from ...utils.dummy_sentencepiece_objects import TaTokenizer

A__						:   int				=    TaTokenizer

if is_tokenizers_available():
      from ..ta.tokenization_ta_fast import TaTokenizerFast
else:
      from ...utils.dummy_tokenizers_objects import TaTokenizerFast

A__						:   Dict				=    TaTokenizerFast

A__						:   Dict				=    {"""configuration_mt5""": ["""MT5Config""", """MT5OnnxConfig"""]}

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      A__						:   Any				=    [
          """MT5EncoderModel""",
          """MT5ForConditionalGeneration""",
          """MT5ForQuestionAnswering""",
          """MT5Model""",
          """MT5PreTrainedModel""",
          """MT5Stack""",
      ]

try:
      if not is_tf_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      A__						:   str				=    ["""TFMT5EncoderModel""", """TFMT5ForConditionalGeneration""", """TFMT5Model"""]

try:
      if not is_flax_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      A__						:   str				=    ["""FlaxMT5EncoderModel""", """FlaxMT5ForConditionalGeneration""", """FlaxMT5Model"""]


if TYPE_CHECKING:
      from .configuration_mta import MTaConfig, MTaOnnxConfig

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_mta import (
                MTaEncoderModel,
                MTaForConditionalGeneration,
                MTaForQuestionAnswering,
                MTaModel,
                MTaPreTrainedModel,
                MTaStack,
            )

      try:
            if not is_tf_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_tf_mta import TFMTaEncoderModel, TFMTaForConditionalGeneration, TFMTaModel

      try:
            if not is_flax_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_flax_mta import FlaxMTaEncoderModel, FlaxMTaForConditionalGeneration, FlaxMTaModel

else:
      import sys

      A__						:   Union[str, Any]				=    _LazyModule(
          __name__,
          globals()["""__file__"""],
          _import_structure,
          extra_objects={"""MT5Tokenizer""": MTaTokenizer, """MT5TokenizerFast""": MTaTokenizerFast},
          module_spec=__spec__,
      )






