






"""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	,A	,A=2	,A=True	,A=True	,A=10	,A=10	,A=8	,A=[2, 4, 4]	,A=3	,A=768	,A=5	,A=4	,A=37	,A="gelu_fast"	,A=0.0	,A=0.0	,A=0.02	,A=1e-0_6	,A=True	,A=None	,):
          UpperCAmelCase							=       parent
          UpperCAmelCase							=       batch_size
          UpperCAmelCase							=       is_training
          UpperCAmelCase							=       use_labels
          UpperCAmelCase							=       num_labels
          UpperCAmelCase							=       image_size
          UpperCAmelCase							=       num_frames
          UpperCAmelCase							=       tubelet_size
          UpperCAmelCase							=       num_channels
          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							=       initializer_range
          UpperCAmelCase							=       layer_norm_eps
          UpperCAmelCase							=       qkv_bias
          UpperCAmelCase							=       scope

          UpperCAmelCase							=       (
              (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					):
          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 				_UpperCamelCase							( self					):
          UpperCAmelCase							=       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							=       self.num_labels
          return config






    def 				_UpperCamelCase							( self	,A	,A	,A					):
          UpperCAmelCase							=       VivitModel(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 				_UpperCamelCase							( self	,A	,A	,A					):
          UpperCAmelCase							=       VivitForVideoClassification(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 				_UpperCamelCase							( self					):
          UpperCAmelCase							=       self.prepare_config_and_inputs()
          UpperCAmelCase			,					UpperCAmelCase			,					UpperCAmelCase							=       config_and_inputs
          UpperCAmelCase							=       {"""pixel_values""": pixel_values}
          return config, inputs_dict







@require_torch
class 					lowerCamelCase__					(       snake_case    ,     unittest.TestCase ):
    SCREAMING_SNAKE_CASE		=			(VivitModel, VivitForVideoClassification) if is_torch_available() else ()

    SCREAMING_SNAKE_CASE		=			False
    SCREAMING_SNAKE_CASE		=			False
    SCREAMING_SNAKE_CASE		=			False
    SCREAMING_SNAKE_CASE		=			False






    def 				_UpperCamelCase							( self					):
          UpperCAmelCase							=       VivitModelTester(self					)
          UpperCAmelCase							=       ConfigTester(self	,config_class=A	,has_text_modality=A	,hidden_size=37					)






    def 				_UpperCamelCase							( self	,A	,A	,A=False					):
          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 				_UpperCamelCase							( self					):
          self.config_tester.run_common_tests()






    @unittest.skip(reason="""Vivit does not use inputs_embeds"""					)
    def 				_UpperCamelCase							( self					):
          pass






    def 				_UpperCamelCase							( self					):
          UpperCAmelCase			,					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 				_UpperCamelCase							( self					):
          UpperCAmelCase			,					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""", """head_mask"""]
                self.assertListEqual(arg_names[:2]	,A					)






    def 				_UpperCamelCase							( self					):
          UpperCAmelCase							=       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*A					)






    def 				_UpperCamelCase							( self					):
          UpperCAmelCase							=       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_video_classification(*A					)






    @slow
    def 				_UpperCamelCase							( self					):
          for model_name in VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                UpperCAmelCase							=       VivitModel.from_pretrained(A					)
                self.assertIsNotNone(A					)






    def 				_UpperCamelCase							( self					):
          UpperCAmelCase			,					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							=       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					)

                self.assertListEqual(
                    list(attentions[0].shape[-3:]					)	,[self.model_tester.num_attention_heads, seq_len, seq_len]	,)
                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					)

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






    def 				_UpperCamelCase							( self					):
          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			,					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					)






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







def _a (		):







      """simple docstring"""






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







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






    @cached_property
    def 				_UpperCamelCase							( self					):
          return VivitImageProcessor() if is_vision_available() else None






    @slow
    def 				_UpperCamelCase							( self					):
          UpperCAmelCase							=       VivitForVideoClassification.from_pretrained("""google/vivit-b-16x2-kinetics400"""					).to(A					)

          UpperCAmelCase							=       self.default_image_processor
          UpperCAmelCase							=       prepare_video()
          UpperCAmelCase							=       image_processor(A	,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					)

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

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











"""simple docstring"""







from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import normalize, rescale, resize, to_channel_dimension_format
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging


_UpperCamelCase     			=  logging.get_logger(__name__)







class 					lowerCamelCase__					(       snake_case ):
    SCREAMING_SNAKE_CASE		=			['''pixel_values''']






    def __init__( self	,A = True	,A = None	,A = PILImageResampling.BILINEAR	,A = True	,A = 1 / 255	,A = True	,A = None	,A = None	,**A	,):
          super().__init__(**A					)
          UpperCAmelCase							=       size if size is not None else {"""height""": 224, """width""": 224}
          UpperCAmelCase							=       get_size_dict(A					)
          UpperCAmelCase							=       do_resize
          UpperCAmelCase							=       do_rescale
          UpperCAmelCase							=       do_normalize
          UpperCAmelCase							=       size
          UpperCAmelCase							=       resample
          UpperCAmelCase							=       rescale_factor
          UpperCAmelCase							=       image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
          UpperCAmelCase							=       image_std if image_std is not None else IMAGENET_STANDARD_STD






    def 				_UpperCamelCase							( self	,A	,A	,A = PILImageResampling.BILINEAR	,A = None	,**A	,):
          UpperCAmelCase							=       get_size_dict(A					)
          if "height" not in size or "width" not in size:
                raise ValueError(F'''The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}'''					)
          return resize(
              A	,size=(size["""height"""], size["""width"""])	,resample=A	,data_format=A	,**A					)






    def 				_UpperCamelCase							( self	,A	,A	,A = None	,**A					):
          return rescale(A	,scale=A	,data_format=A	,**A					)






    def 				_UpperCamelCase							( self	,A	,A	,A	,A = None	,**A	,):
          return normalize(A	,mean=A	,std=A	,data_format=A	,**A					)






    def 				_UpperCamelCase							( self	,A	,A = None	,A = None	,A = None	,A = None	,A = None	,A = None	,A = None	,A = None	,A = None	,A = ChannelDimension.FIRST	,**A	,):
          UpperCAmelCase							=       do_resize if do_resize is not None else self.do_resize
          UpperCAmelCase							=       do_rescale if do_rescale is not None else self.do_rescale
          UpperCAmelCase							=       do_normalize if do_normalize is not None else self.do_normalize
          UpperCAmelCase							=       resample if resample is not None else self.resample
          UpperCAmelCase							=       rescale_factor if rescale_factor is not None else self.rescale_factor
          UpperCAmelCase							=       image_mean if image_mean is not None else self.image_mean
          UpperCAmelCase							=       image_std if image_std is not None else self.image_std

          UpperCAmelCase							=       size if size is not None else self.size
          UpperCAmelCase							=       get_size_dict(A					)

          UpperCAmelCase							=       make_list_of_images(A					)

          if not valid_images(A					):
                raise ValueError(
                    """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """
                    """torch.Tensor, tf.Tensor or jax.ndarray."""					)

          if do_resize and size is None:
                raise ValueError("""Size must be specified if do_resize is True."""					)

          if do_rescale and rescale_factor is None:
                raise ValueError("""Rescale factor must be specified if do_rescale is True."""					)

          # All transformations expect numpy arrays.
          UpperCAmelCase							=       [to_numpy_array(A					) for image in images]

          if do_resize:
                UpperCAmelCase							=       [self.resize(image=A	,size=A	,resample=A					) for image in images]

          if do_rescale:
                UpperCAmelCase							=       [self.rescale(image=A	,scale=A					) for image in images]

          if do_normalize:
                UpperCAmelCase							=       [self.normalize(image=A	,mean=A	,std=A					) for image in images]

          UpperCAmelCase							=       [to_channel_dimension_format(A	,A					) for image in images]

          UpperCAmelCase							=       {"""pixel_values""": images}
          return BatchFeature(data=A	,tensor_type=A					)



