





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				:
 def __init__( self    : Union[str, Any]     ,	UpperCAmelCase    : List[str]     ,	UpperCAmelCase    : List[Any]=13     ,	UpperCAmelCase    : Optional[int]=30     ,	UpperCAmelCase    : Union[str, Any]=2     ,	UpperCAmelCase    : int=3     ,	UpperCAmelCase    : Tuple=True     ,	UpperCAmelCase    : List[str]=True     ,	UpperCAmelCase    : Tuple=32     ,	UpperCAmelCase    : Dict=2     ,	UpperCAmelCase    : Dict=4     ,	UpperCAmelCase    : Union[str, Any]=37     ,	UpperCAmelCase    : str="gelu"     ,	UpperCAmelCase    : int=0.1     ,	UpperCAmelCase    : Dict=0.1     ,	UpperCAmelCase    : Optional[int]=10     ,	UpperCAmelCase    : Optional[Any]=0.0_2     ,	UpperCAmelCase    : Optional[Any]=3     ,	UpperCAmelCase    : Tuple=None     ,	UpperCAmelCase    : Tuple=2     ,	)     ->      Union[str, Any]:
     lowerCamelCase__       : List[str]   					=						parent
     lowerCamelCase__       : List[str]   					=						batch_size
     lowerCamelCase__       : Optional[Any]   					=						image_size
     lowerCamelCase__       : List[Any]   					=						patch_size
     lowerCamelCase__       : Union[str, Any]   					=						num_channels
     lowerCamelCase__       : Any   					=						is_training
     lowerCamelCase__       : List[str]   					=						use_labels
     lowerCamelCase__       : Optional[int]   					=						hidden_size
     lowerCamelCase__       : Tuple   					=						num_hidden_layers
     lowerCamelCase__       : List[str]   					=						num_attention_heads
     lowerCamelCase__       : Optional[int]   					=						intermediate_size
     lowerCamelCase__       : str   					=						hidden_act
     lowerCamelCase__       : List[str]   					=						hidden_dropout_prob
     lowerCamelCase__       : Tuple   					=						attention_probs_dropout_prob
     lowerCamelCase__       : Dict   					=						type_sequence_label_size
     lowerCamelCase__       : List[str]   					=						initializer_range
     lowerCamelCase__       : Tuple   					=						scope
     lowerCamelCase__       : Dict   					=						encoder_stride

     # in DeiT, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distilation tokens)
     lowerCamelCase__       : str   					=						(image_size // patch_size) ** 2
     lowerCamelCase__       : int   					=						num_patches + 2
 def 	A_      ( self    : List[str]      )     ->      str:
     lowerCamelCase__       : List[str]   					=						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

     lowerCamelCase__       : List[str]   					=						None
     if self.use_labels:
         lowerCamelCase__       : int   					=						ids_tensor([self.batch_size]     ,	self.type_sequence_label_size      )

     lowerCamelCase__       : str   					=						self.get_config()

     return config, pixel_values, labels
 def 	A_      ( self    : Dict      )     ->      int:
     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=UpperCAmelCase     ,	initializer_range=self.initializer_range     ,	encoder_stride=self.encoder_stride     ,	)
 def 	A_      ( self    : List[str]     ,	UpperCAmelCase    : Union[str, Any]     ,	UpperCAmelCase    : List[str]     ,	UpperCAmelCase    : Dict      )     ->      Optional[int]:
     lowerCamelCase__       : Dict   					=						TFDeiTModel(config=UpperCAmelCase      )
     lowerCamelCase__       : str   					=						model(UpperCAmelCase      )
     self.parent.assertEqual(result.last_hidden_state.shape     ,	(self.batch_size, self.seq_length, self.hidden_size)      )
 def 	A_      ( self    : Union[str, Any]     ,	UpperCAmelCase    : Dict     ,	UpperCAmelCase    : Optional[Any]     ,	UpperCAmelCase    : Any      )     ->      List[Any]:
     lowerCamelCase__       : Tuple   					=						TFDeiTForMaskedImageModeling(config=UpperCAmelCase      )
     lowerCamelCase__       : int   					=						model(UpperCAmelCase      )
     self.parent.assertEqual(
         result.reconstruction.shape     ,	(self.batch_size, self.num_channels, self.image_size, self.image_size)      )

     # test greyscale images
     lowerCamelCase__       : int   					=						1
     lowerCamelCase__       : int   					=						TFDeiTForMaskedImageModeling(UpperCAmelCase      )

     lowerCamelCase__       : Union[str, Any]   					=						floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
     lowerCamelCase__       : Tuple   					=						model(UpperCAmelCase      )
     self.parent.assertEqual(result.reconstruction.shape     ,	(self.batch_size, 1, self.image_size, self.image_size)      )
 def 	A_      ( self    : int     ,	UpperCAmelCase    : List[str]     ,	UpperCAmelCase    : Optional[int]     ,	UpperCAmelCase    : Dict      )     ->      List[Any]:
     lowerCamelCase__       : str   					=						self.type_sequence_label_size
     lowerCamelCase__       : List[Any]   					=						TFDeiTForImageClassification(UpperCAmelCase      )
     lowerCamelCase__       : Union[str, Any]   					=						model(UpperCAmelCase     ,	labels=UpperCAmelCase      )
     self.parent.assertEqual(result.logits.shape     ,	(self.batch_size, self.type_sequence_label_size)      )

     # test greyscale images
     lowerCamelCase__       : Optional[int]   					=						1
     lowerCamelCase__       : Dict   					=						TFDeiTForImageClassification(UpperCAmelCase      )

     lowerCamelCase__       : int   					=						floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
     lowerCamelCase__       : str   					=						model(UpperCAmelCase     ,	labels=UpperCAmelCase      )
     self.parent.assertEqual(result.logits.shape     ,	(self.batch_size, self.type_sequence_label_size)      )







 def 	A_      ( self    : List[str]      )     ->      Optional[int]:
     lowerCamelCase__       : List[Any]   					=						self.prepare_config_and_inputs()
     lowerCamelCase__					,				lowerCamelCase__					,				lowerCamelCase__       : int   					=						config_and_inputs
     lowerCamelCase__       : Any   					=						{'pixel_values': pixel_values}
     return config, inputs_dict


@require_tf
class   lowerCAmelCase				(   __UpperCamelCase,    __UpperCamelCase,    unittest.TestCase     ):
 UpperCAmelCase__						=						(
     (
         TFDeiTModel,
         TFDeiTForImageClassification,
         TFDeiTForImageClassificationWithTeacher,
         TFDeiTForMaskedImageModeling,
     )
     if is_tf_available()
     else ()
 )
 UpperCAmelCase__						=						(
     {
         """feature-extraction""": TFDeiTModel,
         """image-classification""": (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher),
     }
     if is_tf_available()
     else {}
 )

 UpperCAmelCase__						=						False
 UpperCAmelCase__						=						False
 UpperCAmelCase__						=						False
 UpperCAmelCase__						=						False
 def 	A_      ( self    : int      )     ->      Tuple:
     lowerCamelCase__       : List[str]   					=						TFDeiTModelTester(self      )
     lowerCamelCase__       : Union[str, Any]   					=						ConfigTester(self     ,	config_class=UpperCAmelCase     ,	has_text_modality=UpperCAmelCase     ,	hidden_size=37      )
 def 	A_      ( self    : Optional[int]      )     ->      str:
     self.config_tester.run_common_tests()
 @unittest.skip(reason='DeiT does not use inputs_embeds'      )
 def 	A_      ( self    : Dict      )     ->      Optional[Any]:
     pass
 def 	A_      ( self    : List[Any]      )     ->      Union[str, Any]:
     lowerCamelCase__					,				lowerCamelCase__       : int   					=						self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
         lowerCamelCase__       : Optional[Any]   					=						model_class(UpperCAmelCase      )
         self.assertIsInstance(model.get_input_embeddings()     ,	(tf.keras.layers.Layer)      )
         lowerCamelCase__       : Any   					=						model.get_output_embeddings()
         self.assertTrue(x is None or isinstance(UpperCAmelCase     ,	tf.keras.layers.Dense      )      )
 def 	A_      ( self    : int      )     ->      Dict:
     lowerCamelCase__					,				lowerCamelCase__       : str   					=						self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
         lowerCamelCase__       : Optional[Any]   					=						model_class(UpperCAmelCase      )
         lowerCamelCase__       : Union[str, Any]   					=						inspect.signature(model.call      )
         # signature.parameters is an OrderedDict => so arg_names order is deterministic
         lowerCamelCase__       : str   					=						[*signature.parameters.keys()]

         lowerCamelCase__       : Dict   					=						['pixel_values']
         self.assertListEqual(arg_names[:1]     ,	UpperCAmelCase      )
 def 	A_      ( self    : Optional[int]      )     ->      Tuple:
     lowerCamelCase__       : List[str]   					=						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*UpperCAmelCase      )
 def 	A_      ( self    : Optional[int]      )     ->      int:
     lowerCamelCase__       : Dict   					=						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_masked_image_modeling(*UpperCAmelCase      )
 def 	A_      ( self    : str      )     ->      Any:
     lowerCamelCase__       : int   					=						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_image_classification(*UpperCAmelCase      )
 def 	A_      ( self    : str     ,	UpperCAmelCase    : Tuple     ,	UpperCAmelCase    : Optional[int]     ,	UpperCAmelCase    : Optional[Any]=False      )     ->      str:
     lowerCamelCase__       : Optional[Any]   					=						super()._prepare_for_class(UpperCAmelCase     ,	UpperCAmelCase     ,	return_labels=UpperCAmelCase      )

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


def 			SCREAMING_SNAKE_CASE (			)     ->	Optional[int]:
    lowerCamelCase__       : Dict   					=						Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'					)
    return image


@require_tf
@require_vision
class   lowerCAmelCase				(   unittest.TestCase     ):
 @cached_property
 def 	A_      ( self    : int      )     ->      Tuple:
     return (
         DeiTImageProcessor.from_pretrained('facebook/deit-base-distilled-patch16-224'      )
         if is_vision_available()
         else None
     )







 @slow
 def 	A_      ( self    : Dict      )     ->      List[str]:
     lowerCamelCase__       : List[Any]   					=						TFDeiTForImageClassificationWithTeacher.from_pretrained('facebook/deit-base-distilled-patch16-224'      )

     lowerCamelCase__       : Optional[Any]   					=						self.default_image_processor
     lowerCamelCase__       : str   					=						prepare_img()
     lowerCamelCase__       : Tuple   					=						image_processor(images=UpperCAmelCase     ,	return_tensors='tf'      )

     # forward pass
     lowerCamelCase__       : List[str]   					=						model(**UpperCAmelCase      )

     # verify the logits
     lowerCamelCase__       : List[Any]   					=						tf.TensorShape((1, 1000)      )
     self.assertEqual(outputs.logits.shape     ,	UpperCAmelCase      )

     lowerCamelCase__       : Dict   					=						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]     ,	UpperCAmelCase     ,	atol=1e-4      )      )












from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow


if is_tf_available():
    import tensorflow as tf

    from transformers import AutoTokenizer, TFAutoModelForSeqaSeqLM

@require_tf
@require_sentencepiece
@require_tokenizers
class   lowerCAmelCase				(   unittest.TestCase     ):
 @slow
 def 	A_      ( self    : int      )     ->      Optional[Any]:
     lowerCamelCase__       : Tuple   					=						TFAutoModelForSeqaSeqLM.from_pretrained('google/mt5-small'      )
     lowerCamelCase__       : Any   					=						AutoTokenizer.from_pretrained('google/mt5-small'      )

     lowerCamelCase__       : int   					=						tokenizer('Hello there'     ,	return_tensors='tf'      ).input_ids
     lowerCamelCase__       : int   					=						tokenizer('Hi I am'     ,	return_tensors='tf'      ).input_ids

     lowerCamelCase__       : List[str]   					=						model(UpperCAmelCase     ,	labels=UpperCAmelCase      ).loss
     lowerCamelCase__       : List[Any]   					=						-tf.math.reduce_mean(UpperCAmelCase      ).numpy()

     lowerCamelCase__       : Dict   					=						-2_1.2_2_8_1_6_8
     self.assertTrue(abs(mtf_score - EXPECTED_SCORE      ) < 2e-4      )





