
"""simple docstring"""

import argparse
import json
from pathlib import Path

import requests
import torch
from huggingface_hub import cached_download, hf_hub_url
from PIL import Image

from transformers import DeformableDetrConfig, DeformableDetrForObjectDetection, DeformableDetrImageProcessor
from transformers.utils import logging


logging.set_verbosity_info()
_A				=					logging.get_logger(__name__)
def 						UpperCAmelCase   (						a_     ):





		'''simple docstring'''


		if "backbone.0.body" in orig_key:
				lowerCamelCase	:     Dict            =       orig_key.replace('backbone.0.body',		'backbone.conv_encoder.model'     )
		if "transformer" in orig_key:
				lowerCamelCase	:     Tuple            =       orig_key.replace('transformer.',		''     )
		if "norm1" in orig_key:
				if "encoder" in orig_key:
						lowerCamelCase	:     List[str]            =       orig_key.replace('norm1',		'self_attn_layer_norm'     )
				else:
						lowerCamelCase	:     List[Any]            =       orig_key.replace('norm1',		'encoder_attn_layer_norm'     )
		if "norm2" in orig_key:
				if "encoder" in orig_key:
						lowerCamelCase	:     Tuple            =       orig_key.replace('norm2',		'final_layer_norm'     )
				else:
						lowerCamelCase	:     Any            =       orig_key.replace('norm2',		'self_attn_layer_norm'     )
		if "norm3" in orig_key:
				lowerCamelCase	:     Tuple            =       orig_key.replace('norm3',		'final_layer_norm'     )
		if "linear1" in orig_key:
				lowerCamelCase	:     Any            =       orig_key.replace('linear1',		'fc1'     )
		if "linear2" in orig_key:
				lowerCamelCase	:     List[Any]            =       orig_key.replace('linear2',		'fc2'     )
		if "query_embed" in orig_key:
				lowerCamelCase	:     str            =       orig_key.replace('query_embed',		'query_position_embeddings'     )
		if "cross_attn" in orig_key:
				lowerCamelCase	:     Tuple            =       orig_key.replace('cross_attn',		'encoder_attn'     )

		return orig_key
def 						UpperCAmelCase   (						a_     ):





		'''simple docstring'''


		for i in range(6     ):
				# read in weights + bias of input projection layer of self-attention
				lowerCamelCase	:     Tuple            =       state_dict.pop(F"""decoder.layers.{i}.self_attn.in_proj_weight"""     )
				lowerCamelCase	:     Any            =       state_dict.pop(F"""decoder.layers.{i}.self_attn.in_proj_bias"""     )
				# next, add query, keys and values (in that order) to the state dict
				lowerCamelCase	:     str            =       in_proj_weight[:256, :]
				lowerCamelCase	:     List[Any]            =       in_proj_bias[:256]
				lowerCamelCase	:     Tuple            =       in_proj_weight[256:512, :]
				lowerCamelCase	:     Optional[int]            =       in_proj_bias[256:512]
				lowerCamelCase	:     Optional[Any]            =       in_proj_weight[-256:, :]
				lowerCamelCase	:     Dict            =       in_proj_bias[-256:]
def 						UpperCAmelCase   (						):





		'''simple docstring'''


		lowerCamelCase	:     Union[str, Any]            =       """http://images.cocodataset.org/val2017/000000039769.jpg"""
		lowerCamelCase	:     Dict            =       Image.open(requests.get(_A,		stream=_A     ).raw     )

		return im
@torch.no_grad()
def 						UpperCAmelCase   (						a_,		a_,		a_,		a_,		a_,		a_,		a_,		):





		'''simple docstring'''


		lowerCamelCase	:     List[str]            =       DeformableDetrConfig()
		# set config attributes
		if single_scale:
				lowerCamelCase	:     Optional[int]            =       1
		lowerCamelCase	:     Optional[int]            =       dilation
		lowerCamelCase	:     int            =       with_box_refine
		lowerCamelCase	:     Any            =       two_stage
		# set labels
		lowerCamelCase	:     str            =       91
		lowerCamelCase	:     int            =       """huggingface/label-files"""
		lowerCamelCase	:     Optional[int]            =       """coco-detection-id2label.json"""
		lowerCamelCase	:     Dict            =       json.load(open(cached_download(hf_hub_url(_A,		_A,		repo_type='dataset'     )     ),		'r'     )     )
		lowerCamelCase	:     List[str]            =       {int(_A     ): v for k, v in idalabel.items()}
		lowerCamelCase	:     Tuple            =       idalabel
		lowerCamelCase	:     Optional[Any]            =       {v: k for k, v in idalabel.items()}

		# load image processor
		lowerCamelCase	:     List[str]            =       DeformableDetrImageProcessor(format='coco_detection'     )

		# prepare image
		lowerCamelCase	:     Union[str, Any]            =       prepare_img()
		lowerCamelCase	:     Tuple            =       image_processor(images=_A,		return_tensors='pt'     )
		lowerCamelCase	:     Union[str, Any]            =       encoding["""pixel_values"""]

		logger.info('Converting model...'     )

		# load original state dict
		lowerCamelCase	:     Any            =       torch.load(_A,		map_location='cpu'     )["""model"""]
		# rename keys
		for key in state_dict.copy().keys():
				lowerCamelCase	:     Optional[Any]            =       state_dict.pop(_A     )
				lowerCamelCase	:     Union[str, Any]            =       val
		# query, key and value matrices need special treatment
		read_in_q_k_v(_A     )
		# important: we need to prepend a prefix to each of the base model keys as the head models use different attributes for them
		lowerCamelCase	:     str            =       """model."""
		for key in state_dict.copy().keys():
				if not key.startswith('class_embed'     ) and not key.startswith('bbox_embed'     ):
						lowerCamelCase	:     Optional[int]            =       state_dict.pop(_A     )
						lowerCamelCase	:     int            =       val
    # finally, create HuggingFace model and load state dict
		lowerCamelCase	:     int            =       DeformableDetrForObjectDetection(_A     )
		model.load_state_dict(_A     )
		model.eval()

		lowerCamelCase	:     Any            =       """cuda""" if torch.cuda.is_available() else """cpu"""
		model.to(_A     )
		# verify our conversion
		lowerCamelCase	:     Optional[int]            =       model(pixel_values.to(_A     )     )

		lowerCamelCase	:     Any            =       torch.tensor(
		    [[-9.6_6_4_5, -4.3_4_4_9, -5.8_7_0_5], [-9.7_0_3_5, -3.8_5_0_4, -5.0_7_2_4], [-1_0.5_6_3_4, -5.3_3_7_9, -7.5_1_1_6]]     )
		lowerCamelCase	:     Dict            =       torch.tensor([[0.8_6_9_3, 0.2_2_8_9, 0.2_4_9_2], [0.3_1_5_0, 0.5_4_8_9, 0.5_8_4_5], [0.5_5_6_3, 0.7_5_8_0, 0.8_5_1_8]]     )

		if single_scale:
				lowerCamelCase	:     Optional[int]            =       torch.tensor(
				    [[-9.9_0_5_1, -4.2_5_4_1, -6.4_8_5_2], [-9.6_9_4_7, -4.0_8_5_4, -6.8_0_3_3], [-1_0.0_6_6_5, -5.8_4_7_0, -7.7_0_0_3]]     )
				lowerCamelCase	:     str            =       torch.tensor([[0.7_2_9_2, 0.4_9_9_1, 0.5_5_3_2], [0.7_9_5_9, 0.2_4_2_6, 0.4_2_3_6], [0.7_5_8_2, 0.3_5_1_8, 0.4_4_5_1]]     )

		if single_scale and dilation:
				lowerCamelCase	:     Any            =       torch.tensor(
				    [[-8.9_6_5_2, -4.1_0_7_4, -5.6_6_3_5], [-9.0_5_9_6, -4.9_4_4_7, -6.6_0_7_5], [-1_0.1_1_7_8, -4.5_2_7_5, -6.2_6_7_1]]     )
				lowerCamelCase	:     List[Any]            =       torch.tensor([[0.7_6_6_5, 0.4_1_3_0, 0.4_7_6_9], [0.8_3_6_4, 0.1_8_4_1, 0.3_3_9_1], [0.6_2_6_1, 0.3_8_9_5, 0.7_9_7_8]]     )

		if with_box_refine:
				lowerCamelCase	:     List[str]            =       torch.tensor(
				    [[-8.8_8_9_5, -5.4_1_8_7, -6.8_1_5_3], [-8.4_7_0_6, -6.1_6_6_8, -7.6_1_8_4], [-9.0_0_4_2, -5.5_3_5_9, -6.9_1_4_1]]     )
				lowerCamelCase	:     Optional[int]            =       torch.tensor([[0.7_8_2_8, 0.2_2_0_8, 0.4_3_2_3], [0.0_8_9_2, 0.5_9_9_6, 0.1_3_1_9], [0.5_5_2_4, 0.6_3_8_9, 0.8_9_1_4]]     )

		if with_box_refine and two_stage:
				lowerCamelCase	:     Tuple            =       torch.tensor(
				    [[-6.7_1_0_8, -4.3_2_1_3, -6.3_7_7_7], [-8.9_0_1_4, -6.1_7_9_9, -6.7_2_4_0], [-6.9_3_1_5, -4.4_7_3_5, -6.2_2_9_8]]     )
				lowerCamelCase	:     Union[str, Any]            =       torch.tensor([[0.2_5_8_3, 0.5_4_9_9, 0.4_6_8_3], [0.7_6_5_2, 0.9_0_6_8, 0.4_8_8_2], [0.5_4_9_0, 0.2_7_6_3, 0.0_5_6_4]]     )

		print('Logits:',		outputs.logits[0, :3, :3]     )

		assert torch.allclose(outputs.logits[0, :3, :3],		expected_logits.to(_A     ),		atol=1E-4     )
		assert torch.allclose(outputs.pred_boxes[0, :3, :3],		expected_boxes.to(_A     ),		atol=1E-4     )

		print('Everything ok!'     )

		# Save model and image processor
		logger.info(F"""Saving PyTorch model and image processor to {pytorch_dump_folder_path}..."""     )
		Path(_A     ).mkdir(exist_ok=_A     )
		model.save_pretrained(_A     )
		image_processor.save_pretrained(_A     )

		# Push to hub
		if push_to_hub:
				lowerCamelCase	:     Union[str, Any]            =       """deformable-detr"""
				model_name += "-single-scale" if single_scale else ""
				model_name += "-dc5" if dilation else ""
				model_name += "-with-box-refine" if with_box_refine else ""
				model_name += "-two-stage" if two_stage else ""
				print('Pushing model to hub...'     )
				model.push_to_hub(repo_path_or_name=_A,		organization='nielsr',		commit_message='Add model'     )


if __name__ == "__main__":
					_A				=					argparse.ArgumentParser()

					parser.add_argument(
					    '--checkpoint_path',
					    type=str,
					    default='/home/niels/checkpoints/deformable_detr/r50_deformable_detr-checkpoint.pth',
					    help='Path to Pytorch checkpoint (.pth file) you\'d like to convert.',
					)
					parser.add_argument('--single_scale', action='store_true', help='Whether to set config.num_features_levels = 1.')
					parser.add_argument('--dilation', action='store_true', help='Whether to set config.dilation=True.')
					parser.add_argument('--with_box_refine', action='store_true', help='Whether to set config.with_box_refine=True.')
					parser.add_argument('--two_stage', action='store_true', help='Whether to set config.two_stage=True.')
					parser.add_argument(
					    '--pytorch_dump_folder_path',
					    default=None,
					    type=str,
					    required=True,
					    help='Path to the folder to output PyTorch model.',
					)
					parser.add_argument(
					    '--push_to_hub', action='store_true', help='Whether or not to push the converted model to the 🤗 hub.'
					)
					_A				=					parser.parse_args()
					convert_deformable_detr_checkpoint(
					    args.checkpoint_path,
					    args.single_scale,
					    args.dilation,
					    args.with_box_refine,
					    args.two_stage,
					    args.pytorch_dump_folder_path,
					    args.push_to_hub,
					)









import json
import os
import re
import warnings
from pathlib import Path
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple, Union

import sentencepiece

from ...tokenization_utils import PreTrainedTokenizer
from ...utils import logging


UpperCAmelCase__			:  List[Any]					     =     logging.get_logger(__name__)

UpperCAmelCase__			:  int					     =     {
    """source_spm""": """source.spm""",
    """target_spm""": """target.spm""",
    """vocab""": """vocab.json""",
    """target_vocab_file""": """target_vocab.json""",
    """tokenizer_config_file""": """tokenizer_config.json""",
}

UpperCAmelCase__			:  Tuple					     =     {
    """source_spm""": {
        """Helsinki-NLP/opus-mt-en-de""": """https://huggingface.co/Helsinki-NLP/opus-mt-en-de/resolve/main/source.spm"""
    },
    """target_spm""": {
        """Helsinki-NLP/opus-mt-en-de""": """https://huggingface.co/Helsinki-NLP/opus-mt-en-de/resolve/main/target.spm"""
    },
    """vocab""": {
        """Helsinki-NLP/opus-mt-en-de""": """https://huggingface.co/Helsinki-NLP/opus-mt-en-de/resolve/main/vocab.json"""
    },
    """tokenizer_config_file""": {
        """Helsinki-NLP/opus-mt-en-de""": (
            """https://huggingface.co/Helsinki-NLP/opus-mt-en-de/resolve/main/tokenizer_config.json"""
        )
    },
}

UpperCAmelCase__			:  int					     =     {"""Helsinki-NLP/opus-mt-en-de""": 512}
UpperCAmelCase__			:  Union[str, Any]					     =     {}







class 							a__							( UpperCAmelCase							):







  """simple docstring"""




  UpperCAmelCase__   :			Union[str, Any]      							=VOCAB_FILES_NAMES
  UpperCAmelCase__   :			List[Any]      							=PRETRAINED_VOCAB_FILES_MAP
  UpperCAmelCase__   :			int      							=PRETRAINED_INIT_CONFIGURATION
  UpperCAmelCase__   :			int      							=PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
  UpperCAmelCase__   :			Tuple      							=["""input_ids""", """attention_mask"""]
  UpperCAmelCase__   :			Tuple      							=re.compile(""">>.+<<"""							)  # type: re.Pattern


  def __init__(      self     :			Dict					,					UpperCAmelCase__     :			str					,					UpperCAmelCase__     :			Optional[Any]					,					UpperCAmelCase__     :			List[str]					,					UpperCAmelCase__     :			Union[str, Any]=None					,					UpperCAmelCase__     :			Optional[Any]=None					,					UpperCAmelCase__     :			Tuple=None					,					UpperCAmelCase__     :			Optional[Any]="<unk>"					,					UpperCAmelCase__     :			List[str]="</s>"					,					UpperCAmelCase__     :			Any="<pad>"					,					UpperCAmelCase__     :			Union[str, Any]=5_1_2					,					UpperCAmelCase__     :			Optional[Dict[str, Any]] = None					,					UpperCAmelCase__     :			Dict=False					,					**UpperCAmelCase__     :			List[str]					,					)       ->None:
        """simple docstring"""







        SCREAMING_SNAKE_CASE			:   List[str]	  = {} if sp_model_kwargs is None else sp_model_kwargs

        super().__init__(
            # bos_token=bos_token,  unused. Start decoding with config.decoder_start_token_id
            source_lang=UpperCAmelCase__					,					target_lang=UpperCAmelCase__					,					unk_token=UpperCAmelCase__					,					eos_token=UpperCAmelCase__					,					pad_token=UpperCAmelCase__					,					model_max_length=UpperCAmelCase__					,					sp_model_kwargs=self.sp_model_kwargs					,					target_vocab_file=UpperCAmelCase__					,					separate_vocabs=UpperCAmelCase__					,					**UpperCAmelCase__					,					)
        assert Path(UpperCAmelCase__		).exists(), f"cannot find spm source {source_spm}"

        SCREAMING_SNAKE_CASE			:   Any	  = separate_vocabs
        SCREAMING_SNAKE_CASE			:   Optional[int]	  = load_json(UpperCAmelCase__		)
        if self.unk_token not in self.encoder:
              raise KeyError("""<unk> token must be in vocab"""		)
        assert self.pad_token in self.encoder

        if separate_vocabs:
              SCREAMING_SNAKE_CASE			:   str	  = load_json(UpperCAmelCase__		)
              SCREAMING_SNAKE_CASE			:   str	  = {v: k for k, v in self.target_encoder.items()}
              SCREAMING_SNAKE_CASE			:   Tuple	  = []
        else:
              SCREAMING_SNAKE_CASE			:   List[str]	  = {v: k for k, v in self.encoder.items()}
              SCREAMING_SNAKE_CASE			:   list	  = [k for k in self.encoder if k.startswith(""">>"""		) and k.endswith("""<<"""		)]

        SCREAMING_SNAKE_CASE			:   Dict	  = source_lang
        SCREAMING_SNAKE_CASE			:   Optional[Any]	  = target_lang
        SCREAMING_SNAKE_CASE			:   str	  = [source_spm, target_spm]

        # load SentencePiece model for pre-processing
        SCREAMING_SNAKE_CASE			:   int	  = load_spm(UpperCAmelCase__					,					self.sp_model_kwargs		)
        SCREAMING_SNAKE_CASE			:   Dict	  = load_spm(UpperCAmelCase__					,					self.sp_model_kwargs		)
        SCREAMING_SNAKE_CASE			:   Optional[int]	  = self.spm_source
        SCREAMING_SNAKE_CASE			:   Union[str, Any]	  = self.encoder

        # Multilingual target side: default to using first supported language code.

        self._setup_normalizer()


  def 			_lowercase   (      self     :			Optional[int]		)       ->Union[str, Any]:
        """simple docstring"""







        try:
              from sacremoses import MosesPunctNormalizer

              SCREAMING_SNAKE_CASE			:   Optional[int]	  = MosesPunctNormalizer(self.source_lang		).normalize
        except (ImportError, FileNotFoundError):
              warnings.warn("""Recommended: pip install sacremoses."""		)
              SCREAMING_SNAKE_CASE			:   Optional[int]	  = lambda UpperCAmelCase__		: x


  def 			_lowercase   (      self     :			Tuple					,					UpperCAmelCase__     :			str		)       ->str:
        """simple docstring"""







        return self.punc_normalizer(UpperCAmelCase__		) if x else ""


  def 			_lowercase   (      self     :			Union[str, Any]					,					UpperCAmelCase__     :			Tuple		)       ->Dict:
        """simple docstring"""







        return self.current_encoder.get(UpperCAmelCase__					,					self.current_encoder[self.unk_token]		)


  def 			_lowercase   (      self     :			Any					,					UpperCAmelCase__     :			str		)       ->Optional[int]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE			:   Dict	  = self.language_code_re.match(UpperCAmelCase__		)
        SCREAMING_SNAKE_CASE			:   list	  = [match.group(0		)] if match else []
        return code, self.language_code_re.sub(""""""					,					UpperCAmelCase__		)


  def 			_lowercase   (      self     :			List[str]					,					UpperCAmelCase__     :			str		)       ->List[str]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE  ,						SCREAMING_SNAKE_CASE			:   str	  = self.remove_language_code(UpperCAmelCase__		)
        SCREAMING_SNAKE_CASE			:   int	  = self.current_spm.encode(UpperCAmelCase__					,					out_type=UpperCAmelCase__		)
        return code + pieces


  def 			_lowercase   (      self     :			Dict					,					UpperCAmelCase__     :			int		)       ->str:
        """simple docstring"""







        return self.decoder.get(UpperCAmelCase__					,					self.unk_token		)


  def 			_lowercase   (      self     :			List[Any]					,					UpperCAmelCase__     :			List[str]					,					**UpperCAmelCase__     :			List[Any]		)       ->Dict:
        """simple docstring"""







        return super().batch_decode(UpperCAmelCase__					,					**UpperCAmelCase__		)


  def 			_lowercase   (      self     :			Tuple					,					UpperCAmelCase__     :			str					,					**UpperCAmelCase__     :			List[Any]		)       ->Optional[int]:
        """simple docstring"""







        return super().decode(UpperCAmelCase__					,					**UpperCAmelCase__		)


  def 			_lowercase   (      self     :			int					,					UpperCAmelCase__     :			List[str]		)       ->str:
        """simple docstring"""







        SCREAMING_SNAKE_CASE			:   Tuple	  = self.spm_source if self._decode_use_source_tokenizer else self.spm_target
        SCREAMING_SNAKE_CASE			:   Union[str, Any]	  = []
        SCREAMING_SNAKE_CASE			:   Tuple	  = """"""
        for token in tokens:
              # make sure that special tokens are not decoded using sentencepiece model
              if token in self.all_special_tokens:
                    out_string += sp_model.decode_pieces(UpperCAmelCase__		) + token + " "
                    SCREAMING_SNAKE_CASE			:   List[str]	  = []
              else:
                    current_sub_tokens.append(UpperCAmelCase__		)
        out_string += sp_model.decode_pieces(UpperCAmelCase__		)
        return out_string.strip()


  def 			_lowercase   (      self     :			int					,					UpperCAmelCase__     :			Tuple					,					UpperCAmelCase__     :			Union[str, Any]=None		)       ->List[int]:
        """simple docstring"""







        if token_ids_a is None:
              return token_ids_a + [self.eos_token_id]
        # We don't expect to process pairs, but leave the pair logic for API consistency
        return token_ids_a + token_ids_a + [self.eos_token_id]


  def 			_lowercase   (      self     :			Tuple		)       ->Tuple:
        """simple docstring"""







        SCREAMING_SNAKE_CASE			:   int	  = self.spm_source
        SCREAMING_SNAKE_CASE			:   Union[str, Any]	  = self.encoder


  def 			_lowercase   (      self     :			Optional[int]		)       ->Union[str, Any]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE			:   List[Any]	  = self.spm_target
        if self.separate_vocabs:
              SCREAMING_SNAKE_CASE			:   List[Any]	  = self.target_encoder


  @property
  def 			_lowercase   (      self     :			List[Any]		)       ->int:
        """simple docstring"""







        return len(self.encoder		)


  def 			_lowercase   (      self     :			Tuple					,					UpperCAmelCase__     :			str					,					UpperCAmelCase__     :			Optional[str] = None		)       ->Tuple[str]:
        """simple docstring"""







        if not os.path.isdir(UpperCAmelCase__		):
              logger.error(f"Vocabulary path ({save_directory}) should be a directory"		)
              return
        SCREAMING_SNAKE_CASE			:   Tuple	  = []

        if self.separate_vocabs:
              SCREAMING_SNAKE_CASE			:   Dict	  = os.path.join(
                  UpperCAmelCase__					,					(filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab"""]					,					)
              SCREAMING_SNAKE_CASE			:   Union[str, Any]	  = os.path.join(
                  UpperCAmelCase__					,					(filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""target_vocab_file"""]					,					)
              save_json(self.encoder					,					UpperCAmelCase__		)
              save_json(self.target_encoder					,					UpperCAmelCase__		)
              saved_files.append(UpperCAmelCase__		)
              saved_files.append(UpperCAmelCase__		)
        else:
              SCREAMING_SNAKE_CASE			:   Tuple	  = os.path.join(
                  UpperCAmelCase__					,					(filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab"""]		)
              save_json(self.encoder					,					UpperCAmelCase__		)
              saved_files.append(UpperCAmelCase__		)

        for spm_save_filename, spm_orig_path, spm_model in zip(
            [VOCAB_FILES_NAMES["""source_spm"""], VOCAB_FILES_NAMES["""target_spm"""]]					,					self.spm_files					,					[self.spm_source, self.spm_target]					,					):
              SCREAMING_SNAKE_CASE			:   Any	  = os.path.join(
                  UpperCAmelCase__					,					(filename_prefix + """-""" if filename_prefix else """""") + spm_save_filename		)
              if os.path.abspath(UpperCAmelCase__		) != os.path.abspath(UpperCAmelCase__		) and os.path.isfile(UpperCAmelCase__		):
                    copyfile(UpperCAmelCase__					,					UpperCAmelCase__		)
                    saved_files.append(UpperCAmelCase__		)
              elif not os.path.isfile(UpperCAmelCase__		):
                    with open(UpperCAmelCase__					,					"""wb"""		) as fi:
                          SCREAMING_SNAKE_CASE			:   List[str]	  = spm_model.serialized_model_proto()
                          fi.write(UpperCAmelCase__		)
                    saved_files.append(UpperCAmelCase__		)

        return tuple(UpperCAmelCase__		)


  def 			_lowercase   (      self     :			int		)       ->Dict:
        """simple docstring"""







        return self.get_src_vocab()


  def 			_lowercase   (      self     :			str		)       ->Union[str, Any]:
        """simple docstring"""







        return dict(self.encoder					,					**self.added_tokens_encoder		)


  def 			_lowercase   (      self     :			str		)       ->Tuple:
        """simple docstring"""







        return dict(self.target_encoder					,					**self.added_tokens_decoder		)


  def __getstate__(      self     :			Optional[int]		)       ->Dict:
        """simple docstring"""







        SCREAMING_SNAKE_CASE			:   Dict	  = self.__dict__.copy()
        state.update(
            {k: None for k in ["""spm_source""", """spm_target""", """current_spm""", """punc_normalizer""", """target_vocab_file"""]}		)
        return state


  def __setstate__(      self     :			List[Any]					,					UpperCAmelCase__     :			Dict		)       ->None:
        """simple docstring"""







        SCREAMING_SNAKE_CASE			:   List[str]	  = d

        # for backward compatibility
        if not hasattr(self					,					"""sp_model_kwargs"""		):
              SCREAMING_SNAKE_CASE			:   Optional[int]	  = {}

        SCREAMING_SNAKE_CASE  ,						SCREAMING_SNAKE_CASE			:   List[Any]	  = (load_spm(UpperCAmelCase__					,					self.sp_model_kwargs		) for f in self.spm_files)
        SCREAMING_SNAKE_CASE			:   Any	  = self.spm_source
        self._setup_normalizer()


  def 			_lowercase   (      self     :			Dict					,					*UpperCAmelCase__     :			str					,					**UpperCAmelCase__     :			List[str]		)       ->Dict:
        """simple docstring"""







        return 1


  def 			_lowercase   (      self     :			List[Any]					,					UpperCAmelCase__     :			Dict		)       ->List[Any]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE			:   Optional[Any]	  = set(self.all_special_ids		)  # call it once instead of inside list comp
        all_special_ids.remove(self.unk_token_id		)  # <unk> is only sometimes special
        return [1 if x in all_special_ids else 0 for x in seq]




  def 			_lowercase   (      self     :			Tuple					,					UpperCAmelCase__     :			List					,					UpperCAmelCase__     :			Optional[List] = None					,					UpperCAmelCase__     :			bool = False		)       ->List[int]:
        """simple docstring"""







        if already_has_special_tokens:
              return self._special_token_mask(UpperCAmelCase__		)
        elif token_ids_a is None:
              return self._special_token_mask(UpperCAmelCase__		) + [1]
        else:
              return self._special_token_mask(token_ids_a + token_ids_a		) + [1]






def        __lowercase   (  _A    ,     _A      )  ->    sentencepiece.SentencePieceProcessor:
      SCREAMING_SNAKE_CASE			:   List[str]	  = sentencepiece.SentencePieceProcessor(**_A      )
      spm.Load(_A      )
      return spm






def        __lowercase   (  _A    ,     _A      )  ->    None:
      with open(_A    ,     """w"""      ) as f:
            json.dump(_A    ,     _A    ,     indent=2      )







def        __lowercase   (  _A      )  ->    Union[Dict, List]:
      with open(_A    ,     """r"""      ) as f:
            return json.load(_A      )




