





"""simple docstring"""





import json
import os
from functools import lru_cache
from typing import TYPE_CHECKING, List, Optional, Tuple, Union

import numpy as np
import regex as re

from ...utils import is_tf_available, is_torch_available, logging


if TYPE_CHECKING:
						if is_torch_available():
												import torch
						if is_tf_available():
												import tensorflow as tf

from ...tokenization_utils import AddedToken, PreTrainedTokenizer


lowerCAmelCase__        =      logging.get_logger(__name__)

lowerCAmelCase__        =      {
    """vocab_file""": """vocab.json""",
    """merges_file""": """merges.txt""",
}

lowerCAmelCase__        =      {
    """vocab_file""": {
        """Salesforce/codegen-350M-mono""": """https://huggingface.co/Salesforce/codegen-350M-mono/resolve/main/vocab.json""",
    },
    """merges_file""": {
        """Salesforce/codegen-350M-mono""": """https://huggingface.co/Salesforce/codegen-350M-mono/resolve/main/merges.txt""",
    },
}

lowerCAmelCase__        =      {
    """Salesforce/codegen-350M-mono""": 2_048,
}


@lru_cache()
def    a__       (       ):




		"""simple docstring"""


		UpperCamelCase =						(
		    list(range(ord("!"     )	,						ord("~"     ) + 1     )     ) + list(range(ord("¡"     )	,						ord("¬"     ) + 1     )     ) + list(range(ord("®"     )	,						ord("ÿ"     ) + 1     )     )
		)
		UpperCamelCase =						bs[:]
		UpperCamelCase =						0
		for b in range(2**8     ):
				if b not in bs:
						bs.append(_SCREAMING_SNAKE_CASE     )
						cs.append(2**8 + n     )
						n += 1
		UpperCamelCase =						[chr(_SCREAMING_SNAKE_CASE     ) for n in cs]
		return dict(zip(_SCREAMING_SNAKE_CASE	,						_SCREAMING_SNAKE_CASE     )     )


def    a__       (       _SCREAMING_SNAKE_CASE     ):




		"""simple docstring"""


		UpperCamelCase =						set()
		UpperCamelCase =						word[0]
		for char in word[1:]:
				pairs.add((prev_char, char)     )
				UpperCamelCase =						char
		return pairs






class 							_lowerCamelCase   (							snake_case__			):
						UpperCAmelCase_			=      VOCAB_FILES_NAMES
						UpperCAmelCase_			=      PRETRAINED_VOCAB_FILES_MAP
						UpperCAmelCase_			=      PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
						UpperCAmelCase_			=      ["input_ids", "attention_mask"]


						def __init__(self ,				__a ,				__a ,				__a="replace" ,				__a="<|endoftext|>" ,				__a="<|endoftext|>" ,				__a="<|endoftext|>" ,				__a=None ,				__a=False ,				__a=False ,				**__a ,				)			-> List[str]:
								UpperCamelCase =						AddedToken(lowercase_ ,				lstrip=lowercase_ ,				rstrip=lowercase_		) if isinstance(lowercase_ ,				lowercase_		) else bos_token
								UpperCamelCase =						AddedToken(lowercase_ ,				lstrip=lowercase_ ,				rstrip=lowercase_		) if isinstance(lowercase_ ,				lowercase_		) else eos_token
								UpperCamelCase =						AddedToken(lowercase_ ,				lstrip=lowercase_ ,				rstrip=lowercase_		) if isinstance(lowercase_ ,				lowercase_		) else unk_token
								UpperCamelCase =						AddedToken(lowercase_ ,				lstrip=lowercase_ ,				rstrip=lowercase_		) if isinstance(lowercase_ ,				lowercase_		) else pad_token
								super().__init__(
								    errors=lowercase_ ,				unk_token=lowercase_ ,				bos_token=lowercase_ ,				eos_token=lowercase_ ,				pad_token=lowercase_ ,				add_prefix_space=lowercase_ ,				add_bos_token=lowercase_ ,				**lowercase_ ,				)
								UpperCamelCase =						add_bos_token

								with open(lowercase_ ,				encoding="utf-8"		) as vocab_handle:
										UpperCamelCase =						json.load(lowercase_		)
								UpperCamelCase =						{v: k for k, v in self.encoder.items()}
								UpperCamelCase =						errors  # how to handle errors in decoding
								UpperCamelCase =						bytes_to_unicode()
								UpperCamelCase =						{v: k for k, v in self.byte_encoder.items()}
								with open(lowercase_ ,				encoding="utf-8"		) as merges_handle:
										UpperCamelCase =						merges_handle.read().split("\n"		)[1:-1]
								UpperCamelCase =						[tuple(merge.split()		) for merge in bpe_merges]
								UpperCamelCase =						dict(zip(lowercase_ ,				range(len(lowercase_		)		)		)		)
								UpperCamelCase =						{}
								UpperCamelCase =						add_prefix_space

								# Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions
								UpperCamelCase =						re.compile(r"\'s|\'t|\'re|\'ve|\'m|\'ll|\'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+"		)


						@property
						def      snake_case_ (self		)			-> Any:
								return len(self.encoder		)


						def      snake_case_ (self		)			-> Union[str, Any]:
								return dict(self.encoder ,				**self.added_tokens_encoder		)


						def      snake_case_ (self ,				__a		)			-> List[str]:
								if token in self.cache:
										return self.cache[token]
								UpperCamelCase =						tuple(lowercase_		)
								UpperCamelCase =						get_pairs(lowercase_		)

								if not pairs:
										return token

								while True:
										UpperCamelCase =						min(lowercase_ ,				key=lambda __a		: self.bpe_ranks.get(lowercase_ ,				float("inf"		)		)		)
										if bigram not in self.bpe_ranks:
												break
										UpperCamelCase =						bigram
										UpperCamelCase =						[]
										UpperCamelCase =						0
										while i < len(lowercase_		):
												try:
														UpperCamelCase =						word.index(lowercase_ ,				lowercase_		)
												except ValueError:
														new_word.extend(word[i:]		)
														break
												else:
														new_word.extend(word[i:j]		)
														UpperCamelCase =						j

												if word[i] == first and i < len(lowercase_		) - 1 and word[i + 1] == second:
														new_word.append(first + second		)
														i += 2
												else:
														new_word.append(word[i]		)
														i += 1
										UpperCamelCase =						tuple(lowercase_		)
										UpperCamelCase =						new_word
										if len(lowercase_		) == 1:
												break
										else:
												UpperCamelCase =						get_pairs(lowercase_		)
								UpperCamelCase =						''' '''.join(lowercase_		)
								UpperCamelCase =						word
								return word


						def      snake_case_ (self ,				__a ,				__a=None		)			-> List[str]:
								if self.add_bos_token:
										UpperCamelCase =						[self.bos_token_id]
								else:
										UpperCamelCase =						[]

								UpperCamelCase =						bos_token_ids + token_ids_a

								if token_ids_a is None:
										return output

								return output + bos_token_ids + token_ids_a


						def      snake_case_ (self ,				__a		)			-> Optional[Any]:
								UpperCamelCase =						[]
								for token in re.findall(self.pat ,				lowercase_		):
										UpperCamelCase =						''''''.join(
										    self.byte_encoder[b] for b in token.encode("utf-8"		)		)  # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case)
										bpe_tokens.extend(bpe_token for bpe_token in self.bpe(lowercase_		).split(" "		)		)
								return bpe_tokens


						def      snake_case_ (self ,				__a		)			-> Optional[int]:
								return self.encoder.get(lowercase_ ,				self.encoder.get(self.unk_token		)		)


						def      snake_case_ (self ,				__a		)			-> Optional[int]:
								return self.decoder.get(lowercase_		)


						def      snake_case_ (self ,				__a		)			-> int:
								UpperCamelCase =						''''''.join(lowercase_		)
								UpperCamelCase =						bytearray([self.byte_decoder[c] for c in text]		).decode("utf-8" ,				errors=self.errors		)
								return text


						def      snake_case_ (self ,				__a ,				__a = None		)			-> Tuple:
								if not os.path.isdir(lowercase_		):
										logger.error(F"Vocabulary path ({save_directory}) should be a directory"		)
										return
								UpperCamelCase =						os.path.join(
								    lowercase_ ,				(filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]		)
								UpperCamelCase =						os.path.join(
								    lowercase_ ,				(filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["merges_file"]		)

								with open(lowercase_ ,				"w" ,				encoding="utf-8"		) as f:
										f.write(json.dumps(self.encoder ,				indent=2 ,				sort_keys=lowercase_ ,				ensure_ascii=lowercase_		) + "\n"		)

								UpperCamelCase =						0
								with open(lowercase_ ,				"w" ,				encoding="utf-8"		) as writer:
										writer.write("#version: 0.2\n"		)
										for bpe_tokens, token_index in sorted(self.bpe_ranks.items() ,				key=lambda __a		: kv[1]		):
												if index != token_index:
														logger.warning(
														    F"Saving vocabulary to {merge_file}: BPE merge indices are not consecutive."
														    " Please check that the tokenizer is not corrupted!"		)
														UpperCamelCase =						token_index
												writer.write(" ".join(lowercase_		) + "\n"		)
												index += 1

								return vocab_file, merge_file


						def      snake_case_ (self ,				__a ,				__a=False ,				**__a		)			-> Optional[int]:
								UpperCamelCase =						kwargs.pop("add_prefix_space" ,				self.add_prefix_space		)
								if is_split_into_words or add_prefix_space:
										UpperCamelCase =						''' ''' + text
								return (text, kwargs)


						def      snake_case_ (self ,				__a ,				__a = False ,				__a = None ,				__a = None ,				**__a ,				)			-> List[Any]:
								UpperCamelCase =						super()._decode(
								    token_ids=lowercase_ ,				skip_special_tokens=lowercase_ ,				clean_up_tokenization_spaces=lowercase_ ,				**lowercase_ ,				)

								if truncate_before_pattern is not None and len(lowercase_		) > 0:
										UpperCamelCase =						self.truncate(lowercase_ ,				lowercase_		)

								return decoded_text




						def      snake_case_ (self ,				__a ,				__a		)			-> Dict:
								def find_re(__a ,				__a ,				__a		):
										UpperCamelCase =						pattern.search(lowercase_ ,				lowercase_		)
										return m.start() if m else -1

								UpperCamelCase =						[re.compile(lowercase_ ,				re.MULTILINE		) for pattern in truncate_before_pattern]

								UpperCamelCase =						list(re.finditer("^print" ,				lowercase_ ,				re.MULTILINE		)		)

								if len(lowercase_		) > 1:
										UpperCamelCase =						completion[: prints[1].start()]

								UpperCamelCase =						list(re.finditer("^def" ,				lowercase_ ,				re.MULTILINE		)		)

								if len(lowercase_		) > 1:
										UpperCamelCase =						completion[: defs[1].start()]

								UpperCamelCase =						0

								UpperCamelCase =						[
								    pos for pos in [find_re(lowercase_ ,				lowercase_ ,				lowercase_		) for terminal in terminals] if pos != -1
								]

								if len(lowercase_		) > 0:
										return completion[: min(lowercase_		)]
								else:
										return completion









"""simple docstring"""





import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils import AddedToken
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_mpnet import MPNetTokenizer


_SCREAMING_SNAKE_CASE							:	Union[str, Any]										=			logging.get_logger(__name__)

_SCREAMING_SNAKE_CASE							:	Union[str, Any]										=			{"""vocab_file""": """vocab.txt""", """tokenizer_file""": """tokenizer.json"""}

_SCREAMING_SNAKE_CASE							:	List[Any]										=			{
    """vocab_file""": {
        """microsoft/mpnet-base""": """https://huggingface.co/microsoft/mpnet-base/resolve/main/vocab.txt""",
    },
    """tokenizer_file""": {
        """microsoft/mpnet-base""": """https://huggingface.co/microsoft/mpnet-base/resolve/main/tokenizer.json""",
    },
}

_SCREAMING_SNAKE_CASE							:	List[Any]										=			{
    """microsoft/mpnet-base""": 5_1_2,
}

_SCREAMING_SNAKE_CASE							:	Optional[int]										=			{
    """microsoft/mpnet-base""": {"""do_lower_case""": True},
}

class      __a (   snake_case__     ):

			"""simple docstring"""

			SCREAMING_SNAKE_CASE_      =    VOCAB_FILES_NAMES
			SCREAMING_SNAKE_CASE_      =    PRETRAINED_VOCAB_FILES_MAP
			SCREAMING_SNAKE_CASE_      =    PRETRAINED_INIT_CONFIGURATION
			SCREAMING_SNAKE_CASE_      =    PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
			SCREAMING_SNAKE_CASE_      =    MPNetTokenizer
			SCREAMING_SNAKE_CASE_      =    ['input_ids', 'attention_mask']






			def __init__(				self  :       List[str]    ,	lowercase_  :       List[Any]=None    ,	lowercase_  :       Optional[int]=None    ,	lowercase_  :       Optional[int]=True    ,	lowercase_  :       str="<s>"    ,	lowercase_  :       str="</s>"    ,	lowercase_  :       List[Any]="</s>"    ,	lowercase_  :       Tuple="<s>"    ,	lowercase_  :       Any="[UNK]"    ,	lowercase_  :       Dict="<pad>"    ,	lowercase_  :       Optional[Any]="<mask>"    ,	lowercase_  :       List[str]=True    ,	lowercase_  :       Optional[Any]=None    ,	**lowercase_  :       Dict    ,	):
								super().__init__(
								    lowercase_    ,	tokenizer_file=lowercase_    ,	do_lower_case=lowercase_    ,	bos_token=lowercase_    ,	eos_token=lowercase_    ,	sep_token=lowercase_    ,	cls_token=lowercase_    ,	unk_token=lowercase_    ,	pad_token=lowercase_    ,	mask_token=lowercase_    ,	tokenize_chinese_chars=lowercase_    ,	strip_accents=lowercase_    ,	**lowercase_    ,	)

								UpperCamelCase__		:     List[str]								=json.loads(self.backend_tokenizer.normalizer.__getstate__()    )
								if (
								    pre_tok_state.get('''lowercase'''    ,	lowercase_    ) != do_lower_case
								    or pre_tok_state.get('''strip_accents'''    ,	lowercase_    ) != strip_accents
								):
													UpperCamelCase__		:     int								=getattr(lowercase_    ,	pre_tok_state.pop('''type'''    )    )
													UpperCamelCase__		:     List[Any]								=do_lower_case
													UpperCamelCase__		:     Optional[Any]								=strip_accents
													UpperCamelCase__		:     Optional[int]								=pre_tok_class(**lowercase_    )

								UpperCamelCase__		:     Tuple								=do_lower_case






			@property
			def  _lowerCAmelCase      (				self  :       List[str]    ):
								if self._mask_token is None:
													if self.verbose:
																		logger.error('''Using mask_token, but it is not set yet.'''    )
													return None
								return str(self._mask_token    )






			@mask_token.setter
			def  _lowerCAmelCase      (				self  :       int    ,	lowercase_  :       str    ):
								UpperCamelCase__		:     int								=AddedToken(lowercase_    ,	lstrip=lowercase_    ,	rstrip=lowercase_    ) if isinstance(lowercase_    ,	lowercase_    ) else value
								UpperCamelCase__		:     str								=value






			def  _lowerCAmelCase      (				self  :       Tuple    ,	lowercase_  :       Any    ,	lowercase_  :       Tuple=None    ):
								UpperCamelCase__		:     int								=[self.bos_token_id] + token_ids_a + [self.eos_token_id]
								if token_ids_a is None:
													return output

								return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id]






			def  _lowerCAmelCase      (				self  :       List[str]    ,	lowercase_  :       List[int]    ,	lowercase_  :       Optional[List[int]] = None    ):
								UpperCamelCase__		:     str								=[self.sep_token_id]
								UpperCamelCase__		:     List[str]								=[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 + sep + token_ids_a + sep    ) * [0]






			def  _lowerCAmelCase      (				self  :       str    ,	lowercase_  :       str    ,	lowercase_  :       Optional[str] = None    ):
								UpperCamelCase__		:     List[Any]								=self._tokenizer.model.save(lowercase_    ,	name=lowercase_    )
								return tuple(lowercase_    )


