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




		def __init__(  self					:Union[str, Any]			,    _SCREAMING_SNAKE_CASE					:Tuple			,    _SCREAMING_SNAKE_CASE					:List[Any]=3			,    _SCREAMING_SNAKE_CASE					:List[Any]=3_2			,    _SCREAMING_SNAKE_CASE					:List[Any]=3			,    _SCREAMING_SNAKE_CASE					:List[Any]=1_0			,    _SCREAMING_SNAKE_CASE					:Dict=[1_0, 2_0, 3_0, 4_0]			,    _SCREAMING_SNAKE_CASE					:List[Any]=[1, 1, 2, 1]			,    _SCREAMING_SNAKE_CASE					:str=True			,    _SCREAMING_SNAKE_CASE					:Optional[Any]=True			,    _SCREAMING_SNAKE_CASE					:Dict="relu"			,    _SCREAMING_SNAKE_CASE					:List[str]=3			,    _SCREAMING_SNAKE_CASE					:Optional[int]=None			,    )					->			Dict:






								'''simple docstring'''






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




		def 	_snake_case	(  self					:List[str])					->			int:






								'''simple docstring'''






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

								UpperCamelCase_     :	int					     =     self.get_config()

								return config, pixel_values




		def 	_snake_case	(  self					:Tuple)					->			Optional[int]:






								'''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					:str			,    _SCREAMING_SNAKE_CASE					:Any			,    _SCREAMING_SNAKE_CASE					:Any)					->			Tuple:






								'''simple docstring'''






								UpperCamelCase_     :	str					     =     FlaxResNetModel(config=_SCREAMING_SNAKE_CASE)
								UpperCamelCase_     :	Any					     =     model(_SCREAMING_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 // 3_2, self.image_size // 3_2)			,    )




		def 	_snake_case	(  self					:List[str]			,    _SCREAMING_SNAKE_CASE					:int			,    _SCREAMING_SNAKE_CASE					:Any)					->			List[str]:






								'''simple docstring'''






								UpperCamelCase_     :	Optional[Any]					     =     self.num_labels
								UpperCamelCase_     :	List[Any]					     =     FlaxResNetForImageClassification(config=_SCREAMING_SNAKE_CASE)
								UpperCamelCase_     :	Union[str, Any]					     =     model(_SCREAMING_SNAKE_CASE)
								self.parent.assertEqual(result.logits.shape			,    (self.batch_size, self.num_labels))





		def 	_snake_case	(  self					:Dict)					->			Any:






								'''simple docstring'''






								UpperCamelCase_     :	int					     =     self.prepare_config_and_inputs()
								UpperCamelCase_     ,UpperCamelCase_     :	Union[str, Any]					     =     config_and_inputs
								UpperCamelCase_     :	Tuple					     =     {"""pixel_values""": pixel_values}
								return config, inputs_dict







@require_flax
class     __snake_case			(    _a					,			unittest.TestCase	):
		a_          =					(FlaxResNetModel, FlaxResNetForImageClassification) if is_flax_available() else ()

		a_          =					False
		a_          =					False
		a_          =					False




		def 	_snake_case	(  self					:List[Any])					->			None:






								'''simple docstring'''






								UpperCamelCase_     :	Union[str, Any]					     =     FlaxResNetModelTester(self)
								UpperCamelCase_     :	Tuple					     =     ConfigTester(self			,    config_class=_SCREAMING_SNAKE_CASE			,    has_text_modality=_SCREAMING_SNAKE_CASE)




		def 	_snake_case	(  self					:Union[str, Any])					->			List[Any]:






								'''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					:Optional[int])					->			List[str]:






								'''simple docstring'''






								return




		def 	_snake_case	(  self					:List[str])					->			Dict:






								'''simple docstring'''






								UpperCamelCase_     :	Optional[Any]					     =     self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE)




		def 	_snake_case	(  self					:Optional[int])					->			Dict:






								'''simple docstring'''






								UpperCamelCase_     :	Union[str, Any]					     =     self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_classification(*_SCREAMING_SNAKE_CASE)




		@unittest.skip(reason="""ResNet does not use inputs_embeds""")
		def 	_snake_case	(  self					:Union[str, Any])					->			Tuple:






								'''simple docstring'''






								pass




		@unittest.skip(reason="""ResNet does not support input and output embeddings""")
		def 	_snake_case	(  self					:Tuple)					->			str:






								'''simple docstring'''






								pass




		def 	_snake_case	(  self					:Any)					->			List[Any]:






								'''simple docstring'''






								UpperCamelCase_     ,UpperCamelCase_     :	List[Any]					     =     self.model_tester.prepare_config_and_inputs_for_common()

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

														UpperCamelCase_     :	str					     =     ["""pixel_values"""]
														self.assertListEqual(arg_names[:1]			,    _SCREAMING_SNAKE_CASE)




		def 	_snake_case	(  self					:Union[str, Any])					->			List[str]:






								'''simple docstring'''






								def check_hidden_states_output(_SCREAMING_SNAKE_CASE					:Union[str, Any]			,    _SCREAMING_SNAKE_CASE					:int			,    _SCREAMING_SNAKE_CASE					:List[Any]):
														UpperCamelCase_     :	Optional[Any]					     =     model_class(_SCREAMING_SNAKE_CASE)

														UpperCamelCase_     :	Optional[int]					     =     model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE			,    _SCREAMING_SNAKE_CASE))

														UpperCamelCase_     :	Optional[Any]					     =     outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

														UpperCamelCase_     :	Optional[Any]					     =     self.model_tester.num_stages
														self.assertEqual(len(_SCREAMING_SNAKE_CASE)			,    expected_num_stages + 1)




		@unittest.skip(reason="""ResNet does not use feedforward chunking""")
		def 	_snake_case	(  self					:Union[str, Any])					->			int:






								'''simple docstring'''






								pass





		def 	_snake_case	(  self					:Any)					->			str:






								'''simple docstring'''






								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_     :	Optional[int]					     =     self._prepare_for_class(_SCREAMING_SNAKE_CASE			,    _SCREAMING_SNAKE_CASE)
																				UpperCamelCase_     :	Union[str, Any]					     =     model_class(_SCREAMING_SNAKE_CASE)

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

																				with self.subTest("""JIT Enabled"""):
																										UpperCamelCase_     :	Tuple					     =     model_jitted(**_SCREAMING_SNAKE_CASE).to_tuple()

																				with self.subTest("""JIT Disabled"""):
																										with jax.disable_jit():
																																UpperCamelCase_     :	Optional[int]					     =     model_jitted(**_SCREAMING_SNAKE_CASE).to_tuple()

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







def 					__magic_name__(       ):
						UpperCamelCase_     :	int					     =     Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""	)
						return image







@require_flax
class     __snake_case			(    unittest.TestCase	):




		@cached_property
		def 	_snake_case	(  self					:Optional[int])					->			Optional[int]:






								'''simple docstring'''






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





		@slow
		def 	_snake_case	(  self					:Optional[int])					->			int:






								'''simple docstring'''






								UpperCamelCase_     :	Dict					     =     FlaxResNetForImageClassification.from_pretrained("""microsoft/resnet-50""")

								UpperCamelCase_     :	Any					     =     self.default_image_processor
								UpperCamelCase_     :	Tuple					     =     prepare_img()
								UpperCamelCase_     :	Tuple					     =     image_processor(images=_SCREAMING_SNAKE_CASE			,    return_tensors="""np""")

								UpperCamelCase_     :	str					     =     model(**_SCREAMING_SNAKE_CASE)

								# verify the logits
								UpperCamelCase_     :	str					     =     (1, 1_0_0_0)
								self.assertEqual(outputs.logits.shape			,    _SCREAMING_SNAKE_CASE)

								UpperCamelCase_     :	Optional[Any]					     =     jnp.array([-11.1069, -9.7877, -8.3777])

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






'''simple docstring'''


from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


__SCREAMING_SNAKE_CASE						=      {
    "configuration_markuplm": ["MARKUPLM_PRETRAINED_CONFIG_ARCHIVE_MAP", "MarkupLMConfig"],
    "feature_extraction_markuplm": ["MarkupLMFeatureExtractor"],
    "processing_markuplm": ["MarkupLMProcessor"],
    "tokenization_markuplm": ["MarkupLMTokenizer"],
}

try:
	if not is_tokenizers_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	__SCREAMING_SNAKE_CASE						=      ["MarkupLMTokenizerFast"]

try:
	if not is_torch_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	__SCREAMING_SNAKE_CASE						=      [
	    "MARKUPLM_PRETRAINED_MODEL_ARCHIVE_LIST",
	    "MarkupLMForQuestionAnswering",
	    "MarkupLMForSequenceClassification",
	    "MarkupLMForTokenClassification",
	    "MarkupLMModel",
	    "MarkupLMPreTrainedModel",
	]


if TYPE_CHECKING:
	from .configuration_markuplm import MARKUPLM_PRETRAINED_CONFIG_ARCHIVE_MAP, MarkupLMConfig
	from .feature_extraction_markuplm import MarkupLMFeatureExtractor
	from .processing_markuplm import MarkupLMProcessor
	from .tokenization_markuplm import MarkupLMTokenizer

	try:
		if not is_tokenizers_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .tokenization_markuplm_fast import MarkupLMTokenizerFast

	try:
		if not is_torch_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_markuplm import (
		    MARKUPLM_PRETRAINED_MODEL_ARCHIVE_LIST,
		    MarkupLMForQuestionAnswering,
		    MarkupLMForSequenceClassification,
		    MarkupLMForTokenClassification,
		    MarkupLMModel,
		    MarkupLMPreTrainedModel,
		)


else:
	import sys

	__SCREAMING_SNAKE_CASE						=      _LazyModule(__name__, globals()["__file__"], _import_structure)




