"""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     _UpperCAmelCase     (    self    )			->   List[str]:


        '''simple docstring'''


        lowercase_			:       Any									=			self.config_class(**self.inputs_dict    )
        self.parent.assertTrue(hasattr(__UpperCamelCase  ,'embed_dim'    )    )
        self.parent.assertTrue(hasattr(__UpperCamelCase  ,'num_heads'    )    )







class  UpperCamelCase	:
   def __init__(    self  ,__UpperCamelCase  ,__UpperCamelCase=13  ,__UpperCamelCase=64  ,__UpperCamelCase=3  ,__UpperCamelCase=[16, 48, 96]  ,__UpperCamelCase=[1, 3, 6]  ,__UpperCamelCase=[1, 2, 10]  ,__UpperCamelCase=[7, 3, 3]  ,__UpperCamelCase=[4, 2, 2]  ,__UpperCamelCase=[2, 1, 1]  ,__UpperCamelCase=[2, 2, 2]  ,__UpperCamelCase=[False, False, True]  ,__UpperCamelCase=[0.0, 0.0, 0.0]  ,__UpperCamelCase=0.02  ,__UpperCamelCase=1e-12  ,__UpperCamelCase=True  ,__UpperCamelCase=True  ,__UpperCamelCase=2  ,)			->   List[str]:


        '''simple docstring'''


        lowercase_			:       Optional[Any]									=			parent
        lowercase_			:       Union[str, Any]									=			batch_size
        lowercase_			:       Optional[Any]									=			image_size
        lowercase_			:       Dict									=			patch_sizes
        lowercase_			:       Dict									=			patch_stride
        lowercase_			:       List[Any]									=			patch_padding
        lowercase_			:       Union[str, Any]									=			is_training
        lowercase_			:       int									=			use_labels
        lowercase_			:       Optional[Any]									=			num_labels
        lowercase_			:       Union[str, Any]									=			num_channels
        lowercase_			:       Dict									=			embed_dim
        lowercase_			:       Tuple									=			num_heads
        lowercase_			:       Optional[int]									=			stride_kv
        lowercase_			:       List[Any]									=			depth
        lowercase_			:       Optional[Any]									=			cls_token
        lowercase_			:       Any									=			attention_drop_rate
        lowercase_			:       Union[str, Any]									=			initializer_range
        lowercase_			:       Dict									=			layer_norm_eps
   def     _UpperCAmelCase     (    self    )			->   Optional[Any]:


        '''simple docstring'''


        lowercase_			:       List[Any]									=			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

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

        lowercase_			:       List[Any]									=			self.get_config()
        return config, pixel_values, labels
   def     _UpperCAmelCase     (    self    )			->   Any:


        '''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     _UpperCAmelCase     (    self  ,__UpperCamelCase  ,__UpperCamelCase  ,__UpperCamelCase    )			->   List[Any]:


        '''simple docstring'''


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


        '''simple docstring'''


        lowercase_			:       Any									=			self.num_labels
        lowercase_			:       List[Any]									=			TFCvtForImageClassification(__UpperCamelCase    )
        lowercase_			:       List[str]									=			model(__UpperCamelCase  ,labels=__UpperCamelCase  ,training=__UpperCamelCase    )
        self.parent.assertEqual(result.logits.shape  ,(self.batch_size, self.num_labels)    )







   def     _UpperCAmelCase     (    self    )			->   Dict:


        '''simple docstring'''


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







@require_tf
class  UpperCamelCase	(    lowercase_		,   lowercase_		,   unittest.TestCase						):
   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     _UpperCAmelCase     (    self    )			->   Optional[int]:


        '''simple docstring'''


        lowercase_			:       Optional[int]									=			TFCvtModelTester(self    )
        lowercase_			:       Union[str, Any]									=			TFCvtConfigTester(self  ,config_class=__UpperCamelCase  ,has_text_modality=__UpperCamelCase  ,hidden_size=37    )
   def     _UpperCAmelCase     (    self    )			->   Tuple:


        '''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     _UpperCAmelCase     (    self    )			->   int:


        '''simple docstring'''


        pass
   @unittest.skip(reason='Cvt does not use inputs_embeds'    )
   def     _UpperCAmelCase     (    self    )			->   int:


        '''simple docstring'''


        pass
   @unittest.skip(reason='Cvt does not support input and output embeddings'    )
   def     _UpperCAmelCase     (    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     _UpperCAmelCase     (    self    )			->   Union[str, Any]:


        '''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     _UpperCAmelCase     (    self    )			->   Union[str, Any]:


        '''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     _UpperCAmelCase     (    self    )			->   Optional[Any]:


        '''simple docstring'''


        lowercase_			:       int									=			tf.keras.mixed_precision.Policy('mixed_float16'    )
        tf.keras.mixed_precision.set_global_policy(__UpperCamelCase    )
        super().test_keras_fit()
        tf.keras.mixed_precision.set_global_policy('float32'    )
   def     _UpperCAmelCase     (    self    )			->   List[Any]:


        '''simple docstring'''


        lowercase_							,						lowercase_			:       Any									=			self.model_tester.prepare_config_and_inputs_for_common()

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

             lowercase_			:       Union[str, Any]									=			['pixel_values']
             self.assertListEqual(arg_names[:1]  ,__UpperCamelCase    )
   def     _UpperCAmelCase     (    self    )			->   str:


        '''simple docstring'''


        def check_hidden_states_output(__UpperCamelCase  ,__UpperCamelCase  ,__UpperCamelCase    ):
             lowercase_			:       str									=			model_class(__UpperCamelCase    )

             lowercase_			:       int									=			model(**self._prepare_for_class(__UpperCamelCase  ,__UpperCamelCase    )    )
             lowercase_			:       Optional[int]									=			outputs.hidden_states

             lowercase_			:       Optional[int]									=			len(self.model_tester.depth    )
             self.assertEqual(len(__UpperCamelCase    )  ,__UpperCamelCase    )

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

        lowercase_							,						lowercase_			:       List[str]									=			self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
             lowercase_			:       Tuple									=			True
             check_hidden_states_output(__UpperCamelCase  ,__UpperCamelCase  ,__UpperCamelCase    )

             # check that output_hidden_states also work using config
             del inputs_dict["output_hidden_states"]
             lowercase_			:       Optional[int]									=			True

             check_hidden_states_output(__UpperCamelCase  ,__UpperCamelCase  ,__UpperCamelCase    )
   def     _UpperCAmelCase     (    self    )			->   Optional[Any]:


        '''simple docstring'''


        lowercase_			:       List[Any]									=			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*__UpperCamelCase    )
   def     _UpperCAmelCase     (    self    )			->   Any:


        '''simple docstring'''


        lowercase_			:       Tuple									=			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*__UpperCamelCase    )







   @slow
   def     _UpperCAmelCase     (    self    )			->   Union[str, Any]:


        '''simple docstring'''


        for model_name in TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             lowercase_			:       int									=			TFCvtModel.from_pretrained(__UpperCamelCase    )
             self.assertIsNotNone(__UpperCamelCase    )






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







@require_tf
@require_vision
class  UpperCamelCase	(    unittest.TestCase						):
   @cached_property
   def     _UpperCAmelCase     (    self    )			->   Optional[int]:


        '''simple docstring'''


        return AutoImageProcessor.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]    )







   @slow
   def     _UpperCAmelCase     (    self    )			->   Any:


        '''simple docstring'''


        lowercase_			:       Optional[int]									=			TFCvtForImageClassification.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]    )

        lowercase_			:       Optional[int]									=			self.default_image_processor
        lowercase_			:       Union[str, Any]									=			prepare_img()
        lowercase_			:       List[str]									=			image_processor(images=__UpperCamelCase  ,return_tensors='tf'    )

        # forward pass
        lowercase_			:       Optional[int]									=			model(**__UpperCamelCase    )

        # verify the logits
        lowercase_			:       str									=			tf.TensorShape((1, 1000)    )
        self.assertEqual(outputs.logits.shape  ,__UpperCamelCase    )

        lowercase_			:       Optional[Any]									=			tf.constant([0.9285, 0.9015, -0.3150]    )
        self.assertTrue(np.allclose(outputs.logits[0, :3].numpy()  ,__UpperCamelCase  ,atol=1e-4    )    )


"""simple docstring"""







import unittest

import numpy as np

from transformers import DistilBertConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask


if is_flax_available():
       import jax.numpy as jnp

       from transformers.models.distilbert.modeling_flax_distilbert import (
           FlaxDistilBertForMaskedLM,
           FlaxDistilBertForMultipleChoice,
           FlaxDistilBertForQuestionAnswering,
           FlaxDistilBertForSequenceClassification,
           FlaxDistilBertForTokenClassification,
           FlaxDistilBertModel,
       )







class  UpperCamelCase	(    unittest.TestCase						):
   def __init__(    self  ,__UpperCamelCase  ,__UpperCamelCase=13  ,__UpperCamelCase=7  ,__UpperCamelCase=True  ,__UpperCamelCase=True  ,__UpperCamelCase=True  ,__UpperCamelCase=True  ,__UpperCamelCase=99  ,__UpperCamelCase=32  ,__UpperCamelCase=5  ,__UpperCamelCase=4  ,__UpperCamelCase=37  ,__UpperCamelCase="gelu"  ,__UpperCamelCase=0.1  ,__UpperCamelCase=0.1  ,__UpperCamelCase=512  ,__UpperCamelCase=16  ,__UpperCamelCase=2  ,__UpperCamelCase=0.02  ,__UpperCamelCase=4  ,)			->   str:


        '''simple docstring'''


        lowercase_			:       str									=			parent
        lowercase_			:       List[str]									=			batch_size
        lowercase_			:       Optional[Any]									=			seq_length
        lowercase_			:       str									=			is_training
        lowercase_			:       Any									=			use_attention_mask
        lowercase_			:       int									=			use_token_type_ids
        lowercase_			:       Union[str, Any]									=			use_labels
        lowercase_			:       int									=			vocab_size
        lowercase_			:       Union[str, Any]									=			hidden_size
        lowercase_			:       Optional[int]									=			num_hidden_layers
        lowercase_			:       Dict									=			num_attention_heads
        lowercase_			:       Dict									=			intermediate_size
        lowercase_			:       str									=			hidden_act
        lowercase_			:       Union[str, Any]									=			hidden_dropout_prob
        lowercase_			:       Dict									=			attention_probs_dropout_prob
        lowercase_			:       Union[str, Any]									=			max_position_embeddings
        lowercase_			:       List[Any]									=			type_vocab_size
        lowercase_			:       str									=			type_sequence_label_size
        lowercase_			:       Optional[Any]									=			initializer_range
        lowercase_			:       Any									=			num_choices
   def     _UpperCAmelCase     (    self    )			->   List[Any]:


        '''simple docstring'''


        lowercase_			:       Optional[Any]									=			ids_tensor([self.batch_size, self.seq_length]  ,self.vocab_size    )

        lowercase_			:       List[Any]									=			None
        if self.use_attention_mask:
             lowercase_			:       Dict									=			random_attention_mask([self.batch_size, self.seq_length]    )

        lowercase_			:       Optional[Any]									=			DistilBertConfig(
            vocab_size=self.vocab_size  ,dim=self.hidden_size  ,n_layers=self.num_hidden_layers  ,n_heads=self.num_attention_heads  ,hidden_dim=self.intermediate_size  ,hidden_act=self.hidden_act  ,dropout=self.hidden_dropout_prob  ,attention_dropout=self.attention_probs_dropout_prob  ,max_position_embeddings=self.max_position_embeddings  ,initializer_range=self.initializer_range  ,tie_weights_=__UpperCamelCase  ,)

        return config, input_ids, attention_mask







   def     _UpperCAmelCase     (    self    )			->   Dict:


        '''simple docstring'''


        lowercase_			:       Any									=			self.prepare_config_and_inputs()
        lowercase_							,						lowercase_							,						lowercase_			:       str									=			config_and_inputs
        lowercase_			:       List[str]									=			{'input_ids': input_ids, 'attention_mask': attention_mask}
        return config, inputs_dict







@require_flax
class  UpperCamelCase	(    lowercase_		,   unittest.TestCase						):
   lowercase							=						(
       (
           FlaxDistilBertModel,
           FlaxDistilBertForMaskedLM,
           FlaxDistilBertForMultipleChoice,
           FlaxDistilBertForQuestionAnswering,
           FlaxDistilBertForSequenceClassification,
           FlaxDistilBertForTokenClassification,
           FlaxDistilBertForQuestionAnswering,
       )
       if is_flax_available()
       else ()
   )
   def     _UpperCAmelCase     (    self    )			->   List[Any]:


        '''simple docstring'''


        lowercase_			:       Optional[Any]									=			FlaxDistilBertModelTester(self    )







   @slow
   def     _UpperCAmelCase     (    self    )			->   List[str]:


        '''simple docstring'''


        for model_class_name in self.all_model_classes:
             lowercase_			:       Optional[int]									=			model_class_name.from_pretrained('distilbert-base-uncased'    )
             lowercase_			:       List[str]									=			model(np.ones((1, 1)    )    )
             self.assertIsNotNone(__UpperCamelCase    )







@require_flax
class  UpperCamelCase	(    unittest.TestCase						):
   @slow
   def     _UpperCAmelCase     (    self    )			->   str:


        '''simple docstring'''


        lowercase_			:       Optional[int]									=			FlaxDistilBertModel.from_pretrained('distilbert-base-uncased'    )
        lowercase_			:       Tuple									=			np.array([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]    )
        lowercase_			:       Optional[int]									=			np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]    )
        lowercase_			:       Any									=			model(__UpperCamelCase  ,attention_mask=__UpperCamelCase    )[0]
        lowercase_			:       Tuple									=			(1, 11, 768)
        self.assertEqual(output.shape  ,__UpperCamelCase    )
        lowercase_			:       Union[str, Any]									=			np.array([[[-0.1639, 0.3299, 0.1648], [-0.1746, 0.3289, 0.1710], [-0.1884, 0.3357, 0.1810]]]    )

        self.assertTrue(jnp.allclose(output[:, 1:4, 1:4]  ,__UpperCamelCase  ,atol=1e-4    )    )

