



import copy
import inspect
import unittest

from transformers import PretrainedConfig, SwiftFormerConfig
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_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 SwiftFormerForImageClassification, SwiftFormerModel
						from transformers.models.swiftformer.modeling_swiftformer import SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
						from PIL import Image

						from transformers import ViTImageProcessor


class 				_a						:





							'''simple docstring'''

							def __init__(							self ,					__UpperCAmelCase ,					__UpperCAmelCase=13 ,					__UpperCAmelCase=3 ,					__UpperCAmelCase=True ,					__UpperCAmelCase=True ,					__UpperCAmelCase=0.1 ,					__UpperCAmelCase=0.1 ,					__UpperCAmelCase=224 ,					__UpperCAmelCase=1000 ,					__UpperCAmelCase=[3, 3, 6, 4] ,					__UpperCAmelCase=[48, 56, 112, 220] ,					):







											"""simple docstring"""



											a__	:							Tuple      						= parent
											a__	:							Union[str, Any]      						= batch_size
											a__	:							Dict      						= num_channels
											a__	:							str      						= is_training
											a__	:							Optional[int]      						= use_labels
											a__	:							Any      						= hidden_dropout_prob
											a__	:							List[str]      						= attention_probs_dropout_prob
											a__	:							int      						= num_labels
											a__	:							List[Any]      						= image_size
											a__	:							Optional[Any]      						= layer_depths
											a__	:							Dict      						= embed_dims



							def 		_A	(							self				):







											"""simple docstring"""



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

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

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

											return config, pixel_values, labels



							def 		_A	(							self				):







											"""simple docstring"""



											return SwiftFormerConfig(
											    depths=self.layer_depths ,					embed_dims=self.embed_dims ,					mlp_ratio=4 ,					downsamples=[True, True, True, True] ,					hidden_act="gelu" ,					num_labels=self.num_labels ,					down_patch_size=3 ,					down_stride=2 ,					down_pad=1 ,					drop_rate=0.0 ,					drop_path_rate=0.0 ,					use_layer_scale=__UpperCAmelCase ,					layer_scale_init_value=1E-5 ,					)



							def 		_A	(							self ,					__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase				):







											"""simple docstring"""



											a__	:							Optional[int]      						= SwiftFormerModel(config=__UpperCAmelCase				)
											model.to(__UpperCAmelCase				)
											model.eval()
											a__	:							Optional[int]      						= model(__UpperCAmelCase				)
											self.parent.assertEqual(result.last_hidden_state.shape ,					(self.batch_size, self.embed_dims[-1], 7, 7)				)



							def 		_A	(							self ,					__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase				):







											"""simple docstring"""



											a__	:							Any      						= self.num_labels
											a__	:							int      						= SwiftFormerForImageClassification(__UpperCAmelCase				)
											model.to(__UpperCAmelCase				)
											model.eval()
											a__	:							List[Any]      						= model(__UpperCAmelCase ,					labels=__UpperCAmelCase				)
											self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.num_labels)				)

											a__	:							Dict      						= SwiftFormerForImageClassification(__UpperCAmelCase				)
											model.to(__UpperCAmelCase				)
											model.eval()

											a__	:							List[Any]      						= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)
											a__	:							str      						= model(__UpperCAmelCase				)
											self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.num_labels)				)




							def 		_A	(							self				):







											"""simple docstring"""



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





@require_torch
class 				_a						(       SCREAMING_SNAKE_CASE       ,  SCREAMING_SNAKE_CASE       ,  unittest.TestCase			):





							'''simple docstring'''

							A				:Optional[Any] =       (SwiftFormerModel, SwiftFormerForImageClassification) if is_torch_available() else ()
							A				:Optional[int] =       (
							    {"feature-extraction": SwiftFormerModel, "image-classification": SwiftFormerForImageClassification}
							    if is_torch_available()
							    else {}
							)

							A				:int =       False
							A				:int =       False
							A				:Dict =       False
							A				:int =       False
							A				:List[Any] =       False



							def 		_A	(							self				):







											"""simple docstring"""



											a__	:							List[Any]      						= SwiftFormerModelTester(self				)
											a__	:							Tuple      						= ConfigTester(
											    self ,					config_class=__UpperCAmelCase ,					has_text_modality=__UpperCAmelCase ,					hidden_size=37 ,					num_attention_heads=12 ,					num_hidden_layers=12 ,					)



							def 		_A	(							self				):







											"""simple docstring"""



											self.config_tester.run_common_tests()



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







											"""simple docstring"""



											pass



							def 		_A	(							self				):







											"""simple docstring"""



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

											for model_class in self.all_model_classes:
															a__	:							int      						= model_class(__UpperCAmelCase				)
															a__	:							Optional[Any]      						= model.get_output_embeddings()
															self.assertTrue(x is None or isinstance(__UpperCAmelCase ,					nn.Linear				)				)



							def 		_A	(							self				):







											"""simple docstring"""



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

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

															a__	:							Optional[int]      						= ["pixel_values"]
															self.assertListEqual(arg_names[:1] ,					__UpperCAmelCase				)



							def 		_A	(							self				):







											"""simple docstring"""



											a__	:							Union[str, Any]      						= self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*__UpperCAmelCase				)



							def 		_A	(							self				):







											"""simple docstring"""



											a__	:							int      						= self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*__UpperCAmelCase				)



							@slow
							def 		_A	(							self				):







											"""simple docstring"""



											for model_name in SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															a__	:							Any      						= SwiftFormerModel.from_pretrained(__UpperCAmelCase				)
															self.assertIsNotNone(__UpperCAmelCase				)



							@unittest.skip(reason="SwiftFormer does not output attentions"				)
							def 		_A	(							self				):







											"""simple docstring"""



											pass



							def 		_A	(							self				):







											"""simple docstring"""



											def check_hidden_states_output(__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase				):
															a__	:							Any      						= model_class(__UpperCAmelCase				)
															model.to(__UpperCAmelCase				)
															model.eval()

															with torch.no_grad():
																			a__	:							Tuple      						= model(**self._prepare_for_class(__UpperCAmelCase ,					__UpperCAmelCase				)				)

															a__	:							List[str]      						= outputs.hidden_states

															a__	:							str      						= 8
															self.assertEqual(len(__UpperCAmelCase				) ,					__UpperCAmelCase				)  # TODO

															# SwiftFormer's feature maps are of shape (batch_size, embed_dims, height, width)
															# with the width and height being successively divided by 2, after every 2 blocks
															for i in range(len(__UpperCAmelCase				)				):
																			self.assertEqual(
																			    hidden_states[i].shape ,					torch.Size(
																			        [
																			            self.model_tester.batch_size,
																			            self.model_tester.embed_dims[i // 2],
																			            (self.model_tester.image_size // 4) // 2 ** (i // 2),
																			            (self.model_tester.image_size // 4) // 2 ** (i // 2),
																			        ]				) ,					)

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

											for model_class in self.all_model_classes:
															a__	:							List[Any]      						= True
															check_hidden_states_output(__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase				)

															# check that output_hidden_states also work using config
															del inputs_dict["output_hidden_states"]
															a__	:							Union[str, Any]      						= True

															check_hidden_states_output(__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase				)



							def 		_A	(							self				):







											"""simple docstring"""



											def _config_zero_init(__UpperCAmelCase				):
															a__	:							int      						= copy.deepcopy(__UpperCAmelCase				)
															for key in configs_no_init.__dict__.keys():
																			if "_range" in key or "_std" in key or "initializer_factor" in key or "layer_scale" in key:
																							setattr(__UpperCAmelCase ,					__UpperCAmelCase ,					1E-10				)
																			if isinstance(getattr(__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase				) ,					__UpperCAmelCase				):
																							a__	:							Optional[Any]      						= _config_zero_init(getattr(__UpperCAmelCase ,					__UpperCAmelCase				)				)
																							setattr(__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase				)
															return configs_no_init

											a__						,   a__	:							Tuple      						= self.model_tester.prepare_config_and_inputs_for_common()

											a__	:							List[str]      						= _config_zero_init(__UpperCAmelCase				)
											for model_class in self.all_model_classes:
															a__	:							Dict      						= model_class(config=__UpperCAmelCase				)
															for name, param in model.named_parameters():
																			if param.requires_grad:
																							self.assertIn(
																							    ((param.data.mean() * 1E9) / 1E9).round().item() ,					[0.0, 1.0] ,					msg=f'Parameter {name} of model {model_class} seems not properly initialized' ,					)




							@unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."				)
							def 		_A	(							self				):







											"""simple docstring"""



											pass






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






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





							'''simple docstring'''

							@cached_property
							def 		_A	(							self				):







											"""simple docstring"""



											return ViTImageProcessor.from_pretrained("MBZUAI/swiftformer-xs"				) if is_vision_available() else None




							@slow
							def 		_A	(							self				):







											"""simple docstring"""



											a__	:							int      						= SwiftFormerForImageClassification.from_pretrained("MBZUAI/swiftformer-xs"				).to(__UpperCAmelCase				)

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

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

											# verify the logits
											a__	:							List[str]      						= torch.Size((1, 1000)				)
											self.assertEqual(outputs.logits.shape ,					__UpperCAmelCase				)

											a__	:							int      						= torch.tensor([[-2.1703E00, 2.1107E00, -2.0811E00]]				).to(__UpperCAmelCase				)
											self.assertTrue(torch.allclose(outputs.logits[0, :3] ,					__UpperCAmelCase ,					atol=1E-4				)				)







import operator as op


lowerCamelCase		       =      """scaler.pt"""
lowerCamelCase		       =      """pytorch_model"""
lowerCamelCase		       =      """random_states"""
lowerCamelCase		       =      """optimizer"""
lowerCamelCase		       =      """scheduler"""
lowerCamelCase		       =      """pytorch_model.bin"""
lowerCamelCase		       =      """pytorch_model.bin.index.json"""
lowerCamelCase		       =      """model.safetensors"""
lowerCamelCase		       =      """model.safetensors.index.json"""
lowerCamelCase		       =      """1.10.2"""
lowerCamelCase		       =      """py38"""
lowerCamelCase		       =      """4.17.0"""
lowerCamelCase		       =      ["""ml.p3.16xlarge""", """ml.p3dn.24xlarge""", """ml.p4dn.24xlarge"""]
lowerCamelCase		       =      ["""FULL_SHARD""", """SHARD_GRAD_OP""", """NO_SHARD""", """HYBRID_SHARD""", """HYBRID_SHARD_ZERO2"""]
lowerCamelCase		       =      ["""TRANSFORMER_BASED_WRAP""", """SIZE_BASED_WRAP""", """NO_WRAP"""]
lowerCamelCase		       =      ["""BACKWARD_PRE""", """BACKWARD_POST""", """NO_PREFETCH"""]
lowerCamelCase		       =      ["""FULL_STATE_DICT""", """LOCAL_STATE_DICT""", """SHARDED_STATE_DICT"""]
lowerCamelCase		       =      """2.0.1"""
lowerCamelCase		       =      ["""pdsh""", """standard""", """openmpi""", """mvapich"""]
lowerCamelCase		       =      ["""default""", """reduce-overhead""", """max-autotune"""]

lowerCamelCase		       =      {""">""": op.gt, """>=""": op.ge, """==""": op.eq, """!=""": op.ne, """<=""": op.le, """<""": op.lt}

# These are the args for `torch.distributed.launch` for pytorch < 1.9
lowerCamelCase		       =      [
    """nnodes""",
    """nproc_per_node""",
    """rdzv_backend""",
    """rdzv_endpoint""",
    """rdzv_id""",
    """rdzv_conf""",
    """standalone""",
    """max_restarts""",
    """monitor_interval""",
    """start_method""",
    """role""",
    """module""",
    """m""",
    """no_python""",
    """run_path""",
    """log_dir""",
    """r""",
    """redirects""",
    """t""",
    """tee""",
    """node_rank""",
    """master_addr""",
    """master_port""",
]

lowerCamelCase		       =      ["""DEEPSPEED""", """MULTI_GPU""", """FSDP""", """MEGATRON_LM"""]
lowerCamelCase		       =      ["""DEEPSPEED""", """MULTI_XPU""", """FSDP"""]


