




'''simple docstring'''




import inspect
import unittest

from transformers import ResNetConfig, 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.resnet.modeling_flax_resnet import FlaxResNetForImageClassification, FlaxResNetModel

if is_vision_available():
							from PIL import Image

							from transformers import AutoImageProcessor






class     UpperCamelCase_     (unittest.TestCase     ):







						"""simple docstring"""


						def __init__(     self      :       Dict							, _lowerCamelCase      :       int							, _lowerCamelCase      :       List[str]=3							, _lowerCamelCase      :       List[Any]=32							, _lowerCamelCase      :       Any=3							, _lowerCamelCase      :       Optional[int]=10							, _lowerCamelCase      :       Tuple=[10, 20, 30, 40]							, _lowerCamelCase      :       Optional[int]=[1, 1, 2, 1]							, _lowerCamelCase      :       Dict=True							, _lowerCamelCase      :       List[str]=True							, _lowerCamelCase      :       str="relu"							, _lowerCamelCase      :       Optional[int]=3							, _lowerCamelCase      :       Any=None							, ):





											"""simple docstring"""







											A_ :     Union[str, Any] =							parent
											A_ :     Dict =							batch_size
											A_ :     int =							image_size
											A_ :     Any =							num_channels
											A_ :     Union[str, Any] =							embeddings_size
											A_ :     str =							hidden_sizes
											A_ :     Tuple =							depths
											A_ :     int =							is_training
											A_ :     List[str] =							use_labels
											A_ :     Union[str, Any] =							hidden_act
											A_ :     str =							num_labels
											A_ :     Optional[int] =							scope
											A_ :     Tuple =							len(_lowerCamelCase    )







						def       _a    (     self      :       Union[str, Any]    ):





											"""simple docstring"""







											A_ :     Optional[int] =							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

											A_ :     Optional[Any] =							self.get_config()

											return config, pixel_values







						def       _a    (     self      :       List[Any]    ):





											"""simple docstring"""







											return ResNetConfig(
											    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       _a    (     self      :       Tuple							, _lowerCamelCase      :       Optional[int]							, _lowerCamelCase      :       Optional[Any]    ):





											"""simple docstring"""







											A_ :     Union[str, Any] =							FlaxResNetModel(config=_lowerCamelCase    )
											A_ :     Dict =							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       _a    (     self      :       Union[str, Any]							, _lowerCamelCase      :       int							, _lowerCamelCase      :       Optional[int]    ):





											"""simple docstring"""







											A_ :     Optional[int] =							self.num_labels
											A_ :     Any =							FlaxResNetForImageClassification(config=_lowerCamelCase    )
											A_ :     int =							model(_lowerCamelCase    )
											self.parent.assertEqual(result.logits.shape							, (self.batch_size, self.num_labels)    )







						def       _a    (     self      :       Tuple    ):





											"""simple docstring"""







											A_ :     Dict =							self.prepare_config_and_inputs()
											A_ ,A_ :     Optional[Any] =							config_and_inputs
											A_ :     Dict =							{'''pixel_values''': pixel_values}
											return config, inputs_dict






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







						"""simple docstring"""


						_lowerCAmelCase    						=					(FlaxResNetModel, FlaxResNetForImageClassification) if is_flax_available() else ()

						_lowerCAmelCase    						=					False
						_lowerCAmelCase    						=					False
						_lowerCAmelCase    						=					False







						def       _a    (     self      :       Any    ):





											"""simple docstring"""







											A_ :     Any =							FlaxResNetModelTester(self    )
											A_ :     int =							ConfigTester(self							, config_class=_lowerCamelCase							, has_text_modality=_lowerCamelCase    )







						def       _a    (     self      :       Dict    ):





											"""simple docstring"""







											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       _a    (     self      :       int    ):





											"""simple docstring"""







											return







						def       _a    (     self      :       Any    ):





											"""simple docstring"""







											A_ :     Optional[int] =							self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*_lowerCamelCase    )







						def       _a    (     self      :       Union[str, Any]    ):





											"""simple docstring"""







											A_ :     Dict =							self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*_lowerCamelCase    )







						@unittest.skip(reason='''ResNet does not use inputs_embeds'''    )
						def       _a    (     self      :       Tuple    ):





											"""simple docstring"""







											pass







						@unittest.skip(reason='''ResNet does not support input and output embeddings'''    )
						def       _a    (     self      :       Any    ):





											"""simple docstring"""







											pass







						def       _a    (     self      :       List[Any]    ):





											"""simple docstring"""







											A_ ,A_ :     List[str] =							self.model_tester.prepare_config_and_inputs_for_common()

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

																A_ :     Any =							['''pixel_values''']
																self.assertListEqual(arg_names[:1]							, _lowerCamelCase    )







						def       _a    (     self      :       int    ):





											"""simple docstring"""







											def check_hidden_states_output(_lowerCamelCase      :       Tuple							, _lowerCamelCase      :       Optional[int]							, _lowerCamelCase      :       int    ):
																A_ :     List[str] =							model_class(_lowerCamelCase    )

																A_ :     Tuple =							model(**self._prepare_for_class(_lowerCamelCase							, _lowerCamelCase    )    )

																A_ :     int =							outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

																A_ :     Any =							self.model_tester.num_stages
																self.assertEqual(len(_lowerCamelCase    )							, expected_num_stages + 1    )







						@unittest.skip(reason='''ResNet does not use feedforward chunking'''    )
						def       _a    (     self      :       int    ):





											"""simple docstring"""







											pass







						def       _a    (     self      :       Optional[int]    ):





											"""simple docstring"""







											A_ ,A_ :     Any =							self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																with self.subTest(model_class.__name__    ):
																					A_ :     List[Any] =							self._prepare_for_class(_lowerCamelCase							, _lowerCamelCase    )
																					A_ :     List[str] =							model_class(_lowerCamelCase    )

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

																					with self.subTest('''JIT Enabled'''    ):
																										A_ :     List[str] =							model_jitted(**_lowerCamelCase    ).to_tuple()

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






@require_flax
class     UpperCamelCase_     (unittest.TestCase     ):







						"""simple docstring"""


						@cached_property
						def       _a    (     self      :       List[str]    ):





											"""simple docstring"""







											return AutoImageProcessor.from_pretrained('''microsoft/resnet-50'''    ) if is_vision_available() else None







						@slow
						def       _a    (     self      :       Union[str, Any]    ):





											"""simple docstring"""







											A_ :     Optional[Any] =							FlaxResNetForImageClassification.from_pretrained('''microsoft/resnet-50'''    )

											A_ :     Tuple =							self.default_image_processor
											A_ :     List[str] =							prepare_img()
											A_ :     Union[str, Any] =							image_processor(images=_lowerCamelCase							, return_tensors='''np'''    )

											A_ :     Dict =							model(**_lowerCamelCase    )

											# verify the logits
											A_ :     Optional[int] =							(1, 1000)
											self.assertEqual(outputs.logits.shape							, _lowerCamelCase    )

											A_ :     List[str] =							jnp.array([-11.10_69, -9.78_77, -8.37_77]    )

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









'''simple docstring'''




from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


snake_case__          =						{"""configuration_nat""": ["""NAT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """NatConfig"""]}


try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							snake_case__          =						[
							    """NAT_PRETRAINED_MODEL_ARCHIVE_LIST""",
							    """NatForImageClassification""",
							    """NatModel""",
							    """NatPreTrainedModel""",
							    """NatBackbone""",
							]

if TYPE_CHECKING:
							from .configuration_nat import NAT_PRETRAINED_CONFIG_ARCHIVE_MAP, NatConfig

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_nat import (
														    NAT_PRETRAINED_MODEL_ARCHIVE_LIST,
														    NatBackbone,
														    NatForImageClassification,
														    NatModel,
														    NatPreTrainedModel,
														)

else:
							import sys

							snake_case__          =						_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)



