





'''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       :		str	,			lowerCAmelCase__       :		List[Any]	,			lowerCAmelCase__       :		int=13	,			lowerCAmelCase__       :		Optional[Any]=30	,			lowerCAmelCase__       :		Any=2	,			lowerCAmelCase__       :		List[str]=3	,			lowerCAmelCase__       :		Optional[int]=True	,			lowerCAmelCase__       :		Dict=True	,			lowerCAmelCase__       :		Optional[Any]=32	,			lowerCAmelCase__       :		Any=2	,			lowerCAmelCase__       :		Tuple=4	,			lowerCAmelCase__       :		List[str]=37	,			lowerCAmelCase__       :		Any="gelu"	,			lowerCAmelCase__       :		List[Any]=0.1	,			lowerCAmelCase__       :		Tuple=0.1	,			lowerCAmelCase__       :		List[Any]=10	,			lowerCAmelCase__       :		Optional[Any]=0.02	,			lowerCAmelCase__       :		Optional[int]=3	,			lowerCAmelCase__       :		Tuple=None	,			lowerCAmelCase__       :		int=2	,			)   ->					str:

            '''simple docstring'''




            _UpperCamelCase       =	parent
            _UpperCamelCase       =	batch_size
            _UpperCamelCase       =	image_size
            _UpperCamelCase       =	patch_size
            _UpperCamelCase       =	num_channels
            _UpperCamelCase       =	is_training
            _UpperCamelCase       =	use_labels
            _UpperCamelCase       =	hidden_size
            _UpperCamelCase       =	num_hidden_layers
            _UpperCamelCase       =	num_attention_heads
            _UpperCamelCase       =	intermediate_size
            _UpperCamelCase       =	hidden_act
            _UpperCamelCase       =	hidden_dropout_prob
            _UpperCamelCase       =	attention_probs_dropout_prob
            _UpperCamelCase       =	type_sequence_label_size
            _UpperCamelCase       =	initializer_range
            _UpperCamelCase       =	scope
            _UpperCamelCase       =	encoder_stride

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





       def snake_case__						(							self       :		Tuple					)   ->					Optional[int]:

            '''simple docstring'''




            _UpperCamelCase       =	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

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

            _UpperCamelCase       =	self.get_config()

            return config, pixel_values, labels





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

            '''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=lowerCAmelCase__	,			initializer_range=self.initializer_range	,			encoder_stride=self.encoder_stride	,			)





       def snake_case__						(							self       :		Dict	,			lowerCAmelCase__       :		Optional[int]	,			lowerCAmelCase__       :		Optional[Any]	,			lowerCAmelCase__       :		Optional[int]					)   ->					Dict:

            '''simple docstring'''




            _UpperCamelCase       =	TFDeiTModel(config=lowerCAmelCase__					)
            _UpperCamelCase       =	model(lowerCAmelCase__					)
            self.parent.assertEqual(result.last_hidden_state.shape	,			(self.batch_size, self.seq_length, self.hidden_size)					)





       def snake_case__						(							self       :		int	,			lowerCAmelCase__       :		Dict	,			lowerCAmelCase__       :		Optional[Any]	,			lowerCAmelCase__       :		List[Any]					)   ->					List[str]:

            '''simple docstring'''




            _UpperCamelCase       =	TFDeiTForMaskedImageModeling(config=lowerCAmelCase__					)
            _UpperCamelCase       =	model(lowerCAmelCase__					)
            self.parent.assertEqual(
                result.reconstruction.shape	,			(self.batch_size, self.num_channels, self.image_size, self.image_size)					)

            # test greyscale images
            _UpperCamelCase       =	1
            _UpperCamelCase       =	TFDeiTForMaskedImageModeling(lowerCAmelCase__					)

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





       def snake_case__						(							self       :		Optional[int]	,			lowerCAmelCase__       :		Optional[Any]	,			lowerCAmelCase__       :		Optional[Any]	,			lowerCAmelCase__       :		str					)   ->					List[str]:

            '''simple docstring'''




            _UpperCamelCase       =	self.type_sequence_label_size
            _UpperCamelCase       =	TFDeiTForImageClassification(lowerCAmelCase__					)
            _UpperCamelCase       =	model(lowerCAmelCase__	,			labels=lowerCAmelCase__					)
            self.parent.assertEqual(result.logits.shape	,			(self.batch_size, self.type_sequence_label_size)					)

            # test greyscale images
            _UpperCamelCase       =	1
            _UpperCamelCase       =	TFDeiTForImageClassification(lowerCAmelCase__					)

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







       def snake_case__						(							self       :		Optional[Any]					)   ->					int:

            '''simple docstring'''




            _UpperCamelCase       =	self.prepare_config_and_inputs()
            _UpperCamelCase   ,    _UpperCamelCase   ,    _UpperCamelCase       =	config_and_inputs
            _UpperCamelCase       =	{'''pixel_values''': pixel_values}
            return config, inputs_dict

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






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

       _snake_case     :					Union[str, Any]						    =   False
       _snake_case     :					Union[str, Any]						    =   False
       _snake_case     :					int						    =   False
       _snake_case     :					Optional[int]						    =   False





       def snake_case__						(							self       :		Dict					)   ->					Tuple:

            '''simple docstring'''




            _UpperCamelCase       =	TFDeiTModelTester(self					)
            _UpperCamelCase       =	ConfigTester(self	,			config_class=lowerCAmelCase__	,			has_text_modality=lowerCAmelCase__	,			hidden_size=37					)





       def snake_case__						(							self       :		Dict					)   ->					Optional[int]:

            '''simple docstring'''




            self.config_tester.run_common_tests()





       @unittest.skip(reason='''DeiT does not use inputs_embeds'''					)
       def snake_case__						(							self       :		Optional[int]					)   ->					Union[str, Any]:

            '''simple docstring'''




            pass





       def snake_case__						(							self       :		Tuple					)   ->					Tuple:

            '''simple docstring'''




            _UpperCamelCase   ,    _UpperCamelCase       =	self.model_tester.prepare_config_and_inputs_for_common()

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





       def snake_case__						(							self       :		Optional[Any]					)   ->					int:

            '''simple docstring'''




            _UpperCamelCase   ,    _UpperCamelCase       =	self.model_tester.prepare_config_and_inputs_for_common()

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

                 _UpperCamelCase       =	['''pixel_values''']
                 self.assertListEqual(arg_names[:1]	,			lowerCAmelCase__					)





       def snake_case__						(							self       :		Dict					)   ->					int:

            '''simple docstring'''




            _UpperCamelCase       =	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*lowerCAmelCase__					)





       def snake_case__						(							self       :		List[str]					)   ->					Optional[Any]:

            '''simple docstring'''




            _UpperCamelCase       =	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_masked_image_modeling(*lowerCAmelCase__					)





       def snake_case__						(							self       :		List[Any]					)   ->					List[str]:

            '''simple docstring'''




            _UpperCamelCase       =	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase__					)





       def snake_case__						(							self       :		Optional[int]	,			lowerCAmelCase__       :		List[Any]	,			lowerCAmelCase__       :		Union[str, Any]	,			lowerCAmelCase__       :		Dict=False					)   ->					int:

            '''simple docstring'''




            _UpperCamelCase       =	super()._prepare_for_class(lowerCAmelCase__	,			lowerCAmelCase__	,			return_labels=lowerCAmelCase__					)

            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 snake_case__						(							self       :		Optional[int]					)   ->					List[Any]:

            '''simple docstring'''




            for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 _UpperCamelCase       =	TFDeiTModel.from_pretrained(lowerCAmelCase__					)
                 self.assertIsNotNone(lowerCAmelCase__					)







def  a__    (    )		->		Optional[Any]:





     """simple docstring"""

     _UpperCamelCase       =	Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''				)
     return image




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






       """simple docstring"""





       @cached_property
       def snake_case__						(							self       :		int					)   ->					Any:

            '''simple docstring'''




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







       @slow
       def snake_case__						(							self       :		Dict					)   ->					int:

            '''simple docstring'''




            _UpperCamelCase       =	TFDeiTForImageClassificationWithTeacher.from_pretrained('''facebook/deit-base-distilled-patch16-224'''					)

            _UpperCamelCase       =	self.default_image_processor
            _UpperCamelCase       =	prepare_img()
            _UpperCamelCase       =	image_processor(images=lowerCAmelCase__	,			return_tensors='''tf'''					)

            # forward pass
            _UpperCamelCase       =	model(**lowerCAmelCase__					)

            # verify the logits
            _UpperCamelCase       =	tf.TensorShape((1, 1000)					)
            self.assertEqual(outputs.logits.shape	,			lowerCAmelCase__					)

            _UpperCamelCase       =	tf.constant([-1.0266, 0.1912, -1.2861]					)

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










'''simple docstring'''

import unittest

from transformers.utils.backbone_utils import (
    BackboneMixin,
    get_aligned_output_features_output_indices,
    verify_out_features_out_indices,
)
class     __lowerCAmelCase					(   unittest.TestCase			):






       """simple docstring"""





       def snake_case__						(							self       :		Optional[Any]					)   ->					List[str]:

            '''simple docstring'''




            _UpperCamelCase       =	['''a''', '''b''', '''c''']

            # Defaults to last layer if both are None
            _UpperCamelCase   ,    _UpperCamelCase       =	get_aligned_output_features_output_indices(lowerCAmelCase__	,			lowerCAmelCase__	,			lowerCAmelCase__					)
            self.assertEqual(lowerCAmelCase__	,			['''c''']					)
            self.assertEqual(lowerCAmelCase__	,			[2]					)

            # Out indices set to match out features
            _UpperCamelCase   ,    _UpperCamelCase       =	get_aligned_output_features_output_indices(['''a''', '''c''']	,			lowerCAmelCase__	,			lowerCAmelCase__					)
            self.assertEqual(lowerCAmelCase__	,			['''a''', '''c''']					)
            self.assertEqual(lowerCAmelCase__	,			[0, 2]					)

            # Out features set to match out indices
            _UpperCamelCase   ,    _UpperCamelCase       =	get_aligned_output_features_output_indices(lowerCAmelCase__	,			[0, 2]	,			lowerCAmelCase__					)
            self.assertEqual(lowerCAmelCase__	,			['''a''', '''c''']					)
            self.assertEqual(lowerCAmelCase__	,			[0, 2]					)

            # Out features selected from negative indices
            _UpperCamelCase   ,    _UpperCamelCase       =	get_aligned_output_features_output_indices(lowerCAmelCase__	,			[-3, -1]	,			lowerCAmelCase__					)
            self.assertEqual(lowerCAmelCase__	,			['''a''', '''c''']					)
            self.assertEqual(lowerCAmelCase__	,			[-3, -1]					)





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

            '''simple docstring'''




            with self.assertRaises(lowerCAmelCase__					):
                 verify_out_features_out_indices(['''a''', '''b''']	,			(0, 1)	,			lowerCAmelCase__					)

            # Out features must be a list
            with self.assertRaises(lowerCAmelCase__					):
                 verify_out_features_out_indices(('''a''', '''b''')	,			(0, 1)	,			['''a''', '''b''']					)

            # Out features must be a subset of stage names
            with self.assertRaises(lowerCAmelCase__					):
                 verify_out_features_out_indices(['''a''', '''b''']	,			(0, 1)	,			['''a''']					)

            # Out indices must be a list or tuple
            with self.assertRaises(lowerCAmelCase__					):
                 verify_out_features_out_indices(lowerCAmelCase__	,			0	,			['''a''', '''b''']					)

            # Out indices must be a subset of stage names
            with self.assertRaises(lowerCAmelCase__					):
                 verify_out_features_out_indices(lowerCAmelCase__	,			(0, 1)	,			['''a''']					)

            # Out features and out indices must be the same length
            with self.assertRaises(lowerCAmelCase__					):
                 verify_out_features_out_indices(['''a''', '''b''']	,			(0,)	,			['''a''', '''b''', '''c''']					)

            # Out features should match out indices
            with self.assertRaises(lowerCAmelCase__					):
                 verify_out_features_out_indices(['''a''', '''b''']	,			(0, 2)	,			['''a''', '''b''', '''c''']					)

            # Out features and out indices should be in order
            with self.assertRaises(lowerCAmelCase__					):
                 verify_out_features_out_indices(['''b''', '''a''']	,			(0, 1)	,			['''a''', '''b''']					)

            # Check passes with valid inputs
            verify_out_features_out_indices(['''a''', '''b''', '''d''']	,			(0, 1, -1)	,			['''a''', '''b''', '''c''', '''d''']					)







       def snake_case__						(							self       :		Any					)   ->					str:

            '''simple docstring'''




            _UpperCamelCase       =	BackboneMixin()

            _UpperCamelCase       =	['''a''', '''b''', '''c''']
            _UpperCamelCase       =	['''a''', '''c''']
            _UpperCamelCase       =	[0, 2]

            # Check that the output features and indices are set correctly
            self.assertEqual(backbone.out_features	,			['''a''', '''c''']					)
            self.assertEqual(backbone.out_indices	,			[0, 2]					)

            # Check out features and indices are updated correctly
            _UpperCamelCase       =	['''a''', '''b''']
            self.assertEqual(backbone.out_features	,			['''a''', '''b''']					)
            self.assertEqual(backbone.out_indices	,			[0, 1]					)

            _UpperCamelCase       =	[-3, -1]
            self.assertEqual(backbone.out_features	,			['''a''', '''c''']					)
            self.assertEqual(backbone.out_indices	,			[-3, -1]					)



