



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


						'''simple docstring'''





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






							'''simple docstring'''



							__lowercase		       =			parent
							__lowercase		       =			batch_size
							__lowercase		       =			image_size
							__lowercase		       =			num_channels
							__lowercase		       =			patch_size
							__lowercase		       =			num_frames
							__lowercase		       =			is_training
							__lowercase		       =			use_labels
							__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		       =			attention_type
							__lowercase		       =			initializer_range
							__lowercase		       =			scope
							__lowercase		       =			num_labels

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



						def     _snake_case	(	self	:							int	):






							'''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     _snake_case	(	self	:							Union[str, Any]	):






							'''simple docstring'''



							__lowercase		       =			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							,      )
							__lowercase		       =			self.num_labels
							return config



						def     _snake_case	(	self	:							Tuple							,      lowerCamelCase	:							List[Any]							,      lowerCamelCase	:							int							,      lowerCamelCase	:							Optional[int]	):






							'''simple docstring'''



							__lowercase		       =			TimesformerModel(config=_A	)
							model.to(_A	)
							model.eval()
							__lowercase		       =			model(_A	)
							self.parent.assertEqual(result.last_hidden_state.shape							,      (self.batch_size, self.seq_length, self.hidden_size)	)



						def     _snake_case	(	self	:							Tuple							,      lowerCamelCase	:							int							,      lowerCamelCase	:							List[Any]							,      lowerCamelCase	:							Union[str, Any]	):






							'''simple docstring'''



							__lowercase		       =			TimesformerForVideoClassification(_A	)
							model.to(_A	)
							model.eval()

							__lowercase		       =			model(_A	)

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



						def     _snake_case	(	self	:							Dict	):






							'''simple docstring'''



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

@require_torch
class   _A   ( a__  ,						a__  ,						unittest.TestCase	):


						'''simple docstring'''





						_snake_case			:     Union[str, Any]      							=      (TimesformerModel, TimesformerForVideoClassification) if is_torch_available() else ()
						_snake_case			:     Tuple      							=      (
						    {"""feature-extraction""": TimesformerModel, """video-classification""": TimesformerForVideoClassification}
						    if is_torch_available()
						    else {}
						)

						_snake_case			:     Optional[int]      							=      False
						_snake_case			:     Union[str, Any]      							=      False
						_snake_case			:     Union[str, Any]      							=      False
						_snake_case			:     str      							=      False



						def     _snake_case	(	self	:							List[Any]	):






							'''simple docstring'''



							__lowercase		       =			TimesformerModelTester(self	)
							__lowercase		       =			ConfigTester(
							    self							,      config_class=_A							,      has_text_modality=_A							,      hidden_size=37	)



						def     _snake_case	(	self	:							Dict							,      lowerCamelCase	:							Dict							,      lowerCamelCase	:							Optional[Any]							,      lowerCamelCase	:							List[Any]=False	):






							'''simple docstring'''



							__lowercase		       =			copy.deepcopy(_A	)

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

							return inputs_dict



						def     _snake_case	(	self	:							str	):






							'''simple docstring'''



							self.config_tester.run_common_tests()



						@unittest.skip(reason="TimeSformer does not use inputs_embeds"	)
						def     _snake_case	(	self	:							Union[str, Any]	):






							'''simple docstring'''



							pass



						def     _snake_case	(	self	:							int	):






							'''simple docstring'''



							__lowercase		       =			self.model_tester.prepare_config_and_inputs_for_common()

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



						def     _snake_case	(	self	:							int	):






							'''simple docstring'''



							__lowercase		       =			self.model_tester.prepare_config_and_inputs_for_common()

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

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



						def     _snake_case	(	self	:							List[Any]	):






							'''simple docstring'''



							__lowercase		       =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*_A	)



						def     _snake_case	(	self	:							Tuple	):






							'''simple docstring'''



							__lowercase		       =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_video_classification(*_A	)



						@slow
						def     _snake_case	(	self	:							Dict	):






							'''simple docstring'''



							for model_name in TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								__lowercase		       =			TimesformerModel.from_pretrained(_A	)
								self.assertIsNotNone(_A	)



						def     _snake_case	(	self	:							Optional[int]	):






							'''simple docstring'''



							if not self.has_attentions:
								pass

							else:
								__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		       =			self.model_tester.num_frames

									__lowercase		       =			True
									__lowercase		       =			False
									__lowercase		       =			True
									__lowercase		       =			model_class(_A	)
									model.to(_A	)
									model.eval()
									with torch.no_grad():
										__lowercase		       =			model(**self._prepare_for_class(_A							,      _A	)	)
									__lowercase		       =			outputs.attentions
									self.assertEqual(len(_A	)							,      self.model_tester.num_hidden_layers	)

									# check that output_attentions also work using config
									del inputs_dict["output_attentions"]
									__lowercase		       =			True
									__lowercase		       =			model_class(_A	)
									model.to(_A	)
									model.eval()
									with torch.no_grad():
										__lowercase		       =			model(**self._prepare_for_class(_A							,      _A	)	)
									__lowercase		       =			outputs.attentions
									self.assertEqual(len(_A	)							,      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]							,      )
									__lowercase		       =			len(_A	)

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

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

									__lowercase		       =			outputs.attentions

									self.assertEqual(len(_A	)							,      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     _snake_case	(	self	:							List[Any]	):






							'''simple docstring'''



							def check_hidden_states_output(lowerCamelCase	:							Optional[Any]							,      lowerCamelCase	:							List[str]							,      lowerCamelCase	:							Tuple	):
								__lowercase		       =			model_class(_A	)
								model.to(_A	)
								model.eval()

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

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

								__lowercase		       =			self.model_tester.seq_length

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

							__lowercase		       =			self.model_tester.prepare_config_and_inputs_for_common()

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

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

								check_hidden_states_output(_A							,      _A							,      _A	)



def       snake_case_				(    ):
	__lowercase		       =			hf_hub_download(
	    repo_id="hf-internal-testing/spaghetti-video"  ,	filename="eating_spaghetti.npy"  ,	repo_type="dataset"   )
	__lowercase		       =			np.load(_lowerCamelCase   )
	return list(_lowerCamelCase   )

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


						'''simple docstring'''





						@cached_property
						def     _snake_case	(	self	:							Tuple	):






							'''simple docstring'''



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



						@slow
						def     _snake_case	(	self	:							int	):






							'''simple docstring'''



							__lowercase		       =			TimesformerForVideoClassification.from_pretrained("facebook/timesformer-base-finetuned-k400"	).to(
							    _A	)

							__lowercase		       =			self.default_image_processor
							__lowercase		       =			prepare_video()
							__lowercase		       =			image_processor(video[:8]							,      return_tensors="pt"	).to(_A	)

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

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

							__lowercase		       =			torch.tensor([-0.3016, -0.7713, -0.4205]	).to(_A	)

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








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


snake_case__					:     List[Any]    		=   logging.get_logger(__name__)

snake_case__					:     List[str]    		=   {
    """hustvl/yolos-small""": """https://huggingface.co/hustvl/yolos-small/resolve/main/config.json""",
    # See all YOLOS models at https://huggingface.co/models?filter=yolos
}

class   _A   ( _lowercase	):


						'''simple docstring'''





						_snake_case			:     List[Any]      							=      """yolos"""



						def __init__(	self	:							Union[str, Any]							,      lowerCamelCase	:							Union[str, Any]=768							,      lowerCamelCase	:							int=12							,      lowerCamelCase	:							Union[str, Any]=12							,      lowerCamelCase	:							Optional[Any]=3_072							,      lowerCamelCase	:							Optional[int]="gelu"							,      lowerCamelCase	:							Dict=0.0							,      lowerCamelCase	:							Optional[Any]=0.0							,      lowerCamelCase	:							Any=0.02							,      lowerCamelCase	:							Optional[Any]=1e-12							,      lowerCamelCase	:							Optional[Any]=[512, 864]							,      lowerCamelCase	:							str=16							,      lowerCamelCase	:							Dict=3							,      lowerCamelCase	:							str=True							,      lowerCamelCase	:							List[Any]=100							,      lowerCamelCase	:							Dict=True							,      lowerCamelCase	:							Dict=False							,      lowerCamelCase	:							List[str]=1							,      lowerCamelCase	:							str=5							,      lowerCamelCase	:							Any=2							,      lowerCamelCase	:							str=5							,      lowerCamelCase	:							Optional[int]=2							,      lowerCamelCase	:							List[Any]=0.1							,      **lowerCamelCase	:							List[Any]							,      ):






							'''simple docstring'''



							super().__init__(**lowerCamelCase	)

							__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		       =			image_size
							__lowercase		       =			patch_size
							__lowercase		       =			num_channels
							__lowercase		       =			qkv_bias
							__lowercase		       =			num_detection_tokens
							__lowercase		       =			use_mid_position_embeddings
							__lowercase		       =			auxiliary_loss
							# Hungarian matcher
							__lowercase		       =			class_cost
							__lowercase		       =			bbox_cost
							__lowercase		       =			giou_cost
							# Loss coefficients
							__lowercase		       =			bbox_loss_coefficient
							__lowercase		       =			giou_loss_coefficient
							__lowercase		       =			eos_coefficient

class   _A   ( _lowercase	):


						'''simple docstring'''





						_snake_case			:     Dict      							=      version.parse("""1.11"""	)



						@property
						def     _snake_case	(	self	:							Union[str, Any]	):






							'''simple docstring'''



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



						@property
						def     _snake_case	(	self	:							str	):






							'''simple docstring'''



							return 1e-4



						@property
						def     _snake_case	(	self	:							Tuple	):






							'''simple docstring'''



							return 12



