



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							:  Union[str, Any]  ,	__SCREAMING_SNAKE_CASE							:  Optional[int]  ,	__SCREAMING_SNAKE_CASE							:  Any=13  ,	__SCREAMING_SNAKE_CASE							:  Dict=10  ,	__SCREAMING_SNAKE_CASE							:  int=3  ,	__SCREAMING_SNAKE_CASE							:  List[Any]=2  ,	__SCREAMING_SNAKE_CASE							:  Any=2  ,	__SCREAMING_SNAKE_CASE							:  Optional[Any]=True  ,	__SCREAMING_SNAKE_CASE							:  List[Any]=True  ,	__SCREAMING_SNAKE_CASE							:  Union[str, Any]=32  ,	__SCREAMING_SNAKE_CASE							:  Optional[Any]=5  ,	__SCREAMING_SNAKE_CASE							:  Optional[int]=4  ,	__SCREAMING_SNAKE_CASE							:  Union[str, Any]=37  ,	__SCREAMING_SNAKE_CASE							:  Any="gelu"  ,	__SCREAMING_SNAKE_CASE							:  Tuple=0.1  ,	__SCREAMING_SNAKE_CASE							:  Optional[Any]=0.1  ,	__SCREAMING_SNAKE_CASE							:  Union[str, Any]=10  ,	__SCREAMING_SNAKE_CASE							:  Tuple=0.02  ,	__SCREAMING_SNAKE_CASE							:  Optional[Any]="divided_space_time"  ,	__SCREAMING_SNAKE_CASE							:  Optional[Any]=None  ,	):


           '''simple docstring'''

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

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



       def 	_lowerCamelCase    (	self							:  List[str]):


           '''simple docstring'''

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

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

           __a		 =       self.get_config()

           return config, pixel_values, labels



       def 	_lowerCamelCase    (	self							:  Optional[int]):


           '''simple docstring'''

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



       def 	_lowerCamelCase    (	self							:  Optional[int]  ,	__SCREAMING_SNAKE_CASE							:  Union[str, Any]  ,	__SCREAMING_SNAKE_CASE							:  Any  ,	__SCREAMING_SNAKE_CASE							:  List[Any]):


           '''simple docstring'''

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



       def 	_lowerCamelCase    (	self							:  Any  ,	__SCREAMING_SNAKE_CASE							:  int  ,	__SCREAMING_SNAKE_CASE							:  Optional[Any]  ,	__SCREAMING_SNAKE_CASE							:  Optional[Any]):


           '''simple docstring'''

           __a		 =       TimesformerForVideoClassification(__SCREAMING_SNAKE_CASE)
           model.to(__SCREAMING_SNAKE_CASE)
           model.eval()

           __a		 =       model(__SCREAMING_SNAKE_CASE)

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





       def 	_lowerCamelCase    (	self							:  int):


           '''simple docstring'''

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


@require_torch
class 					_A (			__UpperCAmelCase   ,__UpperCAmelCase   ,unittest.TestCase					):
       UpperCamelCase__						:					Optional[Any]		      =		(TimesformerModel, TimesformerForVideoClassification) if is_torch_available() else ()
       UpperCamelCase__						:					Tuple		      =		(
           {'''feature-extraction''': TimesformerModel, '''video-classification''': TimesformerForVideoClassification}
           if is_torch_available()
           else {}
       )

       UpperCamelCase__						:					Tuple		      =		False
       UpperCamelCase__						:					Tuple		      =		False
       UpperCamelCase__						:					Union[str, Any]		      =		False
       UpperCamelCase__						:					List[Any]		      =		False



       def 	_lowerCamelCase    (	self							:  List[Any]):


           '''simple docstring'''

           __a		 =       TimesformerModelTester(self)
           __a		 =       ConfigTester(
               self  ,	config_class=__SCREAMING_SNAKE_CASE  ,	has_text_modality=__SCREAMING_SNAKE_CASE  ,	hidden_size=37)



       def 	_lowerCamelCase    (	self							:  Any  ,	__SCREAMING_SNAKE_CASE							:  Tuple  ,	__SCREAMING_SNAKE_CASE							:  Dict  ,	__SCREAMING_SNAKE_CASE							:  Union[str, Any]=False):


           '''simple docstring'''

           __a		 =       copy.deepcopy(__SCREAMING_SNAKE_CASE)

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

           return inputs_dict



       def 	_lowerCamelCase    (	self							:  Tuple):


           '''simple docstring'''

           self.config_tester.run_common_tests()



       @unittest.skip(reason='''TimeSformer does not use inputs_embeds''')
       def 	_lowerCamelCase    (	self							:  int):


           '''simple docstring'''

           pass



       def 	_lowerCamelCase    (	self							:  Dict):


           '''simple docstring'''

           __a ,     __a		 =       self.model_tester.prepare_config_and_inputs_for_common()

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



       def 	_lowerCamelCase    (	self							:  List[Any]):


           '''simple docstring'''

           __a ,     __a		 =       self.model_tester.prepare_config_and_inputs_for_common()

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

               __a		 =       ['''pixel_values''']
               self.assertListEqual(arg_names[:1]  ,	__SCREAMING_SNAKE_CASE)



       def 	_lowerCamelCase    (	self							:  List[str]):


           '''simple docstring'''

           __a		 =       self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE)



       def 	_lowerCamelCase    (	self							:  str):


           '''simple docstring'''

           __a		 =       self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_video_classification(*__SCREAMING_SNAKE_CASE)



       @slow
       def 	_lowerCamelCase    (	self							:  Any):


           '''simple docstring'''

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



       def 	_lowerCamelCase    (	self							:  Optional[Any]):


           '''simple docstring'''

           if not self.has_attentions:
               pass

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

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

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

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

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

                   __a		 =       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 	_lowerCamelCase    (	self							:  Dict):


           '''simple docstring'''

           def check_hidden_states_output(__SCREAMING_SNAKE_CASE							:  Any  ,	__SCREAMING_SNAKE_CASE							:  Dict  ,	__SCREAMING_SNAKE_CASE							:  int):
               __a		 =       model_class(__SCREAMING_SNAKE_CASE)
               model.to(__SCREAMING_SNAKE_CASE)
               model.eval()

               with torch.no_grad():
                   __a		 =       model(**self._prepare_for_class(__SCREAMING_SNAKE_CASE  ,	__SCREAMING_SNAKE_CASE))

               __a		 =       outputs.hidden_states
               __a		 =       self.model_tester.num_hidden_layers + 1
               self.assertEqual(len(__SCREAMING_SNAKE_CASE)  ,	__SCREAMING_SNAKE_CASE)

               __a		 =       self.model_tester.seq_length

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

           __a ,     __a		 =       self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
               __a		 =       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"]
               __a		 =       True

               check_hidden_states_output(__SCREAMING_SNAKE_CASE  ,	__SCREAMING_SNAKE_CASE  ,	__SCREAMING_SNAKE_CASE)





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

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



       @cached_property
       def 	_lowerCamelCase    (	self							:  Optional[int]):


           '''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 	_lowerCamelCase    (	self							:  Optional[int]):


           '''simple docstring'''

           __a		 =       TimesformerForVideoClassification.from_pretrained('''facebook/timesformer-base-finetuned-k400''').to(
               __SCREAMING_SNAKE_CASE)

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

           # forward pass
           with torch.no_grad():
               __a		 =       model(**__SCREAMING_SNAKE_CASE)

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

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

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










from collections.abc import Generator
from math import sin





def   __snake_case			(    _UpperCAmelCase       ):
    if len(_UpperCAmelCase       ) != 32:
        raise ValueError('''Input must be of length 32'''       )

    __a		 =       b''''''
    for i in [3, 2, 1, 0]:
        little_endian += string_aa[8 * i : 8 * i + 8]
    return little_endian





def   __snake_case			(    _UpperCAmelCase       ):
    if i < 0:
        raise ValueError('''Input must be non-negative'''       )

    __a		 =       format(_UpperCAmelCase     ,				'''08x'''       )[-8:]
    __a		 =       b''''''
    for i in [3, 2, 1, 0]:
        little_endian_hex += hex_rep[2 * i : 2 * i + 2].encode('''utf-8'''       )
    return little_endian_hex





def   __snake_case			(    _UpperCAmelCase       ):
    __a		 =       b''''''
    for char in message:
        bit_string += format(_UpperCAmelCase     ,				'''08b'''       ).encode('''utf-8'''       )
    __a		 =       format(len(_UpperCAmelCase       )     ,				'''064b'''       ).encode('''utf-8'''       )

    # Pad bit_string to a multiple of 512 chars
    bit_string += b"1"
    while len(_UpperCAmelCase       ) % 512 != 448:
        bit_string += b"0"
    bit_string += to_little_endian(start_len[32:]       ) + to_little_endian(start_len[:32]       )

    return bit_string





def   __snake_case			(    _UpperCAmelCase       ):
    if len(_UpperCAmelCase       ) % 512 != 0:
        raise ValueError('''Input must have length that\'s a multiple of 512'''       )

    for pos in range(0     ,				len(_UpperCAmelCase       )     ,				512       ):
        __a		 =       bit_string[pos : pos + 512]
        __a		 =       []
        for i in range(0     ,				512     ,				32       ):
            block_words.append(int(to_little_endian(block[i : i + 32]       )     ,				2       )       )
        yield block_words





def   __snake_case			(    _UpperCAmelCase       ):
    if i < 0:
        raise ValueError('''Input must be non-negative'''       )

    __a		 =       format(_UpperCAmelCase     ,				'''032b'''       )
    __a		 =       ''''''
    for c in i_str:
        new_str += "1" if c == "0" else "0"
    return int(_UpperCAmelCase     ,				2       )





def   __snake_case			(    _UpperCAmelCase     ,				_UpperCAmelCase       ):
    return (a + b) % 2**32





def   __snake_case			(    _UpperCAmelCase     ,				_UpperCAmelCase       ):
    if i < 0:
        raise ValueError('''Input must be non-negative'''       )
    if shift < 0:
        raise ValueError('''Shift must be non-negative'''       )
    return ((i << shift) ^ (i >> (32 - shift))) % 2**32





def   __snake_case			(    _UpperCAmelCase       ):
    __a		 =       preprocess(_UpperCAmelCase       )

    __a		 =       [int(2**32 * abs(sin(i + 1       )       )       ) for i in range(64       )]

    # Starting states
    __a		 =       0X67_452_301
    __a		 =       0Xef_cda_b89
    __a		 =       0X98_bad_cfe
    __a		 =       0X10_325_476

    __a		 =       [
        7,
        12,
        17,
        22,
        7,
        12,
        17,
        22,
        7,
        12,
        17,
        22,
        7,
        12,
        17,
        22,
        5,
        9,
        14,
        20,
        5,
        9,
        14,
        20,
        5,
        9,
        14,
        20,
        5,
        9,
        14,
        20,
        4,
        11,
        16,
        23,
        4,
        11,
        16,
        23,
        4,
        11,
        16,
        23,
        4,
        11,
        16,
        23,
        6,
        10,
        15,
        21,
        6,
        10,
        15,
        21,
        6,
        10,
        15,
        21,
        6,
        10,
        15,
        21,
    ]

    # Process bit string in chunks, each with 16 32-char words
    for block_words in get_block_words(_UpperCAmelCase       ):
        __a		 =       aa
        __a		 =       ba
        __a		 =       ca
        __a		 =       da

        # Hash current chunk
        for i in range(64       ):
            if i <= 15:
                # f = (b & c) | (not_32(b) & d)     # Alternate definition for f
                __a		 =       d ^ (b & (c ^ d))
                __a		 =       i
            elif i <= 31:
                # f = (d & b) | (not_32(d) & c)     # Alternate definition for f
                __a		 =       c ^ (d & (b ^ c))
                __a		 =       (5 * i + 1) % 16
            elif i <= 47:
                __a		 =       b ^ c ^ d
                __a		 =       (3 * i + 5) % 16
            else:
                __a		 =       c ^ (b | not_aa(_UpperCAmelCase       ))
                __a		 =       (7 * i) % 16
            __a		 =       (f + a + added_consts[i] + block_words[g]) % 2**32
            __a		 =       d
            __a		 =       c
            __a		 =       b
            __a		 =       sum_aa(_UpperCAmelCase     ,				left_rotate_aa(_UpperCAmelCase     ,				shift_amounts[i]       )       )

        # Add hashed chunk to running total
        __a		 =       sum_aa(_UpperCAmelCase     ,				_UpperCAmelCase       )
        __a		 =       sum_aa(_UpperCAmelCase     ,				_UpperCAmelCase       )
        __a		 =       sum_aa(_UpperCAmelCase     ,				_UpperCAmelCase       )
        __a		 =       sum_aa(_UpperCAmelCase     ,				_UpperCAmelCase       )

    __a		 =       reformat_hex(_UpperCAmelCase       ) + reformat_hex(_UpperCAmelCase       ) + reformat_hex(_UpperCAmelCase       ) + reformat_hex(_UpperCAmelCase       )
    return digest


if __name__ == "__main__":
 import doctest

 doctest.testmod()





