
"""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 				__a    :



							'''simple docstring'''







							def __init__(    self     ,						_lowerCamelCase     ,						_lowerCamelCase=2     ,						_lowerCamelCase=True     ,						_lowerCamelCase=True     ,						_lowerCamelCase=10     ,						_lowerCamelCase=10     ,						_lowerCamelCase=8     ,						_lowerCamelCase=[2, 4, 4]     ,						_lowerCamelCase=3     ,						_lowerCamelCase=768     ,						_lowerCamelCase=5     ,						_lowerCamelCase=4     ,						_lowerCamelCase=37     ,						_lowerCamelCase="gelu_fast"     ,						_lowerCamelCase=0.0     ,						_lowerCamelCase=0.0     ,						_lowerCamelCase=0.02     ,						_lowerCamelCase=1e-06     ,						_lowerCamelCase=True     ,						_lowerCamelCase=None     ,						) ->       str:




										'''simple docstring'''
										__lowercase						      =					parent
										__lowercase						      =					batch_size
										__lowercase						      =					is_training
										__lowercase						      =					use_labels
										__lowercase						      =					num_labels
										__lowercase						      =					image_size
										__lowercase						      =					num_frames
										__lowercase						      =					tubelet_size
										__lowercase						      =					num_channels
										__lowercase						      =					hidden_size
										__lowercase						      =					num_hidden_layers
										__lowercase						      =					num_attention_heads
										__lowercase						      =					intermediate_size
										__lowercase						      =					hidden_act
										__lowercase						      =					hidden_dropout_prob
										__lowercase						      =					attention_probs_dropout_prob
										__lowercase						      =					initializer_range
										__lowercase						      =					layer_norm_eps
										__lowercase						      =					qkv_bias
										__lowercase						      =					scope

										__lowercase						      =					(
										    (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      SCREAMING_SNAKE_CASE	(    self					) ->       List[Any]:




										'''simple docstring'''
										__lowercase						      =					floats_tensor(
										    [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]					)

										__lowercase						      =					None
										if self.use_labels:
													__lowercase						      =					ids_tensor([self.batch_size]     ,						self.num_labels					)

										__lowercase						      =					self.get_config()

										return config, pixel_values, labels
							def      SCREAMING_SNAKE_CASE	(    self					) ->       Tuple:




										'''simple docstring'''
										__lowercase						      =					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     ,						)
										__lowercase						      =					self.num_labels
										return config
							def      SCREAMING_SNAKE_CASE	(    self     ,						_lowerCamelCase     ,						_lowerCamelCase     ,						_lowerCamelCase					) ->       Optional[Any]:




										'''simple docstring'''
										__lowercase						      =					VivitModel(config=_lowerCamelCase					)
										model.to(_lowerCamelCase					)
										model.eval()
										__lowercase						      =					model(_lowerCamelCase					)
										self.parent.assertEqual(result.last_hidden_state.shape     ,						(self.batch_size, self.seq_length, self.hidden_size)					)
							def      SCREAMING_SNAKE_CASE	(    self     ,						_lowerCamelCase     ,						_lowerCamelCase     ,						_lowerCamelCase					) ->       List[Any]:




										'''simple docstring'''
										__lowercase						      =					VivitForVideoClassification(_lowerCamelCase					)
										model.to(_lowerCamelCase					)
										model.eval()

										__lowercase						      =					model(_lowerCamelCase					)

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





							def      SCREAMING_SNAKE_CASE	(    self					) ->       Tuple:




										'''simple docstring'''
										__lowercase						      =					self.prepare_config_and_inputs()
										__lowercase      ,				__lowercase      ,				__lowercase						      =					config_and_inputs
										__lowercase						      =					{"pixel_values": pixel_values}
										return config, inputs_dict





@require_torch
class 				__a    (   __a				,      unittest.TestCase					):



							'''simple docstring'''







							_lowerCamelCase				: int					      =      (VivitModel, VivitForVideoClassification) if is_torch_available() else ()

							_lowerCamelCase				: Any					      =      False
							_lowerCamelCase				: Dict					      =      False
							_lowerCamelCase				: Dict					      =      False
							_lowerCamelCase				: str					      =      False
							def      SCREAMING_SNAKE_CASE	(    self					) ->       int:




										'''simple docstring'''
										__lowercase						      =					VivitModelTester(self					)
										__lowercase						      =					ConfigTester(self     ,						config_class=_lowerCamelCase     ,						has_text_modality=_lowerCamelCase     ,						hidden_size=37					)
							def      SCREAMING_SNAKE_CASE	(    self     ,						_lowerCamelCase     ,						_lowerCamelCase     ,						_lowerCamelCase=False					) ->       str:




										'''simple docstring'''
										__lowercase						      =					copy.deepcopy(_lowerCamelCase					)

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

										return inputs_dict
							def      SCREAMING_SNAKE_CASE	(    self					) ->       Any:




										'''simple docstring'''
										self.config_tester.run_common_tests()
							@unittest.skip(reason="Vivit does not use inputs_embeds"					)
							def      SCREAMING_SNAKE_CASE	(    self					) ->       List[str]:




										'''simple docstring'''
										pass
							def      SCREAMING_SNAKE_CASE	(    self					) ->       List[Any]:




										'''simple docstring'''
										__lowercase      ,				__lowercase						      =					self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
													__lowercase						      =					model_class(_lowerCamelCase					)
													self.assertIsInstance(model.get_input_embeddings()     ,						(nn.Module)					)
													__lowercase						      =					model.get_output_embeddings()
													self.assertTrue(x is None or isinstance(_lowerCamelCase     ,						nn.Linear					)					)
							def      SCREAMING_SNAKE_CASE	(    self					) ->       Dict:




										'''simple docstring'''
										__lowercase      ,				__lowercase						      =					self.model_tester.prepare_config_and_inputs_for_common()

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

													__lowercase						      =					["pixel_values", "head_mask"]
													self.assertListEqual(arg_names[:2]     ,						_lowerCamelCase					)
							def      SCREAMING_SNAKE_CASE	(    self					) ->       Optional[Any]:




										'''simple docstring'''
										__lowercase						      =					self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*_lowerCamelCase					)
							def      SCREAMING_SNAKE_CASE	(    self					) ->       Tuple:




										'''simple docstring'''
										__lowercase						      =					self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_video_classification(*_lowerCamelCase					)
							@slow
							def      SCREAMING_SNAKE_CASE	(    self					) ->       Optional[Any]:




										'''simple docstring'''
										for model_name in VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													__lowercase						      =					VivitModel.from_pretrained(_lowerCamelCase					)
													self.assertIsNotNone(_lowerCamelCase					)
							def      SCREAMING_SNAKE_CASE	(    self					) ->       List[str]:




										'''simple docstring'''
										__lowercase      ,				__lowercase						      =					self.model_tester.prepare_config_and_inputs_for_common()
										__lowercase						      =					True

										for model_class in self.all_model_classes:
													__lowercase						      =					self.model_tester.seq_length

													__lowercase						      =					True
													__lowercase						      =					False
													__lowercase						      =					True
													__lowercase						      =					model_class(_lowerCamelCase					)
													model.to(_lowerCamelCase					)
													model.eval()
													with torch.no_grad():
																__lowercase						      =					model(**self._prepare_for_class(_lowerCamelCase     ,						_lowerCamelCase					)					)
													__lowercase						      =					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"]
													__lowercase						      =					True
													__lowercase						      =					model_class(_lowerCamelCase					)
													model.to(_lowerCamelCase					)
													model.eval()
													with torch.no_grad():
																__lowercase						      =					model(**self._prepare_for_class(_lowerCamelCase     ,						_lowerCamelCase					)					)
													__lowercase						      =					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]     ,						)
													__lowercase						      =					len(_lowerCamelCase					)

													# Check attention is always last and order is fine
													__lowercase						      =					True
													__lowercase						      =					True
													__lowercase						      =					model_class(_lowerCamelCase					)
													model.to(_lowerCamelCase					)
													model.eval()
													with torch.no_grad():
																__lowercase						      =					model(**self._prepare_for_class(_lowerCamelCase     ,						_lowerCamelCase					)					)

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

													__lowercase						      =					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      SCREAMING_SNAKE_CASE	(    self					) ->       Dict:




										'''simple docstring'''
										def check_hidden_states_output(_lowerCamelCase     ,						_lowerCamelCase     ,						_lowerCamelCase					):
													__lowercase						      =					model_class(_lowerCamelCase					)
													model.to(_lowerCamelCase					)
													model.eval()

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

													__lowercase						      =					outputs.hidden_states
													__lowercase						      =					self.model_tester.num_hidden_layers + 1
													self.assertEqual(len(_lowerCamelCase					)     ,						_lowerCamelCase					)

													__lowercase						      =					self.model_tester.seq_length

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

										__lowercase      ,				__lowercase						      =					self.model_tester.prepare_config_and_inputs_for_common()

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

													# check that output_hidden_states also work using config
													del inputs_dict["output_hidden_states"]
													__lowercase						      =					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      SCREAMING_SNAKE_CASE	(    self					) ->       Optional[Any]:




										'''simple docstring'''
										pass





def     lowerCAmelCase__ (		)	->str:
			__lowercase						      =					hf_hub_download(
			    repo_id="hf-internal-testing/spaghetti-video" ,      filename="eating_spaghetti_32_frames.npy" ,      repo_type="dataset"			)
			__lowercase						      =					np.load(__magic_name__			)
			return list(__magic_name__			)







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



							'''simple docstring'''







							@cached_property
							def      SCREAMING_SNAKE_CASE	(    self					) ->       List[Any]:




										'''simple docstring'''
										return VivitImageProcessor() if is_vision_available() else None





							@slow
							def      SCREAMING_SNAKE_CASE	(    self					) ->       int:




										'''simple docstring'''
										__lowercase						      =					VivitForVideoClassification.from_pretrained("google/vivit-b-16x2-kinetics400"					).to(_lowerCamelCase					)

										__lowercase						      =					self.default_image_processor
										__lowercase						      =					prepare_video()
										__lowercase						      =					image_processor(_lowerCamelCase     ,						return_tensors="pt"					).to(_lowerCamelCase					)

										# forward pass
										with torch.no_grad():
													__lowercase						      =					model(**_lowerCamelCase					)

										# verify the logits
										__lowercase						      =					torch.Size((1, 400)					)
										self.assertEqual(outputs.logits.shape     ,						_lowerCamelCase					)

										# taken from original model
										__lowercase						      =					torch.tensor([-1.0543, 2.0764, -0.2104, 0.4439, -0.9658]					).to(_lowerCamelCase					)

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







"""simple docstring"""






from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


_lowercase							 =							logging.get_logger(__name__)

_lowercase							 =							{
    '''facebook/convnext-tiny-224''': '''https://huggingface.co/facebook/convnext-tiny-224/resolve/main/config.json''',
    # See all ConvNeXT models at https://huggingface.co/models?filter=convnext
}

class 				__a    (   __a				,      __a					):



							'''simple docstring'''







							_lowerCamelCase				: List[Any]					      =      """convnext"""
							def __init__(    self     ,						_lowerCamelCase=3     ,						_lowerCamelCase=4     ,						_lowerCamelCase=4     ,						_lowerCamelCase=None     ,						_lowerCamelCase=None     ,						_lowerCamelCase="gelu"     ,						_lowerCamelCase=0.02     ,						_lowerCamelCase=1e-12     ,						_lowerCamelCase=1e-6     ,						_lowerCamelCase=0.0     ,						_lowerCamelCase=224     ,						_lowerCamelCase=None     ,						_lowerCamelCase=None     ,						**_lowerCamelCase     ,						) ->       List[str]:




										'''simple docstring'''
										super().__init__(**_lowerCamelCase					)

										__lowercase						      =					num_channels
										__lowercase						      =					patch_size
										__lowercase						      =					num_stages
										__lowercase						      =					[96, 192, 384, 768] if hidden_sizes is None else hidden_sizes
										__lowercase						      =					[3, 3, 9, 3] if depths is None else depths
										__lowercase						      =					hidden_act
										__lowercase						      =					initializer_range
										__lowercase						      =					layer_norm_eps
										__lowercase						      =					layer_scale_init_value
										__lowercase						      =					drop_path_rate
										__lowercase						      =					image_size
										__lowercase						      =					["stem"] + [f'''stage{idx}''' for idx in range(1     ,						len(self.depths					) + 1					)]
										__lowercase      ,				__lowercase						      =					get_aligned_output_features_output_indices(
										    out_features=_lowerCamelCase     ,						out_indices=_lowerCamelCase     ,						stage_names=self.stage_names					)





class 				__a    (   __a					):



							'''simple docstring'''







							_lowerCamelCase				: str					      =      version.parse("""1.11"""					)
							@property
							def      SCREAMING_SNAKE_CASE	(    self					) ->       Mapping[str, Mapping[int, str]]:




										'''simple docstring'''
										return OrderedDict(
										    [
										        ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}),
										    ]					)





							@property
							def      SCREAMING_SNAKE_CASE	(    self					) ->       float:




										'''simple docstring'''
										return 1e-5





