





import argparse

import requests
import torch
from PIL import Image

from transformers import ViTMAEConfig, ViTMAEForPreTraining, ViTMAEImageProcessor



def A_     (		A__    )  ->		int:
       if "cls_token" in name:
              a__						:     Any			=    name.replace('cls_token'		,				'vit.embeddings.cls_token'    )
       if "mask_token" in name:
              a__						:     Any			=    name.replace('mask_token'		,				'decoder.mask_token'    )
       if "decoder_pos_embed" in name:
              a__						:     List[str]			=    name.replace('decoder_pos_embed'		,				'decoder.decoder_pos_embed'    )
       if "pos_embed" in name and "decoder" not in name:
              a__						:     Optional[int]			=    name.replace('pos_embed'		,				'vit.embeddings.position_embeddings'    )
       if "patch_embed.proj" in name:
              a__						:     List[Any]			=    name.replace('patch_embed.proj'		,				'vit.embeddings.patch_embeddings.projection'    )
       if "patch_embed.norm" in name:
              a__						:     str			=    name.replace('patch_embed.norm'		,				'vit.embeddings.norm'    )
       if "decoder_blocks" in name:
              a__						:     Dict			=    name.replace('decoder_blocks'		,				'decoder.decoder_layers'    )
       if "blocks" in name:
              a__						:     Dict			=    name.replace('blocks'		,				'vit.encoder.layer'    )
       if "attn.proj" in name:
              a__						:     Optional[Any]			=    name.replace('attn.proj'		,				'attention.output.dense'    )
       if "attn" in name:
              a__						:     List[str]			=    name.replace('attn'		,				'attention.self'    )
       if "norm1" in name:
              a__						:     Union[str, Any]			=    name.replace('norm1'		,				'layernorm_before'    )
       if "norm2" in name:
              a__						:     List[Any]			=    name.replace('norm2'		,				'layernorm_after'    )
       if "mlp.fc1" in name:
              a__						:     Optional[Any]			=    name.replace('mlp.fc1'		,				'intermediate.dense'    )
       if "mlp.fc2" in name:
              a__						:     Dict			=    name.replace('mlp.fc2'		,				'output.dense'    )
       if "decoder_embed" in name:
              a__						:     str			=    name.replace('decoder_embed'		,				'decoder.decoder_embed'    )
       if "decoder_norm" in name:
              a__						:     str			=    name.replace('decoder_norm'		,				'decoder.decoder_norm'    )
       if "decoder_pred" in name:
              a__						:     List[str]			=    name.replace('decoder_pred'		,				'decoder.decoder_pred'    )
       if "norm.weight" in name and "decoder" not in name:
              a__						:     Dict			=    name.replace('norm.weight'		,				'vit.layernorm.weight'    )
       if "norm.bias" in name and "decoder" not in name:
              a__						:     int			=    name.replace('norm.bias'		,				'vit.layernorm.bias'    )

       return name



def A_     (		A__		,				A__    )  ->		Any:
       for key in orig_state_dict.copy().keys():
              a__						:     Any			=    orig_state_dict.pop(A__    )

              if "qkv" in key:
                     a__						:     Dict			=    key.split('.'    )
                     a__						:     Optional[int]			=    int(key_split[1]    )
                     if "decoder_blocks" in key:
                            a__						:     List[str]			=    config.decoder_hidden_size
                            a__						:     Union[str, Any]			=    'decoder.decoder_layers.'
                            if "weight" in key:
                                   a__						:     int			=    val[:dim, :]
                                   a__						:     Union[str, Any]			=    val[dim : dim * 2, :]
                                   a__						:     Union[str, Any]			=    val[-dim:, :]
                            elif "bias" in key:
                                   a__						:     List[Any]			=    val[:dim]
                                   a__						:     List[Any]			=    val[dim : dim * 2]
                                   a__						:     Tuple			=    val[-dim:]
                     else:
                            a__						:     List[str]			=    config.hidden_size
                            a__						:     Optional[int]			=    'vit.encoder.layer.'
                            if "weight" in key:
                                   a__						:     Optional[int]			=    val[:dim, :]
                                   a__						:     List[Any]			=    val[dim : dim * 2, :]
                                   a__						:     List[Any]			=    val[-dim:, :]
                            elif "bias" in key:
                                   a__						:     Optional[int]			=    val[:dim]
                                   a__						:     Union[str, Any]			=    val[dim : dim * 2]
                                   a__						:     Any			=    val[-dim:]

              else:
                     a__						:     List[Any]			=    val

       return orig_state_dict



def A_     (		A__		,				A__    )  ->		Dict:
       a__						:     Dict			=    ViTMAEConfig()
       if "large" in checkpoint_url:
              a__						:     Optional[int]			=    1024
              a__						:     List[str]			=    4096
              a__						:     Optional[int]			=    24
              a__						:     int			=    16
       elif "huge" in checkpoint_url:
              a__						:     int			=    14
              a__						:     List[Any]			=    1280
              a__						:     Dict			=    5120
              a__						:     Dict			=    32
              a__						:     List[Any]			=    16

       a__						:     Optional[int]			=    ViTMAEForPreTraining(A__    )

       a__						:     Optional[int]			=    torch.hub.load_state_dict_from_url(A__		,				map_location='cpu'    )['model']

       a__						:     Any			=    ViTMAEImageProcessor(size=config.image_size    )

       a__						:     Optional[Any]			=    convert_state_dict(A__		,				A__    )

       model.load_state_dict(A__    )
       model.eval()

       a__						:     Union[str, Any]			=    'https://user-images.githubusercontent.com/11435359/147738734-196fd92f-9260-48d5-ba7e-bf103d29364d.jpg'

       a__						:     List[Any]			=    Image.open(requests.get(A__		,				stream=A__    ).raw    )
       a__						:     Union[str, Any]			=    ViTMAEImageProcessor(size=config.image_size    )
       a__						:     Tuple			=    image_processor(images=A__		,				return_tensors='pt'    )

       # forward pass
       torch.manual_seed(2    )
       a__						:     Union[str, Any]			=    model(**A__    )
       a__						:     List[str]			=    outputs.logits

       if "large" in checkpoint_url:
              a__						:     List[Any]			=    torch.tensor(
                  [[-0.73_09, -0.71_28, -1.01_69], [-1.01_61, -0.90_58, -1.18_78], [-1.04_78, -0.94_11, -1.19_11]]    )
       elif "huge" in checkpoint_url:
              a__						:     int			=    torch.tensor(
                  [[-1.15_99, -0.91_99, -1.22_21], [-1.19_52, -0.92_69, -1.23_07], [-1.21_43, -0.93_37, -1.22_62]]    )
       else:
              a__						:     Any			=    torch.tensor(
                  [[-0.91_92, -0.84_81, -1.12_59], [-1.13_49, -1.00_34, -1.25_99], [-1.17_57, -1.04_29, -1.27_26]]    )

       # verify logits
       assert torch.allclose(logits[0, :3, :3]		,				A__		,				atol=1E-4    )

       print(F'Saving model to {pytorch_dump_folder_path}'    )
       model.save_pretrained(A__    )

       print(F'Saving image processor to {pytorch_dump_folder_path}'    )
       image_processor.save_pretrained(A__    )


if __name__ == "__main__":
    lowercase				:	Optional[Any]      						=  argparse.ArgumentParser()
    # Required parameters
    parser.add_argument(
        """--checkpoint_url""",
        default="""https://dl.fbaipublicfiles.com/mae/visualize/mae_visualize_vit_base.pth""",
        type=str,
        help="""URL of the checkpoint you'd like to convert.""",
    )
    parser.add_argument(
        """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model directory."""
    )

    lowercase				:	Any      						=  parser.parse_args()
    convert_vit_mae_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)









import argparse
import os
import pickle
import sys

import torch

from transformers import TransfoXLConfig, TransfoXLLMHeadModel, load_tf_weights_in_transfo_xl
from transformers.models.transfo_xl import tokenization_transfo_xl as data_utils
from transformers.models.transfo_xl.tokenization_transfo_xl import CORPUS_NAME, VOCAB_FILES_NAMES
from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging


logging.set_verbosity_info()

# We do this to be able to load python 2 datasets pickles
# See e.g. https://stackoverflow.com/questions/2121874/python-pickling-after-changing-a-modules-directory/2121918#2121918
lowercase				:	Union[str, Any]      						=  data_utils.TransfoXLTokenizer
lowercase				:	Optional[int]      						=  data_utils.TransfoXLCorpus
lowercase				:	List[Any]      						=  data_utils
lowercase				:	Tuple      						=  data_utils



def A_     (		A__		,				A__		,				A__		,				A__    )  ->		Optional[Any]:
       if transfo_xl_dataset_file:
              # Convert a pre-processed corpus (see original TensorFlow repo)
              with open(A__		,				'rb'    ) as fp:
                     a__						:     int			=    pickle.load(A__		,				encoding='latin1'    )
              # Save vocabulary and dataset cache as Dictionaries (should be better than pickles for the long-term)
              a__						:     int			=    pytorch_dump_folder_path + '/' + VOCAB_FILES_NAMES['pretrained_vocab_file']
              print(F'Save vocabulary to {pytorch_vocab_dump_path}'    )
              a__						:     List[Any]			=    corpus.vocab.__dict__
              torch.save(A__		,				A__    )

              a__						:     Dict			=    corpus.__dict__
              corpus_dict_no_vocab.pop('vocab'		,				A__    )
              a__						:     Optional[int]			=    pytorch_dump_folder_path + '/' + CORPUS_NAME
              print(F'Save dataset to {pytorch_dataset_dump_path}'    )
              torch.save(A__		,				A__    )

       if tf_checkpoint_path:
              # Convert a pre-trained TensorFlow model
              a__						:     Union[str, Any]			=    os.path.abspath(A__    )
              a__						:     Optional[Any]			=    os.path.abspath(A__    )

              print(F'Converting Transformer XL checkpoint from {tf_path} with config at {config_path}.'    )
              # Initialise PyTorch model
              if transfo_xl_config_file == "":
                     a__						:     Dict			=    TransfoXLConfig()
              else:
                     a__						:     Dict			=    TransfoXLConfig.from_json_file(A__    )
              print(F'Building PyTorch model from configuration: {config}'    )
              a__						:     Optional[int]			=    TransfoXLLMHeadModel(A__    )

              a__						:     int			=    load_tf_weights_in_transfo_xl(A__		,				A__		,				A__    )
              # Save pytorch-model
              a__						:     Any			=    os.path.join(A__		,				A__    )
              a__						:     Dict			=    os.path.join(A__		,				A__    )
              print(F'Save PyTorch model to {os.path.abspath(A__    )}'    )
              torch.save(model.state_dict()		,				A__    )
              print(F'Save configuration file to {os.path.abspath(A__    )}'    )
              with open(A__		,				'w'		,				encoding='utf-8'    ) as f:
                     f.write(config.to_json_string()    )


if __name__ == "__main__":
    lowercase				:	List[Any]      						=  argparse.ArgumentParser()
    parser.add_argument(
        """--pytorch_dump_folder_path""",
        default=None,
        type=str,
        required=True,
        help="""Path to the folder to store the PyTorch model or dataset/vocab.""",
    )
    parser.add_argument(
        """--tf_checkpoint_path""",
        default="""""",
        type=str,
        help="""An optional path to a TensorFlow checkpoint path to be converted.""",
    )
    parser.add_argument(
        """--transfo_xl_config_file""",
        default="""""",
        type=str,
        help=(
            """An optional config json file corresponding to the pre-trained BERT model. \n"""
            """This specifies the model architecture."""
        ),
    )
    parser.add_argument(
        """--transfo_xl_dataset_file""",
        default="""""",
        type=str,
        help="""An optional dataset file to be converted in a vocabulary.""",
    )
    lowercase				:	Any      						=  parser.parse_args()
    convert_transfo_xl_checkpoint_to_pytorch(
        args.tf_checkpoint_path,
        args.transfo_xl_config_file,
        args.pytorch_dump_folder_path,
        args.transfo_xl_dataset_file,
    )


