





"""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  __UpperCamelCase       (		a__		):






							def 					__a     (     self   )			->				Optional[Any]:
								a   :	Tuple			=       self.config_class(**self.inputs_dict   )
								self.parent.assertTrue(hasattr(lowerCAmelCase__   ,      "embed_dim"   )   )
								self.parent.assertTrue(hasattr(lowerCAmelCase__   ,      "num_heads"   )   )


class  __UpperCamelCase       :






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






							def 					__a     (     self   )			->				Any:
								a   :	Dict			=       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

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

								a   :	int			=       self.get_config()
								return config, pixel_values, labels






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






							def 					__a     (     self   )			->				Tuple:
								a   :	List[str]			=       self.prepare_config_and_inputs()
								a   :	Optional[int]			=       config_and_inputs
								a   :	int			=       {"pixel_values": pixel_values}
								return config, inputs_dict


@require_tf
class  __UpperCamelCase       (		a__	, a__	, unittest.TestCase		):
							lowerCamelCase     :      List[str]         =(TFCvtModel, TFCvtForImageClassification) if is_tf_available() else ()
							lowerCamelCase     :      List[Any]         =(
							    {"""feature-extraction""": TFCvtModel, """image-classification""": TFCvtForImageClassification}
							    if is_tf_available()
							    else {}
							)
							lowerCamelCase     :      Optional[Any]         =False
							lowerCamelCase     :      List[str]         =False
							lowerCamelCase     :      Optional[Any]         =False
							lowerCamelCase     :      int         =False
							lowerCamelCase     :      Dict         =False






							def 					__a     (     self   )			->				Optional[int]:
								a   :	Dict			=       TFCvtModelTester(self   )
								a   :	List[Any]			=       TFCvtConfigTester(self   ,      config_class=lowerCAmelCase__   ,      has_text_modality=lowerCAmelCase__   ,      hidden_size=37   )






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






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






							@unittest.skip(reason="Cvt does not support input and output embeddings"   )
							def 					__a     (     self   )			->				Union[str, Any]:
								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 					__a     (     self   )			->				List[Any]:
								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 					__a     (     self   )			->				Optional[int]:
								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 					__a     (     self   )			->				int:
								a   :	Optional[int]			=       tf.keras.mixed_precision.Policy("mixed_float16"   )
								tf.keras.mixed_precision.set_global_policy(lowerCAmelCase__   )
								super().test_keras_fit()
								tf.keras.mixed_precision.set_global_policy("float32"   )






							def 					__a     (     self   )			->				Any:
								a   :	Dict			=       self.model_tester.prepare_config_and_inputs_for_common()

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

									a   :	Union[str, Any]			=       ["pixel_values"]
									self.assertListEqual(arg_names[:1]   ,      lowerCAmelCase__   )






							def 					__a     (     self   )			->				int:
								def check_hidden_states_output(lowerCAmelCase__   ,      lowerCAmelCase__   ,      lowerCAmelCase__   ):
									a   :	Union[str, Any]			=       model_class(lowerCAmelCase__   )

									a   :	Union[str, Any]			=       model(**self._prepare_for_class(lowerCAmelCase__   ,      lowerCAmelCase__   )   )
									a   :	Dict			=       outputs.hidden_states

									a   :	Any			=       len(self.model_tester.depth   )
									self.assertEqual(len(lowerCAmelCase__   )   ,      lowerCAmelCase__   )

									# 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,
									    ]   ,      )

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

								for model_class in self.all_model_classes:
									a   :	Dict			=       True
									check_hidden_states_output(lowerCAmelCase__   ,      lowerCAmelCase__   ,      lowerCAmelCase__   )

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

									check_hidden_states_output(lowerCAmelCase__   ,      lowerCAmelCase__   ,      lowerCAmelCase__   )






							def 					__a     (     self   )			->				Union[str, Any]:
								a   :	Tuple			=       self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*lowerCAmelCase__   )






							def 					__a     (     self   )			->				Any:
								a   :	Dict			=       self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase__   )






							@slow
							def 					__a     (     self   )			->				str:
								for model_name in TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									a   :	Tuple			=       TFCvtModel.from_pretrained(lowerCAmelCase__   )
									self.assertIsNotNone(lowerCAmelCase__   )






def    _SCREAMING_SNAKE_CASE	(			)				->int:






	'''simple docstring'''






	a   :	Optional[Any]			=       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"      )
	return image







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






							@cached_property
							def 					__a     (     self   )			->				Optional[Any]:
								return AutoImageProcessor.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]   )






							@slow
							def 					__a     (     self   )			->				Optional[Any]:
								a   :	Union[str, Any]			=       TFCvtForImageClassification.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]   )

								a   :	Any			=       self.default_image_processor
								a   :	Any			=       prepare_img()
								a   :	str			=       image_processor(images=lowerCAmelCase__   ,      return_tensors="tf"   )

								# forward pass
								a   :	List[str]			=       model(**lowerCAmelCase__   )

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

								a   :	str			=       tf.constant([0.9_285, 0.9_015, -0.3_150]   )
								self.assertTrue(np.allclose(outputs.logits[0, :3].numpy()   ,      lowerCAmelCase__   ,      atol=1E-4   )   )









"""simple docstring"""




from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


if is_sentencepiece_available():
		from ..ta.tokenization_ta import TaTokenizer
else:
		from ...utils.dummy_sentencepiece_objects import TaTokenizer

a		:				List[Any]  			=    TaTokenizer

if is_tokenizers_available():
		from ..ta.tokenization_ta_fast import TaTokenizerFast
else:
		from ...utils.dummy_tokenizers_objects import TaTokenizerFast

a		:				Union[str, Any]  			=    TaTokenizerFast

a		:				Union[str, Any]  			=    {'''configuration_mt5''': ['''MT5Config''', '''MT5OnnxConfig''']}

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		a		:				str  			=    [
		    '''MT5EncoderModel''',
		    '''MT5ForConditionalGeneration''',
		    '''MT5ForQuestionAnswering''',
		    '''MT5Model''',
		    '''MT5PreTrainedModel''',
		    '''MT5Stack''',
		]

try:
		if not is_tf_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		a		:				List[str]  			=    ['''TFMT5EncoderModel''', '''TFMT5ForConditionalGeneration''', '''TFMT5Model''']

try:
		if not is_flax_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		a		:				Optional[Any]  			=    ['''FlaxMT5EncoderModel''', '''FlaxMT5ForConditionalGeneration''', '''FlaxMT5Model''']


if TYPE_CHECKING:
		from .configuration_mta import MTaConfig, MTaOnnxConfig

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_mta import (
				    MTaEncoderModel,
				    MTaForConditionalGeneration,
				    MTaForQuestionAnswering,
				    MTaModel,
				    MTaPreTrainedModel,
				    MTaStack,
				)

		try:
				if not is_tf_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_tf_mta import TFMTaEncoderModel, TFMTaForConditionalGeneration, TFMTaModel

		try:
				if not is_flax_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_flax_mta import FlaxMTaEncoderModel, FlaxMTaForConditionalGeneration, FlaxMTaModel

else:
		import sys

		a		:				List[Any]  			=    _LazyModule(
		    __name__,
		    globals()['''__file__'''],
		    _import_structure,
		    extra_objects={'''MT5Tokenizer''': MTaTokenizer, '''MT5TokenizerFast''': MTaTokenizerFast},
		    module_spec=__spec__,
		)


