




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






						def __init__(     self	,    _A	,    _A=1_3	,    _A=1_0	,    _A=3	,    _A=2	,    _A=2	,    _A=True	,    _A=True	,    _A=3_2	,    _A=5	,    _A=4	,    _A=3_7	,    _A="gelu"	,    _A=0.1	,    _A=0.1	,    _A=1_0	,    _A=0.02	,    _A="divided_space_time"	,    _A=None	,    ):




											"""simple docstring"""



											__lowerCAmelCase            = parent
											__lowerCAmelCase            = batch_size
											__lowerCAmelCase            = image_size
											__lowerCAmelCase            = num_channels
											__lowerCAmelCase            = patch_size
											__lowerCAmelCase            = num_frames
											__lowerCAmelCase            = is_training
											__lowerCAmelCase            = use_labels
											__lowerCAmelCase            = hidden_size
											__lowerCAmelCase            = num_hidden_layers
											__lowerCAmelCase            = num_attention_heads
											__lowerCAmelCase            = intermediate_size
											__lowerCAmelCase            = hidden_act
											__lowerCAmelCase            = hidden_dropout_prob
											__lowerCAmelCase            = attention_probs_dropout_prob
											__lowerCAmelCase            = attention_type
											__lowerCAmelCase            = initializer_range
											__lowerCAmelCase            = scope
											__lowerCAmelCase            = num_labels

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






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											__lowerCAmelCase            = floats_tensor(
											    [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]    )

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

											__lowerCAmelCase            = self.get_config()

											return config, pixel_values, labels






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



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






						def        __SCREAMING_SNAKE_CASE(     self	,    _A	,    _A	,    _A    ):




											"""simple docstring"""



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






						def        __SCREAMING_SNAKE_CASE(     self	,    _A	,    _A	,    _A    ):




											"""simple docstring"""



											__lowerCAmelCase            = TimesformerForVideoClassification(__UpperCamelCase    )
											model.to(__UpperCamelCase    )
											model.eval()

											__lowerCAmelCase            = model(__UpperCamelCase    )

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






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											__lowerCAmelCase            = self.prepare_config_and_inputs()
											__lowerCAmelCase     ,							__lowerCAmelCase     ,							__lowerCAmelCase            = config_and_inputs
											__lowerCAmelCase            = {"pixel_values": pixel_values}
											return config, inputs_dict






@require_torch
class     a__					(      _UpperCAmelCase				,       _UpperCAmelCase				,       unittest.TestCase   ):
						_a							:					Union[str, Any]      =					(TimesformerModel, TimesformerForVideoClassification) if is_torch_available() else ()
						_a							:					Tuple      =					(
						    {'feature-extraction': TimesformerModel, 'video-classification': TimesformerForVideoClassification}
						    if is_torch_available()
						    else {}
						)

						_a							:					Optional[Any]      =					False
						_a							:					int      =					False
						_a							:					List[Any]      =					False
						_a							:					str      =					False






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											__lowerCAmelCase            = TimesformerModelTester(self    )
											__lowerCAmelCase            = ConfigTester(
											    self	,    config_class=__UpperCamelCase	,    has_text_modality=__UpperCamelCase	,    hidden_size=3_7    )






						def        __SCREAMING_SNAKE_CASE(     self	,    _A	,    _A	,    _A=False    ):




											"""simple docstring"""



											__lowerCAmelCase            = copy.deepcopy(__UpperCamelCase    )

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

											return inputs_dict






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											self.config_tester.run_common_tests()






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




											"""simple docstring"""



											pass






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											__lowerCAmelCase     ,							__lowerCAmelCase            = self.model_tester.prepare_config_and_inputs_for_common()

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






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											__lowerCAmelCase     ,							__lowerCAmelCase            = self.model_tester.prepare_config_and_inputs_for_common()

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

																__lowerCAmelCase            = ["pixel_values"]
																self.assertListEqual(arg_names[:1]	,    __UpperCamelCase    )






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											__lowerCAmelCase            = self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*__UpperCamelCase    )






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											__lowerCAmelCase            = self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_video_classification(*__UpperCamelCase    )






						@slow
						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



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






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											if not self.has_attentions:
																pass

											else:
																__lowerCAmelCase     ,							__lowerCAmelCase            = self.model_tester.prepare_config_and_inputs_for_common()
																__lowerCAmelCase            = True

																for model_class in self.all_model_classes:
																					__lowerCAmelCase            = self.model_tester.seq_length
																					__lowerCAmelCase            = self.model_tester.num_frames

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

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

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

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

																					__lowerCAmelCase            = outputs.attentions

																					self.assertEqual(len(__UpperCamelCase    )	,    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    ):




											"""simple docstring"""



											def check_hidden_states_output(_A	,    _A	,    _A    ):
																__lowerCAmelCase            = model_class(__UpperCamelCase    )
																model.to(__UpperCamelCase    )
																model.eval()

																with torch.no_grad():
																					__lowerCAmelCase            = model(**self._prepare_for_class(__UpperCamelCase	,    __UpperCamelCase    )    )

																__lowerCAmelCase            = outputs.hidden_states
																__lowerCAmelCase            = self.model_tester.num_hidden_layers + 1
																self.assertEqual(len(__UpperCamelCase    )	,    __UpperCamelCase    )

																__lowerCAmelCase            = self.model_tester.seq_length

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

											__lowerCAmelCase     ,							__lowerCAmelCase            = self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																__lowerCAmelCase            = True
																check_hidden_states_output(__UpperCamelCase	,    __UpperCamelCase	,    __UpperCamelCase    )

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

																check_hidden_states_output(__UpperCamelCase	,    __UpperCamelCase	,    __UpperCamelCase    )







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


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






						@cached_property
						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""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        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											__lowerCAmelCase            = TimesformerForVideoClassification.from_pretrained("facebook/timesformer-base-finetuned-k400"    ).to(
											    __UpperCamelCase    )

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

											# forward pass
											with torch.no_grad():
																__lowerCAmelCase            = model(**__UpperCamelCase    )

											# verify the logits
											__lowerCAmelCase            = torch.Size((1, 4_0_0)    )
											self.assertEqual(outputs.logits.shape	,    __UpperCamelCase    )

											__lowerCAmelCase            = torch.tensor([-0.30_16, -0.77_13, -0.42_05]    ).to(__UpperCamelCase    )

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









def 			_a   (		SCREAMING_SNAKE_CASE_							:       int   ,   SCREAMING_SNAKE_CASE_							:       int  ):
					if b == 0:
										return 1
					if (b % 2) == 0:
										return actual_power(SCREAMING_SNAKE_CASE_   ,   int(b / 2  )  ) * actual_power(SCREAMING_SNAKE_CASE_   ,   int(b / 2  )  )
					else:
										return a * actual_power(SCREAMING_SNAKE_CASE_   ,   int(b / 2  )  ) * actual_power(SCREAMING_SNAKE_CASE_   ,   int(b / 2  )  )







def 			_a   (		SCREAMING_SNAKE_CASE_							:       int   ,   SCREAMING_SNAKE_CASE_							:       int  ):
					if b < 0:
										return 1 / actual_power(SCREAMING_SNAKE_CASE_   ,   SCREAMING_SNAKE_CASE_  )
					return actual_power(SCREAMING_SNAKE_CASE_   ,   SCREAMING_SNAKE_CASE_  )


if __name__ == "__main__":
					print(power(-2, -3))




