





'''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						(						lowercase	)					->					Tuple:
			__lowerCAmelCase											=				"""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=2048	):
												f.write(lowercase	)





def    _lowerCAmelCase						(						)					->					VivitConfig:
			__lowerCAmelCase											=				VivitConfig()

			__lowerCAmelCase											=				400
			__lowerCAmelCase											=				"""huggingface/label-files"""
			__lowerCAmelCase											=				"""kinetics400-id2label.json"""

			__lowerCAmelCase											=				json.load(open(hf_hub_download(lowercase					,	lowercase					,	repo_type="""dataset"""	)					,	"""r"""	)	)
			__lowerCAmelCase											=				{int(lowercase	): v for k, v in idalabel.items()}
			__lowerCAmelCase											=				idalabel
			__lowerCAmelCase											=				{v: k for k, v in idalabel.items()}
			return config





def    _lowerCAmelCase						(						)					->					List[Any]:
			__lowerCAmelCase											=				hf_hub_download(
			    repo_id="""hf-internal-testing/spaghetti-video"""					,	filename="""eating_spaghetti_32_frames.npy"""					,	repo_type="""dataset"""	)
			__lowerCAmelCase											=				np.load(lowercase	)
			return list(lowercase	)





def    _lowerCAmelCase						(						lowercase	)					->					Union[str, Any]:
			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    _lowerCAmelCase						(						lowercase	)					->					Optional[int]:
			return current.flatten()





def    _lowerCAmelCase						(						lowercase	)					->					Optional[Any]:
			return np.reshape(lowercase					,	(current.shape[0], current.shape[1] * current.shape[2])	).T





def    _lowerCAmelCase						(						lowercase	)					->					Union[str, Any]:
			return np.reshape(lowercase					,	(current.shape[0] * current.shape[1], current.shape[2])	).T





def    _lowerCAmelCase						(						lowercase					,	lowercase	)					->					List[Any]:
			__lowerCAmelCase											=				state_dict["""optimizer"""]["""target"""]["""Transformer"""][f'encoderblock_{i}']

			__lowerCAmelCase											=				f'encoder.layer.{i}.'
			__lowerCAmelCase											=				{
			    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						(						lowercase	)					->					int:
			return sum([1 if """encoderblock_""" in k else 0 for k in state_dict["""optimizer"""]["""target"""]["""Transformer"""].keys()]	)





def    _lowerCAmelCase						(						lowercase					,	lowercase=False	)					->					Optional[Any]:
			__lowerCAmelCase											=				get_n_layers(lowercase	)

			__lowerCAmelCase											=				OrderedDict()

			__lowerCAmelCase											=				state_dict["""optimizer"""]["""target"""]["""Transformer"""]["""encoder_norm"""]["""bias"""]
			__lowerCAmelCase											=				state_dict["""optimizer"""]["""target"""]["""Transformer"""]["""encoder_norm"""]["""scale"""]

			__lowerCAmelCase											=				np.transpose(
			    state_dict["""optimizer"""]["""target"""]["""embedding"""]["""kernel"""]					,	(4, 3, 0, 1, 2)	)
			__lowerCAmelCase											=				state_dict["""optimizer"""]["""target"""]["""embedding"""]["""bias"""]

			__lowerCAmelCase											=				state_dict["""optimizer"""]["""target"""]["""cls"""]
			__lowerCAmelCase											=				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:
						__lowerCAmelCase											=				{"""vivit.""" + k: v for k, v in new_state.items()}
						__lowerCAmelCase											=				np.transpose(state_dict["""optimizer"""]["""target"""]["""output_projection"""]["""kernel"""]	)
						__lowerCAmelCase											=				np.transpose(state_dict["""optimizer"""]["""target"""]["""output_projection"""]["""bias"""]	)

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





def    _lowerCAmelCase						(						)					->					VivitImageProcessor:
			__lowerCAmelCase											=				VivitImageProcessor()

			assert extractor.do_resize is True
			assert extractor.size == {"shortest_edge": 256}
			assert extractor.do_center_crop is True
			assert extractor.crop_size == {"width": 224, "height": 224}
			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 / 255

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

			return extractor





def    _lowerCAmelCase						(						lowercase	)					->					str:
			__lowerCAmelCase											=				"""checkpoint"""

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

			__lowerCAmelCase											=				restore_checkpoint(lowercase					,	lowercase	)
			__lowerCAmelCase											=				transform_state(lowercase					,	classification_head=lowercase	)

			__lowerCAmelCase											=				get_vivit_config()

			assert config.image_size == 224
			assert config.num_frames == 32

			__lowerCAmelCase											=				VivitForVideoClassification(lowercase	)
			model.load_state_dict(lowercase	)
			model.eval()

			__lowerCAmelCase											=				get_processor()

			__lowerCAmelCase											=				prepare_video()
			__lowerCAmelCase											=				extractor(lowercase					,	return_tensors="""pt"""	)

			__lowerCAmelCase											=				model(**lowercase	)

			__lowerCAmelCase											=				torch.Size([1, 400]	)
			__lowerCAmelCase											=				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]					,	lowercase					,	atol=1e-4	), outputs.logits[0, :5]

			model.save_pretrained(lowercase	)
			extractor.save_pretrained(lowercase	)


if __name__ == "__main__":
	_a					: Union[str, Any]      =    argparse.ArgumentParser()

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

	_a					: Union[str, Any]      =    parser.parse_args()
	convert(args.output_model_name)








'''simple docstring'''







# Lint as: python3

import hashlib
from typing import Union





def    _lowerCAmelCase						(						lowercase	)					->					bytes:
			if isinstance(lowercase					,	lowercase	):
						# Data already in bytes, returns as it as
						return hash_data
			elif isinstance(lowercase					,	lowercase	):
						# We keep the data as it as for it ot be later encoded to UTF-8
						# However replace `\\` with `/` for Windows compatibility
						__lowerCAmelCase											=				hash_data.replace("""\\"""					,	"""/"""	)
			elif isinstance(lowercase					,	lowercase	):
						__lowerCAmelCase											=				str(lowercase	)
			else:
						# If data is not of the required type, raise error
						raise InvalidKeyError(lowercase	)

			return hash_data.encode("""utf-8"""	)





class     _UpperCAmelCase							(						lowerCAmelCase_   ):
		def __init__(  self,__SCREAMING_SNAKE_CASE			):



					'''simple docstring'''

					__lowerCAmelCase											=				"""\nFAILURE TO GENERATE DATASET: Invalid key type detected"""
					__lowerCAmelCase											=				f'\nFound Key {hash_data} of type {type(__SCREAMING_SNAKE_CASE			)}'
					__lowerCAmelCase											=				"""\nKeys should be either str, int or bytes type"""
					super().__init__(f'{self.prefix}{self.err_msg}{self.suffix}'			)





class     _UpperCAmelCase							(						lowerCAmelCase_   ):
		def __init__(  self,__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE=""			):



					'''simple docstring'''

					__lowerCAmelCase											=				key
					__lowerCAmelCase											=				duplicate_key_indices
					__lowerCAmelCase											=				fix_msg
					__lowerCAmelCase											=				"""Found multiple examples generated with the same key"""
					if len(__SCREAMING_SNAKE_CASE			) <= 20:
								__lowerCAmelCase											=				f'\nThe examples at index {", ".join(__SCREAMING_SNAKE_CASE			)} have the key {key}'
					else:
								__lowerCAmelCase											=				f'\nThe examples at index {", ".join(duplicate_key_indices[:20]			)}... ({len(__SCREAMING_SNAKE_CASE			) - 20} more) have the key {key}'
					__lowerCAmelCase											=				"""\n""" + fix_msg if fix_msg else """"""
					super().__init__(f'{self.prefix}{self.err_msg}{self.suffix}'			)





class     _UpperCAmelCase							:
		def __init__(  self,__SCREAMING_SNAKE_CASE			):



					'''simple docstring'''

					__lowerCAmelCase											=				hashlib.mda(_as_bytes(__SCREAMING_SNAKE_CASE			)			)







		def   lowerCamelCase__			(  self,__SCREAMING_SNAKE_CASE			):



					'''simple docstring'''

					__lowerCAmelCase											=				self._split_mda.copy()
					__lowerCAmelCase											=				_as_bytes(__SCREAMING_SNAKE_CASE			)
					mda.update(__SCREAMING_SNAKE_CASE			)
					# Convert to integer with hexadecimal conversion
					return int(mda.hexdigest(),16			)

