'''simple docstring'''




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     __magic_name__						(    lowerCAmelCase_):







			def 	SCREAMING_SNAKE_CASE_     (	self				:  Any    ):
										lowercase_					:			Optional[Any]										=       self.config_class(**self.inputs_dict    )
										self.parent.assertTrue(hasattr(__a				,			"""embed_dim"""    )    )
										self.parent.assertTrue(hasattr(__a				,			"""num_heads"""    )    )





class     __magic_name__						:







			def __init__(	self				:  Union[str, Any]				,			lowercase_				:  Union[str, Any]				,			lowercase_				:  str=13				,			lowercase_				:  List[Any]=64				,			lowercase_				:  Dict=3				,			lowercase_				:  List[str]=[16, 48, 96]				,			lowercase_				:  str=[1, 3, 6]				,			lowercase_				:  Union[str, Any]=[1, 2, 10]				,			lowercase_				:  str=[7, 3, 3]				,			lowercase_				:  List[Any]=[4, 2, 2]				,			lowercase_				:  int=[2, 1, 1]				,			lowercase_				:  Tuple=[2, 2, 2]				,			lowercase_				:  Tuple=[False, False, True]				,			lowercase_				:  Tuple=[0.0, 0.0, 0.0]				,			lowercase_				:  List[Any]=0.02				,			lowercase_				:  Optional[Any]=1E-12				,			lowercase_				:  int=True				,			lowercase_				:  List[str]=True				,			lowercase_				:  Any=2				,			):
										lowercase_					:			str										=       parent
										lowercase_					:			List[Any]										=       batch_size
										lowercase_					:			Optional[int]										=       image_size
										lowercase_					:			List[str]										=       patch_sizes
										lowercase_					:			str										=       patch_stride
										lowercase_					:			Any										=       patch_padding
										lowercase_					:			Dict										=       is_training
										lowercase_					:			Union[str, Any]										=       use_labels
										lowercase_					:			Dict										=       num_labels
										lowercase_					:			List[Any]										=       num_channels
										lowercase_					:			Any										=       embed_dim
										lowercase_					:			int										=       num_heads
										lowercase_					:			Optional[int]										=       stride_kv
										lowercase_					:			Dict										=       depth
										lowercase_					:			List[str]										=       cls_token
										lowercase_					:			List[Any]										=       attention_drop_rate
										lowercase_					:			Tuple										=       initializer_range
										lowercase_					:			int										=       layer_norm_eps







			def 	SCREAMING_SNAKE_CASE_     (	self				:  Tuple    ):
										lowercase_					:			str										=       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

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

										lowercase_					:			str										=       self.get_config()
										return config, pixel_values, labels







			def 	SCREAMING_SNAKE_CASE_     (	self				:  Optional[int]    ):
										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 	SCREAMING_SNAKE_CASE_     (	self				:  Any				,			lowercase_				:  Optional[Any]				,			lowercase_				:  Union[str, Any]				,			lowercase_				:  Tuple    ):
										lowercase_					:			Optional[int]										=       TFCvtModel(config=__a    )
										lowercase_					:			Dict										=       model(__a				,			training=__a    )
										lowercase_					:			Any										=       (self.image_size, self.image_size)
										lowercase_					:			Dict										=       image_size[0], image_size[1]
										for i in range(len(self.depth    )    ):
																	lowercase_					:			Tuple										=       floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1    )
																	lowercase_					:			str										=       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 	SCREAMING_SNAKE_CASE_     (	self				:  Dict				,			lowercase_				:  str				,			lowercase_				:  Optional[int]				,			lowercase_				:  List[str]    ):
										lowercase_					:			List[Any]										=       self.num_labels
										lowercase_					:			Optional[int]										=       TFCvtForImageClassification(__a    )
										lowercase_					:			Dict										=       model(__a				,			labels=__a				,			training=__a    )
										self.parent.assertEqual(result.logits.shape				,			(self.batch_size, self.num_labels)    )







			def 	SCREAMING_SNAKE_CASE_     (	self				:  Any    ):
										lowercase_					:			Optional[Any]										=       self.prepare_config_and_inputs()
										lowercase_					:			Tuple										=       config_and_inputs
										lowercase_					:			str										=       {'pixel_values': pixel_values}
										return config, inputs_dict





@require_tf
class     __magic_name__						(    lowerCAmelCase_,     lowerCAmelCase_,     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 	SCREAMING_SNAKE_CASE_     (	self				:  Union[str, Any]    ):
										lowercase_					:			int										=       TFCvtModelTester(self    )
										lowercase_					:			List[Any]										=       TFCvtConfigTester(self				,			config_class=__a				,			has_text_modality=__a				,			hidden_size=37    )







			def 	SCREAMING_SNAKE_CASE_     (	self				:  Optional[int]    ):
										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 	SCREAMING_SNAKE_CASE_     (	self				:  Optional[Any]    ):
										pass







			@unittest.skip(reason="""Cvt does not use inputs_embeds"""    )
			def 	SCREAMING_SNAKE_CASE_     (	self				:  List[str]    ):
										pass







			@unittest.skip(reason="""Cvt does not support input and output embeddings"""    )
			def 	SCREAMING_SNAKE_CASE_     (	self				:  Dict    ):
										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 	SCREAMING_SNAKE_CASE_     (	self				:  int    ):
										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 	SCREAMING_SNAKE_CASE_     (	self				:  List[Any]    ):
										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 	SCREAMING_SNAKE_CASE_     (	self				:  Optional[Any]    ):
										lowercase_					:			Any										=       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 	SCREAMING_SNAKE_CASE_     (	self				:  int    ):
										lowercase_					:			List[Any]										=       self.model_tester.prepare_config_and_inputs_for_common()

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

																	lowercase_					:			Optional[Any]										=       ['pixel_values']
																	self.assertListEqual(arg_names[:1]				,			__a    )







			def 	SCREAMING_SNAKE_CASE_     (	self				:  Any    ):





										def check_hidden_states_output(lowercase_				:  str				,			lowercase_				:  Dict				,			lowercase_				:  Optional[int]    ):
																	lowercase_					:			List[str]										=       model_class(__a    )

																	lowercase_					:			Union[str, Any]										=       model(**self._prepare_for_class(__a				,			__a    )    )
																	lowercase_					:			Any										=       outputs.hidden_states

																	lowercase_					:			Union[str, Any]										=       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,
																	    ]				,			)

										lowercase_					:			Optional[int]										=       self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
																	lowercase_					:			List[str]										=       True
																	check_hidden_states_output(__a				,			__a				,			__a    )

																	# check that output_hidden_states also work using config
																	del inputs_dict["output_hidden_states"]
																	lowercase_					:			Optional[Any]										=       True

																	check_hidden_states_output(__a				,			__a				,			__a    )







			def 	SCREAMING_SNAKE_CASE_     (	self				:  Any    ):
										lowercase_					:			int										=       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*__a    )







			def 	SCREAMING_SNAKE_CASE_     (	self				:  Optional[Any]    ):
										lowercase_					:			Dict										=       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*__a    )







			@slow
			def 	SCREAMING_SNAKE_CASE_     (	self				:  int    ):





										for model_name in TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	lowercase_					:			Optional[Any]										=       TFCvtModel.from_pretrained(__a    )
																	self.assertIsNotNone(__a    )




def 							lowerCamelCase	(      )			->      Optional[Any]:
							lowercase_					:			List[Any]										=       Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" )
							return image





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







			@cached_property
			def 	SCREAMING_SNAKE_CASE_     (	self				:  List[str]    ):
										return AutoImageProcessor.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]    )







			@slow
			def 	SCREAMING_SNAKE_CASE_     (	self				:  Any    ):
										lowercase_					:			int										=       TFCvtForImageClassification.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]    )

										lowercase_					:			Tuple										=       self.default_image_processor
										lowercase_					:			Any										=       prepare_img()
										lowercase_					:			int										=       image_processor(images=__a				,			return_tensors="""tf"""    )

										# forward pass
										lowercase_					:			Any										=       model(**__a    )

										# verify the logits
										lowercase_					:			Any										=       tf.TensorShape((1, 1000)    )
										self.assertEqual(outputs.logits.shape				,			__a    )

										lowercase_					:			Optional[Any]										=       tf.constant([0.92_85, 0.90_15, -0.31_50]    )
										self.assertTrue(np.allclose(outputs.logits[0, :3].numpy()				,			__a				,			atol=1E-4    )    )









'''simple docstring'''



from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxSeqaSeqConfigWithPast
from ...utils import logging


__lowercase	:						Dict       =	logging.get_logger(__name__)

__lowercase	:						Optional[Any]       =	{
    'google/umt5-small': 'https://huggingface.co/google/umt5-small/resolve/main/config.json',
    # See all umt5 models at https://huggingface.co/models?filter=umt5
}





class 							__UpperCamelCase				(		lowerCAmelCase_					):
	A_				=    "umt5"
	A_				=    ["past_key_values"]




	def __init__(   self						,				__a=25_0112						,				__a=512						,				__a=64						,				__a=1024						,				__a=8						,				__a=None						,				__a=6						,				__a=32						,				__a=128						,				__a=0.1						,				__a=1E-6						,				__a=1.0						,				__a="gated-gelu"						,				__a=True						,				__a=True						,				__a="T5Tokenizer"						,				__a=True						,				__a=0						,				__a=1						,				__a=0						,				**__a						,				):

						'''simple docstring'''





						super().__init__(
						    is_encoder_decoder=__a						,				tokenizer_class=__a						,				tie_word_embeddings=__a						,				pad_token_id=__a						,				eos_token_id=__a						,				decoder_start_token_id=__a						,				**__a						,				)
						__a			:       Any					    =  vocab_size
						__a			:       Any					    =  d_model
						__a			:       str					    =  d_kv
						__a			:       Dict					    =  d_ff
						__a			:       Union[str, Any]					    =  num_layers
						__a			:       int					    =  (
						    num_decoder_layers if num_decoder_layers is not None else self.num_layers
						)  # default = symmetry
						__a			:       Optional[int]					    =  num_heads
						__a			:       Tuple					    =  relative_attention_num_buckets
						__a			:       Optional[Any]					    =  relative_attention_max_distance
						__a			:       Optional[int]					    =  dropout_rate
						__a			:       List[Any]					    =  layer_norm_epsilon
						__a			:       int					    =  initializer_factor
						__a			:       Union[str, Any]					    =  feed_forward_proj
						__a			:       Any					    =  use_cache

						__a			:       List[Any]					    =  self.feed_forward_proj.split('-'							)
						__a			:       Dict					    =  act_info[-1]
						__a			:       Dict					    =  act_info[0] == 'gated'

						if len(__a							) > 1 and act_info[0] != "gated" or len(__a							) > 2:
											raise ValueError(
											    f"""`feed_forward_proj`: {feed_forward_proj} is not a valid activation function of the dense layer."""
											    'Please make sure `feed_forward_proj` is of the format `gated-{ACT_FN}` or `{ACT_FN}`, e.g. '
											    '\'gated-gelu\' or \'relu\''							)

						if feed_forward_proj == "gated-gelu":
											__a			:       Optional[int]					    =  'gelu_new'




	@property
	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						return self.d_model




	@property
	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						return self.num_heads





	@property
	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						return self.num_layers





class 							__UpperCamelCase				(		lowerCAmelCase_					):




	@property
	# Copied from transformers.models.t5.configuration_t5.T5OnnxConfig.inputs
	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       str					    =  {
						    'input_ids': {0: 'batch', 1: 'encoder_sequence'},
						    'attention_mask': {0: 'batch', 1: 'encoder_sequence'},
						}
						if self.use_past:
											__a			:       Dict					    =  'past_encoder_sequence + sequence'
											__a			:       Tuple					    =  {0: 'batch'}
											__a			:       Tuple					    =  {0: 'batch', 1: 'past_decoder_sequence + sequence'}
						else:
											__a			:       List[Any]					    =  {0: 'batch', 1: 'decoder_sequence'}
											__a			:       int					    =  {0: 'batch', 1: 'decoder_sequence'}

						if self.use_past:
											self.fill_with_past_key_values_(__a						,				direction='inputs'							)

						return common_inputs




	@property
	# Copied from transformers.models.t5.configuration_t5.T5OnnxConfig.default_onnx_opset
	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						return 13





	@property
	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						return 5E-4



