




import collections
import inspect
import unittest

from transformers import SwinConfig
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, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
	import torch
	from torch import nn

	from transformers import SwinBackbone, SwinForImageClassification, SwinForMaskedImageModeling, SwinModel
	from transformers.models.swin.modeling_swin import SWIN_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
	from PIL import Image

	from transformers import AutoImageProcessor


class  lowerCamelCase     :






			'''simple docstring'''





			def __init__(			self      : List[str]							,    lowerCamelCase_      : Optional[Any]							,    lowerCamelCase_      : List[Any]=13							,    lowerCamelCase_      : Dict=32							,    lowerCamelCase_      : Dict=2							,    lowerCamelCase_      : str=3							,    lowerCamelCase_      : str=16							,    lowerCamelCase_      : Optional[Any]=[1, 2, 1]							,    lowerCamelCase_      : Dict=[2, 2, 4]							,    lowerCamelCase_      : Optional[Any]=2							,    lowerCamelCase_      : Any=2.0							,    lowerCamelCase_      : List[str]=True							,    lowerCamelCase_      : Any=0.0							,    lowerCamelCase_      : Optional[int]=0.0							,    lowerCamelCase_      : Dict=0.1							,    lowerCamelCase_      : Dict="gelu"							,    lowerCamelCase_      : Any=False							,    lowerCamelCase_      : Dict=True							,    lowerCamelCase_      : Optional[int]=0.0_2							,    lowerCamelCase_      : Tuple=1E-5							,    lowerCamelCase_      : Optional[Any]=True							,    lowerCamelCase_      : List[Any]=None							,    lowerCamelCase_      : Optional[Any]=True							,    lowerCamelCase_      : List[str]=10							,    lowerCamelCase_      : str=8							,    lowerCamelCase_      : Optional[Any]=["stage1", "stage2"]							,    lowerCamelCase_      : Tuple=[1, 2]							,    )    -> List[Any]:
								__magic_name__				:						str     				=				parent
								__magic_name__				:						Any     				=				batch_size
								__magic_name__				:						Optional[int]     				=				image_size
								__magic_name__				:						Any     				=				patch_size
								__magic_name__				:						Optional[Any]     				=				num_channels
								__magic_name__				:						int     				=				embed_dim
								__magic_name__				:						Any     				=				depths
								__magic_name__				:						int     				=				num_heads
								__magic_name__				:						List[str]     				=				window_size
								__magic_name__				:						List[str]     				=				mlp_ratio
								__magic_name__				:						List[str]     				=				qkv_bias
								__magic_name__				:						Optional[Any]     				=				hidden_dropout_prob
								__magic_name__				:						Tuple     				=				attention_probs_dropout_prob
								__magic_name__				:						Dict     				=				drop_path_rate
								__magic_name__				:						Optional[Any]     				=				hidden_act
								__magic_name__				:						List[str]     				=				use_absolute_embeddings
								__magic_name__				:						Any     				=				patch_norm
								__magic_name__				:						Any     				=				layer_norm_eps
								__magic_name__				:						List[Any]     				=				initializer_range
								__magic_name__				:						Any     				=				is_training
								__magic_name__				:						Any     				=				scope
								__magic_name__				:						Dict     				=				use_labels
								__magic_name__				:						Optional[Any]     				=				type_sequence_label_size
								__magic_name__				:						Tuple     				=				encoder_stride
								__magic_name__				:						List[Any]     				=				out_features
								__magic_name__				:						Any     				=				out_indices

			def 				UpperCAmelCase__							(			self      : str	)    -> Tuple:
								__magic_name__				:						Union[str, Any]     				=				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

								__magic_name__				:						Any     				=				None
								if self.use_labels:
													__magic_name__				:						Optional[int]     				=				ids_tensor([self.batch_size]							,    self.type_sequence_label_size	)

								__magic_name__				:						Union[str, Any]     				=				self.get_config()

								return config, pixel_values, labels

			def 				UpperCAmelCase__							(			self      : Optional[Any]	)    -> Dict:
								return SwinConfig(
								    image_size=self.image_size							,    patch_size=self.patch_size							,    num_channels=self.num_channels							,    embed_dim=self.embed_dim							,    depths=self.depths							,    num_heads=self.num_heads							,    window_size=self.window_size							,    mlp_ratio=self.mlp_ratio							,    qkv_bias=self.qkv_bias							,    hidden_dropout_prob=self.hidden_dropout_prob							,    attention_probs_dropout_prob=self.attention_probs_dropout_prob							,    drop_path_rate=self.drop_path_rate							,    hidden_act=self.hidden_act							,    use_absolute_embeddings=self.use_absolute_embeddings							,    path_norm=self.patch_norm							,    layer_norm_eps=self.layer_norm_eps							,    initializer_range=self.initializer_range							,    encoder_stride=self.encoder_stride							,    out_features=self.out_features							,    out_indices=self.out_indices							,    )

			def 				UpperCAmelCase__							(			self      : Dict							,    lowerCamelCase_      : List[Any]							,    lowerCamelCase_      : Optional[int]							,    lowerCamelCase_      : Dict	)    -> Dict:
								__magic_name__				:						str     				=				SwinModel(config=_SCREAMING_SNAKE_CASE	)
								model.to(_SCREAMING_SNAKE_CASE	)
								model.eval()
								__magic_name__				:						Any     				=				model(_SCREAMING_SNAKE_CASE	)

								__magic_name__				:						Optional[Any]     				=				((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths	) - 1))
								__magic_name__				:						Optional[int]     				=				int(config.embed_dim * 2 ** (len(config.depths	) - 1)	)

								self.parent.assertEqual(result.last_hidden_state.shape							,    (self.batch_size, expected_seq_len, expected_dim)	)

			def 				UpperCAmelCase__							(			self      : Optional[int]							,    lowerCamelCase_      : Optional[Any]							,    lowerCamelCase_      : Optional[int]							,    lowerCamelCase_      : int	)    -> int:
								__magic_name__				:						int     				=				SwinBackbone(config=_SCREAMING_SNAKE_CASE	)
								model.to(_SCREAMING_SNAKE_CASE	)
								model.eval()
								__magic_name__				:						Optional[Any]     				=				model(_SCREAMING_SNAKE_CASE	)

								# verify hidden states
								self.parent.assertEqual(len(result.feature_maps	)							,    len(config.out_features	)	)
								self.parent.assertListEqual(list(result.feature_maps[0].shape	)							,    [self.batch_size, model.channels[0], 16, 16]	)

								# verify channels
								self.parent.assertEqual(len(model.channels	)							,    len(config.out_features	)	)

								# verify backbone works with out_features=None
								__magic_name__				:						Union[str, Any]     				=				None
								__magic_name__				:						Tuple     				=				SwinBackbone(config=_SCREAMING_SNAKE_CASE	)
								model.to(_SCREAMING_SNAKE_CASE	)
								model.eval()
								__magic_name__				:						int     				=				model(_SCREAMING_SNAKE_CASE	)

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

								# verify channels
								self.parent.assertEqual(len(model.channels	)							,    1	)

			def 				UpperCAmelCase__							(			self      : List[Any]							,    lowerCamelCase_      : Tuple							,    lowerCamelCase_      : int							,    lowerCamelCase_      : Dict	)    -> Dict:
								__magic_name__				:						Optional[int]     				=				SwinForMaskedImageModeling(config=_SCREAMING_SNAKE_CASE	)
								model.to(_SCREAMING_SNAKE_CASE	)
								model.eval()
								__magic_name__				:						List[Any]     				=				model(_SCREAMING_SNAKE_CASE	)
								self.parent.assertEqual(
								    result.logits.shape							,    (self.batch_size, self.num_channels, self.image_size, self.image_size)	)

								# test greyscale images
								__magic_name__				:						List[Any]     				=				1
								__magic_name__				:						str     				=				SwinForMaskedImageModeling(_SCREAMING_SNAKE_CASE	)
								model.to(_SCREAMING_SNAKE_CASE	)
								model.eval()

								__magic_name__				:						Tuple     				=				floats_tensor([self.batch_size, 1, self.image_size, self.image_size]	)
								__magic_name__				:						Dict     				=				model(_SCREAMING_SNAKE_CASE	)
								self.parent.assertEqual(result.logits.shape							,    (self.batch_size, 1, self.image_size, self.image_size)	)

			def 				UpperCAmelCase__							(			self      : str							,    lowerCamelCase_      : List[str]							,    lowerCamelCase_      : Optional[Any]							,    lowerCamelCase_      : Optional[int]	)    -> str:
								__magic_name__				:						Union[str, Any]     				=				self.type_sequence_label_size
								__magic_name__				:						List[str]     				=				SwinForImageClassification(_SCREAMING_SNAKE_CASE	)
								model.to(_SCREAMING_SNAKE_CASE	)
								model.eval()
								__magic_name__				:						str     				=				model(_SCREAMING_SNAKE_CASE							,    labels=_SCREAMING_SNAKE_CASE	)
								self.parent.assertEqual(result.logits.shape							,    (self.batch_size, self.type_sequence_label_size)	)

								# test greyscale images
								__magic_name__				:						List[Any]     				=				1
								__magic_name__				:						str     				=				SwinForImageClassification(_SCREAMING_SNAKE_CASE	)
								model.to(_SCREAMING_SNAKE_CASE	)
								model.eval()

								__magic_name__				:						int     				=				floats_tensor([self.batch_size, 1, self.image_size, self.image_size]	)
								__magic_name__				:						Tuple     				=				model(_SCREAMING_SNAKE_CASE	)
								self.parent.assertEqual(result.logits.shape							,    (self.batch_size, self.type_sequence_label_size)	)


			def 				UpperCAmelCase__							(			self      : Dict	)    -> Any:
								__magic_name__				:						Optional[int]     				=				self.prepare_config_and_inputs()
								(
								    (
								    __magic_name__  
								)				,     (
								    __magic_name__  
								)				,     (
								    __magic_name__  
								)				,       
								)				:						Optional[int]     				=				config_and_inputs
								__magic_name__				:						Optional[int]     				=				{'''pixel_values''': pixel_values}
								return config, inputs_dict






@require_torch
class  lowerCamelCase     (		_UpperCamelCase					,_UpperCamelCase					,unittest.TestCase    ):






			'''simple docstring'''





			UpperCamelCase__										=(
			    (
			        SwinModel,
			        SwinBackbone,
			        SwinForImageClassification,
			        SwinForMaskedImageModeling,
			    )
			    if is_torch_available()
			    else ()
			)
			UpperCamelCase__										=(
			    {"""feature-extraction""": SwinModel, """image-classification""": SwinForImageClassification}
			    if is_torch_available()
			    else {}
			)
			UpperCamelCase__										=True

			UpperCamelCase__										=False
			UpperCamelCase__										=False
			UpperCamelCase__										=False

			def 				UpperCAmelCase__							(			self      : Any	)    -> str:
								__magic_name__				:						Tuple     				=				SwinModelTester(self	)
								__magic_name__				:						List[Any]     				=				ConfigTester(self							,    config_class=_SCREAMING_SNAKE_CASE							,    embed_dim=37	)

			def 				UpperCAmelCase__							(			self      : Optional[int]	)    -> int:
								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      : Any	)    -> Dict:
								return

			def 				UpperCAmelCase__							(			self      : Tuple	)    -> List[Any]:
								__magic_name__				:						Tuple     				=				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE	)

			@unittest.skip(reason='''Got `CUDA error: misaligned address` with PyTorch 2.0.0.'''	)
			def 				UpperCAmelCase__							(			self      : Any	)    -> Dict:
								pass

			def 				UpperCAmelCase__							(			self      : List[Any]	)    -> Any:
								super().test_training_gradient_checkpointing()

			def 				UpperCAmelCase__							(			self      : List[str]	)    -> Optional[Any]:
								__magic_name__				:						Tuple     				=				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_backbone(*_SCREAMING_SNAKE_CASE	)

			def 				UpperCAmelCase__							(			self      : Optional[int]	)    -> List[str]:
								__magic_name__				:						Optional[Any]     				=				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_masked_image_modeling(*_SCREAMING_SNAKE_CASE	)

			def 				UpperCAmelCase__							(			self      : List[Any]	)    -> Union[str, Any]:
								__magic_name__				:						Optional[Any]     				=				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_classification(*_SCREAMING_SNAKE_CASE	)

			@unittest.skip(reason='''Swin does not use inputs_embeds'''	)
			def 				UpperCAmelCase__							(			self      : Any	)    -> Tuple:
								pass

			@unittest.skip(reason='''Swin Transformer does not use feedforward chunking'''	)
			def 				UpperCAmelCase__							(			self      : Tuple	)    -> List[str]:
								pass

			def 				UpperCAmelCase__							(			self      : Optional[int]	)    -> str:
								__magic_name__				,     __magic_name__				:						int     				=				self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
													__magic_name__				:						Tuple     				=				model_class(_SCREAMING_SNAKE_CASE	)
													self.assertIsInstance(model.get_input_embeddings()							,    (nn.Module)	)
													__magic_name__				:						List[str]     				=				model.get_output_embeddings()
													self.assertTrue(x is None or isinstance(_SCREAMING_SNAKE_CASE							,    nn.Linear	)	)

			def 				UpperCAmelCase__							(			self      : int	)    -> Union[str, Any]:
								__magic_name__				,     __magic_name__				:						List[str]     				=				self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
													__magic_name__				:						Optional[Any]     				=				model_class(_SCREAMING_SNAKE_CASE	)
													__magic_name__				:						str     				=				inspect.signature(model.forward	)
													# signature.parameters is an OrderedDict => so arg_names order is deterministic
													__magic_name__				:						str     				=				[*signature.parameters.keys()]

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

			def 				UpperCAmelCase__							(			self      : Tuple	)    -> Union[str, Any]:
								__magic_name__				,     __magic_name__				:						List[Any]     				=				self.model_tester.prepare_config_and_inputs_for_common()
								__magic_name__				:						Tuple     				=				True

								for model_class in self.all_model_classes:
													__magic_name__				:						Optional[Any]     				=				True
													__magic_name__				:						Union[str, Any]     				=				False
													__magic_name__				:						List[Any]     				=				True
													__magic_name__				:						Tuple     				=				model_class(_SCREAMING_SNAKE_CASE	)
													model.to(_SCREAMING_SNAKE_CASE	)
													model.eval()
													with torch.no_grad():
																		__magic_name__				:						Any     				=				model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE							,    _SCREAMING_SNAKE_CASE	)	)
													__magic_name__				:						List[str]     				=				outputs.attentions
													__magic_name__				:						int     				=				len(self.model_tester.depths	)
													self.assertEqual(len(_SCREAMING_SNAKE_CASE	)							,    _SCREAMING_SNAKE_CASE	)

													# check that output_attentions also work using config
													del inputs_dict["output_attentions"]
													__magic_name__				:						Any     				=				True
													__magic_name__				:						Optional[Any]     				=				config.window_size**2
													__magic_name__				:						Optional[int]     				=				model_class(_SCREAMING_SNAKE_CASE	)
													model.to(_SCREAMING_SNAKE_CASE	)
													model.eval()
													with torch.no_grad():
																		__magic_name__				:						Dict     				=				model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE							,    _SCREAMING_SNAKE_CASE	)	)
													__magic_name__				:						int     				=				outputs.attentions
													self.assertEqual(len(_SCREAMING_SNAKE_CASE	)							,    _SCREAMING_SNAKE_CASE	)

													self.assertListEqual(
													    list(attentions[0].shape[-3:]	)							,    [self.model_tester.num_heads[0], window_size_squared, window_size_squared]							,    )
													__magic_name__				:						List[Any]     				=				len(_SCREAMING_SNAKE_CASE	)

													# Check attention is always last and order is fine
													__magic_name__				:						List[str]     				=				True
													__magic_name__				:						int     				=				True
													__magic_name__				:						Optional[int]     				=				model_class(_SCREAMING_SNAKE_CASE	)
													model.to(_SCREAMING_SNAKE_CASE	)
													model.eval()
													with torch.no_grad():
																		__magic_name__				:						List[Any]     				=				model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE							,    _SCREAMING_SNAKE_CASE	)	)

													# also another +1 for reshaped_hidden_states
													__magic_name__				:						List[str]     				=				1 if model_class.__name__ == '''SwinBackbone''' else 2
													self.assertEqual(out_len + added_hidden_states							,    len(_SCREAMING_SNAKE_CASE	)	)

													__magic_name__				:						List[str]     				=				outputs.attentions

													self.assertEqual(len(_SCREAMING_SNAKE_CASE	)							,    _SCREAMING_SNAKE_CASE	)

													self.assertListEqual(
													    list(self_attentions[0].shape[-3:]	)							,    [self.model_tester.num_heads[0], window_size_squared, window_size_squared]							,    )

			def 				UpperCAmelCase__							(			self      : Union[str, Any]							,    lowerCamelCase_      : str							,    lowerCamelCase_      : Union[str, Any]							,    lowerCamelCase_      : Dict							,    lowerCamelCase_      : int	)    -> Tuple:
								__magic_name__				:						List[Any]     				=				model_class(_SCREAMING_SNAKE_CASE	)
								model.to(_SCREAMING_SNAKE_CASE	)
								model.eval()

								with torch.no_grad():
													__magic_name__				:						int     				=				model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE							,    _SCREAMING_SNAKE_CASE	)	)

								__magic_name__				:						List[Any]     				=				outputs.hidden_states

								__magic_name__				:						int     				=				getattr(
								    self.model_tester							,    '''expected_num_hidden_layers'''							,    len(self.model_tester.depths	) + 1	)
								self.assertEqual(len(_SCREAMING_SNAKE_CASE	)							,    _SCREAMING_SNAKE_CASE	)

								# Swin has a different seq_length
								__magic_name__				:						Dict     				=				(
								    config.patch_size
								    if isinstance(config.patch_size							,    collections.abc.Iterable	)
								    else (config.patch_size, config.patch_size)
								)

								__magic_name__				:						Union[str, Any]     				=				(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

								self.assertListEqual(
								    list(hidden_states[0].shape[-2:]	)							,    [num_patches, self.model_tester.embed_dim]							,    )

								if not model_class.__name__ == "SwinBackbone":
													__magic_name__				:						Optional[Any]     				=				outputs.reshaped_hidden_states
													self.assertEqual(len(_SCREAMING_SNAKE_CASE	)							,    _SCREAMING_SNAKE_CASE	)

													__magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				:						Any     				=				reshaped_hidden_states[0].shape
													__magic_name__				:						int     				=				(
													    reshaped_hidden_states[0].view(_SCREAMING_SNAKE_CASE							,    _SCREAMING_SNAKE_CASE							,    height * width	).permute(0							,    2							,    1	)
													)
													self.assertListEqual(
													    list(reshaped_hidden_states.shape[-2:]	)							,    [num_patches, self.model_tester.embed_dim]							,    )

			def 				UpperCAmelCase__							(			self      : Any	)    -> Optional[Any]:
								__magic_name__				,     __magic_name__				:						Any     				=				self.model_tester.prepare_config_and_inputs_for_common()

								__magic_name__				:						List[Any]     				=				(
								    self.model_tester.image_size
								    if isinstance(self.model_tester.image_size							,    collections.abc.Iterable	)
								    else (self.model_tester.image_size, self.model_tester.image_size)
								)

								for model_class in self.all_model_classes:
													__magic_name__				:						Union[str, Any]     				=				True
													self.check_hidden_states_output(_SCREAMING_SNAKE_CASE							,    _SCREAMING_SNAKE_CASE							,    _SCREAMING_SNAKE_CASE							,    _SCREAMING_SNAKE_CASE	)

													# check that output_hidden_states also work using config
													del inputs_dict["output_hidden_states"]
													__magic_name__				:						List[Any]     				=				True

													self.check_hidden_states_output(_SCREAMING_SNAKE_CASE							,    _SCREAMING_SNAKE_CASE							,    _SCREAMING_SNAKE_CASE							,    _SCREAMING_SNAKE_CASE	)

			def 				UpperCAmelCase__							(			self      : str	)    -> Tuple:
								__magic_name__				,     __magic_name__				:						str     				=				self.model_tester.prepare_config_and_inputs_for_common()
								__magic_name__				:						Optional[int]     				=				3

								__magic_name__				:						Union[str, Any]     				=				(
								    self.model_tester.image_size
								    if isinstance(self.model_tester.image_size							,    collections.abc.Iterable	)
								    else (self.model_tester.image_size, self.model_tester.image_size)
								)
								__magic_name__				:						Any     				=				(
								    config.patch_size
								    if isinstance(config.patch_size							,    collections.abc.Iterable	)
								    else (config.patch_size, config.patch_size)
								)

								__magic_name__				:						Dict     				=				image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
								__magic_name__				:						List[Any]     				=				image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

								for model_class in self.all_model_classes:
													__magic_name__				:						Dict     				=				True
													self.check_hidden_states_output(_SCREAMING_SNAKE_CASE							,    _SCREAMING_SNAKE_CASE							,    _SCREAMING_SNAKE_CASE							,    (padded_height, padded_width)	)

													# check that output_hidden_states also work using config
													del inputs_dict["output_hidden_states"]
													__magic_name__				:						Tuple     				=				True
													self.check_hidden_states_output(_SCREAMING_SNAKE_CASE							,    _SCREAMING_SNAKE_CASE							,    _SCREAMING_SNAKE_CASE							,    (padded_height, padded_width)	)

			@slow
			def 				UpperCAmelCase__							(			self      : Any	)    -> Union[str, Any]:





								for model_name in SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													__magic_name__				:						int     				=				SwinModel.from_pretrained(_SCREAMING_SNAKE_CASE	)
													self.assertIsNotNone(_SCREAMING_SNAKE_CASE	)


			def 				UpperCAmelCase__							(			self      : Dict	)    -> Tuple:
								__magic_name__				,     __magic_name__				:						Union[str, Any]     				=				self.model_tester.prepare_config_and_inputs_for_common()

								__magic_name__				:						int     				=				_config_zero_init(_SCREAMING_SNAKE_CASE	)
								for model_class in self.all_model_classes:
													__magic_name__				:						Optional[int]     				=				model_class(config=_SCREAMING_SNAKE_CASE	)
													for name, param in model.named_parameters():
																		if "embeddings" not in name and param.requires_grad:
																							self.assertIn(
																							    ((param.data.mean() * 1E9).round() / 1E9).item()							,    [0.0, 1.0]							,    msg=F'''Parameter {name} of model {model_class} seems not properly initialized'''							,    )






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






			'''simple docstring'''





			@cached_property
			def 				UpperCAmelCase__							(			self      : Any	)    -> str:
								return (
								    AutoImageProcessor.from_pretrained('''microsoft/swin-tiny-patch4-window7-224'''	)
								    if is_vision_available()
								    else None
								)


			@slow
			def 				UpperCAmelCase__							(			self      : Tuple	)    -> Dict:
								__magic_name__				:						List[Any]     				=				SwinForImageClassification.from_pretrained('''microsoft/swin-tiny-patch4-window7-224'''	).to(_SCREAMING_SNAKE_CASE	)
								__magic_name__				:						Any     				=				self.default_image_processor

								__magic_name__				:						Any     				=				Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''	)
								__magic_name__				:						List[Any]     				=				image_processor(images=_SCREAMING_SNAKE_CASE							,    return_tensors='''pt'''	).to(_SCREAMING_SNAKE_CASE	)

								# forward pass
								with torch.no_grad():
													__magic_name__				:						Union[str, Any]     				=				model(**_SCREAMING_SNAKE_CASE	)

								# verify the logits
								__magic_name__				:						Optional[Any]     				=				torch.Size((1, 1000)	)
								self.assertEqual(outputs.logits.shape							,    _SCREAMING_SNAKE_CASE	)
								__magic_name__				:						Union[str, Any]     				=				torch.tensor([-0.0_9_4_8, -0.6_4_5_4, -0.0_9_2_1]	).to(_SCREAMING_SNAKE_CASE	)
								self.assertTrue(torch.allclose(outputs.logits[0, :3]							,    _SCREAMING_SNAKE_CASE							,    atol=1E-4	)	)






@require_torch
class  lowerCamelCase     (		unittest.TestCase					,_UpperCamelCase    ):






			'''simple docstring'''





			UpperCamelCase__										=(SwinBackbone,) if is_torch_available() else ()
			UpperCamelCase__										=SwinConfig

			def 				UpperCAmelCase__							(			self      : Any	)    -> Optional[Any]:
								__magic_name__				:						Any     				=				SwinModelTester(self	)











"""simple docstring"""



import timeit
from typing import Callable, Optional

from ..configuration_utils import PretrainedConfig
from ..models.auto.modeling_auto import MODEL_MAPPING, MODEL_WITH_LM_HEAD_MAPPING
from ..utils import is_pyanvml_available, is_torch_available, logging
from .benchmark_utils import (
    Benchmark,
    Memory,
    MemorySummary,
    measure_peak_memory_cpu,
    start_memory_tracing,
    stop_memory_tracing,
)


if is_torch_available():
						import torch

						from .benchmark_args import PyTorchBenchmarkArguments


if is_pyanvml_available():
						import pyanvml.pyanvml as nvml


_snake_case							:   str	  =       logging.get_logger(__name__)




class 							__lowercase(							_UpperCamelCase	):
						lowerCAmelCase_		:				PyTorchBenchmarkArguments
						lowerCAmelCase_		:				PretrainedConfig
						lowerCAmelCase_		:				str=							"PyTorch"






						@property
						def 		lowerCAmelCase (       self)      ->	List[Any]:




											"""simple docstring"""





											return torch.__version__






						def 		lowerCAmelCase (       self     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)      ->	float:




											"""simple docstring"""





											lowerCamelCase__					=self._prepare_inference_func(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)
											return self._measure_speed(_inference)






						def 		lowerCAmelCase (       self     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)      ->	[Memory, Optional[MemorySummary]]:




											"""simple docstring"""





											lowerCamelCase__					=self._prepare_inference_func(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)
											return self._measure_memory(_inference)






						def 		lowerCAmelCase (       self     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)      ->	float:




											"""simple docstring"""





											lowerCamelCase__					=self._prepare_train_func(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)
											return self._measure_speed(_train)






						def 		lowerCAmelCase (       self     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)      ->	[Memory, Optional[MemorySummary]]:




											"""simple docstring"""





											lowerCamelCase__					=self._prepare_train_func(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)
											return self._measure_memory(_train)






						def 		lowerCAmelCase (       self     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)      ->	Callable[[], None]:




											"""simple docstring"""





											lowerCamelCase__					=self.config_dict[model_name]

											if self.args.torchscript:
																lowerCamelCase__					=True

											lowerCamelCase__					=(
											    hasattr(_SCREAMING_SNAKE_CASE     ,    'architectures')
											    and isinstance(config.architectures     ,    _SCREAMING_SNAKE_CASE)
											    and len(config.architectures) > 0
											)
											if not self.args.only_pretrain_model and has_model_class_in_config:
																try:
																					lowerCamelCase__					=config.architectures[0]
																					lowerCamelCase__					=__import__('transformers'     ,    fromlist=[model_class])
																					lowerCamelCase__					=getattr(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)
																					lowerCamelCase__					=model_cls(_SCREAMING_SNAKE_CASE)
																except ImportError:
																					raise ImportError(
																					    F"{model_class} does not exist. If you just want to test the pretrained model, you might want to"
																					    ' set `--only_pretrain_model` or `args.only_pretrain_model=True`.')
											else:
																lowerCamelCase__					=MODEL_MAPPING[config.__class__](_SCREAMING_SNAKE_CASE)

											model.eval()
											model.to(self.args.device)

											# encoder-decoder has vocab size saved differently
											lowerCamelCase__					=config.vocab_size if hasattr(_SCREAMING_SNAKE_CASE     ,    'vocab_size') else config.encoder.vocab_size
											lowerCamelCase__					=torch.randint(_SCREAMING_SNAKE_CASE     ,    (batch_size, sequence_length)     ,    dtype=torch.long     ,    device=self.args.device)

											if self.args.fpaa:
																logger.info('Running training in Mixed Precision...')
																if not self.args.is_gpu:
																					raise ValueError('Mixed precision is possible only for GPU.')
																# amp seems to have memory leaks so that memory usage
																# is measured using .half() for now https://github.com/NVIDIA/apex/issues/439
																model.half()

											if self.args.torchscript:
																with torch.no_grad():
																					lowerCamelCase__					=torch.jit.trace(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)
											else:
																lowerCamelCase__					=model

											def encoder_decoder_forward():
																with torch.no_grad():
																					lowerCamelCase__					=inference_model(_SCREAMING_SNAKE_CASE     ,    decoder_input_ids=_SCREAMING_SNAKE_CASE)
																return outputs

											def encoder_forward():
																with torch.no_grad():
																					lowerCamelCase__					=inference_model(_SCREAMING_SNAKE_CASE)
																return outputs

											lowerCamelCase__					=encoder_decoder_forward if config.is_encoder_decoder else encoder_forward
											return _forward






						def 		lowerCAmelCase (       self     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)      ->	Callable[[], None]:




											"""simple docstring"""





											lowerCamelCase__					=self.config_dict[model_name]

											lowerCamelCase__					=(
											    hasattr(_SCREAMING_SNAKE_CASE     ,    'architectures')
											    and isinstance(config.architectures     ,    _SCREAMING_SNAKE_CASE)
											    and len(config.architectures) > 0
											)
											if not self.args.only_pretrain_model and has_model_class_in_config:
																try:
																					lowerCamelCase__					=config.architectures[0]
																					lowerCamelCase__					=__import__('transformers'     ,    fromlist=[model_class])
																					lowerCamelCase__					=getattr(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)
																					lowerCamelCase__					=model_cls(_SCREAMING_SNAKE_CASE)
																except ImportError:
																					raise ImportError(
																					    F"{model_class} does not exist. If you just want to test the pretrained model, you might want to"
																					    ' set `--only_pretrain_model` or `args.only_pretrain_model=True`.')
											else:
																lowerCamelCase__					=MODEL_WITH_LM_HEAD_MAPPING[config.__class__](_SCREAMING_SNAKE_CASE)

											if self.args.torchscript:
																raise NotImplementedError('Training for torchscript is currently not implemented')
											else:
																lowerCamelCase__					=model

											model.train()
											model.to(self.args.device)

											# encoder-decoder has vocab size saved differently
											lowerCamelCase__					=config.vocab_size if hasattr(_SCREAMING_SNAKE_CASE     ,    'vocab_size') else config.encoder.vocab_size
											lowerCamelCase__					=torch.randint(_SCREAMING_SNAKE_CASE     ,    (batch_size, sequence_length)     ,    dtype=torch.long     ,    device=self.args.device)

											if self.args.fpaa:
																logger.info('Running training in Mixed Precision...')
																if not self.args.is_gpu:
																					raise ValueError('Mixed precision is possible only for GPU.')

																# amp seems to have memory leaks so that memory usage
																# is measured using .half() for now https://github.com/NVIDIA/apex/issues/439
																model.half()

											def compute_loss_and_backprob_encoder():
																lowerCamelCase__					=train_model(_SCREAMING_SNAKE_CASE     ,    labels=_SCREAMING_SNAKE_CASE)[0]
																loss.backward()
																return loss

											def compute_loss_and_backprob_encoder_decoder():
																lowerCamelCase__					=train_model(_SCREAMING_SNAKE_CASE     ,    decoder_input_ids=_SCREAMING_SNAKE_CASE     ,    labels=_SCREAMING_SNAKE_CASE)[0]
																loss.backward()
																return loss

											lowerCamelCase__					=(
											    compute_loss_and_backprob_encoder_decoder
											    if config.is_encoder_decoder
											    else compute_loss_and_backprob_encoder
											)
											return _train






						def 		lowerCAmelCase (       self     ,    _SCREAMING_SNAKE_CASE)      ->	float:




											"""simple docstring"""





											try:
																if self.args.is_tpu or self.args.torchscript:
																					# run additional 10 times to stabilize compilation for tpu and torchscript
																					logger.info('Do inference on TPU or torchscript. Running model 5 times to stabilize compilation')
																					timeit.repeat(
																					    _SCREAMING_SNAKE_CASE     ,    repeat=1     ,    number=5     ,    )

																# as written in https://docs.python.org/2/library/timeit.html#timeit.Timer.repeat, min should be taken rather than the average
																lowerCamelCase__					=timeit.repeat(
																    _SCREAMING_SNAKE_CASE     ,    repeat=self.args.repeat     ,    number=10     ,    )

																if self.args.is_tpu and self.args.torch_xla_tpu_print_metrics:
																					import torch_xla.debug.metrics as met

																					self.print_fn(met.metrics_report())

																return min(_SCREAMING_SNAKE_CASE) / 10.0
											except RuntimeError as e:
																self.print_fn(F"Doesn't fit on GPU. {e}")
																return "N/A"






						def 		lowerCAmelCase (       self     ,    _SCREAMING_SNAKE_CASE)      ->	[Memory, MemorySummary]:




											"""simple docstring"""





											try:
																if self.args.trace_memory_line_by_line:
																					lowerCamelCase__					=start_memory_tracing('transformers')

																if self.args.is_tpu:
																					# tpu
																					raise NotImplementedError(
																					    'Memory Benchmarking is currently not implemented for TPU. Please disable memory benchmarking with'
																					    ' `--no-memory` or `args.memory=False`')
																elif self.args.is_gpu:
																					if not is_pyanvml_available():
																										logger.warning(
																										    'py3nvml not installed, we won\'t log GPU memory usage. '
																										    'Install py3nvml (pip install py3nvml) to log information about GPU.')
																										lowerCamelCase__					='N/A'
																					else:
																										logger.info(
																										    'Measuring total GPU usage on GPU device. Make sure to not have additional processes running'
																										    ' on the same GPU.')
																										# init nvml
																										nvml.nvmlInit()
																										func()
																										lowerCamelCase__					=nvml.nvmlDeviceGetHandleByIndex(self.args.device_idx)
																										lowerCamelCase__					=nvml.nvmlDeviceGetMemoryInfo(_SCREAMING_SNAKE_CASE)
																										lowerCamelCase__					=meminfo.used
																										lowerCamelCase__					=Memory(_SCREAMING_SNAKE_CASE)
																										# shutdown nvml
																										nvml.nvmlShutdown()
																else:
																					# cpu
																					lowerCamelCase__					=measure_peak_memory_cpu(_SCREAMING_SNAKE_CASE)
																					lowerCamelCase__					=Memory(_SCREAMING_SNAKE_CASE) if isinstance(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE) else memory_bytes

																if self.args.trace_memory_line_by_line:
																					lowerCamelCase__					=stop_memory_tracing(_SCREAMING_SNAKE_CASE)
																else:
																					lowerCamelCase__					=None

																return memory, summary
											except RuntimeError as e:
																self.print_fn(F"Doesn't fit on GPU. {e}")
																return "N/A", None