





'''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__				:   Tuple      =logging.get_logger(__name__)
def    UpperCamelCase__      (		lowerCAmelCase							,	lowerCAmelCase=False  ):





      """simple docstring"""


      _lowerCAmelCase							     =							[]
      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"
            _lowerCAmelCase							     =							[(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    UpperCamelCase__      (		lowerCAmelCase							,	lowerCAmelCase							,	lowerCAmelCase=False  ):





      """simple docstring"""


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





      """simple docstring"""


      _lowerCAmelCase							     =							["""head.weight""", """head.bias"""]
      for k in ignore_keys:
            state_dict.pop(UpperCAmelCase_							,	UpperCAmelCase_  )
def    UpperCamelCase__      (		lowerCAmelCase							,	lowerCAmelCase							,	lowerCAmelCase  ):





      """simple docstring"""


      _lowerCAmelCase							     =							dct.pop(UpperCAmelCase_  )
      _lowerCAmelCase							     =							val
def    UpperCamelCase__      (		):





      """simple docstring"""


      _lowerCAmelCase							     =							"""http://images.cocodataset.org/val2017/000000039769.jpg"""
      _lowerCAmelCase							     =							Image.open(requests.get(UpperCAmelCase_							,	stream=UpperCAmelCase_  ).raw  )
      return im
@torch.no_grad()
def    UpperCamelCase__      (		lowerCAmelCase							,	lowerCAmelCase							,	lowerCAmelCase=True  ):





      """simple docstring"""


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

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

      # load state_dict of original model, remove and rename some keys
      _lowerCAmelCase							     =							original_model.state_dict()
      if base_model:
            remove_classification_head_(UpperCAmelCase_  )
      _lowerCAmelCase							     =							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:
            _lowerCAmelCase							     =							ViTModel(UpperCAmelCase_							,	add_pooling_layer=UpperCAmelCase_  ).eval()
      else:
            _lowerCAmelCase							     =							ViTForImageClassification(UpperCAmelCase_  ).eval()
      model.load_state_dict(UpperCAmelCase_  )

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

      if base_model:
            _lowerCAmelCase							     =							original_model(UpperCAmelCase_  )
            assert torch.allclose(UpperCAmelCase_							,	outputs.last_hidden_state[:, 0, :]							,	atol=1e-1  )
      else:
            _lowerCAmelCase							     =							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__				:   Optional[Any]      =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__				:   Optional[Any]      =parser.parse_args()
      convert_vit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.base_model)










'''simple docstring'''

import os
import unicodedata
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple

import sentencepiece as spm

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import SPIECE_UNDERLINE, logging


a_  :				Any						= logging.get_logger(__name__)

a_  :				Optional[Any]						= {"""vocab_file""": """spiece.model"""}

a_  :				Tuple						= {
    """vocab_file""": {
        """TsinghuaAI/CPM-Generate""": """https://huggingface.co/TsinghuaAI/CPM-Generate/resolve/main/spiece.model""",
    }
}





class      snake_case    (    lowercase			):





  """simple docstring"""




  def __init__(    self			,  UpperCamelCase			,  UpperCamelCase=False			,  UpperCamelCase=True			,  UpperCamelCase=False			,  UpperCamelCase="<s>"			,  UpperCamelCase="</s>"			,  UpperCamelCase="<unk>"			,  UpperCamelCase="<sep>"			,  UpperCamelCase="<pad>"			,  UpperCamelCase="<cls>"			,  UpperCamelCase="<mask>"			,  UpperCamelCase=["<eop>", "<eod>"]			,  UpperCamelCase = None			,  **UpperCamelCase			,  ):







   """simple docstring"""
   lowerCamelCase_      =  AddedToken(UpperCamelCase			,  lstrip=UpperCamelCase			,  rstrip=UpperCamelCase     ) if isinstance(UpperCamelCase			,  UpperCamelCase     ) else mask_token

   lowerCamelCase_      =  {} if sp_model_kwargs is None else sp_model_kwargs

   super().__init__(
       do_lower_case=UpperCamelCase			,  remove_space=UpperCamelCase			,  keep_accents=UpperCamelCase			,  bos_token=UpperCamelCase			,  eos_token=UpperCamelCase			,  unk_token=UpperCamelCase			,  sep_token=UpperCamelCase			,  pad_token=UpperCamelCase			,  cls_token=UpperCamelCase			,  mask_token=UpperCamelCase			,  additional_special_tokens=UpperCamelCase			,  sp_model_kwargs=self.sp_model_kwargs			,  **UpperCamelCase			,  )

   lowerCamelCase_      =  3

   lowerCamelCase_      =  do_lower_case
   lowerCamelCase_      =  remove_space
   lowerCamelCase_      =  keep_accents
   lowerCamelCase_      =  vocab_file

   lowerCamelCase_      =  spm.SentencePieceProcessor(**self.sp_model_kwargs     )
   self.sp_model.Load(UpperCamelCase     )

   try:
    import jieba
   except ModuleNotFoundError as error:
    raise error.__class__(
        "You need to install jieba to use CpmTokenizer or CpmTokenizerFast. "
        "See https://pypi.org/project/jieba/ for installation."     )
   lowerCamelCase_      =  jieba
   lowerCamelCase_      =  str.maketrans(" \n"			,  "\u2582\u2583"     )






  @property
  # Copied from transformers.models.xlnet.tokenization_xlnet.XLNetTokenizer.vocab_size
  def   snake_case		(    self     ):







   """simple docstring"""
   return len(self.sp_model     )






  def   snake_case		(    self     ):







   """simple docstring"""
   lowerCamelCase_      =  {self.convert_ids_to_tokens(UpperCamelCase     ): i for i in range(self.vocab_size     )}
   vocab.update(self.added_tokens_encoder     )
   return vocab






  def __getstate__(    self     ):







   """simple docstring"""
   lowerCamelCase_      =  self.__dict__.copy()
   lowerCamelCase_      =  None
   return state






  def __setstate__(    self			,  UpperCamelCase     ):







   """simple docstring"""
   lowerCamelCase_      =  d

   # for backward compatibility
   if not hasattr(self			,  "sp_model_kwargs"     ):
    lowerCamelCase_      =  {}

   lowerCamelCase_      =  spm.SentencePieceProcessor(**self.sp_model_kwargs     )
   self.sp_model.Load(self.vocab_file     )






  def   snake_case		(    self			,  UpperCamelCase     ):







   """simple docstring"""
   if self.remove_space:
    lowerCamelCase_      =  " ".join(inputs.strip().split()     )
   else:
    lowerCamelCase_      =  inputs
   lowerCamelCase_      =  outputs.replace("``"			,  "\""     ).replace("''"			,  "\""     )

   if not self.keep_accents:
    lowerCamelCase_      =  unicodedata.normalize("NFKD"			,  UpperCamelCase     )
    lowerCamelCase_      =  "".join([c for c in outputs if not unicodedata.combining(UpperCamelCase     )]     )
   if self.do_lower_case:
    lowerCamelCase_      =  outputs.lower()

   return outputs






  def   snake_case		(    self			,  UpperCamelCase     ):







   """simple docstring"""
   lowerCamelCase_      =  self.preprocess_text(UpperCamelCase     )
   lowerCamelCase_      =  self.sp_model.encode(UpperCamelCase			,  out_type=UpperCamelCase     )
   lowerCamelCase_      =  []
   for piece in pieces:
    if len(UpperCamelCase     ) > 1 and piece[-1] == str(","     ) and piece[-2].isdigit():
     lowerCamelCase_      =  self.sp_model.EncodeAsPieces(piece[:-1].replace(UpperCamelCase			,  ""     )     )
     if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE:
      if len(cur_pieces[0]     ) == 1:
       lowerCamelCase_      =  cur_pieces[1:]
      else:
       lowerCamelCase_      =  cur_pieces[0][1:]
     cur_pieces.append(piece[-1]     )
     new_pieces.extend(UpperCamelCase     )
    else:
     new_pieces.append(UpperCamelCase     )

   return new_pieces






  def   snake_case		(    self			,  UpperCamelCase     ):







   """simple docstring"""
   return self.sp_model.PieceToId(UpperCamelCase     )






  def   snake_case		(    self			,  UpperCamelCase     ):







   """simple docstring"""
   return self.sp_model.IdToPiece(UpperCamelCase     )






  def   snake_case		(    self			,  UpperCamelCase     ):







   """simple docstring"""
   lowerCamelCase_      =  "".join(UpperCamelCase     ).replace(UpperCamelCase			,  " "     ).strip()
   return out_string






  def   snake_case		(    self			,  UpperCamelCase			,  UpperCamelCase = None     ):







   """simple docstring"""
   lowerCamelCase_      =  [self.sep_token_id]
   lowerCamelCase_      =  [self.cls_token_id]
   if token_ids_a is None:
    return token_ids_a + sep + cls
   return token_ids_a + sep + token_ids_a + sep + cls






  def   snake_case		(    self			,  UpperCamelCase			,  UpperCamelCase = None			,  UpperCamelCase = False     ):







   """simple docstring"""

   if already_has_special_tokens:
    return super().get_special_tokens_mask(
        token_ids_a=UpperCamelCase			,  token_ids_a=UpperCamelCase			,  already_has_special_tokens=UpperCamelCase     )

   if token_ids_a is not None:
    return ([0] * len(UpperCamelCase     )) + [1] + ([0] * len(UpperCamelCase     )) + [1, 1]
   return ([0] * len(UpperCamelCase     )) + [1, 1]






  def   snake_case		(    self			,  UpperCamelCase			,  UpperCamelCase = None     ):







   """simple docstring"""
   lowerCamelCase_      =  [self.sep_token_id]
   lowerCamelCase_      =  [2]

   if token_ids_a is None:
    return len(token_ids_a + sep     ) * [0] + cls_segment_id
   return len(token_ids_a + sep     ) * [0] + len(token_ids_a + sep     ) * [1] + cls_segment_id






  def   snake_case		(    self			,  UpperCamelCase			,  UpperCamelCase = None     ):







   """simple docstring"""
   if not os.path.isdir(UpperCamelCase     ):
    logger.error(f'''Vocabulary path ({save_directory}) should be a directory'''     )
    return
   lowerCamelCase_      =  os.path.join(
       UpperCamelCase			,  (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]     )

   if os.path.abspath(self.vocab_file     ) != os.path.abspath(UpperCamelCase     ) and os.path.isfile(self.vocab_file     ):
    copyfile(self.vocab_file			,  UpperCamelCase     )
   elif not os.path.isfile(self.vocab_file     ):
    with open(UpperCamelCase			,  "wb"     ) as fi:
     lowerCamelCase_      =  self.sp_model.serialized_model_proto()
     fi.write(UpperCamelCase     )

   return (out_vocab_file,)







  def   snake_case		(    self			,  *UpperCamelCase			,  **UpperCamelCase     ):







   """simple docstring"""
   lowerCamelCase_      =  super()._decode(*UpperCamelCase			,  **UpperCamelCase     )
   lowerCamelCase_      =  text.replace(" "			,  ""     ).replace("\u2582"			,  " "     ).replace("\u2583"			,  "\n"     )
   return text



