






"""simple docstring"""




import argparse
import json
from pathlib import Path

import requests
import torch
from huggingface_hub import hf_hub_download
from PIL import Image

from transformers import ViTConfig, ViTForImageClassification, ViTImageProcessor, ViTModel
from transformers.utils import logging


logging.set_verbosity_info()
UpperCamelCase							       =    logging.get_logger(__name__)






def        lowerCAmelCase (      UpperCamelCase_:							Optional[int]     , UpperCamelCase_:							Union[str, Any]=False					)       ->							Any:







  '''simple docstring'''







  _a							=	[]
  for i in range(config.num_hidden_layers					):
    # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms
    rename_keys.append((f'''blocks.{i}.norm1.weight''', f'''vit.encoder.layer.{i}.layernorm_before.weight''')					)
    rename_keys.append((f'''blocks.{i}.norm1.bias''', f'''vit.encoder.layer.{i}.layernorm_before.bias''')					)
    rename_keys.append((f'''blocks.{i}.attn.proj.weight''', f'''vit.encoder.layer.{i}.attention.output.dense.weight''')					)
    rename_keys.append((f'''blocks.{i}.attn.proj.bias''', f'''vit.encoder.layer.{i}.attention.output.dense.bias''')					)
    rename_keys.append((f'''blocks.{i}.norm2.weight''', f'''vit.encoder.layer.{i}.layernorm_after.weight''')					)
    rename_keys.append((f'''blocks.{i}.norm2.bias''', f'''vit.encoder.layer.{i}.layernorm_after.bias''')					)
    rename_keys.append((f'''blocks.{i}.mlp.fc1.weight''', f'''vit.encoder.layer.{i}.intermediate.dense.weight''')					)
    rename_keys.append((f'''blocks.{i}.mlp.fc1.bias''', f'''vit.encoder.layer.{i}.intermediate.dense.bias''')					)
    rename_keys.append((f'''blocks.{i}.mlp.fc2.weight''', f'''vit.encoder.layer.{i}.output.dense.weight''')					)
    rename_keys.append((f'''blocks.{i}.mlp.fc2.bias''', f'''vit.encoder.layer.{i}.output.dense.bias''')					)

  # projection layer + position embeddings
  rename_keys.extend(
      [
          ("cls_token", "vit.embeddings.cls_token"),
          ("patch_embed.proj.weight", "vit.embeddings.patch_embeddings.projection.weight"),
          ("patch_embed.proj.bias", "vit.embeddings.patch_embeddings.projection.bias"),
          ("pos_embed", "vit.embeddings.position_embeddings"),
      ]					)

  if base_model:
    # layernorm + pooler
    rename_keys.extend(
        [
            ("norm.weight", "layernorm.weight"),
            ("norm.bias", "layernorm.bias"),
        ]					)

    # if just the base model, we should remove "vit" from all keys that start with "vit"
    _a							=	[(pair[0], pair[1][4:]) if pair[1].startswith("vit"					) else pair for pair in rename_keys]
  else:
    # layernorm + classification head
    rename_keys.extend(
        [
            ("norm.weight", "vit.layernorm.weight"),
            ("norm.bias", "vit.layernorm.bias"),
            ("head.weight", "classifier.weight"),
            ("head.bias", "classifier.bias"),
        ]					)

  return rename_keys






def        lowerCAmelCase (      UpperCamelCase_:							Union[str, Any]     , UpperCamelCase_:							str     , UpperCamelCase_:							List[Any]=False					)       ->							str:







  '''simple docstring'''







  for i in range(config.num_hidden_layers					):
    if base_model:
      _a							=	""""""
    else:
      _a							=	"""vit."""
    # read in weights + bias of input projection layer (in timm, this is a single matrix + bias)
    _a							=	state_dict.pop(f'''blocks.{i}.attn.qkv.weight'''					)
    _a							=	state_dict.pop(f'''blocks.{i}.attn.qkv.bias'''					)
    # next, add query, keys and values (in that order) to the state dict
    _a							=	in_proj_weight[
        : config.hidden_size, :
    ]
    _a							=	in_proj_bias[: config.hidden_size]
    _a							=	in_proj_weight[
        config.hidden_size : config.hidden_size * 2, :
    ]
    _a							=	in_proj_bias[
        config.hidden_size : config.hidden_size * 2
    ]
    _a							=	in_proj_weight[
        -config.hidden_size :, :
    ]
    _a							=	in_proj_bias[-config.hidden_size :]






def        lowerCAmelCase (      UpperCamelCase_:							int					)       ->							int:







  '''simple docstring'''







  _a							=	["""head.weight""", """head.bias"""]
  for k in ignore_keys:
    state_dict.pop(UpperCamelCase_     , UpperCamelCase_					)






def        lowerCAmelCase (      UpperCamelCase_:							int     , UpperCamelCase_:							Dict     , UpperCamelCase_:							int					)       ->							str:







  '''simple docstring'''







  _a							=	dct.pop(UpperCamelCase_					)
  _a							=	val






def        lowerCAmelCase (      )       ->							str:







  '''simple docstring'''







  _a							=	"""http://images.cocodataset.org/val2017/000000039769.jpg"""
  _a							=	Image.open(requests.get(UpperCamelCase_     , stream=UpperCamelCase_					).raw					)
  return im






@torch.no_grad()
def        lowerCAmelCase (      UpperCamelCase_:							int     , UpperCamelCase_:							List[Any]     , UpperCamelCase_:							Union[str, Any]=True					)       ->							Dict:







  '''simple docstring'''







  _a							=	ViTConfig()
  # patch_size
  if model_name[-1] == "8":
    _a							=	8
  # set labels if required
  if not base_model:
    _a							=	1000
    _a							=	"""huggingface/label-files"""
    _a							=	"""imagenet-1k-id2label.json"""
    _a							=	json.load(open(hf_hub_download(UpperCamelCase_     , UpperCamelCase_     , repo_type="dataset"					)     , "r"					)					)
    _a							=	{int(UpperCamelCase_					): v for k, v in idalabel.items()}
    _a							=	idalabel
    _a							=	{v: k for k, v in idalabel.items()}
  # size of the architecture
  if model_name in ["dino_vits8", "dino_vits16"]:
    _a							=	384
    _a							=	1536
    _a							=	12
    _a							=	6

  # load original model from torch hub
  _a							=	torch.hub.load("facebookresearch/dino:main"     , UpperCamelCase_					)
  original_model.eval()

  # load state_dict of original model, remove and rename some keys
  _a							=	original_model.state_dict()
  if base_model:
    remove_classification_head_(UpperCamelCase_					)
  _a							=	create_rename_keys(UpperCamelCase_     , base_model=UpperCamelCase_					)
  for src, dest in rename_keys:
    rename_key(UpperCamelCase_     , UpperCamelCase_     , UpperCamelCase_					)
  read_in_q_k_v(UpperCamelCase_     , UpperCamelCase_     , UpperCamelCase_					)

  # load HuggingFace model
  if base_model:
    _a							=	ViTModel(UpperCamelCase_     , add_pooling_layer=UpperCamelCase_					).eval()
  else:
    _a							=	ViTForImageClassification(UpperCamelCase_					).eval()
  model.load_state_dict(UpperCamelCase_					)

  # Check outputs on an image, prepared by ViTImageProcessor
  _a							=	ViTImageProcessor()
  _a							=	image_processor(images=prepare_img()     , return_tensors="pt"					)
  _a							=	encoding["""pixel_values"""]
  _a							=	model(UpperCamelCase_					)

  if base_model:
    _a							=	original_model(UpperCamelCase_					)
    assert torch.allclose(UpperCamelCase_     , outputs.last_hidden_state[:, 0, :]     , atol=1E-1					)
  else:
    _a							=	original_model(UpperCamelCase_					)
    assert logits.shape == outputs.logits.shape
    assert torch.allclose(UpperCamelCase_     , outputs.logits     , atol=1E-3					)

  Path(UpperCamelCase_					).mkdir(exist_ok=UpperCamelCase_					)
  print(f'''Saving model {model_name} 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							       =    argparse.ArgumentParser()
     # Required parameters
     parser.add_argument(
         """--model_name""",
         default="""dino_vitb16""",
         type=str,
         help="""Name of the model trained with DINO you\'d like to convert.""",
     )
     parser.add_argument(
         """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model directory."""
     )
     parser.add_argument(
         """--base_model""",
         action="""store_true""",
         help="""Whether to only convert the base model (no projection head weights).""",
     )

     parser.set_defaults(base_model=True)
     UpperCamelCase							       =    parser.parse_args()
     convert_vit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.base_model)










"""simple docstring"""




from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available


UpperCamelCase							       =    {
    """configuration_transfo_xl""": ["""TRANSFO_XL_PRETRAINED_CONFIG_ARCHIVE_MAP""", """TransfoXLConfig"""],
    """tokenization_transfo_xl""": ["""TransfoXLCorpus""", """TransfoXLTokenizer"""],
}

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     UpperCamelCase							       =    [
         """TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST""",
         """AdaptiveEmbedding""",
         """TransfoXLForSequenceClassification""",
         """TransfoXLLMHeadModel""",
         """TransfoXLModel""",
         """TransfoXLPreTrainedModel""",
         """load_tf_weights_in_transfo_xl""",
     ]

try:
     if not is_tf_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     UpperCamelCase							       =    [
         """TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST""",
         """TFAdaptiveEmbedding""",
         """TFTransfoXLForSequenceClassification""",
         """TFTransfoXLLMHeadModel""",
         """TFTransfoXLMainLayer""",
         """TFTransfoXLModel""",
         """TFTransfoXLPreTrainedModel""",
     ]


if TYPE_CHECKING:
     from .configuration_transfo_xl import TRANSFO_XL_PRETRAINED_CONFIG_ARCHIVE_MAP, TransfoXLConfig
     from .tokenization_transfo_xl import TransfoXLCorpus, TransfoXLTokenizer

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_transfo_xl import (
              TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST,
              AdaptiveEmbedding,
              TransfoXLForSequenceClassification,
              TransfoXLLMHeadModel,
              TransfoXLModel,
              TransfoXLPreTrainedModel,
              load_tf_weights_in_transfo_xl,
          )

     try:
          if not is_tf_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_tf_transfo_xl import (
              TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST,
              TFAdaptiveEmbedding,
              TFTransfoXLForSequenceClassification,
              TFTransfoXLLMHeadModel,
              TFTransfoXLMainLayer,
              TFTransfoXLModel,
              TFTransfoXLPreTrainedModel,
          )

else:
     import sys

     UpperCamelCase							       =    _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)


