




'''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 			_a  :
       def __init__(	self						:	Optional[int]   ,				lowercase						:	Union[str, Any]   ,				lowercase						:	int=13   ,				lowercase						:	List[Any]=3   ,				lowercase						:	Union[str, Any]=True   ,				lowercase						:	Any=True   ,				lowercase						:	Optional[int]=0.1   ,				lowercase						:	List[Any]=0.1   ,				lowercase						:	Optional[Any]=224   ,				lowercase						:	int=1_000   ,				lowercase						:	Dict=[3, 3, 6, 4]   ,				lowercase						:	str=[48, 56, 112, 220]   ,				):



           '''simple docstring'''

           UpperCAmelCase			       =		parent
           UpperCAmelCase			       =		batch_size
           UpperCAmelCase			       =		num_channels
           UpperCAmelCase			       =		is_training
           UpperCAmelCase			       =		use_labels
           UpperCAmelCase			       =		hidden_dropout_prob
           UpperCAmelCase			       =		attention_probs_dropout_prob
           UpperCAmelCase			       =		num_labels
           UpperCAmelCase			       =		image_size
           UpperCAmelCase			       =		layer_depths
           UpperCAmelCase			       =		embed_dims
       def     A      (	self						:	str				):



           '''simple docstring'''

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

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

           UpperCAmelCase			       =		self.get_config()

           return config, pixel_values, labels
       def     A      (	self						:	Optional[Any]				):



           '''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=lowercase   ,				layer_scale_init_value=1E-5   ,				)
       def     A      (	self						:	Tuple   ,				lowercase						:	Any   ,				lowercase						:	Optional[int]   ,				lowercase						:	Optional[int]				):



           '''simple docstring'''

           UpperCAmelCase			       =		SwiftFormerModel(config=lowercase				)
           model.to(lowercase				)
           model.eval()
           UpperCAmelCase			       =		model(lowercase				)
           self.parent.assertEqual(result.last_hidden_state.shape   ,				(self.batch_size, self.embed_dims[-1], 7, 7)				)
       def     A      (	self						:	str   ,				lowercase						:	Optional[Any]   ,				lowercase						:	List[Any]   ,				lowercase						:	Union[str, Any]				):



           '''simple docstring'''

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

           UpperCAmelCase			       =		SwiftFormerForImageClassification(lowercase				)
           model.to(lowercase				)
           model.eval()

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







       def     A      (	self						:	List[Any]				):



           '''simple docstring'''

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






@require_torch
class 			_a  (	lowercase_							,							lowercase_							,							unittest.TestCase ):
       __a				:     int   				=					(SwiftFormerModel, SwiftFormerForImageClassification) if is_torch_available() else ()
       __a				:     int   				=					(
           {"""feature-extraction""": SwiftFormerModel, """image-classification""": SwiftFormerForImageClassification}
           if is_torch_available()
           else {}
       )

       __a				:     str   				=					False
       __a				:     Dict   				=					False
       __a				:     str   				=					False
       __a				:     Tuple   				=					False
       __a				:     List[str]   				=					False
       def     A      (	self						:	Any				):



           '''simple docstring'''

           UpperCAmelCase			       =		SwiftFormerModelTester(self				)
           UpperCAmelCase			       =		ConfigTester(
               self   ,				config_class=lowercase   ,				has_text_modality=lowercase   ,				hidden_size=37   ,				num_attention_heads=12   ,				num_hidden_layers=12   ,				)
       def     A      (	self						:	List[str]				):



           '''simple docstring'''

           self.config_tester.run_common_tests()
       @unittest.skip(reason='''SwiftFormer does not use inputs_embeds'''				)
       def     A      (	self						:	List[Any]				):



           '''simple docstring'''

           pass
       def     A      (	self						:	Optional[Any]				):



           '''simple docstring'''

           UpperCAmelCase       ,     UpperCAmelCase			       =		self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
               UpperCAmelCase			       =		model_class(lowercase				)
               UpperCAmelCase			       =		model.get_output_embeddings()
               self.assertTrue(x is None or isinstance(lowercase   ,				nn.Linear				)				)
       def     A      (	self						:	Union[str, Any]				):



           '''simple docstring'''

           UpperCAmelCase       ,     UpperCAmelCase			       =		self.model_tester.prepare_config_and_inputs_for_common()

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

               UpperCAmelCase			       =		['''pixel_values''']
               self.assertListEqual(arg_names[:1]   ,				lowercase				)
       def     A      (	self						:	Dict				):



           '''simple docstring'''

           UpperCAmelCase			       =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*lowercase				)
       def     A      (	self						:	Any				):



           '''simple docstring'''

           UpperCAmelCase			       =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_classification(*lowercase				)
       @slow
       def     A      (	self						:	Dict				):



           '''simple docstring'''

           for model_name in SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               UpperCAmelCase			       =		SwiftFormerModel.from_pretrained(lowercase				)
               self.assertIsNotNone(lowercase				)
       @unittest.skip(reason='''SwiftFormer does not output attentions'''				)
       def     A      (	self						:	Dict				):



           '''simple docstring'''

           pass
       def     A      (	self						:	int				):



           '''simple docstring'''

           def check_hidden_states_output(lowercase						:	str   ,				lowercase						:	Optional[Any]   ,				lowercase						:	str				):
               UpperCAmelCase			       =		model_class(lowercase				)
               model.to(lowercase				)
               model.eval()

               with torch.no_grad():
                   UpperCAmelCase			       =		model(**self._prepare_for_class(lowercase   ,				lowercase				)				)

               UpperCAmelCase			       =		outputs.hidden_states

               UpperCAmelCase			       =		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),
                           ]				)   ,				)

           UpperCAmelCase       ,     UpperCAmelCase			       =		self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
               UpperCAmelCase			       =		True
               check_hidden_states_output(lowercase   ,				lowercase   ,				lowercase				)

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

               check_hidden_states_output(lowercase   ,				lowercase   ,				lowercase				)
       def     A      (	self						:	Optional[int]				):



           '''simple docstring'''

           def _config_zero_init(lowercase						:	int				):
               UpperCAmelCase			       =		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-10				)
                   if isinstance(getattr(lowercase   ,				lowercase   ,				lowercase				)   ,				lowercase				):
                       UpperCAmelCase			       =		_config_zero_init(getattr(lowercase   ,				lowercase				)				)
                       setattr(lowercase   ,				lowercase   ,				lowercase				)
               return configs_no_init

           UpperCAmelCase       ,     UpperCAmelCase			       =		self.model_tester.prepare_config_and_inputs_for_common()

           UpperCAmelCase			       =		_config_zero_init(lowercase				)
           for model_class in self.all_model_classes:
               UpperCAmelCase			       =		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						:	Dict				):



           '''simple docstring'''

           pass






def 						snake_case_							():
    UpperCAmelCase			       =		Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''     )
    return image







@require_torch
@require_vision
class 			_a  (	unittest.TestCase ):
       @cached_property
       def     A      (	self						:	Dict				):



           '''simple docstring'''

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







       @slow
       def     A      (	self						:	Any				):



           '''simple docstring'''

           UpperCAmelCase			       =		SwiftFormerForImageClassification.from_pretrained('''MBZUAI/swiftformer-xs'''				).to(lowercase				)

           UpperCAmelCase			       =		self.default_image_processor
           UpperCAmelCase			       =		prepare_img()
           UpperCAmelCase			       =		image_processor(images=lowercase   ,				return_tensors='''pt'''				).to(lowercase				)

           # forward pass
           with torch.no_grad():
               UpperCAmelCase			       =		model(**lowercase				)

           # verify the logits
           UpperCAmelCase			       =		torch.Size((1, 1_000)				)
           self.assertEqual(outputs.logits.shape   ,				lowercase				)

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








'''simple docstring'''

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


lowerCamelCase							:   Dict  						=	logging.get_logger(__name__)

lowerCamelCase							:   Union[str, Any]  						=	{
    # See all MEGATRON_BERT models at https://huggingface.co/models?filter=bert
}
class   __lowerCAmelCase						(lowercase_     ):





      '''simple docstring'''






      lowerCAmelCase__					:     List[str]  					=							"""megatron-bert"""



      def __init__(self    :    Tuple   ,						UpperCamelCase    :    Optional[int]=29056   ,						UpperCamelCase    :    Optional[Any]=1024   ,						UpperCamelCase    :    Any=24   ,						UpperCamelCase    :    int=16   ,						UpperCamelCase    :    Optional[int]=4096   ,						UpperCamelCase    :    int="gelu"   ,						UpperCamelCase    :    int=0.1   ,						UpperCamelCase    :    Tuple=0.1   ,						UpperCamelCase    :    Any=512   ,						UpperCamelCase    :    int=2   ,						UpperCamelCase    :    Dict=0.02   ,						UpperCamelCase    :    Dict=1E-12   ,						UpperCamelCase    :    List[Any]=0   ,						UpperCamelCase    :    Optional[int]="absolute"   ,						UpperCamelCase    :    List[Any]=True   ,						**UpperCamelCase    :    str   ,						):

             '''simple docstring'''







             super().__init__(pad_token_id=UpperCamelCase   ,						**UpperCamelCase )

             lowercase__      			= vocab_size
             lowercase__      			= hidden_size
             lowercase__      			= num_hidden_layers
             lowercase__      			= num_attention_heads
             lowercase__      			= hidden_act
             lowercase__      			= intermediate_size
             lowercase__      			= hidden_dropout_prob
             lowercase__      			= attention_probs_dropout_prob
             lowercase__      			= max_position_embeddings
             lowercase__      			= type_vocab_size
             lowercase__      			= initializer_range
             lowercase__      			= layer_norm_eps
             lowercase__      			= position_embedding_type
             lowercase__      			= use_cache




