





'''simple docstring'''


import copy
import inspect
import unittest

import numpy as np
from huggingface_hub import hf_hub_download

from transformers import VivitConfig
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


if is_torch_available():
       import torch
       from torch import nn

       from transformers import MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING, VivitForVideoClassification, VivitModel
       from transformers.models.vivit.modeling_vivit import VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
       from transformers import VivitImageProcessor

class 				lowerCAmelCase_						:




 def __init__(		self     ,       _lowerCAmelCase     ,       _lowerCAmelCase=2     ,       _lowerCAmelCase=True     ,       _lowerCAmelCase=True     ,       _lowerCAmelCase=10     ,       _lowerCAmelCase=10     ,       _lowerCAmelCase=8     ,       _lowerCAmelCase=[2, 4, 4]     ,       _lowerCAmelCase=3     ,       _lowerCAmelCase=768     ,       _lowerCAmelCase=5     ,       _lowerCAmelCase=4     ,       _lowerCAmelCase=37     ,       _lowerCAmelCase="gelu_fast"     ,       _lowerCAmelCase=0.0     ,       _lowerCAmelCase=0.0     ,       _lowerCAmelCase=0.02     ,       _lowerCAmelCase=1E-06     ,       _lowerCAmelCase=True     ,       _lowerCAmelCase=None     ,       )      ->							Union[str, Any]:
     _lowerCAmelCase		     = parent
     _lowerCAmelCase		     = batch_size
     _lowerCAmelCase		     = is_training
     _lowerCAmelCase		     = use_labels
     _lowerCAmelCase		     = num_labels
     _lowerCAmelCase		     = image_size
     _lowerCAmelCase		     = num_frames
     _lowerCAmelCase		     = tubelet_size
     _lowerCAmelCase		     = num_channels
     _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		     = initializer_range
     _lowerCAmelCase		     = layer_norm_eps
     _lowerCAmelCase		     = qkv_bias
     _lowerCAmelCase		     = scope

     _lowerCAmelCase		     = (
         (self.image_size // self.tubelet_size[2])
         * (self.image_size // self.tubelet_size[1])
         * (self.num_frames // self.tubelet_size[0])
     ) + 1  # CLS token




 def 							_snake_case				(		self						)      ->							Any:
     _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 							_snake_case				(		self						)      ->							Dict:
     _lowerCAmelCase		     = VivitConfig(
         num_frames=self.num_frames     ,       image_size=self.image_size     ,       tubelet_size=self.tubelet_size     ,       num_channels=self.num_channels     ,       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     ,       layer_norm_eps=self.layer_norm_eps     ,       qkv_bias=self.qkv_bias     ,       )
     _lowerCAmelCase		     = self.num_labels
     return config




 def 							_snake_case				(		self     ,       _lowerCAmelCase     ,       _lowerCAmelCase     ,       _lowerCAmelCase						)      ->							Tuple:
     _lowerCAmelCase		     = VivitModel(config=_lowerCAmelCase						)
     model.to(_lowerCAmelCase						)
     model.eval()
     _lowerCAmelCase		     = model(_lowerCAmelCase						)
     self.parent.assertEqual(result.last_hidden_state.shape     ,       (self.batch_size, self.seq_length, self.hidden_size)						)




 def 							_snake_case				(		self     ,       _lowerCAmelCase     ,       _lowerCAmelCase     ,       _lowerCAmelCase						)      ->							List[str]:
     _lowerCAmelCase		     = VivitForVideoClassification(_lowerCAmelCase						)
     model.to(_lowerCAmelCase						)
     model.eval()

     _lowerCAmelCase		     = model(_lowerCAmelCase						)

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





 def 							_snake_case				(		self						)      ->							Optional[Any]:
     _lowerCAmelCase		     = self.prepare_config_and_inputs()
     _lowerCAmelCase     ,     _lowerCAmelCase     ,     _lowerCAmelCase		     = config_and_inputs
     _lowerCAmelCase		     = {"pixel_values": pixel_values}
     return config, inputs_dict







@require_torch
class 				lowerCAmelCase_						(						__magic_name__  ,unittest.TestCase						):
 __lowerCamelCase  :							Tuple    					=   (VivitModel, VivitForVideoClassification) if is_torch_available() else ()

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




 def 							_snake_case				(		self						)      ->							Any:
     _lowerCAmelCase		     = VivitModelTester(self						)
     _lowerCAmelCase		     = ConfigTester(self     ,       config_class=_lowerCAmelCase     ,       has_text_modality=_lowerCAmelCase     ,       hidden_size=37						)




 def 							_snake_case				(		self     ,       _lowerCAmelCase     ,       _lowerCAmelCase     ,       _lowerCAmelCase=False						)      ->							Optional[int]:
     _lowerCAmelCase		     = copy.deepcopy(_lowerCAmelCase						)

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

     return inputs_dict




 def 							_snake_case				(		self						)      ->							List[Any]:
     self.config_tester.run_common_tests()




 @unittest.skip(reason="Vivit does not use inputs_embeds"						)
 def 							_snake_case				(		self						)      ->							Optional[Any]:
     pass




 def 							_snake_case				(		self						)      ->							List[str]:
     _lowerCAmelCase     ,     _lowerCAmelCase		     = self.model_tester.prepare_config_and_inputs_for_common()

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




 def 							_snake_case				(		self						)      ->							str:
     _lowerCAmelCase     ,     _lowerCAmelCase		     = self.model_tester.prepare_config_and_inputs_for_common()

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

         _lowerCAmelCase		     = ["pixel_values", "head_mask"]
         self.assertListEqual(arg_names[:2]     ,       _lowerCAmelCase						)




 def 							_snake_case				(		self						)      ->							str:
     _lowerCAmelCase		     = self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*_lowerCAmelCase						)




 def 							_snake_case				(		self						)      ->							str:
     _lowerCAmelCase		     = self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_video_classification(*_lowerCAmelCase						)




 @slow
 def 							_snake_case				(		self						)      ->							Optional[Any]:
     for model_name in VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         _lowerCAmelCase		     = VivitModel.from_pretrained(_lowerCAmelCase						)
         self.assertIsNotNone(_lowerCAmelCase						)




 def 							_snake_case				(		self						)      ->							int:
     _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		     = True
         _lowerCAmelCase		     = False
         _lowerCAmelCase		     = True
         _lowerCAmelCase		     = model_class(_lowerCAmelCase						)
         model.to(_lowerCAmelCase						)
         model.eval()
         with torch.no_grad():
             _lowerCAmelCase		     = model(**self._prepare_for_class(_lowerCAmelCase     ,       _lowerCAmelCase						)						)
         _lowerCAmelCase		     = outputs.attentions
         self.assertEqual(len(_lowerCAmelCase						)     ,       self.model_tester.num_hidden_layers						)

         # check that output_attentions also work using config
         del inputs_dict["output_attentions"]
         _lowerCAmelCase		     = True
         _lowerCAmelCase		     = model_class(_lowerCAmelCase						)
         model.to(_lowerCAmelCase						)
         model.eval()
         with torch.no_grad():
             _lowerCAmelCase		     = model(**self._prepare_for_class(_lowerCAmelCase     ,       _lowerCAmelCase						)						)
         _lowerCAmelCase		     = outputs.attentions
         self.assertEqual(len(_lowerCAmelCase						)     ,       self.model_tester.num_hidden_layers						)

         self.assertListEqual(
             list(attentions[0].shape[-3:]						)     ,       [self.model_tester.num_attention_heads, seq_len, seq_len]     ,       )
         _lowerCAmelCase		     = len(_lowerCAmelCase						)

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

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

         _lowerCAmelCase		     = outputs.attentions

         self.assertEqual(len(_lowerCAmelCase						)     ,       self.model_tester.num_hidden_layers						)

         self.assertListEqual(
             list(self_attentions[0].shape[-3:]						)     ,       [self.model_tester.num_attention_heads, seq_len, seq_len]     ,       )




 def 							_snake_case				(		self						)      ->							Optional[Any]:
     def check_hidden_states_output(_lowerCAmelCase     ,       _lowerCAmelCase     ,       _lowerCAmelCase						):
         _lowerCAmelCase		     = model_class(_lowerCAmelCase						)
         model.to(_lowerCAmelCase						)
         model.eval()

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

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

         _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(_lowerCAmelCase     ,       _lowerCAmelCase     ,       _lowerCAmelCase						)

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

         check_hidden_states_output(_lowerCAmelCase     ,       _lowerCAmelCase     ,       _lowerCAmelCase						)





 @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."						)
 def 							_snake_case				(		self						)      ->							str:
     pass







def 		__a():



    '''simple docstring'''





    _lowerCAmelCase		     = hf_hub_download(
        repo_id="hf-internal-testing/spaghetti-video"						,    filename="eating_spaghetti_32_frames.npy"						,    repo_type="dataset"      )
    _lowerCAmelCase		     = np.load(SCREAMING_SNAKE_CASE_      )
    return list(SCREAMING_SNAKE_CASE_      )





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




 @cached_property
 def 							_snake_case				(		self						)      ->							Union[str, Any]:
     return VivitImageProcessor() if is_vision_available() else None





 @slow
 def 							_snake_case				(		self						)      ->							Optional[int]:
     _lowerCAmelCase		     = VivitForVideoClassification.from_pretrained("google/vivit-b-16x2-kinetics400"						).to(_lowerCAmelCase						)

     _lowerCAmelCase		     = self.default_image_processor
     _lowerCAmelCase		     = prepare_video()
     _lowerCAmelCase		     = image_processor(_lowerCAmelCase     ,       return_tensors="pt"						).to(_lowerCAmelCase						)

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

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

     # taken from original model
     _lowerCAmelCase		     = torch.tensor([-1.0543, 2.0764, -0.2104, 0.4439, -0.9658]						).to(_lowerCAmelCase						)

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









'''simple docstring'''


import math


def 		__a(SCREAMING_SNAKE_CASE_					:   int      ):



    '''simple docstring'''





    if number == 0:
        _lowerCAmelCase		     = "Zero"
        return words
    else:
        _lowerCAmelCase		     = math.logaa(SCREAMING_SNAKE_CASE_      )
        _lowerCAmelCase		     = digits + 1
        _lowerCAmelCase		     = {}
        _lowerCAmelCase		     = ""
        _lowerCAmelCase		     = "One"
        _lowerCAmelCase		     = "Two"
        _lowerCAmelCase		     = "Three"
        _lowerCAmelCase		     = "Four"
        _lowerCAmelCase		     = "Five"
        _lowerCAmelCase		     = "Six"
        _lowerCAmelCase		     = "Seven"
        _lowerCAmelCase		     = "Eight"
        _lowerCAmelCase		     = "Nine"

        _lowerCAmelCase		     = {}
        _lowerCAmelCase		     = ""
        _lowerCAmelCase		     = "Twenty"
        _lowerCAmelCase		     = "Thirty"
        _lowerCAmelCase		     = "Forty"
        _lowerCAmelCase		     = "Fifty"
        _lowerCAmelCase		     = "Sixty"
        _lowerCAmelCase		     = "Seventy"
        _lowerCAmelCase		     = "Eighty"
        _lowerCAmelCase		     = "Ninety"

        _lowerCAmelCase		     = {}
        _lowerCAmelCase		     = "Ten"
        _lowerCAmelCase		     = "Eleven"
        _lowerCAmelCase		     = "Twelve"
        _lowerCAmelCase		     = "Thirteen"
        _lowerCAmelCase		     = "Fourteen"
        _lowerCAmelCase		     = "Fifteen"
        _lowerCAmelCase		     = "Sixteen"
        _lowerCAmelCase		     = "Seventeen"
        _lowerCAmelCase		     = "Eighteen"
        _lowerCAmelCase		     = "Nineteen"

        _lowerCAmelCase		     = {}
        _lowerCAmelCase		     = "Hundred,"
        _lowerCAmelCase		     = "Thousand,"
        _lowerCAmelCase		     = "Lakh,"
        _lowerCAmelCase		     = "Crore,"

        _lowerCAmelCase		     = number
        _lowerCAmelCase		     = ""
        _lowerCAmelCase		     = 0
        _lowerCAmelCase		     = int(SCREAMING_SNAKE_CASE_      )
        while counter < digits:
            _lowerCAmelCase		     = temp_num % 10
            if counter % 2 == 0:
                _lowerCAmelCase		     = ""
                if counter in placevalue and current != 0:
                    _lowerCAmelCase		     = placevalue[counter]
                if counter == 2:
                    _lowerCAmelCase		     = singles[current] + addition + words
                elif counter == 0:
                    if ((temp_num % 100) // 10) == 1:
                        _lowerCAmelCase		     = teens[current] + addition + words
                        _lowerCAmelCase		     = temp_num // 10
                        counter += 1
                    else:
                        _lowerCAmelCase		     = singles[current] + addition + words

                else:
                    _lowerCAmelCase		     = doubles[current] + addition + words

            else:
                if counter == 1:
                    if current == 1:
                        _lowerCAmelCase		     = teens[number % 10] + words
                    else:
                        _lowerCAmelCase		     = ""
                        if counter in placevalue:
                            _lowerCAmelCase		     = placevalue[counter]
                        _lowerCAmelCase		     = doubles[current] + addition + words
                else:
                    _lowerCAmelCase		     = ""
                    if counter in placevalue:
                        if current == 0 and ((temp_num % 100) // 10) == 0:
                            _lowerCAmelCase		     = ""
                        else:
                            _lowerCAmelCase		     = placevalue[counter]
                    if ((temp_num % 100) // 10) == 1:
                        _lowerCAmelCase		     = teens[current] + addition + words
                        _lowerCAmelCase		     = temp_num // 10
                        counter += 1
                    else:
                        _lowerCAmelCase		     = singles[current] + addition + words
            counter += 1
            _lowerCAmelCase		     = temp_num // 10
    return words


if __name__ == "__main__":
       import doctest

       doctest.testmod()


