




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     snake_case__							(unittest.TestCase   ):
				"""simple docstring"""



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





				def 							__UpperCAmelCase							(      self				:     List[str]							)					->     Dict:
							a    =			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

							a    =			self.get_config()

							return config, pixel_values





				def 							__UpperCAmelCase							(      self				:     int							)					->     Tuple:
							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				:     List[str]					,      __lowerCamelCase				:     Union[str, Any]					,      __lowerCamelCase				:     Any							)					->     Dict:
							a    =			FlaxRegNetModel(config=__lowerCamelCase							)
							a    =			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 							__UpperCAmelCase							(      self				:     Tuple					,      __lowerCamelCase				:     Tuple					,      __lowerCamelCase				:     List[Any]							)					->     Optional[int]:
							a    =			self.num_labels
							a    =			FlaxRegNetForImageClassification(config=__lowerCamelCase							)
							a    =			model(__lowerCamelCase							)
							self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.num_labels)							)





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




@require_flax
class     snake_case__							(_UpperCamelCase    ,       unittest.TestCase   ):
				"""simple docstring"""



				SCREAMING_SNAKE_CASE_       :							Tuple							 =					(FlaxRegNetModel, FlaxRegNetForImageClassification) if is_flax_available() else ()

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





				def 							__UpperCAmelCase							(      self				:     Any							)					->     None:
							a    =			FlaxRegNetModelTester(self							)
							a    =			ConfigTester(self					,      config_class=__lowerCamelCase					,      has_text_modality=__lowerCamelCase							)





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





				def 							__UpperCAmelCase							(      self				:     Tuple							)					->     List[Any]:
							a    =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*__lowerCamelCase							)





				def 							__UpperCAmelCase							(      self				:     List[Any]							)					->     Tuple:
							a    =			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 							__UpperCAmelCase							(      self				:     Optional[Any]							)					->     Tuple:
							pass





				@unittest.skip(reason="RegNet does not support input and output embeddings"							)
				def 							__UpperCAmelCase							(      self				:     Tuple							)					->     Dict:
							pass





				def 							__UpperCAmelCase							(      self				:     Dict							)					->     Optional[int]:
							a    ,		a    =			self.model_tester.prepare_config_and_inputs_for_common()

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

										a    =			["pixel_values"]
										self.assertListEqual(arg_names[:1]					,      __lowerCamelCase							)





				def 							__UpperCAmelCase							(      self				:     Dict							)					->     Tuple:
							def check_hidden_states_output(__lowerCamelCase				:     Dict					,      __lowerCamelCase				:     List[str]					,      __lowerCamelCase				:     List[str]							):
										a    =			model_class(__lowerCamelCase							)

										a    =			model(**self._prepare_for_class(__lowerCamelCase					,      __lowerCamelCase							)							)

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

										a    =			self.model_tester.num_stages
										self.assertEqual(len(__lowerCamelCase							)					,      expected_num_stages + 1							)

							a    ,		a    =			self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
										a    =			True
										check_hidden_states_output(__lowerCamelCase					,      __lowerCamelCase					,      __lowerCamelCase							)

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

										check_hidden_states_output(__lowerCamelCase					,      __lowerCamelCase					,      __lowerCamelCase							)





				def 							__UpperCAmelCase							(      self				:     Dict							)					->     str:
							a    ,		a    =			self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
										with self.subTest(model_class.__name__							):
													a    =			self._prepare_for_class(__lowerCamelCase					,      __lowerCamelCase							)
													a    =			model_class(__lowerCamelCase							)

													@jax.jit
													def model_jitted(__lowerCamelCase				:     List[str]					,      **__lowerCamelCase				:     List[Any]							):
																return model(pixel_values=__lowerCamelCase					,      **__lowerCamelCase							)

													with self.subTest("JIT Enabled"							):
																a    =			model_jitted(**__lowerCamelCase							).to_tuple()

													with self.subTest("JIT Disabled"							):
																with jax.disable_jit():
																			a    =			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 			__magic_name__				( ):






			'''simple docstring'''







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






@require_flax
class     snake_case__							(unittest.TestCase   ):
				"""simple docstring"""



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





				@slow
				def 							__UpperCAmelCase							(      self				:     List[Any]							)					->     List[str]:
							a    =			FlaxRegNetForImageClassification.from_pretrained("facebook/regnet-y-040"							)

							a    =			self.default_image_processor
							a    =			prepare_img()
							a    =			image_processor(images=__lowerCamelCase					,      return_tensors="np"							)

							a    =			model(**__lowerCamelCase							)

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

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

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












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


__lowerCAmelCase		:				Optional[Any]       			=		logging.get_logger(__name__)

__lowerCAmelCase		:				str       			=		{'openai-gpt': 'https://huggingface.co/openai-gpt/resolve/main/config.json'}




class     snake_case__							(_UpperCamelCase   ):
				"""simple docstring"""



				SCREAMING_SNAKE_CASE_       :							Dict							 =					"""openai-gpt"""
				SCREAMING_SNAKE_CASE_       :							Tuple							 =					{
				    """max_position_embeddings""": """n_positions""",
				    """hidden_size""": """n_embd""",
				    """num_attention_heads""": """n_head""",
				    """num_hidden_layers""": """n_layer""",
				}





				def __init__(      self				:     str					,      __lowerCamelCase				:     List[str]=4_04_78					,      __lowerCamelCase				:     List[Any]=5_12					,      __lowerCamelCase				:     List[str]=7_68					,      __lowerCamelCase				:     List[str]=12					,      __lowerCamelCase				:     Optional[Any]=12					,      __lowerCamelCase				:     str="gelu"					,      __lowerCamelCase				:     Optional[int]=0.1					,      __lowerCamelCase				:     Any=0.1					,      __lowerCamelCase				:     Dict=0.1					,      __lowerCamelCase				:     Any=1e-5					,      __lowerCamelCase				:     Optional[int]=0.02					,      __lowerCamelCase				:     Optional[int]="cls_index"					,      __lowerCamelCase				:     List[Any]=True					,      __lowerCamelCase				:     Optional[int]=None					,      __lowerCamelCase				:     Optional[Any]=True					,      __lowerCamelCase				:     Tuple=0.1					,      **__lowerCamelCase				:     Union[str, Any]					,      )					->     List[str]:
							a    =			vocab_size
							a    =			n_positions
							a    =			n_embd
							a    =			n_layer
							a    =			n_head
							a    =			afn
							a    =			resid_pdrop
							a    =			embd_pdrop
							a    =			attn_pdrop
							a    =			layer_norm_epsilon
							a    =			initializer_range
							a    =			summary_type
							a    =			summary_use_proj
							a    =			summary_activation
							a    =			summary_first_dropout
							a    =			summary_proj_to_labels
							super().__init__(**__lowerCamelCase							)






