
"""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 	_lowercase       :




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

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




	def       _UpperCamelCase   (			self )		->							List[str]:
			lowerCamelCase					:      Optional[int]          =   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

			lowerCamelCase					:      str          =   None
			if self.use_labels:
					lowerCamelCase					:      Tuple          =   ids_tensor([self.batch_size]		,			self.type_sequence_label_size )

			lowerCamelCase					:      Union[str, Any]          =   self.get_config()

			return config, pixel_values, labels




	def       _UpperCamelCase   (			self )		->							Optional[Any]:
			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       _UpperCamelCase   (			self		,			UpperCAmelCase_		,			UpperCAmelCase_		,			UpperCAmelCase_ )		->							Any:
			lowerCamelCase					:      Optional[Any]          =   TFDeiTModel(config=UpperCAmelCase_ )
			lowerCamelCase					:      Union[str, Any]          =   model(UpperCAmelCase_ )
			self.parent.assertEqual(result.last_hidden_state.shape		,			(self.batch_size, self.seq_length, self.hidden_size) )




	def       _UpperCamelCase   (			self		,			UpperCAmelCase_		,			UpperCAmelCase_		,			UpperCAmelCase_ )		->							List[Any]:
			lowerCamelCase					:      List[Any]          =   TFDeiTForMaskedImageModeling(config=UpperCAmelCase_ )
			lowerCamelCase					:      Union[str, Any]          =   model(UpperCAmelCase_ )
			self.parent.assertEqual(
			    result.reconstruction.shape		,			(self.batch_size, self.num_channels, self.image_size, self.image_size) )

			# test greyscale images
			lowerCamelCase					:      Union[str, Any]          =   1
			lowerCamelCase					:      str          =   TFDeiTForMaskedImageModeling(UpperCAmelCase_ )

			lowerCamelCase					:      Dict          =   floats_tensor([self.batch_size, 1, self.image_size, self.image_size] )
			lowerCamelCase					:      int          =   model(UpperCAmelCase_ )
			self.parent.assertEqual(result.reconstruction.shape		,			(self.batch_size, 1, self.image_size, self.image_size) )




	def       _UpperCamelCase   (			self		,			UpperCAmelCase_		,			UpperCAmelCase_		,			UpperCAmelCase_ )		->							Optional[int]:
			lowerCamelCase					:      Optional[Any]          =   self.type_sequence_label_size
			lowerCamelCase					:      Tuple          =   TFDeiTForImageClassification(UpperCAmelCase_ )
			lowerCamelCase					:      Optional[Any]          =   model(UpperCAmelCase_		,			labels=UpperCAmelCase_ )
			self.parent.assertEqual(result.logits.shape		,			(self.batch_size, self.type_sequence_label_size) )

			# test greyscale images
			lowerCamelCase					:      List[str]          =   1
			lowerCamelCase					:      List[str]          =   TFDeiTForImageClassification(UpperCAmelCase_ )

			lowerCamelCase					:      Optional[Any]          =   floats_tensor([self.batch_size, 1, self.image_size, self.image_size] )
			lowerCamelCase					:      Union[str, Any]          =   model(UpperCAmelCase_		,			labels=UpperCAmelCase_ )
			self.parent.assertEqual(result.logits.shape		,			(self.batch_size, self.type_sequence_label_size) )




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






@require_tf
class 	_lowercase       (    __UpperCAmelCase    ,	__UpperCAmelCase    ,	unittest.TestCase		):
	lowercase_				=     (
	    (
	        TFDeiTModel,
	        TFDeiTForImageClassification,
	        TFDeiTForImageClassificationWithTeacher,
	        TFDeiTForMaskedImageModeling,
	    )
	    if is_tf_available()
	    else ()
	)
	lowercase_				=     (
	    {
	        'feature-extraction': TFDeiTModel,
	        'image-classification': (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher),
	    }
	    if is_tf_available()
	    else {}
	)

	lowercase_				=     False
	lowercase_				=     False
	lowercase_				=     False
	lowercase_				=     False




	def       _UpperCamelCase   (			self )		->							Optional[Any]:
			lowerCamelCase					:      Dict          =   TFDeiTModelTester(self )
			lowerCamelCase					:      Optional[int]          =   ConfigTester(self		,			config_class=UpperCAmelCase_		,			has_text_modality=UpperCAmelCase_		,			hidden_size=37 )




	def       _UpperCamelCase   (			self )		->							Optional[int]:
			self.config_tester.run_common_tests()




	@unittest.skip(reason='DeiT does not use inputs_embeds' )
	def       _UpperCamelCase   (			self )		->							Tuple:
			pass




	def       _UpperCamelCase   (			self )		->							int:
			lowerCamelCase					:      Optional[Any]          =   self.model_tester.prepare_config_and_inputs_for_common()

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




	def       _UpperCamelCase   (			self )		->							int:
			lowerCamelCase					:      Optional[int]          =   self.model_tester.prepare_config_and_inputs_for_common()

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

					lowerCamelCase					:      List[str]          =   ['pixel_values']
					self.assertListEqual(arg_names[:1]		,			UpperCAmelCase_ )




	def       _UpperCamelCase   (			self )		->							Union[str, Any]:
			lowerCamelCase					:      Union[str, Any]          =   self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_model(*UpperCAmelCase_ )




	def       _UpperCamelCase   (			self )		->							List[str]:
			lowerCamelCase					:      List[str]          =   self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_masked_image_modeling(*UpperCAmelCase_ )




	def       _UpperCamelCase   (			self )		->							Dict:
			lowerCamelCase					:      Dict          =   self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_image_classification(*UpperCAmelCase_ )




	def       _UpperCamelCase   (			self		,			UpperCAmelCase_		,			UpperCAmelCase_		,			UpperCAmelCase_=False )		->							List[Any]:
			lowerCamelCase					:      Union[str, 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       _UpperCamelCase   (			self )		->							List[Any]:
			for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					lowerCamelCase					:      Dict          =   TFDeiTModel.from_pretrained(UpperCAmelCase_ )
					self.assertIsNotNone(UpperCAmelCase_ )


def 		UpperCAmelCase_ (		):





		'''simple docstring'''


		lowerCamelCase					:      Union[str, Any]          =   Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'      )
		return image






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




	@cached_property
	def       _UpperCamelCase   (			self )		->							Optional[Any]:
			return (
			    DeiTImageProcessor.from_pretrained('facebook/deit-base-distilled-patch16-224' )
			    if is_vision_available()
			    else None
			)




	@slow
	def       _UpperCamelCase   (			self )		->							Optional[Any]:
			lowerCamelCase					:      Any          =   TFDeiTForImageClassificationWithTeacher.from_pretrained('facebook/deit-base-distilled-patch16-224' )

			lowerCamelCase					:      List[str]          =   self.default_image_processor
			lowerCamelCase					:      Dict          =   prepare_img()
			lowerCamelCase					:      Any          =   image_processor(images=UpperCAmelCase_		,			return_tensors='tf' )

			# forward pass
			lowerCamelCase					:      List[str]          =   model(**UpperCAmelCase_ )

			# verify the logits
			lowerCamelCase					:      Dict          =   tf.TensorShape((1, 1000) )
			self.assertEqual(outputs.logits.shape		,			UpperCAmelCase_ )

			lowerCamelCase					:      str          =   tf.constant([-1.0266, 0.1912, -1.2861] )

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








"""simple docstring"""

import functools
import operator

from ...configuration_utils import PretrainedConfig
from ...utils import logging


_A						=			logging.get_logger(__name__)

_A						=			{
    'asapp/sew-d-tiny-100k': 'https://huggingface.co/asapp/sew-d-tiny-100k/resolve/main/config.json',
    # See all SEW-D models at https://huggingface.co/models?filter=sew-d
}






class 	_lowercase       (    __UpperCAmelCase		):
	lowercase_				=     'sew-d'




	def __init__(			self		,			UpperCAmelCase_=32		,			UpperCAmelCase_=768		,			UpperCAmelCase_=12		,			UpperCAmelCase_=12		,			UpperCAmelCase_=3072		,			UpperCAmelCase_=2		,			UpperCAmelCase_=512		,			UpperCAmelCase_=256		,			UpperCAmelCase_=True		,			UpperCAmelCase_=True		,			UpperCAmelCase_=("p2c", "c2p")		,			UpperCAmelCase_="layer_norm"		,			UpperCAmelCase_="gelu_python"		,			UpperCAmelCase_=0.1		,			UpperCAmelCase_=0.1		,			UpperCAmelCase_=0.1		,			UpperCAmelCase_=0.0		,			UpperCAmelCase_=0.1		,			UpperCAmelCase_=0.02		,			UpperCAmelCase_=1E-7		,			UpperCAmelCase_=1E-5		,			UpperCAmelCase_="group"		,			UpperCAmelCase_="gelu"		,			UpperCAmelCase_=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512)		,			UpperCAmelCase_=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1)		,			UpperCAmelCase_=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1)		,			UpperCAmelCase_=False		,			UpperCAmelCase_=128		,			UpperCAmelCase_=16		,			UpperCAmelCase_=True		,			UpperCAmelCase_=0.05		,			UpperCAmelCase_=10		,			UpperCAmelCase_=2		,			UpperCAmelCase_=0.0		,			UpperCAmelCase_=10		,			UpperCAmelCase_=0		,			UpperCAmelCase_="mean"		,			UpperCAmelCase_=False		,			UpperCAmelCase_=False		,			UpperCAmelCase_=256		,			UpperCAmelCase_=0		,			UpperCAmelCase_=1		,			UpperCAmelCase_=2		,			**UpperCAmelCase_		,			)		->							Optional[Any]:
			super().__init__(**UpperCAmelCase_		,			pad_token_id=UpperCAmelCase_		,			bos_token_id=UpperCAmelCase_		,			eos_token_id=UpperCAmelCase_ )
			lowerCamelCase					:      Any          =   hidden_size
			lowerCamelCase					:      Any          =   feat_extract_norm
			lowerCamelCase					:      List[str]          =   feat_extract_activation
			lowerCamelCase					:      str          =   list(UpperCAmelCase_ )
			lowerCamelCase					:      Any          =   list(UpperCAmelCase_ )
			lowerCamelCase					:      str          =   list(UpperCAmelCase_ )
			lowerCamelCase					:      List[Any]          =   conv_bias
			lowerCamelCase					:      Optional[int]          =   num_conv_pos_embeddings
			lowerCamelCase					:      str          =   num_conv_pos_embedding_groups
			lowerCamelCase					:      Optional[int]          =   len(self.conv_dim )
			lowerCamelCase					:      Optional[int]          =   num_hidden_layers
			lowerCamelCase					:      Union[str, Any]          =   intermediate_size
			lowerCamelCase					:      str          =   squeeze_factor
			lowerCamelCase					:      Any          =   max_position_embeddings
			lowerCamelCase					:      List[Any]          =   position_buckets
			lowerCamelCase					:      Union[str, Any]          =   share_att_key
			lowerCamelCase					:      Optional[int]          =   relative_attention
			lowerCamelCase					:      Tuple          =   norm_rel_ebd
			lowerCamelCase					:      Union[str, Any]          =   list(UpperCAmelCase_ )
			lowerCamelCase					:      List[Any]          =   hidden_act
			lowerCamelCase					:      Optional[Any]          =   num_attention_heads
			lowerCamelCase					:      Tuple          =   hidden_dropout
			lowerCamelCase					:      List[Any]          =   attention_dropout
			lowerCamelCase					:      Optional[Any]          =   activation_dropout
			lowerCamelCase					:      List[str]          =   feat_proj_dropout
			lowerCamelCase					:      List[str]          =   final_dropout
			lowerCamelCase					:      str          =   layer_norm_eps
			lowerCamelCase					:      int          =   feature_layer_norm_eps
			lowerCamelCase					:      Optional[Any]          =   initializer_range
			lowerCamelCase					:      int          =   vocab_size

			if (
			    (len(self.conv_stride ) != self.num_feat_extract_layers)
			    or (len(self.conv_kernel ) != self.num_feat_extract_layers)
			    or (len(self.conv_dim ) != self.num_feat_extract_layers)
			):
					raise ValueError(
					    'Configuration for convolutional layers is incorrect.'
					    'It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,'
					    F"""but is `len(config.conv_dim) = {len(self.conv_dim )}`, `len(config.conv_stride)"""
					    F"""= {len(self.conv_stride )}`, `len(config.conv_kernel) = {len(self.conv_kernel )}`.""" )

			# fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
			lowerCamelCase					:      Any          =   apply_spec_augment
			lowerCamelCase					:      Optional[int]          =   mask_time_prob
			lowerCamelCase					:      Optional[Any]          =   mask_time_length
			lowerCamelCase					:      str          =   mask_time_min_masks
			lowerCamelCase					:      List[Any]          =   mask_feature_prob
			lowerCamelCase					:      int          =   mask_feature_length
			lowerCamelCase					:      List[Any]          =   mask_feature_min_masks

			# ctc loss
			lowerCamelCase					:      Optional[Any]          =   ctc_loss_reduction
			lowerCamelCase					:      Union[str, Any]          =   ctc_zero_infinity

			# sequence classification
			lowerCamelCase					:      Optional[Any]          =   use_weighted_layer_sum
			lowerCamelCase					:      Dict          =   classifier_proj_size




	@property
	def       _UpperCamelCase   (			self )		->							Optional[Any]:
			return functools.reduce(operator.mul		,			self.conv_stride		,			1 )






