'''simple docstring'''

import argparse
import json
import os.path
from collections import OrderedDict

import numpy as np
import requests
import torch
from flax.training.checkpoints import restore_checkpoint
from huggingface_hub import hf_hub_download

from transformers import VivitConfig, VivitForVideoClassification, VivitImageProcessor
from transformers.image_utils import PILImageResampling



def  _lowerCAmelCase				(				__a      )  ->      List[str]:
     '''simple docstring'''







     _UpperCamelCase  :Dict       ="""https://storage.googleapis.com/scenic-bucket/vivit/kinetics_400/vivit_base_16x2_unfactorized/checkpoint"""

     with open(__a			,      """wb"""      ) as f:
          with requests.get(__a			,      stream=__a      ) as req:
               for chunk in req.iter_content(chunk_size=20_48      ):
                    f.write(__a      )



def  _lowerCAmelCase				(				)  ->      VivitConfig:
     '''simple docstring'''







     _UpperCamelCase  :List[Any]       =VivitConfig()

     _UpperCamelCase  :List[str]       =4_00
     _UpperCamelCase  :Any       ="""huggingface/label-files"""
     _UpperCamelCase  :str       ="""kinetics400-id2label.json"""

     _UpperCamelCase  :str       =json.load(open(hf_hub_download(__a			,      __a			,      repo_type="""dataset"""      )			,      """r"""      )      )
     _UpperCamelCase  :Tuple       ={int(__a      ): v for k, v in idalabel.items()}
     _UpperCamelCase  :Tuple       =idalabel
     _UpperCamelCase  :Union[str, Any]       ={v: k for k, v in idalabel.items()}
     return config



def  _lowerCAmelCase				(				)  ->      int:
     '''simple docstring'''







     _UpperCamelCase  :Optional[int]       =hf_hub_download(
         repo_id="""hf-internal-testing/spaghetti-video"""			,      filename="""eating_spaghetti_32_frames.npy"""			,      repo_type="""dataset"""      )
     _UpperCamelCase  :List[Any]       =np.load(__a      )
     return list(__a      )



def  _lowerCAmelCase				(				__a      )  ->      Tuple:
     '''simple docstring'''







     if np.ndim(__a      ) == 2:
          return transform_attention_bias(__a      )

     elif np.ndim(__a      ) == 3:
          return transform_attention_kernel(__a      )

     else:
          raise Exception(F'''Invalid number of dimesions: {np.ndim(__a      )}'''      )



def  _lowerCAmelCase				(				__a      )  ->      Union[str, Any]:
     '''simple docstring'''







     return current.flatten()



def  _lowerCAmelCase				(				__a      )  ->      str:
     '''simple docstring'''







     return np.reshape(__a			,      (current.shape[0], current.shape[1] * current.shape[2])      ).T



def  _lowerCAmelCase				(				__a      )  ->      List[Any]:
     '''simple docstring'''







     return np.reshape(__a			,      (current.shape[0] * current.shape[1], current.shape[2])      ).T



def  _lowerCAmelCase				(				__a			,      __a      )  ->      List[Any]:
     '''simple docstring'''







     _UpperCamelCase  :Union[str, Any]       =state_dict["""optimizer"""]["""target"""]["""Transformer"""][F'''encoderblock_{i}''']

     _UpperCamelCase  :int       =F'''encoder.layer.{i}.'''
     _UpperCamelCase  :List[Any]       ={
         prefix + """intermediate.dense.bias""": state["""MlpBlock_0"""]["""Dense_0"""]["""bias"""],
         prefix + """intermediate.dense.weight""": np.transpose(state["""MlpBlock_0"""]["""Dense_0"""]["""kernel"""]      ),
         prefix + """output.dense.bias""": state["""MlpBlock_0"""]["""Dense_1"""]["""bias"""],
         prefix + """output.dense.weight""": np.transpose(state["""MlpBlock_0"""]["""Dense_1"""]["""kernel"""]      ),
         prefix + """layernorm_before.bias""": state["""LayerNorm_0"""]["""bias"""],
         prefix + """layernorm_before.weight""": state["""LayerNorm_0"""]["""scale"""],
         prefix + """layernorm_after.bias""": state["""LayerNorm_1"""]["""bias"""],
         prefix + """layernorm_after.weight""": state["""LayerNorm_1"""]["""scale"""],
         prefix
         + """attention.attention.query.bias""": transform_attention(
             state["""MultiHeadDotProductAttention_0"""]["""query"""]["""bias"""]      ),
         prefix
         + """attention.attention.query.weight""": transform_attention(
             state["""MultiHeadDotProductAttention_0"""]["""query"""]["""kernel"""]      ),
         prefix
         + """attention.attention.key.bias""": transform_attention(state["""MultiHeadDotProductAttention_0"""]["""key"""]["""bias"""]      ),
         prefix
         + """attention.attention.key.weight""": transform_attention(
             state["""MultiHeadDotProductAttention_0"""]["""key"""]["""kernel"""]      ),
         prefix
         + """attention.attention.value.bias""": transform_attention(
             state["""MultiHeadDotProductAttention_0"""]["""value"""]["""bias"""]      ),
         prefix
         + """attention.attention.value.weight""": transform_attention(
             state["""MultiHeadDotProductAttention_0"""]["""value"""]["""kernel"""]      ),
         prefix + """attention.output.dense.bias""": state["""MultiHeadDotProductAttention_0"""]["""out"""]["""bias"""],
         prefix
         + """attention.output.dense.weight""": transform_attention_output_weight(
             state["""MultiHeadDotProductAttention_0"""]["""out"""]["""kernel"""]      ),
     }

     return new_state



def  _lowerCAmelCase				(				__a      )  ->      str:
     '''simple docstring'''







     return sum([1 if """encoderblock_""" in k else 0 for k in state_dict["""optimizer"""]["""target"""]["""Transformer"""].keys()]      )



def  _lowerCAmelCase				(				__a			,      __a=False      )  ->      Any:
     '''simple docstring'''







     _UpperCamelCase  :Dict       =get_n_layers(__a      )

     _UpperCamelCase  :int       =OrderedDict()

     _UpperCamelCase  :List[Any]       =state_dict["""optimizer"""]["""target"""]["""Transformer"""]["""encoder_norm"""]["""bias"""]
     _UpperCamelCase  :int       =state_dict["""optimizer"""]["""target"""]["""Transformer"""]["""encoder_norm"""]["""scale"""]

     _UpperCamelCase  :Optional[int]       =np.transpose(
         state_dict["""optimizer"""]["""target"""]["""embedding"""]["""kernel"""]			,      (4, 3, 0, 1, 2)      )
     _UpperCamelCase  :Tuple       =state_dict["""optimizer"""]["""target"""]["""embedding"""]["""bias"""]

     _UpperCamelCase  :List[Any]       =state_dict["""optimizer"""]["""target"""]["""cls"""]
     _UpperCamelCase  :Optional[int]       =state_dict["""optimizer"""]["""target"""]["""Transformer"""]["""posembed_input"""][
         """pos_embedding"""
     ]

     for i in range(__a      ):
          new_state.update(transform_state_encoder_block(__a			,      __a      )      )

     if classification_head:
          _UpperCamelCase  :List[str]       ={"""vivit.""" + k: v for k, v in new_state.items()}
          _UpperCamelCase  :str       =np.transpose(state_dict["""optimizer"""]["""target"""]["""output_projection"""]["""kernel"""]      )
          _UpperCamelCase  :Optional[Any]       =np.transpose(state_dict["""optimizer"""]["""target"""]["""output_projection"""]["""bias"""]      )

     return {k: torch.tensor(__a      ) for k, v in new_state.items()}



def  _lowerCAmelCase				(				)  ->      VivitImageProcessor:
     '''simple docstring'''







     _UpperCamelCase  :List[str]       =VivitImageProcessor()

     assert extractor.do_resize is True
     assert extractor.size == {"shortest_edge": 2_56}
     assert extractor.do_center_crop is True
     assert extractor.crop_size == {"width": 2_24, "height": 2_24}
     assert extractor.resample == PILImageResampling.BILINEAR

     # here: https://github.com/deepmind/dmvr/blob/master/dmvr/modalities.py
     # one can seen that add_image has default values for normalization_mean and normalization_std set to 0 and 1
     # which effectively means no normalization (and ViViT does not overwrite those when calling this func)
     assert extractor.do_normalize is False
     assert extractor.do_rescale is True
     assert extractor.rescale_factor == 1 / 2_55

     # zero-centering = True in original implementation
     assert extractor.do_zero_centering is True

     return extractor



def  _lowerCAmelCase				(				__a      )  ->      str:
     '''simple docstring'''







     _UpperCamelCase  :Optional[int]       ="""checkpoint"""

     if not os.path.exists(__a      ):
          download_checkpoint(__a      )

     _UpperCamelCase  :str       =restore_checkpoint(__a			,      __a      )
     _UpperCamelCase  :List[Any]       =transform_state(__a			,      classification_head=__a      )

     _UpperCamelCase  :str       =get_vivit_config()

     assert config.image_size == 2_24
     assert config.num_frames == 32

     _UpperCamelCase  :int       =VivitForVideoClassification(__a      )
     model.load_state_dict(__a      )
     model.eval()

     _UpperCamelCase  :List[str]       =get_processor()

     _UpperCamelCase  :Any       =prepare_video()
     _UpperCamelCase  :Union[str, Any]       =extractor(__a			,      return_tensors="""pt"""      )

     _UpperCamelCase  :str       =model(**__a      )

     _UpperCamelCase  :str       =torch.Size([1, 4_00]      )
     _UpperCamelCase  :Optional[int]       =torch.tensor([-1.05_43, 2.07_64, -0.21_04, 0.44_39, -0.96_58]      )

     assert outputs.logits.shape == expected_shape
     assert torch.allclose(outputs.logits[0, :5]			,      __a			,      atol=1e-4      ), outputs.logits[0, :5]

     model.save_pretrained(__a      )
     extractor.save_pretrained(__a      )


if __name__ == "__main__":
      _lowerCamelCase :				Optional[int]														= argparse.ArgumentParser()

      parser.add_argument("""--output_model_name""", """-o""", type=str, help="""Output path for the converted HuggingFace model""")

      _lowerCamelCase :				int														= parser.parse_args()
      convert(args.output_model_name)

import math
from collections import OrderedDict

import torch
from packaging import version
from torch import Tensor, nn

from .utils import logging


UpperCAmelCase__						:	Any	     = logging.get_logger(__name__)






class UpperCAmelCase  (		nn.Module  ):





   '''simple docstring'''


   def __init__(   self	:				List[str]	):

      """simple docstring"""
      super().__init__()
      if version.parse(torch.__version__	) < version.parse('''1.12.0'''	):
         raise ImportError(
             F"""You are using torch=={torch.__version__}, but torch>=1.12.0 is required to use """
             '''PytorchGELUTanh. Please upgrade torch.'''	)


   def 					__magic_name__							(   self	:				Any     ,     lowerCAmelCase_	:				Tensor	):

      """simple docstring"""
      return nn.functional.gelu(lowerCAmelCase_     ,     approximate='''tanh'''	)






class UpperCAmelCase  (		nn.Module  ):





   '''simple docstring'''


   def 					__magic_name__							(   self	:				Tuple     ,     lowerCAmelCase_	:				Tensor	):

      """simple docstring"""
      return 0.5 * input * (1.0 + torch.tanh(math.sqrt(2.0 / math.pi	) * (input + 0.044715 * torch.pow(lowerCAmelCase_     ,     3.0	))	))






class UpperCAmelCase  (		nn.Module  ):





   '''simple docstring'''


   def __init__(   self	:				Tuple     ,     lowerCAmelCase_	:				bool = False	):

      """simple docstring"""
      super().__init__()
      if use_gelu_python:
         _A:    Union[str, Any]						     =			self._gelu_python
      else:
         _A:    Union[str, Any]						     =			nn.functional.gelu


   def 					__magic_name__							(   self	:				Union[str, Any]     ,     lowerCAmelCase_	:				Tensor	):

      """simple docstring"""
      return input * 0.5 * (1.0 + torch.erf(input / math.sqrt(2.0	)	))


   def 					__magic_name__							(   self	:				Any     ,     lowerCAmelCase_	:				Tensor	):

      """simple docstring"""
      return self.act(lowerCAmelCase_	)






class UpperCAmelCase  (		nn.Module  ):





   '''simple docstring'''


   def 					__magic_name__							(   self	:				Optional[int]     ,     lowerCAmelCase_	:				Tensor	):

      """simple docstring"""
      return 0.5 * input * (1.0 + torch.tanh(input * 0.7978845608 * (1.0 + 0.044715 * input * input)	))






class UpperCAmelCase  (		nn.Module  ):





   '''simple docstring'''


   def 					__magic_name__							(   self	:				Optional[int]     ,     lowerCAmelCase_	:				Tensor	):

      """simple docstring"""
      return input * torch.sigmoid(1.702 * input	)






class UpperCAmelCase  (		nn.Module  ):





   '''simple docstring'''


   def __init__(   self	:				str     ,     lowerCAmelCase_	:				float     ,     lowerCAmelCase_	:				float	):

      """simple docstring"""
      if min > max:
         raise ValueError(F"""min should be < max (got min: {min}, max: {max})"""	)

      super().__init__()
      _A:    Tuple						     =			min
      _A:    str						     =			max


   def 					__magic_name__							(   self	:				int     ,     lowerCAmelCase_	:				Tensor	):

      """simple docstring"""
      return torch.clip(gelu(lowerCAmelCase_	)     ,     self.min     ,     self.max	)






class UpperCAmelCase  (		nn.Module  ):





   '''simple docstring'''


   def __init__(   self	:				Optional[Any]	):

      """simple docstring"""
      super().__init__()
      _A:    Any						     =			math.sqrt(2 / math.pi	)


   def 					__magic_name__							(   self	:				Union[str, Any]     ,     lowerCAmelCase_	:				Tensor	):

      """simple docstring"""
      return 0.5 * input * (1 + torch.tanh(self.precomputed_constant * (input + 0.044715 * torch.pow(lowerCAmelCase_     ,     3	))	))






class UpperCAmelCase  (		nn.Module  ):





   '''simple docstring'''


   def 					__magic_name__							(   self	:				str     ,     lowerCAmelCase_	:				Tensor	):

      """simple docstring"""
      return nn.functional.silu(lowerCAmelCase_	)






class UpperCAmelCase  (		nn.Module  ):





   '''simple docstring'''


   def __init__(   self	:				Union[str, Any]	):

      """simple docstring"""
      super().__init__()
      if version.parse(torch.__version__	) < version.parse('''1.9.0'''	):
         _A:    Optional[Any]						     =			self._mish_python
      else:
         _A:    int						     =			nn.functional.mish


   def 					__magic_name__							(   self	:				Optional[int]     ,     lowerCAmelCase_	:				Tensor	):

      """simple docstring"""
      return input * torch.tanh(nn.functional.softplus(lowerCAmelCase_	)	)


   def 					__magic_name__							(   self	:				Any     ,     lowerCAmelCase_	:				Tensor	):

      """simple docstring"""
      return self.act(lowerCAmelCase_	)






class UpperCAmelCase  (		nn.Module  ):





   '''simple docstring'''


   def 					__magic_name__							(   self	:				Tuple     ,     lowerCAmelCase_	:				Tensor	):

      """simple docstring"""
      return input






class UpperCAmelCase  (		nn.Module  ):





   '''simple docstring'''


   def 					__magic_name__							(   self	:				Dict     ,     lowerCAmelCase_	:				List[str]     ,     lowerCAmelCase_	:				Tuple=0.707107     ,     lowerCAmelCase_	:				Union[str, Any]=0.282095	):

      """simple docstring"""
      _A:    Any						     =			(input - mu).div(sigma * math.sqrt(2.0	)	)
      return 0.5 * (1.0 + torch.erf(lowerCAmelCase_	))






class UpperCAmelCase  (		nn.Module  ):





   '''simple docstring'''


   def 					__magic_name__							(   self	:				Union[str, Any]     ,     lowerCAmelCase_	:				List[Any]	):

      """simple docstring"""
      _A:    Optional[int]						     =			nn.functional.relu(lowerCAmelCase_	)
      _A:    Any						     =			torch.square(lowerCAmelCase_	)
      return squared






class UpperCAmelCase  (		SCREAMING_SNAKE_CASE__  ):





   '''simple docstring'''


   def __getitem__(   self	:				List[Any]     ,     lowerCAmelCase_	:				Tuple	):

      """simple docstring"""
      _A:    Any						     =			super().__getitem__(lowerCAmelCase_	)
      _A     ,    _A:    List[str]						     =			content if isinstance(lowerCAmelCase_     ,     lowerCAmelCase_	) else (content, {})
      return cls(**lowerCAmelCase_	)

UpperCAmelCase__						:	List[str]	     = {
    'gelu': GELUActivation,
    'gelu_10': (ClippedGELUActivation, {'min': -10, 'max': 10}),
    'gelu_fast': FastGELUActivation,
    'gelu_new': NewGELUActivation,
    'gelu_python': (GELUActivation, {'use_gelu_python': True}),
    'gelu_pytorch_tanh': PytorchGELUTanh,
    'gelu_accurate': AccurateGELUActivation,
    'laplace': LaplaceActivation,
    'linear': LinearActivation,
    'mish': MishActivation,
    'quick_gelu': QuickGELUActivation,
    'relu': nn.ReLU,
    'relu2': ReLUSquaredActivation,
    'relu6': nn.ReLUa,
    'sigmoid': nn.Sigmoid,
    'silu': SiLUActivation,
    'swish': SiLUActivation,
    'tanh': nn.Tanh,
}
UpperCAmelCase__						:	List[str]	     = ClassInstantier(ACTaCLS)






def     lowerCamelCase__				(   a ) ->  List[str]:
   if activation_string in ACTaFN:
      return ACTaFN[activation_string]
   else:
      raise KeyError(f"""function {activation_string} not found in ACT2FN mapping {list(ACTaFN.keys() )}""" )


# For backwards compatibility with: from activations import gelu_python
UpperCAmelCase__						:	Any	     = get_activation('gelu_python')
UpperCAmelCase__						:	Dict	     = get_activation('gelu_new')
UpperCAmelCase__						:	List[Any]	     = get_activation('gelu')
UpperCAmelCase__						:	Dict	     = get_activation('gelu_fast')
UpperCAmelCase__						:	str	     = get_activation('quick_gelu')
UpperCAmelCase__						:	Union[str, Any]	     = get_activation('silu')
UpperCAmelCase__						:	List[Any]	     = get_activation('mish')





UpperCAmelCase__						:	int	     = get_activation('linear')



