




import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_layoutlm import LayoutLMTokenizer


UpperCAmelCase__    :							Tuple       		=     logging.get_logger(__name__)

UpperCAmelCase__    :							Optional[Any]       		=     {'vocab_file': 'vocab.txt', 'tokenizer_file': 'tokenizer.json'}

UpperCAmelCase__    :							Dict       		=     {
    'vocab_file': {
        'microsoft/layoutlm-base-uncased': (
            'https://huggingface.co/microsoft/layoutlm-base-uncased/resolve/main/vocab.txt'
        ),
        'microsoft/layoutlm-large-uncased': (
            'https://huggingface.co/microsoft/layoutlm-large-uncased/resolve/main/vocab.txt'
        ),
    },
    'tokenizer_file': {
        'microsoft/layoutlm-base-uncased': (
            'https://huggingface.co/microsoft/layoutlm-base-uncased/resolve/main/tokenizer.json'
        ),
        'microsoft/layoutlm-large-uncased': (
            'https://huggingface.co/microsoft/layoutlm-large-uncased/resolve/main/tokenizer.json'
        ),
    },
}

UpperCAmelCase__    :							Tuple       		=     {
    'microsoft/layoutlm-base-uncased': 512,
    'microsoft/layoutlm-large-uncased': 512,
}

UpperCAmelCase__    :							Union[str, Any]       		=     {
    'microsoft/layoutlm-base-uncased': {'do_lower_case': True},
    'microsoft/layoutlm-large-uncased': {'do_lower_case': True},
}

class   UpperCAmelCase    (      a_ ):
   __SCREAMING_SNAKE_CASE		:				Tuple      					=       VOCAB_FILES_NAMES
   __SCREAMING_SNAKE_CASE		:				Tuple      					=       PRETRAINED_VOCAB_FILES_MAP
   __SCREAMING_SNAKE_CASE		:				Tuple      					=       PRETRAINED_INIT_CONFIGURATION
   __SCREAMING_SNAKE_CASE		:				Dict      					=       PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
   __SCREAMING_SNAKE_CASE		:				Dict      					=       LayoutLMTokenizer






   def __init__(						self :							Dict    ,       UpperCamelCase_ :							Tuple=None    ,       UpperCamelCase_ :							Optional[int]=None    ,       UpperCamelCase_ :							Union[str, Any]=True    ,       UpperCamelCase_ :							Dict="[UNK]"    ,       UpperCamelCase_ :							Any="[SEP]"    ,       UpperCamelCase_ :							Optional[Any]="[PAD]"    ,       UpperCamelCase_ :							str="[CLS]"    ,       UpperCamelCase_ :							Any="[MASK]"    ,       UpperCamelCase_ :							Union[str, Any]=True    ,       UpperCamelCase_ :							Union[str, Any]=None    ,       **UpperCamelCase_ :							Optional[Any]    ,       )->      Dict:
     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_    ,       )

     _lowercase:			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
     ):
       _lowercase:			str      =     getattr(UpperCamelCase_    ,       normalizer_state.pop('type'  )  )
       _lowercase:			Optional[int]      =     do_lower_case
       _lowercase:			Tuple      =     strip_accents
       _lowercase:			int      =     tokenize_chinese_chars
       _lowercase:			Union[str, Any]      =     normalizer_class(**UpperCamelCase_  )

     _lowercase:			Optional[int]      =     do_lower_case






   def 			_a    (						self :							Dict    ,       UpperCamelCase_ :							int    ,       UpperCamelCase_ :							List[Any]=None  )->      str:
     _lowercase:			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 :							Optional[Any]    ,       UpperCamelCase_ :							List[int]    ,       UpperCamelCase_ :							Optional[List[int]] = None  )->      List[int]:
     _lowercase:			Union[str, Any]      =     [self.sep_token_id]
     _lowercase:			Dict      =     [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 :							List[str]    ,       UpperCamelCase_ :							str    ,       UpperCamelCase_ :							Optional[str] = None  )->      Tuple[str]:
     _lowercase:			int      =     self._tokenizer.model.save(UpperCamelCase_    ,       name=UpperCamelCase_  )
     return tuple(UpperCamelCase_  )

import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel


def SCREAMING_SNAKE_CASE					(  lowercase_:						List[str]		,    lowercase_:						Dict		,    lowercase_:						List[Any]					):
       _UpperCamelCase    ,   _UpperCamelCase    ,   _UpperCamelCase  :							List[Any]	    =	pt_weights[F"{prefix}.in_proj_weight"].chunk(3		,    dim=0					)
       _UpperCamelCase    ,   _UpperCamelCase    ,   _UpperCamelCase  :							Dict	    =	pt_weights[F"{prefix}.in_proj_bias"].chunk(3		,    dim=0					)

       _UpperCamelCase  :							List[str]	    =	pt_weights[F"{prefix}.out_proj.weight"]
       _UpperCamelCase  :							Any	    =	pt_weights[F"{prefix}.out_proj.bias"]

       _UpperCamelCase  :							List[Any]	    =	q_proj
       _UpperCamelCase  :							Union[str, Any]	    =	q_proj_bias

       _UpperCamelCase  :							str	    =	k_proj
       _UpperCamelCase  :							List[str]	    =	k_proj_bias

       _UpperCamelCase  :							Dict	    =	v_proj
       _UpperCamelCase  :							str	    =	v_proj_bias

       _UpperCamelCase  :							List[str]	    =	out_proj_weights
       _UpperCamelCase  :							Any	    =	out_proj_bias


def SCREAMING_SNAKE_CASE					(  lowercase_:						List[str]		,    lowercase_:						List[Any]		,    lowercase_:						Any					):
       copy_linear(hf_mlp.fca		,    lowercase_		,    F"{prefix}.c_fc"					)
       copy_linear(hf_mlp.fca		,    lowercase_		,    F"{prefix}.c_proj"					)


def SCREAMING_SNAKE_CASE					(  lowercase_:						Optional[Any]		,    lowercase_:						List[str]		,    lowercase_:						Any					):
       _UpperCamelCase  :							Dict	    =	pt_weights[F"{prefix}.weight"].data
       _UpperCamelCase  :							List[Any]	    =	pt_weights[F"{prefix}.bias"].data


def SCREAMING_SNAKE_CASE					(  lowercase_:						Any		,    lowercase_:						Optional[int]		,    lowercase_:						Union[str, Any]					):
       # copy layer norms
       copy_linear(hf_layer.layer_norma		,    lowercase_		,    F"{prefix}.ln_1"					)
       copy_linear(hf_layer.layer_norma		,    lowercase_		,    F"{prefix}.ln_2"					)

       # copy MLP
       copy_mlp(hf_layer.mlp		,    lowercase_		,    F"{prefix}.mlp"					)

       # copy attn
       copy_attn_layer(hf_layer.self_attn		,    lowercase_		,    F"{prefix}.attn"					)


def SCREAMING_SNAKE_CASE					(  lowercase_:						str		,    lowercase_:						str		,    lowercase_:						int					):
       for layer_id, hf_layer in enumerate(lowercase_					):
              copy_layer(lowercase_		,    lowercase_		,    F"{prefix}.{layer_id}"					)


def SCREAMING_SNAKE_CASE					(  lowercase_:						Union[str, Any]		,    lowercase_:						List[Any]					):
       # copy projection
       _UpperCamelCase  :							List[str]	    =	pt_weights['text_projection'].data.T

       # copy text encoder
       for name, param in hf_model.text_model.named_parameters():
              _UpperCamelCase  :							Tuple	    =	pt_weights[F"bert.{name}"].data


def SCREAMING_SNAKE_CASE					(  lowercase_:						str		,    lowercase_:						Any					):
       # copy projection
       _UpperCamelCase  :							List[Any]	    =	pt_weights['visual.proj'].data.T

       # copy layer norms
       copy_linear(hf_model.vision_model.pre_layrnorm		,    lowercase_		,    'visual.ln_pre'					)
       copy_linear(hf_model.vision_model.post_layernorm		,    lowercase_		,    'visual.ln_post'					)

       # copy embeddings
       _UpperCamelCase  :							List[Any]	    =	pt_weights['visual.conv1.weight'].data
       _UpperCamelCase  :							List[Any]	    =	pt_weights['visual.class_embedding'].data
       _UpperCamelCase  :							int	    =	pt_weights['visual.positional_embedding'].data

       # copy encoder
       copy_layers(hf_model.vision_model.encoder.layers		,    lowercase_		,    'visual.transformer.resblocks'					)


@torch.no_grad()
def SCREAMING_SNAKE_CASE					(  lowercase_:						Optional[Any]		,    lowercase_:						Optional[int]		,    lowercase_:						List[Any]=None					):
       assert config_path is not None, "Please specify the ChineseCLIP model config of the corresponding model size."
       _UpperCamelCase  :							Optional[Any]	    =	ChineseCLIPConfig.from_pretrained(lowercase_					)

       _UpperCamelCase  :							List[Any]	    =	ChineseCLIPModel(lowercase_					).eval()

       _UpperCamelCase  :							List[Any]	    =	torch.load(lowercase_		,    map_location='cpu'					)['state_dict']
       _UpperCamelCase  :							Union[str, Any]	    =	{(name[7:] if name.startswith('module.'					) else name): value for name, value in pt_weights.items()}

       copy_text_model_and_projection(lowercase_		,    lowercase_					)
       copy_vision_model_and_projection(lowercase_		,    lowercase_					)
       _UpperCamelCase  :							List[Any]	    =	pt_weights['logit_scale'].data

       hf_model.save_pretrained(lowercase_					)


if __name__ == "__main__":
 UpperCAmelCase      		=					argparse.ArgumentParser()
 parser.add_argument(
     '''--pytorch_dump_folder_path''',
     default=None,
     type=str,
     help='''Path to the output folder storing converted hf PyTorch model.''',
 )
 parser.add_argument(
     '''--checkpoint_path''', default=None, type=str, help='''Path to original github format ChineseCLIP checkpoint.'''
 )
 parser.add_argument(
     '''--config_path''', default=None, required=True, type=str, help='''Path to hf config.json of model to convert.'''
 )
 UpperCAmelCase      		=					parser.parse_args()

 convert_chinese_clip_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path)
 print('''The conversion is finished!''')



