



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




				def __init__(  self   :  Optional[Any],   __lowercase   :  Optional[Any],   __lowercase   :  Any=13,   __lowercase   :  Optional[Any]=3,   __lowercase   :  str=True,   __lowercase   :  Union[str, Any]=True,   __lowercase   :  int=0.1,   __lowercase   :  Any=0.1,   __lowercase   :  Optional[int]=224,   __lowercase   :  Tuple=1000,   __lowercase   :  Dict=[3, 3, 6, 4],   __lowercase   :  str=[48, 56, 112, 220],   ):
							lowercase__   					=			parent
							lowercase__   					=			batch_size
							lowercase__   					=			num_channels
							lowercase__   					=			is_training
							lowercase__   					=			use_labels
							lowercase__   					=			hidden_dropout_prob
							lowercase__   					=			attention_probs_dropout_prob
							lowercase__   					=			num_labels
							lowercase__   					=			image_size
							lowercase__   					=			layer_depths
							lowercase__   					=			embed_dims




				def 							A__ (  self   :  Tuple  ):
							lowercase__   					=			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

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

							lowercase__   					=			self.get_config()

							return config, pixel_values, labels




				def 							A__ (  self   :  List[Any]  ):
							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=__lowercase,   layer_scale_init_value=1e-5,   )




				def 							A__ (  self   :  str,   __lowercase   :  Optional[Any],   __lowercase   :  Tuple,   __lowercase   :  Union[str, Any]  ):
							lowercase__   					=			SwiftFormerModel(config=__lowercase  )
							model.to(__lowercase  )
							model.eval()
							lowercase__   					=			model(__lowercase  )
							self.parent.assertEqual(result.last_hidden_state.shape,   (self.batch_size, self.embed_dims[-1], 7, 7)  )




				def 							A__ (  self   :  Optional[int],   __lowercase   :  Optional[Any],   __lowercase   :  Dict,   __lowercase   :  Dict  ):
							lowercase__   					=			self.num_labels
							lowercase__   					=			SwiftFormerForImageClassification(__lowercase  )
							model.to(__lowercase  )
							model.eval()
							lowercase__   					=			model(__lowercase,   labels=__lowercase  )
							self.parent.assertEqual(result.logits.shape,   (self.batch_size, self.num_labels)  )

							lowercase__   					=			SwiftFormerForImageClassification(__lowercase  )
							model.to(__lowercase  )
							model.eval()

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




				def 							A__ (  self   :  Optional[Any]  ):
							((lowercase__)					,    (lowercase__)					,    (lowercase__))   					=			self.prepare_config_and_inputs()
							lowercase__   					=			{"pixel_values": pixel_values}
							return config, inputs_dict
@require_torch
class 			_snake_case     (   lowercase__      ,		lowercase__      ,		unittest.TestCase):
				UpperCamelCase__			:   int       						=(SwiftFormerModel, SwiftFormerForImageClassification) if is_torch_available() else ()
				UpperCamelCase__			:   Optional[int]       						=(
				    {"""feature-extraction""": SwiftFormerModel, """image-classification""": SwiftFormerForImageClassification}
				    if is_torch_available()
				    else {}
				)

				UpperCamelCase__			:   Dict       						=False
				UpperCamelCase__			:   List[Any]       						=False
				UpperCamelCase__			:   Optional[Any]       						=False
				UpperCamelCase__			:   int       						=False
				UpperCamelCase__			:   Union[str, Any]       						=False




				def 							A__ (  self   :  Dict  ):
							lowercase__   					=			SwiftFormerModelTester(self  )
							lowercase__   					=			ConfigTester(
							    self,   config_class=__lowercase,   has_text_modality=__lowercase,   hidden_size=37,   num_attention_heads=12,   num_hidden_layers=12,   )




				def 							A__ (  self   :  List[Any]  ):
							self.config_tester.run_common_tests()




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




				def 							A__ (  self   :  Optional[Any]  ):
							lowercase__					,    lowercase__   					=			self.model_tester.prepare_config_and_inputs_for_common()

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




				def 							A__ (  self   :  List[Any]  ):
							lowercase__					,    lowercase__   					=			self.model_tester.prepare_config_and_inputs_for_common()

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

										lowercase__   					=			["pixel_values"]
										self.assertListEqual(arg_names[:1],   __lowercase  )




				def 							A__ (  self   :  Tuple  ):
							lowercase__   					=			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*__lowercase  )




				def 							A__ (  self   :  List[str]  ):
							lowercase__   					=			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*__lowercase  )




				@slow
				def 							A__ (  self   :  str  ):
							for model_name in SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										lowercase__   					=			SwiftFormerModel.from_pretrained(__lowercase  )
										self.assertIsNotNone(__lowercase  )




				@unittest.skip(reason="SwiftFormer does not output attentions"  )
				def 							A__ (  self   :  List[str]  ):
							pass




				def 							A__ (  self   :  Optional[Any]  ):
							def check_hidden_states_output(__lowercase   :  Any,   __lowercase   :  Optional[int],   __lowercase   :  Optional[int]  ):
										lowercase__   					=			model_class(__lowercase  )
										model.to(__lowercase  )
										model.eval()

										with torch.no_grad():
													lowercase__   					=			model(**self._prepare_for_class(__lowercase,   __lowercase  )  )

										lowercase__   					=			outputs.hidden_states

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

							lowercase__					,    lowercase__   					=			self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
										lowercase__   					=			True
										check_hidden_states_output(__lowercase,   __lowercase,   __lowercase  )

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

										check_hidden_states_output(__lowercase,   __lowercase,   __lowercase  )




				def 							A__ (  self   :  Any  ):
							def _config_zero_init(__lowercase   :  List[str]  ):
										lowercase__   					=			copy.deepcopy(__lowercase  )
										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(__lowercase,   __lowercase,   1e-1_0  )
													if isinstance(getattr(__lowercase,   __lowercase,   __lowercase  ),   __lowercase  ):
																lowercase__   					=			_config_zero_init(getattr(__lowercase,   __lowercase  )  )
																setattr(__lowercase,   __lowercase,   __lowercase  )
										return configs_no_init

							lowercase__					,    lowercase__   					=			self.model_tester.prepare_config_and_inputs_for_common()

							lowercase__   					=			_config_zero_init(__lowercase  )
							for model_class in self.all_model_classes:
										lowercase__   					=			model_class(config=__lowercase  )
										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   :  Optional[Any]  ):
							pass



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




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




				@cached_property
				def 							A__ (  self   :  List[str]  ):
							return ViTImageProcessor.from_pretrained("MBZUAI/swiftformer-xs"  ) if is_vision_available() else None




				@slow
				def 							A__ (  self   :  Dict  ):
							lowercase__   					=			SwiftFormerForImageClassification.from_pretrained("MBZUAI/swiftformer-xs"  ).to(__lowercase  )

							lowercase__   					=			self.default_image_processor
							lowercase__   					=			prepare_img()
							lowercase__   					=			image_processor(images=__lowercase,   return_tensors="pt"  ).to(__lowercase  )

							# forward pass
							with torch.no_grad():
										lowercase__   					=			model(**__lowercase  )

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

							lowercase__   					=			torch.tensor([[-2.1_7_0_3e0_0, 2.1_1_0_7e0_0, -2.0_8_1_1e0_0]]  ).to(__lowercase  )
							self.assertTrue(torch.allclose(outputs.logits[0, :3],   __lowercase,   atol=1e-4  )  )









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


lowercase_						=       logging.get_logger(__name__)

lowercase_						=       {
    """facebook/nllb-moe-54B""": """https://huggingface.co/facebook/nllb-moe-54b/resolve/main/config.json""",
}
class 			_snake_case     (   lowercase__):
				UpperCamelCase__			:   Tuple       						="""nllb-moe"""
				UpperCamelCase__			:   Dict       						=["""past_key_values"""]
				UpperCamelCase__			:   List[Any]       						={"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}




				def __init__(  self   :  List[str],   __lowercase   :  str=12_8112,   __lowercase   :  List[Any]=1024,   __lowercase   :  int=12,   __lowercase   :  Union[str, Any]=4096,   __lowercase   :  Union[str, Any]=16,   __lowercase   :  Tuple=12,   __lowercase   :  int=4096,   __lowercase   :  Any=16,   __lowercase   :  List[str]=0.05,   __lowercase   :  Optional[int]=0.05,   __lowercase   :  int=True,   __lowercase   :  Optional[Any]=True,   __lowercase   :  Optional[Any]="relu",   __lowercase   :  str=1024,   __lowercase   :  Optional[Any]=0.1,   __lowercase   :  List[Any]=0.1,   __lowercase   :  Union[str, Any]=0.0,   __lowercase   :  Tuple=0.02,   __lowercase   :  Optional[Any]=2,   __lowercase   :  Optional[int]=True,   __lowercase   :  List[Any]=False,   __lowercase   :  Any="float32",   __lowercase   :  Union[str, Any]=False,   __lowercase   :  Optional[int]=128,   __lowercase   :  Union[str, Any]=64,   __lowercase   :  Optional[int]=4,   __lowercase   :  Dict=4,   __lowercase   :  Any=0.001,   __lowercase   :  Optional[int]=0.001,   __lowercase   :  int="all",   __lowercase   :  Any=False,   __lowercase   :  Optional[int]=False,   __lowercase   :  Dict=1.0,   __lowercase   :  int=0.2,   __lowercase   :  str=1,   __lowercase   :  Optional[Any]=0,   __lowercase   :  Optional[int]=2,   __lowercase   :  Any=False,   **__lowercase   :  List[str],   ):
							lowercase__   					=			vocab_size
							lowercase__   					=			max_position_embeddings
							lowercase__   					=			d_model
							lowercase__   					=			encoder_ffn_dim
							lowercase__   					=			encoder_layers
							lowercase__   					=			encoder_attention_heads
							lowercase__   					=			decoder_ffn_dim
							lowercase__   					=			decoder_layers
							lowercase__   					=			decoder_attention_heads
							lowercase__   					=			dropout
							lowercase__   					=			attention_dropout
							lowercase__   					=			activation_dropout
							lowercase__   					=			activation_function
							lowercase__   					=			init_std
							lowercase__   					=			encoder_layerdrop
							lowercase__   					=			decoder_layerdrop
							lowercase__   					=			use_cache
							lowercase__   					=			encoder_layers
							lowercase__   					=			scale_embedding  # scale factor will be sqrt(d_model) if True
							lowercase__   					=			router_z_loss_coef
							lowercase__   					=			router_aux_loss_coef
							lowercase__   					=			decoder_sparse_step
							lowercase__   					=			encoder_sparse_step
							lowercase__   					=			num_experts
							lowercase__   					=			expert_capacity
							lowercase__   					=			router_bias
							if router_dtype not in ["float32", "float16", "bfloat16"]:
										raise ValueError(F'''`router_dtype` must be one of \'float32\', \'float16\' or \'bfloat16\', got {router_dtype}'''  )
							lowercase__   					=			router_dtype

							lowercase__   					=			router_ignore_padding_tokens
							lowercase__   					=			batch_prioritized_routing
							lowercase__   					=			second_expert_policy
							lowercase__   					=			normalize_router_prob_before_dropping
							lowercase__   					=			moe_eval_capacity_token_fraction
							lowercase__   					=			moe_token_dropout
							lowercase__   					=			output_router_logits
							super().__init__(
							    pad_token_id=__lowercase,   bos_token_id=__lowercase,   eos_token_id=__lowercase,   is_encoder_decoder=__lowercase,   decoder_start_token_id=__lowercase,   **__lowercase,   )




