





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

UpperCamelCase__ :			Any       	=			{"""vocab_file""": """vocab.txt""", """tokenizer_file""": """tokenizer.json"""}

UpperCamelCase__ :			Any       	=			{
    """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__ :			Union[str, Any]       	=			{
    """microsoft/layoutlm-base-uncased""": 5_12,
    """microsoft/layoutlm-large-uncased""": 5_12,
}

UpperCamelCase__ :			Any       	=			{
    """microsoft/layoutlm-base-uncased""": {"""do_lower_case""": True},
    """microsoft/layoutlm-large-uncased""": {"""do_lower_case""": True},
}







class 	_UpperCamelCase	(							snake_case_	):







					'''simple docstring'''

					lowerCamelCase  :  str           =							VOCAB_FILES_NAMES
					lowerCamelCase  :  Tuple           =							PRETRAINED_VOCAB_FILES_MAP
					lowerCamelCase  :  Any           =							PRETRAINED_INIT_CONFIGURATION
					lowerCamelCase  :  Any           =							PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
					lowerCamelCase  :  str           =							LayoutLMTokenizer





					def __init__(    self     :	Any			,       __lowercase     :	List[str]=None			,       __lowercase     :	Dict=None			,       __lowercase     :	Dict=True			,       __lowercase     :	Tuple="[UNK]"			,       __lowercase     :	Optional[Any]="[SEP]"			,       __lowercase     :	Dict="[PAD]"			,       __lowercase     :	Any="[CLS]"			,       __lowercase     :	str="[MASK]"			,       __lowercase     :	Union[str, Any]=True			,       __lowercase     :	List[str]=None			,       **__lowercase     :	Optional[Any]			,       ):






						'''simple docstring'''







						super().__init__(
						    __lowercase			,       tokenizer_file=__lowercase			,       do_lower_case=__lowercase			,       unk_token=__lowercase			,       sep_token=__lowercase			,       pad_token=__lowercase			,       cls_token=__lowercase			,       mask_token=__lowercase			,       tokenize_chinese_chars=__lowercase			,       strip_accents=__lowercase			,       **__lowercase			,       )

						UpperCAmelCase_		    =  json.loads(self.backend_tokenizer.normalizer.__getstate__()		)
						if (
						    normalizer_state.get("""lowercase"""			,       __lowercase		) != do_lower_case
						    or normalizer_state.get("""strip_accents"""			,       __lowercase		) != strip_accents
						    or normalizer_state.get("""handle_chinese_chars"""			,       __lowercase		) != tokenize_chinese_chars
						):
							UpperCAmelCase_		    =  getattr(__lowercase			,       normalizer_state.pop("""type"""		)		)
							UpperCAmelCase_		    =  do_lower_case
							UpperCAmelCase_		    =  strip_accents
							UpperCAmelCase_		    =  tokenize_chinese_chars
							UpperCAmelCase_		    =  normalizer_class(**__lowercase		)

						UpperCAmelCase_		    =  do_lower_case





					def 							SCREAMING_SNAKE_CASE				(    self     :	Tuple			,       __lowercase     :	int			,       __lowercase     :	Union[str, Any]=None		):






						'''simple docstring'''







						UpperCAmelCase_		    =  [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 							SCREAMING_SNAKE_CASE				(    self     :	Union[str, Any]			,       __lowercase     :	Optional[int]			,       __lowercase     :	Dict = None		):






						'''simple docstring'''







						UpperCAmelCase_		    =  [self.sep_token_id]
						UpperCAmelCase_		    =  [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 							SCREAMING_SNAKE_CASE				(    self     :	Optional[Any]			,       __lowercase     :	Tuple			,       __lowercase     :	int = None		):






						'''simple docstring'''







						UpperCAmelCase_		    =  self._tokenizer.model.save(__lowercase			,       name=__lowercase		)
						return tuple(__lowercase		)






import argparse

import torch
from clip import load

from transformers import CLIPConfig, CLIPModel




def SCREAMING_SNAKE_CASE_  (				__lowerCamelCase:		Optional[int]  ,			__lowerCamelCase:		Optional[int]  ):







	'''simple docstring'''



	lowercase_ ,	lowercase_ ,	lowercase_			 = pt_attn_layer.in_proj_weight.chunk(3  ,			dim=0  )
	lowercase_ ,	lowercase_ ,	lowercase_			 = pt_attn_layer.in_proj_bias.chunk(3  ,			dim=0  )

	lowercase_			 = pt_attn_layer.out_proj.weight
	lowercase_			 = pt_attn_layer.out_proj.bias

	lowercase_			 = q_proj
	lowercase_			 = q_proj_bias

	lowercase_			 = k_proj
	lowercase_			 = k_proj_bias

	lowercase_			 = v_proj
	lowercase_			 = v_proj_bias

	lowercase_			 = out_proj_weights
	lowercase_			 = out_proj_bias




def SCREAMING_SNAKE_CASE_  (				__lowerCamelCase:		int  ,			__lowerCamelCase:		List[str]  ):







	'''simple docstring'''



	copy_linear(hf_mlp.fca  ,			pt_mlp.c_fc  )
	copy_linear(hf_mlp.fca  ,			pt_mlp.c_proj  )




def SCREAMING_SNAKE_CASE_  (				__lowerCamelCase:		List[str]  ,			__lowerCamelCase:		Dict  ):







	'''simple docstring'''



	lowercase_			 = pt_linear.weight
	lowercase_			 = pt_linear.bias




def SCREAMING_SNAKE_CASE_  (				__lowerCamelCase:		int  ,			__lowerCamelCase:		Any  ):







	'''simple docstring'''



	copy_linear(hf_layer.layer_norma  ,			pt_layer.ln_a  )
	copy_linear(hf_layer.layer_norma  ,			pt_layer.ln_a  )

	# copy MLP
	copy_mlp(hf_layer.mlp  ,			pt_layer.mlp  )

	# copy attn
	copy_attn_layer(hf_layer.self_attn  ,			pt_layer.attn  )




def SCREAMING_SNAKE_CASE_  (				__lowerCamelCase:		str  ,			__lowerCamelCase:		Optional[int]  ):







	'''simple docstring'''



	for hf_layer, pt_layer in zip(__lowerCamelCase  ,			__lowerCamelCase  ):
		copy_layer(__lowerCamelCase  ,			__lowerCamelCase  )




def SCREAMING_SNAKE_CASE_  (				__lowerCamelCase:		Optional[Any]  ,			__lowerCamelCase:		str  ):







	'''simple docstring'''



	lowercase_			 = pt_model.token_embedding.weight
	lowercase_			 = pt_model.positional_embedding

	# copy layer norm
	copy_linear(hf_encoder.final_layer_norm  ,			pt_model.ln_final  )

	# copy hidden layers
	copy_layers(hf_encoder.encoder.layers  ,			pt_model.transformer.resblocks  )




def SCREAMING_SNAKE_CASE_  (				__lowerCamelCase:		Optional[Any]  ,			__lowerCamelCase:		Any  ):







	'''simple docstring'''



	lowercase_			 = pt_model.text_projection.data.T

	# copy text encoder
	copy_encoder(hf_model.text_model  ,			__lowerCamelCase  )




def SCREAMING_SNAKE_CASE_  (				__lowerCamelCase:		List[Any]  ,			__lowerCamelCase:		List[Any]  ):







	'''simple docstring'''



	lowercase_			 = pt_model.visual.proj.data.T

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

	# copy embeds
	lowercase_			 = pt_model.visual.conva.weight.data
	lowercase_			 = pt_model.visual.class_embedding
	lowercase_			 = pt_model.visual.positional_embedding.data

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




@torch.no_grad()
def SCREAMING_SNAKE_CASE_  (				__lowerCamelCase:		int  ,			__lowerCamelCase:		Dict  ,			__lowerCamelCase:		Dict=None  ):







	'''simple docstring'''



	if config_path is not None:
		lowercase_			 = CLIPConfig.from_pretrained(__lowerCamelCase  )
	else:
		lowercase_			 = CLIPConfig(projection_dim=512  ,			text_config={}  ,			vision_config={}  )

	lowercase_			 = CLIPModel(__lowerCamelCase  ).eval()

	lowercase_ ,	lowercase_			 = load(__lowerCamelCase  ,			device="cpu"  ,			jit=__lowerCamelCase  )
	lowercase_			 = pt_model.eval()

	copy_text_model_and_projection(__lowerCamelCase  ,			__lowerCamelCase  )
	copy_vison_model_and_projection(__lowerCamelCase  ,			__lowerCamelCase  )
	lowercase_			 = pt_model.logit_scale

	lowercase_			 = torch.arange(0  ,			77  ).unsqueeze(0  )
	lowercase_			 = torch.randn(1  ,			3  ,			224  ,			224  )

	lowercase_			 = hf_model(input_ids=__lowerCamelCase  ,			pixel_values=__lowerCamelCase  ,			return_dict=__lowerCamelCase  )
	lowercase_			 = hf_outputs.logits_per_image
	lowercase_			 = hf_outputs.logits_per_text
	lowercase_ ,	lowercase_			 = pt_model(__lowerCamelCase  ,			__lowerCamelCase  )

	assert torch.allclose(__lowerCamelCase  ,			__lowerCamelCase  ,			atol=1E-3  )
	assert torch.allclose(__lowerCamelCase  ,			__lowerCamelCase  ,			atol=1E-3  )

	hf_model.save_pretrained(__lowerCamelCase  )


if __name__ == "__main__":
							SCREAMING_SNAKE_CASE__       			=				argparse.ArgumentParser()
							parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""")
							parser.add_argument("""--checkpoint_path""", default=None, type=str, help="""Path to fairseq checkpoint""")
							parser.add_argument("""--config_path""", default=None, type=str, help="""Path to hf config.json of model to convert""")
							SCREAMING_SNAKE_CASE__       			=				parser.parse_args()

							convert_clip_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path)

