


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      snake_case__	(   unittest.TestCase	):
				def __init__(	self  ,   _UpperCAmelCase  ,   _UpperCAmelCase=3  ,   _UpperCAmelCase=32  ,   _UpperCAmelCase=3  ,   _UpperCAmelCase=10  ,   _UpperCAmelCase=[10, 20, 30, 40]  ,   _UpperCAmelCase=[1, 1, 2, 1]  ,   _UpperCAmelCase=True  ,   _UpperCAmelCase=True  ,   _UpperCAmelCase="relu"  ,   _UpperCAmelCase=3  ,   _UpperCAmelCase=None  ,   ):




										"""simple docstring"""




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




										"""simple docstring"""




										__magic_name__					:				str				=   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

										__magic_name__					:				Optional[int]				=   self.get_config()

										return config, pixel_values
				def 			UpperCamelCase_    (	self   ):




										"""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 			UpperCamelCase_    (	self  ,   _UpperCAmelCase  ,   _UpperCAmelCase   ):




										"""simple docstring"""




										__magic_name__					:				List[str]				=   FlaxResNetModel(config=_UpperCAmelCase   )
										__magic_name__					:				Optional[Any]				=   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 // 32, self.image_size // 32)  ,   )
				def 			UpperCamelCase_    (	self  ,   _UpperCAmelCase  ,   _UpperCAmelCase   ):




										"""simple docstring"""




										__magic_name__					:				Tuple				=   self.num_labels
										__magic_name__					:				List[str]				=   FlaxResNetForImageClassification(config=_UpperCAmelCase   )
										__magic_name__					:				Dict				=   model(_UpperCAmelCase   )
										self.parent.assertEqual(result.logits.shape  ,   (self.batch_size, self.num_labels)   )





				def 			UpperCamelCase_    (	self   ):




										"""simple docstring"""




										__magic_name__					:				Dict				=   self.prepare_config_and_inputs()
										__magic_name__		,			__magic_name__					:				int				=   config_and_inputs
										__magic_name__					:				List[str]				=   {'pixel_values': pixel_values}
										return config, inputs_dict






@require_flax
class      snake_case__	(   _A    ,							unittest.TestCase	):
				__lowerCAmelCase		=   (FlaxResNetModel, FlaxResNetForImageClassification) if is_flax_available() else ()

				__lowerCAmelCase		=   False
				__lowerCAmelCase		=   False
				__lowerCAmelCase		=   False
				def 			UpperCamelCase_    (	self   ):




										"""simple docstring"""




										__magic_name__					:				List[Any]				=   FlaxResNetModelTester(self   )
										__magic_name__					:				int				=   ConfigTester(self  ,   config_class=_UpperCAmelCase  ,   has_text_modality=_UpperCAmelCase   )
				def 			UpperCamelCase_    (	self   ):




										"""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 			UpperCamelCase_    (	self   ):




										"""simple docstring"""




										return
				def 			UpperCamelCase_    (	self   ):




										"""simple docstring"""




										__magic_name__					:				Dict				=   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*_UpperCAmelCase   )
				def 			UpperCamelCase_    (	self   ):




										"""simple docstring"""




										__magic_name__					:				str				=   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*_UpperCAmelCase   )
				@unittest.skip(reason='ResNet does not use inputs_embeds'   )
				def 			UpperCamelCase_    (	self   ):




										"""simple docstring"""




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




										"""simple docstring"""




										pass
				def 			UpperCamelCase_    (	self   ):




										"""simple docstring"""




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

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

																__magic_name__					:				Optional[int]				=   ['pixel_values']
																self.assertListEqual(arg_names[:1]  ,   _UpperCAmelCase   )
				def 			UpperCamelCase_    (	self   ):




										"""simple docstring"""




										def check_hidden_states_output(_UpperCAmelCase  ,   _UpperCAmelCase  ,   _UpperCAmelCase   ):
																__magic_name__					:				str				=   model_class(_UpperCAmelCase   )

																__magic_name__					:				Tuple				=   model(**self._prepare_for_class(_UpperCAmelCase  ,   _UpperCAmelCase   )   )

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

																__magic_name__					:				Union[str, Any]				=   self.model_tester.num_stages
																self.assertEqual(len(_UpperCAmelCase   )  ,   expected_num_stages + 1   )
				@unittest.skip(reason='ResNet does not use feedforward chunking'   )
				def 			UpperCamelCase_    (	self   ):




										"""simple docstring"""




										pass





				def 			UpperCamelCase_    (	self   ):




										"""simple docstring"""




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

										for model_class in self.all_model_classes:
																with self.subTest(model_class.__name__   ):
																						__magic_name__					:				Optional[int]				=   self._prepare_for_class(_UpperCAmelCase  ,   _UpperCAmelCase   )
																						__magic_name__					:				Union[str, Any]				=   model_class(_UpperCAmelCase   )

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

																						with self.subTest('JIT Enabled'   ):
																												__magic_name__					:				Tuple				=   model_jitted(**_UpperCAmelCase   ).to_tuple()

																						with self.subTest('JIT Disabled'   ):
																												with jax.disable_jit():
																																		__magic_name__					:				Optional[int]				=   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 			__SCREAMING_SNAKE_CASE							(					)							->	List[Any]:
						__magic_name__					:				Tuple				=   Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'					)
						return image



@require_flax
class      snake_case__	(   unittest.TestCase	):
				@cached_property
				def 			UpperCamelCase_    (	self   ):




										"""simple docstring"""




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





				@slow
				def 			UpperCamelCase_    (	self   ):




										"""simple docstring"""




										__magic_name__					:				Any				=   FlaxResNetForImageClassification.from_pretrained('microsoft/resnet-50'   )

										__magic_name__					:				Optional[int]				=   self.default_image_processor
										__magic_name__					:				Union[str, Any]				=   prepare_img()
										__magic_name__					:				Dict				=   image_processor(images=_UpperCAmelCase  ,   return_tensors='np'   )

										__magic_name__					:				List[Any]				=   model(**_UpperCAmelCase   )

										# verify the logits
										__magic_name__					:				int				=   (1, 1000)
										self.assertEqual(outputs.logits.shape  ,   _UpperCAmelCase   )

										__magic_name__					:				Union[str, Any]				=   jnp.array([-11.1069, -9.7877, -8.3777]   )

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






from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


snake_case__  				=			{'configuration_qdqbert': ['QDQBERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'QDQBertConfig']}

try:
			if not is_torch_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			snake_case__  				=			[
			    'QDQBERT_PRETRAINED_MODEL_ARCHIVE_LIST',
			    'QDQBertForMaskedLM',
			    'QDQBertForMultipleChoice',
			    'QDQBertForNextSentencePrediction',
			    'QDQBertForQuestionAnswering',
			    'QDQBertForSequenceClassification',
			    'QDQBertForTokenClassification',
			    'QDQBertLayer',
			    'QDQBertLMHeadModel',
			    'QDQBertModel',
			    'QDQBertPreTrainedModel',
			    'load_tf_weights_in_qdqbert',
			]


if TYPE_CHECKING:
			from .configuration_qdqbert import QDQBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, QDQBertConfig

			try:
						if not is_torch_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_qdqbert import (
						    QDQBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
						    QDQBertForMaskedLM,
						    QDQBertForMultipleChoice,
						    QDQBertForNextSentencePrediction,
						    QDQBertForQuestionAnswering,
						    QDQBertForSequenceClassification,
						    QDQBertForTokenClassification,
						    QDQBertLayer,
						    QDQBertLMHeadModel,
						    QDQBertModel,
						    QDQBertPreTrainedModel,
						    load_tf_weights_in_qdqbert,
						)


else:
			import sys

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


