





import argparse

import requests
import torch
from PIL import Image

from transformers import ViTMAEConfig, ViTMAEForPreTraining, ViTMAEImageProcessor


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

    return name


def        SCREAMING_SNAKE_CASE      (       _UpperCAmelCase						,	_UpperCAmelCase )			->     Union[str, Any]:
    for key in orig_state_dict.copy().keys():
        lowerCamelCase__    :      Tuple					 =		orig_state_dict.pop(_UpperCAmelCase )

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

        else:
            lowerCamelCase__    :      Dict					 =		val

    return orig_state_dict


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

    lowerCamelCase__    :      List[Any]					 =		ViTMAEForPreTraining(_UpperCAmelCase )

    lowerCamelCase__    :      Union[str, Any]					 =		torch.hub.load_state_dict_from_url(_UpperCAmelCase						,	map_location='cpu' )['model']

    lowerCamelCase__    :      List[str]					 =		ViTMAEImageProcessor(size=config.image_size )

    lowerCamelCase__    :      int					 =		convert_state_dict(_UpperCAmelCase						,	_UpperCAmelCase )

    model.load_state_dict(_UpperCAmelCase )
    model.eval()

    lowerCamelCase__    :      Dict					 =		'https://user-images.githubusercontent.com/11435359/147738734-196fd92f-9260-48d5-ba7e-bf103d29364d.jpg'

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

    # forward pass
    torch.manual_seed(2 )
    lowerCamelCase__    :      str					 =		model(**_UpperCAmelCase )
    lowerCamelCase__    :      Any					 =		outputs.logits

    if "large" in checkpoint_url:
        lowerCamelCase__    :      List[str]					 =		torch.tensor(
            [[-0.7_309, -0.7_128, -1.0_169], [-1.0_161, -0.9_058, -1.1_878], [-1.0_478, -0.9_411, -1.1_911]] )
    elif "huge" in checkpoint_url:
        lowerCamelCase__    :      Optional[Any]					 =		torch.tensor(
            [[-1.1_599, -0.9_199, -1.2_221], [-1.1_952, -0.9_269, -1.2_307], [-1.2_143, -0.9_337, -1.2_262]] )
    else:
        lowerCamelCase__    :      Tuple					 =		torch.tensor(
            [[-0.9_192, -0.8_481, -1.1_259], [-1.1_349, -1.0_034, -1.2_599], [-1.1_757, -1.0_429, -1.2_726]] )

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

    print(F"""Saving model to {pytorch_dump_folder_path}""" )
    model.save_pretrained(_UpperCAmelCase )

    print(F"""Saving image processor to {pytorch_dump_folder_path}""" )
    image_processor.save_pretrained(_UpperCAmelCase )


if __name__ == "__main__":
    _UpperCAmelCase				:       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."""
    )

    _UpperCAmelCase				:       Union[str, Any]	   =				parser.parse_args()
    convert_vit_mae_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)












import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_base import BatchEncoding
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import PaddingStrategy, logging
from .tokenization_realm import RealmTokenizer


_UpperCAmelCase				:       Optional[int]	   =				logging.get_logger(__name__)

_UpperCAmelCase				:       Any	   =				{"""vocab_file""": """vocab.txt""", """tokenizer_file""": """tokenizer.json"""}

_UpperCAmelCase				:       int	   =				{
    """vocab_file""": {
        """google/realm-cc-news-pretrained-embedder""": (
            """https://huggingface.co/google/realm-cc-news-pretrained-embedder/resolve/main/vocab.txt"""
        ),
        """google/realm-cc-news-pretrained-encoder""": (
            """https://huggingface.co/google/realm-cc-news-pretrained-encoder/resolve/main/vocab.txt"""
        ),
        """google/realm-cc-news-pretrained-scorer""": (
            """https://huggingface.co/google/realm-cc-news-pretrained-scorer/resolve/main/vocab.txt"""
        ),
        """google/realm-cc-news-pretrained-openqa""": (
            """https://huggingface.co/google/realm-cc-news-pretrained-openqa/aresolve/main/vocab.txt"""
        ),
        """google/realm-orqa-nq-openqa""": """https://huggingface.co/google/realm-orqa-nq-openqa/resolve/main/vocab.txt""",
        """google/realm-orqa-nq-reader""": """https://huggingface.co/google/realm-orqa-nq-reader/resolve/main/vocab.txt""",
        """google/realm-orqa-wq-openqa""": """https://huggingface.co/google/realm-orqa-wq-openqa/resolve/main/vocab.txt""",
        """google/realm-orqa-wq-reader""": """https://huggingface.co/google/realm-orqa-wq-reader/resolve/main/vocab.txt""",
    },
    """tokenizer_file""": {
        """google/realm-cc-news-pretrained-embedder""": (
            """https://huggingface.co/google/realm-cc-news-pretrained-embedder/resolve/main/tokenizer.jsont"""
        ),
        """google/realm-cc-news-pretrained-encoder""": (
            """https://huggingface.co/google/realm-cc-news-pretrained-encoder/resolve/main/tokenizer.json"""
        ),
        """google/realm-cc-news-pretrained-scorer""": (
            """https://huggingface.co/google/realm-cc-news-pretrained-scorer/resolve/main/tokenizer.json"""
        ),
        """google/realm-cc-news-pretrained-openqa""": (
            """https://huggingface.co/google/realm-cc-news-pretrained-openqa/aresolve/main/tokenizer.json"""
        ),
        """google/realm-orqa-nq-openqa""": (
            """https://huggingface.co/google/realm-orqa-nq-openqa/resolve/main/tokenizer.json"""
        ),
        """google/realm-orqa-nq-reader""": (
            """https://huggingface.co/google/realm-orqa-nq-reader/resolve/main/tokenizer.json"""
        ),
        """google/realm-orqa-wq-openqa""": (
            """https://huggingface.co/google/realm-orqa-wq-openqa/resolve/main/tokenizer.json"""
        ),
        """google/realm-orqa-wq-reader""": (
            """https://huggingface.co/google/realm-orqa-wq-reader/resolve/main/tokenizer.json"""
        ),
    },
}

_UpperCAmelCase				:       int	   =				{
    """google/realm-cc-news-pretrained-embedder""": 5_12,
    """google/realm-cc-news-pretrained-encoder""": 5_12,
    """google/realm-cc-news-pretrained-scorer""": 5_12,
    """google/realm-cc-news-pretrained-openqa""": 5_12,
    """google/realm-orqa-nq-openqa""": 5_12,
    """google/realm-orqa-nq-reader""": 5_12,
    """google/realm-orqa-wq-openqa""": 5_12,
    """google/realm-orqa-wq-reader""": 5_12,
}

_UpperCAmelCase				:       Any	   =				{
    """google/realm-cc-news-pretrained-embedder""": {"""do_lower_case""": True},
    """google/realm-cc-news-pretrained-encoder""": {"""do_lower_case""": True},
    """google/realm-cc-news-pretrained-scorer""": {"""do_lower_case""": True},
    """google/realm-cc-news-pretrained-openqa""": {"""do_lower_case""": True},
    """google/realm-orqa-nq-openqa""": {"""do_lower_case""": True},
    """google/realm-orqa-nq-reader""": {"""do_lower_case""": True},
    """google/realm-orqa-wq-openqa""": {"""do_lower_case""": True},
    """google/realm-orqa-wq-reader""": {"""do_lower_case""": True},
}

class    lowerCAmelCase							(					__UpperCamelCase			):
 UpperCAmelCase__													=		VOCAB_FILES_NAMES
 UpperCAmelCase__													=		PRETRAINED_VOCAB_FILES_MAP
 UpperCAmelCase__													=		PRETRAINED_INIT_CONFIGURATION
 UpperCAmelCase__													=		PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
 UpperCAmelCase__													=		RealmTokenizer
 def __init__(      self  :      Optional[int]			,     UpperCAmelCase  :      Tuple=None			,     UpperCAmelCase  :      Any=None			,     UpperCAmelCase  :      List[Any]=True			,     UpperCAmelCase  :      Optional[Any]="[UNK]"			,     UpperCAmelCase  :      Any="[SEP]"			,     UpperCAmelCase  :      Tuple="[PAD]"			,     UpperCAmelCase  :      List[Any]="[CLS]"			,     UpperCAmelCase  :      Union[str, Any]="[MASK]"			,     UpperCAmelCase  :      Optional[Any]=True			,     UpperCAmelCase  :      Any=None			,     **UpperCAmelCase  :      Optional[int]			,     )			->	str:
     super().__init__(
         UpperCAmelCase			,     tokenizer_file=UpperCAmelCase			,     do_lower_case=UpperCAmelCase			,     unk_token=UpperCAmelCase			,     sep_token=UpperCAmelCase			,     pad_token=UpperCAmelCase			,     cls_token=UpperCAmelCase			,     mask_token=UpperCAmelCase			,     tokenize_chinese_chars=UpperCAmelCase			,     strip_accents=UpperCAmelCase			,     **UpperCAmelCase			,     )

     lowerCamelCase__    :      List[Any]					 =		json.loads(self.backend_tokenizer.normalizer.__getstate__()	)
     if (
         normalizer_state.get('lowercase'			,     UpperCAmelCase	) != do_lower_case
         or normalizer_state.get('strip_accents'			,     UpperCAmelCase	) != strip_accents
         or normalizer_state.get('handle_chinese_chars'			,     UpperCAmelCase	) != tokenize_chinese_chars
     ):
         lowerCamelCase__    :      Optional[int]					 =		getattr(UpperCAmelCase			,     normalizer_state.pop('type'	)	)
         lowerCamelCase__    :      Optional[Any]					 =		do_lower_case
         lowerCamelCase__    :      str					 =		strip_accents
         lowerCamelCase__    :      Optional[Any]					 =		tokenize_chinese_chars
         lowerCamelCase__    :      int					 =		normalizer_class(**UpperCAmelCase	)

     lowerCamelCase__    :      str					 =		do_lower_case
 def      A_	(      self  :      Optional[int]			,     UpperCAmelCase  :      int			,     **UpperCAmelCase  :      int	)			->	List[Any]:
     lowerCamelCase__    :      List[Any]					 =		PaddingStrategy.MAX_LENGTH

     lowerCamelCase__    :      Optional[int]					 =		text
     lowerCamelCase__    :      Dict					 =		kwargs.pop('text_pair'			,     UpperCAmelCase	)
     lowerCamelCase__    :      List[Any]					 =		kwargs.pop('return_tensors'			,     UpperCAmelCase	)

     lowerCamelCase__    :      List[Any]					 =		{
         'input_ids': [],
         'attention_mask': [],
         'token_type_ids': [],
     }

     for idx, candidate_text in enumerate(UpperCAmelCase	):
         if batch_text_pair is not None:
             lowerCamelCase__    :      Tuple					 =		batch_text_pair[idx]
         else:
             lowerCamelCase__    :      Dict					 =		None

         lowerCamelCase__    :      Optional[int]					 =		super().__call__(UpperCAmelCase			,     UpperCAmelCase			,     return_tensors=UpperCAmelCase			,     **UpperCAmelCase	)

         lowerCamelCase__    :      Any					 =		encoded_candidates.get('input_ids'	)
         lowerCamelCase__    :      Union[str, Any]					 =		encoded_candidates.get('attention_mask'	)
         lowerCamelCase__    :      Tuple					 =		encoded_candidates.get('token_type_ids'	)

         if encoded_input_ids is not None:
             output_data["input_ids"].append(UpperCAmelCase	)
         if encoded_attention_mask is not None:
             output_data["attention_mask"].append(UpperCAmelCase	)
         if encoded_token_type_ids is not None:
             output_data["token_type_ids"].append(UpperCAmelCase	)

     lowerCamelCase__    :      int					 =		{key: item for key, item in output_data.items() if len(UpperCAmelCase	) != 0}

     return BatchEncoding(UpperCAmelCase			,     tensor_type=UpperCAmelCase	)
 def      A_	(      self  :      int			,     UpperCAmelCase  :      Optional[Any]			,     UpperCAmelCase  :      Optional[int]=None	)			->	List[str]:
     lowerCamelCase__    :      Tuple					 =		[self.cls_token_id] + token_ids_a + [self.sep_token_id]

     if token_ids_a:
         output += token_ids_a + [self.sep_token_id]

     return output
 def      A_	(      self  :      Tuple			,     UpperCAmelCase  :      List[int]			,     UpperCAmelCase  :      Optional[List[int]] = None	)			->	List[int]:
     lowerCamelCase__    :      List[Any]					 =		[self.sep_token_id]
     lowerCamelCase__    :      Optional[int]					 =		[self.cls_token_id]
     if token_ids_a is None:
         return len(cls + token_ids_a + sep	) * [0]
     return len(cls + token_ids_a + sep	) * [0] + len(token_ids_a + sep	) * [1]







 def      A_	(      self  :      Optional[Any]			,     UpperCAmelCase  :      str			,     UpperCAmelCase  :      Optional[str] = None	)			->	Tuple[str]:
     lowerCamelCase__    :      int					 =		self._tokenizer.model.save(UpperCAmelCase			,     name=UpperCAmelCase	)
     return tuple(UpperCAmelCase	)





