
import random
import unittest

import numpy as np

import transformers
from transformers import is_flax_available, is_torch_available
from transformers.testing_utils import is_pt_flax_cross_test, require_flax


if is_flax_available():
					import os

					import jax.numpy as jnp
					from jax import jit

					from transformers import AutoTokenizer, FlaxAutoModelForCausalLM
					from transformers.modeling_flax_pytorch_utils import load_flax_weights_in_pytorch_model

					lowerCamelCase__          =		"""0.12"""  # assumed parallelism: 8


if is_torch_available():
					import torch
def  lowerCAmelCase__			(  SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_=None     )				->		Any:
			if rng is None:
						lowerCAmelCase__      :      Tuple         =		random.Random()

			lowerCAmelCase__      :      Optional[int]         =		1
			for dim in shape:
						total_dims *= dim

			lowerCAmelCase__      :      Union[str, Any]         =		[]
			for _ in range(SCREAMING_SNAKE_CASE_     ):
						values.append(rng.randint(0       ,							vocab_size - 1     )     )

			lowerCAmelCase__      :      str         =		np.array(SCREAMING_SNAKE_CASE_       ,							dtype=jnp.intaa     ).reshape(SCREAMING_SNAKE_CASE_     )

			return output
def  lowerCAmelCase__			(  SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_=None     )				->		Union[str, Any]:
			lowerCAmelCase__      :      Dict         =		ids_tensor(SCREAMING_SNAKE_CASE_       ,							vocab_size=2       ,							rng=SCREAMING_SNAKE_CASE_     )
			# make sure that at least one token is attended to for each batch
			lowerCAmelCase__      :      Dict         =		1
			return attn_mask







@require_flax
class      A__							:
				lowercase			    =  None
				lowercase			    =  ()






				def 							_lowerCamelCase		(						self					:      List[str]   ):






							'''simple docstring'''







							lowerCAmelCase__      ,				lowerCAmelCase__      :      int         =		self.model_tester.prepare_config_and_inputs_for_common()

							# cut to half length & take max batch_size 3
							lowerCAmelCase__      :      Tuple         =		2
							lowerCAmelCase__      :      List[str]         =		inputs['input_ids'].shape[-1] // 2
							lowerCAmelCase__      :      Tuple         =		inputs['input_ids'][:max_batch_size, :sequence_length]

							lowerCAmelCase__      :      Optional[Any]         =		jnp.ones_like(a   )
							lowerCAmelCase__      :      List[str]         =		attention_mask[:max_batch_size, :sequence_length]

							# generate max 5 tokens
							lowerCAmelCase__      :      Any         =		input_ids.shape[-1] + 5
							if config.eos_token_id is not None and config.pad_token_id is None:
										# hack to allow generate for models such as GPT2 as is done in `generate()`
										lowerCAmelCase__      :      List[str]         =		config.eos_token_id
							return config, input_ids, attention_mask, max_length






				@is_pt_flax_cross_test
				def 							_lowerCamelCase		(						self					:      Dict   ):






							'''simple docstring'''







							lowerCAmelCase__      ,				lowerCAmelCase__      ,				lowerCAmelCase__      ,				lowerCAmelCase__      :      int         =		self._get_input_ids_and_config()
							lowerCAmelCase__      :      Optional[int]         =		False
							lowerCAmelCase__      :      Union[str, Any]         =		max_length
							lowerCAmelCase__      :      Tuple         =		0

							for model_class in self.all_generative_model_classes:
										lowerCAmelCase__      :      Tuple         =		model_class(a   )

										lowerCAmelCase__      :      Union[str, Any]         =		model_class.__name__[4:]  # Skip the "Flax" at the beginning
										lowerCAmelCase__      :      List[Any]         =		getattr(a    ,     a   )
										lowerCAmelCase__      :      List[Any]         =		pt_model_class(a   ).eval()
										lowerCAmelCase__      :      Tuple         =		load_flax_weights_in_pytorch_model(a    ,     flax_model.params   )

										lowerCAmelCase__      :      Dict         =		flax_model.generate(a   ).sequences
										lowerCAmelCase__      :      Dict         =		pt_model.generate(torch.tensor(a    ,     dtype=torch.long   )   )

										if flax_generation_outputs.shape[-1] > pt_generation_outputs.shape[-1]:
													lowerCAmelCase__      :      Optional[int]         =		flax_generation_outputs[:, : pt_generation_outputs.shape[-1]]

										self.assertListEqual(pt_generation_outputs.numpy().tolist()    ,     flax_generation_outputs.tolist()   )






				def 							_lowerCamelCase		(						self					:      Optional[int]   ):






							'''simple docstring'''







							lowerCAmelCase__      ,				lowerCAmelCase__      ,				lowerCAmelCase__      ,				lowerCAmelCase__      :      Tuple         =		self._get_input_ids_and_config()
							lowerCAmelCase__      :      Any         =		False
							lowerCAmelCase__      :      int         =		max_length

							for model_class in self.all_generative_model_classes:
										lowerCAmelCase__      :      Union[str, Any]         =		model_class(a   )

										lowerCAmelCase__      :      Any         =		model.generate(a   ).sequences
										self.assertEqual(generation_outputs.shape[-1]    ,     a   )

										lowerCAmelCase__      :      List[Any]         =		jit(model.generate   )
										lowerCAmelCase__      :      Any         =		jit_generate(a   ).sequences

										self.assertListEqual(generation_outputs.tolist()    ,     jit_generation_outputs.tolist()   )






				def 							_lowerCamelCase		(						self					:      Any   ):






							'''simple docstring'''







							lowerCAmelCase__      ,				lowerCAmelCase__      ,				lowerCAmelCase__      ,				lowerCAmelCase__      :      Optional[Any]         =		self._get_input_ids_and_config()
							lowerCAmelCase__      :      List[str]         =		True
							lowerCAmelCase__      :      str         =		max_length

							for model_class in self.all_generative_model_classes:
										lowerCAmelCase__      :      str         =		model_class(a   )

										lowerCAmelCase__      :      Union[str, Any]         =		model.generate(a   ).sequences
										self.assertEqual(generation_outputs.shape[-1]    ,     a   )

										lowerCAmelCase__      :      int         =		jit(model.generate   )
										lowerCAmelCase__      :      Dict         =		jit_generate(a   ).sequences

										self.assertListEqual(generation_outputs.tolist()    ,     jit_generation_outputs.tolist()   )






				def 							_lowerCamelCase		(						self					:      Optional[Any]   ):






							'''simple docstring'''







							lowerCAmelCase__      ,				lowerCAmelCase__      ,				lowerCAmelCase__      ,				lowerCAmelCase__      :      Optional[Any]         =		self._get_input_ids_and_config()
							lowerCAmelCase__      :      List[Any]         =		False
							lowerCAmelCase__      :      List[str]         =		max_length
							lowerCAmelCase__      :      str         =		2

							for model_class in self.all_generative_model_classes:
										lowerCAmelCase__      :      Tuple         =		model_class(a   )

										lowerCAmelCase__      :      int         =		model.generate(a   ).sequences
										self.assertEqual(generation_outputs.shape[-1]    ,     a   )

										lowerCAmelCase__      :      str         =		jit(model.generate   )
										lowerCAmelCase__      :      Tuple         =		jit_generate(a   ).sequences

										self.assertListEqual(generation_outputs.tolist()    ,     jit_generation_outputs.tolist()   )






				def 							_lowerCamelCase		(						self					:      List[str]   ):






							'''simple docstring'''







							lowerCAmelCase__      ,				lowerCAmelCase__      ,				lowerCAmelCase__      ,				lowerCAmelCase__      :      Any         =		self._get_input_ids_and_config()
							lowerCAmelCase__      :      Union[str, Any]         =		False
							lowerCAmelCase__      :      Optional[Any]         =		max_length
							lowerCAmelCase__      :      List[str]         =		2
							lowerCAmelCase__      :      List[Any]         =		2

							for model_class in self.all_generative_model_classes:
										lowerCAmelCase__      :      Union[str, Any]         =		model_class(a   )

										lowerCAmelCase__      :      Any         =		model.generate(a   ).sequences
										self.assertEqual(generation_outputs.shape[0]    ,     input_ids.shape[0] * config.num_return_sequences   )






				def 							_lowerCamelCase		(						self					:      Dict   ):






							'''simple docstring'''







							lowerCAmelCase__      ,				lowerCAmelCase__      ,				lowerCAmelCase__      ,				lowerCAmelCase__      :      Optional[Any]         =		self._get_input_ids_and_config()
							lowerCAmelCase__      :      List[Any]         =		True
							lowerCAmelCase__      :      Optional[Any]         =		max_length
							lowerCAmelCase__      :      int         =		0.8
							lowerCAmelCase__      :      Optional[Any]         =		10
							lowerCAmelCase__      :      Optional[Any]         =		0.3
							lowerCAmelCase__      :      Optional[Any]         =		1
							lowerCAmelCase__      :      Union[str, Any]         =		8
							lowerCAmelCase__      :      Any         =		9

							for model_class in self.all_generative_model_classes:
										lowerCAmelCase__      :      List[str]         =		model_class(a   )

										lowerCAmelCase__      :      int         =		model.generate(a   ).sequences
										self.assertEqual(generation_outputs.shape[-1]    ,     a   )

										lowerCAmelCase__      :      List[Any]         =		jit(model.generate   )
										lowerCAmelCase__      :      str         =		jit_generate(a   ).sequences

										self.assertListEqual(generation_outputs.tolist()    ,     jit_generation_outputs.tolist()   )






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






							'''simple docstring'''







							lowerCAmelCase__      ,				lowerCAmelCase__      ,				lowerCAmelCase__      ,				lowerCAmelCase__      :      str         =		self._get_input_ids_and_config()
							lowerCAmelCase__      :      Dict         =		max_length
							lowerCAmelCase__      :      int         =		1
							lowerCAmelCase__      :      int         =		8
							lowerCAmelCase__      :      List[str]         =		9

							for model_class in self.all_generative_model_classes:
										lowerCAmelCase__      :      Optional[Any]         =		model_class(a   )

										lowerCAmelCase__      :      Tuple         =		model.generate(a   ).sequences
										self.assertEqual(generation_outputs.shape[-1]    ,     a   )

										lowerCAmelCase__      :      Optional[Any]         =		jit(model.generate   )
										lowerCAmelCase__      :      Tuple         =		jit_generate(a   ).sequences

										self.assertListEqual(generation_outputs.tolist()    ,     jit_generation_outputs.tolist()   )






				def 							_lowerCamelCase		(						self					:      List[Any]   ):






							'''simple docstring'''







							lowerCAmelCase__      ,				lowerCAmelCase__      ,				lowerCAmelCase__      ,				lowerCAmelCase__      :      Optional[Any]         =		self._get_input_ids_and_config()
							lowerCAmelCase__      :      Union[str, Any]         =		max_length
							lowerCAmelCase__      :      int         =		2
							lowerCAmelCase__      :      Tuple         =		1
							lowerCAmelCase__      :      Optional[int]         =		8
							lowerCAmelCase__      :      Optional[int]         =		9

							for model_class in self.all_generative_model_classes:
										lowerCAmelCase__      :      str         =		model_class(a   )

										lowerCAmelCase__      :      int         =		model.generate(a   ).sequences
										self.assertEqual(generation_outputs.shape[-1]    ,     a   )

										lowerCAmelCase__      :      List[Any]         =		jit(model.generate   )
										lowerCAmelCase__      :      Optional[Any]         =		jit_generate(a   ).sequences

										self.assertListEqual(generation_outputs.tolist()    ,     jit_generation_outputs.tolist()   )






				def 							_lowerCamelCase		(						self					:      int   ):






							'''simple docstring'''







							lowerCAmelCase__      ,				lowerCAmelCase__      ,				lowerCAmelCase__      ,				lowerCAmelCase__      :      str         =		self._get_input_ids_and_config()

							# pad attention mask on the left
							lowerCAmelCase__      :      str         =		attention_mask.at[(0, 0)].set(0   )

							lowerCAmelCase__      :      str         =		False
							lowerCAmelCase__      :      Union[str, Any]         =		max_length

							for model_class in self.all_generative_model_classes:
										lowerCAmelCase__      :      int         =		model_class(a   )

										lowerCAmelCase__      :      Dict         =		model.generate(a    ,     attention_mask=a   ).sequences
										self.assertEqual(generation_outputs.shape[-1]    ,     a   )

										lowerCAmelCase__      :      Optional[int]         =		jit(model.generate   )
										lowerCAmelCase__      :      List[Any]         =		jit_generate(a    ,     attention_mask=a   ).sequences

										self.assertListEqual(generation_outputs.tolist()    ,     jit_generation_outputs.tolist()   )






				def 							_lowerCamelCase		(						self					:      Tuple   ):






							'''simple docstring'''







							lowerCAmelCase__      ,				lowerCAmelCase__      ,				lowerCAmelCase__      ,				lowerCAmelCase__      :      List[Any]         =		self._get_input_ids_and_config()

							# pad attention mask on the left
							lowerCAmelCase__      :      List[Any]         =		attention_mask.at[(0, 0)].set(0   )

							lowerCAmelCase__      :      str         =		True
							lowerCAmelCase__      :      Tuple         =		max_length

							for model_class in self.all_generative_model_classes:
										lowerCAmelCase__      :      Tuple         =		model_class(a   )

										lowerCAmelCase__      :      List[Any]         =		model.generate(a    ,     attention_mask=a   ).sequences
										self.assertEqual(generation_outputs.shape[-1]    ,     a   )

										lowerCAmelCase__      :      int         =		jit(model.generate   )
										lowerCAmelCase__      :      str         =		jit_generate(a    ,     attention_mask=a   ).sequences

										self.assertListEqual(generation_outputs.tolist()    ,     jit_generation_outputs.tolist()   )






				def 							_lowerCamelCase		(						self					:      str   ):






							'''simple docstring'''







							lowerCAmelCase__      ,				lowerCAmelCase__      ,				lowerCAmelCase__      ,				lowerCAmelCase__      :      Union[str, Any]         =		self._get_input_ids_and_config()

							# pad attention mask on the left
							lowerCAmelCase__      :      Union[str, Any]         =		attention_mask.at[(0, 0)].set(0   )

							lowerCAmelCase__      :      str         =		2
							lowerCAmelCase__      :      List[str]         =		max_length

							for model_class in self.all_generative_model_classes:
										lowerCAmelCase__      :      Tuple         =		model_class(a   )

										lowerCAmelCase__      :      Optional[Any]         =		model.generate(a    ,     attention_mask=a   ).sequences
										self.assertEqual(generation_outputs.shape[-1]    ,     a   )

										lowerCAmelCase__      :      int         =		jit(model.generate   )
										lowerCAmelCase__      :      List[Any]         =		jit_generate(a    ,     attention_mask=a   ).sequences

										self.assertListEqual(generation_outputs.tolist()    ,     jit_generation_outputs.tolist()   )







@require_flax
class      A__							(     unittest.TestCase	):






				def 							_lowerCamelCase		(						self					:      Any   ):






							'''simple docstring'''







							lowerCAmelCase__      :      List[Any]         =		AutoTokenizer.from_pretrained('hf-internal-testing/tiny-bert'   )
							lowerCAmelCase__      :      Optional[int]         =		FlaxAutoModelForCausalLM.from_pretrained('hf-internal-testing/tiny-bert-flax-only'   )

							lowerCAmelCase__      :      List[Any]         =		'Hello world'
							lowerCAmelCase__      :      Any         =		tokenizer(a    ,     return_tensors='np'   ).input_ids

							# typos are quickly detected (the correct argument is `do_sample`)
							with self.assertRaisesRegex(a    ,     'do_samples'   ):
										model.generate(a    ,     do_samples=a   )

							# arbitrary arguments that will not be used anywhere are also not accepted
							with self.assertRaisesRegex(a    ,     'foo'   ):
										lowerCAmelCase__      :      Optional[int]         =		{'foo': 'bar'}
										model.generate(a    ,     **a   )

import argparse

import gdown
import numpy as np
import torch
from huggingface_hub import hf_hub_download

from transformers import (
    CLIPTokenizer,
    CLIPTokenizerFast,
    VideoMAEImageProcessor,
    XCLIPConfig,
    XCLIPModel,
    XCLIPProcessor,
    XCLIPTextConfig,
    XCLIPVisionConfig,
)
def  lowerCAmelCase__			(  SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_     )				->		int:
			lowerCAmelCase__      :      int         =		XCLIPTextConfig()

			# derive patch size from model name
			lowerCAmelCase__      :      Any         =		model_name.find('patch'     )
			lowerCAmelCase__      :      Any         =		int(model_name[start_idx + len('patch'     ) : start_idx + len('patch'     ) + 2]     )
			lowerCAmelCase__      :      Any         =		XCLIPVisionConfig(patch_size=SCREAMING_SNAKE_CASE_       ,							num_frames=SCREAMING_SNAKE_CASE_     )

			if "large" in model_name:
						lowerCAmelCase__      :      List[str]         =		768
						lowerCAmelCase__      :      Optional[int]         =		3_072
						lowerCAmelCase__      :      Any         =		12

						lowerCAmelCase__      :      Optional[int]         =		1_024
						lowerCAmelCase__      :      List[Any]         =		4_096
						lowerCAmelCase__      :      Optional[Any]         =		16
						lowerCAmelCase__      :      Any         =		24
						lowerCAmelCase__      :      Dict         =		768
						lowerCAmelCase__      :      List[str]         =		3_072

			if model_name == "xclip-large-patch14-16-frames":
						lowerCAmelCase__      :      str         =		336

			lowerCAmelCase__      :      Any         =		XCLIPConfig.from_text_vision_configs(SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_     )

			if "large" in model_name:
						lowerCAmelCase__      :      List[Any]         =		768

			return config
def  lowerCAmelCase__			(  SCREAMING_SNAKE_CASE_     )				->		List[Any]:
			# text encoder
			if name == "token_embedding.weight":
						lowerCAmelCase__      :      Optional[Any]         =		name.replace('token_embedding.weight'       ,							'text_model.embeddings.token_embedding.weight'     )
			if name == "positional_embedding":
						lowerCAmelCase__      :      int         =		name.replace('positional_embedding'       ,							'text_model.embeddings.position_embedding.weight'     )
			if "ln_1" in name:
						lowerCAmelCase__      :      Optional[Any]         =		name.replace('ln_1'       ,							'layer_norm1'     )
			if "ln_2" in name:
						lowerCAmelCase__      :      List[str]         =		name.replace('ln_2'       ,							'layer_norm2'     )
			if "c_fc" in name:
						lowerCAmelCase__      :      Tuple         =		name.replace('c_fc'       ,							'fc1'     )
			if "c_proj" in name:
						lowerCAmelCase__      :      Optional[Any]         =		name.replace('c_proj'       ,							'fc2'     )
			if name.startswith('transformer.resblocks'     ):
						lowerCAmelCase__      :      List[str]         =		name.replace('transformer.resblocks'       ,							'text_model.encoder.layers'     )
			if "attn.out_proj" in name and "message" not in name:
						lowerCAmelCase__      :      Optional[Any]         =		name.replace('attn.out_proj'       ,							'self_attn.out_proj'     )
			if "ln_final" in name:
						lowerCAmelCase__      :      List[str]         =		name.replace('ln_final'       ,							'text_model.final_layer_norm'     )
			# visual encoder
			if name == "visual.class_embedding":
						lowerCAmelCase__      :      Dict         =		name.replace('visual.class_embedding'       ,							'vision_model.embeddings.class_embedding'     )
			if name == "visual.positional_embedding":
						lowerCAmelCase__      :      Any         =		name.replace('visual.positional_embedding'       ,							'vision_model.embeddings.position_embedding.weight'     )
			if name.startswith('visual.transformer.resblocks'     ):
						lowerCAmelCase__      :      Union[str, Any]         =		name.replace('visual.transformer.resblocks'       ,							'vision_model.encoder.layers'     )
			if "visual.conv1" in name:
						lowerCAmelCase__      :      Dict         =		name.replace('visual.conv1'       ,							'vision_model.embeddings.patch_embedding'     )
			if "visual.ln_pre" in name:
						lowerCAmelCase__      :      Union[str, Any]         =		name.replace('visual.ln_pre'       ,							'vision_model.pre_layernorm'     )
			if "visual.ln_post" in name:
						lowerCAmelCase__      :      Tuple         =		name.replace('visual.ln_post'       ,							'vision_model.post_layernorm'     )
			if "visual.proj" in name:
						lowerCAmelCase__      :      Optional[Any]         =		name.replace('visual.proj'       ,							'visual_projection.weight'     )
			if "text_projection" in name:
						lowerCAmelCase__      :      Any         =		name.replace('text_projection'       ,							'text_projection.weight'     )
			# things on top
			if "prompts_visual_proj" in name:
						lowerCAmelCase__      :      str         =		name.replace('prompts_visual_proj'       ,							'prompts_visual_projection'     )
			if "prompts_visual_ln" in name:
						lowerCAmelCase__      :      Tuple         =		name.replace('prompts_visual_ln'       ,							'prompts_visual_layernorm'     )
			# mit
			if name == "mit.positional_embedding":
						lowerCAmelCase__      :      Dict         =		name.replace('positional'       ,							'position'     )
			if name.startswith('mit.resblocks'     ):
						lowerCAmelCase__      :      List[str]         =		name.replace('mit.resblocks'       ,							'mit.encoder.layers'     )
			# prompts generator
			if name.startswith('prompts_generator.norm'     ):
						lowerCAmelCase__      :      List[Any]         =		name.replace('prompts_generator.norm'       ,							'prompts_generator.layernorm'     )

			return name
def  lowerCAmelCase__			(  SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_     )				->		Union[str, Any]:
			for key in orig_state_dict.copy().keys():
						lowerCAmelCase__      :      Any         =		orig_state_dict.pop(SCREAMING_SNAKE_CASE_     )

						if "attn.in_proj" in key:
									lowerCAmelCase__      :      str         =		key.split('.'     )
									if key.startswith('visual'     ):
												lowerCAmelCase__      :      str         =		key_split[3]
												lowerCAmelCase__      :      Optional[Any]         =		config.vision_config.hidden_size
												if "message_attn" in key:
															if "weight" in key:
																		lowerCAmelCase__      :      Any         =		val[
																		    :dim, :
																		]
																		lowerCAmelCase__      :      Optional[Any]         =		val[
																		    dim : dim * 2, :
																		]
																		lowerCAmelCase__      :      int         =		val[
																		    -dim:, :
																		]
															else:
																		lowerCAmelCase__      :      str         =		val[
																		    :dim
																		]
																		lowerCAmelCase__      :      Union[str, Any]         =		val[
																		    dim : dim * 2
																		]
																		lowerCAmelCase__      :      Any         =		val[
																		    -dim:
																		]
												else:
															if "weight" in key:
																		lowerCAmelCase__      :      str         =		val[
																		    :dim, :
																		]
																		lowerCAmelCase__      :      Tuple         =		val[
																		    dim : dim * 2, :
																		]
																		lowerCAmelCase__      :      int         =		val[
																		    -dim:, :
																		]
															else:
																		lowerCAmelCase__      :      List[Any]         =		val[:dim]
																		lowerCAmelCase__      :      Optional[Any]         =		val[
																		    dim : dim * 2
																		]
																		lowerCAmelCase__      :      int         =		val[-dim:]
									elif key.startswith('mit'     ):
												lowerCAmelCase__      :      Dict         =		key_split[2]
												lowerCAmelCase__      :      str         =		config.vision_config.mit_hidden_size
												if "weight" in key:
															lowerCAmelCase__      :      List[Any]         =		val[:dim, :]
															lowerCAmelCase__      :      List[Any]         =		val[dim : dim * 2, :]
															lowerCAmelCase__      :      Any         =		val[-dim:, :]
												else:
															lowerCAmelCase__      :      int         =		val[:dim]
															lowerCAmelCase__      :      Optional[Any]         =		val[dim : dim * 2]
															lowerCAmelCase__      :      List[str]         =		val[-dim:]
									else:
												lowerCAmelCase__      :      int         =		key_split[2]
												lowerCAmelCase__      :      Optional[Any]         =		config.text_config.hidden_size
												if "weight" in key:
															lowerCAmelCase__      :      int         =		val[:dim, :]
															lowerCAmelCase__      :      List[str]         =		val[
															    dim : dim * 2, :
															]
															lowerCAmelCase__      :      Dict         =		val[-dim:, :]
												else:
															lowerCAmelCase__      :      List[str]         =		val[:dim]
															lowerCAmelCase__      :      int         =		val[
															    dim : dim * 2
															]
															lowerCAmelCase__      :      Optional[int]         =		val[-dim:]
						else:
									lowerCAmelCase__      :      Optional[int]         =		rename_key(SCREAMING_SNAKE_CASE_     )
									if new_key_name in ["visual_projection.weight", "text_projection.weight"]:
												lowerCAmelCase__      :      List[str]         =		val.T
									lowerCAmelCase__      :      List[str]         =		val

			return orig_state_dict
def  lowerCAmelCase__			(  SCREAMING_SNAKE_CASE_     )				->		Dict:
			if num_frames == 8:
						lowerCAmelCase__      :      Tuple         =		'eating_spaghetti_8_frames.npy'
			elif num_frames == 16:
						lowerCAmelCase__      :      Dict         =		'eating_spaghetti.npy'
			elif num_frames == 32:
						lowerCAmelCase__      :      List[Any]         =		'eating_spaghetti_32_frames.npy'
			lowerCAmelCase__      :      Dict         =		hf_hub_download(
			    repo_id='hf-internal-testing/spaghetti-video'       ,							filename=SCREAMING_SNAKE_CASE_       ,							repo_type='dataset'       ,							)
			lowerCAmelCase__      :      Optional[int]         =		np.load(SCREAMING_SNAKE_CASE_     )
			return list(SCREAMING_SNAKE_CASE_     )
def  lowerCAmelCase__			(  SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_=None       ,							SCREAMING_SNAKE_CASE_=False     )				->		str:
			lowerCAmelCase__      :      Optional[int]         =		{
			    # fully supervised kinetics-400 checkpoints
			    'xclip-base-patch32': 'https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_32_8.pth',
			    'xclip-base-patch32-16-frames': (
			        'https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_32_16.pth'
			    ),
			    'xclip-base-patch16': 'https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_16_8.pth',
			    'xclip-base-patch16-16-frames': (
			        'https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_16_16.pth'
			    ),
			    'xclip-large-patch14': 'https://drive.google.com/u/0/uc?id=1NUOImq0o5DlQTST17iIP3vG7DgmHQuCx&amp;export=download&amp;confirm=t&amp;uuid=b26caedc-88e2-473e-830a-9d158b653cdb',
			    'xclip-large-patch14-16-frames': 'https://drive.google.com/u/0/uc?id=1FOYgnJc097OJ4lGwtRCCydQyVPJEOH7d&amp;export=download&amp;confirm=t&amp;uuid=538fa810-e671-4050-b385-9a623f89804f',
			    # fully supervised kinetics-600 checkpoints
			    'xclip-base-patch16-kinetics-600': (
			        'https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k600_16_8.pth'
			    ),
			    'xclip-base-patch16-kinetics-600-16-frames': (
			        'https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k600_16_16.pth'
			    ),
			    'xclip-large-patch14-kinetics-600': 'https://drive.google.com/u/0/uc?id=1FV8C1INuM91sLAN4ImjzePLIlpMSihwV&amp;export=download&amp;confirm=t&amp;uuid=141d4977-4a65-44ae-864f-4b0c19f838be',
			    # few shot
			    'xclip-base-patch16-hmdb-2-shot': (
			        'https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_2.pth'
			    ),
			    'xclip-base-patch16-hmdb-4-shot': (
			        'https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_4.pth'
			    ),
			    'xclip-base-patch16-hmdb-8-shot': (
			        'https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_8.pth'
			    ),
			    'xclip-base-patch16-hmdb-16-shot': (
			        'https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_16.pth'
			    ),
			    'xclip-base-patch16-ucf-2-shot': (
			        'https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_2.pth'
			    ),
			    'xclip-base-patch16-ucf-4-shot': (
			        'https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_4.pth'
			    ),
			    'xclip-base-patch16-ucf-8-shot': (
			        'https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_8.pth'
			    ),
			    'xclip-base-patch16-ucf-16-shot': (
			        'https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_16.pth'
			    ),
			    # zero shot
			    'xclip-base-patch16-zero-shot': 'https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/zero.pth',
			}

			lowerCAmelCase__      :      Dict         =		model_to_url[model_name]
			lowerCAmelCase__      :      List[str]         =		8
			if "16-frames" in model_name:
						lowerCAmelCase__      :      Any         =		16
			elif "shot" in model_name:
						lowerCAmelCase__      :      str         =		32

			lowerCAmelCase__      :      str         =		get_xclip_config(SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_     )
			lowerCAmelCase__      :      Any         =		XCLIPModel(SCREAMING_SNAKE_CASE_     )
			model.eval()

			if "drive" in checkpoint_url:
						lowerCAmelCase__      :      Any         =		'pytorch_model.bin'
						gdown.cached_download(SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_       ,							quiet=SCREAMING_SNAKE_CASE_     )
						lowerCAmelCase__      :      Tuple         =		torch.load(SCREAMING_SNAKE_CASE_       ,							map_location='cpu'     )['model']
			else:
						lowerCAmelCase__      :      Optional[Any]         =		torch.hub.load_state_dict_from_url(SCREAMING_SNAKE_CASE_     )['model']

			lowerCAmelCase__      :      Optional[Any]         =		convert_state_dict(SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_     )

			lowerCAmelCase__      :      Optional[int]         =		XCLIPModel(SCREAMING_SNAKE_CASE_     )
			lowerCAmelCase__      ,				lowerCAmelCase__      :      str         =		model.load_state_dict(SCREAMING_SNAKE_CASE_       ,							strict=SCREAMING_SNAKE_CASE_     )
			assert missing_keys == ["text_model.embeddings.position_ids", "vision_model.embeddings.position_ids"]
			model.eval()

			lowerCAmelCase__      :      Optional[int]         =		336 if model_name == 'xclip-large-patch14-16-frames' else 224
			lowerCAmelCase__      :      List[str]         =		VideoMAEImageProcessor(size=SCREAMING_SNAKE_CASE_     )
			lowerCAmelCase__      :      Tuple         =		CLIPTokenizer.from_pretrained('openai/clip-vit-base-patch32'     )
			lowerCAmelCase__      :      List[Any]         =		CLIPTokenizerFast.from_pretrained('openai/clip-vit-base-patch32'     )
			lowerCAmelCase__      :      Optional[Any]         =		XCLIPProcessor(image_processor=SCREAMING_SNAKE_CASE_       ,							tokenizer=SCREAMING_SNAKE_CASE_     )

			lowerCAmelCase__      :      List[Any]         =		prepare_video(SCREAMING_SNAKE_CASE_     )
			lowerCAmelCase__      :      str         =		processor(
			    text=['playing sports', 'eating spaghetti', 'go shopping']       ,							videos=SCREAMING_SNAKE_CASE_       ,							return_tensors='pt'       ,							padding=SCREAMING_SNAKE_CASE_     )

			print('Shape of pixel values:'       ,							inputs.pixel_values.shape     )

			with torch.no_grad():
						lowerCAmelCase__      :      Union[str, Any]         =		model(**SCREAMING_SNAKE_CASE_     )

			# Verify outputs
			lowerCAmelCase__      :      Optional[Any]         =		outputs.logits_per_video
			lowerCAmelCase__      :      Tuple         =		logits_per_video.softmax(dim=1     )
			print('Probs:'       ,							SCREAMING_SNAKE_CASE_     )
			# kinetics-400
			if model_name == "xclip-base-patch32":
						lowerCAmelCase__      :      str         =		torch.tensor([[0.0019, 0.9951, 0.0030]]     )
			elif model_name == "xclip-base-patch32-16-frames":
						lowerCAmelCase__      :      Dict         =		torch.tensor([[7.0999e-04, 9.9883e-01, 4.5580e-04]]     )
			elif model_name == "xclip-base-patch16":
						lowerCAmelCase__      :      int         =		torch.tensor([[0.0083, 0.9681, 0.0236]]     )
			elif model_name == "xclip-base-patch16-16-frames":
						lowerCAmelCase__      :      Any         =		torch.tensor([[7.6937e-04, 9.9728e-01, 1.9473e-03]]     )
			elif model_name == "xclip-large-patch14":
						lowerCAmelCase__      :      Any         =		torch.tensor([[0.0062, 0.9864, 0.0075]]     )
			elif model_name == "xclip-large-patch14-16-frames":
						lowerCAmelCase__      :      Union[str, Any]         =		torch.tensor([[3.3877e-04, 9.9937e-01, 2.8888e-04]]     )
			# kinetics-600
			elif model_name == "xclip-base-patch16-kinetics-600":
						lowerCAmelCase__      :      Optional[int]         =		torch.tensor([[0.0555, 0.8914, 0.0531]]     )
			elif model_name == "xclip-base-patch16-kinetics-600-16-frames":
						lowerCAmelCase__      :      int         =		torch.tensor([[3.8554e-04, 9.9929e-01, 3.2754e-04]]     )
			elif model_name == "xclip-large-patch14-kinetics-600":
						lowerCAmelCase__      :      Dict         =		torch.tensor([[0.0036, 0.9920, 0.0045]]     )
			# few shot
			elif model_name == "xclip-base-patch16-hmdb-2-shot":
						lowerCAmelCase__      :      Optional[Any]         =		torch.tensor([[7.1890e-06, 9.9994e-01, 5.6559e-05]]     )
			elif model_name == "xclip-base-patch16-hmdb-4-shot":
						lowerCAmelCase__      :      Tuple         =		torch.tensor([[1.0320e-05, 9.9993e-01, 6.2435e-05]]     )
			elif model_name == "xclip-base-patch16-hmdb-8-shot":
						lowerCAmelCase__      :      int         =		torch.tensor([[4.1377e-06, 9.9990e-01, 9.8386e-05]]     )
			elif model_name == "xclip-base-patch16-hmdb-16-shot":
						lowerCAmelCase__      :      Optional[Any]         =		torch.tensor([[4.1347e-05, 9.9962e-01, 3.3411e-04]]     )
			elif model_name == "xclip-base-patch16-ucf-2-shot":
						lowerCAmelCase__      :      str         =		torch.tensor([[8.5857e-05, 9.9928e-01, 6.3291e-04]]     )
			elif model_name == "xclip-base-patch16-ucf-4-shot":
						lowerCAmelCase__      :      Optional[Any]         =		torch.tensor([[8.5857e-05, 9.9928e-01, 6.3291e-04]]     )
			elif model_name == "xclip-base-patch16-ucf-8-shot":
						lowerCAmelCase__      :      int         =		torch.tensor([[0.0027, 0.9904, 0.0070]]     )
			elif model_name == "xclip-base-patch16-ucf-16-shot":
						lowerCAmelCase__      :      Union[str, Any]         =		torch.tensor([[9.8219e-04, 9.9593e-01, 3.0863e-03]]     )
			# zero shot
			elif model_name == "xclip-base-patch16-zero-shot":
						lowerCAmelCase__      :      List[Any]         =		torch.tensor([[3.5082e-04, 9.9785e-01, 1.7966e-03]]     )
			else:
						raise ValueError(F'''Model name {model_name} not supported'''     )
			assert torch.allclose(SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_       ,							atol=1e-3     )
			print('Looks ok!'     )

			if pytorch_dump_folder_path is not None:
						print(F'''Saving model {model_name} to {pytorch_dump_folder_path}'''     )
						model.save_pretrained(SCREAMING_SNAKE_CASE_     )

			if push_to_hub:
						print('Pushing model, processor and slow tokenizer files to the hub...'     )
						model.push_to_hub(SCREAMING_SNAKE_CASE_       ,							organization='nielsr'     )
						processor.push_to_hub(SCREAMING_SNAKE_CASE_       ,							organization='nielsr'     )
						slow_tokenizer.push_to_hub(SCREAMING_SNAKE_CASE_       ,							organization='nielsr'     )


if __name__ == "__main__":
					lowerCamelCase__          =		argparse.ArgumentParser()
					# Required parameters
					parser.add_argument(
					    """--model_name""",
					    default="""xclip-base-patch32""",
					    type=str,
					    help="""Name of the model.""",
					)
					parser.add_argument(
					    """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model directory."""
					)
					parser.add_argument(
					    """--push_to_hub""", action="""store_true""", help="""Whether or not to push the converted model to the 🤗 hub."""
					)

					lowerCamelCase__          =		parser.parse_args()
					convert_xclip_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)