



from abc import ABC, abstractmethod
from typing import List, Optional

class 				_UpperCamelCase(_UpperCamelCase      ):

					"""simple docstring"""






					def __init__(							self :     List[str]					):
									# test for the above condition
									self.test()







					def 			snake_case					(							self :     Union[str, Any]					):
									lowercase__					:   List[Any]													=					0
									lowercase__					:   str													=					False
									while not completed:
													if counter == 1:
																	self.reset()
													lowercase__					:   List[Any]													=					self.advance()
													if not self.does_advance(SCREAMING_SNAKE_CASE					):
																	raise Exception(
																	    "Custom Constraint is not defined correctly. self.does_advance(self.advance()) must be true."					)

													lowercase__					:   str													=					self.update(SCREAMING_SNAKE_CASE					)
													counter += 1

													if counter > 10_000:
																	raise Exception("update() does not fulfill the constraint."					)

									if self.remaining() != 0:
													raise Exception("Custom Constraint is not defined correctly."					)







					@abstractmethod
					def 			snake_case					(							self :     Any					):
									raise NotImplementedError(
									    f"""{self.__class__} is an abstract class. Only classes inheriting this class can be called."""					)







					@abstractmethod
					def 			snake_case					(							self :     Optional[Any]				,       SCREAMING_SNAKE_CASE :     int					):
									raise NotImplementedError(
									    f"""{self.__class__} is an abstract class. Only classes inheriting this class can be called."""					)







					@abstractmethod
					def 			snake_case					(							self :     Tuple				,       SCREAMING_SNAKE_CASE :     int					):
									raise NotImplementedError(
									    f"""{self.__class__} is an abstract class. Only classes inheriting this class can be called."""					)







					@abstractmethod
					def 			snake_case					(							self :     Dict					):
									raise NotImplementedError(
									    f"""{self.__class__} is an abstract class. Only classes inheriting this class can be called."""					)







					@abstractmethod
					def 			snake_case					(							self :     List[Any]					):
									raise NotImplementedError(
									    f"""{self.__class__} is an abstract class. Only classes inheriting this class can be called."""					)







					@abstractmethod
					def 			snake_case					(							self :     List[str]				,       SCREAMING_SNAKE_CASE :     Tuple=False					):
									raise NotImplementedError(
									    f"""{self.__class__} is an abstract class. Only classes inheriting this class can be called."""					)






class 				_UpperCamelCase(_UpperCamelCase      ):

					"""simple docstring"""






					def __init__(							self :     Optional[Any]				,       SCREAMING_SNAKE_CASE :     List[int]					):
									super(SCREAMING_SNAKE_CASE				,       self					).__init__()

									if not isinstance(SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE					) or len(SCREAMING_SNAKE_CASE					) == 0:
													raise ValueError(f"""`token_ids` has to be a non-empty list, but is {token_ids}."""					)
									if any((not isinstance(SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE					) or token_id < 0) for token_id in token_ids					):
													raise ValueError(f"""Each list in `token_ids` has to be a list of positive integers, but is {token_ids}."""					)

									lowercase__					:   Optional[Any]													=					token_ids

									lowercase__					:   Tuple													=					len(self.token_ids					)
									lowercase__					:   Dict													=					-1  # the index of the currently fulfilled step
									lowercase__					:   List[str]													=					False







					def 			snake_case					(							self :     int					):
									if self.completed:
													return None
									return self.token_ids[self.fulfilled_idx + 1]







					def 			snake_case					(							self :     Dict				,       SCREAMING_SNAKE_CASE :     int					):
									if not isinstance(SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE					):
													raise ValueError(f"""`token_id` has to be an `int`, but is {token_id} of type {type(SCREAMING_SNAKE_CASE					)}"""					)

									if self.completed:
													return False

									return token_id == self.token_ids[self.fulfilled_idx + 1]







					def 			snake_case					(							self :     Union[str, Any]				,       SCREAMING_SNAKE_CASE :     int					):
									if not isinstance(SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE					):
													raise ValueError(f"""`token_id` has to be an `int`, but is {token_id} of type {type(SCREAMING_SNAKE_CASE					)}"""					)

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

									if self.does_advance(SCREAMING_SNAKE_CASE					):
													self.fulfilled_idx += 1
													lowercase__					:   Tuple													=					True
													if self.fulfilled_idx == (self.seqlen - 1):
																	lowercase__					:   Union[str, Any]													=					True
													lowercase__					:   Optional[Any]													=					completed
									else:
													# failed to make progress.
													lowercase__					:   List[Any]													=					True
													self.reset()
									return stepped, completed, reset







					def 			snake_case					(							self :     Optional[int]					):
									lowercase__					:   Any													=					False
									lowercase__					:   List[str]													=					0







					def 			snake_case					(							self :     int					):
									return self.seqlen - (self.fulfilled_idx + 1)







					def 			snake_case					(							self :     str				,       SCREAMING_SNAKE_CASE :     Any=False					):
									lowercase__					:   List[str]													=					PhrasalConstraint(self.token_ids					)

									if stateful:
													lowercase__					:   List[Any]													=					self.seqlen
													lowercase__					:   Dict													=					self.fulfilled_idx
													lowercase__					:   str													=					self.completed

									return new_constraint






class 				_UpperCamelCase:

					"""simple docstring"""






					def __init__(							self :     Tuple				,       SCREAMING_SNAKE_CASE :     List[List[int]]				,       SCREAMING_SNAKE_CASE :     Optional[Any]=True					):
									lowercase__					:   Dict													=					max([len(SCREAMING_SNAKE_CASE					) for one in nested_token_ids]					)

									lowercase__					:   str													=					{}
									for token_ids in nested_token_ids:
													lowercase__					:   Union[str, Any]													=					root
													for tidx, token_id in enumerate(SCREAMING_SNAKE_CASE					):
																	if token_id not in level:
																					lowercase__					:   str													=					{}

																	lowercase__					:   Optional[Any]													=					level[token_id]

									if no_subsets and self.has_subsets(SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE					):
													raise ValueError(
													    "Each list in `nested_token_ids` can't be a complete subset of another list, but is"
													    f""" {nested_token_ids}."""					)

									lowercase__					:   Any													=					root







					def 			snake_case					(							self :     Any				,       SCREAMING_SNAKE_CASE :     int					):
									lowercase__					:   str													=					self.trie

									for current_token in current_seq:
													lowercase__					:   Optional[Any]													=					start[current_token]

									lowercase__					:   Optional[Any]													=					list(start.keys()					)

									return next_tokens







					def 			snake_case					(							self :     Tuple				,       SCREAMING_SNAKE_CASE :     Any					):
									lowercase__					:   Optional[Any]													=					self.next_tokens(SCREAMING_SNAKE_CASE					)

									return len(SCREAMING_SNAKE_CASE					) == 0







					def 			snake_case					(							self :     Tuple				,       SCREAMING_SNAKE_CASE :     List[str]					):
									lowercase__					:   Optional[Any]													=					list(root.values()					)
									if len(SCREAMING_SNAKE_CASE					) == 0:
													return 1
									else:
													return sum([self.count_leaves(SCREAMING_SNAKE_CASE					) for nn in next_nodes]					)







					def 			snake_case					(							self :     Any				,       SCREAMING_SNAKE_CASE :     int				,       SCREAMING_SNAKE_CASE :     int					):
									lowercase__					:   List[str]													=					self.count_leaves(SCREAMING_SNAKE_CASE					)
									return len(SCREAMING_SNAKE_CASE					) != leaf_count






class 				_UpperCamelCase(_UpperCamelCase      ):

					"""simple docstring"""






					def __init__(							self :     Tuple				,       SCREAMING_SNAKE_CASE :     List[List[int]]					):
									super(SCREAMING_SNAKE_CASE				,       self					).__init__()

									if not isinstance(SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE					) or len(SCREAMING_SNAKE_CASE					) == 0:
													raise ValueError(f"""`nested_token_ids` has to be a non-empty list, but is {nested_token_ids}."""					)
									if any(not isinstance(SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE					) for token_ids in nested_token_ids					):
													raise ValueError(f"""`nested_token_ids` has to be a list of lists, but is {nested_token_ids}."""					)
									if any(
									    any((not isinstance(SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE					) or token_id < 0) for token_id in token_ids					)
									    for token_ids in nested_token_ids					):
													raise ValueError(
													    f"""Each list in `nested_token_ids` has to be a list of positive integers, but is {nested_token_ids}."""					)

									lowercase__					:   int													=					DisjunctiveTrie(SCREAMING_SNAKE_CASE					)
									lowercase__					:   Optional[Any]													=					nested_token_ids

									lowercase__					:   Tuple													=					self.trie.max_height
									lowercase__					:   Tuple													=					[]
									lowercase__					:   List[str]													=					False







					def 			snake_case					(							self :     Tuple					):
									lowercase__					:   int													=					self.trie.next_tokens(self.current_seq					)

									if len(SCREAMING_SNAKE_CASE					) == 0:
													return None
									else:
													return token_list







					def 			snake_case					(							self :     Tuple				,       SCREAMING_SNAKE_CASE :     int					):
									if not isinstance(SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE					):
													raise ValueError(f"""`token_id` is supposed to be type `int`, but is {token_id} of type {type(SCREAMING_SNAKE_CASE					)}"""					)

									lowercase__					:   Optional[int]													=					self.trie.next_tokens(self.current_seq					)

									return token_id in next_tokens







					def 			snake_case					(							self :     Optional[Any]				,       SCREAMING_SNAKE_CASE :     int					):
									if not isinstance(SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE					):
													raise ValueError(f"""`token_id` is supposed to be type `int`, but is {token_id} of type {type(SCREAMING_SNAKE_CASE					)}"""					)

									lowercase__					:   List[Any]													=					False
									lowercase__					:   Any													=					False
									lowercase__					:   Dict													=					False

									if self.does_advance(SCREAMING_SNAKE_CASE					):
													self.current_seq.append(SCREAMING_SNAKE_CASE					)
													lowercase__					:   str													=					True
									else:
													lowercase__					:   str													=					True
													self.reset()

									lowercase__					:   Optional[int]													=					self.trie.reached_leaf(self.current_seq					)
									lowercase__					:   Dict													=					completed

									return stepped, completed, reset







					def 			snake_case					(							self :     Dict					):
									lowercase__					:   List[str]													=					False
									lowercase__					:   Tuple													=					[]







					def 			snake_case					(							self :     Dict					):
									if self.completed:
													# since this can be completed without reaching max height
													return 0
									else:
													return self.seqlen - len(self.current_seq					)







					def 			snake_case					(							self :     Tuple				,       SCREAMING_SNAKE_CASE :     List[Any]=False					):
									lowercase__					:   Dict													=					DisjunctiveConstraint(self.token_ids					)

									if stateful:
													lowercase__					:   Any													=					self.seqlen
													lowercase__					:   Union[str, Any]													=					self.current_seq
													lowercase__					:   Dict													=					self.completed

									return new_constraint






class 				_UpperCamelCase:

					"""simple docstring"""






					def __init__(							self :     int				,       SCREAMING_SNAKE_CASE :     List[Constraint]					):
									lowercase__					:   Any													=					constraints

									# max # of steps required to fulfill a given constraint
									lowercase__					:   List[str]													=					max([c.seqlen for c in constraints]					)
									lowercase__					:   int													=					len(SCREAMING_SNAKE_CASE					)
									lowercase__					:   Optional[Any]													=					False

									self.init_state()







					def 			snake_case					(							self :     Optional[Any]					):
									lowercase__					:   Optional[int]													=					[]
									lowercase__					:   Tuple													=					None
									lowercase__					:   Optional[int]													=					[constraint.copy(stateful=SCREAMING_SNAKE_CASE					) for constraint in self.constraints]







					def 			snake_case					(							self :     Optional[Any]					):
									lowercase__					:   List[Any]													=					0
									if self.inprogress_constraint:
													# extra points for having a constraint mid-fulfilled
													add += self.max_seqlen - self.inprogress_constraint.remaining()

									return (len(self.complete_constraints					) * self.max_seqlen) + add







					def 			snake_case					(							self :     str					):
									lowercase__					:   Any													=					[]
									if self.inprogress_constraint is None:
													for constraint in self.pending_constraints:  # "pending" == "unfulfilled yet"
																	lowercase__					:   Optional[int]													=					constraint.advance()
																	if isinstance(SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE					):
																					token_list.append(SCREAMING_SNAKE_CASE					)
																	elif isinstance(SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE					):
																					token_list.extend(SCREAMING_SNAKE_CASE					)
									else:
													lowercase__					:   Tuple													=					self.inprogress_constraint.advance()
													if isinstance(SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE					):
																	token_list.append(SCREAMING_SNAKE_CASE					)
													elif isinstance(SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE					):
																	token_list.extend(SCREAMING_SNAKE_CASE					)

									if len(SCREAMING_SNAKE_CASE					) == 0:
													return None
									else:
													return token_list







					def 			snake_case					(							self :     Dict				,       SCREAMING_SNAKE_CASE :     Optional[List[int]]					):
									self.init_state()

									if token_ids is not None:
													for token in token_ids:
																	# completes or steps **one** constraint
																	lowercase__					:   Optional[int]													=					self.add(SCREAMING_SNAKE_CASE					)

																	# the entire list of constraints are fulfilled
																	if self.completed:
																					break







					def 			snake_case					(							self :     Optional[int]				,       SCREAMING_SNAKE_CASE :     int					):
									if not isinstance(SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE					):
													raise ValueError(f"""`token_id` should be an `int`, but is `{token_id}`."""					)

									lowercase__					:   int													=					False, False

									if self.completed:
													lowercase__					:   Optional[int]													=					True
													lowercase__					:   List[Any]													=					False
													return complete, stepped

									if self.inprogress_constraint is not None:
													# In the middle of fulfilling a constraint. If the `token_id` *does* makes an incremental progress to current
													# job, simply update the state

													lowercase__					:   Dict													=					self.inprogress_constraint.update(SCREAMING_SNAKE_CASE					)
													if reset:
																	# 1. If the next token breaks the progress, then we must restart.
																	#     e.g. constraint = "I love pies" and sequence so far is "I love" but `token_id` == "books".

																	#     But that doesn't mean we self.init_state(), since we only reset the state for this particular
																	#     constraint, not the full list of constraints.

																	self.pending_constraints.append(self.inprogress_constraint.copy(stateful=SCREAMING_SNAKE_CASE					)					)
																	lowercase__					:   Tuple													=					None

													if complete:
																	# 2. If the next token completes the constraint, move it to completed list, set
																	#     inprogress to None. If there are no pending constraints either, then this full list of constraints
																	#     is complete.

																	self.complete_constraints.append(self.inprogress_constraint					)
																	lowercase__					:   Optional[int]													=					None

																	if len(self.pending_constraints					) == 0:
																					# we're done!
																					lowercase__					:   Union[str, Any]													=					True

									else:
													# Not in the middle of fulfilling a constraint. So does this `token_id` helps us step towards any of our list
													# of constraints?

													for cidx, pending_constraint in enumerate(self.pending_constraints					):
																	if pending_constraint.does_advance(SCREAMING_SNAKE_CASE					):
																					lowercase__					:   Any													=					pending_constraint.update(SCREAMING_SNAKE_CASE					)

																					if not stepped:
																									raise Exception(
																									    "`constraint.update(token_id)` is not yielding incremental progress, "
																									    "even though `constraint.does_advance(token_id)` is true."					)

																					if complete:
																									self.complete_constraints.append(SCREAMING_SNAKE_CASE					)
																									lowercase__					:   int													=					None

																					if not complete and stepped:
																									lowercase__					:   Dict													=					pending_constraint

																					if complete or stepped:
																									# If we made any progress at all, then it's at least not a "pending constraint".

																									lowercase__					:   str													=					(
																									    self.pending_constraints[:cidx] + self.pending_constraints[cidx + 1 :]
																									)

																									if len(self.pending_constraints					) == 0 and self.inprogress_constraint is None:
																													# If there's no longer any pending after this and no inprogress either, then we must be
																													# complete.

																													lowercase__					:   List[str]													=					True

																									break  # prevent accidentally stepping through multiple constraints with just one token.

									return complete, stepped







					def 			snake_case					(							self :     str				,       SCREAMING_SNAKE_CASE :     Optional[Any]=True					):
									lowercase__					:   Dict													=					ConstraintListState(self.constraints					)  # we actually never though self.constraints objects
									# throughout this process. So it's at initialization state.

									if stateful:
													lowercase__					:   Optional[int]													=					[
													    constraint.copy(stateful=SCREAMING_SNAKE_CASE					) for constraint in self.complete_constraints
													]
													if self.inprogress_constraint is not None:
																	lowercase__					:   Dict													=					self.inprogress_constraint.copy(stateful=SCREAMING_SNAKE_CASE					)
													lowercase__					:   Optional[int]													=					[constraint.copy() for constraint in self.pending_constraints]

									return new_state









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

					"""simple docstring"""






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

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







					def 			snake_case					(							self :     Optional[int]					):
									lowercase__					:   Any													=					floats_tensor(
									    [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]					)

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

									lowercase__					:   List[Any]													=					self.get_config()

									return config, pixel_values, labels







					def 			snake_case					(							self :     Any					):
									lowercase__					:   Optional[int]													=					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__					:   List[Any]													=					self.num_labels
									return config







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







					def 			snake_case					(							self :     Dict				,       SCREAMING_SNAKE_CASE :     Optional[Any]				,       SCREAMING_SNAKE_CASE :     Optional[int]				,       SCREAMING_SNAKE_CASE :     Any					):
									lowercase__					:   List[Any]													=					TimesformerForVideoClassification(SCREAMING_SNAKE_CASE					)
									model.to(SCREAMING_SNAKE_CASE					)
									model.eval()

									lowercase__					:   List[str]													=					model(SCREAMING_SNAKE_CASE					)

									# verify the logits shape
									lowercase__					:   List[str]													=					torch.Size((self.batch_size, self.num_labels)					)
									self.parent.assertEqual(result.logits.shape				,       SCREAMING_SNAKE_CASE					)







					def 			snake_case					(							self :     Optional[Any]					):
									lowercase__					:   List[str]													=					self.prepare_config_and_inputs()
									lowercase__							,							lowercase__							,							lowercase__					:   int													=					config_and_inputs
									lowercase__					:   Optional[int]													=					{"pixel_values": pixel_values}
									return config, inputs_dict






@require_torch
class 				snake_case__(_UpperCamelCase    ,      _UpperCamelCase    ,      unittest.TestCase      ):

					"""simple docstring"""






					lowercase_								=		(TimesformerModel, TimesformerForVideoClassification) if is_torch_available() else ()
					lowercase_								=		(
					    {"""feature-extraction""": TimesformerModel, """video-classification""": TimesformerForVideoClassification}
					    if is_torch_available()
					    else {}
					)

					lowercase_								=		False
					lowercase_								=		False
					lowercase_								=		False
					lowercase_								=		False







					def 			snake_case					(							self :     Dict					):
									lowercase__					:   Tuple													=					TimesformerModelTester(self					)
									lowercase__					:   Any													=					ConfigTester(
									    self				,       config_class=SCREAMING_SNAKE_CASE				,       has_text_modality=SCREAMING_SNAKE_CASE				,       hidden_size=37					)







					def 			snake_case					(							self :     Dict				,       SCREAMING_SNAKE_CASE :     List[str]				,       SCREAMING_SNAKE_CASE :     Any				,       SCREAMING_SNAKE_CASE :     Tuple=False					):
									lowercase__					:   Union[str, Any]													=					copy.deepcopy(SCREAMING_SNAKE_CASE					)

									if return_labels:
													if model_class in get_values(SCREAMING_SNAKE_CASE					):
																	lowercase__					:   int													=					torch.zeros(
																	    self.model_tester.batch_size				,       dtype=torch.long				,       device=SCREAMING_SNAKE_CASE					)

									return inputs_dict







					def 			snake_case					(							self :     Optional[Any]					):
									self.config_tester.run_common_tests()







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







					def 			snake_case					(							self :     Tuple					):
									lowercase__							,							lowercase__					:   str													=					self.model_tester.prepare_config_and_inputs_for_common()

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







					def 			snake_case					(							self :     Union[str, Any]					):
									lowercase__							,							lowercase__					:   Optional[Any]													=					self.model_tester.prepare_config_and_inputs_for_common()

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

													lowercase__					:   Dict													=					["pixel_values"]
													self.assertListEqual(arg_names[:1]				,       SCREAMING_SNAKE_CASE					)







					def 			snake_case					(							self :     Tuple					):
									lowercase__					:   Union[str, Any]													=					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE					)







					def 			snake_case					(							self :     str					):
									lowercase__					:   List[str]													=					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_video_classification(*SCREAMING_SNAKE_CASE					)







					@slow
					def 			snake_case					(							self :     Optional[int]					):
									for model_name in TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													lowercase__					:   Optional[int]													=					TimesformerModel.from_pretrained(SCREAMING_SNAKE_CASE					)
													self.assertIsNotNone(SCREAMING_SNAKE_CASE					)







					def 			snake_case					(							self :     Any					):
									if not self.has_attentions:
													pass

									else:
													lowercase__							,							lowercase__					:   Any													=					self.model_tester.prepare_config_and_inputs_for_common()
													lowercase__					:   Union[str, Any]													=					True

													for model_class in self.all_model_classes:
																	lowercase__					:   List[str]													=					self.model_tester.seq_length
																	lowercase__					:   Any													=					self.model_tester.num_frames

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

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

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

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

																	lowercase__					:   Optional[Any]													=					outputs.attentions

																	self.assertEqual(len(SCREAMING_SNAKE_CASE					)				,       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]					):
									def check_hidden_states_output(SCREAMING_SNAKE_CASE :     str				,       SCREAMING_SNAKE_CASE :     int				,       SCREAMING_SNAKE_CASE :     Union[str, Any]					):
													lowercase__					:   Optional[int]													=					model_class(SCREAMING_SNAKE_CASE					)
													model.to(SCREAMING_SNAKE_CASE					)
													model.eval()

													with torch.no_grad():
																	lowercase__					:   Tuple													=					model(**self._prepare_for_class(SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE					)					)

													lowercase__					:   Optional[Any]													=					outputs.hidden_states
													lowercase__					:   List[str]													=					self.model_tester.num_hidden_layers + 1
													self.assertEqual(len(SCREAMING_SNAKE_CASE					)				,       SCREAMING_SNAKE_CASE					)

													lowercase__					:   int													=					self.model_tester.seq_length

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

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

									for model_class in self.all_model_classes:
													lowercase__					:   Dict													=					True
													check_hidden_states_output(SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE					)

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

													check_hidden_states_output(SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE					)






def  __lowerCamelCase (       ):

				"""simple docstring"""


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

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

					"""simple docstring"""






					@cached_property
					def 			snake_case					(							self :     Dict					):
									# 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 			snake_case					(							self :     List[Any]					):
									lowercase__					:   List[Any]													=					TimesformerForVideoClassification.from_pretrained("facebook/timesformer-base-finetuned-k400"					).to(
									    SCREAMING_SNAKE_CASE					)

									lowercase__					:   int													=					self.default_image_processor
									lowercase__					:   List[str]													=					prepare_video()
									lowercase__					:   str													=					image_processor(video[:8]				,       return_tensors="pt"					).to(SCREAMING_SNAKE_CASE					)

									# forward pass
									with torch.no_grad():
													lowercase__					:   str													=					model(**SCREAMING_SNAKE_CASE					)

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

									lowercase__					:   Optional[Any]													=					torch.tensor([-0.3_016, -0.7_713, -0.4_205]					).to(SCREAMING_SNAKE_CASE					)

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




