



import inspect
import unittest

from transformers import ResNetConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch
		from torch import nn

		from transformers import ResNetBackbone, ResNetForImageClassification, ResNetModel
		from transformers.models.resnet.modeling_resnet import RESNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
		from PIL import Image

		from transformers import AutoImageProcessor


class 		lowerCamelCase__       :


						def __init__(       self  :     Tuple					,				_a  :     List[str]					,				_a  :     Union[str, Any]=3					,				_a  :     List[str]=3_2					,				_a  :     Union[str, Any]=3					,				_a  :     Dict=1_0					,				_a  :     List[Any]=[1_0, 2_0, 3_0, 4_0]					,				_a  :     Optional[int]=[1, 1, 2, 1]					,				_a  :     int=True					,				_a  :     Optional[int]=True					,				_a  :     int="relu"					,				_a  :     List[str]=3					,				_a  :     List[str]=None					,				_a  :     int=["stage2", "stage3", "stage4"]					,				_a  :     Optional[Any]=[2, 3, 4]					,				):
													a__:				Optional[int] 							=parent
													a__:				str 							=batch_size
													a__:				Tuple 							=image_size
													a__:				List[str] 							=num_channels
													a__:				List[str] 							=embeddings_size
													a__:				Any 							=hidden_sizes
													a__:				Optional[int] 							=depths
													a__:				List[str] 							=is_training
													a__:				Union[str, Any] 							=use_labels
													a__:				Any 							=hidden_act
													a__:				Union[str, Any] 							=num_labels
													a__:				Any 							=scope
													a__:				int 							=len(lowerCAmelCase__	)
													a__:				Any 							=out_features
													a__:				Optional[Any] 							=out_indices


						def      _lowerCamelCase	(       self  :     Tuple	):
													a__:				Any 							=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

													a__:				Dict 							=None
													if self.use_labels:
																				a__:				Any 							=ids_tensor([self.batch_size]					,				self.num_labels	)

													a__:				List[str] 							=self.get_config()

													return config, pixel_values, labels


						def      _lowerCamelCase	(       self  :     str	):
													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					,				out_features=self.out_features					,				out_indices=self.out_indices					,				)


						def      _lowerCamelCase	(       self  :     Tuple					,				_a  :     Any					,				_a  :     List[Any]					,				_a  :     Union[str, Any]	):
													a__:				Optional[int] 							=ResNetModel(config=lowerCAmelCase__	)
													model.to(lowerCAmelCase__	)
													model.eval()
													a__:				Union[str, Any] 							=model(lowerCAmelCase__	)
													# expected last hidden states: B, C, H // 32, W // 32
													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      _lowerCamelCase	(       self  :     Optional[int]					,				_a  :     int					,				_a  :     Union[str, Any]					,				_a  :     List[str]	):
													a__:				Any 							=self.num_labels
													a__:				str 							=ResNetForImageClassification(lowerCAmelCase__	)
													model.to(lowerCAmelCase__	)
													model.eval()
													a__:				List[str] 							=model(lowerCAmelCase__					,				labels=lowerCAmelCase__	)
													self.parent.assertEqual(result.logits.shape					,				(self.batch_size, self.num_labels)	)


						def      _lowerCamelCase	(       self  :     Union[str, Any]					,				_a  :     Optional[int]					,				_a  :     Any					,				_a  :     Optional[Any]	):
													a__:				List[str] 							=ResNetBackbone(config=lowerCAmelCase__	)
													model.to(lowerCAmelCase__	)
													model.eval()
													a__:				Tuple 							=model(lowerCAmelCase__	)

													# verify feature maps
													self.parent.assertEqual(len(result.feature_maps	)					,				len(config.out_features	)	)
													self.parent.assertListEqual(list(result.feature_maps[0].shape	)					,				[self.batch_size, self.hidden_sizes[1], 4, 4]	)

													# verify channels
													self.parent.assertEqual(len(model.channels	)					,				len(config.out_features	)	)
													self.parent.assertListEqual(model.channels					,				config.hidden_sizes[1:]	)

													# verify backbone works with out_features=None
													a__:				Dict 							=None
													a__:				Optional[Any] 							=ResNetBackbone(config=lowerCAmelCase__	)
													model.to(lowerCAmelCase__	)
													model.eval()
													a__:				Any 							=model(lowerCAmelCase__	)

													# verify feature maps
													self.parent.assertEqual(len(result.feature_maps	)					,				1	)
													self.parent.assertListEqual(list(result.feature_maps[0].shape	)					,				[self.batch_size, self.hidden_sizes[-1], 1, 1]	)

													# verify channels
													self.parent.assertEqual(len(model.channels	)					,				1	)
													self.parent.assertListEqual(model.channels					,				[config.hidden_sizes[-1]]	)







						def      _lowerCamelCase	(       self  :     Dict	):
													a__:				Tuple 							=self.prepare_config_and_inputs()
													a__:				str 							=config_and_inputs
													a__:				str 							={'pixel_values': pixel_values}
													return config, inputs_dict


@require_torch
class 		lowerCamelCase__       (						__SCREAMING_SNAKE_CASE ,    __SCREAMING_SNAKE_CASE ,    unittest.TestCase      ):
						_lowerCAmelCase	    =       (
						    (
						        ResNetModel,
						        ResNetForImageClassification,
						        ResNetBackbone,
						    )
						    if is_torch_available()
						    else ()
						)
						_lowerCAmelCase	    =       (
						    {'''feature-extraction''': ResNetModel, '''image-classification''': ResNetForImageClassification}
						    if is_torch_available()
						    else {}
						)

						_lowerCAmelCase	    =       True
						_lowerCAmelCase	    =       False
						_lowerCAmelCase	    =       False
						_lowerCAmelCase	    =       False
						_lowerCAmelCase	    =       False


						def      _lowerCamelCase	(       self  :     Optional[int]	):
													a__:				str 							=ResNetModelTester(self	)
													a__:				Optional[Any] 							=ConfigTester(self					,				config_class=lowerCAmelCase__					,				has_text_modality=lowerCAmelCase__	)


						def      _lowerCamelCase	(       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      _lowerCamelCase	(       self  :     List[str]	):
													return


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


						@unittest.skip(reason="ResNet does not support input and output embeddings"	)
						def      _lowerCamelCase	(       self  :     Optional[Any]	):
													pass


						def      _lowerCamelCase	(       self  :     Optional[Any]	):
													a__:				str 							=self.model_tester.prepare_config_and_inputs_for_common()

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

																				a__:				Optional[Any] 							=['pixel_values']
																				self.assertListEqual(arg_names[:1]					,				lowerCAmelCase__	)


						def      _lowerCamelCase	(       self  :     Optional[int]	):
													a__:				Any 							=self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*lowerCAmelCase__	)


						def      _lowerCamelCase	(       self  :     Optional[Any]	):
													a__:				int 							=self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_backbone(*lowerCAmelCase__	)


						def      _lowerCamelCase	(       self  :     Union[str, Any]	):
													a__:				List[str] 							=self.model_tester.prepare_config_and_inputs_for_common()

													for model_class in self.all_model_classes:
																				a__:				List[str] 							=model_class(config=lowerCAmelCase__	)
																				for name, module in model.named_modules():
																											if isinstance(lowerCAmelCase__					,				(nn.BatchNormad, nn.GroupNorm)	):
																																		self.assertTrue(
																																		    torch.all(module.weight == 1	)					,				msg=F"Parameter {name} of model {model_class} seems not properly initialized"					,				)
																																		self.assertTrue(
																																		    torch.all(module.bias == 0	)					,				msg=F"Parameter {name} of model {model_class} seems not properly initialized"					,				)


						def      _lowerCamelCase	(       self  :     int	):




													def check_hidden_states_output(_a  :     Tuple					,				_a  :     List[Any]					,				_a  :     Optional[Any]	):
																				a__:				Any 							=model_class(lowerCAmelCase__	)
																				model.to(lowerCAmelCase__	)
																				model.eval()

																				with torch.no_grad():
																											a__:				Dict 							=model(**self._prepare_for_class(lowerCAmelCase__					,				lowerCAmelCase__	)	)

																				a__:				Dict 							=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	)

																				# ResNet's feature maps are of shape (batch_size, num_channels, height, width)
																				self.assertListEqual(
																				    list(hidden_states[0].shape[-2:]	)					,				[self.model_tester.image_size // 4, self.model_tester.image_size // 4]					,				)

													a__:				Dict 							=self.model_tester.prepare_config_and_inputs_for_common()
													a__:				Optional[Any] 							=['basic', 'bottleneck']
													for model_class in self.all_model_classes:
																				for layer_type in layers_type:
																											a__:				Any 							=layer_type
																											a__:				Optional[Any] 							=True
																											check_hidden_states_output(lowerCAmelCase__					,				lowerCAmelCase__					,				lowerCAmelCase__	)

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

																											check_hidden_states_output(lowerCAmelCase__					,				lowerCAmelCase__					,				lowerCAmelCase__	)


						@unittest.skip(reason="ResNet does not use feedforward chunking"	)
						def      _lowerCamelCase	(       self  :     Optional[int]	):
													pass


						def      _lowerCamelCase	(       self  :     Optional[Any]	):
													a__:				int 							=self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase__	)







						@slow
						def      _lowerCamelCase	(       self  :     List[Any]	):




													for model_name in RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																				a__:				List[str] 							=ResNetModel.from_pretrained(lowerCAmelCase__	)
																				self.assertIsNotNone(lowerCAmelCase__	)





def   __lowerCamelCase				(					):
							a__:				Optional[int] 							=Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"  )
							return image


@require_torch
@require_vision
class 		lowerCamelCase__       (						unittest.TestCase      ):


						@cached_property
						def      _lowerCamelCase	(       self  :     Optional[Any]	):
													return (
													    AutoImageProcessor.from_pretrained(RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]	)
													    if is_vision_available()
													    else None
													)







						@slow
						def      _lowerCamelCase	(       self  :     Dict	):
													a__:				str 							=ResNetForImageClassification.from_pretrained(RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]	).to(lowerCAmelCase__	)

													a__:				int 							=self.default_image_processor
													a__:				Any 							=prepare_img()
													a__:				List[str] 							=image_processor(images=lowerCAmelCase__					,				return_tensors="pt"	).to(lowerCAmelCase__	)

													# forward pass
													with torch.no_grad():
																				a__:				Optional[Any] 							=model(**lowerCAmelCase__	)

													# verify the logits
													a__:				Any 							=torch.Size((1, 1_0_0_0)	)
													self.assertEqual(outputs.logits.shape					,				lowerCAmelCase__	)

													a__:				Optional[int] 							=torch.tensor([-1_1.1_0_6_9, -9.7_8_7_7, -8.3_7_7_7]	).to(lowerCAmelCase__	)

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


@require_torch
class 		lowerCamelCase__       (						__SCREAMING_SNAKE_CASE ,    unittest.TestCase      ):
						_lowerCAmelCase	    =       (ResNetBackbone,) if is_torch_available() else ()
						_lowerCAmelCase	    =       False
						_lowerCAmelCase	    =       ResNetConfig


						def      _lowerCamelCase	(       self  :     str	):
													a__:				Dict 							=ResNetModelTester(self	)









from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCAmelCase__       :	Any     				=logging.get_logger(__name__)

lowerCAmelCase__       :	Union[str, Any]     				={
    'google/reformer-crime-and-punishment': (
        'https://huggingface.co/google/reformer-crime-and-punishment/resolve/main/config.json'
    ),
    'google/reformer-enwik8': 'https://huggingface.co/google/reformer-enwik8/resolve/main/config.json',
}







class      __lowercase  (__SCREAMING_SNAKE_CASE   ):





							"""simple docstring"""







							_UpperCAmelCase     =		"""reformer"""
							_UpperCAmelCase     =		["""past_buckets_states"""]
							_UpperCAmelCase     =		{}






							def __init__(	self       ,		lowerCAmelCase__=6_4       ,		lowerCAmelCase__=["local", "lsh", "local", "lsh", "local", "lsh"]       ,		lowerCAmelCase__=1.0       ,		lowerCAmelCase__=True       ,		lowerCAmelCase__=[6_4, 6_4]       ,		lowerCAmelCase__=[6_4, 1_9_2]       ,		lowerCAmelCase__=0       ,		lowerCAmelCase__=2       ,		lowerCAmelCase__=5_1_2       ,		lowerCAmelCase__=None       ,		lowerCAmelCase__="relu"       ,		lowerCAmelCase__=0.05       ,		lowerCAmelCase__=2_5_6       ,		lowerCAmelCase__=0.02       ,		lowerCAmelCase__=False       ,		lowerCAmelCase__=1E-12       ,		lowerCAmelCase__=1       ,		lowerCAmelCase__=0       ,		lowerCAmelCase__=0.05       ,		lowerCAmelCase__=6_4       ,		lowerCAmelCase__=6_4       ,		lowerCAmelCase__=0.0       ,		lowerCAmelCase__=1       ,		lowerCAmelCase__=0       ,		lowerCAmelCase__=4_0_9_6       ,		lowerCAmelCase__=1_2       ,		lowerCAmelCase__=None       ,		lowerCAmelCase__=1       ,		lowerCAmelCase__=0       ,		lowerCAmelCase__=3_2_0       ,		lowerCAmelCase__=False       ,		lowerCAmelCase__=True       ,		lowerCAmelCase__=None       ,		**lowerCAmelCase__       ,		):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_						:			str       						= hash_seed
										SCREAMING_SNAKE_CASE_						:			Union[str, Any]       						= vocab_size
										SCREAMING_SNAKE_CASE_						:			Any       						= attention_head_size
										SCREAMING_SNAKE_CASE_						:			List[Any]       						= hidden_size
										SCREAMING_SNAKE_CASE_						:			Optional[Any]       						= num_attention_heads
										SCREAMING_SNAKE_CASE_						:			Any       						= num_hashes
										SCREAMING_SNAKE_CASE_						:			int       						= len(lowerCAmelCase__    )
										SCREAMING_SNAKE_CASE_						:			str       						= tuple(lowerCAmelCase__    ) if isinstance(lowerCAmelCase__       ,		lowerCAmelCase__    ) else num_buckets
										SCREAMING_SNAKE_CASE_						:			int       						= lsh_attn_chunk_length
										SCREAMING_SNAKE_CASE_						:			List[str]       						= local_attn_chunk_length
										SCREAMING_SNAKE_CASE_						:			Tuple       						= lsh_num_chunks_after
										SCREAMING_SNAKE_CASE_						:			str       						= lsh_num_chunks_before
										SCREAMING_SNAKE_CASE_						:			List[Any]       						= local_num_chunks_after
										SCREAMING_SNAKE_CASE_						:			Any       						= local_num_chunks_before
										SCREAMING_SNAKE_CASE_						:			Any       						= hidden_act
										SCREAMING_SNAKE_CASE_						:			Dict       						= feed_forward_size
										SCREAMING_SNAKE_CASE_						:			Optional[int]       						= hidden_dropout_prob
										SCREAMING_SNAKE_CASE_						:			Tuple       						= lsh_attention_probs_dropout_prob
										SCREAMING_SNAKE_CASE_						:			Any       						= local_attention_probs_dropout_prob
										SCREAMING_SNAKE_CASE_						:			str       						= max_position_embeddings
										SCREAMING_SNAKE_CASE_						:			Optional[int]       						= initializer_range
										SCREAMING_SNAKE_CASE_						:			Optional[Any]       						= layer_norm_eps
										SCREAMING_SNAKE_CASE_						:			List[str]       						= axial_pos_embds
										SCREAMING_SNAKE_CASE_						:			List[str]       						= tuple(lowerCAmelCase__    )
										SCREAMING_SNAKE_CASE_						:			Tuple       						= tuple(lowerCAmelCase__    )
										SCREAMING_SNAKE_CASE_						:			Dict       						= axial_norm_std
										SCREAMING_SNAKE_CASE_						:			Any       						= chunk_size_lm_head
										SCREAMING_SNAKE_CASE_						:			int       						= attn_layers
										SCREAMING_SNAKE_CASE_						:			Union[str, Any]       						= use_cache
										SCREAMING_SNAKE_CASE_						:			Tuple       						= classifier_dropout
										super().__init__(
										    pad_token_id=lowerCAmelCase__       ,		eos_token_id=lowerCAmelCase__       ,		is_decoder=lowerCAmelCase__       ,		tie_word_embeddings=lowerCAmelCase__       ,		**lowerCAmelCase__       ,		)





