



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




				def __init__(							self						,    _snake_case						,    _snake_case=3						,    _snake_case=32						,    _snake_case=3						,    _snake_case=10						,    _snake_case=[10, 20, 30, 40]						,    _snake_case=[1, 1, 2, 1]						,    _snake_case=True						,    _snake_case=True						,    _snake_case="relu"						,    _snake_case=3						,    _snake_case=None						,    ):



									"""simple docstring"""





									_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(_snake_case		)




				def 							snake_case       (							self		):



									"""simple docstring"""





									_lowerCAmelCase      =   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

									_lowerCAmelCase      =   self.get_config()

									return config, pixel_values




				def 							snake_case       (							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 							snake_case       (							self						,    _snake_case						,    _snake_case		):



									"""simple docstring"""





									_lowerCAmelCase      =   FlaxResNetModel(config=_snake_case		)
									_lowerCAmelCase      =   model(_snake_case		)

									# 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 							snake_case       (							self						,    _snake_case						,    _snake_case		):



									"""simple docstring"""





									_lowerCAmelCase      =   self.num_labels
									_lowerCAmelCase      =   FlaxResNetForImageClassification(config=_snake_case		)
									_lowerCAmelCase      =   model(_snake_case		)
									self.parent.assertEqual(result.logits.shape						,    (self.batch_size, self.num_labels)		)




				def 							snake_case       (							self		):



									"""simple docstring"""





									_lowerCAmelCase      =   self.prepare_config_and_inputs()
									_lowerCAmelCase     ,     _lowerCAmelCase      =   config_and_inputs
									_lowerCAmelCase      =   {"""pixel_values""": pixel_values}
									return config, inputs_dict





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

				__lowerCamelCase			=    False
				__lowerCamelCase			=    False
				__lowerCamelCase			=    False




				def 							snake_case       (							self		):



									"""simple docstring"""





									_lowerCAmelCase      =   FlaxResNetModelTester(self		)
									_lowerCAmelCase      =   ConfigTester(self						,    config_class=_snake_case						,    has_text_modality=_snake_case		)




				def 							snake_case       (							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 							snake_case       (							self		):



									"""simple docstring"""





									return




				def 							snake_case       (							self		):



									"""simple docstring"""





									_lowerCAmelCase      =   self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_snake_case		)




				def 							snake_case       (							self		):



									"""simple docstring"""





									_lowerCAmelCase      =   self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*_snake_case		)




				@unittest.skip(reason="""ResNet does not use inputs_embeds"""		)
				def 							snake_case       (							self		):



									"""simple docstring"""





									pass




				@unittest.skip(reason="""ResNet does not support input and output embeddings"""		)
				def 							snake_case       (							self		):



									"""simple docstring"""





									pass




				def 							snake_case       (							self		):



									"""simple docstring"""





									_lowerCAmelCase     ,     _lowerCAmelCase      =   self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
														_lowerCAmelCase      =   model_class(_snake_case		)
														_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]						,    _snake_case		)




				def 							snake_case       (							self		):



									"""simple docstring"""





									def check_hidden_states_output(_snake_case						,    _snake_case						,    _snake_case		):
														_lowerCAmelCase      =   model_class(_snake_case		)

														_lowerCAmelCase      =   model(**self._prepare_for_class(_snake_case						,    _snake_case		)		)

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

														_lowerCAmelCase      =   self.model_tester.num_stages
														self.assertEqual(len(_snake_case		)						,    expected_num_stages + 1		)




				@unittest.skip(reason="""ResNet does not use feedforward chunking"""		)
				def 							snake_case       (							self		):



									"""simple docstring"""





									pass




				def 							snake_case       (							self		):



									"""simple docstring"""





									_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(_snake_case						,    _snake_case		)
																			_lowerCAmelCase      =   model_class(_snake_case		)

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

																			with self.subTest("""JIT Enabled"""		):
																								_lowerCAmelCase      =   model_jitted(**_snake_case		).to_tuple()

																			with self.subTest("""JIT Disabled"""		):
																								with jax.disable_jit():
																													_lowerCAmelCase      =   model_jitted(**_snake_case		).to_tuple()

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


def 		_UpperCAmelCase    (				):





					"""simple docstring"""

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





@require_flax
class  __lowerCAmelCase			(					unittest.TestCase	):




				@cached_property
				def 							snake_case       (							self		):



									"""simple docstring"""





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




				@slow
				def 							snake_case       (							self		):



									"""simple docstring"""





									_lowerCAmelCase      =   FlaxResNetForImageClassification.from_pretrained("""microsoft/resnet-50"""		)

									_lowerCAmelCase      =   self.default_image_processor
									_lowerCAmelCase      =   prepare_img()
									_lowerCAmelCase      =   image_processor(images=_snake_case						,    return_tensors="""np"""		)

									_lowerCAmelCase      =   model(**_snake_case		)

									# verify the logits
									_lowerCAmelCase      =   (1, 1000)
									self.assertEqual(outputs.logits.shape						,    _snake_case		)

									_lowerCAmelCase      =   jnp.array([-11.1069, -9.7877, -8.3777]		)

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








import os

from packaging import version

from .. import __version__
from .accelerate_utils import apply_forward_hook
from .constants import (
    CONFIG_NAME,
    DEPRECATED_REVISION_ARGS,
    DIFFUSERS_CACHE,
    DIFFUSERS_DYNAMIC_MODULE_NAME,
    FLAX_WEIGHTS_NAME,
    HF_MODULES_CACHE,
    HUGGINGFACE_CO_RESOLVE_ENDPOINT,
    ONNX_EXTERNAL_WEIGHTS_NAME,
    ONNX_WEIGHTS_NAME,
    SAFETENSORS_WEIGHTS_NAME,
    TEXT_ENCODER_ATTN_MODULE,
    WEIGHTS_NAME,
)
from .deprecation_utils import deprecate
from .doc_utils import replace_example_docstring
from .dynamic_modules_utils import get_class_from_dynamic_module
from .hub_utils import (
    HF_HUB_OFFLINE,
    _add_variant,
    _get_model_file,
    extract_commit_hash,
    http_user_agent,
)
from .import_utils import (
    BACKENDS_MAPPING,
    ENV_VARS_TRUE_AND_AUTO_VALUES,
    ENV_VARS_TRUE_VALUES,
    USE_JAX,
    USE_TF,
    USE_TORCH,
    DummyObject,
    OptionalDependencyNotAvailable,
    is_accelerate_available,
    is_accelerate_version,
    is_bsa_available,
    is_flax_available,
    is_ftfy_available,
    is_inflect_available,
    is_invisible_watermark_available,
    is_k_diffusion_available,
    is_k_diffusion_version,
    is_librosa_available,
    is_note_seq_available,
    is_omegaconf_available,
    is_onnx_available,
    is_safetensors_available,
    is_scipy_available,
    is_tensorboard_available,
    is_tf_available,
    is_torch_available,
    is_torch_version,
    is_torchsde_available,
    is_transformers_available,
    is_transformers_version,
    is_unidecode_available,
    is_wandb_available,
    is_xformers_available,
    requires_backends,
)
from .logging import get_logger
from .outputs import BaseOutput
from .pil_utils import PIL_INTERPOLATION, numpy_to_pil, pt_to_pil
from .torch_utils import is_compiled_module, randn_tensor


if is_torch_available():
	from .testing_utils import (
	    floats_tensor,
	    load_hf_numpy,
	    load_image,
	    load_numpy,
	    load_pt,
	    nightly,
	    parse_flag_from_env,
	    print_tensor_test,
	    require_torch_a,
	    require_torch_gpu,
	    skip_mps,
	    slow,
	    torch_all_close,
	    torch_device,
	)
	from .torch_utils import maybe_allow_in_graph

from .testing_utils import export_to_gif, export_to_video


A__     				=	get_logger(__name__)


def 		_UpperCAmelCase    (				snake_case      ):





					"""simple docstring"""

					if version.parse(snake_case      ) < version.parse(snake_case      ):
										if "dev" in min_version:
															_lowerCAmelCase      =   (
															    """This example requires a source install from HuggingFace diffusers (see """
															    """`https://huggingface.co/docs/diffusers/installation#install-from-source`),"""
															)
										else:
															_lowerCAmelCase      =   F'This example requires a minimum version of {min_version},'
										error_message += F' but the version found is {__version__}.\n'
										raise ImportError(snake_case      )



