

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




			"""simple docstring"""







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

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



			def        _SCREAMING_SNAKE_CASE			( self  :     Any):
									SCREAMING_SNAKE_CASE_: List[str]  		=       floats_tensor(
									    [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size])

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

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

									return config, pixel_values, labels



			def        _SCREAMING_SNAKE_CASE			( self  :     List[str]):
									SCREAMING_SNAKE_CASE_: List[Any]  		=       TimesformerConfig(
									    image_size=self.image_size   ,						patch_size=self.patch_size   ,						num_channels=self.num_channels   ,						num_frames=self.num_frames   ,						hidden_size=self.hidden_size   ,						num_hidden_layers=self.num_hidden_layers   ,						num_attention_heads=self.num_attention_heads   ,						intermediate_size=self.intermediate_size   ,						hidden_act=self.hidden_act   ,						hidden_dropout_prob=self.hidden_dropout_prob   ,						attention_probs_dropout_prob=self.attention_probs_dropout_prob   ,						initializer_range=self.initializer_range   ,						attention_type=self.attention_type   ,						)
									SCREAMING_SNAKE_CASE_: str  		=       self.num_labels
									return config



			def        _SCREAMING_SNAKE_CASE			( self  :     str   ,						lowerCAmelCase__  :     str   ,						lowerCAmelCase__  :     Union[str, Any]   ,						lowerCAmelCase__  :     Optional[int]):
									SCREAMING_SNAKE_CASE_: List[Any]  		=       TimesformerModel(config=lowerCAmelCase__)
									model.to(lowerCAmelCase__)
									model.eval()
									SCREAMING_SNAKE_CASE_: List[str]  		=       model(lowerCAmelCase__)
									self.parent.assertEqual(result.last_hidden_state.shape   ,						(self.batch_size, self.seq_length, self.hidden_size))



			def        _SCREAMING_SNAKE_CASE			( self  :     int   ,						lowerCAmelCase__  :     List[Any]   ,						lowerCAmelCase__  :     Optional[Any]   ,						lowerCAmelCase__  :     str):
									SCREAMING_SNAKE_CASE_: int  		=       TimesformerForVideoClassification(lowerCAmelCase__)
									model.to(lowerCAmelCase__)
									model.eval()

									SCREAMING_SNAKE_CASE_: str  		=       model(lowerCAmelCase__)

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




			def        _SCREAMING_SNAKE_CASE			( self  :     Tuple):
									SCREAMING_SNAKE_CASE_: Dict  		=       self.prepare_config_and_inputs()
									SCREAMING_SNAKE_CASE_		,    SCREAMING_SNAKE_CASE_		,    SCREAMING_SNAKE_CASE_: Tuple  		=       config_and_inputs
									SCREAMING_SNAKE_CASE_: Tuple  		=       {"pixel_values": pixel_values}
									return config, inputs_dict







@require_torch
class   __lowercase				( UpperCAmelCase_		,       UpperCAmelCase_		,       unittest.TestCase	):




			"""simple docstring"""







			_UpperCAmelCase   :							Optional[Any]										=     (TimesformerModel, TimesformerForVideoClassification) if is_torch_available() else ()
			_UpperCAmelCase   :							List[str]										=     (
			    {'''feature-extraction''': TimesformerModel, '''video-classification''': TimesformerForVideoClassification}
			    if is_torch_available()
			    else {}
			)

			_UpperCAmelCase   :							Dict										=     False
			_UpperCAmelCase   :							int										=     False
			_UpperCAmelCase   :							Dict										=     False
			_UpperCAmelCase   :							List[Any]										=     False



			def        _SCREAMING_SNAKE_CASE			( self  :     List[str]):
									SCREAMING_SNAKE_CASE_: Dict  		=       TimesformerModelTester(self)
									SCREAMING_SNAKE_CASE_: Dict  		=       ConfigTester(
									    self   ,						config_class=lowerCAmelCase__   ,						has_text_modality=lowerCAmelCase__   ,						hidden_size=37)



			def        _SCREAMING_SNAKE_CASE			( self  :     Union[str, Any]   ,						lowerCAmelCase__  :     Any   ,						lowerCAmelCase__  :     str   ,						lowerCAmelCase__  :     Any=False):
									SCREAMING_SNAKE_CASE_: str  		=       copy.deepcopy(lowerCAmelCase__)

									if return_labels:
															if model_class in get_values(lowerCAmelCase__):
																					SCREAMING_SNAKE_CASE_: Tuple  		=       torch.zeros(
																					    self.model_tester.batch_size   ,						dtype=torch.long   ,						device=lowerCAmelCase__)

									return inputs_dict



			def        _SCREAMING_SNAKE_CASE			( self  :     Union[str, Any]):
									self.config_tester.run_common_tests()



			@unittest.skip(reason="TimeSformer does not use inputs_embeds")
			def        _SCREAMING_SNAKE_CASE			( self  :     Any):
									pass



			def        _SCREAMING_SNAKE_CASE			( self  :     Optional[Any]):
									SCREAMING_SNAKE_CASE_		,    SCREAMING_SNAKE_CASE_: Optional[int]  		=       self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
															SCREAMING_SNAKE_CASE_: str  		=       model_class(lowerCAmelCase__)
															self.assertIsInstance(model.get_input_embeddings()   ,						(nn.Module))
															SCREAMING_SNAKE_CASE_: Union[str, Any]  		=       model.get_output_embeddings()
															self.assertTrue(x is None or isinstance(lowerCAmelCase__   ,						nn.Linear))



			def        _SCREAMING_SNAKE_CASE			( self  :     Dict):
									SCREAMING_SNAKE_CASE_		,    SCREAMING_SNAKE_CASE_: Union[str, Any]  		=       self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
															SCREAMING_SNAKE_CASE_: Dict  		=       model_class(lowerCAmelCase__)
															SCREAMING_SNAKE_CASE_: Tuple  		=       inspect.signature(model.forward)
															# signature.parameters is an OrderedDict => so arg_names order is deterministic
															SCREAMING_SNAKE_CASE_: Optional[Any]  		=       [*signature.parameters.keys()]

															SCREAMING_SNAKE_CASE_: str  		=       ["pixel_values"]
															self.assertListEqual(arg_names[:1]   ,						lowerCAmelCase__)



			def        _SCREAMING_SNAKE_CASE			( self  :     Optional[int]):
									SCREAMING_SNAKE_CASE_: List[str]  		=       self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*lowerCAmelCase__)



			def        _SCREAMING_SNAKE_CASE			( self  :     List[Any]):
									SCREAMING_SNAKE_CASE_: int  		=       self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_video_classification(*lowerCAmelCase__)



			@slow
			def        _SCREAMING_SNAKE_CASE			( self  :     Any):
									for model_name in TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															SCREAMING_SNAKE_CASE_: int  		=       TimesformerModel.from_pretrained(lowerCAmelCase__)
															self.assertIsNotNone(lowerCAmelCase__)



			def        _SCREAMING_SNAKE_CASE			( self  :     Optional[int]):
									if not self.has_attentions:
															pass

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

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

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

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

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

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

																					SCREAMING_SNAKE_CASE_: Any  		=       outputs.attentions

																					self.assertEqual(len(lowerCAmelCase__)   ,						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        _SCREAMING_SNAKE_CASE			( self  :     List[str]):
									def check_hidden_states_output(lowerCAmelCase__  :     Any   ,						lowerCAmelCase__  :     Dict   ,						lowerCAmelCase__  :     Any):
															SCREAMING_SNAKE_CASE_: List[Any]  		=       model_class(lowerCAmelCase__)
															model.to(lowerCAmelCase__)
															model.eval()

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

															SCREAMING_SNAKE_CASE_: List[Any]  		=       outputs.hidden_states
															SCREAMING_SNAKE_CASE_: Union[str, Any]  		=       self.model_tester.num_hidden_layers + 1
															self.assertEqual(len(lowerCAmelCase__)   ,						lowerCAmelCase__)

															SCREAMING_SNAKE_CASE_: Any  		=       self.model_tester.seq_length

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

									SCREAMING_SNAKE_CASE_		,    SCREAMING_SNAKE_CASE_: List[str]  		=       self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
															SCREAMING_SNAKE_CASE_: Optional[int]  		=       True
															check_hidden_states_output(lowerCAmelCase__   ,						lowerCAmelCase__   ,						lowerCAmelCase__)

															# check that output_hidden_states also work using config
															del inputs_dict["output_hidden_states"]
															SCREAMING_SNAKE_CASE_: Optional[Any]  		=       True

															check_hidden_states_output(lowerCAmelCase__   ,						lowerCAmelCase__   ,						lowerCAmelCase__)







def 			A_      (    ):
						SCREAMING_SNAKE_CASE_: Any  		=       hf_hub_download(
						    repo_id="hf-internal-testing/spaghetti-video"				,	filename="eating_spaghetti.npy"				,	repo_type="dataset"    )
						SCREAMING_SNAKE_CASE_: str  		=       np.load(_UpperCAmelCase    )
						return list(_UpperCAmelCase    )



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




			"""simple docstring"""







			@cached_property
			def        _SCREAMING_SNAKE_CASE			( self  :     List[Any]):
									# logits were tested with a different mean and std, so we use the same here
									return (
									    VideoMAEImageProcessor(image_mean=[0.5, 0.5, 0.5]   ,						image_std=[0.5, 0.5, 0.5])
									    if is_vision_available()
									    else None
									)




			@slow
			def        _SCREAMING_SNAKE_CASE			( self  :     str):
									SCREAMING_SNAKE_CASE_: Dict  		=       TimesformerForVideoClassification.from_pretrained("facebook/timesformer-base-finetuned-k400").to(
									    lowerCAmelCase__)

									SCREAMING_SNAKE_CASE_: Union[str, Any]  		=       self.default_image_processor
									SCREAMING_SNAKE_CASE_: Tuple  		=       prepare_video()
									SCREAMING_SNAKE_CASE_: int  		=       image_processor(video[:8]   ,						return_tensors="pt").to(lowerCAmelCase__)

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

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

									SCREAMING_SNAKE_CASE_: int  		=       torch.tensor([-0.3016, -0.7713, -0.4205]).to(lowerCAmelCase__)

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













def 			A__							(    SCREAMING_SNAKE_CASE__ = 200)   ->    int:
  __snake_case:						Optional[int]   =						[1, 2, 5, 10, 20, 50, 100, 200]
  __snake_case:						List[Any]   =						[0] * (pence + 1)
  __snake_case:						int   =						1  # base case: 1 way to make 0 pence

  for coin in coins:
    for i in range(SCREAMING_SNAKE_CASE__					,					pence + 1					,					1):
      number_of_ways[i] += number_of_ways[i - coin]
  return number_of_ways[pence]


if __name__ == "__main__":
      assert solution(200) == 73_682






