
import shutil
import tempfile
import unittest

from transformers import (
    SPIECE_UNDERLINE,
    AddedToken,
    BatchEncoding,
    NllbTokenizer,
    NllbTokenizerFast,
    is_torch_available,
)
from transformers.testing_utils import (
    get_tests_dir,
    nested_simplify,
    require_sentencepiece,
    require_tokenizers,
    require_torch,
)

from ...test_tokenization_common import TokenizerTesterMixin


lowerCamelCase__          =		get_tests_dir("""fixtures/test_sentencepiece.model""")


if is_torch_available():
					from transformers.models.mam_aaa.modeling_mam_aaa import shift_tokens_right

lowerCamelCase__          =		25_6047
lowerCamelCase__          =		25_6145







@require_sentencepiece
@require_tokenizers
class      A__							(     __magic_name__						,   unittest.TestCase	):
				lowercase			    =  NllbTokenizer
				lowercase			    =  NllbTokenizerFast
				lowercase			    =  True
				lowercase			    =  True
				lowercase			    =  {}






				def 							_lowerCamelCase		(						self					:      Any   ):






							'''simple docstring'''







							super().setUp()

							# We have a SentencePiece fixture for testing
							lowerCAmelCase__      :      str         =		NllbTokenizer(a    ,     keep_accents=a   )
							tokenizer.save_pretrained(self.tmpdirname   )






				def 							_lowerCamelCase		(						self					:      Any   ):






							'''simple docstring'''







							lowerCAmelCase__      :      List[str]         =		NllbTokenizer(a    ,     keep_accents=a   )

							lowerCAmelCase__      :      Dict         =		tokenizer.tokenize('This is a test'   )
							self.assertListEqual(a    ,     ['▁This', '▁is', '▁a', '▁t', 'est']   )

							self.assertListEqual(
							    tokenizer.convert_tokens_to_ids(a   )    ,     [value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]]    ,     )

							lowerCAmelCase__      :      Dict         =		tokenizer.tokenize('I was born in 92000, and this is falsé.'   )
							self.assertListEqual(
							    a    ,     [
							        SPIECE_UNDERLINE + 'I',
							        SPIECE_UNDERLINE + 'was',
							        SPIECE_UNDERLINE + 'b',
							        'or',
							        'n',
							        SPIECE_UNDERLINE + 'in',
							        SPIECE_UNDERLINE + '',
							        '9',
							        '2',
							        '0',
							        '0',
							        '0',
							        ',',
							        SPIECE_UNDERLINE + 'and',
							        SPIECE_UNDERLINE + 'this',
							        SPIECE_UNDERLINE + 'is',
							        SPIECE_UNDERLINE + 'f',
							        'al',
							        's',
							        'é',
							        '.',
							    ]    ,     )
							lowerCAmelCase__      :      List[Any]         =		tokenizer.convert_tokens_to_ids(a   )
							self.assertListEqual(
							    a    ,     [
							        value + tokenizer.fairseq_offset
							        for value in [8, 21, 84, 55, 24, 19, 7, 2, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 2, 4]
							    ]    ,     )

							lowerCAmelCase__      :      Optional[int]         =		tokenizer.convert_ids_to_tokens(a   )
							self.assertListEqual(
							    a    ,     [
							        SPIECE_UNDERLINE + 'I',
							        SPIECE_UNDERLINE + 'was',
							        SPIECE_UNDERLINE + 'b',
							        'or',
							        'n',
							        SPIECE_UNDERLINE + 'in',
							        SPIECE_UNDERLINE + '',
							        '<unk>',
							        '2',
							        '0',
							        '0',
							        '0',
							        ',',
							        SPIECE_UNDERLINE + 'and',
							        SPIECE_UNDERLINE + 'this',
							        SPIECE_UNDERLINE + 'is',
							        SPIECE_UNDERLINE + 'f',
							        'al',
							        's',
							        '<unk>',
							        '.',
							    ]    ,     )






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






							'''simple docstring'''







							lowerCAmelCase__      :      Dict         =		(self.rust_tokenizer_class, 'hf-internal-testing/tiny-random-nllb', {})
							for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
										with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})'''   ):
													lowerCAmelCase__      :      str         =		self.rust_tokenizer_class.from_pretrained(a    ,     **a   )
													lowerCAmelCase__      :      Optional[Any]         =		self.tokenizer_class.from_pretrained(a    ,     **a   )

													lowerCAmelCase__      :      Optional[int]         =		tempfile.mkdtemp()

													lowerCAmelCase__      :      str         =		tokenizer_r.save_pretrained(a   )
													lowerCAmelCase__      :      List[str]         =		tokenizer_p.save_pretrained(a   )

													# Checks it save with the same files + the tokenizer.json file for the fast one
													self.assertTrue(any('tokenizer.json' in f for f in tokenizer_r_files   )   )
													lowerCAmelCase__      :      Union[str, Any]         =		tuple(f for f in tokenizer_r_files if 'tokenizer.json' not in f   )
													self.assertSequenceEqual(a    ,     a   )

													# Checks everything loads correctly in the same way
													lowerCAmelCase__      :      Any         =		tokenizer_r.from_pretrained(a   )
													lowerCAmelCase__      :      Optional[Any]         =		tokenizer_p.from_pretrained(a   )

													# Check special tokens are set accordingly on Rust and Python
													for key in tokenizer_pp.special_tokens_map:
																self.assertTrue(hasattr(a    ,     a   )   )

													shutil.rmtree(a   )

													# Save tokenizer rust, legacy_format=True
													lowerCAmelCase__      :      int         =		tempfile.mkdtemp()

													lowerCAmelCase__      :      Optional[Any]         =		tokenizer_r.save_pretrained(a    ,     legacy_format=a   )
													lowerCAmelCase__      :      List[Any]         =		tokenizer_p.save_pretrained(a   )

													# Checks it save with the same files
													self.assertSequenceEqual(a    ,     a   )

													# Checks everything loads correctly in the same way
													lowerCAmelCase__      :      Any         =		tokenizer_r.from_pretrained(a   )
													lowerCAmelCase__      :      int         =		tokenizer_p.from_pretrained(a   )

													# Check special tokens are set accordingly on Rust and Python
													for key in tokenizer_pp.special_tokens_map:
																self.assertTrue(hasattr(a    ,     a   )   )

													shutil.rmtree(a   )

													# Save tokenizer rust, legacy_format=False
													lowerCAmelCase__      :      Optional[int]         =		tempfile.mkdtemp()

													lowerCAmelCase__      :      int         =		tokenizer_r.save_pretrained(a    ,     legacy_format=a   )
													lowerCAmelCase__      :      Optional[Any]         =		tokenizer_p.save_pretrained(a   )

													# Checks it saved the tokenizer.json file
													self.assertTrue(any('tokenizer.json' in f for f in tokenizer_r_files   )   )

													# Checks everything loads correctly in the same way
													lowerCAmelCase__      :      str         =		tokenizer_r.from_pretrained(a   )
													lowerCAmelCase__      :      Dict         =		tokenizer_p.from_pretrained(a   )

													# Check special tokens are set accordingly on Rust and Python
													for key in tokenizer_pp.special_tokens_map:
																self.assertTrue(hasattr(a    ,     a   )   )

													shutil.rmtree(a   )






				@require_torch
				def 							_lowerCamelCase		(						self					:      str   ):






							'''simple docstring'''







							if not self.test_seqaseq:
										return

							lowerCAmelCase__      :      Union[str, Any]         =		self.get_tokenizers()
							for tokenizer in tokenizers:
										with self.subTest(f'''{tokenizer.__class__.__name__}'''   ):
													# Longer text that will definitely require truncation.
													lowerCAmelCase__      :      Any         =		[
													    ' UN Chief Says There Is No Military Solution in Syria',
													    ' Secretary-General Ban Ki-moon says his response to Russia\'s stepped up military support for'
													    ' Syria is that \'there is no military solution\' to the nearly five-year conflict and more weapons'
													    ' will only worsen the violence and misery for millions of people.',
													]
													lowerCAmelCase__      :      Any         =		[
													    'Şeful ONU declară că nu există o soluţie militară în Siria',
													    'Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al'
													    ' Rusiei pentru Siria este că "nu există o soluţie militară" la conflictul de aproape cinci ani şi'
													    ' că noi arme nu vor face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.',
													]
													try:
																lowerCAmelCase__      :      Optional[Any]         =		tokenizer.prepare_seqaseq_batch(
																    src_texts=a    ,     tgt_texts=a    ,     max_length=3    ,     max_target_length=10    ,     return_tensors='pt'    ,     src_lang='eng_Latn'    ,     tgt_lang='ron_Latn'    ,     )
													except NotImplementedError:
																return
													self.assertEqual(batch.input_ids.shape[1]    ,     3   )
													self.assertEqual(batch.labels.shape[1]    ,     10   )
													# max_target_length will default to max_length if not specified
													lowerCAmelCase__      :      Any         =		tokenizer.prepare_seqaseq_batch(
													    a    ,     tgt_texts=a    ,     max_length=3    ,     return_tensors='pt'   )
													self.assertEqual(batch.input_ids.shape[1]    ,     3   )
													self.assertEqual(batch.labels.shape[1]    ,     3   )

													lowerCAmelCase__      :      List[Any]         =		tokenizer.prepare_seqaseq_batch(
													    src_texts=a    ,     max_length=3    ,     max_target_length=10    ,     return_tensors='pt'   )
													self.assertEqual(batch_encoder_only.input_ids.shape[1]    ,     3   )
													self.assertEqual(batch_encoder_only.attention_mask.shape[1]    ,     3   )
													self.assertNotIn('decoder_input_ids'    ,     a   )






				@unittest.skip('Unfortunately way too slow to build a BPE with SentencePiece.'   )
				def 							_lowerCamelCase		(						self					:      Dict   ):






							'''simple docstring'''







							pass






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






							'''simple docstring'''







							for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
										with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})'''   ):
													lowerCAmelCase__      :      List[Any]         =		[AddedToken('<special>'    ,     lstrip=a   )]

													lowerCAmelCase__      :      Dict         =		self.rust_tokenizer_class.from_pretrained(
													    a    ,     additional_special_tokens=a    ,     **a   )
													lowerCAmelCase__      :      Tuple         =		tokenizer_r.encode('Hey this is a <special> token'   )

													lowerCAmelCase__      :      Any         =		tokenizer_r.encode('<special>'    ,     add_special_tokens=a   )[0]

													self.assertTrue(special_token_id in r_output   )

													if self.test_slow_tokenizer:
																lowerCAmelCase__      :      Any         =		self.rust_tokenizer_class.from_pretrained(
																    a    ,     additional_special_tokens=a    ,     **a    ,     )
																lowerCAmelCase__      :      str         =		self.tokenizer_class.from_pretrained(
																    a    ,     additional_special_tokens=a    ,     **a   )

																lowerCAmelCase__      :      int         =		tokenizer_p.encode('Hey this is a <special> token'   )

																lowerCAmelCase__      :      int         =		tokenizer_cr.encode('Hey this is a <special> token'   )

																self.assertEqual(a    ,     a   )
																self.assertEqual(a    ,     a   )
																self.assertTrue(special_token_id in p_output   )
																self.assertTrue(special_token_id in cr_output   )







@require_torch
@require_sentencepiece
@require_tokenizers
class      A__							(     unittest.TestCase	):
				lowercase			    =  'facebook/nllb-200-distilled-600M'
				lowercase			    =  [
				    ' UN Chief Says There Is No Military Solution in Syria',
				    ' Secretary-General Ban Ki-moon says his response to Russia\'s stepped up military support for Syria is that "there is no military solution" to the nearly five-year conflict and more weapons will only worsen the violence and misery for millions of people.',
				]
				lowercase			    =  [
				    'Şeful ONU declară că nu există o soluţie militară în Siria',
				    'Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al Rusiei'
				    ' pentru Siria este că "nu există o soluţie militară" la conflictul de aproape cinci ani şi că noi arme nu vor'
				    ' face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.',
				]
				lowercase			    =  [
				    256047,
				    16297,
				    134408,
				    8165,
				    248066,
				    14734,
				    950,
				    1135,
				    105721,
				    3573,
				    83,
				    27352,
				    108,
				    49486,
				    2,
				]






				@classmethod
				def 							_lowerCamelCase		(						cls					:      List[str]   ):






							'''simple docstring'''







							lowerCAmelCase__      :      NllbTokenizer         =		NllbTokenizer.from_pretrained(
							    cls.checkpoint_name    ,     src_lang='eng_Latn'    ,     tgt_lang='ron_Latn'   )
							lowerCAmelCase__      :      int         =		1
							return cls






				def 							_lowerCamelCase		(						self					:      Dict   ):






							'''simple docstring'''







							self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['ace_Arab']    ,     256_001   )
							self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['ace_Latn']    ,     256_002   )
							self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['fra_Latn']    ,     256_057   )






				def 							_lowerCamelCase		(						self					:      Optional[Any]   ):






							'''simple docstring'''







							lowerCAmelCase__      :      Tuple         =		self.tokenizer.batch_encode_plus(self.src_text   ).input_ids[0]
							self.assertListEqual(self.expected_src_tokens    ,     a   )






				def 							_lowerCamelCase		(						self					:      List[Any]   ):






							'''simple docstring'''







							self.assertIn(a    ,     self.tokenizer.all_special_ids   )
							# fmt: off
							lowerCAmelCase__      :      List[Any]         =		[RO_CODE, 4_254, 98_068, 112_923, 39_072, 3_909, 713, 102_767, 26, 17_314, 35_642, 14_683, 33_118, 2_022, 66_987, 2, 256_047]
							# fmt: on

							lowerCAmelCase__      :      Optional[Any]         =		self.tokenizer.decode(a    ,     skip_special_tokens=a   )
							lowerCAmelCase__      :      Union[str, Any]         =		self.tokenizer.decode(generated_ids[1:]    ,     skip_special_tokens=a   )
							self.assertEqual(a    ,     a   )
							self.assertNotIn(self.tokenizer.eos_token    ,     a   )






				def 							_lowerCamelCase		(						self					:      Optional[int]   ):






							'''simple docstring'''







							lowerCAmelCase__      :      Union[str, Any]         =		['this is gunna be a long sentence ' * 20]
							assert isinstance(src_text[0]    ,     a   )
							lowerCAmelCase__      :      Optional[int]         =		10
							lowerCAmelCase__      :      Dict         =		self.tokenizer(a    ,     max_length=a    ,     truncation=a   ).input_ids[0]
							self.assertEqual(ids[-1]    ,     2   )
							self.assertEqual(ids[0]    ,     a   )
							self.assertEqual(len(a   )    ,     a   )






				def 							_lowerCamelCase		(						self					:      Dict   ):






							'''simple docstring'''







							self.assertListEqual(self.tokenizer.convert_tokens_to_ids(['<mask>', 'ar_AR']   )    ,     [256_203, 3]   )






				def 							_lowerCamelCase		(						self					:      List[str]   ):






							'''simple docstring'''







							lowerCAmelCase__      :      Dict         =		tempfile.mkdtemp()
							lowerCAmelCase__      :      Union[str, Any]         =		self.tokenizer.fairseq_tokens_to_ids
							self.tokenizer.save_pretrained(a   )
							lowerCAmelCase__      :      List[Any]         =		NllbTokenizer.from_pretrained(a   )
							self.assertDictEqual(new_tok.fairseq_tokens_to_ids    ,     a   )






				@require_torch
				def 							_lowerCamelCase		(						self					:      List[str]   ):






							'''simple docstring'''







							lowerCAmelCase__      :      Union[str, Any]         =		self.tokenizer(
							    self.src_text    ,     text_target=self.tgt_text    ,     padding=a    ,     truncation=a    ,     max_length=len(self.expected_src_tokens   )    ,     return_tensors='pt'    ,     )
							lowerCAmelCase__      :      Optional[Any]         =		shift_tokens_right(
							    batch['labels']    ,     self.tokenizer.pad_token_id    ,     self.tokenizer.lang_code_to_id['ron_Latn']   )

							self.assertIsInstance(a    ,     a   )

							self.assertEqual((2, 15)    ,     batch.input_ids.shape   )
							self.assertEqual((2, 15)    ,     batch.attention_mask.shape   )
							lowerCAmelCase__      :      List[str]         =		batch.input_ids.tolist()[0]
							self.assertListEqual(self.expected_src_tokens    ,     a   )
							self.assertEqual(a    ,     batch.decoder_input_ids[0, 0]   )  # EOS
							# Test that special tokens are reset
							self.assertEqual(self.tokenizer.prefix_tokens    ,     [EN_CODE]   )
							self.assertEqual(self.tokenizer.suffix_tokens    ,     [self.tokenizer.eos_token_id]   )






				def 							_lowerCamelCase		(						self					:      Optional[int]   ):






							'''simple docstring'''







							lowerCAmelCase__      :      List[str]         =		self.tokenizer(self.src_text    ,     padding=a    ,     truncation=a    ,     max_length=3    ,     return_tensors='pt'   )
							lowerCAmelCase__      :      Any         =		self.tokenizer(
							    text_target=self.tgt_text    ,     padding=a    ,     truncation=a    ,     max_length=10    ,     return_tensors='pt'   )
							lowerCAmelCase__      :      List[Any]         =		targets['input_ids']
							lowerCAmelCase__      :      str         =		shift_tokens_right(
							    a    ,     self.tokenizer.pad_token_id    ,     decoder_start_token_id=self.tokenizer.lang_code_to_id[self.tokenizer.tgt_lang]    ,     )

							self.assertEqual(batch.input_ids.shape[1]    ,     3   )
							self.assertEqual(batch.decoder_input_ids.shape[1]    ,     10   )






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






							'''simple docstring'''







							lowerCAmelCase__      :      List[Any]         =		self.tokenizer._build_translation_inputs(
							    'A test'    ,     return_tensors='pt'    ,     src_lang='eng_Latn'    ,     tgt_lang='fra_Latn'   )

							self.assertEqual(
							    nested_simplify(a   )    ,     {
							        # A, test, EOS, en_XX
							        'input_ids': [[256_047, 70, 7_356, 2]],
							        'attention_mask': [[1, 1, 1, 1]],
							        # ar_AR
							        'forced_bos_token_id': 256_057,
							    }    ,     )






				@require_torch
				def 							_lowerCamelCase		(						self					:      Optional[int]   ):






							'''simple docstring'''







							lowerCAmelCase__      :      Optional[Any]         =		True
							lowerCAmelCase__      :      Optional[Any]         =		self.tokenizer(
							    'UN Chief says there is no military solution in Syria'    ,     src_lang='eng_Latn'    ,     tgt_lang='fra_Latn'   )
							self.assertEqual(
							    inputs.input_ids    ,     [16_297, 134_408, 25_653, 6_370, 248, 254, 103_929, 94_995, 108, 49_486, 2, 256_047]   )

							lowerCAmelCase__      :      str         =		False
							lowerCAmelCase__      :      Any         =		self.tokenizer(
							    'UN Chief says there is no military solution in Syria'    ,     src_lang='eng_Latn'    ,     tgt_lang='fra_Latn'   )
							self.assertEqual(
							    inputs.input_ids    ,     [256_047, 16_297, 134_408, 25_653, 6_370, 248, 254, 103_929, 94_995, 108, 49_486, 2]   )

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)