






"""simple docstring"""







import json
import os
import re
import unicodedata
from typing import Dict, List, Optional, Tuple

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


lowerCAmelCase_		:			Optional[int]				   =					logging.get_logger(__name__)

lowerCAmelCase_		:			Any				   =					{
    '''src_vocab_file''': '''vocab-src.json''',
    '''tgt_vocab_file''': '''vocab-tgt.json''',
    '''merges_file''': '''merges.txt''',
}

lowerCAmelCase_		:			List[str]				   =					{
    '''src_vocab_file''': {
        '''stas/tiny-wmt19-en-de''': '''https://huggingface.co/stas/tiny-wmt19-en-de/resolve/main/vocab-src.json'''
    },
    '''tgt_vocab_file''': {
        '''stas/tiny-wmt19-en-de''': '''https://huggingface.co/stas/tiny-wmt19-en-de/resolve/main/vocab-tgt.json'''
    },
    '''merges_file''': {'''stas/tiny-wmt19-en-de''': '''https://huggingface.co/stas/tiny-wmt19-en-de/resolve/main/merges.txt'''},
}

lowerCAmelCase_		:			Optional[Any]				   =					{'''stas/tiny-wmt19-en-de''': 1_0_2_4}
lowerCAmelCase_		:			List[str]				   =					{
    '''stas/tiny-wmt19-en-de''': {
        '''langs''': ['''en''', '''de'''],
        '''model_max_length''': 1_0_2_4,
        '''special_tokens_map_file''': None,
        '''full_tokenizer_file''': None,
    }
}






def 					_lowerCAmelCase				(					lowerCAmelCase						):







       '''simple docstring'''
       UpperCAmelCase					=	set()
       UpperCAmelCase					=	word[0]
       for char in word[1:]:
              pairs.add((prev_char, char)						)
              UpperCAmelCase					=	char
       return pairs






def 					_lowerCAmelCase				(					lowerCAmelCase						):







       '''simple docstring'''
       UpperCAmelCase					=	text.replace("""，"""		,					""","""						)
       UpperCAmelCase					=	re.sub(r"""。\s*"""		,					""". """		,					lowerCAmelCase						)
       UpperCAmelCase					=	text.replace("""、"""		,					""","""						)
       UpperCAmelCase					=	text.replace("""”"""		,					"""\""""						)
       UpperCAmelCase					=	text.replace("""“"""		,					"""\""""						)
       UpperCAmelCase					=	text.replace("""∶"""		,					""":"""						)
       UpperCAmelCase					=	text.replace("""："""		,					""":"""						)
       UpperCAmelCase					=	text.replace("""？"""		,					"""?"""						)
       UpperCAmelCase					=	text.replace("""《"""		,					"""\""""						)
       UpperCAmelCase					=	text.replace("""》"""		,					"""\""""						)
       UpperCAmelCase					=	text.replace("""）"""		,					""")"""						)
       UpperCAmelCase					=	text.replace("""！"""		,					"""!"""						)
       UpperCAmelCase					=	text.replace("""（"""		,					"""("""						)
       UpperCAmelCase					=	text.replace("""；"""		,					""";"""						)
       UpperCAmelCase					=	text.replace("""１"""		,					"""1"""						)
       UpperCAmelCase					=	text.replace("""」"""		,					"""\""""						)
       UpperCAmelCase					=	text.replace("""「"""		,					"""\""""						)
       UpperCAmelCase					=	text.replace("""０"""		,					"""0"""						)
       UpperCAmelCase					=	text.replace("""３"""		,					"""3"""						)
       UpperCAmelCase					=	text.replace("""２"""		,					"""2"""						)
       UpperCAmelCase					=	text.replace("""５"""		,					"""5"""						)
       UpperCAmelCase					=	text.replace("""６"""		,					"""6"""						)
       UpperCAmelCase					=	text.replace("""９"""		,					"""9"""						)
       UpperCAmelCase					=	text.replace("""７"""		,					"""7"""						)
       UpperCAmelCase					=	text.replace("""８"""		,					"""8"""						)
       UpperCAmelCase					=	text.replace("""４"""		,					"""4"""						)
       UpperCAmelCase					=	re.sub(r"""．\s*"""		,					""". """		,					lowerCAmelCase						)
       UpperCAmelCase					=	text.replace("""～"""		,					"""~"""						)
       UpperCAmelCase					=	text.replace("""’"""		,					"""'"""						)
       UpperCAmelCase					=	text.replace("""…"""		,					"""..."""						)
       UpperCAmelCase					=	text.replace("""━"""		,					"""-"""						)
       UpperCAmelCase					=	text.replace("""〈"""		,					"""<"""						)
       UpperCAmelCase					=	text.replace("""〉"""		,					""">"""						)
       UpperCAmelCase					=	text.replace("""【"""		,					"""["""						)
       UpperCAmelCase					=	text.replace("""】"""		,					"""]"""						)
       UpperCAmelCase					=	text.replace("""％"""		,					"""%"""						)
       return text






def 					_lowerCAmelCase				(					lowerCAmelCase						):







       '''simple docstring'''
       UpperCAmelCase					=	[]
       for char in text:
              UpperCAmelCase					=	unicodedata.category(lowerCAmelCase						)
              if cat.startswith("""C"""						):
                     continue
              output.append(lowerCAmelCase						)
       return "".join(lowerCAmelCase						)





class      UpperCamelCase_       (				a_     ):
  _A				:    str =		VOCAB_FILES_NAMES
  _A				:    str =		PRETRAINED_VOCAB_FILES_MAP
  _A				:    List[Any] =		PRETRAINED_INIT_CONFIGURATION
  _A				:    List[Any] =		PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
  _A				:    Dict =		['input_ids', 'attention_mask']



  def __init__(					self       ,						snake_case__=None       ,						snake_case__=None       ,						snake_case__=None       ,						snake_case__=None       ,						snake_case__=False       ,						snake_case__="<unk>"       ,						snake_case__="<s>"       ,						snake_case__="</s>"       ,						snake_case__="<pad>"       ,						**snake_case__       ,						)	->	Optional[int]:



         """simple docstring"""





         super().__init__(
             langs=snake_case__       ,						src_vocab_file=snake_case__       ,						tgt_vocab_file=snake_case__       ,						merges_file=snake_case__       ,						do_lower_case=snake_case__       ,						unk_token=snake_case__       ,						bos_token=snake_case__       ,						sep_token=snake_case__       ,						pad_token=snake_case__       ,						**snake_case__       ,						)

         try:
                import sacremoses
         except ImportError:
                raise ImportError(
                    """You need to install sacremoses to use XLMTokenizer. """
                    """See https://pypi.org/project/sacremoses/ for installation.""" )

         UpperCAmelCase					=	sacremoses

         UpperCAmelCase					=	src_vocab_file
         UpperCAmelCase					=	tgt_vocab_file
         UpperCAmelCase					=	merges_file
         UpperCAmelCase					=	do_lower_case

         # cache of sm.MosesPunctNormalizer instance
         UpperCAmelCase					=	{}
         # cache of sm.MosesTokenizer instance
         UpperCAmelCase					=	{}
         UpperCAmelCase					=	{}

         if langs and len(snake_case__ ) == 2:
                UpperCAmelCase			,				UpperCAmelCase					=	langs
         else:
                raise ValueError(
                    f'''arg `langs` needs to be a list of 2 langs, e.g. [\'en\', \'ru\'], but got {langs}. '''
                    """Usually that means that tokenizer can't find a mapping for the given model path """
                    """in PRETRAINED_VOCAB_FILES_MAP, and other maps of this tokenizer.""" )

         with open(snake_case__       ,						encoding="""utf-8""" ) as src_vocab_handle:
                UpperCAmelCase					=	json.load(snake_case__ )
         with open(snake_case__       ,						encoding="""utf-8""" ) as tgt_vocab_handle:
                UpperCAmelCase					=	json.load(snake_case__ )
                UpperCAmelCase					=	{v: k for k, v in tgt_vocab.items()}
         with open(snake_case__       ,						encoding="""utf-8""" ) as merges_handle:
                UpperCAmelCase					=	merges_handle.read().split("""\n""" )[:-1]
         UpperCAmelCase					=	[tuple(merge.split()[:2] ) for merge in merges]
         UpperCAmelCase					=	dict(zip(snake_case__       ,						range(len(snake_case__ ) ) ) )
         UpperCAmelCase					=	{}



  def 						UpperCamelCase_ (					self )	->	Dict[str, int]:



         """simple docstring"""





         return self.get_src_vocab()



  @property
  def 						UpperCamelCase_ (					self )	->	int:



         """simple docstring"""





         return self.src_vocab_size



  def 						UpperCamelCase_ (					self       ,						snake_case__       ,						snake_case__ )	->	Optional[Any]:



         """simple docstring"""





         if lang not in self.cache_moses_punct_normalizer:
                UpperCAmelCase					=	self.sm.MosesPunctNormalizer(lang=snake_case__ )
                UpperCAmelCase					=	punct_normalizer
         return self.cache_moses_punct_normalizer[lang].normalize(snake_case__ )



  def 						UpperCamelCase_ (					self       ,						snake_case__       ,						snake_case__ )	->	Union[str, Any]:



         """simple docstring"""





         if lang not in self.cache_moses_tokenizer:
                UpperCAmelCase					=	self.sm.MosesTokenizer(lang=snake_case__ )
                UpperCAmelCase					=	moses_tokenizer
         return self.cache_moses_tokenizer[lang].tokenize(
             snake_case__       ,						aggressive_dash_splits=snake_case__       ,						return_str=snake_case__       ,						escape=snake_case__ )



  def 						UpperCamelCase_ (					self       ,						snake_case__       ,						snake_case__ )	->	Optional[Any]:



         """simple docstring"""





         if lang not in self.cache_moses_detokenizer:
                UpperCAmelCase					=	self.sm.MosesDetokenizer(lang=snake_case__ )
                UpperCAmelCase					=	moses_detokenizer
         return self.cache_moses_detokenizer[lang].detokenize(snake_case__ )



  def 						UpperCamelCase_ (					self       ,						snake_case__       ,						snake_case__ )	->	List[Any]:



         """simple docstring"""





         UpperCAmelCase					=	replace_unicode_punct(snake_case__ )
         UpperCAmelCase					=	self.moses_punct_norm(snake_case__       ,						snake_case__ )
         UpperCAmelCase					=	remove_non_printing_char(snake_case__ )
         return text



  @property
  def 						UpperCamelCase_ (					self )	->	Optional[int]:



         """simple docstring"""





         return len(self.encoder )



  @property
  def 						UpperCamelCase_ (					self )	->	str:



         """simple docstring"""





         return len(self.decoder )



  def 						UpperCamelCase_ (					self )	->	Any:



         """simple docstring"""





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



  def 						UpperCamelCase_ (					self )	->	Optional[Any]:



         """simple docstring"""





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



  def 						UpperCamelCase_ (					self       ,						snake_case__ )	->	Union[str, Any]:



         """simple docstring"""





         UpperCAmelCase					=	tuple(token[:-1] ) + (token[-1] + """</w>""",)
         if token in self.cache:
                return self.cache[token]
         UpperCAmelCase					=	get_pairs(snake_case__ )

         if not pairs:
                return token + "</w>"

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

                       if word[i] == first and i < len(snake_case__ ) - 1 and word[i + 1] == second:
                              new_word.append(first + second )
                              i += 2
                       else:
                              new_word.append(word[i] )
                              i += 1
                UpperCAmelCase					=	tuple(snake_case__ )
                UpperCAmelCase					=	new_word
                if len(snake_case__ ) == 1:
                       break
                else:
                       UpperCAmelCase					=	get_pairs(snake_case__ )
         UpperCAmelCase					=	""" """.join(snake_case__ )
         if word == "\n  </w>":
                UpperCAmelCase					=	"""\n</w>"""
         UpperCAmelCase					=	word
         return word



  def 						UpperCamelCase_ (					self       ,						snake_case__       ,						snake_case__="en"       ,						snake_case__=False )	->	Any:



         """simple docstring"""





         UpperCAmelCase					=	self.src_lang

         if self.do_lower_case:
                UpperCAmelCase					=	text.lower()

         if bypass_tokenizer:
                UpperCAmelCase					=	text.split()
         else:
                UpperCAmelCase					=	self.moses_pipeline(snake_case__       ,						lang=snake_case__ )
                UpperCAmelCase					=	self.moses_tokenize(snake_case__       ,						lang=snake_case__ )

         UpperCAmelCase					=	[]
         for token in text:
                if token:
                       split_tokens.extend(list(self.bpe(snake_case__ ).split(""" """ ) ) )

         return split_tokens



  def 						UpperCamelCase_ (					self       ,						snake_case__ )	->	Optional[int]:



         """simple docstring"""





         return self.encoder.get(snake_case__       ,						self.encoder.get(self.unk_token ) )



  def 						UpperCamelCase_ (					self       ,						snake_case__ )	->	List[Any]:



         """simple docstring"""





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



  def 						UpperCamelCase_ (					self       ,						snake_case__ )	->	Optional[int]:



         """simple docstring"""





         UpperCAmelCase					=	[t.replace(""" """       ,						"""""" ).replace("""</w>"""       ,						""" """ ) for t in tokens]
         UpperCAmelCase					=	"""""".join(snake_case__ ).split()
         # detokenize
         UpperCAmelCase					=	self.moses_detokenize(snake_case__       ,						self.tgt_lang )
         return text



  def 						UpperCamelCase_ (					self       ,						snake_case__       ,						snake_case__ = None )	->	List[int]:



         """simple docstring"""





         UpperCAmelCase					=	[self.sep_token_id]

         # no bos used in fairseq
         if token_ids_a is None:
                return token_ids_a + sep
         return token_ids_a + sep + token_ids_a + sep



  def 						UpperCamelCase_ (					self       ,						snake_case__       ,						snake_case__ = None       ,						snake_case__ = False )	->	List[int]:



         """simple docstring"""





         if already_has_special_tokens:
                return super().get_special_tokens_mask(
                    token_ids_a=snake_case__       ,						token_ids_a=snake_case__       ,						already_has_special_tokens=snake_case__ )
         # no bos used in fairseq
         if token_ids_a is not None:
                return ([0] * len(snake_case__ )) + [1] + ([0] * len(snake_case__ )) + [1]
         return ([0] * len(snake_case__ )) + [1]



  def 						UpperCamelCase_ (					self       ,						snake_case__       ,						snake_case__ = None )	->	List[int]:



         """simple docstring"""





         UpperCAmelCase					=	[self.sep_token_id]

         # no bos used in fairseq
         if token_ids_a is None:
                return len(token_ids_a + sep ) * [0]
         return len(token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1]



  def 						UpperCamelCase_ (					self       ,						snake_case__       ,						snake_case__ = None )	->	Tuple[str]:



         """simple docstring"""





         if not os.path.isdir(snake_case__ ):
                logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' )
                return

         UpperCAmelCase					=	os.path.join(
             snake_case__       ,						(filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""src_vocab_file"""] )
         UpperCAmelCase					=	os.path.join(
             snake_case__       ,						(filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""tgt_vocab_file"""] )
         UpperCAmelCase					=	os.path.join(
             snake_case__       ,						(filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""merges_file"""] )

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

         with open(snake_case__       ,						"""w"""       ,						encoding="""utf-8""" ) as f:
                UpperCAmelCase					=	{v: k for k, v in self.decoder.items()}
                f.write(json.dumps(snake_case__       ,						indent=2       ,						sort_keys=snake_case__       ,						ensure_ascii=snake_case__ ) + """\n""" )

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

         return src_vocab_file, tgt_vocab_file, merges_file



  def __getstate__(					self )	->	List[str]:



         """simple docstring"""





         UpperCAmelCase					=	self.__dict__.copy()
         UpperCAmelCase					=	None
         return state



  def __setstate__(					self       ,						snake_case__ )	->	int:



         """simple docstring"""





         UpperCAmelCase					=	d

         try:
                import sacremoses
         except ImportError:
                raise ImportError(
                    """You need to install sacremoses to use XLMTokenizer. """
                    """See https://pypi.org/project/sacremoses/ for installation.""" )

         UpperCAmelCase					=	sacremoses








"""simple docstring"""







from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCAmelCase_		:			str				   =					logging.get_logger(__name__)

lowerCAmelCase_		:			Optional[int]				   =					{
    '''google/reformer-crime-and-punishment''': (
        '''https://huggingface.co/google/reformer-crime-and-punishment/resolve/main/config.json'''
    ),
    '''google/reformer-enwik8''': '''https://huggingface.co/google/reformer-enwik8/resolve/main/config.json''',
}





class      UpperCamelCase_       (				a_     ):
  _A				:    int =		'reformer'
  _A				:    Any =		['past_buckets_states']
  _A				:    Optional[Any] =		{}



  def __init__(					self       ,						snake_case__=64       ,						snake_case__=["local", "lsh", "local", "lsh", "local", "lsh"]       ,						snake_case__=1.0       ,						snake_case__=True       ,						snake_case__=[64, 64]       ,						snake_case__=[64, 1_92]       ,						snake_case__=0       ,						snake_case__=2       ,						snake_case__=5_12       ,						snake_case__=None       ,						snake_case__="relu"       ,						snake_case__=0.05       ,						snake_case__=2_56       ,						snake_case__=0.02       ,						snake_case__=False       ,						snake_case__=1e-12       ,						snake_case__=1       ,						snake_case__=0       ,						snake_case__=0.05       ,						snake_case__=64       ,						snake_case__=64       ,						snake_case__=0.0       ,						snake_case__=1       ,						snake_case__=0       ,						snake_case__=40_96       ,						snake_case__=12       ,						snake_case__=None       ,						snake_case__=1       ,						snake_case__=0       ,						snake_case__=3_20       ,						snake_case__=False       ,						snake_case__=True       ,						snake_case__=None       ,						**snake_case__       ,						)	->	List[Any]:



         """simple docstring"""





         UpperCAmelCase					=	hash_seed
         UpperCAmelCase					=	vocab_size
         UpperCAmelCase					=	attention_head_size
         UpperCAmelCase					=	hidden_size
         UpperCAmelCase					=	num_attention_heads
         UpperCAmelCase					=	num_hashes
         UpperCAmelCase					=	len(snake_case__ )
         UpperCAmelCase					=	tuple(snake_case__ ) if isinstance(snake_case__       ,						snake_case__ ) else num_buckets
         UpperCAmelCase					=	lsh_attn_chunk_length
         UpperCAmelCase					=	local_attn_chunk_length
         UpperCAmelCase					=	lsh_num_chunks_after
         UpperCAmelCase					=	lsh_num_chunks_before
         UpperCAmelCase					=	local_num_chunks_after
         UpperCAmelCase					=	local_num_chunks_before
         UpperCAmelCase					=	hidden_act
         UpperCAmelCase					=	feed_forward_size
         UpperCAmelCase					=	hidden_dropout_prob
         UpperCAmelCase					=	lsh_attention_probs_dropout_prob
         UpperCAmelCase					=	local_attention_probs_dropout_prob
         UpperCAmelCase					=	max_position_embeddings
         UpperCAmelCase					=	initializer_range
         UpperCAmelCase					=	layer_norm_eps
         UpperCAmelCase					=	axial_pos_embds
         UpperCAmelCase					=	tuple(snake_case__ )
         UpperCAmelCase					=	tuple(snake_case__ )
         UpperCAmelCase					=	axial_norm_std
         UpperCAmelCase					=	chunk_size_lm_head
         UpperCAmelCase					=	attn_layers
         UpperCAmelCase					=	use_cache
         UpperCAmelCase					=	classifier_dropout
         super().__init__(
             pad_token_id=snake_case__       ,						eos_token_id=snake_case__       ,						is_decoder=snake_case__       ,						tie_word_embeddings=snake_case__       ,						**snake_case__       ,						)
