





'''simple docstring'''





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   UpperCamelCase_       :


							def __init__(							self    :int							,				__A    :Tuple							,				__A    :List[Any]=13							,				__A    :List[str]=3							,				__A    :int=True							,				__A    :str=True							,				__A    :int=0.1							,				__A    :Optional[int]=0.1							,				__A    :int=224							,				__A    :Tuple=1000							,				__A    :Any=[3, 3, 6, 4]							,				__A    :List[str]=[48, 56, 112, 220]							,				)   ->   str:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      parent
									SCREAMING_SNAKE_CASE__			=      batch_size
									SCREAMING_SNAKE_CASE__			=      num_channels
									SCREAMING_SNAKE_CASE__			=      is_training
									SCREAMING_SNAKE_CASE__			=      use_labels
									SCREAMING_SNAKE_CASE__			=      hidden_dropout_prob
									SCREAMING_SNAKE_CASE__			=      attention_probs_dropout_prob
									SCREAMING_SNAKE_CASE__			=      num_labels
									SCREAMING_SNAKE_CASE__			=      image_size
									SCREAMING_SNAKE_CASE__			=      layer_depths
									SCREAMING_SNAKE_CASE__			=      embed_dims


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



									"""simple docstring"""



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

									SCREAMING_SNAKE_CASE__			=      None
									if self.use_labels:
											SCREAMING_SNAKE_CASE__			=      ids_tensor([self.batch_size]							,				self.num_labels		)

									SCREAMING_SNAKE_CASE__			=      self.get_config()

									return config, pixel_values, labels


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



									"""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=__lowerCamelCase							,				layer_scale_init_value=1E-5							,				)


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



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      SwiftFormerModel(config=__lowerCamelCase		)
									model.to(__lowerCamelCase		)
									model.eval()
									SCREAMING_SNAKE_CASE__			=      model(__lowerCamelCase		)
									self.parent.assertEqual(result.last_hidden_state.shape							,				(self.batch_size, self.embed_dims[-1], 7, 7)		)


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



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      self.num_labels
									SCREAMING_SNAKE_CASE__			=      SwiftFormerForImageClassification(__lowerCamelCase		)
									model.to(__lowerCamelCase		)
									model.eval()
									SCREAMING_SNAKE_CASE__			=      model(__lowerCamelCase							,				labels=__lowerCamelCase		)
									self.parent.assertEqual(result.logits.shape							,				(self.batch_size, self.num_labels)		)

									SCREAMING_SNAKE_CASE__			=      SwiftFormerForImageClassification(__lowerCamelCase		)
									model.to(__lowerCamelCase		)
									model.eval()

									SCREAMING_SNAKE_CASE__			=      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)
									SCREAMING_SNAKE_CASE__			=      model(__lowerCamelCase		)
									self.parent.assertEqual(result.logits.shape							,				(self.batch_size, self.num_labels)		)


							def      _snake_case  (							self    :Any		)   ->   Any:



									"""simple docstring"""



									(SCREAMING_SNAKE_CASE__)			=      self.prepare_config_and_inputs()
									SCREAMING_SNAKE_CASE__			=      {'''pixel_values''': pixel_values}
									return config, inputs_dict




@require_torch
class   UpperCamelCase_       (     lowerCamelCase__				,  lowerCamelCase__				,  unittest.TestCase    ):
							lowerCamelCase_           =					(SwiftFormerModel, SwiftFormerForImageClassification) if is_torch_available() else ()
							lowerCamelCase_           =					(
							    {"feature-extraction": SwiftFormerModel, "image-classification": SwiftFormerForImageClassification}
							    if is_torch_available()
							    else {}
							)

							lowerCamelCase_           =					False
							lowerCamelCase_           =					False
							lowerCamelCase_           =					False
							lowerCamelCase_           =					False
							lowerCamelCase_           =					False


							def      _snake_case  (							self    :Dict		)   ->   Any:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      SwiftFormerModelTester(self		)
									SCREAMING_SNAKE_CASE__			=      ConfigTester(
									    self							,				config_class=__lowerCamelCase							,				has_text_modality=__lowerCamelCase							,				hidden_size=37							,				num_attention_heads=12							,				num_hidden_layers=12							,				)


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



									"""simple docstring"""



									self.config_tester.run_common_tests()


							@unittest.skip(reason="""SwiftFormer does not use inputs_embeds"""		)
							def      _snake_case  (							self    :List[str]		)   ->   List[Any]:



									"""simple docstring"""



									pass


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



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
											SCREAMING_SNAKE_CASE__			=      model_class(__lowerCamelCase		)
											SCREAMING_SNAKE_CASE__			=      model.get_output_embeddings()
											self.assertTrue(x is None or isinstance(__lowerCamelCase							,				nn.Linear		)		)


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



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      self.model_tester.prepare_config_and_inputs_for_common()

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

											SCREAMING_SNAKE_CASE__			=      ['''pixel_values''']
											self.assertListEqual(arg_names[:1]							,				__lowerCamelCase		)


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



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*__lowerCamelCase		)


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



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*__lowerCamelCase		)


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



									"""simple docstring"""



									for model_name in SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											SCREAMING_SNAKE_CASE__			=      SwiftFormerModel.from_pretrained(__lowerCamelCase		)
											self.assertIsNotNone(__lowerCamelCase		)


							@unittest.skip(reason="""SwiftFormer does not output attentions"""		)
							def      _snake_case  (							self    :Dict		)   ->   Union[str, Any]:



									"""simple docstring"""



									pass


							def      _snake_case  (							self    :int		)   ->   Tuple:



									"""simple docstring"""



									def check_hidden_states_output(__A    :Any							,				__A    :Tuple							,				__A    :int		):
											SCREAMING_SNAKE_CASE__			=      model_class(__lowerCamelCase		)
											model.to(__lowerCamelCase		)
											model.eval()

											with torch.no_grad():
													SCREAMING_SNAKE_CASE__			=      model(**self._prepare_for_class(__lowerCamelCase							,				__lowerCamelCase		)		)

											SCREAMING_SNAKE_CASE__			=      outputs.hidden_states

											SCREAMING_SNAKE_CASE__			=      8
											self.assertEqual(len(__lowerCamelCase		)							,				__lowerCamelCase		)  # 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(__lowerCamelCase		)		):
													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),
													        ]		)							,				)

									SCREAMING_SNAKE_CASE__			=      self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
											SCREAMING_SNAKE_CASE__			=      True
											check_hidden_states_output(__lowerCamelCase							,				__lowerCamelCase							,				__lowerCamelCase		)

											# check that output_hidden_states also work using config
											del inputs_dict["output_hidden_states"]
											SCREAMING_SNAKE_CASE__			=      True

											check_hidden_states_output(__lowerCamelCase							,				__lowerCamelCase							,				__lowerCamelCase		)


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



									"""simple docstring"""



									def _config_zero_init(__A    :Tuple		):
											SCREAMING_SNAKE_CASE__			=      copy.deepcopy(__lowerCamelCase		)
											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(__lowerCamelCase							,				__lowerCamelCase							,				1E-10		)
													if isinstance(getattr(__lowerCamelCase							,				__lowerCamelCase							,				__lowerCamelCase		)							,				__lowerCamelCase		):
															SCREAMING_SNAKE_CASE__			=      _config_zero_init(getattr(__lowerCamelCase							,				__lowerCamelCase		)		)
															setattr(__lowerCamelCase							,				__lowerCamelCase							,				__lowerCamelCase		)
											return configs_no_init

									SCREAMING_SNAKE_CASE__			=      self.model_tester.prepare_config_and_inputs_for_common()

									SCREAMING_SNAKE_CASE__			=      _config_zero_init(__lowerCamelCase		)
									for model_class in self.all_model_classes:
											SCREAMING_SNAKE_CASE__			=      model_class(config=__lowerCamelCase		)
											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      _snake_case  (							self    :str		)   ->   Tuple:



									"""simple docstring"""



									pass






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




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


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



									"""simple docstring"""



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


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



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      SwiftFormerForImageClassification.from_pretrained("""MBZUAI/swiftformer-xs"""		).to(__lowerCamelCase		)

									SCREAMING_SNAKE_CASE__			=      self.default_image_processor
									SCREAMING_SNAKE_CASE__			=      prepare_img()
									SCREAMING_SNAKE_CASE__			=      image_processor(images=__lowerCamelCase							,				return_tensors="""pt"""		).to(__lowerCamelCase		)

									# forward pass
									with torch.no_grad():
											SCREAMING_SNAKE_CASE__			=      model(**__lowerCamelCase		)

									# verify the logits
									SCREAMING_SNAKE_CASE__			=      torch.Size((1, 1000)		)
									self.assertEqual(outputs.logits.shape							,				__lowerCamelCase		)

									SCREAMING_SNAKE_CASE__			=      torch.tensor([[-2.1_703E00, 2.1_107E00, -2.0_811E00]]		).to(__lowerCamelCase		)
									self.assertTrue(torch.allclose(outputs.logits[0, :3]							,				__lowerCamelCase							,				atol=1E-4		)		)






from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


_lowerCamelCase   				=			logging.get_logger(__name__)

_lowerCamelCase   				=			{
    'shi-labs/nat-mini-in1k-224': 'https://huggingface.co/shi-labs/nat-mini-in1k-224/resolve/main/config.json',
    # See all Nat models at https://huggingface.co/models?filter=nat
}




class   UpperCamelCase_       (     UpperCamelCase__				,  UpperCamelCase__    ):
							lowerCamelCase_           =					"nat"

							lowerCamelCase_           =					{
							    "num_attention_heads": "num_heads",
							    "num_hidden_layers": "num_layers",
							}


							def __init__(							self    :List[Any]							,				__A    :Optional[Any]=4							,				__A    :Any=3							,				__A    :Optional[int]=64							,				__A    :Optional[int]=[3, 4, 6, 5]							,				__A    :Union[str, Any]=[2, 4, 8, 16]							,				__A    :Optional[Any]=7							,				__A    :Optional[Any]=3.0							,				__A    :List[Any]=True							,				__A    :int=0.0							,				__A    :Dict=0.0							,				__A    :Optional[Any]=0.1							,				__A    :str="gelu"							,				__A    :Optional[Any]=0.0_2							,				__A    :Optional[int]=1E-5							,				__A    :Optional[int]=0.0							,				__A    :Optional[Any]=None							,				__A    :Union[str, Any]=None							,				**__A    :Union[str, Any]							,				)   ->   Optional[int]:



									"""simple docstring"""



									super().__init__(**__A		)

									SCREAMING_SNAKE_CASE__			=      patch_size
									SCREAMING_SNAKE_CASE__			=      num_channels
									SCREAMING_SNAKE_CASE__			=      embed_dim
									SCREAMING_SNAKE_CASE__			=      depths
									SCREAMING_SNAKE_CASE__			=      len(__A		)
									SCREAMING_SNAKE_CASE__			=      num_heads
									SCREAMING_SNAKE_CASE__			=      kernel_size
									SCREAMING_SNAKE_CASE__			=      mlp_ratio
									SCREAMING_SNAKE_CASE__			=      qkv_bias
									SCREAMING_SNAKE_CASE__			=      hidden_dropout_prob
									SCREAMING_SNAKE_CASE__			=      attention_probs_dropout_prob
									SCREAMING_SNAKE_CASE__			=      drop_path_rate
									SCREAMING_SNAKE_CASE__			=      hidden_act
									SCREAMING_SNAKE_CASE__			=      layer_norm_eps
									SCREAMING_SNAKE_CASE__			=      initializer_range
									# we set the hidden_size attribute in order to make Nat work with VisionEncoderDecoderModel
									# this indicates the channel dimension after the last stage of the model
									SCREAMING_SNAKE_CASE__			=      int(embed_dim * 2 ** (len(__A		) - 1)		)
									SCREAMING_SNAKE_CASE__			=      layer_scale_init_value
									SCREAMING_SNAKE_CASE__			=      ["""stem"""] + [f'''stage{idx}''' for idx in range(1							,				len(__A		) + 1		)]
									SCREAMING_SNAKE_CASE__	,						SCREAMING_SNAKE_CASE__			=      get_aligned_output_features_output_indices(
									    out_features=__A							,				out_indices=__A							,				stage_names=self.stage_names		)