


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



    """simple docstring"""






    def __init__(			self					,   A_					,   A_=13					,   A_=10					,   A_=3					,   A_=2					,   A_=2					,   A_=True					,   A_=True					,   A_=32					,   A_=5					,   A_=4					,   A_=37					,   A_="gelu"					,   A_=0.1					,   A_=0.1					,   A_=10					,   A_=0.02					,   A_="divided_space_time"					,   A_=None					,   )  ->       Optional[Any]:
         __UpperCamelCase    							=parent
         __UpperCamelCase    							=batch_size
         __UpperCamelCase    							=image_size
         __UpperCamelCase    							=num_channels
         __UpperCamelCase    							=patch_size
         __UpperCamelCase    							=num_frames
         __UpperCamelCase    							=is_training
         __UpperCamelCase    							=use_labels
         __UpperCamelCase    							=hidden_size
         __UpperCamelCase    							=num_hidden_layers
         __UpperCamelCase    							=num_attention_heads
         __UpperCamelCase    							=intermediate_size
         __UpperCamelCase    							=hidden_act
         __UpperCamelCase    							=hidden_dropout_prob
         __UpperCamelCase    							=attention_probs_dropout_prob
         __UpperCamelCase    							=attention_type
         __UpperCamelCase    							=initializer_range
         __UpperCamelCase    							=scope
         __UpperCamelCase    							=num_labels

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






    def _a					(			self    )  ->       List[Any]:
         __UpperCamelCase    							=floats_tensor(
             [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]    )

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

         __UpperCamelCase    							=self.get_config()

         return config, pixel_values, labels






    def _a					(			self    )  ->       Optional[Any]:
         __UpperCamelCase    							=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					,   )
         __UpperCamelCase    							=self.num_labels
         return config






    def _a					(			self					,   A_					,   A_					,   A_    )  ->       Union[str, Any]:
         __UpperCamelCase    							=TimesformerModel(config=A_    )
         model.to(A_    )
         model.eval()
         __UpperCamelCase    							=model(A_    )
         self.parent.assertEqual(result.last_hidden_state.shape					,   (self.batch_size, self.seq_length, self.hidden_size)    )






    def _a					(			self					,   A_					,   A_					,   A_    )  ->       int:
         __UpperCamelCase    							=TimesformerForVideoClassification(A_    )
         model.to(A_    )
         model.eval()

         __UpperCamelCase    							=model(A_    )

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






    def _a					(			self    )  ->       Union[str, Any]:
         __UpperCamelCase    							=self.prepare_config_and_inputs()
         __UpperCamelCase    							=config_and_inputs
         __UpperCamelCase    							={'pixel_values': pixel_values}
         return config, inputs_dict






@require_torch
class     UpperCAmelCase__       (					A_   ,  A_   ,  unittest.TestCase		):



    """simple docstring"""






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

    UpperCAmelCase__						:		Optional[int]      =  False
    UpperCAmelCase__						:		Any      =  False
    UpperCAmelCase__						:		int      =  False
    UpperCAmelCase__						:		Dict      =  False






    def _a					(			self    )  ->       int:
         __UpperCamelCase    							=TimesformerModelTester(self    )
         __UpperCamelCase    							=ConfigTester(
             self					,   config_class=A_					,   has_text_modality=A_					,   hidden_size=37    )






    def _a					(			self					,   A_					,   A_					,   A_=False    )  ->       Dict:
         __UpperCamelCase    							=copy.deepcopy(A_    )

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

         return inputs_dict






    def _a					(			self    )  ->       Optional[Any]:
         self.config_tester.run_common_tests()






    @unittest.skip(reason='TimeSformer does not use inputs_embeds'    )
    def _a					(			self    )  ->       Union[str, Any]:
         pass






    def _a					(			self    )  ->       Dict:
         __UpperCamelCase    							=self.model_tester.prepare_config_and_inputs_for_common()

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






    def _a					(			self    )  ->       Any:
         __UpperCamelCase    							=self.model_tester.prepare_config_and_inputs_for_common()

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

              __UpperCamelCase    							=['pixel_values']
              self.assertListEqual(arg_names[:1]					,   A_    )






    def _a					(			self    )  ->       int:
         __UpperCamelCase    							=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*A_    )






    def _a					(			self    )  ->       Dict:
         __UpperCamelCase    							=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_video_classification(*A_    )






    @slow
    def _a					(			self    )  ->       Any:
         for model_name in TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              __UpperCamelCase    							=TimesformerModel.from_pretrained(A_    )
              self.assertIsNotNone(A_    )






    def _a					(			self    )  ->       Dict:
         if not self.has_attentions:
              pass

         else:
              __UpperCamelCase    							=self.model_tester.prepare_config_and_inputs_for_common()
              __UpperCamelCase    							=True

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

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

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

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

                   __UpperCamelCase    							=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 _a					(			self    )  ->       Optional[Any]:
         def check_hidden_states_output(A_					,   A_					,   A_    ):
              __UpperCamelCase    							=model_class(A_    )
              model.to(A_    )
              model.eval()

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

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

              __UpperCamelCase    							=self.model_tester.seq_length

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

         __UpperCamelCase    							=self.model_tester.prepare_config_and_inputs_for_common()

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

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

              check_hidden_states_output(A_					,   A_					,   A_    )

def 						_UpperCAmelCase				(			):
     __UpperCamelCase    							=hf_hub_download(
         repo_id='hf-internal-testing/spaghetti-video'			,     filename='eating_spaghetti.npy'			,     repo_type='dataset'				)
     __UpperCamelCase    							=np.load(lowercase__				)
     return list(lowercase__				)






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



    """simple docstring"""






    @cached_property
    def _a					(			self    )  ->       Optional[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 _a					(			self    )  ->       Any:
         __UpperCamelCase    							=TimesformerForVideoClassification.from_pretrained('facebook/timesformer-base-finetuned-k400'    ).to(
             A_    )

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

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

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

         __UpperCamelCase    							=torch.tensor([-0.3016, -0.7713, -0.4205]    ).to(A_    )

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











"""simple docstring"""






lowercase__     						=						{
    """meter""": """m""",
    """kilometer""": """km""",
    """megametre""": """Mm""",
    """gigametre""": """Gm""",
    """terametre""": """Tm""",
    """petametre""": """Pm""",
    """exametre""": """Em""",
    """zettametre""": """Zm""",
    """yottametre""": """Ym""",
}
# Exponent of the factor(meter)
lowercase__     						=						{
    """m""": 0,
    """km""": 3,
    """Mm""": 6,
    """Gm""": 9,
    """Tm""": 12,
    """Pm""": 15,
    """Em""": 18,
    """Zm""": 21,
    """Ym""": 24,
}







def      _snake_case			(				lowercase__							,    lowercase__							,    lowercase__		):
				_lowerCamelCase     :			List[Any]    =    from_type.lower().strip('s'		)
				_lowerCamelCase     :			List[Any]    =    to_type.lower().strip('s'		)

				_lowerCamelCase     :			Optional[int]    =    UNIT_SYMBOL.get(lowercase__							,    lowercase__		)
				_lowerCamelCase     :			Any    =    UNIT_SYMBOL.get(lowercase__							,    lowercase__		)

				if from_sanitized not in METRIC_CONVERSION:
								_lowerCamelCase     :			Tuple    =    (
								    f'''Invalid \'from_type\' value: {from_type!r}.\n'''
								    f'''Conversion abbreviations are: {', '.join(lowercase__		)}'''
								)
								raise ValueError(lowercase__		)
				if to_sanitized not in METRIC_CONVERSION:
								_lowerCamelCase     :			Any    =    (
								    f'''Invalid \'to_type\' value: {to_type!r}.\n'''
								    f'''Conversion abbreviations are: {', '.join(lowercase__		)}'''
								)
								raise ValueError(lowercase__		)
				_lowerCamelCase     :			List[Any]    =    METRIC_CONVERSION[from_sanitized]
				_lowerCamelCase     :			int    =    METRIC_CONVERSION[to_sanitized]
				_lowerCamelCase     :			List[str]    =    1

				if from_exponent > to_exponent:
								_lowerCamelCase     :			List[str]    =    from_exponent - to_exponent
				else:
								_lowerCamelCase     :			List[Any]    =    -(to_exponent - from_exponent)

				return value * pow(10							,    lowercase__		)


if __name__ == "__main__":
			from doctest import testmod

			testmod()