






import argparse

import requests
import torch
from PIL import Image

from transformers import ViTMAEConfig, ViTMAEForPreTraining, ViTMAEImageProcessor






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

  return name






def 				lowerCamelCase__      (    __lowerCamelCase			:     Dict  ,		__lowerCamelCase			:     Tuple		):
  for key in orig_state_dict.copy().keys():
    __UpperCAmelCase							:	Any           =  orig_state_dict.pop(__lowerCamelCase		)

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

    else:
      __UpperCAmelCase							:	Optional[Any]           =  val

  return orig_state_dict






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

  __UpperCAmelCase							:	Tuple           =  ViTMAEForPreTraining(__lowerCamelCase		)

  __UpperCAmelCase							:	int           =  torch.hub.load_state_dict_from_url(__lowerCamelCase  ,		map_location="""cpu"""		)["""model"""]

  __UpperCAmelCase							:	List[Any]           =  ViTMAEImageProcessor(size=config.image_size		)

  __UpperCAmelCase							:	Optional[int]           =  convert_state_dict(__lowerCamelCase  ,		__lowerCamelCase		)

  model.load_state_dict(__lowerCamelCase		)
  model.eval()

  __UpperCAmelCase							:	List[Any]           =  """https://user-images.githubusercontent.com/11435359/147738734-196fd92f-9260-48d5-ba7e-bf103d29364d.jpg"""

  __UpperCAmelCase							:	Optional[Any]           =  Image.open(requests.get(__lowerCamelCase  ,		stream=__lowerCamelCase		).raw		)
  __UpperCAmelCase							:	Optional[int]           =  ViTMAEImageProcessor(size=config.image_size		)
  __UpperCAmelCase							:	Optional[int]           =  image_processor(images=__lowerCamelCase  ,		return_tensors="""pt"""		)

  # forward pass
  torch.manual_seed(2		)
  __UpperCAmelCase							:	int           =  model(**__lowerCamelCase		)
  __UpperCAmelCase							:	Any           =  outputs.logits

  if "large" in checkpoint_url:
    __UpperCAmelCase							:	str           =  torch.tensor(
        [[-0.7_3_0_9, -0.7_1_2_8, -1.0_1_6_9], [-1.0_1_6_1, -0.9_0_5_8, -1.1_8_7_8], [-1.0_4_7_8, -0.9_4_1_1, -1.1_9_1_1]]		)
  elif "huge" in checkpoint_url:
    __UpperCAmelCase							:	Optional[Any]           =  torch.tensor(
        [[-1.1_5_9_9, -0.9_1_9_9, -1.2_2_2_1], [-1.1_9_5_2, -0.9_2_6_9, -1.2_3_0_7], [-1.2_1_4_3, -0.9_3_3_7, -1.2_2_6_2]]		)
  else:
    __UpperCAmelCase							:	Optional[int]           =  torch.tensor(
        [[-0.9_1_9_2, -0.8_4_8_1, -1.1_2_5_9], [-1.1_3_4_9, -1.0_0_3_4, -1.2_5_9_9], [-1.1_7_5_7, -1.0_4_2_9, -1.2_7_2_6]]		)

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

  print(f"""Saving model to {pytorch_dump_folder_path}"""		)
  model.save_pretrained(__lowerCamelCase		)

  print(f"""Saving image processor to {pytorch_dump_folder_path}"""		)
  image_processor.save_pretrained(__lowerCamelCase		)


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

       a		:       List[Any]			     = parser.parse_args()
       convert_vit_mae_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)










from functools import lru_cache






@lru_cache
def 				lowerCamelCase__      (    __lowerCamelCase			:     int		):
  if num < 0:
    raise ValueError("""Number should not be negative."""		)

  return 1 if num in (0, 1) else num * factorial(num - 1		)


if __name__ == "__main__":
       import doctest

       doctest.testmod()


