


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


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





      '''simple docstring'''






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


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





      '''simple docstring'''






      UpperCamelCase__					:    Union[str, Any]          =   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

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

      UpperCamelCase__					:    Optional[int]          =   self.get_config()

      return config, pixel_values, labels


     def      UpperCAmelCase__	(      self							:     Any			)  ->      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=lowerCamelCase__							,	layer_scale_init_value=1E-5							,	)


     def      UpperCAmelCase__	(      self							:     str							,	lowerCamelCase__							:     str							,	lowerCamelCase__							:     int							,	lowerCamelCase__							:     List[str]			)  ->      Dict:





      '''simple docstring'''






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


     def      UpperCAmelCase__	(      self							:     List[Any]							,	lowerCamelCase__							:     Tuple							,	lowerCamelCase__							:     List[str]							,	lowerCamelCase__							:     List[Any]			)  ->      str:





      '''simple docstring'''






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

      UpperCamelCase__					:    Tuple          =   SwiftFormerForImageClassification(lowerCamelCase__			)
      model.to(lowerCamelCase__			)
      model.eval()

      UpperCamelCase__					:    Tuple          =   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)
      UpperCamelCase__					:    Optional[int]          =   model(lowerCamelCase__			)
      self.parent.assertEqual(result.logits.shape							,	(self.batch_size, self.num_labels)			)


     def      UpperCAmelCase__	(      self							:     Optional[Any]			)  ->      str:





      '''simple docstring'''






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



@require_torch
class       __magic_name__	(     __lowerCAmelCase					,				__lowerCAmelCase					,				unittest.TestCase):
     A:		Tuple			=      (SwiftFormerModel, SwiftFormerForImageClassification) if is_torch_available() else ()
     A:		Optional[int]			=      (
         {"feature-extraction": SwiftFormerModel, "image-classification": SwiftFormerForImageClassification}
         if is_torch_available()
         else {}
     )

     A:		Optional[Any]			=      False
     A:		Tuple			=      False
     A:		List[str]			=      False
     A:		Union[str, Any]			=      False
     A:		List[str]			=      False


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





      '''simple docstring'''






      UpperCamelCase__					:    int          =   SwiftFormerModelTester(self			)
      UpperCamelCase__					:    Any          =   ConfigTester(
          self							,	config_class=lowerCamelCase__							,	has_text_modality=lowerCamelCase__							,	hidden_size=37							,	num_attention_heads=12							,	num_hidden_layers=12							,	)


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





      '''simple docstring'''






      self.config_tester.run_common_tests()


     @unittest.skip(reason='''SwiftFormer does not use inputs_embeds'''			)
     def      UpperCAmelCase__	(      self							:     Optional[int]			)  ->      List[str]:





      '''simple docstring'''






      pass


     def      UpperCAmelCase__	(      self							:     str			)  ->      int:





      '''simple docstring'''






      UpperCamelCase__					:    int          =   self.model_tester.prepare_config_and_inputs_for_common()

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


     def      UpperCAmelCase__	(      self							:     int			)  ->      Optional[int]:





      '''simple docstring'''






      UpperCamelCase__					:    Dict          =   self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
       UpperCamelCase__					:    Tuple          =   model_class(lowerCamelCase__			)
       UpperCamelCase__					:    Tuple          =   inspect.signature(model.forward			)
       # signature.parameters is an OrderedDict => so arg_names order is deterministic
       UpperCamelCase__					:    Union[str, Any]          =   [*signature.parameters.keys()]

       UpperCamelCase__					:    Optional[int]          =   ['''pixel_values''']
       self.assertListEqual(arg_names[:1]							,	lowerCamelCase__			)


     def      UpperCAmelCase__	(      self							:     str			)  ->      Optional[Any]:





      '''simple docstring'''






      UpperCamelCase__					:    List[Any]          =   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*lowerCamelCase__			)


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





      '''simple docstring'''






      UpperCamelCase__					:    str          =   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*lowerCamelCase__			)


     @slow
     def      UpperCAmelCase__	(      self							:     Union[str, Any]			)  ->      List[str]:





      '''simple docstring'''






      for model_name in SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       UpperCamelCase__					:    Tuple          =   SwiftFormerModel.from_pretrained(lowerCamelCase__			)
       self.assertIsNotNone(lowerCamelCase__			)


     @unittest.skip(reason='''SwiftFormer does not output attentions'''			)
     def      UpperCAmelCase__	(      self							:     Dict			)  ->      str:





      '''simple docstring'''






      pass


     def      UpperCAmelCase__	(      self							:     Dict			)  ->      List[Any]:





      '''simple docstring'''






      def check_hidden_states_output(lowerCamelCase__							:     Union[str, Any]							,	lowerCamelCase__							:     List[str]							,	lowerCamelCase__							:     Optional[int]			):
       UpperCamelCase__					:    Optional[int]          =   model_class(lowerCamelCase__			)
       model.to(lowerCamelCase__			)
       model.eval()

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

       UpperCamelCase__					:    Tuple          =   outputs.hidden_states

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

      UpperCamelCase__					:    Optional[int]          =   self.model_tester.prepare_config_and_inputs_for_common()

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

       # check that output_hidden_states also work using config
       del inputs_dict["output_hidden_states"]
       UpperCamelCase__					:    str          =   True

       check_hidden_states_output(lowerCamelCase__							,	lowerCamelCase__							,	lowerCamelCase__			)


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





      '''simple docstring'''






      def _config_zero_init(lowerCamelCase__							:     Optional[int]			):
       UpperCamelCase__					:    int          =   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-1_0			)
        if isinstance(getattr(lowerCamelCase__							,	lowerCamelCase__							,	lowerCamelCase__			)							,	lowerCamelCase__			):
         UpperCamelCase__					:    Optional[Any]          =   _config_zero_init(getattr(lowerCamelCase__							,	lowerCamelCase__			)			)
         setattr(lowerCamelCase__							,	lowerCamelCase__							,	lowerCamelCase__			)
       return configs_no_init

      UpperCamelCase__					:    Dict          =   self.model_tester.prepare_config_and_inputs_for_common()

      UpperCamelCase__					:    int          =   _config_zero_init(lowerCamelCase__			)
      for model_class in self.all_model_classes:
       UpperCamelCase__					:    List[str]          =   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      UpperCAmelCase__	(      self							:     str			)  ->      Optional[int]:





      '''simple docstring'''






      pass



def 			_a  (				):







 """simple docstring"""



 UpperCamelCase__					:    Tuple          =   Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''     )
 return image






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


     @cached_property
     def      UpperCAmelCase__	(      self							:     List[str]			)  ->      Optional[int]:





      '''simple docstring'''






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


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





      '''simple docstring'''






      UpperCamelCase__					:    str          =   SwiftFormerForImageClassification.from_pretrained('''MBZUAI/swiftformer-xs'''			).to(lowerCamelCase__			)

      UpperCamelCase__					:    Any          =   self.default_image_processor
      UpperCamelCase__					:    Optional[int]          =   prepare_img()
      UpperCamelCase__					:    List[Any]          =   image_processor(images=lowerCamelCase__							,	return_tensors='''pt'''			).to(lowerCamelCase__			)

      # forward pass
      with torch.no_grad():
       UpperCamelCase__					:    Tuple          =   model(**lowerCamelCase__			)

      # verify the logits
      UpperCamelCase__					:    Union[str, Any]          =   torch.Size((1, 1000)			)
      self.assertEqual(outputs.logits.shape							,	lowerCamelCase__			)

      UpperCamelCase__					:    Tuple          =   torch.tensor([[-2.1_7_0_3E0_0, 2.1_1_0_7E0_0, -2.0_8_1_1E0_0]]			).to(lowerCamelCase__			)
      self.assertTrue(torch.allclose(outputs.logits[0, :3]							,	lowerCamelCase__							,	atol=1E-4			)			)










from argparse import ArgumentParser, Namespace

from ..utils import logging
from . import BaseTransformersCLICommand

def 			_a  (				SCREAMING_SNAKE_CASE	:				Namespace     ):







 """simple docstring"""



 return ConvertCommand(
     args.model_type    , args.tf_checkpoint    , args.pytorch_dump_output    , args.config    , args.finetuning_task_name     )


__UpperCamelCase       :       List[Any]						       =				"\ntransformers can only be used from the commandline to convert TensorFlow models in PyTorch, In that case, it requires\nTensorFlow to be installed. Please see https://www.tensorflow.org/install/ for installation instructions.\n"






class       __magic_name__	(     __lowerCAmelCase):


     @staticmethod
     def      UpperCAmelCase__	(      lowerCamelCase__							:     ArgumentParser			)  ->      Optional[int]:





      '''simple docstring'''






      UpperCamelCase__					:    int          =   parser.add_parser(
          '''convert'''							,	help='''CLI tool to run convert model from original author checkpoints to Transformers PyTorch checkpoints.'''							,	)
      train_parser.add_argument('''--model_type'''							,	type=lowerCamelCase__							,	required=lowerCamelCase__							,	help='''Model\'s type.'''			)
      train_parser.add_argument(
          '''--tf_checkpoint'''							,	type=lowerCamelCase__							,	required=lowerCamelCase__							,	help='''TensorFlow checkpoint path or folder.'''			)
      train_parser.add_argument(
          '''--pytorch_dump_output'''							,	type=lowerCamelCase__							,	required=lowerCamelCase__							,	help='''Path to the PyTorch saved model output.'''			)
      train_parser.add_argument('''--config'''							,	type=lowerCamelCase__							,	default=''''''							,	help='''Configuration file path or folder.'''			)
      train_parser.add_argument(
          '''--finetuning_task_name'''							,	type=lowerCamelCase__							,	default=lowerCamelCase__							,	help='''Optional fine-tuning task name if the TF model was a finetuned model.'''							,	)
      train_parser.set_defaults(func=lowerCamelCase__			)


     def __init__(      self							:     Optional[Any]							,	lowerCamelCase__							:     str							,	lowerCamelCase__							:     str							,	lowerCamelCase__							:     str							,	lowerCamelCase__							:     str							,	lowerCamelCase__							:     str							,	*lowerCamelCase__							:     Optional[int]							,	)  ->      List[Any]:





      '''simple docstring'''






      UpperCamelCase__					:    List[Any]          =   logging.get_logger('''transformers-cli/converting'''			)

      self._logger.info(F"Loading model {model_type}"			)
      UpperCamelCase__					:    List[str]          =   model_type
      UpperCamelCase__					:    Optional[int]          =   tf_checkpoint
      UpperCamelCase__					:    List[Any]          =   pytorch_dump_output
      UpperCamelCase__					:    List[Any]          =   config
      UpperCamelCase__					:    Any          =   finetuning_task_name


     def      UpperCAmelCase__	(      self							:     Optional[Any]			)  ->      Optional[Any]:





      '''simple docstring'''






      if self._model_type == "albert":
       try:
        from ..models.albert.convert_albert_original_tf_checkpoint_to_pytorch import (
            convert_tf_checkpoint_to_pytorch,
        )
       except ImportError:
        raise ImportError(lowerCamelCase__			)

       convert_tf_checkpoint_to_pytorch(self._tf_checkpoint							,	self._config							,	self._pytorch_dump_output			)
      elif self._model_type == "bert":
       try:
        from ..models.bert.convert_bert_original_tf_checkpoint_to_pytorch import (
            convert_tf_checkpoint_to_pytorch,
        )
       except ImportError:
        raise ImportError(lowerCamelCase__			)

       convert_tf_checkpoint_to_pytorch(self._tf_checkpoint							,	self._config							,	self._pytorch_dump_output			)
      elif self._model_type == "funnel":
       try:
        from ..models.funnel.convert_funnel_original_tf_checkpoint_to_pytorch import (
            convert_tf_checkpoint_to_pytorch,
        )
       except ImportError:
        raise ImportError(lowerCamelCase__			)

       convert_tf_checkpoint_to_pytorch(self._tf_checkpoint							,	self._config							,	self._pytorch_dump_output			)
      elif self._model_type == "t5":
       try:
        from ..models.ta.convert_ta_original_tf_checkpoint_to_pytorch import convert_tf_checkpoint_to_pytorch
       except ImportError:
        raise ImportError(lowerCamelCase__			)

       convert_tf_checkpoint_to_pytorch(self._tf_checkpoint							,	self._config							,	self._pytorch_dump_output			)
      elif self._model_type == "gpt":
       from ..models.openai.convert_openai_original_tf_checkpoint_to_pytorch import (
           convert_openai_checkpoint_to_pytorch,
       )

       convert_openai_checkpoint_to_pytorch(self._tf_checkpoint							,	self._config							,	self._pytorch_dump_output			)
      elif self._model_type == "transfo_xl":
       try:
        from ..models.transfo_xl.convert_transfo_xl_original_tf_checkpoint_to_pytorch import (
            convert_transfo_xl_checkpoint_to_pytorch,
        )
       except ImportError:
        raise ImportError(lowerCamelCase__			)

       if "ckpt" in self._tf_checkpoint.lower():
        UpperCamelCase__					:    str          =   self._tf_checkpoint
        UpperCamelCase__					:    List[Any]          =   ''''''
       else:
        UpperCamelCase__					:    Any          =   self._tf_checkpoint
        UpperCamelCase__					:    List[Any]          =   ''''''
       convert_transfo_xl_checkpoint_to_pytorch(
           lowerCamelCase__							,	self._config							,	self._pytorch_dump_output							,	lowerCamelCase__			)
      elif self._model_type == "gpt2":
       try:
        from ..models.gpta.convert_gpta_original_tf_checkpoint_to_pytorch import (
            convert_gpta_checkpoint_to_pytorch,
        )
       except ImportError:
        raise ImportError(lowerCamelCase__			)

       convert_gpta_checkpoint_to_pytorch(self._tf_checkpoint							,	self._config							,	self._pytorch_dump_output			)
      elif self._model_type == "xlnet":
       try:
        from ..models.xlnet.convert_xlnet_original_tf_checkpoint_to_pytorch import (
            convert_xlnet_checkpoint_to_pytorch,
        )
       except ImportError:
        raise ImportError(lowerCamelCase__			)

       convert_xlnet_checkpoint_to_pytorch(
           self._tf_checkpoint							,	self._config							,	self._pytorch_dump_output							,	self._finetuning_task_name			)
      elif self._model_type == "xlm":
       from ..models.xlm.convert_xlm_original_pytorch_checkpoint_to_pytorch import (
           convert_xlm_checkpoint_to_pytorch,
       )

       convert_xlm_checkpoint_to_pytorch(self._tf_checkpoint							,	self._pytorch_dump_output			)
      elif self._model_type == "lxmert":
       from ..models.lxmert.convert_lxmert_original_tf_checkpoint_to_pytorch import (
           convert_lxmert_checkpoint_to_pytorch,
       )

       convert_lxmert_checkpoint_to_pytorch(self._tf_checkpoint							,	self._pytorch_dump_output			)
      elif self._model_type == "rembert":
       from ..models.rembert.convert_rembert_tf_checkpoint_to_pytorch import (
           convert_rembert_tf_checkpoint_to_pytorch,
       )

       convert_rembert_tf_checkpoint_to_pytorch(self._tf_checkpoint							,	self._config							,	self._pytorch_dump_output			)
      else:
       raise ValueError(
           '''--model_type should be selected in the list [bert, gpt, gpt2, t5, transfo_xl, xlnet, xlm, lxmert]'''			)


