




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     snake_case__							(_A   ):
				"""simple docstring"""



				def 							__UpperCAmelCase							(      self				:     str							)					->     Dict:
							a    =			self.config_class(**self.inputs_dict							)
							self.parent.assertTrue(hasattr(__lowerCamelCase					,      "embed_dim"							)							)
							self.parent.assertTrue(hasattr(__lowerCamelCase					,      "num_heads"							)							)




class     snake_case__							:
				"""simple docstring"""



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





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

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

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





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





				def 							__UpperCAmelCase							(      self				:     Optional[Any]							)					->     Any:
							a    =			self.prepare_config_and_inputs()
							a    =			config_and_inputs
							a    =			{'''pixel_values''': pixel_values}
							return config, inputs_dict




@require_tf
class     snake_case__							(_A    ,       _A    ,       unittest.TestCase   ):
				"""simple docstring"""



				SCREAMING_SNAKE_CASE_       :							Optional[int]							 =					(TFCvtModel, TFCvtForImageClassification) if is_tf_available() else ()
				SCREAMING_SNAKE_CASE_       :							Union[str, Any]							 =					(
				    {"feature-extraction": TFCvtModel, "image-classification": TFCvtForImageClassification}
				    if is_tf_available()
				    else {}
				)
				SCREAMING_SNAKE_CASE_       :							Optional[Any]							 =					False
				SCREAMING_SNAKE_CASE_       :							Dict							 =					False
				SCREAMING_SNAKE_CASE_       :							Dict							 =					False
				SCREAMING_SNAKE_CASE_       :							List[Any]							 =					False
				SCREAMING_SNAKE_CASE_       :							List[str]							 =					False





				def 							__UpperCAmelCase							(      self				:     Optional[int]							)					->     Tuple:
							a    =			TFCvtModelTester(self							)
							a    =			TFCvtConfigTester(self					,      config_class=__lowerCamelCase					,      has_text_modality=__lowerCamelCase					,      hidden_size=37							)





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





				@unittest.skip(reason="Cvt does not use inputs_embeds"							)
				def 							__UpperCAmelCase							(      self				:     Dict							)					->     Optional[Any]:
							pass





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

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

										a    =			['''pixel_values''']
										self.assertListEqual(arg_names[:1]					,      __lowerCamelCase							)





				def 							__UpperCAmelCase							(      self				:     Tuple							)					->     str:
							def check_hidden_states_output(__lowerCamelCase				:     Dict					,      __lowerCamelCase				:     List[Any]					,      __lowerCamelCase				:     Optional[Any]							):
										a    =			model_class(__lowerCamelCase							)

										a    =			model(**self._prepare_for_class(__lowerCamelCase					,      __lowerCamelCase							)							)
										a    =			outputs.hidden_states

										a    =			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    =			self.model_tester.prepare_config_and_inputs_for_common()

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

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

										check_hidden_states_output(__lowerCamelCase					,      __lowerCamelCase					,      __lowerCamelCase							)





				def 							__UpperCAmelCase							(      self				:     int							)					->     List[str]:
							a    =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*__lowerCamelCase							)





				def 							__UpperCAmelCase							(      self				:     Optional[int]							)					->     Optional[Any]:
							a    =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*__lowerCamelCase							)





				@slow
				def 							__UpperCAmelCase							(      self				:     Any							)					->     List[Any]:
							for model_name in TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										a    =			TFCvtModel.from_pretrained(__lowerCamelCase							)
										self.assertIsNotNone(__lowerCamelCase							)




def 			__magic_name__				( ):






			'''simple docstring'''







			a    =			Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"   )
			return image






@require_tf
@require_vision
class     snake_case__							(unittest.TestCase   ):
				"""simple docstring"""



				@cached_property
				def 							__UpperCAmelCase							(      self				:     List[str]							)					->     Optional[int]:
							return AutoImageProcessor.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]							)





				@slow
				def 							__UpperCAmelCase							(      self				:     str							)					->     List[Any]:
							a    =			TFCvtForImageClassification.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]							)

							a    =			self.default_image_processor
							a    =			prepare_img()
							a    =			image_processor(images=__lowerCamelCase					,      return_tensors="tf"							)

							# forward pass
							a    =			model(**__lowerCamelCase							)

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

							a    =			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_torch_available,
)


__A		:       List[Any]  				=		{
    '''configuration_mega''': ['''MEGA_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MegaConfig''', '''MegaOnnxConfig'''],
}

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						__A		:       List[str]  				=		[
						    '''MEGA_PRETRAINED_MODEL_ARCHIVE_LIST''',
						    '''MegaForCausalLM''',
						    '''MegaForMaskedLM''',
						    '''MegaForMultipleChoice''',
						    '''MegaForQuestionAnswering''',
						    '''MegaForSequenceClassification''',
						    '''MegaForTokenClassification''',
						    '''MegaModel''',
						    '''MegaPreTrainedModel''',
						]

if TYPE_CHECKING:
						from .configuration_mega import MEGA_PRETRAINED_CONFIG_ARCHIVE_MAP, MegaConfig, MegaOnnxConfig

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_mega import (
												    MEGA_PRETRAINED_MODEL_ARCHIVE_LIST,
												    MegaForCausalLM,
												    MegaForMaskedLM,
												    MegaForMultipleChoice,
												    MegaForQuestionAnswering,
												    MegaForSequenceClassification,
												    MegaForTokenClassification,
												    MegaModel,
												    MegaPreTrainedModel,
												)

else:
						import sys

						__A		:       Any  				=		_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)



