

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



	def     a   (   self			:    List[Any]		):
		__UpperCAmelCase							=      self.config_class(**self.inputs_dict		)
		self.parent.assertTrue(hasattr(SCREAMING_SNAKE_CASE__       ,  '''embed_dim'''		)		)
		self.parent.assertTrue(hasattr(SCREAMING_SNAKE_CASE__       ,  '''num_heads'''		)		)






class 			_UpperCAmelCase					:



	def __init__(   self			:    Tuple       ,  _lowercase			:    Tuple       ,  _lowercase			:    Tuple=13       ,  _lowercase			:    Tuple=64       ,  _lowercase			:    Dict=3       ,  _lowercase			:    Union[str, Any]=[16, 48, 96]       ,  _lowercase			:    str=[1, 3, 6]       ,  _lowercase			:    Tuple=[1, 2, 10]       ,  _lowercase			:    Optional[int]=[7, 3, 3]       ,  _lowercase			:    Optional[int]=[4, 2, 2]       ,  _lowercase			:    Optional[int]=[2, 1, 1]       ,  _lowercase			:    Dict=[2, 2, 2]       ,  _lowercase			:    str=[False, False, True]       ,  _lowercase			:    Tuple=[0.0, 0.0, 0.0]       ,  _lowercase			:    Dict=0.02       ,  _lowercase			:    Dict=1E-12       ,  _lowercase			:    List[str]=True       ,  _lowercase			:    Tuple=True       ,  _lowercase			:    Optional[int]=2       ,  ):
		__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     a   (   self			:    List[str]		):
		__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     a   (   self			:    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     a   (   self			:    List[Any]       ,  _lowercase			:    List[Any]       ,  _lowercase			:    Dict       ,  _lowercase			:    Optional[int]		):
		__UpperCAmelCase							=      TFCvtModel(config=SCREAMING_SNAKE_CASE__		)
		__UpperCAmelCase							=      model(SCREAMING_SNAKE_CASE__       ,  training=SCREAMING_SNAKE_CASE__		)
		__UpperCAmelCase							=      (self.image_size, self.image_size)
		__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     a   (   self			:    Dict       ,  _lowercase			:    Any       ,  _lowercase			:    Tuple       ,  _lowercase			:    Any		):
		__UpperCAmelCase							=      self.num_labels
		__UpperCAmelCase							=      TFCvtForImageClassification(SCREAMING_SNAKE_CASE__		)
		__UpperCAmelCase							=      model(SCREAMING_SNAKE_CASE__       ,  labels=SCREAMING_SNAKE_CASE__       ,  training=SCREAMING_SNAKE_CASE__		)
		self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.num_labels)		)



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






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



	def     a   (   self			:    Optional[Any]		):
		__UpperCAmelCase							=      TFCvtModelTester(self		)
		__UpperCAmelCase							=      TFCvtConfigTester(self       ,  config_class=SCREAMING_SNAKE_CASE__       ,  has_text_modality=SCREAMING_SNAKE_CASE__       ,  hidden_size=37		)



	def     a   (   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     a   (   self			:    int		):
		pass



	@unittest.skip(reason='''Cvt does not use inputs_embeds'''		)
	def     a   (   self			:    List[Any]		):
		pass



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



	def     a   (   self			:    Any		):
		__UpperCAmelCase							=      self.model_tester.prepare_config_and_inputs_for_common()

		for model_class in self.all_model_classes:
			__UpperCAmelCase							=      model_class(SCREAMING_SNAKE_CASE__		)
			__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]       ,  SCREAMING_SNAKE_CASE__		)



	def     a   (   self			:    Union[str, Any]		):
		def check_hidden_states_output(_lowercase			:    List[Any]       ,  _lowercase			:    Optional[Any]       ,  _lowercase			:    Optional[Any]		):
			__UpperCAmelCase							=      model_class(SCREAMING_SNAKE_CASE__		)

			__UpperCAmelCase							=      model(**self._prepare_for_class(SCREAMING_SNAKE_CASE__       ,  SCREAMING_SNAKE_CASE__		)		)
			__UpperCAmelCase							=      outputs.hidden_states

			__UpperCAmelCase							=      len(self.model_tester.depth		)
			self.assertEqual(len(SCREAMING_SNAKE_CASE__		)       ,  SCREAMING_SNAKE_CASE__		)

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

		for model_class in self.all_model_classes:
			__UpperCAmelCase							=      True
			check_hidden_states_output(SCREAMING_SNAKE_CASE__       ,  SCREAMING_SNAKE_CASE__       ,  SCREAMING_SNAKE_CASE__		)

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

			check_hidden_states_output(SCREAMING_SNAKE_CASE__       ,  SCREAMING_SNAKE_CASE__       ,  SCREAMING_SNAKE_CASE__		)



	def     a   (   self			:    Union[str, Any]		):
		__UpperCAmelCase							=      self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE__		)



	def     a   (   self			:    List[str]		):
		__UpperCAmelCase							=      self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE__		)



	@slow
	def     a   (   self			:    Optional[int]		):
		for model_name in TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
			__UpperCAmelCase							=      TFCvtModel.from_pretrained(SCREAMING_SNAKE_CASE__		)
			self.assertIsNotNone(SCREAMING_SNAKE_CASE__		)






def 	lowercase__		(    ):
	__UpperCAmelCase							=      Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''  )
	return image




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



	@cached_property
	def     a   (   self			:    Tuple		):
		return AutoImageProcessor.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]		)



	@slow
	def     a   (   self			:    Optional[Any]		):
		__UpperCAmelCase							=      TFCvtForImageClassification.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]		)

		__UpperCAmelCase							=      self.default_image_processor
		__UpperCAmelCase							=      prepare_img()
		__UpperCAmelCase							=      image_processor(images=SCREAMING_SNAKE_CASE__       ,  return_tensors='''tf'''		)

		# forward pass
		__UpperCAmelCase							=      model(**SCREAMING_SNAKE_CASE__		)

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

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




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


UpperCAmelCase_      : Dict  					=       logging.get_logger(__name__)

UpperCAmelCase_      : Union[str, Any]  					=       {
    'google/switch-base-8': 'https://huggingface.co/google/switch-base-8/blob/main/config.json',
}
class  SCREAMING_SNAKE_CASE__  (				lowercase__			):
      snake_case__				:     List[str]        =						'''switch_transformers'''
      snake_case__				:     Optional[int]        =						['''past_key_values''']
      snake_case__				:     Optional[Any]        =						{'''hidden_size''': '''d_model''', '''num_attention_heads''': '''num_heads''', '''num_hidden_layers''': '''num_layers'''}
      def __init__(       self				:       Dict     ,							SCREAMING_SNAKE_CASE__				:       Optional[int]=3_2_1_2_8     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]=7_6_8     ,							SCREAMING_SNAKE_CASE__				:       Optional[Any]=6_4     ,							SCREAMING_SNAKE_CASE__				:       List[str]=2_0_4_8     ,							SCREAMING_SNAKE_CASE__				:       Dict=6_4     ,							SCREAMING_SNAKE_CASE__				:       List[Any]=1_2     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]=3     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]=1_2     ,							SCREAMING_SNAKE_CASE__				:       str=3     ,							SCREAMING_SNAKE_CASE__				:       Tuple=1_2     ,							SCREAMING_SNAKE_CASE__				:       Tuple=8     ,							SCREAMING_SNAKE_CASE__				:       Dict=False     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]=0.01     ,							SCREAMING_SNAKE_CASE__				:       str="float32"     ,							SCREAMING_SNAKE_CASE__				:       str=False     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]=3_2     ,							SCREAMING_SNAKE_CASE__				:       Dict=1_2_8     ,							SCREAMING_SNAKE_CASE__				:       Dict=0.1     ,							SCREAMING_SNAKE_CASE__				:       Dict=1E-6     ,							SCREAMING_SNAKE_CASE__				:       Dict=0.001     ,							SCREAMING_SNAKE_CASE__				:       Any=0.001     ,							SCREAMING_SNAKE_CASE__				:       Optional[int]=1.0     ,							SCREAMING_SNAKE_CASE__				:       Any="relu"     ,							SCREAMING_SNAKE_CASE__				:       List[Any]=True     ,							SCREAMING_SNAKE_CASE__				:       Optional[Any]=False     ,							SCREAMING_SNAKE_CASE__				:       Dict=True     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]=0     ,							SCREAMING_SNAKE_CASE__				:       Optional[int]=1     ,							**SCREAMING_SNAKE_CASE__				:       Dict     ,							)		->			Optional[Any]:
             a_					:		Optional[int]							=  vocab_size
             a_					:		List[str]							=  d_model
             a_					:		Tuple							=  d_kv
             a_					:		Optional[Any]							=  d_ff

             a_					:		List[Any]							=  num_sparse_encoder_layers

             a_					:		Any							=  num_layers
             a_					:		str							=  (
                 num_decoder_layers if num_decoder_layers is not None else self.num_layers
             )  # default = symmetry
             a_					:		List[Any]							=  num_sparse_decoder_layers

             # This tells us, each how many encoder layer we'll have to set a sparse layer.
             if self.num_sparse_encoder_layers > 0:
                    a_					:		Optional[int]							=  self.num_layers // self.num_sparse_encoder_layers
             else:
                    a_					:		List[Any]							=  self.num_layers  # HACK: this will create 0 sparse layers

             # This tells us, each how many encoder layer we'll have to set a sparse layer.
             if self.num_sparse_decoder_layers > 0:
                    a_					:		Union[str, Any]							=  self.num_decoder_layers // self.num_sparse_decoder_layers
             else:
                    a_					:		List[str]							=  self.num_decoder_layers  # HACK: this will create 0 sparse layers

             a_					:		Dict							=  num_heads
             a_					:		str							=  num_experts
             a_					:		Any							=  expert_capacity
             a_					:		List[Any]							=  router_bias
             a_					:		str							=  router_jitter_noise
             if router_dtype not in ["float32", "float16", "bfloat16"]:
                    raise ValueError(F"""`router_dtype` must be one of 'float32', 'float16' or 'bfloat16', got {router_dtype}"""     )
             a_					:		Optional[int]							=  router_dtype

             a_					:		int							=  router_ignore_padding_tokens
             a_					:		Any							=  relative_attention_num_buckets
             a_					:		List[str]							=  relative_attention_max_distance

             a_					:		Optional[Any]							=  dropout_rate
             a_					:		Tuple							=  layer_norm_epsilon
             a_					:		Dict							=  initializer_factor
             a_					:		Any							=  feed_forward_proj
             a_					:		Tuple							=  use_cache
             a_					:		str							=  add_router_probs

             a_					:		Optional[int]							=  router_z_loss_coef
             a_					:		List[str]							=  router_aux_loss_coef

             a_					:		int							=  self.feed_forward_proj.split('-'     )
             a_					:		int							=  act_info[-1]
             a_					:		Optional[int]							=  act_info[0] == 'gated'

             if len(SCREAMING_SNAKE_CASE__     ) > 1 and act_info[0] != "gated" or len(SCREAMING_SNAKE_CASE__     ) > 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\''     )

             # for backwards compatibility
             if feed_forward_proj == "gated-gelu":
                    a_					:		Any							=  'gelu_new'

             super().__init__(
                 pad_token_id=SCREAMING_SNAKE_CASE__     ,							eos_token_id=SCREAMING_SNAKE_CASE__     ,							is_encoder_decoder=SCREAMING_SNAKE_CASE__     ,							**SCREAMING_SNAKE_CASE__     ,							)


