



from __future__ import annotations

import inspect
import unittest
from math import floor

import numpy as np

from transformers import CvtConfig
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 TFCvtForImageClassification, TFCvtModel
							from transformers.models.cvt.modeling_tf_cvt import TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
							from PIL import Image

							from transformers import AutoImageProcessor







class 			__lowerCAmelCase    (			UpperCAmelCase__						):



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



						"""simple docstring"""






						_UpperCAmelCase		       =   self.config_class(**self.inputs_dict)
						self.parent.assertTrue(hasattr(_a ,		'embed_dim'))
						self.parent.assertTrue(hasattr(_a ,		'num_heads'))







class 			__lowerCAmelCase    :



			def __init__(						self   :  Dict ,		A   :  str ,		A   :  Dict=13 ,		A   :  List[str]=64 ,		A   :  int=3 ,		A   :  List[Any]=[16, 48, 96] ,		A   :  int=[1, 3, 6] ,		A   :  Dict=[1, 2, 10] ,		A   :  Tuple=[7, 3, 3] ,		A   :  List[Any]=[4, 2, 2] ,		A   :  Union[str, Any]=[2, 1, 1] ,		A   :  List[str]=[2, 2, 2] ,		A   :  List[str]=[False, False, True] ,		A   :  int=[0.0, 0.0, 0.0] ,		A   :  str=0.0_2 ,		A   :  Tuple=1E-12 ,		A   :  Union[str, Any]=True ,		A   :  List[Any]=True ,		A   :  Tuple=2 ,		)   ->    Optional[Any]:



						"""simple docstring"""






						_UpperCAmelCase		       =   parent
						_UpperCAmelCase		       =   batch_size
						_UpperCAmelCase		       =   image_size
						_UpperCAmelCase		       =   patch_sizes
						_UpperCAmelCase		       =   patch_stride
						_UpperCAmelCase		       =   patch_padding
						_UpperCAmelCase		       =   is_training
						_UpperCAmelCase		       =   use_labels
						_UpperCAmelCase		       =   num_labels
						_UpperCAmelCase		       =   num_channels
						_UpperCAmelCase		       =   embed_dim
						_UpperCAmelCase		       =   num_heads
						_UpperCAmelCase		       =   stride_kv
						_UpperCAmelCase		       =   depth
						_UpperCAmelCase		       =   cls_token
						_UpperCAmelCase		       =   attention_drop_rate
						_UpperCAmelCase		       =   initializer_range
						_UpperCAmelCase		       =   layer_norm_eps



			def     _lowerCamelCase       (						self   :  Optional[int])   ->    Union[str, Any]:



						"""simple docstring"""






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

						_UpperCAmelCase		       =   None
						if self.use_labels:
									# create a random int32 tensor of given shape
									_UpperCAmelCase		       =   ids_tensor([self.batch_size] ,		self.num_labels)

						_UpperCAmelCase		       =   self.get_config()
						return config, pixel_values, labels



			def     _lowerCamelCase       (						self   :  List[str])   ->    int:



						"""simple docstring"""






						return CvtConfig(
						    image_size=self.image_size ,		num_labels=self.num_labels ,		num_channels=self.num_channels ,		embed_dim=self.embed_dim ,		num_heads=self.num_heads ,		patch_sizes=self.patch_sizes ,		patch_padding=self.patch_padding ,		patch_stride=self.patch_stride ,		stride_kv=self.stride_kv ,		depth=self.depth ,		cls_token=self.cls_token ,		attention_drop_rate=self.attention_drop_rate ,		initializer_range=self.initializer_range ,		)



			def     _lowerCamelCase       (						self   :  Tuple ,		A   :  List[Any] ,		A   :  Dict ,		A   :  Tuple)   ->    Dict:



						"""simple docstring"""






						_UpperCAmelCase		       =   TFCvtModel(config=_a)
						_UpperCAmelCase		       =   model(_a ,		training=_a)
						_UpperCAmelCase		       =   (self.image_size, self.image_size)
						_UpperCAmelCase       ,     _UpperCAmelCase		       =   image_size[0], image_size[1]
						for i in range(len(self.depth)):
									_UpperCAmelCase		       =   floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1)
									_UpperCAmelCase		       =   floor(((width + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1)
						self.parent.assertEqual(result.last_hidden_state.shape ,		(self.batch_size, self.embed_dim[-1], height, width))



			def     _lowerCamelCase       (						self   :  Optional[int] ,		A   :  str ,		A   :  str ,		A   :  Union[str, Any])   ->    Optional[Any]:



						"""simple docstring"""






						_UpperCAmelCase		       =   self.num_labels
						_UpperCAmelCase		       =   TFCvtForImageClassification(_a)
						_UpperCAmelCase		       =   model(_a ,		labels=_a ,		training=_a)
						self.parent.assertEqual(result.logits.shape ,		(self.batch_size, self.num_labels))



			def     _lowerCamelCase       (						self   :  str)   ->    str:



						"""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    (			UpperCAmelCase__				,						UpperCAmelCase__				,						unittest.TestCase						):
			UpperCamelCase   					=     (TFCvtModel, TFCvtForImageClassification) if is_tf_available() else ()
			UpperCamelCase   					=     (
			    {'''feature-extraction''': TFCvtModel, '''image-classification''': TFCvtForImageClassification}
			    if is_tf_available()
			    else {}
			)
			UpperCamelCase   					=     False
			UpperCamelCase   					=     False
			UpperCamelCase   					=     False
			UpperCamelCase   					=     False
			UpperCamelCase   					=     False



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



						"""simple docstring"""






						_UpperCAmelCase		       =   TFCvtModelTester(self)
						_UpperCAmelCase		       =   TFCvtConfigTester(self ,		config_class=_a ,		has_text_modality=_a ,		hidden_size=37)



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



						"""simple docstring"""






						self.config_tester.create_and_test_config_common_properties()
						self.config_tester.create_and_test_config_to_json_string()
						self.config_tester.create_and_test_config_to_json_file()
						self.config_tester.create_and_test_config_from_and_save_pretrained()
						self.config_tester.create_and_test_config_with_num_labels()
						self.config_tester.check_config_can_be_init_without_params()
						self.config_tester.check_config_arguments_init()



			@unittest.skip(reason='Cvt does not output attentions')
			def     _lowerCamelCase       (						self   :  Dict)   ->    Tuple:



						"""simple docstring"""






						pass



			@unittest.skip(reason='Cvt does not use inputs_embeds')
			def     _lowerCamelCase       (						self   :  List[str])   ->    Union[str, Any]:



						"""simple docstring"""






						pass



			@unittest.skip(reason='Cvt does not support input and output embeddings')
			def     _lowerCamelCase       (						self   :  str)   ->    Optional[int]:



						"""simple docstring"""






						pass



			@unittest.skipIf(
			    not is_tf_available() or len(tf.config.list_physical_devices('GPU')) == 0 ,		reason='TF does not support backprop for grouped convolutions on CPU.' ,		)
			def     _lowerCamelCase       (						self   :  Optional[int])   ->    Optional[int]:



						"""simple docstring"""






						super().test_dataset_conversion()



			@unittest.skipIf(
			    not is_tf_available() or len(tf.config.list_physical_devices('GPU')) == 0 ,		reason='TF does not support backprop for grouped convolutions on CPU.' ,		)
			@slow
			def     _lowerCamelCase       (						self   :  Optional[int])   ->    Optional[Any]:



						"""simple docstring"""






						super().test_keras_fit()



			@unittest.skip(reason='Get `Failed to determine best cudnn convolution algo.` error after using TF 2.12+cuda 11.8')
			def     _lowerCamelCase       (						self   :  Optional[Any])   ->    Optional[Any]:



						"""simple docstring"""






						_UpperCAmelCase		       =   tf.keras.mixed_precision.Policy('mixed_float16')
						tf.keras.mixed_precision.set_global_policy(_a)
						super().test_keras_fit()
						tf.keras.mixed_precision.set_global_policy('float32')



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



						"""simple docstring"""






						_UpperCAmelCase       ,     _UpperCAmelCase		       =   self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
									_UpperCAmelCase		       =   model_class(_a)
									_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] ,		_a)



			def     _lowerCamelCase       (						self   :  str)   ->    str:



						"""simple docstring"""






						def check_hidden_states_output(A   :  Tuple ,		A   :  Tuple ,		A   :  int):
									_UpperCAmelCase		       =   model_class(_a)

									_UpperCAmelCase		       =   model(**self._prepare_for_class(_a ,		_a))
									_UpperCAmelCase		       =   outputs.hidden_states

									_UpperCAmelCase		       =   len(self.model_tester.depth)
									self.assertEqual(len(_a) ,		_a)

									# verify the first hidden states (first block)
									self.assertListEqual(
									    list(hidden_states[0].shape[-3:]) ,		[
									        self.model_tester.embed_dim[0],
									        self.model_tester.image_size // 4,
									        self.model_tester.image_size // 4,
									    ] ,		)

						_UpperCAmelCase       ,     _UpperCAmelCase		       =   self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
									_UpperCAmelCase		       =   True
									check_hidden_states_output(_a ,		_a ,		_a)

									# check that output_hidden_states also work using config
									del inputs_dict["output_hidden_states"]
									_UpperCAmelCase		       =   True

									check_hidden_states_output(_a ,		_a ,		_a)



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



						"""simple docstring"""






						_UpperCAmelCase		       =   self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*_a)



			def     _lowerCamelCase       (						self   :  Any)   ->    Dict:



						"""simple docstring"""






						_UpperCAmelCase		       =   self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*_a)



			@slow
			def     _lowerCamelCase       (						self   :  Tuple)   ->    Optional[Any]:



						"""simple docstring"""






						for model_name in TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									_UpperCAmelCase		       =   TFCvtModel.from_pretrained(_a)
									self.assertIsNotNone(_a)





def 							A	(     )				-> Optional[int]:







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







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



			@cached_property
			def     _lowerCamelCase       (						self   :  Any)   ->    Any:



						"""simple docstring"""






						return AutoImageProcessor.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0])



			@slow
			def     _lowerCamelCase       (						self   :  Optional[int])   ->    List[str]:



						"""simple docstring"""






						_UpperCAmelCase		       =   TFCvtForImageClassification.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0])

						_UpperCAmelCase		       =   self.default_image_processor
						_UpperCAmelCase		       =   prepare_img()
						_UpperCAmelCase		       =   image_processor(images=_a ,		return_tensors='tf')

						# forward pass
						_UpperCAmelCase		       =   model(**_a)

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

						_UpperCAmelCase		       =   tf.constant([0.9_2_8_5, 0.9_0_1_5, -0.3_1_5_0])
						self.assertTrue(np.allclose(outputs.logits[0, :3].numpy() ,		_a ,		atol=1E-4))






"""simple docstring"""




import functools
import operator

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


lowerCAmelCase							:			int     							=							logging.get_logger(__name__)

lowerCAmelCase							:			List[str]     							=							{
    """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     __magic_name__							(       UpperCAmelCase__				):






				'''simple docstring'''
				__UpperCamelCase			  =						"sew-d"





				def __init__(       self		, _a=32		, _a=768		, _a=12		, _a=12		, _a=3_072		, _a=2		, _a=512		, _a=256		, _a=True		, _a=True		, _a=("p2c", "c2p")		, _a="layer_norm"		, _a="gelu_python"		, _a=0.1		, _a=0.1		, _a=0.1		, _a=0.0		, _a=0.1		, _a=0.02		, _a=1e-7		, _a=1e-5		, _a="group"		, _a="gelu"		, _a=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512)		, _a=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1)		, _a=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1)		, _a=False		, _a=128		, _a=16		, _a=True		, _a=0.05		, _a=10		, _a=2		, _a=0.0		, _a=10		, _a=0		, _a="mean"		, _a=False		, _a=False		, _a=256		, _a=0		, _a=1		, _a=2		, **_a		, ):





							"""simple docstring"""
							super().__init__(**_a		, pad_token_id=_a		, bos_token_id=_a		, eos_token_id=_a )
							lowerCamelCase			      =					hidden_size
							lowerCamelCase			      =					feat_extract_norm
							lowerCamelCase			      =					feat_extract_activation
							lowerCamelCase			      =					list(_a )
							lowerCamelCase			      =					list(_a )
							lowerCamelCase			      =					list(_a )
							lowerCamelCase			      =					conv_bias
							lowerCamelCase			      =					num_conv_pos_embeddings
							lowerCamelCase			      =					num_conv_pos_embedding_groups
							lowerCamelCase			      =					len(self.conv_dim )
							lowerCamelCase			      =					num_hidden_layers
							lowerCamelCase			      =					intermediate_size
							lowerCamelCase			      =					squeeze_factor
							lowerCamelCase			      =					max_position_embeddings
							lowerCamelCase			      =					position_buckets
							lowerCamelCase			      =					share_att_key
							lowerCamelCase			      =					relative_attention
							lowerCamelCase			      =					norm_rel_ebd
							lowerCamelCase			      =					list(_a )
							lowerCamelCase			      =					hidden_act
							lowerCamelCase			      =					num_attention_heads
							lowerCamelCase			      =					hidden_dropout
							lowerCamelCase			      =					attention_dropout
							lowerCamelCase			      =					activation_dropout
							lowerCamelCase			      =					feat_proj_dropout
							lowerCamelCase			      =					final_dropout
							lowerCamelCase			      =					layer_norm_eps
							lowerCamelCase			      =					feature_layer_norm_eps
							lowerCamelCase			      =					initializer_range
							lowerCamelCase			      =					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			      =					apply_spec_augment
							lowerCamelCase			      =					mask_time_prob
							lowerCamelCase			      =					mask_time_length
							lowerCamelCase			      =					mask_time_min_masks
							lowerCamelCase			      =					mask_feature_prob
							lowerCamelCase			      =					mask_feature_length
							lowerCamelCase			      =					mask_feature_min_masks

							# ctc loss
							lowerCamelCase			      =					ctc_loss_reduction
							lowerCamelCase			      =					ctc_zero_infinity

							# sequence classification
							lowerCamelCase			      =					use_weighted_layer_sum
							lowerCamelCase			      =					classifier_proj_size





				@property
				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							return functools.reduce(operator.mul		, self.conv_stride		, 1 )



