


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     a		(unittest.TestCase				):




							"""simple docstring"""


							def __init__(							self	:  str   ,     lowerCamelCase	:  str   ,     lowerCamelCase	:  Tuple=3   ,     lowerCamelCase	:  str=32   ,     lowerCamelCase	:  Optional[int]=3   ,     lowerCamelCase	:  Union[str, Any]=10   ,     lowerCamelCase	:  int=[10, 20, 30, 40]   ,     lowerCamelCase	:  str=[1, 1, 2, 1]   ,     lowerCamelCase	:  Optional[Any]=True   ,     lowerCamelCase	:  Tuple=True   ,     lowerCamelCase	:  Dict="relu"   ,     lowerCamelCase	:  str=3   ,     lowerCamelCase	:  Dict=None   ,     )     ->   Tuple:
														__snake_case		:      Union[str, Any]										=       parent
														__snake_case		:      Optional[int]										=       batch_size
														__snake_case		:      Optional[int]										=       image_size
														__snake_case		:      Union[str, Any]										=       num_channels
														__snake_case		:      Dict										=       embeddings_size
														__snake_case		:      Any										=       hidden_sizes
														__snake_case		:      Optional[Any]										=       depths
														__snake_case		:      List[Any]										=       is_training
														__snake_case		:      int										=       use_labels
														__snake_case		:      List[str]										=       hidden_act
														__snake_case		:      str										=       num_labels
														__snake_case		:      Union[str, Any]										=       scope
														__snake_case		:      Tuple										=       len(_SCREAMING_SNAKE_CASE    )

							def    __snake_case (							self	:  str    )     ->   Dict:
														__snake_case		:      Optional[Any]										=       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

														__snake_case		:      str										=       self.get_config()

														return config, pixel_values

							def    __snake_case (							self	:  Optional[Any]    )     ->   Union[str, Any]:
														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	:  Any   ,     lowerCamelCase	:  Optional[int]   ,     lowerCamelCase	:  Any    )     ->   List[str]:
														__snake_case		:      List[Any]										=       FlaxRegNetModel(config=_SCREAMING_SNAKE_CASE    )
														__snake_case		:      Optional[Any]										=       model(_SCREAMING_SNAKE_CASE    )

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

							def    __snake_case (							self	:  List[str]   ,     lowerCamelCase	:  Union[str, Any]   ,     lowerCamelCase	:  Dict    )     ->   Any:
														__snake_case		:      Any										=       self.num_labels
														__snake_case		:      Dict										=       FlaxRegNetForImageClassification(config=_SCREAMING_SNAKE_CASE    )
														__snake_case		:      List[str]										=       model(_SCREAMING_SNAKE_CASE    )
														self.parent.assertEqual(result.logits.shape   ,     (self.batch_size, self.num_labels)    )






							def    __snake_case (							self	:  List[str]    )     ->   List[Any]:
														__snake_case		:      Optional[int]										=       self.prepare_config_and_inputs()
														__snake_case		:      Union[str, Any]										=       config_and_inputs
														__snake_case		:      Any										=       {"pixel_values": pixel_values}
														return config, inputs_dict

@require_flax
class     a		(SCREAMING_SNAKE_CASE__					,    unittest.TestCase				):




							"""simple docstring"""


							__UpperCAmelCase   :							str					  =  (FlaxRegNetModel, FlaxRegNetForImageClassification) if is_flax_available() else ()

							__UpperCAmelCase   :							Optional[Any]					  =  False
							__UpperCAmelCase   :							Dict					  =  False
							__UpperCAmelCase   :							List[Any]					  =  False

							def    __snake_case (							self	:  str    )     ->   None:
														__snake_case		:      Tuple										=       FlaxRegNetModelTester(self    )
														__snake_case		:      Optional[Any]										=       ConfigTester(self   ,     config_class=_SCREAMING_SNAKE_CASE   ,     has_text_modality=_SCREAMING_SNAKE_CASE    )

							def    __snake_case (							self	:  int    )     ->   List[str]:
														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[Any]    )     ->   str:
														return

							def    __snake_case (							self	:  Optional[int]    )     ->   Any:
														__snake_case		:      Dict										=       self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE    )

							def    __snake_case (							self	:  Optional[int]    )     ->   Union[str, Any]:
														__snake_case		:      Union[str, Any]										=       self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_image_classification(*_SCREAMING_SNAKE_CASE    )

							@unittest.skip(reason="RegNet does not use inputs_embeds"    )
							def    __snake_case (							self	:  List[Any]    )     ->   Optional[int]:
														pass

							@unittest.skip(reason="RegNet does not support input and output embeddings"    )
							def    __snake_case (							self	:  Tuple    )     ->   List[Any]:
														pass

							def    __snake_case (							self	:  Tuple    )     ->   str:
														__snake_case		:      List[str]										=       self.model_tester.prepare_config_and_inputs_for_common()

														for model_class in self.all_model_classes:
																					__snake_case		:      List[Any]										=       model_class(_SCREAMING_SNAKE_CASE    )
																					__snake_case		:      Union[str, Any]										=       inspect.signature(model.__call__    )
																					# signature.parameters is an OrderedDict => so arg_names order is deterministic
																					__snake_case		:      Union[str, Any]										=       [*signature.parameters.keys()]

																					__snake_case		:      List[Any]										=       ["pixel_values"]
																					self.assertListEqual(arg_names[:1]   ,     _SCREAMING_SNAKE_CASE    )

							def    __snake_case (							self	:  Optional[int]    )     ->   Optional[int]:
														def check_hidden_states_output(lowerCamelCase	:  str   ,     lowerCamelCase	:  Dict   ,     lowerCamelCase	:  int    ):
																					__snake_case		:      Any										=       model_class(_SCREAMING_SNAKE_CASE    )

																					__snake_case		:      Dict										=       model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE   ,     _SCREAMING_SNAKE_CASE    )    )

																					__snake_case		:      Optional[int]										=       outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

																					__snake_case		:      List[str]										=       self.model_tester.num_stages
																					self.assertEqual(len(_SCREAMING_SNAKE_CASE    )   ,     expected_num_stages + 1    )

														__snake_case		:      int										=       self.model_tester.prepare_config_and_inputs_for_common()

														for model_class in self.all_model_classes:
																					__snake_case		:      str										=       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"]
																					__snake_case		:      Union[str, Any]										=       True

																					check_hidden_states_output(_SCREAMING_SNAKE_CASE   ,     _SCREAMING_SNAKE_CASE   ,     _SCREAMING_SNAKE_CASE    )






							def    __snake_case (							self	:  Any    )     ->   int:
														__snake_case		:      Optional[int]										=       self.model_tester.prepare_config_and_inputs_for_common()

														for model_class in self.all_model_classes:
																					with self.subTest(model_class.__name__    ):
																												__snake_case		:      Optional[Any]										=       self._prepare_for_class(_SCREAMING_SNAKE_CASE   ,     _SCREAMING_SNAKE_CASE    )
																												__snake_case		:      str										=       model_class(_SCREAMING_SNAKE_CASE    )

																												@jax.jit
																												def model_jitted(lowerCamelCase	:  Union[str, Any]   ,     **lowerCamelCase	:  Optional[int]    ):
																																			return model(pixel_values=_SCREAMING_SNAKE_CASE   ,     **_SCREAMING_SNAKE_CASE    )

																												with self.subTest("JIT Enabled"    ):
																																			__snake_case		:      List[str]										=       model_jitted(**_SCREAMING_SNAKE_CASE    ).to_tuple()

																												with self.subTest("JIT Disabled"    ):
																																			with jax.disable_jit():
																																										__snake_case		:      List[Any]										=       model_jitted(**_SCREAMING_SNAKE_CASE    ).to_tuple()

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







def     lowerCAmelCase_					( ):
							__snake_case		:      Optional[int]										=       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"     )
							return image

@require_flax
class     a		(unittest.TestCase				):




							"""simple docstring"""


							@cached_property
							def    __snake_case (							self	:  List[Any]    )     ->   Optional[Any]:
														return AutoImageProcessor.from_pretrained("facebook/regnet-y-040"    ) if is_vision_available() else None






							@slow
							def    __snake_case (							self	:  Union[str, Any]    )     ->   str:
														__snake_case		:      Any										=       FlaxRegNetForImageClassification.from_pretrained("facebook/regnet-y-040"    )

														__snake_case		:      str										=       self.default_image_processor
														__snake_case		:      List[Any]										=       prepare_img()
														__snake_case		:      List[str]										=       image_processor(images=_SCREAMING_SNAKE_CASE   ,     return_tensors="np"    )

														__snake_case		:      Optional[int]										=       model(**_SCREAMING_SNAKE_CASE    )

														# verify the logits
														__snake_case		:      Union[str, Any]										=       (1, 1000)
														self.assertEqual(outputs.logits.shape   ,     _SCREAMING_SNAKE_CASE    )

														__snake_case		:      List[str]										=       jnp.array([-0.41_80, -1.50_51, -3.48_36]    )

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





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


_snake_case					:		int      =    logging.get_logger(__name__)

_snake_case					:		int      =    {
    "microsoft/beit-base-patch16-224-pt22k": (
        "https://huggingface.co/microsoft/beit-base-patch16-224-pt22k/resolve/main/config.json"
    ),
    # See all BEiT models at https://huggingface.co/models?filter=beit
}

class     a		(_lowerCAmelCase				):




							"""simple docstring"""


							__UpperCAmelCase   :							Union[str, Any]					  =  "beit"

							def __init__(							self	:  Union[str, Any]   ,     lowerCamelCase	:  Any=8192   ,     lowerCamelCase	:  Dict=768   ,     lowerCamelCase	:  int=12   ,     lowerCamelCase	:  Optional[Any]=12   ,     lowerCamelCase	:  List[str]=3072   ,     lowerCamelCase	:  Tuple="gelu"   ,     lowerCamelCase	:  Union[str, Any]=0.0   ,     lowerCamelCase	:  int=0.0   ,     lowerCamelCase	:  Dict=0.02   ,     lowerCamelCase	:  List[str]=1E-12   ,     lowerCamelCase	:  Optional[Any]=224   ,     lowerCamelCase	:  Optional[int]=16   ,     lowerCamelCase	:  Any=3   ,     lowerCamelCase	:  Optional[int]=False   ,     lowerCamelCase	:  Any=False   ,     lowerCamelCase	:  Optional[Any]=False   ,     lowerCamelCase	:  int=False   ,     lowerCamelCase	:  Any=0.1   ,     lowerCamelCase	:  Tuple=0.1   ,     lowerCamelCase	:  Optional[int]=True   ,     lowerCamelCase	:  int=[3, 5, 7, 11]   ,     lowerCamelCase	:  str=[1, 2, 3, 6]   ,     lowerCamelCase	:  int=True   ,     lowerCamelCase	:  List[Any]=0.4   ,     lowerCamelCase	:  int=256   ,     lowerCamelCase	:  str=1   ,     lowerCamelCase	:  List[str]=False   ,     lowerCamelCase	:  List[str]=255   ,     **lowerCamelCase	:  Dict   ,     )     ->   int:
														super().__init__(**lowerCamelCase    )

														__snake_case		:      Any										=       vocab_size
														__snake_case		:      List[str]										=       hidden_size
														__snake_case		:      List[Any]										=       num_hidden_layers
														__snake_case		:      Tuple										=       num_attention_heads
														__snake_case		:      Dict										=       intermediate_size
														__snake_case		:      Union[str, Any]										=       hidden_act
														__snake_case		:      Optional[Any]										=       hidden_dropout_prob
														__snake_case		:      Optional[int]										=       attention_probs_dropout_prob
														__snake_case		:      Union[str, Any]										=       initializer_range
														__snake_case		:      str										=       layer_norm_eps

														__snake_case		:      Optional[Any]										=       image_size
														__snake_case		:      List[str]										=       patch_size
														__snake_case		:      Optional[Any]										=       num_channels
														__snake_case		:      Any										=       use_mask_token
														__snake_case		:      List[str]										=       use_absolute_position_embeddings
														__snake_case		:      List[Any]										=       use_relative_position_bias
														__snake_case		:      str										=       use_shared_relative_position_bias
														__snake_case		:      str										=       layer_scale_init_value
														__snake_case		:      Any										=       drop_path_rate
														__snake_case		:      int										=       use_mean_pooling
														# decode head attributes (semantic segmentation)
														__snake_case		:      Optional[Any]										=       out_indices
														__snake_case		:      List[str]										=       pool_scales
														# auxiliary head attributes (semantic segmentation)
														__snake_case		:      int										=       use_auxiliary_head
														__snake_case		:      int										=       auxiliary_loss_weight
														__snake_case		:      Optional[int]										=       auxiliary_channels
														__snake_case		:      int										=       auxiliary_num_convs
														__snake_case		:      str										=       auxiliary_concat_input
														__snake_case		:      List[str]										=       semantic_loss_ignore_index

class     a		(_lowerCAmelCase				):




							"""simple docstring"""


							__UpperCAmelCase   :							Union[str, Any]					  =  version.parse("1.11"				)

							@property
							def    __snake_case (							self	:  Dict    )     ->   Mapping[str, Mapping[int, str]]:
														return OrderedDict(
														    [
														        ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}),
														    ]    )






							@property
							def    __snake_case (							self	:  str    )     ->   float:
														return 1E-4

