




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_				( _SCREAMING_SNAKE_CASE   ):


 '''simple docstring'''







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



 def  lowerCAmelCase_    (				self    :						Union[str, Any]		):
       SCREAMING_SNAKE_CASE_      		=     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

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

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



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




 def  lowerCAmelCase_    (				self    :						Any		):
       SCREAMING_SNAKE_CASE_      		=     self.prepare_config_and_inputs()
       SCREAMING_SNAKE_CASE_		,					SCREAMING_SNAKE_CASE_		,					SCREAMING_SNAKE_CASE_      		=     config_and_inputs
       SCREAMING_SNAKE_CASE_      		=     {'pixel_values': pixel_values}
       return config, inputs_dict





@require_tf
class 			lowerCamelCase_				( _SCREAMING_SNAKE_CASE			,  _SCREAMING_SNAKE_CASE			,  unittest.TestCase   ):


 '''simple docstring'''







 lowercase_							       =       (TFCvtModel, TFCvtForImageClassification) if is_tf_available() else ()
 lowercase_							       =       (
     {"feature-extraction": TFCvtModel, "image-classification": TFCvtForImageClassification}
     if is_tf_available()
     else {}
 )
 lowercase_							       =       False
 lowercase_							       =       False
 lowercase_							       =       False
 lowercase_							       =       False
 lowercase_							       =       False



 def  lowerCAmelCase_    (				self    :						Optional[Any]		):
       SCREAMING_SNAKE_CASE_      		=     TFCvtModelTester(self		)
       SCREAMING_SNAKE_CASE_      		=     TFCvtConfigTester(self							,							config_class=_lowerCAmelCase							,							has_text_modality=_lowerCAmelCase							,							hidden_size=37		)



 def  lowerCAmelCase_    (				self    :						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  lowerCAmelCase_    (				self    :						Optional[int]		):
       pass



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



 @unittest.skip(reason='Cvt does not support input and output embeddings'		)
 def  lowerCAmelCase_    (				self    :						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  lowerCAmelCase_    (				self    :						Tuple		):
       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  lowerCAmelCase_    (				self    :						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  lowerCAmelCase_    (				self    :						Union[str, Any]		):
       SCREAMING_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  lowerCAmelCase_    (				self    :						List[Any]		):
       SCREAMING_SNAKE_CASE_		,					SCREAMING_SNAKE_CASE_      		=     self.model_tester.prepare_config_and_inputs_for_common()

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

             SCREAMING_SNAKE_CASE_      		=     ['pixel_values']
             self.assertListEqual(arg_names[:1]							,							_lowerCAmelCase		)



 def  lowerCAmelCase_    (				self    :						str		):
       def check_hidden_states_output(_lowerCAmelCase    :						Dict							,							_lowerCAmelCase    :						str							,							_lowerCAmelCase    :						Optional[Any]		):
             SCREAMING_SNAKE_CASE_      		=     model_class(_lowerCAmelCase		)

             SCREAMING_SNAKE_CASE_      		=     model(**self._prepare_for_class(_lowerCAmelCase							,							_lowerCAmelCase		)		)
             SCREAMING_SNAKE_CASE_      		=     outputs.hidden_states

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

       SCREAMING_SNAKE_CASE_		,					SCREAMING_SNAKE_CASE_      		=     self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
             SCREAMING_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"]
             SCREAMING_SNAKE_CASE_      		=     True

             check_hidden_states_output(_lowerCAmelCase							,							_lowerCAmelCase							,							_lowerCAmelCase		)



 def  lowerCAmelCase_    (				self    :						List[str]		):
       SCREAMING_SNAKE_CASE_      		=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*_lowerCAmelCase		)



 def  lowerCAmelCase_    (				self    :						int		):
       SCREAMING_SNAKE_CASE_      		=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*_lowerCAmelCase		)




 @slow
 def  lowerCAmelCase_    (				self    :						List[Any]		):
       for model_name in TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             SCREAMING_SNAKE_CASE_      		=     TFCvtModel.from_pretrained(_lowerCAmelCase		)
             self.assertIsNotNone(_lowerCAmelCase		)


def    UpperCAmelCase_				( )   ->					str:
      SCREAMING_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  lowerCAmelCase_    (				self    :						int		):
       return AutoImageProcessor.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]		)




 @slow
 def  lowerCAmelCase_    (				self    :						List[str]		):
       SCREAMING_SNAKE_CASE_      		=     TFCvtForImageClassification.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]		)

       SCREAMING_SNAKE_CASE_      		=     self.default_image_processor
       SCREAMING_SNAKE_CASE_      		=     prepare_img()
       SCREAMING_SNAKE_CASE_      		=     image_processor(images=_lowerCAmelCase							,							return_tensors='tf'		)

       # forward pass
       SCREAMING_SNAKE_CASE_      		=     model(**_lowerCAmelCase		)

       # verify the logits
       SCREAMING_SNAKE_CASE_      		=     tf.TensorShape((1, 1_000)		)
       self.assertEqual(outputs.logits.shape							,							_lowerCAmelCase		)

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





import torch
from transformers import PreTrainedModel, XLMRobertaConfig, XLMRobertaModel





class 			lowerCamelCase_				( _SCREAMING_SNAKE_CASE   ):


 '''simple docstring'''







 lowercase_							       =       "M-CLIP"



 def __init__(				self    :						Tuple							,							_lowerCAmelCase    :						List[str]=1_024							,							_lowerCAmelCase    :						str=768							,							**_lowerCAmelCase    :						Union[str, Any]		):
       SCREAMING_SNAKE_CASE_      		=     transformerDimSize
       SCREAMING_SNAKE_CASE_      		=     imageDimSize
       super().__init__(**_lowerCAmelCase		)





class 			lowerCamelCase_				( _SCREAMING_SNAKE_CASE   ):


 '''simple docstring'''







 lowercase_							       =       MCLIPConfig



 def __init__(				self    :						Dict							,							_lowerCAmelCase    :						Union[str, Any]							,							*_lowerCAmelCase    :						str							,							**_lowerCAmelCase    :						str		):
       super().__init__(_lowerCAmelCase							,							*_lowerCAmelCase							,							**_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     XLMRobertaModel(_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     torch.nn.Linear(
           in_features=config.transformerDimensions							,							out_features=config.numDims		)




 def  lowerCAmelCase_    (				self    :						List[Any]							,							_lowerCAmelCase    :						Any							,							_lowerCAmelCase    :						Union[str, Any]		):
       SCREAMING_SNAKE_CASE_      		=     self.transformer(input_ids=_lowerCAmelCase							,							attention_mask=_lowerCAmelCase		)[0]
       SCREAMING_SNAKE_CASE_      		=     (embs * attention_mask.unsqueeze(2		)).sum(dim=1		) / attention_mask.sum(dim=1		)[:, None]
       return self.LinearTransformation(_lowerCAmelCase		), embs