
"""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()
A__ :       List[Any]				= logging.get_logger(__name__)




def 			_lowerCAmelCase		(		_UpperCamelCase			,					_UpperCamelCase=False    ):






  """simple docstring"""



  _lowercase:      Any   			=  []
  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"
    _lowercase:      List[str]   			=  [(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			,					_UpperCamelCase			,					_UpperCamelCase=False    ):






  """simple docstring"""



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




def 			_lowerCAmelCase		(		_UpperCamelCase    ):






  """simple docstring"""



  _lowercase:      Tuple   			=  ['''head.weight''', '''head.bias''']
  for k in ignore_keys:
    state_dict.pop(_UpperCamelCase			,					_UpperCamelCase    )




def 			_lowerCAmelCase		(		_UpperCamelCase			,					_UpperCamelCase			,					_UpperCamelCase    ):






  """simple docstring"""



  _lowercase:      str   			=  dct.pop(_UpperCamelCase    )
  _lowercase:      Dict   			=  val




def 			_lowerCAmelCase		(		):






  """simple docstring"""



  _lowercase:      Union[str, Any]   			=  '''http://images.cocodataset.org/val2017/000000039769.jpg'''
  _lowercase:      List[str]   			=  Image.open(requests.get(_UpperCamelCase			,					stream=_UpperCamelCase    ).raw    )
  return im




@torch.no_grad()
def 			_lowerCAmelCase		(		_UpperCamelCase			,					_UpperCamelCase			,					_UpperCamelCase=True    ):






  """simple docstring"""



  _lowercase:      Union[str, Any]   			=  ViTConfig()
  # patch_size
  if model_name[-1] == "8":
    _lowercase:      Optional[Any]   			=  8
  # set labels if required
  if not base_model:
    _lowercase:      str   			=  1_000
    _lowercase:      Dict   			=  '''huggingface/label-files'''
    _lowercase:      Dict   			=  '''imagenet-1k-id2label.json'''
    _lowercase:      Tuple   			=  json.load(open(hf_hub_download(_UpperCamelCase			,					_UpperCamelCase			,					repo_type='''dataset'''    )			,					'''r'''    )    )
    _lowercase:      str   			=  {int(_UpperCamelCase    ): v for k, v in idalabel.items()}
    _lowercase:      Optional[int]   			=  idalabel
    _lowercase:      Dict   			=  {v: k for k, v in idalabel.items()}
  # size of the architecture
  if model_name in ["dino_vits8", "dino_vits16"]:
    _lowercase:      Tuple   			=  384
    _lowercase:      Tuple   			=  1_536
    _lowercase:      Tuple   			=  12
    _lowercase:      Optional[int]   			=  6

  # load original model from torch hub
  _lowercase:      int   			=  torch.hub.load('''facebookresearch/dino:main'''			,					_UpperCamelCase    )
  original_model.eval()

  # load state_dict of original model, remove and rename some keys
  _lowercase:      List[Any]   			=  original_model.state_dict()
  if base_model:
    remove_classification_head_(_UpperCamelCase    )
  _lowercase:      str   			=  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:
    _lowercase:      List[Any]   			=  ViTModel(_UpperCamelCase			,					add_pooling_layer=_UpperCamelCase    ).eval()
  else:
    _lowercase:      List[Any]   			=  ViTForImageClassification(_UpperCamelCase    ).eval()
  model.load_state_dict(_UpperCamelCase    )

  # Check outputs on an image, prepared by ViTImageProcessor
  _lowercase:      Dict   			=  ViTImageProcessor()
  _lowercase:      Dict   			=  image_processor(images=prepare_img()			,					return_tensors='''pt'''    )
  _lowercase:      Any   			=  encoding['''pixel_values''']
  _lowercase:      Optional[Any]   			=  model(_UpperCamelCase    )

  if base_model:
    _lowercase:      Union[str, Any]   			=  original_model(_UpperCamelCase    )
    assert torch.allclose(_UpperCamelCase			,					outputs.last_hidden_state[:, 0, :]			,					atol=1e-1    )
  else:
    _lowercase:      Tuple   			=  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__":
  A__ :       Tuple				= 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)
  A__ :       Dict				= parser.parse_args()
  convert_vit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.base_model)


"""simple docstring"""




import functools
import logging
import os
import sys
import threading
from logging import (
    CRITICAL,  # NOQA
    DEBUG,  # NOQA
    ERROR,  # NOQA
    FATAL,  # NOQA
    INFO,  # NOQA
    NOTSET,  # NOQA
    WARN,  # NOQA
    WARNING,  # NOQA
)
from typing import Optional

import huggingface_hub.utils as hf_hub_utils
from tqdm import auto as tqdm_lib


A__ :       int				= threading.Lock()
A__ :       Optional[logging.Handler]				= None

A__ :       str				= {
    'debug': logging.DEBUG,
    'info': logging.INFO,
    'warning': logging.WARNING,
    'error': logging.ERROR,
    'critical': logging.CRITICAL,
}

A__ :       str				= logging.WARNING

A__ :       Union[str, Any]				= True




def 			_lowerCAmelCase		(		):






  """simple docstring"""



  _lowercase:      List[Any]   			=  os.getenv('''TRANSFORMERS_VERBOSITY'''			,					_UpperCamelCase    )
  if env_level_str:
    if env_level_str in log_levels:
      return log_levels[env_level_str]
    else:
      logging.getLogger().warning(
          f'''Unknown option TRANSFORMERS_VERBOSITY={env_level_str}, '''
          f'''has to be one of: { ", ".join(log_levels.keys()    ) }'''    )
  return _default_log_level




def 			_lowerCAmelCase		(		):






  """simple docstring"""



  return __name__.split('''.'''    )[0]




def 			_lowerCAmelCase		(		):






  """simple docstring"""



  return logging.getLogger(_get_library_name()    )




def 			_lowerCAmelCase		(		):






  """simple docstring"""



  global _default_handler

  with _lock:
    if _default_handler:
      # This library has already configured the library root logger.
      return
    _lowercase:      int   			=  logging.StreamHandler()  # Set sys.stderr as stream.
    _lowercase:      Dict   			=  sys.stderr.flush

    # Apply our default configuration to the library root logger.
    _lowercase:      Dict   			=  _get_library_root_logger()
    library_root_logger.addHandler(_default_handler    )
    library_root_logger.setLevel(_get_default_logging_level()    )
    _lowercase:      Optional[Any]   			=  False




def 			_lowerCAmelCase		(		):






  """simple docstring"""



  global _default_handler

  with _lock:
    if not _default_handler:
      return

    _lowercase:      Tuple   			=  _get_library_root_logger()
    library_root_logger.removeHandler(_default_handler    )
    library_root_logger.setLevel(logging.NOTSET    )
    _lowercase:      Dict   			=  None




def 			_lowerCAmelCase		(		):






  """simple docstring"""



  return log_levels




def 			_lowerCAmelCase		(		_UpperCamelCase = None    ):






  """simple docstring"""



  if name is None:
    _lowercase:      Tuple   			=  _get_library_name()

  _configure_library_root_logger()
  return logging.getLogger(_UpperCamelCase    )




def 			_lowerCAmelCase		(		):






  """simple docstring"""



  _configure_library_root_logger()
  return _get_library_root_logger().getEffectiveLevel()




def 			_lowerCAmelCase		(		_UpperCamelCase    ):






  """simple docstring"""



  _configure_library_root_logger()
  _get_library_root_logger().setLevel(_UpperCamelCase    )




def 			_lowerCAmelCase		(		):






  """simple docstring"""



  return set_verbosity(_UpperCamelCase    )




def 			_lowerCAmelCase		(		):






  """simple docstring"""



  return set_verbosity(_UpperCamelCase    )




def 			_lowerCAmelCase		(		):






  """simple docstring"""



  return set_verbosity(_UpperCamelCase    )




def 			_lowerCAmelCase		(		):






  """simple docstring"""



  return set_verbosity(_UpperCamelCase    )




def 			_lowerCAmelCase		(		):






  """simple docstring"""



  _configure_library_root_logger()

  assert _default_handler is not None
  _get_library_root_logger().removeHandler(_default_handler    )




def 			_lowerCAmelCase		(		):






  """simple docstring"""



  _configure_library_root_logger()

  assert _default_handler is not None
  _get_library_root_logger().addHandler(_default_handler    )




def 			_lowerCAmelCase		(		_UpperCamelCase    ):






  """simple docstring"""



  _configure_library_root_logger()

  assert handler is not None
  _get_library_root_logger().addHandler(_UpperCamelCase    )




def 			_lowerCAmelCase		(		_UpperCamelCase    ):






  """simple docstring"""



  _configure_library_root_logger()

  assert handler is not None and handler not in _get_library_root_logger().handlers
  _get_library_root_logger().removeHandler(_UpperCamelCase    )




def 			_lowerCAmelCase		(		):






  """simple docstring"""



  _configure_library_root_logger()
  _lowercase:      str   			=  False




def 			_lowerCAmelCase		(		):






  """simple docstring"""



  _configure_library_root_logger()
  _lowercase:      List[str]   			=  True




def 			_lowerCAmelCase		(		):






  """simple docstring"""



  _lowercase:      Any   			=  _get_library_root_logger().handlers

  for handler in handlers:
    _lowercase:      int   			=  logging.Formatter('''[%(levelname)s|%(filename)s:%(lineno)s] %(asctime)s >> %(message)s'''    )
    handler.setFormatter(_UpperCamelCase    )




def 			_lowerCAmelCase		(		):






  """simple docstring"""



  _lowercase:      List[str]   			=  _get_library_root_logger().handlers

  for handler in handlers:
    handler.setFormatter(_UpperCamelCase    )




def 			_lowerCAmelCase		(		self			,					*_UpperCamelCase			,					**_UpperCamelCase    ):






  """simple docstring"""



  _lowercase:      Any   			=  os.getenv('''TRANSFORMERS_NO_ADVISORY_WARNINGS'''			,					_UpperCamelCase    )
  if no_advisory_warnings:
    return
  self.warning(*_UpperCamelCase			,					**_UpperCamelCase    )


A__ :       Optional[int]				= warning_advice




@functools.lru_cache(_UpperCamelCase    )
def 			_lowerCAmelCase		(		self			,					*_UpperCamelCase			,					**_UpperCamelCase    ):






  """simple docstring"""



  self.warning(*_UpperCamelCase			,					**_UpperCamelCase    )


A__ :       List[Any]				= warning_once


class       __magic_name__							:
       def __init__(  self			,					*A_			,					**A_							)  ->						Any:  # pylint: disable=unused-argument





         """simple docstring"""




         _lowercase:      Tuple   			=  args[0] if args else None
       def __iter__(  self							)  ->						Union[str, Any]:





         """simple docstring"""




         return iter(self._iterator							)
       def __getattr__(  self			,					A_							)  ->						List[Any]:





         """simple docstring"""




         def empty_fn(*A_			,					**A_							):  # pylint: disable=unused-argument
           return

         return empty_fn
       def __enter__(  self							)  ->						Tuple:





         """simple docstring"""




         return self



       def __exit__(  self			,					A_			,					A_			,					A_							)  ->						Optional[Any]:





         """simple docstring"""




         return


class       __magic_name__							:
       def __call__(  self			,					*A_			,					**A_							)  ->						Dict:





         """simple docstring"""




         if _tqdm_active:
           return tqdm_lib.tqdm(*A_			,					**A_							)
         else:
           return EmptyTqdm(*A_			,					**A_							)
       def 				lowercase_				(  self			,					*A_			,					**A_							)  ->						List[str]:





         """simple docstring"""




         _lowercase:      Optional[Any]   			=  None
         if _tqdm_active:
           return tqdm_lib.tqdm.set_lock(*A_			,					**A_							)



       def 				lowercase_				(  self							)  ->						Optional[Any]:





         """simple docstring"""




         if _tqdm_active:
           return tqdm_lib.tqdm.get_lock()

A__ :       str				= _tqdm_cls()




def 			_lowerCAmelCase		(		):






  """simple docstring"""



  global _tqdm_active
  return bool(_tqdm_active    )




def 			_lowerCAmelCase		(		):






  """simple docstring"""



  global _tqdm_active
  _lowercase:      str   			=  True
  hf_hub_utils.enable_progress_bars()







def 			_lowerCAmelCase		(		):






  """simple docstring"""



  global _tqdm_active
  _lowercase:      Union[str, Any]   			=  False
  hf_hub_utils.disable_progress_bars()
