






'''simple docstring'''







import copy
import inspect
import unittest

import numpy as np
from huggingface_hub import hf_hub_download

from transformers import VivitConfig
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


if is_torch_available():
  import torch
  from torch import nn

  from transformers import MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING, VivitForVideoClassification, VivitModel
  from transformers.models.vivit.modeling_vivit import VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
  from transformers import VivitImageProcessor




class 					__UpperCAmelCase :

      '''simple docstring'''





      def __init__(					self  ,		_UpperCAmelCase  ,		_UpperCAmelCase=2  ,		_UpperCAmelCase=True  ,		_UpperCAmelCase=True  ,		_UpperCAmelCase=10  ,		_UpperCAmelCase=10  ,		_UpperCAmelCase=8  ,		_UpperCAmelCase=[2, 4, 4]  ,		_UpperCAmelCase=3  ,		_UpperCAmelCase=768  ,		_UpperCAmelCase=5  ,		_UpperCAmelCase=4  ,		_UpperCAmelCase=37  ,		_UpperCAmelCase="gelu_fast"  ,		_UpperCAmelCase=0.0  ,		_UpperCAmelCase=0.0  ,		_UpperCAmelCase=0.02  ,		_UpperCAmelCase=1E-06  ,		_UpperCAmelCase=True  ,		_UpperCAmelCase=None  ,		):
             UpperCAmelCase__	:   List[str]												=	parent
             UpperCAmelCase__	:   Tuple												=	batch_size
             UpperCAmelCase__	:   int												=	is_training
             UpperCAmelCase__	:   Optional[Any]												=	use_labels
             UpperCAmelCase__	:   Optional[int]												=	num_labels
             UpperCAmelCase__	:   List[Any]												=	image_size
             UpperCAmelCase__	:   List[Any]												=	num_frames
             UpperCAmelCase__	:   Optional[Any]												=	tubelet_size
             UpperCAmelCase__	:   List[str]												=	num_channels
             UpperCAmelCase__	:   Optional[Any]												=	hidden_size
             UpperCAmelCase__	:   Optional[Any]												=	num_hidden_layers
             UpperCAmelCase__	:   Dict												=	num_attention_heads
             UpperCAmelCase__	:   Union[str, Any]												=	intermediate_size
             UpperCAmelCase__	:   int												=	hidden_act
             UpperCAmelCase__	:   Any												=	hidden_dropout_prob
             UpperCAmelCase__	:   Any												=	attention_probs_dropout_prob
             UpperCAmelCase__	:   Optional[int]												=	initializer_range
             UpperCAmelCase__	:   Optional[int]												=	layer_norm_eps
             UpperCAmelCase__	:   Optional[int]												=	qkv_bias
             UpperCAmelCase__	:   List[str]												=	scope

             UpperCAmelCase__	:   Union[str, Any]												=	(
                 (self.image_size // self.tubelet_size[2])
                 * (self.image_size // self.tubelet_size[1])
                 * (self.num_frames // self.tubelet_size[0])
             ) + 1  # CLS token




      def 							lowerCamelCase     (					self						):
             UpperCAmelCase__	:   int												=	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__	:   str												=	ids_tensor([self.batch_size]  ,		self.num_labels						)

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

             return config, pixel_values, labels




      def 							lowerCamelCase     (					self						):
             UpperCAmelCase__	:   Dict												=	VivitConfig(
                 num_frames=self.num_frames  ,		image_size=self.image_size  ,		tubelet_size=self.tubelet_size  ,		num_channels=self.num_channels  ,		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  ,		layer_norm_eps=self.layer_norm_eps  ,		qkv_bias=self.qkv_bias  ,		)
             UpperCAmelCase__	:   Tuple												=	self.num_labels
             return config




      def 							lowerCamelCase     (					self  ,		_UpperCAmelCase  ,		_UpperCAmelCase  ,		_UpperCAmelCase						):
             UpperCAmelCase__	:   int												=	VivitModel(config=_UpperCAmelCase						)
             model.to(_UpperCAmelCase						)
             model.eval()
             UpperCAmelCase__	:   List[Any]												=	model(_UpperCAmelCase						)
             self.parent.assertEqual(result.last_hidden_state.shape  ,		(self.batch_size, self.seq_length, self.hidden_size)						)




      def 							lowerCamelCase     (					self  ,		_UpperCAmelCase  ,		_UpperCAmelCase  ,		_UpperCAmelCase						):
             UpperCAmelCase__	:   int												=	VivitForVideoClassification(_UpperCAmelCase						)
             model.to(_UpperCAmelCase						)
             model.eval()

             UpperCAmelCase__	:   int												=	model(_UpperCAmelCase						)

             # verify the logits shape
             UpperCAmelCase__	:   Optional[int]												=	torch.Size((self.batch_size, self.num_labels)						)
             self.parent.assertEqual(result.logits.shape  ,		_UpperCAmelCase						)




      def 							lowerCamelCase     (					self						):
             UpperCAmelCase__	:   str												=	self.prepare_config_and_inputs()
             UpperCAmelCase__	:   Tuple												=	config_and_inputs
             UpperCAmelCase__	:   Any												=	{"""pixel_values""": pixel_values}
             return config, inputs_dict




@require_torch
class 					__UpperCAmelCase (		_SCREAMING_SNAKE_CASE      ,				unittest.TestCase	):

      '''simple docstring'''





      SCREAMING_SNAKE_CASE				:    Tuple				       =					(VivitModel, VivitForVideoClassification) if is_torch_available() else ()

      SCREAMING_SNAKE_CASE				:    Tuple				       =					False
      SCREAMING_SNAKE_CASE				:    Tuple				       =					False
      SCREAMING_SNAKE_CASE				:    Optional[int]				       =					False
      SCREAMING_SNAKE_CASE				:    List[str]				       =					False




      def 							lowerCamelCase     (					self						):
             UpperCAmelCase__	:   Dict												=	VivitModelTester(self						)
             UpperCAmelCase__	:   Tuple												=	ConfigTester(self  ,		config_class=_UpperCAmelCase  ,		has_text_modality=_UpperCAmelCase  ,		hidden_size=37						)




      def 							lowerCamelCase     (					self  ,		_UpperCAmelCase  ,		_UpperCAmelCase  ,		_UpperCAmelCase=False						):
             UpperCAmelCase__	:   Dict												=	copy.deepcopy(_UpperCAmelCase						)

             if return_labels:
                    if model_class in get_values(_UpperCAmelCase						):
                           UpperCAmelCase__	:   List[Any]												=	torch.zeros(
                               self.model_tester.batch_size  ,		dtype=torch.long  ,		device=_UpperCAmelCase						)

             return inputs_dict




      def 							lowerCamelCase     (					self						):
             self.config_tester.run_common_tests()




      @unittest.skip(reason='''Vivit does not use inputs_embeds'''						)
      def 							lowerCamelCase     (					self						):
             pass




      def 							lowerCamelCase     (					self						):
             UpperCAmelCase__	:   int												=	self.model_tester.prepare_config_and_inputs_for_common()

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




      def 							lowerCamelCase     (					self						):
             UpperCAmelCase__	:   Any												=	self.model_tester.prepare_config_and_inputs_for_common()

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

                    UpperCAmelCase__	:   List[str]												=	["""pixel_values""", """head_mask"""]
                    self.assertListEqual(arg_names[:2]  ,		_UpperCAmelCase						)




      def 							lowerCamelCase     (					self						):
             UpperCAmelCase__	:   Any												=	self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*_UpperCAmelCase						)




      def 							lowerCamelCase     (					self						):
             UpperCAmelCase__	:   Any												=	self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_video_classification(*_UpperCAmelCase						)




      @slow
      def 							lowerCamelCase     (					self						):





             for model_name in VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                    UpperCAmelCase__	:   int												=	VivitModel.from_pretrained(_UpperCAmelCase						)
                    self.assertIsNotNone(_UpperCAmelCase						)




      def 							lowerCamelCase     (					self						):
             UpperCAmelCase__	:   str												=	self.model_tester.prepare_config_and_inputs_for_common()
             UpperCAmelCase__	:   Tuple												=	True

             for model_class in self.all_model_classes:
                    UpperCAmelCase__	:   Tuple												=	self.model_tester.seq_length

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

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

                    self.assertListEqual(
                        list(attentions[0].shape[-3:]						)  ,		[self.model_tester.num_attention_heads, seq_len, seq_len]  ,		)
                    UpperCAmelCase__	:   Any												=	len(_UpperCAmelCase						)

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

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

                    UpperCAmelCase__	:   Dict												=	outputs.attentions

                    self.assertEqual(len(_UpperCAmelCase						)  ,		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						):





             def check_hidden_states_output(_UpperCAmelCase  ,		_UpperCAmelCase  ,		_UpperCAmelCase						):
                    UpperCAmelCase__	:   Tuple												=	model_class(_UpperCAmelCase						)
                    model.to(_UpperCAmelCase						)
                    model.eval()

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

                    UpperCAmelCase__	:   Tuple												=	outputs.hidden_states
                    UpperCAmelCase__	:   Any												=	self.model_tester.num_hidden_layers + 1
                    self.assertEqual(len(_UpperCAmelCase						)  ,		_UpperCAmelCase						)

                    UpperCAmelCase__	:   List[str]												=	self.model_tester.seq_length

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

             UpperCAmelCase__	:   Tuple												=	self.model_tester.prepare_config_and_inputs_for_common()

             for model_class in self.all_model_classes:
                    UpperCAmelCase__	:   Tuple												=	True
                    check_hidden_states_output(_UpperCAmelCase  ,		_UpperCAmelCase  ,		_UpperCAmelCase						)

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

                    check_hidden_states_output(_UpperCAmelCase  ,		_UpperCAmelCase  ,		_UpperCAmelCase						)




      @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''						)
      def 							lowerCamelCase     (					self						):
             pass






def  lowerCAmelCase__		(					)   -> Any:
       UpperCAmelCase__	:   str												=	hf_hub_download(
           repo_id='''hf-internal-testing/spaghetti-video'''			,						filename='''eating_spaghetti_32_frames.npy'''			,						repo_type='''dataset'''			)
       UpperCAmelCase__	:   Optional[Any]												=	np.load(__SCREAMING_SNAKE_CASE			)
       return list(__SCREAMING_SNAKE_CASE			)




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

      '''simple docstring'''





      @cached_property
      def 							lowerCamelCase     (					self						):
             return VivitImageProcessor() if is_vision_available() else None




      @slow
      def 							lowerCamelCase     (					self						):
             UpperCAmelCase__	:   List[str]												=	VivitForVideoClassification.from_pretrained('''google/vivit-b-16x2-kinetics400'''						).to(_UpperCAmelCase						)

             UpperCAmelCase__	:   List[str]												=	self.default_image_processor
             UpperCAmelCase__	:   Optional[Any]												=	prepare_video()
             UpperCAmelCase__	:   str												=	image_processor(_UpperCAmelCase  ,		return_tensors='''pt'''						).to(_UpperCAmelCase						)

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

             # verify the logits
             UpperCAmelCase__	:   Union[str, Any]												=	torch.Size((1, 400)						)
             self.assertEqual(outputs.logits.shape  ,		_UpperCAmelCase						)

             # taken from original model
             UpperCAmelCase__	:   str												=	torch.tensor([-1.05_43, 2.07_64, -0.21_04, 0.44_39, -0.96_58]						).to(_UpperCAmelCase						)

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

'''simple docstring'''
import copy

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


A__ :		str					=logging.get_logger(__name__)

A__ :		Optional[int]					={
    'facebook/sam-vit-huge': 'https://huggingface.co/facebook/sam-vit-huge/resolve/main/config.json',
    'facebook/sam-vit-large': 'https://huggingface.co/facebook/sam-vit-large/resolve/main/config.json',
    'facebook/sam-vit-base': 'https://huggingface.co/facebook/sam-vit-base/resolve/main/config.json',
}







class 			__A					(			_SCREAMING_SNAKE_CASE      ):
 def __init__( self      :     Tuple					, lowerCamelCase      :     Tuple=2_56					, lowerCamelCase      :     Union[str, Any]=10_24					, lowerCamelCase      :     List[str]=16					, lowerCamelCase      :     str=16					, lowerCamelCase      :     Union[str, Any]=4					, lowerCamelCase      :     Optional[Any]="gelu"					, lowerCamelCase      :     Union[str, Any]=1e-6					, **lowerCamelCase      :     Union[str, Any]					, ):



   """simple docstring"""





   super().__init__(**lowerCamelCase							)
   __A		:  List[str]       				=		hidden_size
   __A		:  Any       				=		image_size
   __A		:  Dict       				=		patch_size
   __A		:  Union[str, Any]       				=		image_size // patch_size
   __A		:  List[Any]       				=		mask_input_channels
   __A		:  str       				=		num_point_embeddings
   __A		:  Tuple       				=		hidden_act
   __A		:  List[str]       				=		layer_norm_eps







class 			__A					(			_SCREAMING_SNAKE_CASE      ):
 def __init__( self      :     Optional[int]					, lowerCamelCase      :     int=2_56					, lowerCamelCase      :     Tuple="relu"					, lowerCamelCase      :     Optional[Any]=20_48					, lowerCamelCase      :     int=2					, lowerCamelCase      :     Tuple=8					, lowerCamelCase      :     int=2					, lowerCamelCase      :     List[str]=3					, lowerCamelCase      :     Dict=3					, lowerCamelCase      :     Any=2_56					, lowerCamelCase      :     int=1e-6					, **lowerCamelCase      :     Tuple					, ):



   """simple docstring"""





   super().__init__(**lowerCamelCase							)
   __A		:  List[Any]       				=		hidden_size
   __A		:  Union[str, Any]       				=		hidden_act
   __A		:  Tuple       				=		mlp_dim
   __A		:  Any       				=		num_hidden_layers
   __A		:  Dict       				=		num_attention_heads
   __A		:  Optional[Any]       				=		attention_downsample_rate
   __A		:  List[Any]       				=		num_multimask_outputs
   __A		:  Optional[Any]       				=		iou_head_depth
   __A		:  Optional[int]       				=		iou_head_hidden_dim
   __A		:  Any       				=		layer_norm_eps







class 			__A					(			_SCREAMING_SNAKE_CASE      ):
 def __init__( self      :     Optional[int]					, lowerCamelCase      :     List[str]=7_68					, lowerCamelCase      :     Tuple=2_56					, lowerCamelCase      :     Optional[Any]=12					, lowerCamelCase      :     Tuple=12					, lowerCamelCase      :     int=3					, lowerCamelCase      :     Dict=10_24					, lowerCamelCase      :     int=16					, lowerCamelCase      :     Any="gelu"					, lowerCamelCase      :     Tuple=1e-0_6					, lowerCamelCase      :     Optional[int]=0.0					, lowerCamelCase      :     List[str]=1e-1_0					, lowerCamelCase      :     Optional[int]=True					, lowerCamelCase      :     Union[str, Any]=4.0					, lowerCamelCase      :     List[Any]=True					, lowerCamelCase      :     Optional[Any]=True					, lowerCamelCase      :     str=14					, lowerCamelCase      :     str=[2, 5, 8, 11]					, lowerCamelCase      :     Dict=1_28					, lowerCamelCase      :     Any=None					, **lowerCamelCase      :     Optional[int]					, ):



   """simple docstring"""





   super().__init__(**lowerCamelCase							)

   __A		:  Any       				=		hidden_size
   __A		:  Union[str, Any]       				=		output_channels
   __A		:  int       				=		num_hidden_layers
   __A		:  Any       				=		num_attention_heads
   __A		:  Tuple       				=		num_channels
   __A		:  Tuple       				=		image_size
   __A		:  Optional[Any]       				=		patch_size
   __A		:  List[str]       				=		hidden_act
   __A		:  Dict       				=		layer_norm_eps
   __A		:  Optional[int]       				=		attention_dropout
   __A		:  Dict       				=		initializer_range
   __A		:  Union[str, Any]       				=		qkv_bias
   __A		:  Optional[Any]       				=		mlp_ratio
   __A		:  Optional[int]       				=		use_abs_pos
   __A		:  int       				=		use_rel_pos
   __A		:  str       				=		window_size
   __A		:  Union[str, Any]       				=		global_attn_indexes
   __A		:  Dict       				=		num_pos_feats
   __A		:  int       				=		int(hidden_size * mlp_ratio							) if mlp_dim is None else mlp_dim







class 			__A					(			_SCREAMING_SNAKE_CASE      ):
 lowerCamelCase												='''sam'''
 lowerCamelCase												=True
 def __init__( self      :     Optional[int]					, lowerCamelCase      :     Dict=None					, lowerCamelCase      :     Dict=None					, lowerCamelCase      :     Optional[Any]=None					, lowerCamelCase      :     List[Any]=0.02					, **lowerCamelCase      :     Any					, ):



   """simple docstring"""





   super().__init__(**lowerCamelCase							)
   __A		:  str       				=		vision_config if vision_config is not None else {}
   __A		:  int       				=		prompt_encoder_config if prompt_encoder_config is not None else {}
   __A		:  Dict       				=		mask_decoder_config if mask_decoder_config is not None else {}

   if isinstance(lowerCamelCase					, lowerCamelCase							):
     __A		:  List[str]       				=		vision_config.to_dict()
   if isinstance(lowerCamelCase					, lowerCamelCase							):
     __A		:  Any       				=		prompt_encoder_config.to_dict()
   if isinstance(lowerCamelCase					, lowerCamelCase							):
     __A		:  Any       				=		mask_decoder_config.to_dict()

   __A		:  Any       				=		SamVisionConfig(**lowerCamelCase							)
   __A		:  List[Any]       				=		SamPromptEncoderConfig(**lowerCamelCase							)
   __A		:  Union[str, Any]       				=		SamMaskDecoderConfig(**lowerCamelCase							)
   __A		:  Any       				=		initializer_range






 def 		lowercase_( self      :     List[Any]							):



   """simple docstring"""





   __A		:  str       				=		copy.deepcopy(self.__dict__							)
   __A		:  Dict       				=		self.vision_config.to_dict()
   __A		:  Any       				=		self.prompt_encoder_config.to_dict()
   __A		:  str       				=		self.mask_decoder_config.to_dict()
   __A		:  Optional[int]       				=		self.__class__.model_type
   return output



