


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 			SCREAMING_SNAKE_CASE__  (    lowercase__      ):
      def 	SCREAMING_SNAKE_CASE							(	self    :	int					)		->      Dict:
             a_ :		Optional[int]							=  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 			SCREAMING_SNAKE_CASE__  :
      def __init__(	self    :	List[str]					,							SCREAMING_SNAKE_CASE__    :	Tuple					,							SCREAMING_SNAKE_CASE__    :	Dict=1_3					,							SCREAMING_SNAKE_CASE__    :	Union[str, Any]=6_4					,							SCREAMING_SNAKE_CASE__    :	int=3					,							SCREAMING_SNAKE_CASE__    :	Optional[Any]=[1_6, 4_8, 9_6]					,							SCREAMING_SNAKE_CASE__    :	List[Any]=[1, 3, 6]					,							SCREAMING_SNAKE_CASE__    :	int=[1, 2, 1_0]					,							SCREAMING_SNAKE_CASE__    :	Any=[7, 3, 3]					,							SCREAMING_SNAKE_CASE__    :	Union[str, Any]=[4, 2, 2]					,							SCREAMING_SNAKE_CASE__    :	Dict=[2, 1, 1]					,							SCREAMING_SNAKE_CASE__    :	Dict=[2, 2, 2]					,							SCREAMING_SNAKE_CASE__    :	Tuple=[False, False, True]					,							SCREAMING_SNAKE_CASE__    :	Union[str, Any]=[0.0, 0.0, 0.0]					,							SCREAMING_SNAKE_CASE__    :	Dict=0.02					,							SCREAMING_SNAKE_CASE__    :	Any=1E-12					,							SCREAMING_SNAKE_CASE__    :	Union[str, Any]=True					,							SCREAMING_SNAKE_CASE__    :	Optional[int]=True					,							SCREAMING_SNAKE_CASE__    :	str=2					,							)		->      List[str]:
             a_ :		Dict							=  parent
             a_ :		Optional[int]							=  batch_size
             a_ :		Union[str, Any]							=  image_size
             a_ :		Optional[int]							=  patch_sizes
             a_ :		Union[str, Any]							=  patch_stride
             a_ :		Optional[int]							=  patch_padding
             a_ :		str							=  is_training
             a_ :		str							=  use_labels
             a_ :		Dict							=  num_labels
             a_ :		int							=  num_channels
             a_ :		Dict							=  embed_dim
             a_ :		List[str]							=  num_heads
             a_ :		int							=  stride_kv
             a_ :		str							=  depth
             a_ :		Any							=  cls_token
             a_ :		Optional[int]							=  attention_drop_rate
             a_ :		Union[str, Any]							=  initializer_range
             a_ :		Union[str, Any]							=  layer_norm_eps
      def 	SCREAMING_SNAKE_CASE							(	self    :	Union[str, Any]					)		->      int:
             a_ :		Optional[int]							=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

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

             a_ :		Optional[Any]							=  self.get_config()
             return config, pixel_values, labels
      def 	SCREAMING_SNAKE_CASE							(	self    :	Dict					)		->      str:
             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    :	int					,							SCREAMING_SNAKE_CASE__    :	List[Any]					,							SCREAMING_SNAKE_CASE__    :	Union[str, Any]					,							SCREAMING_SNAKE_CASE__    :	Tuple					)		->      List[Any]:
             a_ :		Dict							=  TFCvtModel(config=SCREAMING_SNAKE_CASE__					)
             a_ :		int							=  model(SCREAMING_SNAKE_CASE__					,							training=SCREAMING_SNAKE_CASE__					)
             a_ :		str							=  (self.image_size, self.image_size)
             a_							,		a_ :		Optional[Any]							=  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_ :		Optional[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 	SCREAMING_SNAKE_CASE							(	self    :	int					,							SCREAMING_SNAKE_CASE__    :	Dict					,							SCREAMING_SNAKE_CASE__    :	Tuple					,							SCREAMING_SNAKE_CASE__    :	List[Any]					)		->      Optional[int]:
             a_ :		Union[str, Any]							=  self.num_labels
             a_ :		str							=  TFCvtForImageClassification(SCREAMING_SNAKE_CASE__					)
             a_ :		Union[str, Any]							=  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 	SCREAMING_SNAKE_CASE							(	self    :	int					)		->      int:
             a_ :		Optional[int]							=  self.prepare_config_and_inputs()
             a_							,		a_							,		a_ :		str							=  config_and_inputs
             a_ :		Optional[int]							=  {'pixel_values': pixel_values}
             return config, inputs_dict
@require_tf
class 			SCREAMING_SNAKE_CASE__  (    lowercase__						,   lowercase__						,   unittest.TestCase      ):
      snake_case__    :					str					       =						(TFCvtModel, TFCvtForImageClassification) if is_tf_available() else ()
      snake_case__    :					Optional[int]					       =						(
          {'''feature-extraction''': TFCvtModel, '''image-classification''': TFCvtForImageClassification}
          if is_tf_available()
          else {}
      )
      snake_case__    :					str					       =						False
      snake_case__    :					Union[str, Any]					       =						False
      snake_case__    :					Tuple					       =						False
      snake_case__    :					Dict					       =						False
      snake_case__    :					Union[str, Any]					       =						False
      def 	SCREAMING_SNAKE_CASE							(	self    :	str					)		->      Union[str, Any]:
             a_ :		Optional[int]							=  TFCvtModelTester(self					)
             a_ :		int							=  TFCvtConfigTester(self					,							config_class=SCREAMING_SNAKE_CASE__					,							has_text_modality=SCREAMING_SNAKE_CASE__					,							hidden_size=3_7					)
      def 	SCREAMING_SNAKE_CASE							(	self    :	int					)		->      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[int]					)		->      Optional[Any]:
             pass
      @unittest.skip(reason='Cvt does not use inputs_embeds'					)
      def 	SCREAMING_SNAKE_CASE							(	self    :	Tuple					)		->      List[str]:
             pass
      @unittest.skip(reason='Cvt does not support input and output embeddings'					)
      def 	SCREAMING_SNAKE_CASE							(	self    :	str					)		->      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 	SCREAMING_SNAKE_CASE							(	self    :	Dict					)		->      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 	SCREAMING_SNAKE_CASE							(	self    :	Tuple					)		->      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    :	List[Any]					)		->      Optional[int]:
             a_ :		List[Any]							=  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 	SCREAMING_SNAKE_CASE							(	self    :	List[str]					)		->      Optional[int]:
             a_							,		a_ :		Optional[Any]							=  self.model_tester.prepare_config_and_inputs_for_common()

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

                    a_ :		Tuple							=  ['pixel_values']
                    self.assertListEqual(arg_names[:1]					,							SCREAMING_SNAKE_CASE__					)
      def 	SCREAMING_SNAKE_CASE							(	self    :	Optional[Any]					)		->      Tuple:
             def check_hidden_states_output(SCREAMING_SNAKE_CASE__    :	str					,							SCREAMING_SNAKE_CASE__    :	Tuple					,							SCREAMING_SNAKE_CASE__    :	Any					):
                    a_ :		Dict							=  model_class(SCREAMING_SNAKE_CASE__					)

                    a_ :		Tuple							=  model(**self._prepare_for_class(SCREAMING_SNAKE_CASE__					,							SCREAMING_SNAKE_CASE__					)					)
                    a_ :		List[str]							=  outputs.hidden_states

                    a_ :		List[Any]							=  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,
                        ]					,							)

             a_							,		a_ :		Union[str, Any]							=  self.model_tester.prepare_config_and_inputs_for_common()

             for model_class in self.all_model_classes:
                    a_ :		Any							=  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"]
                    a_ :		Optional[int]							=  True

                    check_hidden_states_output(SCREAMING_SNAKE_CASE__					,							SCREAMING_SNAKE_CASE__					,							SCREAMING_SNAKE_CASE__					)
      def 	SCREAMING_SNAKE_CASE							(	self    :	str					)		->      int:
             a_ :		str							=  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE__					)
      def 	SCREAMING_SNAKE_CASE							(	self    :	Optional[int]					)		->      int:
             a_ :		Optional[int]							=  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE__					)

      @slow
      def 	SCREAMING_SNAKE_CASE							(	self    :	Dict					)		->      str:
             for model_name in TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                    a_ :		Tuple							=  TFCvtModel.from_pretrained(SCREAMING_SNAKE_CASE__					)
                    self.assertIsNotNone(SCREAMING_SNAKE_CASE__					)







def 	SCREAMING_SNAKE_CASE_						(      )     ->		Union[str, Any]:

       """simple docstring"""

       a_ :		List[Any]							=  Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'    )
       return image
@require_tf
@require_vision
class 			SCREAMING_SNAKE_CASE__  (    unittest.TestCase      ):
      @cached_property
      def 	SCREAMING_SNAKE_CASE							(	self    :	Dict					)		->      List[Any]:
             return AutoImageProcessor.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]					)

      @slow
      def 	SCREAMING_SNAKE_CASE							(	self    :	int					)		->      Tuple:
             a_ :		List[Any]							=  TFCvtForImageClassification.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]					)

             a_ :		Dict							=  self.default_image_processor
             a_ :		Optional[Any]							=  prepare_img()
             a_ :		Any							=  image_processor(images=SCREAMING_SNAKE_CASE__					,							return_tensors='tf'					)

             # forward pass
             a_ :		Any							=  model(**SCREAMING_SNAKE_CASE__					)

             # verify the logits
             a_ :		Tuple							=  tf.TensorShape((1, 1_0_0_0)					)
             self.assertEqual(outputs.logits.shape					,							SCREAMING_SNAKE_CASE__					)

             a_ :		Tuple							=  tf.constant([0.9285, 0.9015, -0.3150]					)
             self.assertTrue(np.allclose(outputs.logits[0, :3].numpy()					,							SCREAMING_SNAKE_CASE__					,							atol=1E-4					)					)






from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


UpperCAmelCase_				:			Dict   =	{'configuration_vit_msn': ['VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP', 'ViTMSNConfig']}

try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   UpperCAmelCase_				:			int   =	[
       'VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST',
       'ViTMSNModel',
       'ViTMSNForImageClassification',
       'ViTMSNPreTrainedModel',
   ]

if TYPE_CHECKING:
   from .configuration_vit_msn import VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP, ViTMSNConfig

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_vit_msn import (
          VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST,
          ViTMSNForImageClassification,
          ViTMSNModel,
          ViTMSNPreTrainedModel,
      )

else:
   import sys

   UpperCAmelCase_				:			Dict   =	_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)


