



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



      """simple docstring"""
      def __init__(    self   ,    SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_=2   ,    SCREAMING_SNAKE_CASE_=True   ,    SCREAMING_SNAKE_CASE_=True   ,    SCREAMING_SNAKE_CASE_=10   ,    SCREAMING_SNAKE_CASE_=10   ,    SCREAMING_SNAKE_CASE_=8   ,    SCREAMING_SNAKE_CASE_=[2, 4, 4]   ,    SCREAMING_SNAKE_CASE_=3   ,    SCREAMING_SNAKE_CASE_=768   ,    SCREAMING_SNAKE_CASE_=5   ,    SCREAMING_SNAKE_CASE_=4   ,    SCREAMING_SNAKE_CASE_=37   ,    SCREAMING_SNAKE_CASE_="gelu_fast"   ,    SCREAMING_SNAKE_CASE_=0.0   ,    SCREAMING_SNAKE_CASE_=0.0   ,    SCREAMING_SNAKE_CASE_=0.02   ,    SCREAMING_SNAKE_CASE_=1e-06   ,    SCREAMING_SNAKE_CASE_=True   ,    SCREAMING_SNAKE_CASE_=None   ,    )    ->				Dict:
          UpperCamelCase	:Optional[Any]     							=       parent
          UpperCamelCase	:Tuple     							=       batch_size
          UpperCamelCase	:Optional[Any]     							=       is_training
          UpperCamelCase	:Dict     							=       use_labels
          UpperCamelCase	:Optional[Any]     							=       num_labels
          UpperCamelCase	:Tuple     							=       image_size
          UpperCamelCase	:Dict     							=       num_frames
          UpperCamelCase	:Union[str, Any]     							=       tubelet_size
          UpperCamelCase	:str     							=       num_channels
          UpperCamelCase	:Dict     							=       hidden_size
          UpperCamelCase	:List[Any]     							=       num_hidden_layers
          UpperCamelCase	:int     							=       num_attention_heads
          UpperCamelCase	:Tuple     							=       intermediate_size
          UpperCamelCase	:List[str]     							=       hidden_act
          UpperCamelCase	:str     							=       hidden_dropout_prob
          UpperCamelCase	:str     							=       attention_probs_dropout_prob
          UpperCamelCase	:Optional[Any]     							=       initializer_range
          UpperCamelCase	:List[Any]     							=       layer_norm_eps
          UpperCamelCase	:Any     							=       qkv_bias
          UpperCamelCase	:str     							=       scope

          UpperCamelCase	:Optional[Any]     							=       (
              (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    UpperCAmelCase						(    self			)    ->				List[str]:
          UpperCamelCase	:Tuple     							=       floats_tensor(
              [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]			)

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

          UpperCamelCase	:Any     							=       self.get_config()

          return config, pixel_values, labels
      def    UpperCAmelCase						(    self			)    ->				int:
          UpperCamelCase	:int     							=       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   ,    )
          UpperCamelCase	:Any     							=       self.num_labels
          return config
      def    UpperCAmelCase						(    self   ,    SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_			)    ->				str:
          UpperCamelCase	:Optional[int]     							=       VivitModel(config=_lowerCamelCase			)
          model.to(_lowerCamelCase			)
          model.eval()
          UpperCamelCase	:int     							=       model(_lowerCamelCase			)
          self.parent.assertEqual(result.last_hidden_state.shape   ,    (self.batch_size, self.seq_length, self.hidden_size)			)
      def    UpperCAmelCase						(    self   ,    SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_			)    ->				str:
          UpperCamelCase	:int     							=       VivitForVideoClassification(_lowerCamelCase			)
          model.to(_lowerCamelCase			)
          model.eval()

          UpperCamelCase	:Union[str, Any]     							=       model(_lowerCamelCase			)

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




      def    UpperCAmelCase						(    self			)    ->				int:
          UpperCamelCase	:int     							=       self.prepare_config_and_inputs()
          UpperCamelCase							,       UpperCamelCase							,       UpperCamelCase	:List[Any]     							=       config_and_inputs
          UpperCamelCase	:str     							=       {'''pixel_values''': pixel_values}
          return config, inputs_dict







@require_torch
class       UpperCAmelCase_						( _lowerCAmelCase,     unittest.TestCase			):



      """simple docstring"""
      UpperCamelCase_    : List[str]									=(VivitModel, VivitForVideoClassification) if is_torch_available() else ()

      UpperCamelCase_    : Optional[Any]									=False
      UpperCamelCase_    : List[str]									=False
      UpperCamelCase_    : Optional[Any]									=False
      UpperCamelCase_    : str									=False
      def    UpperCAmelCase						(    self			)    ->				List[str]:
          UpperCamelCase	:Union[str, Any]     							=       VivitModelTester(self			)
          UpperCamelCase	:Optional[int]     							=       ConfigTester(self   ,    config_class=_lowerCamelCase   ,    has_text_modality=_lowerCamelCase   ,    hidden_size=37			)
      def    UpperCAmelCase						(    self   ,    SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_=False			)    ->				Tuple:
          UpperCamelCase	:List[Any]     							=       copy.deepcopy(_lowerCamelCase			)

          if return_labels:
              if model_class in get_values(_lowerCamelCase			):
                  UpperCamelCase	:Optional[int]     							=       torch.zeros(
                      self.model_tester.batch_size   ,    dtype=torch.long   ,    device=_lowerCamelCase			)

          return inputs_dict
      def    UpperCAmelCase						(    self			)    ->				Any:
          self.config_tester.run_common_tests()
      @unittest.skip(reason='''Vivit does not use inputs_embeds'''			)
      def    UpperCAmelCase						(    self			)    ->				Any:
          pass
      def    UpperCAmelCase						(    self			)    ->				Optional[Any]:
          UpperCamelCase							,       UpperCamelCase	:Any     							=       self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
              UpperCamelCase	:Tuple     							=       model_class(_lowerCamelCase			)
              self.assertIsInstance(model.get_input_embeddings()   ,    (nn.Module)			)
              UpperCamelCase	:Dict     							=       model.get_output_embeddings()
              self.assertTrue(x is None or isinstance(_lowerCamelCase   ,    nn.Linear			)			)
      def    UpperCAmelCase						(    self			)    ->				Optional[int]:
          UpperCamelCase							,       UpperCamelCase	:Any     							=       self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
              UpperCamelCase	:Dict     							=       model_class(_lowerCamelCase			)
              UpperCamelCase	:int     							=       inspect.signature(model.forward			)
              # signature.parameters is an OrderedDict => so arg_names order is deterministic
              UpperCamelCase	:Union[str, Any]     							=       [*signature.parameters.keys()]

              UpperCamelCase	:List[Any]     							=       ['''pixel_values''', '''head_mask''']
              self.assertListEqual(arg_names[:2]   ,    _lowerCamelCase			)
      def    UpperCAmelCase						(    self			)    ->				Optional[int]:
          UpperCamelCase	:Optional[Any]     							=       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*_lowerCamelCase			)
      def    UpperCAmelCase						(    self			)    ->				List[str]:
          UpperCamelCase	:Tuple     							=       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_video_classification(*_lowerCamelCase			)
      @slow
      def    UpperCAmelCase						(    self			)    ->				Union[str, Any]:
          for model_name in VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              UpperCamelCase	:Optional[int]     							=       VivitModel.from_pretrained(_lowerCamelCase			)
              self.assertIsNotNone(_lowerCamelCase			)
      def    UpperCAmelCase						(    self			)    ->				Union[str, Any]:
          UpperCamelCase							,       UpperCamelCase	:Union[str, Any]     							=       self.model_tester.prepare_config_and_inputs_for_common()
          UpperCamelCase	:List[Any]     							=       True

          for model_class in self.all_model_classes:
              UpperCamelCase	:Dict     							=       self.model_tester.seq_length

              UpperCamelCase	:Tuple     							=       True
              UpperCamelCase	:str     							=       False
              UpperCamelCase	:Union[str, Any]     							=       True
              UpperCamelCase	:List[str]     							=       model_class(_lowerCamelCase			)
              model.to(_lowerCamelCase			)
              model.eval()
              with torch.no_grad():
                  UpperCamelCase	:Any     							=       model(**self._prepare_for_class(_lowerCamelCase   ,    _lowerCamelCase			)			)
              UpperCamelCase	:Optional[int]     							=       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"]
              UpperCamelCase	:Any     							=       True
              UpperCamelCase	:str     							=       model_class(_lowerCamelCase			)
              model.to(_lowerCamelCase			)
              model.eval()
              with torch.no_grad():
                  UpperCamelCase	:Dict     							=       model(**self._prepare_for_class(_lowerCamelCase   ,    _lowerCamelCase			)			)
              UpperCamelCase	:Tuple     							=       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]   ,    )
              UpperCamelCase	:List[Any]     							=       len(_lowerCamelCase			)

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

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

              UpperCamelCase	:List[Any]     							=       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    UpperCAmelCase						(    self			)    ->				List[Any]:
          def check_hidden_states_output(SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_			):
              UpperCamelCase	:int     							=       model_class(_lowerCamelCase			)
              model.to(_lowerCamelCase			)
              model.eval()

              with torch.no_grad():
                  UpperCamelCase	:List[Any]     							=       model(**self._prepare_for_class(_lowerCamelCase   ,    _lowerCamelCase			)			)

              UpperCamelCase	:Optional[int]     							=       outputs.hidden_states
              UpperCamelCase	:Union[str, Any]     							=       self.model_tester.num_hidden_layers + 1
              self.assertEqual(len(_lowerCamelCase			)   ,    _lowerCamelCase			)

              UpperCamelCase	:Optional[Any]     							=       self.model_tester.seq_length

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

          UpperCamelCase							,       UpperCamelCase	:Optional[Any]     							=       self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
              UpperCamelCase	:str     							=       True
              check_hidden_states_output(_lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase			)

              # check that output_hidden_states also work using config
              del inputs_dict["output_hidden_states"]
              UpperCamelCase	:Union[str, Any]     							=       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    UpperCAmelCase						(    self			)    ->				Optional[Any]:
          pass







def   _A       (    ):
    UpperCamelCase	:Any     							=       hf_hub_download(
        repo_id='''hf-internal-testing/spaghetti-video'''					,  filename='''eating_spaghetti_32_frames.npy'''					,  repo_type='''dataset'''	)
    UpperCamelCase	:Dict     							=       np.load(__UpperCamelCase	)
    return list(__UpperCamelCase	)







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



      """simple docstring"""
      @cached_property
      def    UpperCAmelCase						(    self			)    ->				List[str]:
          return VivitImageProcessor() if is_vision_available() else None




      @slow
      def    UpperCAmelCase						(    self			)    ->				List[str]:
          UpperCamelCase	:Optional[Any]     							=       VivitForVideoClassification.from_pretrained('''google/vivit-b-16x2-kinetics400'''			).to(_lowerCamelCase			)

          UpperCamelCase	:str     							=       self.default_image_processor
          UpperCamelCase	:List[Any]     							=       prepare_video()
          UpperCamelCase	:List[str]     							=       image_processor(_lowerCamelCase   ,    return_tensors='''pt'''			).to(_lowerCamelCase			)

          # forward pass
          with torch.no_grad():
              UpperCamelCase	:Optional[int]     							=       model(**_lowerCamelCase			)

          # verify the logits
          UpperCamelCase	:Tuple     							=       torch.Size((1, 400)			)
          self.assertEqual(outputs.logits.shape   ,    _lowerCamelCase			)

          # taken from original model
          UpperCamelCase	:Optional[Any]     							=       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			)			)













def   _A       (    SCREAMING_SNAKE_CASE__  :     int = 998001	):

    # fetches the next number
    for number in range(n - 1					,  9999					,  -1	):
        UpperCamelCase	:Union[str, Any]     							=       str(SCREAMING_SNAKE_CASE__	)

        # checks whether 'str_number' is a palindrome.
        if str_number == str_number[::-1]:
            UpperCamelCase	:Dict     							=       999

            # if 'number' is a product of two 3-digit numbers
            # then number is the answer otherwise fetch next number.
            while divisor != 99:
                if (number % divisor == 0) and (len(str(number // divisor	)	) == 3.0):
                    return number
                divisor -= 1
    raise ValueError('''That number is larger than our acceptable range.'''	)


if __name__ == "__main__":
 print(f'''{solution() = }''')





