




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

     '''simple docstring'''





     def __init__(			self     :    str      ,					__snake_case     :    Dict      ,					__snake_case     :    Union[str, Any]=2      ,					__snake_case     :    int=True      ,					__snake_case     :    str=True      ,					__snake_case     :    List[str]=10      ,					__snake_case     :    Optional[Any]=10      ,					__snake_case     :    str=8      ,					__snake_case     :    str=[2, 4, 4]      ,					__snake_case     :    Optional[int]=3      ,					__snake_case     :    Tuple=7_68      ,					__snake_case     :    Optional[int]=5      ,					__snake_case     :    int=4      ,					__snake_case     :    Optional[int]=37      ,					__snake_case     :    Tuple="gelu_fast"      ,					__snake_case     :    Optional[int]=0.0      ,					__snake_case     :    Any=0.0      ,					__snake_case     :    Tuple=0.02      ,					__snake_case     :    Tuple=1E-06      ,					__snake_case     :    Optional[Any]=True      ,					__snake_case     :    Any=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 						lowerCamelCase_     (			self     :    Optional[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 						lowerCamelCase_     (			self     :    Any       ):
         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 						lowerCamelCase_     (			self     :    Optional[Any]      ,					__snake_case     :    Optional[Any]      ,					__snake_case     :    Dict      ,					__snake_case     :    int       ):
         UpperCAmelCase_											=  VivitModel(config=__snake_case       )
         model.to(__snake_case       )
         model.eval()
         UpperCAmelCase_											=  model(__snake_case       )
         self.parent.assertEqual(result.last_hidden_state.shape      ,					(self.batch_size, self.seq_length, self.hidden_size)       )
     def 						lowerCamelCase_     (			self     :    Tuple      ,					__snake_case     :    List[Any]      ,					__snake_case     :    List[Any]      ,					__snake_case     :    str       ):
         UpperCAmelCase_											=  VivitForVideoClassification(__snake_case       )
         model.to(__snake_case       )
         model.eval()

         UpperCAmelCase_											=  model(__snake_case       )

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




     def 						lowerCamelCase_     (			self     :    Dict       ):
         UpperCAmelCase_											=  self.prepare_config_and_inputs()
         UpperCAmelCase_				,							UpperCAmelCase_				,							UpperCAmelCase_											=  config_and_inputs
         UpperCAmelCase_											=  {'''pixel_values''': pixel_values}
         return config, inputs_dict



@require_torch
class     a  (     _A			,     unittest.TestCase    ):

     '''simple docstring'''





     lowerCAmelCase       : Any     	=						(VivitModel, VivitForVideoClassification) if is_torch_available() else ()

     lowerCAmelCase       : List[str]     	=						False
     lowerCAmelCase       : Tuple     	=						False
     lowerCAmelCase       : Tuple     	=						False
     lowerCAmelCase       : Any     	=						False
     def 						lowerCamelCase_     (			self     :    Dict       ):
         UpperCAmelCase_											=  VivitModelTester(self       )
         UpperCAmelCase_											=  ConfigTester(self      ,					config_class=__snake_case      ,					has_text_modality=__snake_case      ,					hidden_size=37       )
     def 						lowerCamelCase_     (			self     :    Union[str, Any]      ,					__snake_case     :    Optional[int]      ,					__snake_case     :    Union[str, Any]      ,					__snake_case     :    List[Any]=False       ):
         UpperCAmelCase_											=  copy.deepcopy(__snake_case       )

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

         return inputs_dict
     def 						lowerCamelCase_     (			self     :    Optional[Any]       ):
         self.config_tester.run_common_tests()
     @unittest.skip(reason='''Vivit does not use inputs_embeds'''       )
     def 						lowerCamelCase_     (			self     :    Union[str, Any]       ):
         pass
     def 						lowerCamelCase_     (			self     :    str       ):
         UpperCAmelCase_				,							UpperCAmelCase_											=  self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
             UpperCAmelCase_											=  model_class(__snake_case       )
             self.assertIsInstance(model.get_input_embeddings()      ,					(nn.Module)       )
             UpperCAmelCase_											=  model.get_output_embeddings()
             self.assertTrue(x is None or isinstance(__snake_case      ,					nn.Linear       )       )
     def 						lowerCamelCase_     (			self     :    Optional[Any]       ):
         UpperCAmelCase_				,							UpperCAmelCase_											=  self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
             UpperCAmelCase_											=  model_class(__snake_case       )
             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]      ,					__snake_case       )
     def 						lowerCamelCase_     (			self     :    Tuple       ):
         UpperCAmelCase_											=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*__snake_case       )
     def 						lowerCamelCase_     (			self     :    Optional[Any]       ):
         UpperCAmelCase_											=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_video_classification(*__snake_case       )
     @slow
     def 						lowerCamelCase_     (			self     :    List[str]       ):
         for model_name in VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             UpperCAmelCase_											=  VivitModel.from_pretrained(__snake_case       )
             self.assertIsNotNone(__snake_case       )
     def 						lowerCamelCase_     (			self     :    str       ):
         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(__snake_case       )
             model.to(__snake_case       )
             model.eval()
             with torch.no_grad():
                 UpperCAmelCase_											=  model(**self._prepare_for_class(__snake_case      ,					__snake_case       )       )
             UpperCAmelCase_											=  outputs.attentions
             self.assertEqual(len(__snake_case       )      ,					self.model_tester.num_hidden_layers       )

             # check that output_attentions also work using config
             del inputs_dict["output_attentions"]
             UpperCAmelCase_											=  True
             UpperCAmelCase_											=  model_class(__snake_case       )
             model.to(__snake_case       )
             model.eval()
             with torch.no_grad():
                 UpperCAmelCase_											=  model(**self._prepare_for_class(__snake_case      ,					__snake_case       )       )
             UpperCAmelCase_											=  outputs.attentions
             self.assertEqual(len(__snake_case       )      ,					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(__snake_case       )

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

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

             UpperCAmelCase_											=  outputs.attentions

             self.assertEqual(len(__snake_case       )      ,					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 						lowerCamelCase_     (			self     :    Dict       ):
         def check_hidden_states_output(__snake_case     :    Any      ,					__snake_case     :    Optional[int]      ,					__snake_case     :    List[Any]       ):
             UpperCAmelCase_											=  model_class(__snake_case       )
             model.to(__snake_case       )
             model.eval()

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

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

             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(__snake_case      ,					__snake_case      ,					__snake_case       )

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

             check_hidden_states_output(__snake_case      ,					__snake_case      ,					__snake_case       )




     @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''       )
     def 						lowerCamelCase_     (			self     :    Union[str, Any]       ):
         pass




def       SCREAMING_SNAKE_CASE			( )						->							List[str]:
    UpperCAmelCase_											=  hf_hub_download(
        repo_id='''hf-internal-testing/spaghetti-video''' ,  filename='''eating_spaghetti_32_frames.npy''' ,  repo_type='''dataset'''   )
    UpperCAmelCase_											=  np.load(__UpperCamelCase   )
    return list(__UpperCamelCase   )



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

     '''simple docstring'''





     @cached_property
     def 						lowerCamelCase_     (			self     :    Optional[int]       ):
         return VivitImageProcessor() if is_vision_available() else None




     @slow
     def 						lowerCamelCase_     (			self     :    int       ):
         UpperCAmelCase_											=  VivitForVideoClassification.from_pretrained('''google/vivit-b-16x2-kinetics400'''       ).to(__snake_case       )

         UpperCAmelCase_											=  self.default_image_processor
         UpperCAmelCase_											=  prepare_video()
         UpperCAmelCase_											=  image_processor(__snake_case      ,					return_tensors='''pt'''       ).to(__snake_case       )

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

         # verify the logits
         UpperCAmelCase_											=  torch.Size((1, 4_00)       )
         self.assertEqual(outputs.logits.shape      ,					__snake_case       )

         # taken from original model
         UpperCAmelCase_											=  torch.tensor([-1.0_543, 2.0_764, -0.2_104, 0.4_439, -0.9_658]       ).to(__snake_case       )

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











import logging
import os
import sys
import threading
from logging import (
    CRITICAL,  # NOQA
    DEBUG,  # NOQA
    ERROR,  # NOQA
    FATAL,  # NOQA
    INFO,  # NOQA
    NOTSET,  # NOQA
    WARN,  # NOQA
    WARNING,  # NOQA
)
from typing import Optional

from tqdm import auto as tqdm_lib


_lowerCamelCase    				=		threading.Lock()
_lowerCamelCase    				=		None

_lowerCamelCase    				=		{
    'debug': logging.DEBUG,
    'info': logging.INFO,
    'warning': logging.WARNING,
    'error': logging.ERROR,
    'critical': logging.CRITICAL,
}

_lowerCamelCase    				=		logging.WARNING

_lowerCamelCase    				=		True




def       SCREAMING_SNAKE_CASE			( )						->							List[str]:
    UpperCAmelCase_											=  os.getenv('''DIFFUSERS_VERBOSITY''' ,  __UpperCamelCase   )
    if env_level_str:
        if env_level_str in log_levels:
            return log_levels[env_level_str]
        else:
            logging.getLogger().warning(
                f'Unknown option DIFFUSERS_VERBOSITY={env_level_str}, '
                f'has to be one of: { ", ".join(log_levels.keys()   ) }'   )
    return _default_log_level




def       SCREAMING_SNAKE_CASE			( )						->							str:
    return __name__.split('''.'''   )[0]




def       SCREAMING_SNAKE_CASE			( )						->							logging.Logger:
    return logging.getLogger(_get_library_name()   )




def       SCREAMING_SNAKE_CASE			( )						->							None:
    global _default_handler

    with _lock:
        if _default_handler:
            # This library has already configured the library root logger.
            return
        UpperCAmelCase_											=  logging.StreamHandler()  # Set sys.stderr as stream.
        UpperCAmelCase_											=  sys.stderr.flush

        # Apply our default configuration to the library root logger.
        UpperCAmelCase_											=  _get_library_root_logger()
        library_root_logger.addHandler(_default_handler   )
        library_root_logger.setLevel(_get_default_logging_level()   )
        UpperCAmelCase_											=  False




def       SCREAMING_SNAKE_CASE			( )						->							None:
    global _default_handler

    with _lock:
        if not _default_handler:
            return

        UpperCAmelCase_											=  _get_library_root_logger()
        library_root_logger.removeHandler(_default_handler   )
        library_root_logger.setLevel(logging.NOTSET   )
        UpperCAmelCase_											=  None




def       SCREAMING_SNAKE_CASE			( )						->							Dict:
    return log_levels




def       SCREAMING_SNAKE_CASE			( __UpperCamelCase    :		Optional[str] = None   )						->							logging.Logger:

    if name is None:
        UpperCAmelCase_											=  _get_library_name()

    _configure_library_root_logger()
    return logging.getLogger(__UpperCamelCase   )




def       SCREAMING_SNAKE_CASE			( )						->							int:
    _configure_library_root_logger()
    return _get_library_root_logger().getEffectiveLevel()




def       SCREAMING_SNAKE_CASE			( __UpperCamelCase    :		int   )						->							None:
    _configure_library_root_logger()
    _get_library_root_logger().setLevel(__UpperCamelCase   )




def       SCREAMING_SNAKE_CASE			( )						->							Optional[Any]:
    return set_verbosity(__UpperCamelCase   )




def       SCREAMING_SNAKE_CASE			( )						->							Union[str, Any]:
    return set_verbosity(__UpperCamelCase   )




def       SCREAMING_SNAKE_CASE			( )						->							Tuple:
    return set_verbosity(__UpperCamelCase   )




def       SCREAMING_SNAKE_CASE			( )						->							int:
    return set_verbosity(__UpperCamelCase   )




def       SCREAMING_SNAKE_CASE			( )						->							None:
    _configure_library_root_logger()

    assert _default_handler is not None
    _get_library_root_logger().removeHandler(_default_handler   )




def       SCREAMING_SNAKE_CASE			( )						->							None:
    _configure_library_root_logger()

    assert _default_handler is not None
    _get_library_root_logger().addHandler(_default_handler   )




def       SCREAMING_SNAKE_CASE			( __UpperCamelCase    :		logging.Handler   )						->							None:
    _configure_library_root_logger()

    assert handler is not None
    _get_library_root_logger().addHandler(__UpperCamelCase   )




def       SCREAMING_SNAKE_CASE			( __UpperCamelCase    :		logging.Handler   )						->							None:
    _configure_library_root_logger()

    assert handler is not None and handler not in _get_library_root_logger().handlers
    _get_library_root_logger().removeHandler(__UpperCamelCase   )




def       SCREAMING_SNAKE_CASE			( )						->							None:
    _configure_library_root_logger()
    UpperCAmelCase_											=  False




def       SCREAMING_SNAKE_CASE			( )						->							None:
    _configure_library_root_logger()
    UpperCAmelCase_											=  True




def       SCREAMING_SNAKE_CASE			( )						->							None:
    UpperCAmelCase_											=  _get_library_root_logger().handlers

    for handler in handlers:
        UpperCAmelCase_											=  logging.Formatter('''[%(levelname)s|%(filename)s:%(lineno)s] %(asctime)s >> %(message)s'''   )
        handler.setFormatter(__UpperCamelCase   )




def       SCREAMING_SNAKE_CASE			( )						->							None:
    UpperCAmelCase_											=  _get_library_root_logger().handlers

    for handler in handlers:
        handler.setFormatter(__UpperCamelCase   )




def       SCREAMING_SNAKE_CASE			( self    :		Tuple ,  *__UpperCamelCase    :		Any ,  **__UpperCamelCase    :		Any   )						->							Union[str, Any]:
    UpperCAmelCase_											=  os.getenv('''DIFFUSERS_NO_ADVISORY_WARNINGS''' ,  __UpperCamelCase   )
    if no_advisory_warnings:
        return
    self.warning(*__UpperCamelCase ,  **__UpperCamelCase   )


_lowerCamelCase    				=		warning_advice



class     a  :

     '''simple docstring'''





     def __init__(			self     :    List[Any]      ,					*__snake_case     :    Tuple      ,					**__snake_case     :    str       ):  # pylint: disable=unused-argument
         UpperCAmelCase_											=  args[0] if args else None
     def __iter__(			self     :    Union[str, Any]       ):
         return iter(self._iterator       )
     def __getattr__(			self     :    Tuple      ,					__snake_case     :    str       ):

         def empty_fn(*__snake_case     :    Optional[int]      ,					**__snake_case     :    int       ):  # pylint: disable=unused-argument
             return

         return empty_fn
     def __enter__(			self     :    Optional[Any]       ):
         return self




     def __exit__(			self     :    Union[str, Any]      ,					__snake_case     :    List[Any]      ,					__snake_case     :    List[str]      ,					__snake_case     :    Dict       ):
         return



class     a  :

     '''simple docstring'''





     def __call__(			self     :    Union[str, Any]      ,					*__snake_case     :    List[str]      ,					**__snake_case     :    Dict       ):
         if _tqdm_active:
             return tqdm_lib.tqdm(*__snake_case      ,					**__snake_case       )
         else:
             return EmptyTqdm(*__snake_case      ,					**__snake_case       )
     def 						lowerCamelCase_     (			self     :    int      ,					*__snake_case     :    Dict      ,					**__snake_case     :    List[str]       ):
         UpperCAmelCase_											=  None
         if _tqdm_active:
             return tqdm_lib.tqdm.set_lock(*__snake_case      ,					**__snake_case       )




     def 						lowerCamelCase_     (			self     :    str       ):
         if _tqdm_active:
             return tqdm_lib.tqdm.get_lock()


_lowerCamelCase    				=		_tqdm_cls()




def       SCREAMING_SNAKE_CASE			( )						->							bool:
    global _tqdm_active
    return bool(_tqdm_active   )




def       SCREAMING_SNAKE_CASE			( )						->							List[str]:
    global _tqdm_active
    UpperCAmelCase_											=  True




def       SCREAMING_SNAKE_CASE			( )						->							Optional[int]:
    global _tqdm_active
    UpperCAmelCase_											=  False





