






'''simple docstring'''
import inspect
import unittest

from transformers import ConvNextVaConfig
from transformers.models.auto import get_values
from transformers.models.auto.modeling_auto import MODEL_FOR_BACKBONE_MAPPING_NAMES, MODEL_MAPPING_NAMES
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 transformers import ConvNextVaBackbone, ConvNextVaForImageClassification, ConvNextVaModel
  from transformers.models.convnextva.modeling_convnextva import CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
  from PIL import Image

  from transformers import AutoImageProcessor






class      __SCREAMING_SNAKE_CASE      :




 def __init__(      self	:							Optional[int]					,      __lowercase	:							Tuple					,      __lowercase	:							Optional[int]=13					,      __lowercase	:							str=32					,      __lowercase	:							List[Any]=3					,      __lowercase	:							Optional[Any]=4					,      __lowercase	:							Dict=[10, 20, 30, 40]					,      __lowercase	:							List[Any]=[2, 2, 3, 2]					,      __lowercase	:							Tuple=True					,      __lowercase	:							Tuple=True					,      __lowercase	:							Any=37					,      __lowercase	:							str="gelu"					,      __lowercase	:							Optional[int]=10					,      __lowercase	:							Dict=0.02					,      __lowercase	:							int=["stage2", "stage3", "stage4"]					,      __lowercase	:							Optional[int]=[2, 3, 4]					,      __lowercase	:							List[str]=None					,      )	->						Union[str, Any]:
       SCREAMING_SNAKE_CASE__     :				Tuple									=parent
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=batch_size
       SCREAMING_SNAKE_CASE__     :				Tuple									=image_size
       SCREAMING_SNAKE_CASE__     :				str									=num_channels
       SCREAMING_SNAKE_CASE__     :				Tuple									=num_stages
       SCREAMING_SNAKE_CASE__     :				str									=hidden_sizes
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=depths
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=is_training
       SCREAMING_SNAKE_CASE__     :				str									=use_labels
       SCREAMING_SNAKE_CASE__     :				Dict									=intermediate_size
       SCREAMING_SNAKE_CASE__     :				Tuple									=hidden_act
       SCREAMING_SNAKE_CASE__     :				Any									=num_labels
       SCREAMING_SNAKE_CASE__     :				Any									=initializer_range
       SCREAMING_SNAKE_CASE__     :				Any									=out_features
       SCREAMING_SNAKE_CASE__     :				Any									=out_indices
       SCREAMING_SNAKE_CASE__     :				List[str]									=scope




 def      __magic_name__	(      self	:							List[str]      )	->						str:
       SCREAMING_SNAKE_CASE__     :				int									=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

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

       SCREAMING_SNAKE_CASE__     :				Dict									=self.get_config()

       return config, pixel_values, labels




 def      __magic_name__	(      self	:							Dict      )	->						Optional[int]:
       return ConvNextVaConfig(
           num_channels=self.num_channels					,      hidden_sizes=self.hidden_sizes					,      depths=self.depths					,      num_stages=self.num_stages					,      hidden_act=self.hidden_act					,      is_decoder=UpperCAmelCase_					,      initializer_range=self.initializer_range					,      out_features=self.out_features					,      out_indices=self.out_indices					,      num_labels=self.num_labels					,      )




 def      __magic_name__	(      self	:							List[Any]					,      __lowercase	:							List[Any]					,      __lowercase	:							List[str]					,      __lowercase	:							Optional[Any]      )	->						List[Any]:
       SCREAMING_SNAKE_CASE__     :				Any									=ConvNextVaModel(config=UpperCAmelCase_      )
       model.to(UpperCAmelCase_      )
       model.eval()
       SCREAMING_SNAKE_CASE__     :				int									=model(UpperCAmelCase_      )
       # expected last hidden states: B, C, H // 32, W // 32
       self.parent.assertEqual(
           result.last_hidden_state.shape					,      (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)					,      )




 def      __magic_name__	(      self	:							List[str]					,      __lowercase	:							Tuple					,      __lowercase	:							str					,      __lowercase	:							Optional[Any]      )	->						Any:
       SCREAMING_SNAKE_CASE__     :				int									=ConvNextVaForImageClassification(UpperCAmelCase_      )
       model.to(UpperCAmelCase_      )
       model.eval()
       SCREAMING_SNAKE_CASE__     :				Any									=model(UpperCAmelCase_					,      labels=UpperCAmelCase_      )
       self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.num_labels)      )




 def      __magic_name__	(      self	:							Optional[Any]					,      __lowercase	:							Tuple					,      __lowercase	:							Optional[Any]					,      __lowercase	:							Union[str, Any]      )	->						Tuple:
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=ConvNextVaBackbone(config=UpperCAmelCase_      )
       model.to(UpperCAmelCase_      )
       model.eval()
       SCREAMING_SNAKE_CASE__     :				List[Any]									=model(UpperCAmelCase_      )

       # verify hidden states
       self.parent.assertEqual(len(result.feature_maps      )					,      len(config.out_features      )      )
       self.parent.assertListEqual(list(result.feature_maps[0].shape      )					,      [self.batch_size, self.hidden_sizes[1], 4, 4]      )

       # verify channels
       self.parent.assertEqual(len(model.channels      )					,      len(config.out_features      )      )
       self.parent.assertListEqual(model.channels					,      config.hidden_sizes[1:]      )

       # verify backbone works with out_features=None
       SCREAMING_SNAKE_CASE__     :				Dict									=None
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=ConvNextVaBackbone(config=UpperCAmelCase_      )
       model.to(UpperCAmelCase_      )
       model.eval()
       SCREAMING_SNAKE_CASE__     :				List[Any]									=model(UpperCAmelCase_      )

       # verify feature maps
       self.parent.assertEqual(len(result.feature_maps      )					,      1      )
       self.parent.assertListEqual(list(result.feature_maps[0].shape      )					,      [self.batch_size, self.hidden_sizes[-1], 1, 1]      )

       # verify channels
       self.parent.assertEqual(len(model.channels      )					,      1      )
       self.parent.assertListEqual(model.channels					,      [config.hidden_sizes[-1]]      )




 def      __magic_name__	(      self	:							Optional[int]      )	->						List[str]:
       SCREAMING_SNAKE_CASE__     :				Tuple									=self.prepare_config_and_inputs()
       SCREAMING_SNAKE_CASE__		,	SCREAMING_SNAKE_CASE__		,	SCREAMING_SNAKE_CASE__     :				Any									=config_and_inputs
       SCREAMING_SNAKE_CASE__     :				List[str]									={'''pixel_values''': pixel_values}
       return config, inputs_dict




 def      __magic_name__	(      self	:							List[str]      )	->						Union[str, Any]:
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=self.prepare_config_and_inputs()
       SCREAMING_SNAKE_CASE__		,	SCREAMING_SNAKE_CASE__		,	SCREAMING_SNAKE_CASE__     :				List[Any]									=config_and_inputs
       SCREAMING_SNAKE_CASE__     :				Optional[int]									={'''pixel_values''': pixel_values, '''labels''': labels}
       return config, inputs_dict






@require_torch
class      __SCREAMING_SNAKE_CASE      (			snake_case__	,				snake_case__	,				unittest.TestCase				):
 snake_case_  							=							(
     (
         ConvNextVaModel,
         ConvNextVaForImageClassification,
         ConvNextVaBackbone,
     )
     if is_torch_available()
     else ()
 )
 snake_case_  							=							(
     {"""feature-extraction""": ConvNextVaModel, """image-classification""": ConvNextVaForImageClassification}
     if is_torch_available()
     else {}
 )

 snake_case_  							=							False
 snake_case_  							=							False
 snake_case_  							=							False
 snake_case_  							=							False
 snake_case_  							=							False




 def      __magic_name__	(      self	:							Union[str, Any]      )	->						Tuple:
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=ConvNextVaModelTester(self      )
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=ConfigTester(self					,      config_class=UpperCAmelCase_					,      has_text_modality=UpperCAmelCase_					,      hidden_size=37      )




 def      __magic_name__	(      self	:							List[Any]      )	->						Optional[Any]:
       self.create_and_test_config_common_properties()
       self.config_tester.create_and_test_config_to_json_string()
       self.config_tester.create_and_test_config_to_json_file()
       self.config_tester.create_and_test_config_from_and_save_pretrained()
       self.config_tester.create_and_test_config_with_num_labels()
       self.config_tester.check_config_can_be_init_without_params()
       self.config_tester.check_config_arguments_init()




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




 @unittest.skip(reason='''ConvNextV2 does not use inputs_embeds'''      )
 def      __magic_name__	(      self	:							Optional[int]      )	->						Union[str, Any]:
       pass




 @unittest.skip(reason='''ConvNextV2 does not support input and output embeddings'''      )
 def      __magic_name__	(      self	:							Optional[Any]      )	->						List[str]:
       pass




 @unittest.skip(reason='''ConvNextV2 does not use feedforward chunking'''      )
 def      __magic_name__	(      self	:							Optional[int]      )	->						List[str]:
       pass




 def      __magic_name__	(      self	:							Dict      )	->						Optional[int]:



       if not self.model_tester.is_training:
             return

       for model_class in self.all_model_classes:
             SCREAMING_SNAKE_CASE__		,	SCREAMING_SNAKE_CASE__     :				List[Any]									=self.model_tester.prepare_config_and_inputs_with_labels()
             SCREAMING_SNAKE_CASE__     :				Optional[int]									=True

             if model_class.__name__ in [
                 *get_values(UpperCAmelCase_      ),
                 *get_values(UpperCAmelCase_      ),
             ]:
                   continue

             SCREAMING_SNAKE_CASE__     :				List[Any]									=model_class(UpperCAmelCase_      )
             model.to(UpperCAmelCase_      )
             model.train()
             SCREAMING_SNAKE_CASE__     :				Optional[Any]									=self._prepare_for_class(UpperCAmelCase_					,      UpperCAmelCase_					,      return_labels=UpperCAmelCase_      )
             SCREAMING_SNAKE_CASE__     :				int									=model(**UpperCAmelCase_      ).loss
             loss.backward()




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



       if not self.model_tester.is_training:
             return

       for model_class in self.all_model_classes:
             SCREAMING_SNAKE_CASE__		,	SCREAMING_SNAKE_CASE__     :				Tuple									=self.model_tester.prepare_config_and_inputs_with_labels()
             SCREAMING_SNAKE_CASE__     :				Any									=False
             SCREAMING_SNAKE_CASE__     :				Any									=True

             if (
                 model_class.__name__
                 in [*get_values(UpperCAmelCase_      ), *get_values(UpperCAmelCase_      )]
                 or not model_class.supports_gradient_checkpointing
             ):
                   continue

             SCREAMING_SNAKE_CASE__     :				Optional[int]									=model_class(UpperCAmelCase_      )
             model.to(UpperCAmelCase_      )
             model.gradient_checkpointing_enable()
             model.train()
             SCREAMING_SNAKE_CASE__     :				Tuple									=self._prepare_for_class(UpperCAmelCase_					,      UpperCAmelCase_					,      return_labels=UpperCAmelCase_      )
             SCREAMING_SNAKE_CASE__     :				Tuple									=model(**UpperCAmelCase_      ).loss
             loss.backward()




 def      __magic_name__	(      self	:							List[Any]      )	->						Union[str, Any]:
       SCREAMING_SNAKE_CASE__		,	SCREAMING_SNAKE_CASE__     :				int									=self.model_tester.prepare_config_and_inputs_for_common()

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

             SCREAMING_SNAKE_CASE__     :				int									=['''pixel_values''']
             self.assertListEqual(arg_names[:1]					,      UpperCAmelCase_      )




 def      __magic_name__	(      self	:							Optional[int]      )	->						Union[str, Any]:
       SCREAMING_SNAKE_CASE__     :				Dict									=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*UpperCAmelCase_      )




 def      __magic_name__	(      self	:							Optional[Any]      )	->						Dict:



       def check_hidden_states_output(__lowercase	:							int					,      __lowercase	:							Optional[Any]					,      __lowercase	:							str      ):
             SCREAMING_SNAKE_CASE__     :				Tuple									=model_class(UpperCAmelCase_      )
             model.to(UpperCAmelCase_      )
             model.eval()

             with torch.no_grad():
                   SCREAMING_SNAKE_CASE__     :				int									=model(**self._prepare_for_class(UpperCAmelCase_					,      UpperCAmelCase_      )      )

             SCREAMING_SNAKE_CASE__     :				List[Any]									=outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

             SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=self.model_tester.num_stages
             self.assertEqual(len(UpperCAmelCase_      )					,      expected_num_stages + 1      )

             # ConvNextV2's feature maps are of shape (batch_size, num_channels, height, width)
             self.assertListEqual(
                 list(hidden_states[0].shape[-2:]      )					,      [self.model_tester.image_size // 4, self.model_tester.image_size // 4]					,      )

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

       for model_class in self.all_model_classes:
             SCREAMING_SNAKE_CASE__     :				Any									=True
             check_hidden_states_output(UpperCAmelCase_					,      UpperCAmelCase_					,      UpperCAmelCase_      )

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

             check_hidden_states_output(UpperCAmelCase_					,      UpperCAmelCase_					,      UpperCAmelCase_      )




 def      __magic_name__	(      self	:							Union[str, Any]      )	->						Dict:
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*UpperCAmelCase_      )




 @slow
 def      __magic_name__	(      self	:							Tuple      )	->						str:



       for model_name in CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=ConvNextVaModel.from_pretrained(UpperCAmelCase_      )
             self.assertIsNotNone(UpperCAmelCase_      )





def  _a(  )     ->   str:




      '''simple docstring'''






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







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




 @cached_property
 def      __magic_name__	(      self	:							Union[str, Any]      )	->						Optional[int]:
       return AutoImageProcessor.from_pretrained('''facebook/convnextv2-tiny-1k-224'''      ) if is_vision_available() else None




 @slow
 def      __magic_name__	(      self	:							Tuple      )	->						int:
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=ConvNextVaForImageClassification.from_pretrained('''facebook/convnextv2-tiny-1k-224'''      ).to(UpperCAmelCase_      )

       SCREAMING_SNAKE_CASE__     :				List[str]									=self.default_image_processor
       SCREAMING_SNAKE_CASE__     :				Any									=prepare_img()
       SCREAMING_SNAKE_CASE__     :				int									=preprocessor(images=UpperCAmelCase_					,      return_tensors='''pt'''      ).to(UpperCAmelCase_      )

       # forward pass
       with torch.no_grad():
             SCREAMING_SNAKE_CASE__     :				Tuple									=model(**UpperCAmelCase_      )

       # verify the logits
       SCREAMING_SNAKE_CASE__     :				List[Any]									=torch.Size((1, 10_00)      )
       self.assertEqual(outputs.logits.shape					,      UpperCAmelCase_      )

       SCREAMING_SNAKE_CASE__     :				Optional[int]									=torch.tensor([0.9996, 0.1966, -0.4386]      ).to(UpperCAmelCase_      )
       self.assertTrue(torch.allclose(outputs.logits[0, :3]					,      UpperCAmelCase_					,      atol=1e-4      )      )







'''simple docstring'''
import copy
import inspect
import unittest

import numpy as np
from huggingface_hub import hf_hub_download

from transformers import TimesformerConfig
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,
      TimesformerForVideoClassification,
      TimesformerModel,
  )
  from transformers.models.timesformer.modeling_timesformer import TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
  from transformers import VideoMAEImageProcessor






class      __SCREAMING_SNAKE_CASE      :




 def __init__(      self	:							Optional[int]					,      __lowercase	:							Optional[int]					,      __lowercase	:							str=13					,      __lowercase	:							int=10					,      __lowercase	:							List[Any]=3					,      __lowercase	:							List[str]=2					,      __lowercase	:							int=2					,      __lowercase	:							Dict=True					,      __lowercase	:							Optional[Any]=True					,      __lowercase	:							int=32					,      __lowercase	:							List[Any]=5					,      __lowercase	:							Union[str, Any]=4					,      __lowercase	:							Any=37					,      __lowercase	:							Optional[Any]="gelu"					,      __lowercase	:							Union[str, Any]=0.1					,      __lowercase	:							Tuple=0.1					,      __lowercase	:							Dict=10					,      __lowercase	:							int=0.02					,      __lowercase	:							str="divided_space_time"					,      __lowercase	:							Union[str, Any]=None					,      )	->						Optional[Any]:
       SCREAMING_SNAKE_CASE__     :				int									=parent
       SCREAMING_SNAKE_CASE__     :				List[str]									=batch_size
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=image_size
       SCREAMING_SNAKE_CASE__     :				List[Any]									=num_channels
       SCREAMING_SNAKE_CASE__     :				int									=patch_size
       SCREAMING_SNAKE_CASE__     :				Tuple									=num_frames
       SCREAMING_SNAKE_CASE__     :				List[Any]									=is_training
       SCREAMING_SNAKE_CASE__     :				List[str]									=use_labels
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=hidden_size
       SCREAMING_SNAKE_CASE__     :				str									=num_hidden_layers
       SCREAMING_SNAKE_CASE__     :				int									=num_attention_heads
       SCREAMING_SNAKE_CASE__     :				Tuple									=intermediate_size
       SCREAMING_SNAKE_CASE__     :				List[str]									=hidden_act
       SCREAMING_SNAKE_CASE__     :				Dict									=hidden_dropout_prob
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=attention_probs_dropout_prob
       SCREAMING_SNAKE_CASE__     :				List[Any]									=attention_type
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=initializer_range
       SCREAMING_SNAKE_CASE__     :				Any									=scope
       SCREAMING_SNAKE_CASE__     :				int									=num_labels

       # in TimeSformer, the number of spatial tokens equals num_frames * num_patches per frame + 1 CLS token
       SCREAMING_SNAKE_CASE__     :				List[str]									=(image_size // patch_size) ** 2
       SCREAMING_SNAKE_CASE__     :				str									=(num_frames) * self.num_patches_per_frame + 1




 def      __magic_name__	(      self	:							Optional[int]      )	->						Tuple:
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=floats_tensor(
           [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]      )

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

       SCREAMING_SNAKE_CASE__     :				int									=self.get_config()

       return config, pixel_values, labels




 def      __magic_name__	(      self	:							int      )	->						Any:
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=TimesformerConfig(
           image_size=self.image_size					,      patch_size=self.patch_size					,      num_channels=self.num_channels					,      num_frames=self.num_frames					,      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					,      initializer_range=self.initializer_range					,      attention_type=self.attention_type					,      )
       SCREAMING_SNAKE_CASE__     :				List[Any]									=self.num_labels
       return config




 def      __magic_name__	(      self	:							int					,      __lowercase	:							Optional[int]					,      __lowercase	:							Dict					,      __lowercase	:							List[Any]      )	->						int:
       SCREAMING_SNAKE_CASE__     :				Tuple									=TimesformerModel(config=__lowercase      )
       model.to(__lowercase      )
       model.eval()
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=model(__lowercase      )
       self.parent.assertEqual(result.last_hidden_state.shape					,      (self.batch_size, self.seq_length, self.hidden_size)      )




 def      __magic_name__	(      self	:							List[str]					,      __lowercase	:							str					,      __lowercase	:							Tuple					,      __lowercase	:							List[Any]      )	->						List[Any]:
       SCREAMING_SNAKE_CASE__     :				str									=TimesformerForVideoClassification(__lowercase      )
       model.to(__lowercase      )
       model.eval()

       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=model(__lowercase      )

       # verify the logits shape
       SCREAMING_SNAKE_CASE__     :				Tuple									=torch.Size((self.batch_size, self.num_labels)      )
       self.parent.assertEqual(result.logits.shape					,      __lowercase      )




 def      __magic_name__	(      self	:							Any      )	->						List[Any]:
       SCREAMING_SNAKE_CASE__     :				int									=self.prepare_config_and_inputs()
       SCREAMING_SNAKE_CASE__		,	SCREAMING_SNAKE_CASE__		,	SCREAMING_SNAKE_CASE__     :				str									=config_and_inputs
       SCREAMING_SNAKE_CASE__     :				Any									={'''pixel_values''': pixel_values}
       return config, inputs_dict






@require_torch
class      __SCREAMING_SNAKE_CASE      (			lowerCamelCase	,				lowerCamelCase	,				unittest.TestCase				):
 snake_case_  							=							(TimesformerModel, TimesformerForVideoClassification) if is_torch_available() else ()
 snake_case_  							=							(
     {"""feature-extraction""": TimesformerModel, """video-classification""": TimesformerForVideoClassification}
     if is_torch_available()
     else {}
 )

 snake_case_  							=							False
 snake_case_  							=							False
 snake_case_  							=							False
 snake_case_  							=							False




 def      __magic_name__	(      self	:							str      )	->						Optional[int]:
       SCREAMING_SNAKE_CASE__     :				Dict									=TimesformerModelTester(self      )
       SCREAMING_SNAKE_CASE__     :				List[Any]									=ConfigTester(
           self					,      config_class=__lowercase					,      has_text_modality=__lowercase					,      hidden_size=37      )




 def      __magic_name__	(      self	:							str					,      __lowercase	:							Dict					,      __lowercase	:							str					,      __lowercase	:							Optional[int]=False      )	->						int:
       SCREAMING_SNAKE_CASE__     :				str									=copy.deepcopy(__lowercase      )

       if return_labels:
             if model_class in get_values(__lowercase      ):
                   SCREAMING_SNAKE_CASE__     :				Any									=torch.zeros(
                       self.model_tester.batch_size					,      dtype=torch.long					,      device=__lowercase      )

       return inputs_dict




 def      __magic_name__	(      self	:							List[Any]      )	->						Any:
       self.config_tester.run_common_tests()




 @unittest.skip(reason='''TimeSformer does not use inputs_embeds'''      )
 def      __magic_name__	(      self	:							List[Any]      )	->						Optional[int]:
       pass




 def      __magic_name__	(      self	:							str      )	->						Tuple:
       SCREAMING_SNAKE_CASE__		,	SCREAMING_SNAKE_CASE__     :				List[Any]									=self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
             SCREAMING_SNAKE_CASE__     :				str									=model_class(__lowercase      )
             self.assertIsInstance(model.get_input_embeddings()					,      (nn.Module)      )
             SCREAMING_SNAKE_CASE__     :				Any									=model.get_output_embeddings()
             self.assertTrue(x is None or isinstance(__lowercase					,      nn.Linear      )      )




 def      __magic_name__	(      self	:							Any      )	->						Any:
       SCREAMING_SNAKE_CASE__		,	SCREAMING_SNAKE_CASE__     :				str									=self.model_tester.prepare_config_and_inputs_for_common()

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

             SCREAMING_SNAKE_CASE__     :				Dict									=['''pixel_values''']
             self.assertListEqual(arg_names[:1]					,      __lowercase      )




 def      __magic_name__	(      self	:							int      )	->						Dict:
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*__lowercase      )




 def      __magic_name__	(      self	:							List[str]      )	->						Optional[int]:
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_video_classification(*__lowercase      )




 @slow
 def      __magic_name__	(      self	:							Optional[int]      )	->						Optional[Any]:
       for model_name in TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=TimesformerModel.from_pretrained(__lowercase      )
             self.assertIsNotNone(__lowercase      )




 def      __magic_name__	(      self	:							Union[str, Any]      )	->						List[str]:
       if not self.has_attentions:
             pass

       else:
             SCREAMING_SNAKE_CASE__		,	SCREAMING_SNAKE_CASE__     :				Any									=self.model_tester.prepare_config_and_inputs_for_common()
             SCREAMING_SNAKE_CASE__     :				List[Any]									=True

             for model_class in self.all_model_classes:
                   SCREAMING_SNAKE_CASE__     :				List[Any]									=self.model_tester.seq_length
                   SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=self.model_tester.num_frames

                   SCREAMING_SNAKE_CASE__     :				Optional[Any]									=True
                   SCREAMING_SNAKE_CASE__     :				str									=False
                   SCREAMING_SNAKE_CASE__     :				Tuple									=True
                   SCREAMING_SNAKE_CASE__     :				Dict									=model_class(__lowercase      )
                   model.to(__lowercase      )
                   model.eval()
                   with torch.no_grad():
                         SCREAMING_SNAKE_CASE__     :				Optional[Any]									=model(**self._prepare_for_class(__lowercase					,      __lowercase      )      )
                   SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=outputs.attentions
                   self.assertEqual(len(__lowercase      )					,      self.model_tester.num_hidden_layers      )

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

                   # attentions has shape (batch_size x num_frames) x num_heads x (num_patches per frame + 1) x (num_patches per frame + 1)
                   self.assertListEqual(
                       list(attentions[0].shape[-3:]      )					,      [self.model_tester.num_attention_heads, seq_len // num_frames + 1, seq_len // num_frames + 1]					,      )
                   SCREAMING_SNAKE_CASE__     :				Optional[int]									=len(__lowercase      )

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

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

                   SCREAMING_SNAKE_CASE__     :				List[str]									=outputs.attentions

                   self.assertEqual(len(__lowercase      )					,      self.model_tester.num_hidden_layers      )

                   # attentions has shape (batch_size x num_frames) x num_heads x (num_patches per frame + 1) x (num_patches per frame + 1)
                   self.assertListEqual(
                       list(self_attentions[0].shape[-3:]      )					,      [self.model_tester.num_attention_heads, seq_len // num_frames + 1, seq_len // num_frames + 1]					,      )




 def      __magic_name__	(      self	:							Tuple      )	->						List[Any]:
       def check_hidden_states_output(__lowercase	:							Tuple					,      __lowercase	:							Dict					,      __lowercase	:							Optional[int]      ):
             SCREAMING_SNAKE_CASE__     :				List[Any]									=model_class(__lowercase      )
             model.to(__lowercase      )
             model.eval()

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

             SCREAMING_SNAKE_CASE__     :				List[Any]									=outputs.hidden_states
             SCREAMING_SNAKE_CASE__     :				Tuple									=self.model_tester.num_hidden_layers + 1
             self.assertEqual(len(__lowercase      )					,      __lowercase      )

             SCREAMING_SNAKE_CASE__     :				int									=self.model_tester.seq_length

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

       SCREAMING_SNAKE_CASE__		,	SCREAMING_SNAKE_CASE__     :				int									=self.model_tester.prepare_config_and_inputs_for_common()

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

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

             check_hidden_states_output(__lowercase					,      __lowercase					,      __lowercase      )





def  _a(  ):




      '''simple docstring'''






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







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




 @cached_property
 def      __magic_name__	(      self	:							Any      )	->						List[str]:
       # logits were tested with a different mean and std, so we use the same here
       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      __magic_name__	(      self	:							Any      )	->						Optional[Any]:
       SCREAMING_SNAKE_CASE__     :				int									=TimesformerForVideoClassification.from_pretrained('''facebook/timesformer-base-finetuned-k400'''      ).to(
           __lowercase      )

       SCREAMING_SNAKE_CASE__     :				List[str]									=self.default_image_processor
       SCREAMING_SNAKE_CASE__     :				Tuple									=prepare_video()
       SCREAMING_SNAKE_CASE__     :				Any									=image_processor(video[:8]					,      return_tensors='''pt'''      ).to(__lowercase      )

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

       # verify the logits
       SCREAMING_SNAKE_CASE__     :				List[str]									=torch.Size((1, 4_00)      )
       self.assertEqual(outputs.logits.shape					,      __lowercase      )

       SCREAMING_SNAKE_CASE__     :				List[Any]									=torch.tensor([-0.3016, -0.7713, -0.4205]      ).to(__lowercase      )

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