


'''simple docstring'''




import contextlib
import copy
import random
from typing import Any, Dict, Iterable, Optional, Union

import numpy as np
import torch

from .utils import deprecate, is_transformers_available


if is_transformers_available():
 import transformers


def    UpperCAmelCase_  (       A      ):
   '''simple docstring'''





   random.seed(A      )
   np.random.seed(A      )
   torch.manual_seed(A      )
   torch.cuda.manual_seed_all(A      )
   # ^^ safe to call this function even if cuda is not available







class 					a			:
 '''simple docstring'''




 def __init__(    self		,      lowerCamelCase_		,      lowerCamelCase_ = 0.9999		,      lowerCamelCase_ = 0.0		,      lowerCamelCase_ = 0		,      lowerCamelCase_ = False		,      lowerCamelCase_ = 1.0		,      lowerCamelCase_ = 2 / 3		,      lowerCamelCase_ = None		,      lowerCamelCase_ = None		,      **lowerCamelCase_		,      )      ->    Tuple:

    if isinstance(lowerCamelCase_		,      torch.nn.Module      ):
       _a					:  Tuple						=  (
           'Passing a `torch.nn.Module` to `ExponentialMovingAverage` is deprecated. '
           'Please pass the parameters of the module instead.'
       )
       deprecate(
           'passing a `torch.nn.Module` to `ExponentialMovingAverage`'		,      '1.0.0'		,      lowerCamelCase_		,      standard_warn=lowerCamelCase_		,      )
       _a					:  Tuple						=  parameters.parameters()

       # set use_ema_warmup to True if a torch.nn.Module is passed for backwards compatibility
       _a					:  int						=  True

    if kwargs.get('max_value'		,      lowerCamelCase_      ) is not None:
       _a					:  str						=  'The `max_value` argument is deprecated. Please use `decay` instead.'
       deprecate('max_value'		,      '1.0.0'		,      lowerCamelCase_		,      standard_warn=lowerCamelCase_      )
       _a					:  List[str]						=  kwargs['max_value']

    if kwargs.get('min_value'		,      lowerCamelCase_      ) is not None:
       _a					:  List[str]						=  'The `min_value` argument is deprecated. Please use `min_decay` instead.'
       deprecate('min_value'		,      '1.0.0'		,      lowerCamelCase_		,      standard_warn=lowerCamelCase_      )
       _a					:  Dict						=  kwargs['min_value']

    _a					:  List[Any]						=  list(lowerCamelCase_      )
    _a					:  str						=  [p.clone().detach() for p in parameters]

    if kwargs.get('device'		,      lowerCamelCase_      ) is not None:
       _a					:  Union[str, Any]						=  'The `device` argument is deprecated. Please use `to` instead.'
       deprecate('device'		,      '1.0.0'		,      lowerCamelCase_		,      standard_warn=lowerCamelCase_      )
       self.to(device=kwargs['device']      )

    _a					:  str						=  None

    _a					:  Any						=  decay
    _a					:  Union[str, Any]						=  min_decay
    _a					:  Optional[int]						=  update_after_step
    _a					:  str						=  use_ema_warmup
    _a					:  List[Any]						=  inv_gamma
    _a					:  str						=  power
    _a					:  str						=  0
    _a					:  List[str]						=  None  # set in `step()`

    _a					:  int						=  model_cls
    _a					:  Optional[int]						=  model_config




 @classmethod
 def   __UpperCamelCase   (    cls		,      lowerCamelCase_		,      lowerCamelCase_      )      ->    "EMAModel":
    _a		,						_a					:  Optional[Any]						=  model_cls.load_config(lowerCamelCase_		,      return_unused_kwargs=lowerCamelCase_      )
    _a					:  Optional[int]						=  model_cls.from_pretrained(lowerCamelCase_      )

    _a					:  Optional[Any]						=  cls(model.parameters()		,      model_cls=lowerCamelCase_		,      model_config=model.config      )

    ema_model.load_state_dict(lowerCamelCase_      )
    return ema_model




 def   __UpperCamelCase   (    self		,      lowerCamelCase_      )      ->    Optional[Any]:
    if self.model_cls is None:
       raise ValueError('`save_pretrained` can only be used if `model_cls` was defined at __init__.'      )

    if self.model_config is None:
       raise ValueError('`save_pretrained` can only be used if `model_config` was defined at __init__.'      )

    _a					:  Optional[Any]						=  self.model_cls.from_config(self.model_config      )
    _a					:  List[Any]						=  self.state_dict()
    state_dict.pop('shadow_params'		,      lowerCamelCase_      )

    model.register_to_config(**lowerCamelCase_      )
    self.copy_to(model.parameters()      )
    model.save_pretrained(lowerCamelCase_      )




 def   __UpperCamelCase   (    self		,      lowerCamelCase_      )      ->    float:
    _a					:  Optional[Any]						=  max(0		,      optimization_step - self.update_after_step - 1      )

    if step <= 0:
       return 0.0

    if self.use_ema_warmup:
       _a					:  Union[str, Any]						=  1 - (1 + step / self.inv_gamma) ** -self.power
    else:
       _a					:  Optional[int]						=  (1 + step) / (1_0 + step)

    _a					:  str						=  min(lowerCamelCase_		,      self.decay      )
    # make sure decay is not smaller than min_decay
    _a					:  str						=  max(lowerCamelCase_		,      self.min_decay      )
    return cur_decay_value




 @torch.no_grad()
 def   __UpperCamelCase   (    self		,      lowerCamelCase_      )      ->    Tuple:
    if isinstance(lowerCamelCase_		,      torch.nn.Module      ):
       _a					:  str						=  (
           'Passing a `torch.nn.Module` to `ExponentialMovingAverage.step` is deprecated. '
           'Please pass the parameters of the module instead.'
       )
       deprecate(
           'passing a `torch.nn.Module` to `ExponentialMovingAverage.step`'		,      '1.0.0'		,      lowerCamelCase_		,      standard_warn=lowerCamelCase_		,      )
       _a					:  Tuple						=  parameters.parameters()

    _a					:  Any						=  list(lowerCamelCase_      )

    self.optimization_step += 1

    # Compute the decay factor for the exponential moving average.
    _a					:  Dict						=  self.get_decay(self.optimization_step      )
    _a					:  Union[str, Any]						=  decay
    _a					:  Union[str, Any]						=  1 - decay

    _a					:  str						=  contextlib.nullcontext
    if is_transformers_available() and transformers.deepspeed.is_deepspeed_zeroa_enabled():
       import deepspeed

    for s_param, param in zip(self.shadow_params		,      lowerCamelCase_      ):
       if is_transformers_available() and transformers.deepspeed.is_deepspeed_zeroa_enabled():
          _a					:  Dict						=  deepspeed.zero.GatheredParameters(lowerCamelCase_		,      modifier_rank=lowerCamelCase_      )

       with context_manager():
          if param.requires_grad:
             s_param.sub_(one_minus_decay * (s_param - param)      )
          else:
             s_param.copy_(lowerCamelCase_      )




 def   __UpperCamelCase   (    self		,      lowerCamelCase_      )      ->    None:
    _a					:  Optional[int]						=  list(lowerCamelCase_      )
    for s_param, param in zip(self.shadow_params		,      lowerCamelCase_      ):
       param.data.copy_(s_param.to(param.device      ).data      )




 def   __UpperCamelCase   (    self		,      lowerCamelCase_=None		,      lowerCamelCase_=None      )      ->    None:
    _a					:  List[str]						=  [
        p.to(device=lowerCamelCase_		,      dtype=lowerCamelCase_      ) if p.is_floating_point() else p.to(device=lowerCamelCase_      )
        for p in self.shadow_params
    ]




 def   __UpperCamelCase   (    self      )      ->    dict:
    return {
        "decay": self.decay,
        "min_decay": self.min_decay,
        "optimization_step": self.optimization_step,
        "update_after_step": self.update_after_step,
        "use_ema_warmup": self.use_ema_warmup,
        "inv_gamma": self.inv_gamma,
        "power": self.power,
        "shadow_params": self.shadow_params,
    }




 def   __UpperCamelCase   (    self		,      lowerCamelCase_      )      ->    None:
    _a					:  Optional[Any]						=  [param.detach().cpu().clone() for param in parameters]




 def   __UpperCamelCase   (    self		,      lowerCamelCase_      )      ->    None:
    if self.temp_stored_params is None:
       raise RuntimeError('This ExponentialMovingAverage has no `store()`ed weights ' 'to `restore()`'      )
    for c_param, param in zip(self.temp_stored_params		,      lowerCamelCase_      ):
       param.data.copy_(c_param.data      )

    # Better memory-wise.
    _a					:  Any						=  None




 def   __UpperCamelCase   (    self		,      lowerCamelCase_      )      ->    None:
    _a					:  str						=  copy.deepcopy(lowerCamelCase_      )

    _a					:  Union[str, Any]						=  state_dict.get('decay'		,      self.decay      )
    if self.decay < 0.0 or self.decay > 1.0:
       raise ValueError('Decay must be between 0 and 1'      )

    _a					:  int						=  state_dict.get('min_decay'		,      self.min_decay      )
    if not isinstance(self.min_decay		,      lowerCamelCase_      ):
       raise ValueError('Invalid min_decay'      )

    _a					:  Optional[Any]						=  state_dict.get('optimization_step'		,      self.optimization_step      )
    if not isinstance(self.optimization_step		,      lowerCamelCase_      ):
       raise ValueError('Invalid optimization_step'      )

    _a					:  Optional[Any]						=  state_dict.get('update_after_step'		,      self.update_after_step      )
    if not isinstance(self.update_after_step		,      lowerCamelCase_      ):
       raise ValueError('Invalid update_after_step'      )

    _a					:  Dict						=  state_dict.get('use_ema_warmup'		,      self.use_ema_warmup      )
    if not isinstance(self.use_ema_warmup		,      lowerCamelCase_      ):
       raise ValueError('Invalid use_ema_warmup'      )

    _a					:  Any						=  state_dict.get('inv_gamma'		,      self.inv_gamma      )
    if not isinstance(self.inv_gamma		,      (float, int)      ):
       raise ValueError('Invalid inv_gamma'      )

    _a					:  Any						=  state_dict.get('power'		,      self.power      )
    if not isinstance(self.power		,      (float, int)      ):
       raise ValueError('Invalid power'      )

    _a					:  List[Any]						=  state_dict.get('shadow_params'		,      lowerCamelCase_      )
    if shadow_params is not None:
       _a					:  Union[str, Any]						=  shadow_params
       if not isinstance(self.shadow_params		,      lowerCamelCase_      ):
          raise ValueError('shadow_params must be a list'      )
       if not all(isinstance(lowerCamelCase_		,      torch.Tensor      ) for p in self.shadow_params      ):
          raise ValueError('shadow_params must all be Tensors'      )






'''simple docstring'''




from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCAmelCase_      :		Optional[int]          =		logging.get_logger(__name__)

UpperCAmelCase_      :		List[str]          =		{
    "google/vivit-b-16x2-kinetics400": (
        "https://huggingface.co/google/vivit-b-16x2-kinetics400/resolve/main/config.json"
    ),
    # See all Vivit models at https://huggingface.co/models?filter=vivit
}







class 					a			(				snake_case__  ):
 '''simple docstring'''




 __lowerCAmelCase						: str							  =		"""vivit"""




 def __init__(    self		,      lowerCamelCase_=2_2_4		,      lowerCamelCase_=3_2		,      lowerCamelCase_=[2, 1_6, 1_6]		,      lowerCamelCase_=3		,      lowerCamelCase_=7_6_8		,      lowerCamelCase_=1_2		,      lowerCamelCase_=1_2		,      lowerCamelCase_=3_0_7_2		,      lowerCamelCase_="gelu_fast"		,      lowerCamelCase_=0.0		,      lowerCamelCase_=0.0		,      lowerCamelCase_=0.02		,      lowerCamelCase_=1e-06		,      lowerCamelCase_=True		,      **lowerCamelCase_		,      )      ->    int:
    _a					:  Tuple						=  hidden_size
    _a					:  Dict						=  num_hidden_layers
    _a					:  List[str]						=  num_attention_heads
    _a					:  int						=  intermediate_size
    _a					:  Optional[int]						=  hidden_act
    _a					:  Dict						=  hidden_dropout_prob
    _a					:  List[str]						=  attention_probs_dropout_prob
    _a					:  List[str]						=  initializer_range
    _a					:  List[str]						=  layer_norm_eps

    _a					:  Any						=  image_size
    _a					:  Optional[Any]						=  num_frames
    _a					:  Dict						=  tubelet_size
    _a					:  Union[str, Any]						=  num_channels
    _a					:  Optional[int]						=  qkv_bias

    super().__init__(**lowerCamelCase_      )


