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



				"""simple docstring"""

				def 						snake_case	(			self      ):






										"""simple docstring"""

										snake_case        =     self.config_class(**self.inputs_dict      )
										self.parent.assertTrue(hasattr(lowerCAmelCase						,      'embed_dim'      )      )
										self.parent.assertTrue(hasattr(lowerCAmelCase						,      'num_heads'      )      )





class 					lowerCAmelCase_      :



				"""simple docstring"""

				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						,      ):






										"""simple docstring"""

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

				def 						snake_case	(			self      ):






										"""simple docstring"""

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

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

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

				def 						snake_case	(			self      ):






										"""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 						snake_case	(			self						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase      ):






										"""simple docstring"""

										snake_case        =     TFCvtModel(config=lowerCAmelCase      )
										snake_case        =     model(lowerCAmelCase						,      training=lowerCAmelCase      )
										snake_case        =     (self.image_size, self.image_size)
										snake_case   ,snake_case        =     image_size[0], image_size[1]
										for i in range(len(self.depth      )      ):
																snake_case        =     floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1      )
																snake_case        =     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 						snake_case	(			self						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase      ):






										"""simple docstring"""

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

				def 						snake_case	(			self      ):






										"""simple docstring"""

										snake_case        =     self.prepare_config_and_inputs()
										snake_case   ,snake_case   ,snake_case        =     config_and_inputs
										snake_case        =     {'pixel_values': pixel_values}
										return config, inputs_dict





@require_tf
class 					lowerCAmelCase_      (     lowerCAmelCase       ,      lowerCAmelCase       ,      unittest.TestCase   ):



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

				def 						snake_case	(			self      ):






										"""simple docstring"""

										snake_case        =     TFCvtModelTester(self      )
										snake_case        =     TFCvtConfigTester(self						,      config_class=lowerCAmelCase						,      has_text_modality=lowerCAmelCase						,      hidden_size=37      )

				def 						snake_case	(			self      ):






										"""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 						snake_case	(			self      ):






										"""simple docstring"""

										pass

				@unittest.skip(reason='Cvt does not use inputs_embeds'      )
				def 						snake_case	(			self      ):






										"""simple docstring"""

										pass

				@unittest.skip(reason='Cvt does not support input and output embeddings'      )
				def 						snake_case	(			self      ):






										"""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 						snake_case	(			self      ):






										"""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 						snake_case	(			self      ):






										"""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 						snake_case	(			self      ):






										"""simple docstring"""

										snake_case        =     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 						snake_case	(			self      ):






										"""simple docstring"""

										snake_case   ,snake_case        =     self.model_tester.prepare_config_and_inputs_for_common()

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

																snake_case        =     ['pixel_values']
																self.assertListEqual(arg_names[:1]						,      lowerCAmelCase      )

				def 						snake_case	(			self      ):






										"""simple docstring"""

										def check_hidden_states_output(lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase      ):
																snake_case        =     model_class(lowerCAmelCase      )

																snake_case        =     model(**self._prepare_for_class(lowerCAmelCase						,      lowerCAmelCase      )      )
																snake_case        =     outputs.hidden_states

																snake_case        =     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,
																    ]						,      )

										snake_case   ,snake_case        =     self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
																snake_case        =     True
																check_hidden_states_output(lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase      )

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

																check_hidden_states_output(lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase      )

				def 						snake_case	(			self      ):






										"""simple docstring"""

										snake_case        =     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*lowerCAmelCase      )

				def 						snake_case	(			self      ):






										"""simple docstring"""

										snake_case        =     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase      )

				@slow
				def 						snake_case	(			self      ):






										"""simple docstring"""

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





def 				lowerCAmelCase__      (     )   ->			List[Any]:





						"""simple docstring"""






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



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



				"""simple docstring"""

				@cached_property
				def 						snake_case	(			self      ):






										"""simple docstring"""

										return AutoImageProcessor.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]      )

				@slow
				def 						snake_case	(			self      ):






										"""simple docstring"""

										snake_case        =     TFCvtForImageClassification.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]      )

										snake_case        =     self.default_image_processor
										snake_case        =     prepare_img()
										snake_case        =     image_processor(images=lowerCAmelCase						,      return_tensors='tf'      )

										# forward pass
										snake_case        =     model(**lowerCAmelCase      )

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

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



"""simple docstring"""
from ..utils import DummyObject, requires_backends



class 					lowerCAmelCase_      (     metaclass=lowerCAmelCase   ):



				"""simple docstring"""
				_lowerCAmelCase       :      Dict     					=  ["""sentencepiece"""]

				def __init__(			self						,      *lowerCAmelCase						,      **lowerCAmelCase      ):






										"""simple docstring"""

										requires_backends(self						,      ['sentencepiece']      )





class 					lowerCAmelCase_      (     metaclass=lowerCAmelCase   ):



				"""simple docstring"""
				_lowerCAmelCase       :      Optional[Any]     					=  ["""sentencepiece"""]

				def __init__(			self						,      *lowerCAmelCase						,      **lowerCAmelCase      ):






										"""simple docstring"""

										requires_backends(self						,      ['sentencepiece']      )





class 					lowerCAmelCase_      (     metaclass=lowerCAmelCase   ):



				"""simple docstring"""
				_lowerCAmelCase       :      Optional[Any]     					=  ["""sentencepiece"""]

				def __init__(			self						,      *lowerCAmelCase						,      **lowerCAmelCase      ):






										"""simple docstring"""

										requires_backends(self						,      ['sentencepiece']      )





class 					lowerCAmelCase_      (     metaclass=lowerCAmelCase   ):



				"""simple docstring"""
				_lowerCAmelCase       :      Union[str, Any]     					=  ["""sentencepiece"""]

				def __init__(			self						,      *lowerCAmelCase						,      **lowerCAmelCase      ):






										"""simple docstring"""

										requires_backends(self						,      ['sentencepiece']      )





class 					lowerCAmelCase_      (     metaclass=lowerCAmelCase   ):



				"""simple docstring"""
				_lowerCAmelCase       :      Optional[int]     					=  ["""sentencepiece"""]

				def __init__(			self						,      *lowerCAmelCase						,      **lowerCAmelCase      ):






										"""simple docstring"""

										requires_backends(self						,      ['sentencepiece']      )





class 					lowerCAmelCase_      (     metaclass=lowerCAmelCase   ):



				"""simple docstring"""
				_lowerCAmelCase       :      Dict     					=  ["""sentencepiece"""]

				def __init__(			self						,      *lowerCAmelCase						,      **lowerCAmelCase      ):






										"""simple docstring"""

										requires_backends(self						,      ['sentencepiece']      )





class 					lowerCAmelCase_      (     metaclass=lowerCAmelCase   ):



				"""simple docstring"""
				_lowerCAmelCase       :      Union[str, Any]     					=  ["""sentencepiece"""]

				def __init__(			self						,      *lowerCAmelCase						,      **lowerCAmelCase      ):






										"""simple docstring"""

										requires_backends(self						,      ['sentencepiece']      )





class 					lowerCAmelCase_      (     metaclass=lowerCAmelCase   ):



				"""simple docstring"""
				_lowerCAmelCase       :      Union[str, Any]     					=  ["""sentencepiece"""]

				def __init__(			self						,      *lowerCAmelCase						,      **lowerCAmelCase      ):






										"""simple docstring"""

										requires_backends(self						,      ['sentencepiece']      )





class 					lowerCAmelCase_      (     metaclass=lowerCAmelCase   ):



				"""simple docstring"""
				_lowerCAmelCase       :      Optional[int]     					=  ["""sentencepiece"""]

				def __init__(			self						,      *lowerCAmelCase						,      **lowerCAmelCase      ):






										"""simple docstring"""

										requires_backends(self						,      ['sentencepiece']      )





class 					lowerCAmelCase_      (     metaclass=lowerCAmelCase   ):



				"""simple docstring"""
				_lowerCAmelCase       :      Any     					=  ["""sentencepiece"""]

				def __init__(			self						,      *lowerCAmelCase						,      **lowerCAmelCase      ):






										"""simple docstring"""

										requires_backends(self						,      ['sentencepiece']      )





class 					lowerCAmelCase_      (     metaclass=lowerCAmelCase   ):



				"""simple docstring"""
				_lowerCAmelCase       :      Union[str, Any]     					=  ["""sentencepiece"""]

				def __init__(			self						,      *lowerCAmelCase						,      **lowerCAmelCase      ):






										"""simple docstring"""

										requires_backends(self						,      ['sentencepiece']      )





class 					lowerCAmelCase_      (     metaclass=lowerCAmelCase   ):



				"""simple docstring"""
				_lowerCAmelCase       :      List[Any]     					=  ["""sentencepiece"""]

				def __init__(			self						,      *lowerCAmelCase						,      **lowerCAmelCase      ):






										"""simple docstring"""

										requires_backends(self						,      ['sentencepiece']      )





class 					lowerCAmelCase_      (     metaclass=lowerCAmelCase   ):



				"""simple docstring"""
				_lowerCAmelCase       :      str     					=  ["""sentencepiece"""]

				def __init__(			self						,      *lowerCAmelCase						,      **lowerCAmelCase      ):






										"""simple docstring"""

										requires_backends(self						,      ['sentencepiece']      )





class 					lowerCAmelCase_      (     metaclass=lowerCAmelCase   ):



				"""simple docstring"""
				_lowerCAmelCase       :      Tuple     					=  ["""sentencepiece"""]

				def __init__(			self						,      *lowerCAmelCase						,      **lowerCAmelCase      ):






										"""simple docstring"""

										requires_backends(self						,      ['sentencepiece']      )





class 					lowerCAmelCase_      (     metaclass=lowerCAmelCase   ):



				"""simple docstring"""
				_lowerCAmelCase       :      Union[str, Any]     					=  ["""sentencepiece"""]

				def __init__(			self						,      *lowerCAmelCase						,      **lowerCAmelCase      ):






										"""simple docstring"""

										requires_backends(self						,      ['sentencepiece']      )





class 					lowerCAmelCase_      (     metaclass=lowerCAmelCase   ):



				"""simple docstring"""
				_lowerCAmelCase       :      Union[str, Any]     					=  ["""sentencepiece"""]

				def __init__(			self						,      *lowerCAmelCase						,      **lowerCAmelCase      ):






										"""simple docstring"""

										requires_backends(self						,      ['sentencepiece']      )





class 					lowerCAmelCase_      (     metaclass=lowerCAmelCase   ):



				"""simple docstring"""
				_lowerCAmelCase       :      str     					=  ["""sentencepiece"""]

				def __init__(			self						,      *lowerCAmelCase						,      **lowerCAmelCase      ):






										"""simple docstring"""

										requires_backends(self						,      ['sentencepiece']      )





class 					lowerCAmelCase_      (     metaclass=lowerCAmelCase   ):



				"""simple docstring"""
				_lowerCAmelCase       :      Tuple     					=  ["""sentencepiece"""]

				def __init__(			self						,      *lowerCAmelCase						,      **lowerCAmelCase      ):






										"""simple docstring"""

										requires_backends(self						,      ['sentencepiece']      )





class 					lowerCAmelCase_      (     metaclass=lowerCAmelCase   ):



				"""simple docstring"""
				_lowerCAmelCase       :      Any     					=  ["""sentencepiece"""]

				def __init__(			self						,      *lowerCAmelCase						,      **lowerCAmelCase      ):






										"""simple docstring"""

										requires_backends(self						,      ['sentencepiece']      )





class 					lowerCAmelCase_      (     metaclass=lowerCAmelCase   ):



				"""simple docstring"""
				_lowerCAmelCase       :      Tuple     					=  ["""sentencepiece"""]

				def __init__(			self						,      *lowerCAmelCase						,      **lowerCAmelCase      ):






										"""simple docstring"""

										requires_backends(self						,      ['sentencepiece']      )





class 					lowerCAmelCase_      (     metaclass=lowerCAmelCase   ):



				"""simple docstring"""
				_lowerCAmelCase       :      Tuple     					=  ["""sentencepiece"""]

				def __init__(			self						,      *lowerCAmelCase						,      **lowerCAmelCase      ):






										"""simple docstring"""

										requires_backends(self						,      ['sentencepiece']      )





class 					lowerCAmelCase_      (     metaclass=lowerCAmelCase   ):



				"""simple docstring"""
				_lowerCAmelCase       :      int     					=  ["""sentencepiece"""]

				def __init__(			self						,      *lowerCAmelCase						,      **lowerCAmelCase      ):






										"""simple docstring"""

										requires_backends(self						,      ['sentencepiece']      )





class 					lowerCAmelCase_      (     metaclass=lowerCAmelCase   ):



				"""simple docstring"""
				_lowerCAmelCase       :      Any     					=  ["""sentencepiece"""]

				def __init__(			self						,      *lowerCAmelCase						,      **lowerCAmelCase      ):






										"""simple docstring"""

										requires_backends(self						,      ['sentencepiece']      )





class 					lowerCAmelCase_      (     metaclass=lowerCAmelCase   ):



				"""simple docstring"""
				_lowerCAmelCase       :      Optional[int]     					=  ["""sentencepiece"""]

				def __init__(			self						,      *lowerCAmelCase						,      **lowerCAmelCase      ):






										"""simple docstring"""

										requires_backends(self						,      ['sentencepiece']      )





class 					lowerCAmelCase_      (     metaclass=lowerCAmelCase   ):



				"""simple docstring"""
				_lowerCAmelCase       :      int     					=  ["""sentencepiece"""]

				def __init__(			self						,      *lowerCAmelCase						,      **lowerCAmelCase      ):






										"""simple docstring"""

										requires_backends(self						,      ['sentencepiece']      )





class 					lowerCAmelCase_      (     metaclass=lowerCAmelCase   ):



				"""simple docstring"""
				_lowerCAmelCase       :      Dict     					=  ["""sentencepiece"""]

				def __init__(			self						,      *lowerCAmelCase						,      **lowerCAmelCase      ):






										"""simple docstring"""

										requires_backends(self						,      ['sentencepiece']      )





class 					lowerCAmelCase_      (     metaclass=lowerCAmelCase   ):



				"""simple docstring"""
				_lowerCAmelCase       :      Any     					=  ["""sentencepiece"""]

				def __init__(			self						,      *lowerCAmelCase						,      **lowerCAmelCase      ):






										"""simple docstring"""

										requires_backends(self						,      ['sentencepiece']      )





class 					lowerCAmelCase_      (     metaclass=lowerCAmelCase   ):



				"""simple docstring"""
				_lowerCAmelCase       :      Union[str, Any]     					=  ["""sentencepiece"""]

				def __init__(			self						,      *lowerCAmelCase						,      **lowerCAmelCase      ):






										"""simple docstring"""

										requires_backends(self						,      ['sentencepiece']      )





class 					lowerCAmelCase_      (     metaclass=lowerCAmelCase   ):



				"""simple docstring"""
				_lowerCAmelCase       :      str     					=  ["""sentencepiece"""]

				def __init__(			self						,      *lowerCAmelCase						,      **lowerCAmelCase      ):






										"""simple docstring"""

										requires_backends(self						,      ['sentencepiece']      )





class 					lowerCAmelCase_      (     metaclass=lowerCAmelCase   ):



				"""simple docstring"""
				_lowerCAmelCase       :      Dict     					=  ["""sentencepiece"""]

				def __init__(			self						,      *lowerCAmelCase						,      **lowerCAmelCase      ):






										"""simple docstring"""

										requires_backends(self						,      ['sentencepiece']      )





class 					lowerCAmelCase_      (     metaclass=lowerCAmelCase   ):



				"""simple docstring"""
				_lowerCAmelCase       :      Tuple     					=  ["""sentencepiece"""]

				def __init__(			self						,      *lowerCAmelCase						,      **lowerCAmelCase      ):






										"""simple docstring"""

										requires_backends(self						,      ['sentencepiece']      )


