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







   def 		lowerCamelCase_     (			self		:		Optional[Any]							):





    """simple docstring"""


    UpperCamelCase          =	self.config_class(**self.inputs_dict							)
    self.parent.assertTrue(hasattr(_lowerCAmelCase					,					"""embed_dim"""							)							)
    self.parent.assertTrue(hasattr(_lowerCAmelCase					,					"""num_heads"""							)							)





class SCREAMING_SNAKE_CASE_						:







   def __init__(			self		:		Optional[Any]					,					lowerCamelCase_		:		int					,					lowerCamelCase_		:		Optional[int]=13					,					lowerCamelCase_		:		Tuple=64					,					lowerCamelCase_		:		Optional[int]=3					,					lowerCamelCase_		:		Any=[16, 48, 96]					,					lowerCamelCase_		:		Optional[Any]=[1, 3, 6]					,					lowerCamelCase_		:		Optional[int]=[1, 2, 10]					,					lowerCamelCase_		:		List[Any]=[7, 3, 3]					,					lowerCamelCase_		:		Union[str, Any]=[4, 2, 2]					,					lowerCamelCase_		:		int=[2, 1, 1]					,					lowerCamelCase_		:		Union[str, Any]=[2, 2, 2]					,					lowerCamelCase_		:		Tuple=[False, False, True]					,					lowerCamelCase_		:		List[Any]=[0.0, 0.0, 0.0]					,					lowerCamelCase_		:		Tuple=0.0_2					,					lowerCamelCase_		:		Optional[Any]=1E-12					,					lowerCamelCase_		:		Union[str, Any]=True					,					lowerCamelCase_		:		List[str]=True					,					lowerCamelCase_		:		Optional[Any]=2					,					):





    """simple docstring"""


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





    """simple docstring"""


    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 		lowerCamelCase_     (			self		:		str							):





    """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 		lowerCamelCase_     (			self		:		int					,					lowerCamelCase_		:		Dict					,					lowerCamelCase_		:		List[str]					,					lowerCamelCase_		:		Any							):





    """simple docstring"""


    UpperCamelCase          =	TFCvtModel(config=_lowerCAmelCase							)
    UpperCamelCase          =	model(_lowerCAmelCase					,					training=_lowerCAmelCase							)
    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 		lowerCamelCase_     (			self		:		Dict					,					lowerCamelCase_		:		int					,					lowerCamelCase_		:		List[Any]					,					lowerCamelCase_		:		Dict							):





    """simple docstring"""


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







   def 		lowerCamelCase_     (			self		:		List[Any]							):





    """simple docstring"""


    UpperCamelCase          =	self.prepare_config_and_inputs()
    UpperCamelCase          =	config_and_inputs
    UpperCamelCase          =	{"pixel_values": pixel_values}
    return config, inputs_dict





@require_tf
class SCREAMING_SNAKE_CASE_						(	__lowerCAmelCase   ,			__lowerCAmelCase   ,			unittest.TestCase						):
   __lowerCAmelCase      =							(TFCvtModel, TFCvtForImageClassification) if is_tf_available() else ()
   __lowerCAmelCase      =							(
       {"""feature-extraction""": TFCvtModel, """image-classification""": TFCvtForImageClassification}
       if is_tf_available()
       else {}
   )
   __lowerCAmelCase      =							False
   __lowerCAmelCase      =							False
   __lowerCAmelCase      =							False
   __lowerCAmelCase      =							False
   __lowerCAmelCase      =							False







   def 		lowerCamelCase_     (			self		:		Any							):





    """simple docstring"""


    UpperCamelCase          =	TFCvtModelTester(self							)
    UpperCamelCase          =	TFCvtConfigTester(self					,					config_class=_lowerCAmelCase					,					has_text_modality=_lowerCAmelCase					,					hidden_size=37							)







   def 		lowerCamelCase_     (			self		:		Optional[int]							):





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





    """simple docstring"""


    pass







   @unittest.skip(reason="""Cvt does not use inputs_embeds"""							)
   def 		lowerCamelCase_     (			self		:		Optional[int]							):





    """simple docstring"""


    pass







   @unittest.skip(reason="""Cvt does not support input and output embeddings"""							)
   def 		lowerCamelCase_     (			self		:		str							):





    """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 		lowerCamelCase_     (			self		:		Optional[int]							):





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





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





    """simple docstring"""


    UpperCamelCase          =	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		:		Union[str, Any]							):





    """simple docstring"""


    UpperCamelCase          =	self.model_tester.prepare_config_and_inputs_for_common()

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







   def 		lowerCamelCase_     (			self		:		List[Any]							):





    """simple docstring"""


    def check_hidden_states_output(lowerCamelCase_		:		Tuple					,					lowerCamelCase_		:		Union[str, Any]					,					lowerCamelCase_		:		Optional[Any]							):
     UpperCamelCase          =	model_class(_lowerCAmelCase							)

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

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

    UpperCamelCase          =	self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
     UpperCamelCase          =	True
     check_hidden_states_output(_lowerCAmelCase					,					_lowerCAmelCase					,					_lowerCAmelCase							)

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

     check_hidden_states_output(_lowerCAmelCase					,					_lowerCAmelCase					,					_lowerCAmelCase							)







   def 		lowerCamelCase_     (			self		:		int							):





    """simple docstring"""


    UpperCamelCase          =	self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*_lowerCAmelCase							)







   def 		lowerCamelCase_     (			self		:		Tuple							):





    """simple docstring"""


    UpperCamelCase          =	self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_image_classification(*_lowerCAmelCase							)







   @slow
   def 		lowerCamelCase_     (			self		:		Optional[int]							):





    """simple docstring"""


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





def        lowercase(     )						->     str:







 '''simple docstring'''
 UpperCamelCase          =	Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""    )
 return image


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







   @cached_property
   def 		lowerCamelCase_     (			self		:		Any							):





    """simple docstring"""


    return AutoImageProcessor.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]							)







   @slow
   def 		lowerCamelCase_     (			self		:		Dict							):





    """simple docstring"""


    UpperCamelCase          =	TFCvtForImageClassification.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]							)

    UpperCamelCase          =	self.default_image_processor
    UpperCamelCase          =	prepare_img()
    UpperCamelCase          =	image_processor(images=_lowerCAmelCase					,					return_tensors="""tf"""							)

    # forward pass
    UpperCamelCase          =	model(**_lowerCAmelCase							)

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

    UpperCamelCase          =	tf.constant([0.9_2_8_5, 0.9_0_1_5, -0.3_1_5_0]							)
    self.assertTrue(np.allclose(outputs.logits[0, :3].numpy()					,					_lowerCAmelCase					,					atol=1E-4							)							)






from __future__ import annotations

from typing import Dict

from ...configuration_utils import PretrainedConfig


_UpperCAmelCase		:  Tuple	=   {
    "susnato/ernie-m-base_pytorch": "https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/config.json",
    "susnato/ernie-m-large_pytorch": "https://huggingface.co/susnato/ernie-m-large_pytorch/blob/main/config.json",
}




class       __lowerCAmelCase				(							lowerCAmelCase):
       _a           =  '''ernie_m'''
       _a           =  {"dropout": "classifier_dropout", "num_classes": "num_labels"}






       def __init__(      self:    List[Any]				,    _lowerCAmelCase:    int = 25_00_02				,    _lowerCAmelCase:    int = 7_68				,    _lowerCAmelCase:    int = 12				,    _lowerCAmelCase:    int = 12				,    _lowerCAmelCase:    int = 30_72				,    _lowerCAmelCase:    str = "gelu"				,    _lowerCAmelCase:    float = 0.1				,    _lowerCAmelCase:    float = 0.1				,    _lowerCAmelCase:    int = 5_14				,    _lowerCAmelCase:    float = 0.02				,    _lowerCAmelCase:    int = 1				,    _lowerCAmelCase:    float = 1e-0_5				,    _lowerCAmelCase:    Dict=None				,    _lowerCAmelCase:    Optional[int]=False				,    _lowerCAmelCase:    List[str]=0.0				,    **_lowerCAmelCase:    Tuple				,    ):
         super().__init__(pad_token_id=_lowerCAmelCase				,    **_lowerCAmelCase )
         lowercase			:Tuple							=  vocab_size
         lowercase			:List[str]							=  hidden_size
         lowercase			:Optional[int]							=  num_hidden_layers
         lowercase			:Optional[Any]							=  num_attention_heads
         lowercase			:Optional[Any]							=  intermediate_size
         lowercase			:Optional[Any]							=  hidden_act
         lowercase			:Any							=  hidden_dropout_prob
         lowercase			:int							=  attention_probs_dropout_prob
         lowercase			:Dict							=  max_position_embeddings
         lowercase			:Optional[Any]							=  initializer_range
         lowercase			:Any							=  layer_norm_eps
         lowercase			:Union[str, Any]							=  classifier_dropout
         lowercase			:int							=  is_decoder
         lowercase			:List[str]							=  act_dropout



