

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

import regex as re

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import logging


__UpperCAmelCase      = logging.get_logger(__name__)

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

__UpperCAmelCase      = {
    """vocab_file""": {
        """roberta-base""": """https://huggingface.co/roberta-base/resolve/main/vocab.json""",
        """roberta-large""": """https://huggingface.co/roberta-large/resolve/main/vocab.json""",
        """roberta-large-mnli""": """https://huggingface.co/roberta-large-mnli/resolve/main/vocab.json""",
        """distilroberta-base""": """https://huggingface.co/distilroberta-base/resolve/main/vocab.json""",
        """roberta-base-openai-detector""": """https://huggingface.co/roberta-base-openai-detector/resolve/main/vocab.json""",
        """roberta-large-openai-detector""": (
            """https://huggingface.co/roberta-large-openai-detector/resolve/main/vocab.json"""
        ),
    },
    """merges_file""": {
        """roberta-base""": """https://huggingface.co/roberta-base/resolve/main/merges.txt""",
        """roberta-large""": """https://huggingface.co/roberta-large/resolve/main/merges.txt""",
        """roberta-large-mnli""": """https://huggingface.co/roberta-large-mnli/resolve/main/merges.txt""",
        """distilroberta-base""": """https://huggingface.co/distilroberta-base/resolve/main/merges.txt""",
        """roberta-base-openai-detector""": """https://huggingface.co/roberta-base-openai-detector/resolve/main/merges.txt""",
        """roberta-large-openai-detector""": (
            """https://huggingface.co/roberta-large-openai-detector/resolve/main/merges.txt"""
        ),
    },
}

__UpperCAmelCase      = {
    """roberta-base""": 512,
    """roberta-large""": 512,
    """roberta-large-mnli""": 512,
    """distilroberta-base""": 512,
    """roberta-base-openai-detector""": 512,
    """roberta-large-openai-detector""": 512,
}





@lru_cache()
def 	snake_case_      ()			->	Optional[Any]:
   __lowerCAmelCase  :			int					    =						(
       list(range(ord("""!"""     )					,   ord("""~"""     ) + 1     )     ) + list(range(ord("""¡"""     )					,   ord("""¬"""     ) + 1     )     ) + list(range(ord("""®"""     )					,   ord("""ÿ"""     ) + 1     )     )
   )
   __lowerCAmelCase  :			Tuple					    =						bs[:]
   __lowerCAmelCase  :			Optional[Any]					    =						0
   for b in range(2**8     ):
      if b not in bs:
         bs.append(__A     )
         cs.append(2**8 + n     )
         n += 1
   __lowerCAmelCase  :			List[Any]					    =						[chr(__A     ) for n in cs]
   return dict(zip(__A					,   __A     )     )





def 	snake_case_      (__A     :						Dict     )			->	Any:
   __lowerCAmelCase  :			List[Any]					    =						set()
   __lowerCAmelCase  :			int					    =						word[0]
   for char in word[1:]:
      pairs.add((prev_char, char)     )
      __lowerCAmelCase  :			List[Any]					    =						char
   return pairs



class SCREAMING_SNAKE_CASE	(  a_ ):
  """simple docstring"""






  lowerCamelCase    :			Union[str, Any]						      =VOCAB_FILES_NAMES
  lowerCamelCase    :			int						      =PRETRAINED_VOCAB_FILES_MAP
  lowerCamelCase    :			str						      =PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
  lowerCamelCase    :			int						      =["input_ids", "attention_mask"]







  def __init__(			self :    int       ,							lowerCAmelCase :    Optional[int]       ,							lowerCAmelCase :    int       ,							lowerCAmelCase :    Dict="replace"       ,							lowerCAmelCase :    List[Any]="<s>"       ,							lowerCAmelCase :    Optional[int]="</s>"       ,							lowerCAmelCase :    Optional[Any]="</s>"       ,							lowerCAmelCase :    Any="<s>"       ,							lowerCAmelCase :    str="<unk>"       ,							lowerCAmelCase :    Union[str, Any]="<pad>"       ,							lowerCAmelCase :    int="<mask>"       ,							lowerCAmelCase :    int=False       ,							**lowerCAmelCase :    List[Any]       ,							)							->	Tuple:






     """simple docstring"""







     __lowerCAmelCase  :			Any					    =						AddedToken(lowerCAmelCase       ,							lstrip=lowerCAmelCase       ,							rstrip=lowerCAmelCase    ) if isinstance(lowerCAmelCase       ,							lowerCAmelCase    ) else bos_token
     __lowerCAmelCase  :			Dict					    =						AddedToken(lowerCAmelCase       ,							lstrip=lowerCAmelCase       ,							rstrip=lowerCAmelCase    ) if isinstance(lowerCAmelCase       ,							lowerCAmelCase    ) else eos_token
     __lowerCAmelCase  :			str					    =						AddedToken(lowerCAmelCase       ,							lstrip=lowerCAmelCase       ,							rstrip=lowerCAmelCase    ) if isinstance(lowerCAmelCase       ,							lowerCAmelCase    ) else sep_token
     __lowerCAmelCase  :			Dict					    =						AddedToken(lowerCAmelCase       ,							lstrip=lowerCAmelCase       ,							rstrip=lowerCAmelCase    ) if isinstance(lowerCAmelCase       ,							lowerCAmelCase    ) else cls_token
     __lowerCAmelCase  :			Tuple					    =						AddedToken(lowerCAmelCase       ,							lstrip=lowerCAmelCase       ,							rstrip=lowerCAmelCase    ) if isinstance(lowerCAmelCase       ,							lowerCAmelCase    ) else unk_token
     __lowerCAmelCase  :			Optional[Any]					    =						AddedToken(lowerCAmelCase       ,							lstrip=lowerCAmelCase       ,							rstrip=lowerCAmelCase    ) if isinstance(lowerCAmelCase       ,							lowerCAmelCase    ) else pad_token

     # Mask token behave like a normal word, i.e. include the space before it
     __lowerCAmelCase  :			Optional[Any]					    =						AddedToken(lowerCAmelCase       ,							lstrip=lowerCAmelCase       ,							rstrip=lowerCAmelCase    ) if isinstance(lowerCAmelCase       ,							lowerCAmelCase    ) else mask_token

     super().__init__(
         errors=lowerCAmelCase       ,							bos_token=lowerCAmelCase       ,							eos_token=lowerCAmelCase       ,							unk_token=lowerCAmelCase       ,							sep_token=lowerCAmelCase       ,							cls_token=lowerCAmelCase       ,							pad_token=lowerCAmelCase       ,							mask_token=lowerCAmelCase       ,							add_prefix_space=lowerCAmelCase       ,							**lowerCAmelCase       ,							)

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

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







  @property
  def        SCREAMING_SNAKE_CASE    (			self :    List[str]    )							->	Optional[Any]:






     """simple docstring"""







     return len(self.encoder    )







  def        SCREAMING_SNAKE_CASE    (			self :    Optional[Any]    )							->	Dict:






     """simple docstring"""







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







  def        SCREAMING_SNAKE_CASE    (			self :    Optional[Any]       ,							lowerCAmelCase :    Union[str, Any]    )							->	str:






     """simple docstring"""







     if token in self.cache:
        return self.cache[token]
     __lowerCAmelCase  :			Union[str, Any]					    =						tuple(lowerCAmelCase    )
     __lowerCAmelCase  :			Optional[int]					    =						get_pairs(lowerCAmelCase    )

     if not pairs:
        return token

     while True:
        __lowerCAmelCase  :			Any					    =						min(lowerCAmelCase       ,							key=lambda lowerCAmelCase    : self.bpe_ranks.get(lowerCAmelCase       ,							float("""inf"""    )    )    )
        if bigram not in self.bpe_ranks:
           break
        __lowerCAmelCase    ,__lowerCAmelCase  :			int					    =						bigram
        __lowerCAmelCase  :			Union[str, Any]					    =						[]
        __lowerCAmelCase  :			Optional[Any]					    =						0
        while i < len(lowerCAmelCase    ):
           try:
              __lowerCAmelCase  :			Optional[Any]					    =						word.index(lowerCAmelCase       ,							lowerCAmelCase    )
           except ValueError:
              new_word.extend(word[i:]    )
              break
           else:
              new_word.extend(word[i:j]    )
              __lowerCAmelCase  :			Tuple					    =						j

           if word[i] == first and i < len(lowerCAmelCase    ) - 1 and word[i + 1] == second:
              new_word.append(first + second    )
              i += 2
           else:
              new_word.append(word[i]    )
              i += 1
        __lowerCAmelCase  :			Tuple					    =						tuple(lowerCAmelCase    )
        __lowerCAmelCase  :			Dict					    =						new_word
        if len(lowerCAmelCase    ) == 1:
           break
        else:
           __lowerCAmelCase  :			Optional[int]					    =						get_pairs(lowerCAmelCase    )
     __lowerCAmelCase  :			str					    =						""" """.join(lowerCAmelCase    )
     __lowerCAmelCase  :			int					    =						word
     return word







  def        SCREAMING_SNAKE_CASE    (			self :    Optional[int]       ,							lowerCAmelCase :    List[str]    )							->	Union[str, Any]:






     """simple docstring"""







     __lowerCAmelCase  :			Dict					    =						[]
     for token in re.findall(self.pat       ,							lowerCAmelCase    ):
        __lowerCAmelCase  :			Union[str, Any]					    =						"""""".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(lowerCAmelCase    ).split(""" """    )    )
     return bpe_tokens







  def        SCREAMING_SNAKE_CASE    (			self :    List[Any]       ,							lowerCAmelCase :    Tuple    )							->	Any:






     """simple docstring"""







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







  def        SCREAMING_SNAKE_CASE    (			self :    List[Any]       ,							lowerCAmelCase :    Union[str, Any]    )							->	List[Any]:






     """simple docstring"""







     return self.decoder.get(lowerCAmelCase    )







  def        SCREAMING_SNAKE_CASE    (			self :    int       ,							lowerCAmelCase :    Tuple    )							->	int:






     """simple docstring"""







     __lowerCAmelCase  :			Union[str, Any]					    =						"""""".join(lowerCAmelCase    )
     __lowerCAmelCase  :			Optional[int]					    =						bytearray([self.byte_decoder[c] for c in text]    ).decode("""utf-8"""       ,							errors=self.errors    )
     return text







  def        SCREAMING_SNAKE_CASE    (			self :    List[str]       ,							lowerCAmelCase :    str       ,							lowerCAmelCase :    Optional[str] = None    )							->	Tuple[str]:






     """simple docstring"""







     if not os.path.isdir(lowerCAmelCase    ):
        logger.error(f'''Vocabulary path ({save_directory}) should be a directory'''    )
        return
     __lowerCAmelCase  :			Tuple					    =						os.path.join(
         lowerCAmelCase       ,							(filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""]    )
     __lowerCAmelCase  :			Any					    =						os.path.join(
         lowerCAmelCase       ,							(filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""merges_file"""]    )

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

     __lowerCAmelCase  :			Optional[Any]					    =						0
     with open(lowerCAmelCase       ,							"""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 lowerCAmelCase    : 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!"""    )
              __lowerCAmelCase  :			Any					    =						token_index
           writer.write(""" """.join(lowerCAmelCase    ) + """\n"""    )
           index += 1

     return vocab_file, merge_file







  def        SCREAMING_SNAKE_CASE    (			self :    str       ,							lowerCAmelCase :    List[int]       ,							lowerCAmelCase :    Optional[List[int]] = None    )							->	List[int]:






     """simple docstring"""







     if token_ids_a is None:
        return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
     __lowerCAmelCase  :			List[Any]					    =						[self.cls_token_id]
     __lowerCAmelCase  :			int					    =						[self.sep_token_id]
     return cls + token_ids_a + sep + sep + token_ids_a + sep







  def        SCREAMING_SNAKE_CASE    (			self :    Optional[int]       ,							lowerCAmelCase :    List[int]       ,							lowerCAmelCase :    Optional[List[int]] = None       ,							lowerCAmelCase :    bool = False    )							->	List[int]:






     """simple docstring"""







     if already_has_special_tokens:
        return super().get_special_tokens_mask(
            token_ids_a=lowerCAmelCase       ,							token_ids_a=lowerCAmelCase       ,							already_has_special_tokens=lowerCAmelCase    )

     if token_ids_a is None:
        return [1] + ([0] * len(lowerCAmelCase    )) + [1]
     return [1] + ([0] * len(lowerCAmelCase    )) + [1, 1] + ([0] * len(lowerCAmelCase    )) + [1]







  def        SCREAMING_SNAKE_CASE    (			self :    Dict       ,							lowerCAmelCase :    List[int]       ,							lowerCAmelCase :    Optional[List[int]] = None    )							->	List[int]:






     """simple docstring"""







     __lowerCAmelCase  :			Tuple					    =						[self.sep_token_id]
     __lowerCAmelCase  :			Union[str, Any]					    =						[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        SCREAMING_SNAKE_CASE    (			self :    List[str]       ,							lowerCAmelCase :    List[str]       ,							lowerCAmelCase :    Dict=False       ,							**lowerCAmelCase :    Any    )							->	int:






     """simple docstring"""







     __lowerCAmelCase  :			Tuple					    =						kwargs.pop("""add_prefix_space"""       ,							self.add_prefix_space    )
     if (is_split_into_words or add_prefix_space) and (len(lowerCAmelCase    ) > 0 and not text[0].isspace()):
        __lowerCAmelCase  :			Tuple					    =						""" """ + text
     return (text, kwargs)










import math
from collections import OrderedDict

import torch
from packaging import version
from torch import Tensor, nn

from .utils import logging


lowerCamelCase_						     =  logging.get_logger(__name__)




class 							a_ (						nn.Module		):


							'''simple docstring'''






							def __init__(		self   )					->					Any:
														'''simple docstring'''

														super().__init__()
														if version.parse(torch.__version__   ) < version.parse('1.12.0'   ):
																					raise ImportError(
																					    f'''You are using torch=={torch.__version__}, but torch>=1.12.0 is required to use '''
																					    'PytorchGELUTanh. Please upgrade torch.'   )






							def      _lowercase       (		self			,			lowercase_   )					->					Tensor:
														'''simple docstring'''

														return nn.functional.gelu(lowercase_			,			approximate='tanh'   )




class 							a_ (						nn.Module		):


							'''simple docstring'''






							def      _lowercase       (		self			,			lowercase_   )					->					Tensor:
														'''simple docstring'''

														return 0.5 * input * (1.0 + torch.tanh(math.sqrt(2.0 / math.pi   ) * (input + 0.04_47_15 * torch.pow(lowercase_			,			3.0   ))   ))




class 							a_ (						nn.Module		):


							'''simple docstring'''






							def __init__(		self			,			lowercase_ = False   )					->					Optional[Any]:
														'''simple docstring'''

														super().__init__()
														if use_gelu_python:
																					lowerCAmelCase_										=     self._gelu_python
														else:
																					lowerCAmelCase_										=     nn.functional.gelu

							def      _lowercase       (		self			,			lowercase_   )					->					Tensor:
														'''simple docstring'''

														return input * 0.5 * (1.0 + torch.erf(input / math.sqrt(2.0   )   ))






							def      _lowercase       (		self			,			lowercase_   )					->					Tensor:
														'''simple docstring'''

														return self.act(lowercase_   )




class 							a_ (						nn.Module		):


							'''simple docstring'''






							def      _lowercase       (		self			,			lowercase_   )					->					Tensor:
														'''simple docstring'''

														return 0.5 * input * (1.0 + torch.tanh(input * 0.79_78_84_56_08 * (1.0 + 0.04_47_15 * input * input)   ))




class 							a_ (						nn.Module		):


							'''simple docstring'''






							def      _lowercase       (		self			,			lowercase_   )					->					Tensor:
														'''simple docstring'''

														return input * torch.sigmoid(1.7_02 * input   )




class 							a_ (						nn.Module		):


							'''simple docstring'''






							def __init__(		self			,			lowercase_			,			lowercase_   )					->					Optional[int]:
														'''simple docstring'''

														if min > max:
																					raise ValueError(f'''min should be < max (got min: {min}, max: {max})'''   )

														super().__init__()
														lowerCAmelCase_										=     min
														lowerCAmelCase_										=     max






							def      _lowercase       (		self			,			lowercase_   )					->					Tensor:
														'''simple docstring'''

														return torch.clip(gelu(lowercase_   )			,			self.min			,			self.max   )




class 							a_ (						nn.Module		):


							'''simple docstring'''






							def __init__(		self   )					->					Union[str, Any]:
														'''simple docstring'''

														super().__init__()
														lowerCAmelCase_										=     math.sqrt(2 / math.pi   )






							def      _lowercase       (		self			,			lowercase_   )					->					Tensor:
														'''simple docstring'''

														return 0.5 * input * (1 + torch.tanh(self.precomputed_constant * (input + 0.04_47_15 * torch.pow(lowercase_			,			3   ))   ))




class 							a_ (						nn.Module		):


							'''simple docstring'''






							def      _lowercase       (		self			,			lowercase_   )					->					Tensor:
														'''simple docstring'''

														return nn.functional.silu(lowercase_   )




class 							a_ (						nn.Module		):


							'''simple docstring'''






							def __init__(		self   )					->					List[Any]:
														'''simple docstring'''

														super().__init__()
														if version.parse(torch.__version__   ) < version.parse('1.9.0'   ):
																					lowerCAmelCase_										=     self._mish_python
														else:
																					lowerCAmelCase_										=     nn.functional.mish

							def      _lowercase       (		self			,			lowercase_   )					->					Tensor:
														'''simple docstring'''

														return input * torch.tanh(nn.functional.softplus(lowercase_   )   )






							def      _lowercase       (		self			,			lowercase_   )					->					Tensor:
														'''simple docstring'''

														return self.act(lowercase_   )




class 							a_ (						nn.Module		):


							'''simple docstring'''






							def      _lowercase       (		self			,			lowercase_   )					->					Tensor:
														'''simple docstring'''

														return input




class 							a_ (						nn.Module		):


							'''simple docstring'''






							def      _lowercase       (		self			,			lowercase_			,			lowercase_=0.70_71_07			,			lowercase_=0.28_20_95   )					->					Optional[Any]:
														'''simple docstring'''

														lowerCAmelCase_										=     (input - mu).div(sigma * math.sqrt(2.0   )   )
														return 0.5 * (1.0 + torch.erf(lowercase_   ))




class 							a_ (						nn.Module		):


							'''simple docstring'''






							def      _lowercase       (		self			,			lowercase_   )					->					Any:
														'''simple docstring'''

														lowerCAmelCase_										=     nn.functional.relu(lowercase_   )
														lowerCAmelCase_										=     torch.square(lowercase_   )
														return squared




class 							a_ (						a_		):


							'''simple docstring'''






							def __getitem__(		self			,			lowercase_   )					->					Any:
														'''simple docstring'''

														lowerCAmelCase_										=     super().__getitem__(lowercase_   )
														lowerCAmelCase_						,   lowerCAmelCase_										=     content if isinstance(lowercase_			,			lowercase_   ) else (content, {})
														return cls(**lowercase_   )


lowerCamelCase_						     =  {
    """gelu""": GELUActivation,
    """gelu_10""": (ClippedGELUActivation, {"""min""": -1_0, """max""": 1_0}),
    """gelu_fast""": FastGELUActivation,
    """gelu_new""": NewGELUActivation,
    """gelu_python""": (GELUActivation, {"""use_gelu_python""": True}),
    """gelu_pytorch_tanh""": PytorchGELUTanh,
    """gelu_accurate""": AccurateGELUActivation,
    """laplace""": LaplaceActivation,
    """linear""": LinearActivation,
    """mish""": MishActivation,
    """quick_gelu""": QuickGELUActivation,
    """relu""": nn.ReLU,
    """relu2""": ReLUSquaredActivation,
    """relu6""": nn.ReLUa,
    """sigmoid""": nn.Sigmoid,
    """silu""": SiLUActivation,
    """swish""": SiLUActivation,
    """tanh""": nn.Tanh,
}
lowerCamelCase_						     =  ClassInstantier(ACTaCLS)
def      lowerCamelCase					(						a_	)	->			str:
							if activation_string in ACTaFN:
														return ACTaFN[activation_string]
							else:
														raise KeyError(F'''function {activation_string} not found in ACT2FN mapping {list(ACTaFN.keys()	)}'''	)


# For backwards compatibility with: from activations import gelu_python
lowerCamelCase_						     =  get_activation("""gelu_python""")
lowerCamelCase_						     =  get_activation("""gelu_new""")
lowerCamelCase_						     =  get_activation("""gelu""")
lowerCamelCase_						     =  get_activation("""gelu_fast""")
lowerCamelCase_						     =  get_activation("""quick_gelu""")
lowerCamelCase_						     =  get_activation("""silu""")
lowerCamelCase_						     =  get_activation("""mish""")



lowerCamelCase_						     =  get_activation("""linear""")



