


"""simple docstring"""



import inspect
import unittest

from transformers import RegNetConfig, is_flax_available
from transformers.testing_utils import require_flax, slow
from transformers.utils import cached_property, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor


if is_flax_available():
 import jax
 import jax.numpy as jnp

 from transformers.models.regnet.modeling_flax_regnet import FlaxRegNetForImageClassification, FlaxRegNetModel

if is_vision_available():
 from PIL import Image

 from transformers import AutoImageProcessor
class 					__magic_name__		(			unittest.TestCase     ):






       def __init__(   self  :    Any  ,     snake_case__  :    List[Any]  ,     snake_case__  :    List[str]=3  ,     snake_case__  :    Union[str, Any]=3_2  ,     snake_case__  :    List[str]=3  ,     snake_case__  :    Any=1_0  ,     snake_case__  :    str=[1_0, 2_0, 3_0, 4_0]  ,     snake_case__  :    Any=[1, 1, 2, 1]  ,     snake_case__  :    Any=True  ,     snake_case__  :    str=True  ,     snake_case__  :    Dict="relu"  ,     snake_case__  :    Tuple=3  ,     snake_case__  :    List[Any]=None  ,     ):

              '''simple docstring'''






              lowercase		:Union[str, Any]       							=				parent
              lowercase		:List[Any]       							=				batch_size
              lowercase		:Tuple       							=				image_size
              lowercase		:str       							=				num_channels
              lowercase		:str       							=				embeddings_size
              lowercase		:Union[str, Any]       							=				hidden_sizes
              lowercase		:List[str]       							=				depths
              lowercase		:Tuple       							=				is_training
              lowercase		:List[str]       							=				use_labels
              lowercase		:int       							=				hidden_act
              lowercase		:Optional[int]       							=				num_labels
              lowercase		:Optional[int]       							=				scope
              lowercase		:Dict       							=				len(_A				)






       def  __snake_case						(   self  :    List[str]				):

              '''simple docstring'''






              lowercase		:List[str]       							=				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

              lowercase		:Union[str, Any]       							=				self.get_config()

              return config, pixel_values






       def  __snake_case						(   self  :    Union[str, Any]				):

              '''simple docstring'''






              return RegNetConfig(
                  num_channels=self.num_channels  ,     embeddings_size=self.embeddings_size  ,     hidden_sizes=self.hidden_sizes  ,     depths=self.depths  ,     hidden_act=self.hidden_act  ,     num_labels=self.num_labels  ,     image_size=self.image_size  ,     )






       def  __snake_case						(   self  :    str  ,     snake_case__  :    Union[str, Any]  ,     snake_case__  :    str				):

              '''simple docstring'''






              lowercase		:Any       							=				FlaxRegNetModel(config=_A				)
              lowercase		:Any       							=				model(_A				)

              # Output shape (b, c, h, w)
              self.parent.assertEqual(
                  result.last_hidden_state.shape  ,     (self.batch_size, self.hidden_sizes[-1], self.image_size // 3_2, self.image_size // 3_2)  ,     )






       def  __snake_case						(   self  :    Optional[int]  ,     snake_case__  :    Union[str, Any]  ,     snake_case__  :    str				):

              '''simple docstring'''






              lowercase		:Optional[Any]       							=				self.num_labels
              lowercase		:Dict       							=				FlaxRegNetForImageClassification(config=_A				)
              lowercase		:Optional[int]       							=				model(_A				)
              self.parent.assertEqual(result.logits.shape  ,     (self.batch_size, self.num_labels)				)







       def  __snake_case						(   self  :    Union[str, Any]				):

              '''simple docstring'''






              lowercase		:Optional[Any]       							=				self.prepare_config_and_inputs()
              lowercase							,  lowercase		:List[Any]       							=				config_and_inputs
              lowercase		:int       							=				{'''pixel_values''': pixel_values}
              return config, inputs_dict







@require_flax
class 					__magic_name__		(			__lowercase			,						unittest.TestCase     ):
       __A			:      Tuple        =   (FlaxRegNetModel, FlaxRegNetForImageClassification) if is_flax_available() else ()

       __A			:      Tuple        =   False
       __A			:      Dict        =   False
       __A			:      int        =   False






       def  __snake_case						(   self  :    List[Any]				):

              '''simple docstring'''






              lowercase		:Any       							=				FlaxRegNetModelTester(self				)
              lowercase		:str       							=				ConfigTester(self  ,     config_class=_A  ,     has_text_modality=_A				)






       def  __snake_case						(   self  :    Any				):

              '''simple docstring'''






              self.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()






       def  __snake_case						(   self  :    List[str]				):

              '''simple docstring'''






              return






       def  __snake_case						(   self  :    Optional[int]				):

              '''simple docstring'''






              lowercase		:Union[str, Any]       							=				self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_model(*_A				)






       def  __snake_case						(   self  :    str				):

              '''simple docstring'''






              lowercase		:Tuple       							=				self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_image_classification(*_A				)






       @unittest.skip(reason='''RegNet does not use inputs_embeds'''				)
       def  __snake_case						(   self  :    Union[str, Any]				):

              '''simple docstring'''






              pass






       @unittest.skip(reason='''RegNet does not support input and output embeddings'''				)
       def  __snake_case						(   self  :    Dict				):

              '''simple docstring'''






              pass






       def  __snake_case						(   self  :    Tuple				):

              '''simple docstring'''






              lowercase							,  lowercase		:Dict       							=				self.model_tester.prepare_config_and_inputs_for_common()

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

                     lowercase		:Optional[int]       							=				['''pixel_values''']
                     self.assertListEqual(arg_names[:1]  ,     _A				)






       def  __snake_case						(   self  :    Optional[Any]				):

              '''simple docstring'''






              def check_hidden_states_output(snake_case__  :    Optional[int]  ,     snake_case__  :    Union[str, Any]  ,     snake_case__  :    Optional[int]				):
                     lowercase		:Optional[Any]       							=				model_class(_A				)

                     lowercase		:Any       							=				model(**self._prepare_for_class(_A  ,     _A				)				)

                     lowercase		:int       							=				outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

                     lowercase		:str       							=				self.model_tester.num_stages
                     self.assertEqual(len(_A				)  ,     expected_num_stages + 1				)

              lowercase							,  lowercase		:str       							=				self.model_tester.prepare_config_and_inputs_for_common()

              for model_class in self.all_model_classes:
                     lowercase		:Dict       							=				True
                     check_hidden_states_output(_A  ,     _A  ,     _A				)

                     # check that output_hidden_states also work using config
                     del inputs_dict["output_hidden_states"]
                     lowercase		:List[str]       							=				True

                     check_hidden_states_output(_A  ,     _A  ,     _A				)







       def  __snake_case						(   self  :    Optional[int]				):

              '''simple docstring'''






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

              for model_class in self.all_model_classes:
                     with self.subTest(model_class.__name__				):
                            lowercase		:List[str]       							=				self._prepare_for_class(_A  ,     _A				)
                            lowercase		:int       							=				model_class(_A				)

                            @jax.jit
                            def model_jitted(snake_case__  :    Any  ,     **snake_case__  :    Dict				):
                                   return model(pixel_values=_A  ,     **_A				)

                            with self.subTest('''JIT Enabled'''				):
                                   lowercase		:int       							=				model_jitted(**_A				).to_tuple()

                            with self.subTest('''JIT Disabled'''				):
                                   with jax.disable_jit():
                                          lowercase		:Any       							=				model_jitted(**_A				).to_tuple()

                            self.assertEqual(len(_A				)  ,     len(_A				)				)
                            for jitted_output, output in zip(_A  ,     _A				):
                                   self.assertEqual(jitted_output.shape  ,     output.shape				)







def       lowerCamelCase					()     ->							List[Any]:
       lowercase		:Dict       							=				Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''')
       return image




@require_flax
class 					__magic_name__		(			unittest.TestCase     ):






       @cached_property
       def  __snake_case						(   self  :    str				):

              '''simple docstring'''






              return AutoImageProcessor.from_pretrained('''facebook/regnet-y-040'''				) if is_vision_available() else None







       @slow
       def  __snake_case						(   self  :    str				):

              '''simple docstring'''






              lowercase		:Optional[int]       							=				FlaxRegNetForImageClassification.from_pretrained('''facebook/regnet-y-040'''				)

              lowercase		:Optional[int]       							=				self.default_image_processor
              lowercase		:List[str]       							=				prepare_img()
              lowercase		:Dict       							=				image_processor(images=_A  ,     return_tensors='''np'''				)

              lowercase		:int       							=				model(**_A				)

              # verify the logits
              lowercase		:List[str]       							=				(1, 1_0_0_0)
              self.assertEqual(outputs.logits.shape  ,     _A				)

              lowercase		:Optional[Any]       							=				jnp.array([-0.41_80, -1.50_51, -3.48_36]				)

              self.assertTrue(jnp.allclose(outputs.logits[0, :3]  ,     _A  ,     atol=1e-4				)				)










"""simple docstring"""



from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


UpperCAmelCase      		=			logging.get_logger(__name__)

UpperCAmelCase      		=			{
    '''facebook/deit-base-distilled-patch16-224''': (
        '''https://huggingface.co/facebook/deit-base-patch16-224/resolve/main/config.json'''
    ),
    # See all DeiT models at https://huggingface.co/models?filter=deit
}
class 					__magic_name__		(			__UpperCAmelCase     ):
       __A			:      Tuple        =   "deit"






       def __init__(   self  :    List[str]  ,     snake_case__  :    int=7_6_8  ,     snake_case__  :    List[Any]=1_2  ,     snake_case__  :    Dict=1_2  ,     snake_case__  :    List[Any]=3_0_7_2  ,     snake_case__  :    Optional[int]="gelu"  ,     snake_case__  :    Dict=0.0  ,     snake_case__  :    str=0.0  ,     snake_case__  :    List[str]=0.02  ,     snake_case__  :    Optional[int]=1e-1_2  ,     snake_case__  :    Any=2_2_4  ,     snake_case__  :    Optional[int]=1_6  ,     snake_case__  :    int=3  ,     snake_case__  :    str=True  ,     snake_case__  :    Optional[int]=1_6  ,     **snake_case__  :    List[Any]  ,     ):

              '''simple docstring'''






              super().__init__(**snake_case__				)

              lowercase		:List[str]       							=				hidden_size
              lowercase		:Union[str, Any]       							=				num_hidden_layers
              lowercase		:Optional[Any]       							=				num_attention_heads
              lowercase		:Optional[Any]       							=				intermediate_size
              lowercase		:int       							=				hidden_act
              lowercase		:int       							=				hidden_dropout_prob
              lowercase		:Dict       							=				attention_probs_dropout_prob
              lowercase		:Optional[int]       							=				initializer_range
              lowercase		:Tuple       							=				layer_norm_eps
              lowercase		:Optional[int]       							=				image_size
              lowercase		:Union[str, Any]       							=				patch_size
              lowercase		:Optional[Any]       							=				num_channels
              lowercase		:Tuple       							=				qkv_bias
              lowercase		:Optional[Any]       							=				encoder_stride







class 					__magic_name__		(			__UpperCAmelCase     ):
       __A			:      Optional[int]        =   version.parse("1.11"     )






       @property
       def  __snake_case						(   self  :    int				):

              '''simple docstring'''






              return OrderedDict(
                  [
                      ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}),
                  ]				)







       @property
       def  __snake_case						(   self  :    Optional[int]				):

              '''simple docstring'''






              return 1e-4

