
"""simple docstring"""






import copy
import inspect
import unittest

import numpy as np
from huggingface_hub import hf_hub_download

from transformers import VideoMAEConfig
from transformers.models.auto import get_values
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 (
     MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING,
     VideoMAEForPreTraining,
     VideoMAEForVideoClassification,
     VideoMAEModel,
 )
 from transformers.models.videomae.modeling_videomae import VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
 from transformers import VideoMAEImageProcessor





class 				a__					:



     def __init__(							self :Optional[Any]      ,  _lowerCamelCase :List[str]      ,  _lowerCamelCase :List[str]=13      ,  _lowerCamelCase :List[Any]=10      ,  _lowerCamelCase :List[str]=3      ,  _lowerCamelCase :Any=2      ,  _lowerCamelCase :str=2      ,  _lowerCamelCase :Optional[int]=2      ,  _lowerCamelCase :List[str]=True      ,  _lowerCamelCase :Tuple=True      ,  _lowerCamelCase :List[Any]=32      ,  _lowerCamelCase :Any=5      ,  _lowerCamelCase :List[Any]=4      ,  _lowerCamelCase :Dict=37      ,  _lowerCamelCase :List[str]="gelu"      ,  _lowerCamelCase :Any=0.1      ,  _lowerCamelCase :Optional[int]=0.1      ,  _lowerCamelCase :Union[str, Any]=10      ,  _lowerCamelCase :Optional[Any]=0.02      ,  _lowerCamelCase :Tuple=0.9      ,  _lowerCamelCase :Dict=None      ,  ):







      '''simple docstring'''



      UpperCamelCase_     :					int   	=parent
      UpperCamelCase_     :					Union[str, Any]   	=batch_size
      UpperCamelCase_     :					List[Any]   	=image_size
      UpperCamelCase_     :					str   	=num_channels
      UpperCamelCase_     :					int   	=patch_size
      UpperCamelCase_     :					Optional[int]   	=tubelet_size
      UpperCamelCase_     :					str   	=num_frames
      UpperCamelCase_     :					str   	=is_training
      UpperCamelCase_     :					int   	=use_labels
      UpperCamelCase_     :					Tuple   	=hidden_size
      UpperCamelCase_     :					Optional[int]   	=num_hidden_layers
      UpperCamelCase_     :					Any   	=num_attention_heads
      UpperCamelCase_     :					Union[str, Any]   	=intermediate_size
      UpperCamelCase_     :					Optional[Any]   	=hidden_act
      UpperCamelCase_     :					Any   	=hidden_dropout_prob
      UpperCamelCase_     :					Union[str, Any]   	=attention_probs_dropout_prob
      UpperCamelCase_     :					Any   	=type_sequence_label_size
      UpperCamelCase_     :					List[Any]   	=initializer_range
      UpperCamelCase_     :					int   	=mask_ratio
      UpperCamelCase_     :					Optional[int]   	=scope

      # in VideoMAE, the number of tokens equals num_frames/tubelet_size * num_patches per frame
      UpperCamelCase_     :					Tuple   	=(image_size // patch_size) ** 2
      UpperCamelCase_     :					Dict   	=(num_frames // tubelet_size) * self.num_patches_per_frame

      # use this variable to define bool_masked_pos
      UpperCamelCase_     :					List[Any]   	=int(mask_ratio * self.seq_length   )



     def  lowerCamelCase_						(							self :Union[str, Any]   ):







      '''simple docstring'''



      UpperCamelCase_     :					Tuple   	=floats_tensor(
          [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]   )

      UpperCamelCase_     :					Optional[int]   	=None
      if self.use_labels:
       UpperCamelCase_     :					List[str]   	=ids_tensor([self.batch_size]      ,  self.type_sequence_label_size   )

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

      return config, pixel_values, labels



     def  lowerCamelCase_						(							self :Dict   ):







      '''simple docstring'''



      return VideoMAEConfig(
          image_size=self.image_size      ,  patch_size=self.patch_size      ,  num_channels=self.num_channels      ,  num_frames=self.num_frames      ,  tubelet_size=self.tubelet_size      ,  hidden_size=self.hidden_size      ,  num_hidden_layers=self.num_hidden_layers      ,  num_attention_heads=self.num_attention_heads      ,  intermediate_size=self.intermediate_size      ,  hidden_act=self.hidden_act      ,  hidden_dropout_prob=self.hidden_dropout_prob      ,  attention_probs_dropout_prob=self.attention_probs_dropout_prob      ,  is_decoder=__lowerCamelCase      ,  initializer_range=self.initializer_range      ,  )



     def  lowerCamelCase_						(							self :Dict      ,  _lowerCamelCase :List[Any]      ,  _lowerCamelCase :List[Any]      ,  _lowerCamelCase :Optional[Any]   ):







      '''simple docstring'''



      UpperCamelCase_     :					Any   	=VideoMAEModel(config=__lowerCamelCase   )
      model.to(__lowerCamelCase   )
      model.eval()
      UpperCamelCase_     :					List[Any]   	=model(__lowerCamelCase   )
      self.parent.assertEqual(result.last_hidden_state.shape      ,  (self.batch_size, self.seq_length, self.hidden_size)   )



     def  lowerCamelCase_						(							self :int      ,  _lowerCamelCase :Optional[Any]      ,  _lowerCamelCase :Any      ,  _lowerCamelCase :str   ):







      '''simple docstring'''



      UpperCamelCase_     :					str   	=VideoMAEForPreTraining(__lowerCamelCase   )
      model.to(__lowerCamelCase   )
      model.eval()
      # important: each video needs to have the same number of masked patches
      # hence we define a single mask, which we then repeat for each example in the batch
      UpperCamelCase_     :					Tuple   	=torch.ones((self.num_masks,)   )
      UpperCamelCase_     :					int   	=torch.cat([mask, torch.zeros(self.seq_length - mask.size(0   )   )]   )
      UpperCamelCase_     :					Dict   	=mask.expand(self.batch_size      ,  -1   ).bool()

      UpperCamelCase_     :					Optional[Any]   	=model(__lowerCamelCase      ,  __lowerCamelCase   )
      # model only returns predictions for masked patches
      UpperCamelCase_     :					Union[str, Any]   	=mask.sum().item()
      UpperCamelCase_     :					int   	=3 * self.tubelet_size * self.patch_size**2
      self.parent.assertEqual(result.logits.shape      ,  (self.batch_size, num_masked_patches, decoder_num_labels)   )




     def  lowerCamelCase_						(							self :List[str]   ):







      '''simple docstring'''



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





@require_torch
class 				a__					(						__snake_case   ,							__snake_case   ,							unittest.TestCase       ):
     UpperCAmelCase__         =      (
         (VideoMAEModel, VideoMAEForPreTraining, VideoMAEForVideoClassification) if is_torch_available() else ()
     )
     UpperCAmelCase__         =      (
         {'''feature-extraction''': VideoMAEModel, '''video-classification''': VideoMAEForVideoClassification}
         if is_torch_available()
         else {}
     )

     UpperCAmelCase__         =      False
     UpperCAmelCase__         =      False
     UpperCAmelCase__         =      False
     UpperCAmelCase__         =      False



     def  lowerCamelCase_						(							self :str   ):







      '''simple docstring'''



      UpperCamelCase_     :					str   	=VideoMAEModelTester(self   )
      UpperCamelCase_     :					Any   	=ConfigTester(self      ,  config_class=__lowerCamelCase      ,  has_text_modality=__lowerCamelCase      ,  hidden_size=37   )



     def  lowerCamelCase_						(							self :Optional[int]      ,  _lowerCamelCase :str      ,  _lowerCamelCase :Union[str, Any]      ,  _lowerCamelCase :Union[str, Any]=False   ):







      '''simple docstring'''



      UpperCamelCase_     :					int   	=copy.deepcopy(__lowerCamelCase   )

      if model_class == VideoMAEForPreTraining:
       # important: each video needs to have the same number of masked patches
       # hence we define a single mask, which we then repeat for each example in the batch
       UpperCamelCase_     :					Optional[Any]   	=torch.ones((self.model_tester.num_masks,)   )
       UpperCamelCase_     :					str   	=torch.cat([mask, torch.zeros(self.model_tester.seq_length - mask.size(0   )   )]   )
       UpperCamelCase_     :					Union[str, Any]   	=mask.expand(self.model_tester.batch_size      ,  -1   ).bool()
       UpperCamelCase_     :					List[str]   	=bool_masked_pos.to(__lowerCamelCase   )

      if return_labels:
       if model_class in [
           *get_values(__lowerCamelCase   ),
       ]:
        UpperCamelCase_     :					Any   	=torch.zeros(
            self.model_tester.batch_size      ,  dtype=torch.long      ,  device=__lowerCamelCase   )

      return inputs_dict



     def  lowerCamelCase_						(							self :int   ):







      '''simple docstring'''



      self.config_tester.run_common_tests()



     @unittest.skip(reason='VideoMAE does not use inputs_embeds'   )
     def  lowerCamelCase_						(							self :Any   ):







      '''simple docstring'''



      pass



     def  lowerCamelCase_						(							self :Optional[Any]   ):







      '''simple docstring'''



      UpperCamelCase_	,     UpperCamelCase_     :					Any   	=self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
       UpperCamelCase_     :					Optional[int]   	=model_class(__lowerCamelCase   )
       self.assertIsInstance(model.get_input_embeddings()      ,  (nn.Module)   )
       UpperCamelCase_     :					int   	=model.get_output_embeddings()
       self.assertTrue(x is None or isinstance(__lowerCamelCase      ,  nn.Linear   )   )



     def  lowerCamelCase_						(							self :Optional[int]   ):







      '''simple docstring'''



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

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

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



     def  lowerCamelCase_						(							self :Optional[int]   ):







      '''simple docstring'''



      UpperCamelCase_     :					Optional[int]   	=self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*__lowerCamelCase   )



     def  lowerCamelCase_						(							self :Union[str, Any]   ):







      '''simple docstring'''



      UpperCamelCase_     :					Optional[Any]   	=self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_pretraining(*__lowerCamelCase   )



     @slow
     def  lowerCamelCase_						(							self :Optional[Any]   ):







      '''simple docstring'''



      for model_name in VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       UpperCamelCase_     :					List[str]   	=VideoMAEModel.from_pretrained(__lowerCamelCase   )
       self.assertIsNotNone(__lowerCamelCase   )



     def  lowerCamelCase_						(							self :Optional[Any]   ):







      '''simple docstring'''



      if not self.has_attentions:
       pass

      else:
       UpperCamelCase_	,     UpperCamelCase_     :					Optional[int]   	=self.model_tester.prepare_config_and_inputs_for_common()
       UpperCamelCase_     :					Union[str, Any]   	=True

       for model_class in self.all_model_classes:
        UpperCamelCase_     :					Any   	=self.model_tester.seq_length - self.model_tester.num_masks
        UpperCamelCase_     :					str   	=(
            num_visible_patches if model_class == VideoMAEForPreTraining else self.model_tester.seq_length
        )

        UpperCamelCase_     :					Tuple   	=True
        UpperCamelCase_     :					Optional[Any]   	=False
        UpperCamelCase_     :					Optional[int]   	=True
        UpperCamelCase_     :					List[str]   	=model_class(__lowerCamelCase   )
        model.to(__lowerCamelCase   )
        model.eval()
        with torch.no_grad():
         UpperCamelCase_     :					Tuple   	=model(**self._prepare_for_class(__lowerCamelCase      ,  __lowerCamelCase   )   )
        UpperCamelCase_     :					Any   	=outputs.attentions
        self.assertEqual(len(__lowerCamelCase   )      ,  self.model_tester.num_hidden_layers   )

        # check that output_attentions also work using config
        del inputs_dict["output_attentions"]
        UpperCamelCase_     :					Union[str, Any]   	=True
        UpperCamelCase_     :					Tuple   	=model_class(__lowerCamelCase   )
        model.to(__lowerCamelCase   )
        model.eval()
        with torch.no_grad():
         UpperCamelCase_     :					Tuple   	=model(**self._prepare_for_class(__lowerCamelCase      ,  __lowerCamelCase   )   )
        UpperCamelCase_     :					List[Any]   	=outputs.attentions
        self.assertEqual(len(__lowerCamelCase   )      ,  self.model_tester.num_hidden_layers   )

        self.assertListEqual(
            list(attentions[0].shape[-3:]   )      ,  [self.model_tester.num_attention_heads, seq_len, seq_len]      ,  )
        UpperCamelCase_     :					int   	=len(__lowerCamelCase   )

        # Check attention is always last and order is fine
        UpperCamelCase_     :					Optional[int]   	=True
        UpperCamelCase_     :					Union[str, Any]   	=True
        UpperCamelCase_     :					List[Any]   	=model_class(__lowerCamelCase   )
        model.to(__lowerCamelCase   )
        model.eval()
        with torch.no_grad():
         UpperCamelCase_     :					Any   	=model(**self._prepare_for_class(__lowerCamelCase      ,  __lowerCamelCase   )   )

        self.assertEqual(out_len + 1      ,  len(__lowerCamelCase   )   )

        UpperCamelCase_     :					Union[str, Any]   	=outputs.attentions

        self.assertEqual(len(__lowerCamelCase   )      ,  self.model_tester.num_hidden_layers   )
        self.assertListEqual(
            list(self_attentions[0].shape[-3:]   )      ,  [self.model_tester.num_attention_heads, seq_len, seq_len]      ,  )



     def  lowerCamelCase_						(							self :Union[str, Any]   ):







      '''simple docstring'''



      def check_hidden_states_output(_lowerCamelCase :List[Any]      ,  _lowerCamelCase :Union[str, Any]      ,  _lowerCamelCase :int   ):
       UpperCamelCase_     :					int   	=model_class(__lowerCamelCase   )
       model.to(__lowerCamelCase   )
       model.eval()

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

       UpperCamelCase_     :					Dict   	=outputs.hidden_states
       UpperCamelCase_     :					Any   	=self.model_tester.num_hidden_layers + 1
       self.assertEqual(len(__lowerCamelCase   )      ,  __lowerCamelCase   )

       UpperCamelCase_     :					Union[str, Any]   	=self.model_tester.seq_length - self.model_tester.num_masks
       UpperCamelCase_     :					Tuple   	=num_visible_patches if model_class == VideoMAEForPreTraining else self.model_tester.seq_length

       self.assertListEqual(
           list(hidden_states[0].shape[-2:]   )      ,  [seq_length, self.model_tester.hidden_size]      ,  )

      UpperCamelCase_	,     UpperCamelCase_     :					Optional[Any]   	=self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
       UpperCamelCase_     :					List[Any]   	=True
       check_hidden_states_output(__lowerCamelCase      ,  __lowerCamelCase      ,  __lowerCamelCase   )

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

       check_hidden_states_output(__lowerCamelCase      ,  __lowerCamelCase      ,  __lowerCamelCase   )




     @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'   )
     def  lowerCamelCase_						(							self :Optional[int]   ):







      '''simple docstring'''



      pass




def       A_  (				):
 UpperCamelCase_     :					Union[str, Any]   	=hf_hub_download(
     repo_id='hf-internal-testing/spaghetti-video'						,	filename='eating_spaghetti.npy'						,	repo_type='dataset'	)
 UpperCamelCase_     :					Dict   	=np.load(lowerCAmelCase_	)
 return list(lowerCAmelCase_	)





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



     @cached_property
     def  lowerCamelCase_						(							self :Optional[int]   ):







      '''simple docstring'''



      return (
          VideoMAEImageProcessor(image_mean=[0.5, 0.5, 0.5]      ,  image_std=[0.5, 0.5, 0.5]   )
          if is_vision_available()
          else None
      )



     @slow
     def  lowerCamelCase_						(							self :Any   ):







      '''simple docstring'''



      UpperCamelCase_     :					Optional[int]   	=VideoMAEForVideoClassification.from_pretrained('MCG-NJU/videomae-base-finetuned-kinetics'   ).to(
          __lowerCamelCase   )

      UpperCamelCase_     :					Union[str, Any]   	=self.default_image_processor
      UpperCamelCase_     :					Any   	=prepare_video()
      UpperCamelCase_     :					List[str]   	=image_processor(__lowerCamelCase      ,  return_tensors='pt'   ).to(__lowerCamelCase   )

      # forward pass
      with torch.no_grad():
       UpperCamelCase_     :					List[Any]   	=model(**__lowerCamelCase   )

      # verify the logits
      UpperCamelCase_     :					Optional[Any]   	=torch.Size((1, 400)   )
      self.assertEqual(outputs.logits.shape      ,  __lowerCamelCase   )

      UpperCamelCase_     :					List[Any]   	=torch.tensor([0.3669, -0.0688, -0.2421]   ).to(__lowerCamelCase   )

      self.assertTrue(torch.allclose(outputs.logits[0, :3]      ,  __lowerCamelCase      ,  atol=1E-4   )   )




     @slow
     def  lowerCamelCase_						(							self :Dict   ):







      '''simple docstring'''



      UpperCamelCase_     :					Optional[int]   	=VideoMAEForPreTraining.from_pretrained('MCG-NJU/videomae-base-short'   ).to(__lowerCamelCase   )

      UpperCamelCase_     :					List[Any]   	=self.default_image_processor
      UpperCamelCase_     :					List[Any]   	=prepare_video()
      UpperCamelCase_     :					Tuple   	=image_processor(__lowerCamelCase      ,  return_tensors='pt'   ).to(__lowerCamelCase   )

      # add boolean mask, indicating which patches to mask
      UpperCamelCase_     :					int   	=hf_hub_download(repo_id='hf-internal-testing/bool-masked-pos'      ,  filename='bool_masked_pos.pt'   )
      UpperCamelCase_     :					int   	=torch.load(__lowerCamelCase   )

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

      # verify the logits
      UpperCamelCase_     :					Dict   	=torch.Size([1, 1_408, 1_536]   )
      UpperCamelCase_     :					Any   	=torch.tensor(
          [[0.7994, 0.9612, 0.8508], [0.7401, 0.8958, 0.8302], [0.5862, 0.7468, 0.7325]]      ,  device=__lowerCamelCase   )
      self.assertEqual(outputs.logits.shape      ,  __lowerCamelCase   )
      self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]      ,  __lowerCamelCase      ,  atol=1E-4   )   )

      # verify the loss (`config.norm_pix_loss` = `True`)
      UpperCamelCase_     :					str   	=torch.tensor([0.5142]      ,  device=__lowerCamelCase   )
      self.assertTrue(torch.allclose(outputs.loss      ,  __lowerCamelCase      ,  atol=1E-4   )   )

      # verify the loss (`config.norm_pix_loss` = `False`)
      UpperCamelCase_     :					int   	=VideoMAEForPreTraining.from_pretrained('MCG-NJU/videomae-base-short'      ,  norm_pix_loss=__lowerCamelCase   ).to(
          __lowerCamelCase   )

      with torch.no_grad():
       UpperCamelCase_     :					Optional[int]   	=model(**__lowerCamelCase   )

      UpperCamelCase_     :					Dict   	=torch.tensor(torch.tensor([0.6469]   )      ,  device=__lowerCamelCase   )
      self.assertTrue(torch.allclose(outputs.loss      ,  __lowerCamelCase      ,  atol=1E-4   )   )




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





      def __init__(		self    :       Optional[Any]       ,							__lowerCamelCase    :       Any       ,							__lowerCamelCase    :       int=1_3       ,							__lowerCamelCase    :       List[str]=3       ,							__lowerCamelCase    :       List[str]=True       ,							__lowerCamelCase    :       Any=True       ,							__lowerCamelCase    :       Optional[int]=0.1       ,							__lowerCamelCase    :       Optional[Any]=0.1       ,							__lowerCamelCase    :       Tuple=2_2_4       ,							__lowerCamelCase    :       int=1_0_0_0       ,							__lowerCamelCase    :       int=[3, 3, 6, 4]       ,							__lowerCamelCase    :       Dict=[4_8, 5_6, 1_1_2, 2_2_0]       ,							)     -> Union[str, Any]:






         """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 							_lowercase					(		self    :       List[str]							)     -> Optional[Any]:






         """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 							_lowercase					(		self    :       Optional[Any]							)     -> str:






         """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 							_lowercase					(		self    :       Tuple       ,							__lowerCamelCase    :       int       ,							__lowerCamelCase    :       List[str]       ,							__lowerCamelCase    :       Optional[int]							)     -> str:






         """simple docstring"""







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





      def 							_lowercase					(		self    :       List[str]       ,							__lowerCamelCase    :       int       ,							__lowerCamelCase    :       List[str]       ,							__lowerCamelCase    :       Any							)     -> Dict:






         """simple docstring"""







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

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

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





      def 							_lowercase					(		self    :       Tuple							)     -> Optional[Any]:






         """simple docstring"""







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






@require_torch
class 	__lowercase					(	__snake_case			,       __snake_case			,       unittest.TestCase	):
      UpperCamelCase										=							(SwiftFormerModel, SwiftFormerForImageClassification) if is_torch_available() else ()
      UpperCamelCase										=							(
          {'''feature-extraction''': SwiftFormerModel, '''image-classification''': SwiftFormerForImageClassification}
          if is_torch_available()
          else {}
      )

      UpperCamelCase										=							False
      UpperCamelCase										=							False
      UpperCamelCase										=							False
      UpperCamelCase										=							False
      UpperCamelCase										=							False





      def 							_lowercase					(		self    :       str							)     -> Tuple:






         """simple docstring"""







         UpperCAmelCase      					=  SwiftFormerModelTester(self							)
         UpperCAmelCase      					=  ConfigTester(
             self       ,							config_class=__lowerCamelCase       ,							has_text_modality=__lowerCamelCase       ,							hidden_size=3_7       ,							num_attention_heads=1_2       ,							num_hidden_layers=1_2       ,							)





      def 							_lowercase					(		self    :       Dict							)     -> Union[str, Any]:






         """simple docstring"""







         self.config_tester.run_common_tests()





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






         """simple docstring"""







         pass





      def 							_lowercase					(		self    :       str							)     -> str:






         """simple docstring"""







         UpperCAmelCase					,	UpperCAmelCase      					=  self.model_tester.prepare_config_and_inputs_for_common()

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





      def 							_lowercase					(		self    :       Union[str, Any]							)     -> List[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(__lowerCamelCase							)
            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]       ,							__lowerCamelCase							)





      def 							_lowercase					(		self    :       List[str]							)     -> Dict:






         """simple docstring"""







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





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






         """simple docstring"""







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





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






         """simple docstring"""







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





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






         """simple docstring"""







         pass





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






         """simple docstring"""







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

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

            UpperCAmelCase      					=  outputs.hidden_states

            UpperCAmelCase      					=  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					,	UpperCAmelCase      					=  self.model_tester.prepare_config_and_inputs_for_common()

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

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

            check_hidden_states_output(__lowerCamelCase       ,							__lowerCamelCase       ,							__lowerCamelCase							)





      def 							_lowercase					(		self    :       Dict							)     -> Union[str, Any]:






         """simple docstring"""







         def _config_zero_init(__lowerCamelCase    :       Any							):
            UpperCAmelCase      					=  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      					=  _config_zero_init(getattr(__lowerCamelCase       ,							__lowerCamelCase							)							)
                  setattr(__lowerCamelCase       ,							__lowerCamelCase       ,							__lowerCamelCase							)
            return configs_no_init

         UpperCAmelCase					,	UpperCAmelCase      					=  self.model_tester.prepare_config_and_inputs_for_common()

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






         """simple docstring"""







         pass

def 	_UpperCamelCase					(    )    ->Dict:
   UpperCAmelCase      					=  Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" )
   return image







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





      @cached_property
      def 							_lowercase					(		self    :       Optional[int]							)     -> int:






         """simple docstring"""







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





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






         """simple docstring"""







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

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

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

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

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



