


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  __SCREAMING_SNAKE_CASE       (				lowercase__  :Any				)       ->  Any:
						__magic_name__				:							List[Any]				=      'https://storage.googleapis.com/scenic-bucket/vivit/kinetics_400/vivit_base_16x2_unfactorized/checkpoint'

						with open(lowercase__      ,						'wb'				) as f:
												with requests.get(lowercase__      ,						stream=lowercase__				) as req:
																		for chunk in req.iter_content(chunk_size=2_0_4_8				):
																								f.write(lowercase__				)
def  __SCREAMING_SNAKE_CASE       (				)       ->  VivitConfig:
						__magic_name__				:							Dict				=      VivitConfig()

						__magic_name__				:							Any				=      4_0_0
						__magic_name__				:							List[Any]				=      'huggingface/label-files'
						__magic_name__				:							Union[str, Any]				=      'kinetics400-id2label.json'

						__magic_name__				:							int				=      json.load(open(hf_hub_download(lowercase__      ,						lowercase__      ,						repo_type='dataset'				)      ,						'r'				)				)
						__magic_name__				:							Optional[Any]				=      {int(lowercase__				): v for k, v in idalabel.items()}
						__magic_name__				:							List[str]				=      idalabel
						__magic_name__				:							Optional[int]				=      {v: k for k, v in idalabel.items()}
						return config
def  __SCREAMING_SNAKE_CASE       (				)       ->  Tuple:
						__magic_name__				:							int				=      hf_hub_download(
						    repo_id='hf-internal-testing/spaghetti-video'      ,						filename='eating_spaghetti_32_frames.npy'      ,						repo_type='dataset'				)
						__magic_name__				:							List[str]				=      np.load(lowercase__				)
						return list(lowercase__				)
def  __SCREAMING_SNAKE_CASE       (				lowercase__  :np.ndarray				)       ->  Dict:
						if np.ndim(lowercase__				) == 2:
												return transform_attention_bias(lowercase__				)

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

						else:
												raise Exception(f"Invalid number of dimesions: {np.ndim(lowercase__				)}"				)
def  __SCREAMING_SNAKE_CASE       (				lowercase__  :np.ndarray				)       ->  List[Any]:
						return current.flatten()
def  __SCREAMING_SNAKE_CASE       (				lowercase__  :np.ndarray				)       ->  List[str]:
						return np.reshape(lowercase__      ,						(current.shape[0], current.shape[1] * current.shape[2])				).T
def  __SCREAMING_SNAKE_CASE       (				lowercase__  :np.ndarray				)       ->  str:
						return np.reshape(lowercase__      ,						(current.shape[0] * current.shape[1], current.shape[2])				).T
def  __SCREAMING_SNAKE_CASE       (				lowercase__  :Optional[int]      ,						lowercase__  :str				)       ->  Optional[int]:
						__magic_name__				:							Optional[int]				=      state_dict['optimizer']['target']['Transformer'][f"encoderblock_{i}"]

						__magic_name__				:							List[Any]				=      f"encoder.layer.{i}."
						__magic_name__				:							List[str]				=      {
						    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  __SCREAMING_SNAKE_CASE       (				lowercase__  :Tuple				)       ->  Any:
						return sum([1 if 'encoderblock_' in k else 0 for k in state_dict['optimizer']['target']['Transformer'].keys()]				)
def  __SCREAMING_SNAKE_CASE       (				lowercase__  :Optional[Any]      ,						lowercase__  :List[str]=False				)       ->  Optional[int]:
						__magic_name__				:							Optional[Any]				=      get_n_layers(lowercase__				)

						__magic_name__				:							List[str]				=      OrderedDict()

						__magic_name__				:							List[str]				=      state_dict['optimizer']['target']['Transformer']['encoder_norm']['bias']
						__magic_name__				:							List[str]				=      state_dict['optimizer']['target']['Transformer']['encoder_norm']['scale']

						__magic_name__				:							Tuple				=      np.transpose(
						    state_dict['optimizer']['target']['embedding']['kernel']      ,						(4, 3, 0, 1, 2)				)
						__magic_name__				:							str				=      state_dict['optimizer']['target']['embedding']['bias']

						__magic_name__				:							Union[str, Any]				=      state_dict['optimizer']['target']['cls']
						__magic_name__				:							Tuple				=      state_dict['optimizer']['target']['Transformer']['posembed_input'][
						    'pos_embedding'
						]

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

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

						return {k: torch.tensor(lowercase__				) for k, v in new_state.items()}
def  __SCREAMING_SNAKE_CASE       (				)       ->  VivitImageProcessor:
						__magic_name__				:							str				=      VivitImageProcessor()

						assert extractor.do_resize is True
						assert extractor.size == {"shortest_edge": 2_5_6}
						assert extractor.do_center_crop is True
						assert extractor.crop_size == {"width": 2_2_4, "height": 2_2_4}
						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_5_5

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

						return extractor
def  __SCREAMING_SNAKE_CASE       (				lowercase__  :str				)       ->  Tuple:
						__magic_name__				:							Dict				=      'checkpoint'

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

						__magic_name__				:							Dict				=      restore_checkpoint(lowercase__      ,						lowercase__				)
						__magic_name__				:							Optional[int]				=      transform_state(lowercase__      ,						classification_head=lowercase__				)

						__magic_name__				:							Any				=      get_vivit_config()

						assert config.image_size == 2_2_4
						assert config.num_frames == 3_2

						__magic_name__				:							List[str]				=      VivitForVideoClassification(lowercase__				)
						model.load_state_dict(lowercase__				)
						model.eval()

						__magic_name__				:							List[Any]				=      get_processor()

						__magic_name__				:							int				=      prepare_video()
						__magic_name__				:							Optional[int]				=      extractor(lowercase__      ,						return_tensors='pt'				)

						__magic_name__				:							Any				=      model(**lowercase__				)

						__magic_name__				:							Optional[Any]				=      torch.Size([1, 4_0_0]				)
						__magic_name__				:							Optional[int]				=      torch.tensor([-1.0_5_4_3, 2.0_7_6_4, -0.2_1_0_4, 0.4_4_3_9, -0.9_6_5_8]				)

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

						model.save_pretrained(lowercase__				)
						extractor.save_pretrained(lowercase__				)


if __name__ == "__main__":
			snake_case__     							= argparse.ArgumentParser()

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

			snake_case__     							= parser.parse_args()
			convert(args.output_model_name)






# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import ast
import difflib
from collections.abc import Mapping
from typing import Any, Callable, Dict



class     snake_case__  (      _A  ):
				pass






def  __SCREAMING_SNAKE_CASE       (				lowercase__  :str      ,						lowercase__  :Dict[str, Callable]      ,						lowercase__  :Union[str, Any]=None      ,						lowercase__  :str=False				)       ->  List[str]:
						try:
												__magic_name__				:							str				=      ast.parse(lowercase__				)
						except SyntaxError as e:
												print('The code generated by the agent is not valid.\n'      ,						lowercase__				)
												return
						if state is None:
												__magic_name__				:							Optional[Any]				=      {}
						__magic_name__				:							Dict				=      None
						for idx, node in enumerate(expression.body				):
												try:
																		__magic_name__				:							Any				=      evaluate_ast(lowercase__      ,						lowercase__      ,						lowercase__				)
												except InterpretorError as e:
																		__magic_name__				:							Tuple				=      f"Evaluation of the code stopped at line {idx} before the end because of the following error"
																		if chat_mode:
																								msg += (
																								    f". Copy paste the following error message and send it back to the agent:\nI get an error: '{e}'"
																								)
																		else:
																								msg += f":\n{e}"
																		print(lowercase__				)
																		break
												if line_result is not None:
																		__magic_name__				:							List[str]				=      line_result

						return result
def  __SCREAMING_SNAKE_CASE       (				lowercase__  :ast.AST      ,						lowercase__  :Dict[str, Any]      ,						lowercase__  :Dict[str, Callable]				)       ->  Dict:
						if isinstance(lowercase__      ,						ast.Assign				):
												# Assignement -> we evaluate the assignement which should update the state
												# We return the variable assigned as it may be used to determine the final result.
												return evaluate_assign(lowercase__      ,						lowercase__      ,						lowercase__				)
						elif isinstance(lowercase__      ,						ast.Call				):
												# Function call -> we return the value of the function call
												return evaluate_call(lowercase__      ,						lowercase__      ,						lowercase__				)
						elif isinstance(lowercase__      ,						ast.Constant				):
												# Constant -> just return the value
												return expression.value
						elif isinstance(lowercase__      ,						ast.Dict				):
												# Dict -> evaluate all keys and values
												__magic_name__				:							Any				=      [evaluate_ast(lowercase__      ,						lowercase__      ,						lowercase__				) for k in expression.keys]
												__magic_name__				:							Dict				=      [evaluate_ast(lowercase__      ,						lowercase__      ,						lowercase__				) for v in expression.values]
												return dict(zip(lowercase__      ,						lowercase__				)				)
						elif isinstance(lowercase__      ,						ast.Expr				):
												# Expression -> evaluate the content
												return evaluate_ast(expression.value      ,						lowercase__      ,						lowercase__				)
						elif isinstance(lowercase__      ,						ast.For				):
												# For loop -> execute the loop
												return evaluate_for(lowercase__      ,						lowercase__      ,						lowercase__				)
						elif isinstance(lowercase__      ,						ast.FormattedValue				):
												# Formatted value (part of f-string) -> evaluate the content and return
												return evaluate_ast(expression.value      ,						lowercase__      ,						lowercase__				)
						elif isinstance(lowercase__      ,						ast.If				):
												# If -> execute the right branch
												return evaluate_if(lowercase__      ,						lowercase__      ,						lowercase__				)
						elif hasattr(lowercase__      ,						'Index'				) and isinstance(lowercase__      ,						ast.Index				):
												return evaluate_ast(expression.value      ,						lowercase__      ,						lowercase__				)
						elif isinstance(lowercase__      ,						ast.JoinedStr				):
												return "".join([str(evaluate_ast(lowercase__      ,						lowercase__      ,						lowercase__				)				) for v in expression.values]				)
						elif isinstance(lowercase__      ,						ast.List				):
												# List -> evaluate all elements
												return [evaluate_ast(lowercase__      ,						lowercase__      ,						lowercase__				) for elt in expression.elts]
						elif isinstance(lowercase__      ,						ast.Name				):
												# Name -> pick up the value in the state
												return evaluate_name(lowercase__      ,						lowercase__      ,						lowercase__				)
						elif isinstance(lowercase__      ,						ast.Subscript				):
												# Subscript -> return the value of the indexing
												return evaluate_subscript(lowercase__      ,						lowercase__      ,						lowercase__				)
						else:
												# For now we refuse anything else. Let's add things as we need them.
												raise InterpretorError(f"{expression.__class__.__name__} is not supported."				)
def  __SCREAMING_SNAKE_CASE       (				lowercase__  :List[Any]      ,						lowercase__  :Optional[Any]      ,						lowercase__  :Union[str, Any]				)       ->  List[Any]:
						__magic_name__				:							Union[str, Any]				=      assign.targets
						__magic_name__				:							int				=      evaluate_ast(assign.value      ,						lowercase__      ,						lowercase__				)

						if len(lowercase__				) == 1:
												__magic_name__				:							Any				=      result
						else:
												if len(lowercase__				) != len(lowercase__				):
																		raise InterpretorError(f"Expected {len(lowercase__				)} values but got {len(lowercase__				)}."				)
												for var_name, r in zip(lowercase__      ,						lowercase__				):
																		__magic_name__				:							str				=      r
						return result
def  __SCREAMING_SNAKE_CASE       (				lowercase__  :List[str]      ,						lowercase__  :List[str]      ,						lowercase__  :Tuple				)       ->  Optional[Any]:
						if not isinstance(call.func      ,						ast.Name				):
												raise InterpretorError(
												    f"It is not permitted to evaluate other functions than the provided tools (tried to execute {call.func} of "
												    f"type {type(call.func				)}."				)
						__magic_name__				:							Dict				=      call.func.id
						if func_name not in tools:
												raise InterpretorError(
												    f"It is not permitted to evaluate other functions than the provided tools (tried to execute {call.func.id})."				)

						__magic_name__				:							Tuple				=      tools[func_name]
						# Todo deal with args
						__magic_name__				:							List[str]				=      [evaluate_ast(lowercase__      ,						lowercase__      ,						lowercase__				) for arg in call.args]
						__magic_name__				:							int				=      {keyword.arg: evaluate_ast(keyword.value      ,						lowercase__      ,						lowercase__				) for keyword in call.keywords}
						return func(*lowercase__      ,						**lowercase__				)
def  __SCREAMING_SNAKE_CASE       (				lowercase__  :Union[str, Any]      ,						lowercase__  :int      ,						lowercase__  :int				)       ->  List[Any]:
						__magic_name__				:							Optional[int]				=      evaluate_ast(subscript.slice      ,						lowercase__      ,						lowercase__				)
						__magic_name__				:							Union[str, Any]				=      evaluate_ast(subscript.value      ,						lowercase__      ,						lowercase__				)
						if isinstance(lowercase__      ,						(list, tuple)				):
												return value[int(lowercase__				)]
						if index in value:
												return value[index]
						if isinstance(lowercase__      ,						lowercase__				) and isinstance(lowercase__      ,						lowercase__				):
												__magic_name__				:							Any				=      difflib.get_close_matches(lowercase__      ,						list(value.keys()				)				)
												if len(lowercase__				) > 0:
																		return value[close_matches[0]]

						raise InterpretorError(f"Could not index {value} with '{index}'."				)
def  __SCREAMING_SNAKE_CASE       (				lowercase__  :List[str]      ,						lowercase__  :Optional[Any]      ,						lowercase__  :Optional[int]				)       ->  int:
						if name.id in state:
												return state[name.id]
						__magic_name__				:							Union[str, Any]				=      difflib.get_close_matches(name.id      ,						list(state.keys()				)				)
						if len(lowercase__				) > 0:
												return state[close_matches[0]]
						raise InterpretorError(f"The variable `{name.id}` is not defined."				)
def  __SCREAMING_SNAKE_CASE       (				lowercase__  :Any      ,						lowercase__  :Union[str, Any]      ,						lowercase__  :Optional[Any]				)       ->  List[str]:
						if len(condition.ops				) > 1:
												raise InterpretorError('Cannot evaluate conditions with multiple operators'				)

						__magic_name__				:							int				=      evaluate_ast(condition.left      ,						lowercase__      ,						lowercase__				)
						__magic_name__				:							Optional[int]				=      condition.ops[0]
						__magic_name__				:							Union[str, Any]				=      evaluate_ast(condition.comparators[0]      ,						lowercase__      ,						lowercase__				)

						if isinstance(lowercase__      ,						ast.Eq				):
												return left == right
						elif isinstance(lowercase__      ,						ast.NotEq				):
												return left != right
						elif isinstance(lowercase__      ,						ast.Lt				):
												return left < right
						elif isinstance(lowercase__      ,						ast.LtE				):
												return left <= right
						elif isinstance(lowercase__      ,						ast.Gt				):
												return left > right
						elif isinstance(lowercase__      ,						ast.GtE				):
												return left >= right
						elif isinstance(lowercase__      ,						ast.Is				):
												return left is right
						elif isinstance(lowercase__      ,						ast.IsNot				):
												return left is not right
						elif isinstance(lowercase__      ,						ast.In				):
												return left in right
						elif isinstance(lowercase__      ,						ast.NotIn				):
												return left not in right
						else:
												raise InterpretorError(f"Operator not supported: {comparator}"				)
def  __SCREAMING_SNAKE_CASE       (				lowercase__  :Optional[int]      ,						lowercase__  :str      ,						lowercase__  :Tuple				)       ->  int:
						__magic_name__				:							Optional[int]				=      None
						if evaluate_condition(if_statement.test      ,						lowercase__      ,						lowercase__				):
												for line in if_statement.body:
																		__magic_name__				:							List[Any]				=      evaluate_ast(lowercase__      ,						lowercase__      ,						lowercase__				)
																		if line_result is not None:
																								__magic_name__				:							Tuple				=      line_result
						else:
												for line in if_statement.orelse:
																		__magic_name__				:							Optional[int]				=      evaluate_ast(lowercase__      ,						lowercase__      ,						lowercase__				)
																		if line_result is not None:
																								__magic_name__				:							List[Any]				=      line_result
						return result

def  __SCREAMING_SNAKE_CASE       (				lowercase__  :Optional[Any]      ,						lowercase__  :Union[str, Any]      ,						lowercase__  :List[str]				)       ->  Dict:
						__magic_name__				:							int				=      None
						__magic_name__				:							List[Any]				=      evaluate_ast(for_loop.iter      ,						lowercase__      ,						lowercase__				)
						for counter in iterator:
												__magic_name__				:							Tuple				=      counter
												for expression in for_loop.body:
																		__magic_name__				:							Dict				=      evaluate_ast(lowercase__      ,						lowercase__      ,						lowercase__				)
																		if line_result is not None:
																								__magic_name__				:							Dict				=      line_result
						return result


