



'''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			):





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





	def  _lowercase(  self    )			->			Union[str, Any]:
								UpperCAmelCase     :		str							     =		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

								UpperCAmelCase     :		int							     =		self.get_config()

								return config, pixel_values





	def  _lowercase(  self    )			->			Optional[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  _lowercase(  self		,		A		,		A    )			->			List[Any]:
								UpperCAmelCase     :		List[Any]							     =		FlaxRegNetModel(config=A    )
								UpperCAmelCase     :		Union[str, 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 // 32, self.image_size // 32)		,		)





	def  _lowercase(  self		,		A		,		A    )			->			Any:
								UpperCAmelCase     :		List[str]							     =		self.num_labels
								UpperCAmelCase     :		str							     =		FlaxRegNetForImageClassification(config=A    )
								UpperCAmelCase     :		Dict							     =		model(A    )
								self.parent.assertEqual(result.logits.shape		,		(self.batch_size, self.num_labels)    )






	def  _lowercase(  self    )			->			List[Any]:
								UpperCAmelCase     :		Tuple							     =		self.prepare_config_and_inputs()
								UpperCAmelCase     ,     UpperCAmelCase     :		List[str]							     =		config_and_inputs
								UpperCAmelCase     :		Tuple							     =		{"""pixel_values""": pixel_values}
								return config, inputs_dict




@require_flax
class 			UpperCamelCase_ (							lowercase_      ,     unittest.TestCase			):
	lowercase          = (FlaxRegNetModel, FlaxRegNetForImageClassification) if is_flax_available() else ()

	lowercase          = False
	lowercase          = False
	lowercase          = False





	def  _lowercase(  self    )			->			Optional[int]:
								UpperCAmelCase     :		str							     =		FlaxRegNetModelTester(self    )
								UpperCAmelCase     :		Union[str, Any]							     =		ConfigTester(self		,		config_class=A		,		has_text_modality=A    )





	def  _lowercase(  self    )			->			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  _lowercase(  self    )			->			int:
								return





	def  _lowercase(  self    )			->			Any:
								UpperCAmelCase     :		Optional[Any]							     =		self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*A    )





	def  _lowercase(  self    )			->			Tuple:
								UpperCAmelCase     :		Union[str, Any]							     =		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  _lowercase(  self    )			->			Optional[Any]:
								pass





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





	def  _lowercase(  self    )			->			Dict:
								UpperCAmelCase     ,     UpperCAmelCase     :		Any							     =		self.model_tester.prepare_config_and_inputs_for_common()

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

															UpperCAmelCase     :		Tuple							     =		["""pixel_values"""]
															self.assertListEqual(arg_names[:1]		,		A    )





	def  _lowercase(  self    )			->			List[str]:
								def check_hidden_states_output(A		,		A		,		A    ):
															UpperCAmelCase     :		List[str]							     =		model_class(A    )

															UpperCAmelCase     :		str							     =		model(**self._prepare_for_class(A		,		A    )    )

															UpperCAmelCase     :		Any							     =		outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

															UpperCAmelCase     :		Tuple							     =		self.model_tester.num_stages
															self.assertEqual(len(A    )		,		expected_num_stages + 1    )

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

								for model_class in self.all_model_classes:
															UpperCAmelCase     :		List[Any]							     =		True
															check_hidden_states_output(A		,		A		,		A    )

															# check that output_hidden_states also work using config
															del inputs_dict["output_hidden_states"]
															UpperCAmelCase     :		Tuple							     =		True

															check_hidden_states_output(A		,		A		,		A    )






	def  _lowercase(  self    )			->			Optional[int]:
								UpperCAmelCase     ,     UpperCAmelCase     :		Union[str, Any]							     =		self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
															with self.subTest(model_class.__name__    ):
																						UpperCAmelCase     :		int							     =		self._prepare_for_class(A		,		A    )
																						UpperCAmelCase     :		int							     =		model_class(A    )

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

																						with self.subTest("""JIT Enabled"""    ):
																													UpperCAmelCase     :		List[Any]							     =		model_jitted(**A    ).to_tuple()

																						with self.subTest("""JIT Disabled"""    ):
																													with jax.disable_jit():
																																				UpperCAmelCase     :		List[str]							     =		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  (					)      ->			Any:
							UpperCAmelCase     :		List[Any]							     =		Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""		)
							return image


@require_flax
class 			UpperCamelCase_ (							unittest.TestCase			):





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






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

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

								UpperCAmelCase     :		List[str]							     =		model(**A    )

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

								UpperCAmelCase     :		int							     =		jnp.array([-0.4_1_8_0, -1.5_0_5_1, -3.4_8_3_6]    )

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




import numpy as np





def 					__lowerCamelCase	( UpperCamelCase__      ):







							'''simple docstring'''






							return 1 / (1 + np.exp(-vector      ))





def 					__lowerCamelCase	( UpperCamelCase__      ):







							'''simple docstring'''






							return vector * sigmoid(UpperCamelCase__      )


if __name__ == "__main__":
					import doctest

					doctest.testmod()




