


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





							'''simple docstring'''






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







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

														lowerCAmelCase__      =			self.get_config()

														return config, pixel_values







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							int:
														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 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_      ,					lowerCamelCase_     ) ->							Optional[Any]:
														lowerCAmelCase__      =			FlaxRegNetModel(config=lowerCamelCase_     )
														lowerCAmelCase__      =			model(lowerCamelCase_     )

														# 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 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_      ,					lowerCamelCase_     ) ->							int:
														lowerCAmelCase__      =			self.num_labels
														lowerCAmelCase__      =			FlaxRegNetForImageClassification(config=lowerCamelCase_     )
														lowerCAmelCase__      =			model(lowerCamelCase_     )
														self.parent.assertEqual(result.logits.shape      ,					(self.batch_size, self.num_labels)     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Dict:
														lowerCAmelCase__      =			self.prepare_config_and_inputs()
														lowerCAmelCase__     ,      lowerCAmelCase__      =			config_and_inputs
														lowerCAmelCase__      =			{'''pixel_values''': pixel_values}
														return config, inputs_dict




@require_flax
class 	a__       (							a__       ,       unittest.TestCase						):





							'''simple docstring'''






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

							lowercase__    :     List[str]					=   False
							lowercase__    :     Optional[Any]					=   False
							lowercase__    :     List[Any]					=   False







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							None:
														lowerCAmelCase__      =			FlaxRegNetModelTester(self     )
														lowerCAmelCase__      =			ConfigTester(self      ,					config_class=lowerCamelCase_      ,					has_text_modality=lowerCamelCase_     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Dict:
														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 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Dict:
														return







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Union[str, Any]:
														lowerCAmelCase__      =			self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*lowerCamelCase_     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							List[Any]:
														lowerCAmelCase__      =			self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_image_classification(*lowerCamelCase_     )







							@unittest.skip(reason='''RegNet does not use inputs_embeds'''     )
							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							List[Any]:
														pass







							@unittest.skip(reason='''RegNet does not support input and output embeddings'''     )
							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							str:
														pass







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Optional[int]:
														lowerCAmelCase__     ,      lowerCAmelCase__      =			self.model_tester.prepare_config_and_inputs_for_common()

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

																					lowerCAmelCase__      =			['''pixel_values''']
																					self.assertListEqual(arg_names[:1]      ,					lowerCamelCase_     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Tuple:
														def check_hidden_states_output(lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_     ):
																					lowerCAmelCase__      =			model_class(lowerCamelCase_     )

																					lowerCAmelCase__      =			model(**self._prepare_for_class(lowerCamelCase_      ,					lowerCamelCase_     )     )

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

																					lowerCAmelCase__      =			self.model_tester.num_stages
																					self.assertEqual(len(lowerCamelCase_     )      ,					expected_num_stages + 1     )

														lowerCAmelCase__     ,      lowerCAmelCase__      =			self.model_tester.prepare_config_and_inputs_for_common()

														for model_class in self.all_model_classes:
																					lowerCAmelCase__      =			True
																					check_hidden_states_output(lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_     )

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

																					check_hidden_states_output(lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_     )







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

														for model_class in self.all_model_classes:
																					with self.subTest(model_class.__name__     ):
																												lowerCAmelCase__      =			self._prepare_for_class(lowerCamelCase_      ,					lowerCamelCase_     )
																												lowerCAmelCase__      =			model_class(lowerCamelCase_     )

																												@jax.jit
																												def model_jitted(lowerCamelCase_      ,					**lowerCamelCase_     ):
																																			return model(pixel_values=lowerCamelCase_      ,					**lowerCamelCase_     )

																												with self.subTest('''JIT Enabled'''     ):
																																			lowerCAmelCase__      =			model_jitted(**lowerCamelCase_     ).to_tuple()

																												with self.subTest('''JIT Disabled'''     ):
																																			with jax.disable_jit():
																																										lowerCAmelCase__      =			model_jitted(**lowerCamelCase_     ).to_tuple()

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


def       _snake_case     (  )		->					str:
							lowerCAmelCase__      =			Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' )
							return image






@require_flax
class 	a__       (							unittest.TestCase						):





							'''simple docstring'''






							@cached_property
							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Optional[int]:
														return AutoImageProcessor.from_pretrained('''facebook/regnet-y-040'''     ) if is_vision_available() else None







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

														lowerCAmelCase__      =			self.default_image_processor
														lowerCAmelCase__      =			prepare_img()
														lowerCAmelCase__      =			image_processor(images=lowerCamelCase_      ,					return_tensors='''np'''     )

														lowerCAmelCase__      =			model(**lowerCamelCase_     )

														# verify the logits
														lowerCAmelCase__      =			(1, 10_00)
														self.assertEqual(outputs.logits.shape      ,					lowerCamelCase_     )

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

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



'''simple docstring'''






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


__UpperCAmelCase					       =							logging.get_logger(__name__)

__UpperCAmelCase					       =							{'''ctrl''': '''https://huggingface.co/ctrl/resolve/main/config.json'''}




class 	a__       (							a__						):





							'''simple docstring'''






							lowercase__    :     Dict					=   "ctrl"
							lowercase__    :     str					=   ["past_key_values"]
							lowercase__    :     Optional[int]					=   {
							    "max_position_embeddings": "n_positions",
							    "hidden_size": "n_embd",
							    "num_attention_heads": "n_head",
							    "num_hidden_layers": "n_layer",
							}







							def __init__(	self      ,					lowerCamelCase_=24_65_34      ,					lowerCamelCase_=2_56      ,					lowerCamelCase_=12_80      ,					lowerCamelCase_=81_92      ,					lowerCamelCase_=48      ,					lowerCamelCase_=16      ,					lowerCamelCase_=0.1      ,					lowerCamelCase_=0.1      ,					lowerCamelCase_=1e-6      ,					lowerCamelCase_=0.02      ,					lowerCamelCase_=True      ,					**lowerCamelCase_      ,					) ->							Union[str, Any]:
														lowerCAmelCase__      =			vocab_size
														lowerCAmelCase__      =			n_positions
														lowerCAmelCase__      =			n_embd
														lowerCAmelCase__      =			n_layer
														lowerCAmelCase__      =			n_head
														lowerCAmelCase__      =			dff
														lowerCAmelCase__      =			resid_pdrop
														lowerCAmelCase__      =			embd_pdrop
														lowerCAmelCase__      =			layer_norm_epsilon
														lowerCAmelCase__      =			initializer_range

														lowerCAmelCase__      =			use_cache

														super().__init__(**lowerCamelCase_     )