




import collections
import inspect
import unittest

from transformers import DonutSwinConfig
from transformers.testing_utils import require_torch, slow, torch_device
from transformers.utils import is_torch_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
	import torch
	from torch import nn

	from transformers import DonutSwinModel
	from transformers.models.donut.modeling_donut_swin import DONUT_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST


class  lowerCamelCase     :






			'''simple docstring'''





			def __init__(			self      : Any							,    lowerCamelCase_      : str							,    lowerCamelCase_      : Optional[int]=13							,    lowerCamelCase_      : Optional[Any]=32							,    lowerCamelCase_      : Dict=2							,    lowerCamelCase_      : Optional[int]=3							,    lowerCamelCase_      : Union[str, Any]=16							,    lowerCamelCase_      : Optional[int]=[1, 2, 1]							,    lowerCamelCase_      : Dict=[2, 2, 4]							,    lowerCamelCase_      : Optional[int]=2							,    lowerCamelCase_      : Union[str, Any]=2.0							,    lowerCamelCase_      : Optional[Any]=True							,    lowerCamelCase_      : Optional[Any]=0.0							,    lowerCamelCase_      : Tuple=0.0							,    lowerCamelCase_      : Tuple=0.1							,    lowerCamelCase_      : int="gelu"							,    lowerCamelCase_      : Union[str, Any]=False							,    lowerCamelCase_      : Union[str, Any]=True							,    lowerCamelCase_      : Any=0.0_2							,    lowerCamelCase_      : str=1E-5							,    lowerCamelCase_      : Optional[int]=True							,    lowerCamelCase_      : Optional[Any]=None							,    lowerCamelCase_      : Any=True							,    lowerCamelCase_      : Tuple=10							,    lowerCamelCase_      : Dict=8							,    )    -> Optional[int]:
								__magic_name__				:						Union[str, Any]     				=				parent
								__magic_name__				:						Dict     				=				batch_size
								__magic_name__				:						Optional[Any]     				=				image_size
								__magic_name__				:						int     				=				patch_size
								__magic_name__				:						str     				=				num_channels
								__magic_name__				:						List[Any]     				=				embed_dim
								__magic_name__				:						Optional[Any]     				=				depths
								__magic_name__				:						Any     				=				num_heads
								__magic_name__				:						Dict     				=				window_size
								__magic_name__				:						Tuple     				=				mlp_ratio
								__magic_name__				:						Union[str, Any]     				=				qkv_bias
								__magic_name__				:						List[str]     				=				hidden_dropout_prob
								__magic_name__				:						Optional[int]     				=				attention_probs_dropout_prob
								__magic_name__				:						Optional[Any]     				=				drop_path_rate
								__magic_name__				:						List[Any]     				=				hidden_act
								__magic_name__				:						Tuple     				=				use_absolute_embeddings
								__magic_name__				:						Tuple     				=				patch_norm
								__magic_name__				:						int     				=				layer_norm_eps
								__magic_name__				:						Optional[int]     				=				initializer_range
								__magic_name__				:						str     				=				is_training
								__magic_name__				:						Optional[Any]     				=				scope
								__magic_name__				:						Dict     				=				use_labels
								__magic_name__				:						Optional[Any]     				=				type_sequence_label_size
								__magic_name__				:						Dict     				=				encoder_stride

			def 				UpperCAmelCase__							(			self      : Union[str, Any]	)    -> Tuple:
								__magic_name__				:						str     				=				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

								__magic_name__				:						int     				=				None
								if self.use_labels:
													__magic_name__				:						Tuple     				=				ids_tensor([self.batch_size]							,    self.type_sequence_label_size	)

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

								return config, pixel_values, labels

			def 				UpperCAmelCase__							(			self      : Tuple	)    -> Optional[Any]:
								return DonutSwinConfig(
								    image_size=self.image_size							,    patch_size=self.patch_size							,    num_channels=self.num_channels							,    embed_dim=self.embed_dim							,    depths=self.depths							,    num_heads=self.num_heads							,    window_size=self.window_size							,    mlp_ratio=self.mlp_ratio							,    qkv_bias=self.qkv_bias							,    hidden_dropout_prob=self.hidden_dropout_prob							,    attention_probs_dropout_prob=self.attention_probs_dropout_prob							,    drop_path_rate=self.drop_path_rate							,    hidden_act=self.hidden_act							,    use_absolute_embeddings=self.use_absolute_embeddings							,    path_norm=self.patch_norm							,    layer_norm_eps=self.layer_norm_eps							,    initializer_range=self.initializer_range							,    encoder_stride=self.encoder_stride							,    )

			def 				UpperCAmelCase__							(			self      : Dict							,    lowerCamelCase_      : str							,    lowerCamelCase_      : List[str]							,    lowerCamelCase_      : List[str]	)    -> Union[str, Any]:
								__magic_name__				:						str     				=				DonutSwinModel(config=__lowerCamelCase	)
								model.to(__lowerCamelCase	)
								model.eval()
								__magic_name__				:						Union[str, Any]     				=				model(__lowerCamelCase	)

								__magic_name__				:						str     				=				((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths	) - 1))
								__magic_name__				:						str     				=				int(config.embed_dim * 2 ** (len(config.depths	) - 1)	)

								self.parent.assertEqual(result.last_hidden_state.shape							,    (self.batch_size, expected_seq_len, expected_dim)	)


			def 				UpperCAmelCase__							(			self      : int	)    -> Tuple:
								__magic_name__				:						str     				=				self.prepare_config_and_inputs()
								(
								    __magic_name__  
								)				:						Dict     				=				config_and_inputs
								__magic_name__				:						Any     				=				{"pixel_values": pixel_values}
								return config, inputs_dict






@require_torch
class  lowerCamelCase     (		lowercase__					,lowercase__					,unittest.TestCase    ):






			'''simple docstring'''





			UpperCamelCase__										=(DonutSwinModel,) if is_torch_available() else ()
			UpperCamelCase__										={'''feature-extraction''': DonutSwinModel} if is_torch_available() else {}
			UpperCamelCase__										=True

			UpperCamelCase__										=False
			UpperCamelCase__										=False
			UpperCamelCase__										=False

			def 				UpperCAmelCase__							(			self      : Optional[Any]	)    -> List[str]:
								__magic_name__				:						List[Any]     				=				DonutSwinModelTester(self	)
								__magic_name__				:						str     				=				ConfigTester(self							,    config_class=__lowerCamelCase							,    embed_dim=37	)

			def 				UpperCAmelCase__							(			self      : Optional[int]	)    -> 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 				UpperCAmelCase__							(			self      : Optional[int]	)    -> List[str]:
								return

			def 				UpperCAmelCase__							(			self      : int	)    -> Any:
								__magic_name__				:						str     				=				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*__lowerCamelCase	)

			def 				UpperCAmelCase__							(			self      : Dict	)    -> Any:
								pass

			def 				UpperCAmelCase__							(			self      : List[Any]	)    -> List[Any]:
								__magic_name__				:						int     				=				self.model_tester.prepare_config_and_inputs_for_common()

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

			def 				UpperCAmelCase__							(			self      : Optional[int]	)    -> str:
								__magic_name__				:						List[Any]     				=				self.model_tester.prepare_config_and_inputs_for_common()

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

													__magic_name__				:						Dict     				=				["pixel_values"]
													self.assertListEqual(arg_names[:1]							,    __lowerCamelCase	)

			def 				UpperCAmelCase__							(			self      : str	)    -> Optional[Any]:
								__magic_name__				:						List[Any]     				=				self.model_tester.prepare_config_and_inputs_for_common()
								__magic_name__				:						List[Any]     				=				True

								for model_class in self.all_model_classes:
													__magic_name__				:						int     				=				True
													__magic_name__				:						Union[str, Any]     				=				False
													__magic_name__				:						Dict     				=				True
													__magic_name__				:						Any     				=				model_class(__lowerCamelCase	)
													model.to(__lowerCamelCase	)
													model.eval()
													with torch.no_grad():
																		__magic_name__				:						Dict     				=				model(**self._prepare_for_class(__lowerCamelCase							,    __lowerCamelCase	)	)
													__magic_name__				:						Tuple     				=				outputs.attentions
													__magic_name__				:						Any     				=				len(self.model_tester.depths	)
													self.assertEqual(len(__lowerCamelCase	)							,    __lowerCamelCase	)

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

													self.assertListEqual(
													    list(attentions[0].shape[-3:]	)							,    [self.model_tester.num_heads[0], window_size_squared, window_size_squared]							,    )
													__magic_name__				:						str     				=				len(__lowerCamelCase	)

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

													if hasattr(self.model_tester							,    '''num_hidden_states_types'''	):
																		__magic_name__				:						Union[str, Any]     				=				self.model_tester.num_hidden_states_types
													else:
																		# also another +1 for reshaped_hidden_states
																		__magic_name__				:						str     				=				2
													self.assertEqual(out_len + added_hidden_states							,    len(__lowerCamelCase	)	)

													__magic_name__				:						str     				=				outputs.attentions

													self.assertEqual(len(__lowerCamelCase	)							,    __lowerCamelCase	)

													self.assertListEqual(
													    list(self_attentions[0].shape[-3:]	)							,    [self.model_tester.num_heads[0], window_size_squared, window_size_squared]							,    )

			def 				UpperCAmelCase__							(			self      : int							,    lowerCamelCase_      : Dict							,    lowerCamelCase_      : str							,    lowerCamelCase_      : List[str]							,    lowerCamelCase_      : Optional[int]	)    -> Union[str, Any]:
								__magic_name__				:						Optional[int]     				=				model_class(__lowerCamelCase	)
								model.to(__lowerCamelCase	)
								model.eval()

								with torch.no_grad():
													__magic_name__				:						Optional[Any]     				=				model(**self._prepare_for_class(__lowerCamelCase							,    __lowerCamelCase	)	)

								__magic_name__				:						Union[str, Any]     				=				outputs.hidden_states

								__magic_name__				:						Any     				=				getattr(
								    self.model_tester							,    '''expected_num_hidden_layers'''							,    len(self.model_tester.depths	) + 1	)
								self.assertEqual(len(__lowerCamelCase	)							,    __lowerCamelCase	)

								# DonutSwin has a different seq_length
								__magic_name__				:						Dict     				=				(
								    config.patch_size
								    if isinstance(config.patch_size							,    collections.abc.Iterable	)
								    else (config.patch_size, config.patch_size)
								)

								__magic_name__				:						Union[str, Any]     				=				(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

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

								__magic_name__				:						Optional[int]     				=				outputs.reshaped_hidden_states
								self.assertEqual(len(__lowerCamelCase	)							,    __lowerCamelCase	)

								__magic_name__				:						List[Any]     				=				reshaped_hidden_states[0].shape
								__magic_name__				:						str     				=				(
								    reshaped_hidden_states[0].view(__lowerCamelCase							,    __lowerCamelCase							,    height * width	).permute(0							,    2							,    1	)
								)
								self.assertListEqual(
								    list(reshaped_hidden_states.shape[-2:]	)							,    [num_patches, self.model_tester.embed_dim]							,    )

			def 				UpperCAmelCase__							(			self      : str	)    -> Union[str, Any]:
								__magic_name__				:						Optional[Any]     				=				self.model_tester.prepare_config_and_inputs_for_common()

								__magic_name__				:						List[Any]     				=				(
								    self.model_tester.image_size
								    if isinstance(self.model_tester.image_size							,    collections.abc.Iterable	)
								    else (self.model_tester.image_size, self.model_tester.image_size)
								)

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

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

													self.check_hidden_states_output(__lowerCamelCase							,    __lowerCamelCase							,    __lowerCamelCase							,    __lowerCamelCase	)

			def 				UpperCAmelCase__							(			self      : Dict	)    -> str:
								__magic_name__				:						List[Any]     				=				self.model_tester.prepare_config_and_inputs_for_common()
								__magic_name__				:						List[Any]     				=				3

								__magic_name__				:						str     				=				(
								    self.model_tester.image_size
								    if isinstance(self.model_tester.image_size							,    collections.abc.Iterable	)
								    else (self.model_tester.image_size, self.model_tester.image_size)
								)
								__magic_name__				:						List[str]     				=				(
								    config.patch_size
								    if isinstance(config.patch_size							,    collections.abc.Iterable	)
								    else (config.patch_size, config.patch_size)
								)

								__magic_name__				:						Union[str, Any]     				=				image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
								__magic_name__				:						Optional[int]     				=				image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

								for model_class in self.all_model_classes:
													__magic_name__				:						Union[str, Any]     				=				True
													self.check_hidden_states_output(__lowerCamelCase							,    __lowerCamelCase							,    __lowerCamelCase							,    (padded_height, padded_width)	)

													# check that output_hidden_states also work using config
													del inputs_dict["output_hidden_states"]
													__magic_name__				:						Any     				=				True
													self.check_hidden_states_output(__lowerCamelCase							,    __lowerCamelCase							,    __lowerCamelCase							,    (padded_height, padded_width)	)

			@slow
			def 				UpperCAmelCase__							(			self      : Dict	)    -> int:



								for model_name in DONUT_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													__magic_name__				:						Optional[Any]     				=				DonutSwinModel.from_pretrained(__lowerCamelCase	)
													self.assertIsNotNone(__lowerCamelCase	)


			def 				UpperCAmelCase__							(			self      : List[str]	)    -> int:
								__magic_name__				:						List[str]     				=				self.model_tester.prepare_config_and_inputs_for_common()

								__magic_name__				:						Dict     				=				_config_zero_init(__lowerCamelCase	)
								for model_class in self.all_model_classes:
													__magic_name__				:						Tuple     				=				model_class(config=__lowerCamelCase	)
													for name, param in model.named_parameters():
																		if "embeddings" not in name and param.requires_grad:
																							self.assertIn(
																							    ((param.data.mean() * 1E9).round() / 1E9).item()							,    [0.0, 1.0]							,    msg=F'''Parameter {name} of model {model_class} seems not properly initialized'''							,    )







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


       """simple docstring"""




       def __init__(  self				:     Union[str, Any]   , __lowerCamelCase				:     List[Any]   , __lowerCamelCase				:     int=2   , __lowerCamelCase				:     Any=True   , __lowerCamelCase				:     Union[str, Any]=True   , __lowerCamelCase				:     int=10   , __lowerCamelCase				:     str=10   , __lowerCamelCase				:     int=8   , __lowerCamelCase				:     Optional[int]=[2, 4, 4]   , __lowerCamelCase				:     List[Any]=3   , __lowerCamelCase				:     str=768   , __lowerCamelCase				:     int=5   , __lowerCamelCase				:     Any=4   , __lowerCamelCase				:     int=37   , __lowerCamelCase				:     int="gelu_fast"   , __lowerCamelCase				:     int=0.0   , __lowerCamelCase				:     Optional[int]=0.0   , __lowerCamelCase				:     Union[str, Any]=0.0_2   , __lowerCamelCase				:     Any=1E-0_6   , __lowerCamelCase				:     Optional[int]=True   , __lowerCamelCase				:     Tuple=None   , ):


             '''simple docstring'''



             lowerCamelCase__						:							Optional[Any]       =    parent
             lowerCamelCase__						:							int       =    batch_size
             lowerCamelCase__						:							List[Any]       =    is_training
             lowerCamelCase__						:							Optional[Any]       =    use_labels
             lowerCamelCase__						:							List[str]       =    num_labels
             lowerCamelCase__						:							Union[str, Any]       =    image_size
             lowerCamelCase__						:							Tuple       =    num_frames
             lowerCamelCase__						:							Union[str, Any]       =    tubelet_size
             lowerCamelCase__						:							Optional[int]       =    num_channels
             lowerCamelCase__						:							Any       =    hidden_size
             lowerCamelCase__						:							Optional[Any]       =    num_hidden_layers
             lowerCamelCase__						:							List[str]       =    num_attention_heads
             lowerCamelCase__						:							List[Any]       =    intermediate_size
             lowerCamelCase__						:							Union[str, Any]       =    hidden_act
             lowerCamelCase__						:							Any       =    hidden_dropout_prob
             lowerCamelCase__						:							Union[str, Any]       =    attention_probs_dropout_prob
             lowerCamelCase__						:							List[Any]       =    initializer_range
             lowerCamelCase__						:							Optional[int]       =    layer_norm_eps
             lowerCamelCase__						:							Tuple       =    qkv_bias
             lowerCamelCase__						:							List[Any]       =    scope

             lowerCamelCase__						:							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				:     List[Any]			):


             '''simple docstring'''



             lowerCamelCase__						:							int       =    floats_tensor(
                 [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]			)

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

             lowerCamelCase__						:							Any       =    self.get_config()

             return config, pixel_values, labels





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


             '''simple docstring'''



             lowerCamelCase__						:							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   , )
             lowerCamelCase__						:							Union[str, Any]       =    self.num_labels
             return config





       def 		lowerCAmelCase	(  self				:     Optional[int]   , __lowerCamelCase				:     List[str]   , __lowerCamelCase				:     Union[str, Any]   , __lowerCamelCase				:     Union[str, Any]			):


             '''simple docstring'''



             lowerCamelCase__						:							Tuple       =    VivitModel(config=__lowerCamelCase			)
             model.to(__lowerCamelCase			)
             model.eval()
             lowerCamelCase__						:							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				:     Union[str, Any]   , __lowerCamelCase				:     Optional[int]   , __lowerCamelCase				:     Optional[int]			):


             '''simple docstring'''



             lowerCamelCase__						:							Dict       =    VivitForVideoClassification(__lowerCamelCase			)
             model.to(__lowerCamelCase			)
             model.eval()

             lowerCamelCase__						:							List[str]       =    model(__lowerCamelCase			)

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





       def 		lowerCAmelCase	(  self				:     Optional[Any]			):


             '''simple docstring'''



             lowerCamelCase__						:							Optional[int]       =    self.prepare_config_and_inputs()
             lowerCamelCase__       ,  lowerCamelCase__       ,  lowerCamelCase__						:							Any       =    config_and_inputs
             lowerCamelCase__						:							int       =    {"pixel_values": pixel_values}
             return config, inputs_dict







@require_torch
class      _lowercase					(				lowercase__				,    unittest.TestCase):


       """simple docstring"""




       A__   		=     (VivitModel, VivitForVideoClassification) if is_torch_available() else ()

       A__   		=     False
       A__   		=     False
       A__   		=     False
       A__   		=     False





       def 		lowerCAmelCase	(  self				:     Optional[int]			):


             '''simple docstring'''



             lowerCamelCase__						:							Any       =    VivitModelTester(self			)
             lowerCamelCase__						:							Any       =    ConfigTester(self   , config_class=__lowerCamelCase   , has_text_modality=__lowerCamelCase   , hidden_size=37			)





       def 		lowerCAmelCase	(  self				:     int   , __lowerCamelCase				:     List[str]   , __lowerCamelCase				:     Dict   , __lowerCamelCase				:     Union[str, Any]=False			):


             '''simple docstring'''



             lowerCamelCase__						:							int       =    copy.deepcopy(__lowerCamelCase			)

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

             return inputs_dict





       def 		lowerCAmelCase	(  self				:     List[str]			):


             '''simple docstring'''



             self.config_tester.run_common_tests()





       @unittest.skip(reason="Vivit does not use inputs_embeds"			)
       def 		lowerCAmelCase	(  self				:     int			):


             '''simple docstring'''



             pass





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


             '''simple docstring'''



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

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





       def 		lowerCAmelCase	(  self				:     List[str]			):


             '''simple docstring'''



             lowerCamelCase__       ,  lowerCamelCase__						:							str       =    self.model_tester.prepare_config_and_inputs_for_common()

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

                   lowerCamelCase__						:							Any       =    ["pixel_values", "head_mask"]
                   self.assertListEqual(arg_names[:2]   , __lowerCamelCase			)





       def 		lowerCAmelCase	(  self				:     Dict			):


             '''simple docstring'''



             lowerCamelCase__						:							str       =    self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*__lowerCamelCase			)





       def 		lowerCAmelCase	(  self				:     Tuple			):


             '''simple docstring'''



             lowerCamelCase__						:							int       =    self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_video_classification(*__lowerCamelCase			)





       @slow
       def 		lowerCAmelCase	(  self				:     List[Any]			):


             '''simple docstring'''



             for model_name in VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                   lowerCamelCase__						:							str       =    VivitModel.from_pretrained(__lowerCamelCase			)
                   self.assertIsNotNone(__lowerCamelCase			)





       def 		lowerCAmelCase	(  self				:     Tuple			):


             '''simple docstring'''



             lowerCamelCase__       ,  lowerCamelCase__						:							Dict       =    self.model_tester.prepare_config_and_inputs_for_common()
             lowerCamelCase__						:							List[str]       =    True

             for model_class in self.all_model_classes:
                   lowerCamelCase__						:							Any       =    self.model_tester.seq_length

                   lowerCamelCase__						:							List[str]       =    True
                   lowerCamelCase__						:							List[str]       =    False
                   lowerCamelCase__						:							Tuple       =    True
                   lowerCamelCase__						:							Dict       =    model_class(__lowerCamelCase			)
                   model.to(__lowerCamelCase			)
                   model.eval()
                   with torch.no_grad():
                         lowerCamelCase__						:							str       =    model(**self._prepare_for_class(__lowerCamelCase   , __lowerCamelCase			)			)
                   lowerCamelCase__						:							Dict       =    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"]
                   lowerCamelCase__						:							List[str]       =    True
                   lowerCamelCase__						:							Union[str, Any]       =    model_class(__lowerCamelCase			)
                   model.to(__lowerCamelCase			)
                   model.eval()
                   with torch.no_grad():
                         lowerCamelCase__						:							List[str]       =    model(**self._prepare_for_class(__lowerCamelCase   , __lowerCamelCase			)			)
                   lowerCamelCase__						:							Optional[int]       =    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]   , )
                   lowerCamelCase__						:							int       =    len(__lowerCamelCase			)

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

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

                   lowerCamelCase__						:							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				:     List[str]			):


             '''simple docstring'''



             def check_hidden_states_output(__lowerCamelCase				:     str   , __lowerCamelCase				:     List[str]   , __lowerCamelCase				:     Tuple			):
                   lowerCamelCase__						:							Union[str, Any]       =    model_class(__lowerCamelCase			)
                   model.to(__lowerCamelCase			)
                   model.eval()

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

                   lowerCamelCase__						:							List[Any]       =    outputs.hidden_states
                   lowerCamelCase__						:							Optional[int]       =    self.model_tester.num_hidden_layers + 1
                   self.assertEqual(len(__lowerCamelCase			)   , __lowerCamelCase			)

                   lowerCamelCase__						:							Optional[int]       =    self.model_tester.seq_length

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

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

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

                   # check that output_hidden_states also work using config
                   del inputs_dict["output_hidden_states"]
                   lowerCamelCase__						:							int       =    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				:     Tuple			):


             '''simple docstring'''



             pass



def  lowercase_						(						):

      """simple docstring"""




      lowerCamelCase__						:							str       =    hf_hub_download(
          repo_id="hf-internal-testing/spaghetti-video"       ,   filename="eating_spaghetti_32_frames.npy"       ,   repo_type="dataset"		)
      lowerCamelCase__						:							Union[str, Any]       =    np.load(_A		)
      return list(_A		)







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


       """simple docstring"""




       @cached_property
       def 		lowerCAmelCase	(  self				:     str			):


             '''simple docstring'''



             return VivitImageProcessor() if is_vision_available() else None





       @slow
       def 		lowerCAmelCase	(  self				:     Tuple			):


             '''simple docstring'''



             lowerCamelCase__						:							Optional[int]       =    VivitForVideoClassification.from_pretrained("google/vivit-b-16x2-kinetics400"			).to(__lowerCamelCase			)

             lowerCamelCase__						:							Tuple       =    self.default_image_processor
             lowerCamelCase__						:							List[str]       =    prepare_video()
             lowerCamelCase__						:							Optional[int]       =    image_processor(__lowerCamelCase   , return_tensors="pt"			).to(__lowerCamelCase			)

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

             # verify the logits
             lowerCamelCase__						:							str       =    torch.Size((1, 400)			)
             self.assertEqual(outputs.logits.shape   , __lowerCamelCase			)

             # taken from original model
             lowerCamelCase__						:							str       =    torch.tensor([-1.0_5_4_3, 2.0_7_6_4, -0.2_1_0_4, 0.4_4_3_9, -0.9_6_5_8]			).to(__lowerCamelCase			)

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


