


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

	"""simple docstring"""





	def __init__(			self      :    Optional[Any]      ,      UpperCAmelCase__      :    List[Any]      ,      UpperCAmelCase__      :    Optional[int]=3      ,      UpperCAmelCase__      :    Tuple=3_2      ,      UpperCAmelCase__      :    int=3      ,      UpperCAmelCase__      :    Optional[Any]=1_0      ,      UpperCAmelCase__      :    Optional[int]=[1_0, 2_0, 3_0, 4_0]      ,      UpperCAmelCase__      :    Dict=[1, 1, 2, 1]      ,      UpperCAmelCase__      :    Union[str, Any]=True      ,      UpperCAmelCase__      :    Dict=True      ,      UpperCAmelCase__      :    List[str]="relu"      ,      UpperCAmelCase__      :    Dict=3      ,      UpperCAmelCase__      :    List[str]=None      ,      )	->     Tuple:
						__SCREAMING_SNAKE_CASE						       =    parent
						__SCREAMING_SNAKE_CASE						       =    batch_size
						__SCREAMING_SNAKE_CASE						       =    image_size
						__SCREAMING_SNAKE_CASE						       =    num_channels
						__SCREAMING_SNAKE_CASE						       =    embeddings_size
						__SCREAMING_SNAKE_CASE						       =    hidden_sizes
						__SCREAMING_SNAKE_CASE						       =    depths
						__SCREAMING_SNAKE_CASE						       =    is_training
						__SCREAMING_SNAKE_CASE						       =    use_labels
						__SCREAMING_SNAKE_CASE						       =    hidden_act
						__SCREAMING_SNAKE_CASE						       =    num_labels
						__SCREAMING_SNAKE_CASE						       =    scope
						__SCREAMING_SNAKE_CASE						       =    len(UpperCAmelCase__       )




	def   UpperCAmelCase_				(			self      :    List[Any]       )	->     List[str]:
						__SCREAMING_SNAKE_CASE						       =    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

						__SCREAMING_SNAKE_CASE						       =    self.get_config()

						return config, pixel_values




	def   UpperCAmelCase_				(			self      :    int       )	->     List[str]:
						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   UpperCAmelCase_				(			self      :    Tuple      ,      UpperCAmelCase__      :    Union[str, Any]      ,      UpperCAmelCase__      :    List[Any]       )	->     Tuple:
						__SCREAMING_SNAKE_CASE						       =    FlaxRegNetModel(config=UpperCAmelCase__       )
						__SCREAMING_SNAKE_CASE						       =    model(UpperCAmelCase__       )

						# 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   UpperCAmelCase_				(			self      :    Optional[Any]      ,      UpperCAmelCase__      :    List[Any]      ,      UpperCAmelCase__      :    Any       )	->     List[str]:
						__SCREAMING_SNAKE_CASE						       =    self.num_labels
						__SCREAMING_SNAKE_CASE						       =    FlaxRegNetForImageClassification(config=UpperCAmelCase__       )
						__SCREAMING_SNAKE_CASE						       =    model(UpperCAmelCase__       )
						self.parent.assertEqual(result.logits.shape      ,      (self.batch_size, self.num_labels)       )




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




@require_flax
class 			UpperCamelCase_     (			UpperCamelCase      ,  unittest.TestCase):

	"""simple docstring"""





	snake_case__    :  int    =      (FlaxRegNetModel, FlaxRegNetForImageClassification) if is_flax_available() else ()

	snake_case__    :  List[Any]    =      False
	snake_case__    :  Union[str, Any]    =      False
	snake_case__    :  Optional[Any]    =      False




	def   UpperCAmelCase_				(			self      :    Any       )	->     None:
						__SCREAMING_SNAKE_CASE						       =    FlaxRegNetModelTester(self       )
						__SCREAMING_SNAKE_CASE						       =    ConfigTester(self      ,      config_class=UpperCAmelCase__      ,      has_text_modality=UpperCAmelCase__       )




	def   UpperCAmelCase_				(			self      :    Optional[int]       )	->     int:
						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   UpperCAmelCase_				(			self      :    Optional[Any]       )	->     Dict:
						return




	def   UpperCAmelCase_				(			self      :    Optional[Any]       )	->     List[Any]:
						__SCREAMING_SNAKE_CASE						       =    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*UpperCAmelCase__       )




	def   UpperCAmelCase_				(			self      :    str       )	->     List[str]:
						__SCREAMING_SNAKE_CASE						       =    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*UpperCAmelCase__       )




	@unittest.skip(reason="RegNet does not use inputs_embeds"       )
	def   UpperCAmelCase_				(			self      :    Dict       )	->     Union[str, Any]:
						pass




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




	def   UpperCAmelCase_				(			self      :    int       )	->     List[str]:
						__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(UpperCAmelCase__       )
											__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]      ,      UpperCAmelCase__       )




	def   UpperCAmelCase_				(			self      :    Union[str, Any]       )	->     Union[str, Any]:
						def check_hidden_states_output(UpperCAmelCase__      :    Union[str, Any]      ,      UpperCAmelCase__      :    Optional[int]      ,      UpperCAmelCase__      :    int       ):
											__SCREAMING_SNAKE_CASE						       =    model_class(UpperCAmelCase__       )

											__SCREAMING_SNAKE_CASE						       =    model(**self._prepare_for_class(UpperCAmelCase__      ,      UpperCAmelCase__       )       )

											__SCREAMING_SNAKE_CASE						       =    outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

											__SCREAMING_SNAKE_CASE						       =    self.model_tester.num_stages
											self.assertEqual(len(UpperCAmelCase__       )      ,      expected_num_stages + 1       )

						__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(UpperCAmelCase__      ,      UpperCAmelCase__      ,      UpperCAmelCase__       )

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

											check_hidden_states_output(UpperCAmelCase__      ,      UpperCAmelCase__      ,      UpperCAmelCase__       )




	def   UpperCAmelCase_				(			self      :    List[Any]       )	->     int:
						__SCREAMING_SNAKE_CASE       ,  __SCREAMING_SNAKE_CASE						       =    self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
											with self.subTest(model_class.__name__       ):
																__SCREAMING_SNAKE_CASE						       =    self._prepare_for_class(UpperCAmelCase__      ,      UpperCAmelCase__       )
																__SCREAMING_SNAKE_CASE						       =    model_class(UpperCAmelCase__       )

																@jax.jit
																def model_jitted(UpperCAmelCase__      :    Union[str, Any]      ,      **UpperCAmelCase__      :    Tuple       ):
																					return model(pixel_values=UpperCAmelCase__      ,      **UpperCAmelCase__       )

																with self.subTest("JIT Enabled"       ):
																					__SCREAMING_SNAKE_CASE						       =    model_jitted(**UpperCAmelCase__       ).to_tuple()

																with self.subTest("JIT Disabled"       ):
																					with jax.disable_jit():
																										__SCREAMING_SNAKE_CASE						       =    model_jitted(**UpperCAmelCase__       ).to_tuple()

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




def        UpperCAmelCase__	():




					'''simple docstring'''


					__SCREAMING_SNAKE_CASE						       =    Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"       )
					return image




@require_flax
class 			UpperCamelCase_     (			unittest.TestCase):

	"""simple docstring"""





	@cached_property
	def   UpperCAmelCase_				(			self      :    Dict       )	->     Tuple:
						return AutoImageProcessor.from_pretrained("facebook/regnet-y-040"       ) if is_vision_available() else None




	@slow
	def   UpperCAmelCase_				(			self      :    List[Any]       )	->     Optional[Any]:
						__SCREAMING_SNAKE_CASE						       =    FlaxRegNetForImageClassification.from_pretrained("facebook/regnet-y-040"       )

						__SCREAMING_SNAKE_CASE						       =    self.default_image_processor
						__SCREAMING_SNAKE_CASE						       =    prepare_img()
						__SCREAMING_SNAKE_CASE						       =    image_processor(images=UpperCAmelCase__      ,      return_tensors="np"       )

						__SCREAMING_SNAKE_CASE						       =    model(**UpperCAmelCase__       )

						# verify the logits
						__SCREAMING_SNAKE_CASE						       =    (1, 1_0_0_0)
						self.assertEqual(outputs.logits.shape      ,      UpperCAmelCase__       )

						__SCREAMING_SNAKE_CASE						       =    jnp.array([-0.4_180, -1.5_051, -3.4_836]       )

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




"""simple docstring"""







from typing import List

from ...configuration_utils import PretrainedConfig
from ...utils import logging


a__    :							Tuple            =					logging.get_logger(__name__)

a__    :							List[Any]            =					{
    '''snap-research/efficientformer-l1-300''': (
        '''https://huggingface.co/snap-research/efficientformer-l1-300/resolve/main/config.json'''
    ),
}




class 			UpperCamelCase_     (			UpperCamelCase):

	"""simple docstring"""





	snake_case__    :  Any    =      "efficientformer"




	def __init__(			self      :    Any      ,      UpperCAmelCase__      :    List[int] = [3, 2, 6, 4]      ,      UpperCAmelCase__      :    List[int] = [4_8, 9_6, 2_2_4, 4_4_8]      ,      UpperCAmelCase__      :    List[bool] = [True, True, True, True]      ,      UpperCAmelCase__      :    int = 4_4_8      ,      UpperCAmelCase__      :    int = 3_2      ,      UpperCAmelCase__      :    int = 4      ,      UpperCAmelCase__      :    int = 7      ,      UpperCAmelCase__      :    int = 5      ,      UpperCAmelCase__      :    int = 8      ,      UpperCAmelCase__      :    int = 4      ,      UpperCAmelCase__      :    float = 0.0      ,      UpperCAmelCase__      :    int = 1_6      ,      UpperCAmelCase__      :    int = 3      ,      UpperCAmelCase__      :    int = 3      ,      UpperCAmelCase__      :    int = 3      ,      UpperCAmelCase__      :    int = 2      ,      UpperCAmelCase__      :    int = 1      ,      UpperCAmelCase__      :    float = 0.0      ,      UpperCAmelCase__      :    int = 1      ,      UpperCAmelCase__      :    bool = True      ,      UpperCAmelCase__      :    bool = True      ,      UpperCAmelCase__      :    float = 1E-5      ,      UpperCAmelCase__      :    str = "gelu"      ,      UpperCAmelCase__      :    float = 0.02      ,      UpperCAmelCase__      :    float = 1E-12      ,      UpperCAmelCase__      :    int = 2_2_4      ,      UpperCAmelCase__      :    float = 1E-05      ,      **UpperCAmelCase__      :    Tuple      ,      )	->     None:
						super().__init__(**UpperCAmelCase__       )

						__SCREAMING_SNAKE_CASE						       =    hidden_act
						__SCREAMING_SNAKE_CASE						       =    hidden_dropout_prob
						__SCREAMING_SNAKE_CASE						       =    hidden_sizes
						__SCREAMING_SNAKE_CASE						       =    num_hidden_layers
						__SCREAMING_SNAKE_CASE						       =    num_attention_heads
						__SCREAMING_SNAKE_CASE						       =    initializer_range
						__SCREAMING_SNAKE_CASE						       =    layer_norm_eps
						__SCREAMING_SNAKE_CASE						       =    patch_size
						__SCREAMING_SNAKE_CASE						       =    num_channels
						__SCREAMING_SNAKE_CASE						       =    depths
						__SCREAMING_SNAKE_CASE						       =    mlp_expansion_ratio
						__SCREAMING_SNAKE_CASE						       =    downsamples
						__SCREAMING_SNAKE_CASE						       =    dim
						__SCREAMING_SNAKE_CASE						       =    key_dim
						__SCREAMING_SNAKE_CASE						       =    attention_ratio
						__SCREAMING_SNAKE_CASE						       =    resolution
						__SCREAMING_SNAKE_CASE						       =    pool_size
						__SCREAMING_SNAKE_CASE						       =    downsample_patch_size
						__SCREAMING_SNAKE_CASE						       =    downsample_stride
						__SCREAMING_SNAKE_CASE						       =    downsample_pad
						__SCREAMING_SNAKE_CASE						       =    drop_path_rate
						__SCREAMING_SNAKE_CASE						       =    num_metaad_blocks
						__SCREAMING_SNAKE_CASE						       =    distillation
						__SCREAMING_SNAKE_CASE						       =    use_layer_scale
						__SCREAMING_SNAKE_CASE						       =    layer_scale_init_value
						__SCREAMING_SNAKE_CASE						       =    image_size
						__SCREAMING_SNAKE_CASE						       =    batch_norm_eps
