

"""simple docstring"""






import tempfile
import unittest
from pathlib import Path
from shutil import copyfile

from transformers import BatchEncoding, MarianTokenizer
from transformers.testing_utils import get_tests_dir, require_sentencepiece, slow
from transformers.utils import is_sentencepiece_available, is_tf_available, is_torch_available


if is_sentencepiece_available():
 from transformers.models.marian.tokenization_marian import VOCAB_FILES_NAMES, save_json

from ...test_tokenization_common import TokenizerTesterMixin


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

__lowerCamelCase										=		{"target_lang": "fi", "source_lang": "en"}
__lowerCamelCase										=		">>zh<<"
__lowerCamelCase										=		"Helsinki-NLP/"

if is_torch_available():
 __lowerCamelCase										=		"pt"
elif is_tf_available():
 __lowerCamelCase										=		"tf"
else:
 __lowerCamelCase										=		"jax"
@require_sentencepiece
class        _lowercase					(     __UpperCAmelCase   , unittest.TestCase     ):
     _lowerCamelCase     =       MarianTokenizer
     _lowerCamelCase     =       False
     _lowerCamelCase     =       True







     def 	lowerCAmelCase__    (      self       ):
      super().setUp()
      __magic_name__     =      ['''</s>''', '''<unk>''', '''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est''', '''\u0120''', '''<pad>''']
      __magic_name__     =      dict(zip(UpperCamelCase_ ,						range(len(UpperCamelCase_       )       )       )       )
      __magic_name__     =      Path(self.tmpdirname       )
      save_json(UpperCamelCase_ ,						save_dir / VOCAB_FILES_NAMES['''vocab''']       )
      save_json(UpperCamelCase_ ,						save_dir / VOCAB_FILES_NAMES['''tokenizer_config_file''']       )
      if not (save_dir / VOCAB_FILES_NAMES["source_spm"]).exists():
       copyfile(UpperCamelCase_ ,						save_dir / VOCAB_FILES_NAMES['''source_spm''']       )
       copyfile(UpperCamelCase_ ,						save_dir / VOCAB_FILES_NAMES['''target_spm''']       )

      __magic_name__     =      MarianTokenizer.from_pretrained(self.tmpdirname       )
      tokenizer.save_pretrained(self.tmpdirname       )







     def 	lowerCAmelCase__    (      self ,						**UpperCamelCase_       ):
      return MarianTokenizer.from_pretrained(self.tmpdirname ,						**UpperCamelCase_       )







     def 	lowerCAmelCase__    (      self ,						UpperCamelCase_       ):
      return (
          "This is a test",
          "This is a test",
      )







     def 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      '''</s>'''
      __magic_name__     =      0

      self.assertEqual(self.get_tokenizer()._convert_token_to_id(UpperCamelCase_       ) ,						UpperCamelCase_       )
      self.assertEqual(self.get_tokenizer()._convert_id_to_token(UpperCamelCase_       ) ,						UpperCamelCase_       )







     def 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      list(self.get_tokenizer().get_vocab().keys()       )

      self.assertEqual(vocab_keys[0] ,						'''</s>'''       )
      self.assertEqual(vocab_keys[1] ,						'''<unk>'''       )
      self.assertEqual(vocab_keys[-1] ,						'''<pad>'''       )
      self.assertEqual(len(UpperCamelCase_       ) ,						9       )







     def 	lowerCAmelCase__    (      self       ):
      self.assertEqual(self.get_tokenizer().vocab_size ,						9       )







     def 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      MarianTokenizer.from_pretrained(f'''{ORG_NAME}opus-mt-en-de'''       )
      __magic_name__     =      en_de_tokenizer(['''I am a small frog'''] ,						return_tensors=UpperCamelCase_       )
      self.assertIsInstance(UpperCamelCase_ ,						UpperCamelCase_       )
      __magic_name__     =      [38, 121, 14, 697, 3_8848, 0]
      self.assertListEqual(UpperCamelCase_ ,						batch.input_ids[0]       )

      __magic_name__     =      tempfile.mkdtemp()
      en_de_tokenizer.save_pretrained(UpperCamelCase_       )
      __magic_name__     =      [x.name for x in Path(UpperCamelCase_       ).glob('''*'''       )]
      self.assertIn('''source.spm''' ,						UpperCamelCase_       )
      MarianTokenizer.from_pretrained(UpperCamelCase_       )







     def 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      self.get_tokenizer()

      __magic_name__     =      tok(
          ['''I am a small frog''' * 1000, '''I am a small frog'''] ,						padding=UpperCamelCase_ ,						truncation=UpperCamelCase_ ,						return_tensors=UpperCamelCase_       )
      self.assertIsInstance(UpperCamelCase_ ,						UpperCamelCase_       )
      self.assertEqual(batch.input_ids.shape ,						(2, 512)       )







     def 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      self.get_tokenizer()
      __magic_name__     =      tok(['''I am a tiny frog''', '''I am a small frog'''] ,						padding=UpperCamelCase_ ,						return_tensors=UpperCamelCase_       )
      self.assertIsInstance(UpperCamelCase_ ,						UpperCamelCase_       )
      self.assertEqual(batch_smaller.input_ids.shape ,						(2, 10)       )







     @slow
     def 	lowerCAmelCase__    (      self       ):
      # fmt: off
      __magic_name__     =      {'''input_ids''': [[4_3495, 462, 20, 4_2164, 1369, 52, 464, 132, 1703, 492, 13, 7491, 3_8999, 6, 8, 464, 132, 1703, 492, 13, 4669, 3_7867, 13, 7525, 27, 1593, 988, 13, 3_3972, 7029, 6, 20, 8251, 383, 2, 270, 5866, 3788, 2, 2353, 8251, 1_2338, 2, 1_3958, 387, 2, 3629, 6953, 188, 2900, 2, 1_3958, 8011, 1_1501, 23, 8460, 4073, 3_4009, 20, 435, 1_1439, 27, 8, 8460, 4073, 6004, 20, 9988, 375, 27, 33, 266, 1945, 1076, 1350, 3_7867, 3288, 5, 577, 1076, 4374, 8, 5082, 5, 2_6453, 257, 556, 403, 2, 242, 132, 383, 316, 492, 8, 1_0767, 6, 316, 304, 4239, 3, 0], [148, 1_5722, 19, 1839, 12, 1350, 13, 2_2327, 5082, 5418, 4_7567, 3_5938, 59, 318, 1_9552, 108, 2183, 54, 1_4976, 4835, 32, 547, 1114, 8, 315, 2417, 5, 92, 1_9088, 3, 0, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100], [36, 6395, 1_2570, 3_9147, 1_1597, 6, 266, 4, 4_5405, 7296, 3, 0, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100]], '''attention_mask''': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]}  # noqa: E501
      # fmt: on

      self.tokenizer_integration_test_util(
          expected_encoding=UpperCamelCase_ ,						model_name='''Helsinki-NLP/opus-mt-en-de''' ,						revision='''1a8c2263da11e68e50938f97e10cd57820bd504c''' ,						decode_kwargs={'''use_source_tokenizer''': True} ,						)







     def 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      MarianTokenizer.from_pretrained('''hf-internal-testing/test-marian-two-vocabs'''       )

      __magic_name__     =      '''Tämä on testi'''
      __magic_name__     =      '''This is a test'''

      __magic_name__     =      [76, 7, 2047, 2]
      __magic_name__     =      [69, 12, 11, 940, 2]

      __magic_name__     =      tokenizer(UpperCamelCase_       ).input_ids
      self.assertListEqual(UpperCamelCase_ ,						UpperCamelCase_       )

      __magic_name__     =      tokenizer(text_target=UpperCamelCase_       ).input_ids
      self.assertListEqual(UpperCamelCase_ ,						UpperCamelCase_       )

      __magic_name__     =      tokenizer.decode(UpperCamelCase_ ,						skip_special_tokens=UpperCamelCase_       )
      self.assertEqual(UpperCamelCase_ ,						UpperCamelCase_       )




"""simple docstring"""






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 	lowercase      (   __UpperCamelCase      ,   __UpperCamelCase						) ->      List[str]:
 __magic_name__     =      XCLIPTextConfig()

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

 if "large" in model_name:
  __magic_name__     =      768
  __magic_name__     =      3072
  __magic_name__     =      12

  __magic_name__     =      1024
  __magic_name__     =      4096
  __magic_name__     =      16
  __magic_name__     =      24
  __magic_name__     =      768
  __magic_name__     =      3072

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

 __magic_name__     =      XCLIPConfig.from_text_vision_configs(__UpperCamelCase      ,   __UpperCamelCase						)

 if "large" in model_name:
  __magic_name__     =      768

 return config





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

 return name





def 	lowercase      (   __UpperCamelCase      ,   __UpperCamelCase						) ->      str:
 for key in orig_state_dict.copy().keys():
  __magic_name__     =      orig_state_dict.pop(__UpperCamelCase						)

  if "attn.in_proj" in key:
   __magic_name__     =      key.split('''.'''						)
   if key.startswith('''visual'''						):
    __magic_name__     =      key_split[3]
    __magic_name__     =      config.vision_config.hidden_size
    if "message_attn" in key:
     if "weight" in key:
      __magic_name__     =      val[
          :dim, :
      ]
      __magic_name__     =      val[
          dim : dim * 2, :
      ]
      __magic_name__     =      val[
          -dim:, :
      ]
     else:
      __magic_name__     =      val[
          :dim
      ]
      __magic_name__     =      val[
          dim : dim * 2
      ]
      __magic_name__     =      val[
          -dim:
      ]
    else:
     if "weight" in key:
      __magic_name__     =      val[
          :dim, :
      ]
      __magic_name__     =      val[
          dim : dim * 2, :
      ]
      __magic_name__     =      val[
          -dim:, :
      ]
     else:
      __magic_name__     =      val[:dim]
      __magic_name__     =      val[
          dim : dim * 2
      ]
      __magic_name__     =      val[-dim:]
   elif key.startswith('''mit'''						):
    __magic_name__     =      key_split[2]
    __magic_name__     =      config.vision_config.mit_hidden_size
    if "weight" in key:
     __magic_name__     =      val[:dim, :]
     __magic_name__     =      val[dim : dim * 2, :]
     __magic_name__     =      val[-dim:, :]
    else:
     __magic_name__     =      val[:dim]
     __magic_name__     =      val[dim : dim * 2]
     __magic_name__     =      val[-dim:]
   else:
    __magic_name__     =      key_split[2]
    __magic_name__     =      config.text_config.hidden_size
    if "weight" in key:
     __magic_name__     =      val[:dim, :]
     __magic_name__     =      val[
         dim : dim * 2, :
     ]
     __magic_name__     =      val[-dim:, :]
    else:
     __magic_name__     =      val[:dim]
     __magic_name__     =      val[
         dim : dim * 2
     ]
     __magic_name__     =      val[-dim:]
  else:
   __magic_name__     =      rename_key(__UpperCamelCase						)
   if new_key_name in ["visual_projection.weight", "text_projection.weight"]:
    __magic_name__     =      val.T
   __magic_name__     =      val

 return orig_state_dict





def 	lowercase      (   __UpperCamelCase						) ->      Any:
 if num_frames == 8:
  __magic_name__     =      '''eating_spaghetti_8_frames.npy'''
 elif num_frames == 16:
  __magic_name__     =      '''eating_spaghetti.npy'''
 elif num_frames == 32:
  __magic_name__     =      '''eating_spaghetti_32_frames.npy'''
 __magic_name__     =      hf_hub_download(
     repo_id='''hf-internal-testing/spaghetti-video'''      ,   filename=__UpperCamelCase      ,   repo_type='''dataset'''      ,   )
 __magic_name__     =      np.load(__UpperCamelCase						)
 return list(__UpperCamelCase						)





def 	lowercase      (   __UpperCamelCase      ,   __UpperCamelCase=None      ,   __UpperCamelCase=False						) ->      Tuple:
 __magic_name__     =      {
     # 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''',
 }

 __magic_name__     =      model_to_url[model_name]
 __magic_name__     =      8
 if "16-frames" in model_name:
  __magic_name__     =      16
 elif "shot" in model_name:
  __magic_name__     =      32

 __magic_name__     =      get_xclip_config(__UpperCamelCase      ,   __UpperCamelCase						)
 __magic_name__     =      XCLIPModel(__UpperCamelCase						)
 model.eval()

 if "drive" in checkpoint_url:
  __magic_name__     =      '''pytorch_model.bin'''
  gdown.cached_download(__UpperCamelCase      ,   __UpperCamelCase      ,   quiet=__UpperCamelCase						)
  __magic_name__     =      torch.load(__UpperCamelCase      ,   map_location='''cpu'''						)['''model''']
 else:
  __magic_name__     =      torch.hub.load_state_dict_from_url(__UpperCamelCase						)['''model''']

 __magic_name__     =      convert_state_dict(__UpperCamelCase      ,   __UpperCamelCase						)

 __magic_name__     =      XCLIPModel(__UpperCamelCase						)
 __magic_name__   ,						__magic_name__     =      model.load_state_dict(__UpperCamelCase      ,   strict=__UpperCamelCase						)
 assert missing_keys == ["text_model.embeddings.position_ids", "vision_model.embeddings.position_ids"]
 model.eval()

 __magic_name__     =      336 if model_name == '''xclip-large-patch14-16-frames''' else 224
 __magic_name__     =      VideoMAEImageProcessor(size=__UpperCamelCase						)
 __magic_name__     =      CLIPTokenizer.from_pretrained('''openai/clip-vit-base-patch32'''						)
 __magic_name__     =      CLIPTokenizerFast.from_pretrained('''openai/clip-vit-base-patch32'''						)
 __magic_name__     =      XCLIPProcessor(image_processor=__UpperCamelCase      ,   tokenizer=__UpperCamelCase						)

 __magic_name__     =      prepare_video(__UpperCamelCase						)
 __magic_name__     =      processor(
     text=['''playing sports''', '''eating spaghetti''', '''go shopping''']      ,   videos=__UpperCamelCase      ,   return_tensors='''pt'''      ,   padding=__UpperCamelCase						)

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

 with torch.no_grad():
  __magic_name__     =      model(**__UpperCamelCase						)

 # Verify outputs
 __magic_name__     =      outputs.logits_per_video
 __magic_name__     =      logits_per_video.softmax(dim=1						)
 print('''Probs:'''      ,   __UpperCamelCase						)
 # kinetics-400
 if model_name == "xclip-base-patch32":
  __magic_name__     =      torch.tensor([[0.0_019, 0.9_951, 0.0_030]]						)
 elif model_name == "xclip-base-patch32-16-frames":
  __magic_name__     =      torch.tensor([[7.0999e-04, 9.9883e-01, 4.5580e-04]]						)
 elif model_name == "xclip-base-patch16":
  __magic_name__     =      torch.tensor([[0.0_083, 0.9_681, 0.0_236]]						)
 elif model_name == "xclip-base-patch16-16-frames":
  __magic_name__     =      torch.tensor([[7.6937e-04, 9.9728e-01, 1.9473e-03]]						)
 elif model_name == "xclip-large-patch14":
  __magic_name__     =      torch.tensor([[0.0_062, 0.9_864, 0.0_075]]						)
 elif model_name == "xclip-large-patch14-16-frames":
  __magic_name__     =      torch.tensor([[3.3877e-04, 9.9937e-01, 2.8888e-04]]						)
 # kinetics-600
 elif model_name == "xclip-base-patch16-kinetics-600":
  __magic_name__     =      torch.tensor([[0.0_555, 0.8_914, 0.0_531]]						)
 elif model_name == "xclip-base-patch16-kinetics-600-16-frames":
  __magic_name__     =      torch.tensor([[3.8554e-04, 9.9929e-01, 3.2754e-04]]						)
 elif model_name == "xclip-large-patch14-kinetics-600":
  __magic_name__     =      torch.tensor([[0.0_036, 0.9_920, 0.0_045]]						)
 # few shot
 elif model_name == "xclip-base-patch16-hmdb-2-shot":
  __magic_name__     =      torch.tensor([[7.1890e-06, 9.9994e-01, 5.6559e-05]]						)
 elif model_name == "xclip-base-patch16-hmdb-4-shot":
  __magic_name__     =      torch.tensor([[1.0320e-05, 9.9993e-01, 6.2435e-05]]						)
 elif model_name == "xclip-base-patch16-hmdb-8-shot":
  __magic_name__     =      torch.tensor([[4.1377e-06, 9.9990e-01, 9.8386e-05]]						)
 elif model_name == "xclip-base-patch16-hmdb-16-shot":
  __magic_name__     =      torch.tensor([[4.1347e-05, 9.9962e-01, 3.3411e-04]]						)
 elif model_name == "xclip-base-patch16-ucf-2-shot":
  __magic_name__     =      torch.tensor([[8.5857e-05, 9.9928e-01, 6.3291e-04]]						)
 elif model_name == "xclip-base-patch16-ucf-4-shot":
  __magic_name__     =      torch.tensor([[8.5857e-05, 9.9928e-01, 6.3291e-04]]						)
 elif model_name == "xclip-base-patch16-ucf-8-shot":
  __magic_name__     =      torch.tensor([[0.0_027, 0.9_904, 0.0_070]]						)
 elif model_name == "xclip-base-patch16-ucf-16-shot":
  __magic_name__     =      torch.tensor([[9.8219e-04, 9.9593e-01, 3.0863e-03]]						)
 # zero shot
 elif model_name == "xclip-base-patch16-zero-shot":
  __magic_name__     =      torch.tensor([[3.5082e-04, 9.9785e-01, 1.7966e-03]]						)
 else:
  raise ValueError(f'''Model name {model_name} not supported'''						)
 assert torch.allclose(__UpperCamelCase      ,   __UpperCamelCase      ,   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(__UpperCamelCase						)

 if push_to_hub:
  print('''Pushing model, processor and slow tokenizer files to the hub...'''						)
  model.push_to_hub(__UpperCamelCase      ,   organization='''nielsr'''						)
  processor.push_to_hub(__UpperCamelCase      ,   organization='''nielsr'''						)
  slow_tokenizer.push_to_hub(__UpperCamelCase      ,   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)

