



'''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        lowerCAmelCase_:
  '''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]  ,)    ->		Dict:
      lowerCAmelCase__ :  Dict							      =					parent
      lowerCAmelCase__ :  str							      =					batch_size
      lowerCAmelCase__ :  Tuple							      =					num_channels
      lowerCAmelCase__ :  Dict							      =					is_training
      lowerCAmelCase__ :  Union[str, Any]							      =					use_labels
      lowerCAmelCase__ :  List[str]							      =					hidden_dropout_prob
      lowerCAmelCase__ :  str							      =					attention_probs_dropout_prob
      lowerCAmelCase__ :  Optional[int]							      =					num_labels
      lowerCAmelCase__ :  List[Any]							      =					image_size
      lowerCAmelCase__ :  Tuple							      =					layer_depths
      lowerCAmelCase__ :  str							      =					embed_dims




  def 							UpperCAmelCase_	(    self      )    ->		Optional[int]:
      lowerCAmelCase__ :  List[Any]							      =					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

      lowerCAmelCase__ :  List[Any]							      =					None
      if self.use_labels:
          lowerCAmelCase__ :  Optional[Any]							      =					ids_tensor([self.batch_size]  ,self.num_labels      )

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

      return config, pixel_values, labels




  def 							UpperCAmelCase_	(    self      )    ->		Dict:
      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 							UpperCAmelCase_	(    self  ,__UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase      )    ->		Tuple:
      lowerCAmelCase__ :  Optional[int]							      =					SwiftFormerModel(config=__UpperCAmelCase      )
      model.to(__UpperCAmelCase      )
      model.eval()
      lowerCAmelCase__ :  List[Any]							      =					model(__UpperCAmelCase      )
      self.parent.assertEqual(result.last_hidden_state.shape  ,(self.batch_size, self.embed_dims[-1], 7, 7)      )




  def 							UpperCAmelCase_	(    self  ,__UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase      )    ->		int:
      lowerCAmelCase__ :  Any							      =					self.num_labels
      lowerCAmelCase__ :  Optional[int]							      =					SwiftFormerForImageClassification(__UpperCAmelCase      )
      model.to(__UpperCAmelCase      )
      model.eval()
      lowerCAmelCase__ :  Optional[Any]							      =					model(__UpperCAmelCase  ,labels=__UpperCAmelCase      )
      self.parent.assertEqual(result.logits.shape  ,(self.batch_size, self.num_labels)      )

      lowerCAmelCase__ :  List[Any]							      =					SwiftFormerForImageClassification(__UpperCAmelCase      )
      model.to(__UpperCAmelCase      )
      model.eval()

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






  def 							UpperCAmelCase_	(    self      )    ->		str:
      ((lowerCAmelCase__)      ,   (lowerCAmelCase__)      ,   (lowerCAmelCase__)) :  str							      =					self.prepare_config_and_inputs()
      lowerCAmelCase__ :  Optional[Any]							      =					{"""pixel_values""": pixel_values}
      return config, inputs_dict







@require_torch
class        lowerCAmelCase_(				SCREAMING_SNAKE_CASE_						, SCREAMING_SNAKE_CASE_						, unittest.TestCase     ):
  '''simple docstring'''




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

  __lowercase							:      List[str]				=      False
  __lowercase							:      str				=      False
  __lowercase							:      str				=      False
  __lowercase							:      List[Any]				=      False
  __lowercase							:      Tuple				=      False




  def 							UpperCAmelCase_	(    self      )    ->		int:
      lowerCAmelCase__ :  Optional[int]							      =					SwiftFormerModelTester(self      )
      lowerCAmelCase__ :  str							      =					ConfigTester(
          self  ,config_class=__UpperCAmelCase  ,has_text_modality=__UpperCAmelCase  ,hidden_size=37  ,num_attention_heads=12  ,num_hidden_layers=12  ,)




  def 							UpperCAmelCase_	(    self      )    ->		str:
      self.config_tester.run_common_tests()




  @unittest.skip(reason="""SwiftFormer does not use inputs_embeds"""      )
  def 							UpperCAmelCase_	(    self      )    ->		Union[str, Any]:
      pass




  def 							UpperCAmelCase_	(    self      )    ->		Union[str, Any]:
      lowerCAmelCase__      ,   lowerCAmelCase__ :  int							      =					self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
          lowerCAmelCase__ :  Dict							      =					model_class(__UpperCAmelCase      )
          lowerCAmelCase__ :  int							      =					model.get_output_embeddings()
          self.assertTrue(x is None or isinstance(__UpperCAmelCase  ,nn.Linear      )      )




  def 							UpperCAmelCase_	(    self      )    ->		str:
      lowerCAmelCase__      ,   lowerCAmelCase__ :  List[Any]							      =					self.model_tester.prepare_config_and_inputs_for_common()

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

          lowerCAmelCase__ :  str							      =					["""pixel_values"""]
          self.assertListEqual(arg_names[:1]  ,__UpperCAmelCase      )




  def 							UpperCAmelCase_	(    self      )    ->		Optional[int]:
      lowerCAmelCase__ :  Optional[Any]							      =					self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*__UpperCAmelCase      )




  def 							UpperCAmelCase_	(    self      )    ->		Optional[Any]:
      lowerCAmelCase__ :  Optional[Any]							      =					self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*__UpperCAmelCase      )




  @slow
  def 							UpperCAmelCase_	(    self      )    ->		int:
      for model_name in SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          lowerCAmelCase__ :  Optional[int]							      =					SwiftFormerModel.from_pretrained(__UpperCAmelCase      )
          self.assertIsNotNone(__UpperCAmelCase      )




  @unittest.skip(reason="""SwiftFormer does not output attentions"""      )
  def 							UpperCAmelCase_	(    self      )    ->		Any:
      pass




  def 							UpperCAmelCase_	(    self      )    ->		Any:
      def check_hidden_states_output(__UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase      ):
          lowerCAmelCase__ :  Optional[int]							      =					model_class(__UpperCAmelCase      )
          model.to(__UpperCAmelCase      )
          model.eval()

          with torch.no_grad():
              lowerCAmelCase__ :  Optional[int]							      =					model(**self._prepare_for_class(__UpperCAmelCase  ,__UpperCAmelCase      )      )

          lowerCAmelCase__ :  str							      =					outputs.hidden_states

          lowerCAmelCase__ :  int							      =					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),
                      ]      )  ,)

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

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

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

          check_hidden_states_output(__UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase      )




  def 							UpperCAmelCase_	(    self      )    ->		int:
      def _config_zero_init(__UpperCAmelCase      ):
          lowerCAmelCase__ :  Optional[Any]							      =					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      ):
                  lowerCAmelCase__ :  Dict							      =					_config_zero_init(getattr(__UpperCAmelCase  ,__UpperCAmelCase      )      )
                  setattr(__UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase      )
          return configs_no_init

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

      lowerCAmelCase__ :  Optional[int]							      =					_config_zero_init(__UpperCAmelCase      )
      for model_class in self.all_model_classes:
          lowerCAmelCase__ :  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 							UpperCAmelCase_	(    self      )    ->		Optional[Any]:
      pass







def    _SCREAMING_SNAKE_CASE  (       ):



    """simple docstring"""





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






@require_torch
@require_vision
class        lowerCAmelCase_(				unittest.TestCase     ):
  '''simple docstring'''




  @cached_property
  def 							UpperCAmelCase_	(    self      )    ->		int:
      return ViTImageProcessor.from_pretrained("""MBZUAI/swiftformer-xs"""      ) if is_vision_available() else None






  @slow
  def 							UpperCAmelCase_	(    self      )    ->		Optional[Any]:
      lowerCAmelCase__ :  Optional[int]							      =					SwiftFormerForImageClassification.from_pretrained("""MBZUAI/swiftformer-xs"""      ).to(__UpperCAmelCase      )

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

      # forward pass
      with torch.no_grad():
          lowerCAmelCase__ :  Optional[int]							      =					model(**__UpperCAmelCase      )

      # verify the logits
      lowerCAmelCase__ :  Any							      =					torch.Size((1, 1000)      )
      self.assertEqual(outputs.logits.shape  ,__UpperCAmelCase      )

      lowerCAmelCase__ :  List[Any]							      =					torch.tensor([[-2.17_03E00, 2.11_07E00, -2.08_11E00]]      ).to(__UpperCAmelCase      )
      self.assertTrue(torch.allclose(outputs.logits[0, :3]  ,__UpperCAmelCase  ,atol=1E-4      )      )







'''simple docstring'''





from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


_lowerCAmelCase      						=	logging.get_logger(__name__)

_lowerCAmelCase      						=	{
    '''facebook/xlm-roberta-xl''': '''https://huggingface.co/facebook/xlm-roberta-xl/resolve/main/config.json''',
    '''facebook/xlm-roberta-xxl''': '''https://huggingface.co/facebook/xlm-roberta-xxl/resolve/main/config.json''',
    # See all XLM-RoBERTa-XL models at https://huggingface.co/models?filter=xlm-roberta-xl
}






class        lowerCAmelCase_(				SCREAMING_SNAKE_CASE_     ):
  '''simple docstring'''




  __lowercase							:      int				=      '''xlm-roberta-xl'''




  def __init__(    self  ,__UpperCAmelCase=25_0880  ,__UpperCAmelCase=2560  ,__UpperCAmelCase=36  ,__UpperCAmelCase=32  ,__UpperCAmelCase=1_0240  ,__UpperCAmelCase="gelu"  ,__UpperCAmelCase=0.1  ,__UpperCAmelCase=0.1  ,__UpperCAmelCase=514  ,__UpperCAmelCase=1  ,__UpperCAmelCase=0.0_2  ,__UpperCAmelCase=1E-05  ,__UpperCAmelCase=1  ,__UpperCAmelCase=0  ,__UpperCAmelCase=2  ,__UpperCAmelCase="absolute"  ,__UpperCAmelCase=True  ,__UpperCAmelCase=None  ,**__UpperCAmelCase  ,)    ->		str:
      super().__init__(pad_token_id=__UpperCAmelCase  ,bos_token_id=__UpperCAmelCase  ,eos_token_id=__UpperCAmelCase  ,**__UpperCAmelCase      )
      lowerCAmelCase__ :  List[Any]							      =					vocab_size
      lowerCAmelCase__ :  int							      =					hidden_size
      lowerCAmelCase__ :  int							      =					num_hidden_layers
      lowerCAmelCase__ :  str							      =					num_attention_heads
      lowerCAmelCase__ :  int							      =					hidden_act
      lowerCAmelCase__ :  Dict							      =					intermediate_size
      lowerCAmelCase__ :  List[Any]							      =					hidden_dropout_prob
      lowerCAmelCase__ :  str							      =					attention_probs_dropout_prob
      lowerCAmelCase__ :  Optional[int]							      =					max_position_embeddings
      lowerCAmelCase__ :  List[str]							      =					type_vocab_size
      lowerCAmelCase__ :  List[Any]							      =					initializer_range
      lowerCAmelCase__ :  Tuple							      =					layer_norm_eps
      lowerCAmelCase__ :  int							      =					position_embedding_type
      lowerCAmelCase__ :  Optional[Any]							      =					use_cache
      lowerCAmelCase__ :  Optional[Any]							      =					classifier_dropout







class        lowerCAmelCase_(				SCREAMING_SNAKE_CASE_     ):
  '''simple docstring'''




  @property
  def 							UpperCAmelCase_	(    self      )    ->		Mapping[str, Mapping[int, str]]:
      if self.task == "multiple-choice":
          lowerCAmelCase__ :  Dict							      =					{0: """batch""", 1: """choice""", 2: """sequence"""}
      else:
          lowerCAmelCase__ :  Any							      =					{0: """batch""", 1: """sequence"""}
      return OrderedDict(
          [
              ("""input_ids""", dynamic_axis),
              ("""attention_mask""", dynamic_axis),
          ]      )


