






import os
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple

import sentencepiece as spm

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


UpperCAmelCase__				   =		logging.get_logger(__name__)

UpperCAmelCase__				   =		"▁"

UpperCAmelCase__				   =		{"vocab_file": "sentencepiece.bpe.model"}

UpperCAmelCase__				   =		{
    "vocab_file": {
        "xlm-roberta-base": "https://huggingface.co/xlm-roberta-base/resolve/main/sentencepiece.bpe.model",
        "xlm-roberta-large": "https://huggingface.co/xlm-roberta-large/resolve/main/sentencepiece.bpe.model",
        "xlm-roberta-large-finetuned-conll02-dutch": (
            "https://huggingface.co/xlm-roberta-large-finetuned-conll02-dutch/resolve/main/sentencepiece.bpe.model"
        ),
        "xlm-roberta-large-finetuned-conll02-spanish": (
            "https://huggingface.co/xlm-roberta-large-finetuned-conll02-spanish/resolve/main/sentencepiece.bpe.model"
        ),
        "xlm-roberta-large-finetuned-conll03-english": (
            "https://huggingface.co/xlm-roberta-large-finetuned-conll03-english/resolve/main/sentencepiece.bpe.model"
        ),
        "xlm-roberta-large-finetuned-conll03-german": (
            "https://huggingface.co/xlm-roberta-large-finetuned-conll03-german/resolve/main/sentencepiece.bpe.model"
        ),
    }
}

UpperCAmelCase__				   =		{
    "xlm-roberta-base": 512,
    "xlm-roberta-large": 512,
    "xlm-roberta-large-finetuned-conll02-dutch": 512,
    "xlm-roberta-large-finetuned-conll02-spanish": 512,
    "xlm-roberta-large-finetuned-conll03-english": 512,
    "xlm-roberta-large-finetuned-conll03-german": 512,
}






class 		lowercase_					(	lowercase   ):






      '''simple docstring'''




      __snake_case       =  VOCAB_FILES_NAMES
      __snake_case       =  PRETRAINED_VOCAB_FILES_MAP
      __snake_case       =  PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
      __snake_case       =  ['''input_ids''', '''attention_mask''']







      def __init__(      self      :						Optional[int] ,					__UpperCAmelCase      :						int ,					__UpperCAmelCase      :						Optional[Any]="<s>" ,					__UpperCAmelCase      :						Optional[int]="</s>" ,					__UpperCAmelCase      :						str="</s>" ,					__UpperCAmelCase      :						int="<s>" ,					__UpperCAmelCase      :						Union[str, Any]="<unk>" ,					__UpperCAmelCase      :						Optional[Any]="<pad>" ,					__UpperCAmelCase      :						str="<mask>" ,					__UpperCAmelCase      :						Optional[Dict[str, Any]] = None ,					**__UpperCAmelCase      :						Any ,					)    ->None:




       """simple docstring"""


       a		  =      AddedToken(__UpperCAmelCase ,					lstrip=__UpperCAmelCase ,					rstrip=__UpperCAmelCase  ) if isinstance(__UpperCAmelCase ,					__UpperCAmelCase  ) else mask_token

       a		  =      {} if sp_model_kwargs is None else sp_model_kwargs

       super().__init__(
           bos_token=__UpperCAmelCase ,					eos_token=__UpperCAmelCase ,					unk_token=__UpperCAmelCase ,					sep_token=__UpperCAmelCase ,					cls_token=__UpperCAmelCase ,					pad_token=__UpperCAmelCase ,					mask_token=__UpperCAmelCase ,					sp_model_kwargs=self.sp_model_kwargs ,					**__UpperCAmelCase ,					)

       a		  =      spm.SentencePieceProcessor(**self.sp_model_kwargs  )
       self.sp_model.Load(str(__UpperCAmelCase  )  )
       a		  =      vocab_file

       # Original fairseq vocab and spm vocab must be "aligned":
       # Vocab    |    0    |    1    |   2    |    3    |  4  |  5  |  6  |   7   |   8   |  9
       # -------- | ------- | ------- | ------ | ------- | --- | --- | --- | ----- | ----- | ----
       # fairseq  | '<s>'   | '<pad>' | '</s>' | '<unk>' | ',' | '.' | '▁' | 's'   | '▁de' | '-'
       # spm      | '<unk>' | '<s>'   | '</s>' | ','     | '.' | '▁' | 's' | '▁de' | '-'   | '▁a'

       # Mimic fairseq token-to-id alignment for the first 4 token
       a		  =      {'''<s>''': 0, '''<pad>''': 1, '''</s>''': 2, '''<unk>''': 3}

       # The first "real" token "," has position 4 in the original fairseq vocab and position 3 in the spm vocab
       a		  =      1

       a		  =      len(self.sp_model  ) + self.fairseq_offset
       a		  =      {v: k for k, v in self.fairseq_tokens_to_ids.items()}







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




       """simple docstring"""


       a		  =      self.__dict__.copy()
       a		  =      None
       a		  =      self.sp_model.serialized_model_proto()
       return state







      def __setstate__(      self      :						Tuple ,					__UpperCAmelCase      :						Tuple  )    ->Optional[Any]:




       """simple docstring"""


       a		  =      d

       # for backward compatibility
       if not hasattr(self ,					'''sp_model_kwargs'''  ):
        a		  =      {}

       a		  =      spm.SentencePieceProcessor(**self.sp_model_kwargs  )
       self.sp_model.LoadFromSerializedProto(self.sp_model_proto  )







      def       __lowerCAmelCase					(      self      :						Optional[Any] ,					__UpperCAmelCase      :						List[int] ,					__UpperCAmelCase      :						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]
       a		  =      [self.cls_token_id]
       a		  =      [self.sep_token_id]
       return cls + token_ids_a + sep + sep + token_ids_a + sep







      def       __lowerCAmelCase					(      self      :						str ,					__UpperCAmelCase      :						List[int] ,					__UpperCAmelCase      :						Optional[List[int]] = None ,					__UpperCAmelCase      :						bool = False  )    ->List[int]:




       """simple docstring"""


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

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







      def       __lowerCAmelCase					(      self      :						Optional[Any] ,					__UpperCAmelCase      :						List[int] ,					__UpperCAmelCase      :						Optional[List[int]] = None  )    ->List[int]:




       """simple docstring"""


       a		  =      [self.sep_token_id]
       a		  =      [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]







      @property
      def       __lowerCAmelCase					(      self      :						Tuple  )    ->List[Any]:




       """simple docstring"""


       return len(self.sp_model  ) + self.fairseq_offset + 1  # Add the <mask> token







      def       __lowerCAmelCase					(      self      :						Dict  )    ->Tuple:




       """simple docstring"""


       a		  =      {self.convert_ids_to_tokens(__UpperCAmelCase  ): i for i in range(self.vocab_size  )}
       vocab.update(self.added_tokens_encoder  )
       return vocab







      def       __lowerCAmelCase					(      self      :						Union[str, Any] ,					__UpperCAmelCase      :						str  )    ->List[str]:




       """simple docstring"""


       return self.sp_model.encode(__UpperCAmelCase ,					out_type=__UpperCAmelCase  )







      def       __lowerCAmelCase					(      self      :						Optional[Any] ,					__UpperCAmelCase      :						Optional[int]  )    ->Optional[int]:




       """simple docstring"""


       if token in self.fairseq_tokens_to_ids:
        return self.fairseq_tokens_to_ids[token]
       a		  =      self.sp_model.PieceToId(__UpperCAmelCase  )

       # Need to return unknown token if the SP model returned 0
       return spm_id + self.fairseq_offset if spm_id else self.unk_token_id







      def       __lowerCAmelCase					(      self      :						Dict ,					__UpperCAmelCase      :						str  )    ->List[str]:




       """simple docstring"""


       if index in self.fairseq_ids_to_tokens:
        return self.fairseq_ids_to_tokens[index]
       return self.sp_model.IdToPiece(index - self.fairseq_offset  )







      def       __lowerCAmelCase					(      self      :						Tuple ,					__UpperCAmelCase      :						Optional[Any]  )    ->Union[str, Any]:




       """simple docstring"""


       a		  =      ''''''.join(__UpperCAmelCase  ).replace(__UpperCAmelCase ,					''' '''  ).strip()
       return out_string







      def       __lowerCAmelCase					(      self      :						Any ,					__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
       a		  =      os.path.join(
           __UpperCAmelCase ,					(filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file''']  )

       if os.path.abspath(self.vocab_file  ) != os.path.abspath(__UpperCAmelCase  ) and os.path.isfile(self.vocab_file  ):
        copyfile(self.vocab_file ,					__UpperCAmelCase  )
       elif not os.path.isfile(self.vocab_file  ):
        with open(__UpperCAmelCase ,					'''wb'''  ) as fi:
         a		  =      self.sp_model.serialized_model_proto()
         fi.write(__UpperCAmelCase  )

       return (out_vocab_file,)










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


UpperCAmelCase__				   =		logging.get_logger(__name__)

UpperCAmelCase__				   =		{}






class 		lowercase_					(	lowercase   ):






      '''simple docstring'''




      __snake_case       =  '''llama'''
      __snake_case       =  ['''past_key_values''']







      def __init__(      self      :						Optional[Any] ,					__UpperCAmelCase      :						Union[str, Any]=32_000 ,					__UpperCAmelCase      :						str=4_096 ,					__UpperCAmelCase      :						int=11_008 ,					__UpperCAmelCase      :						Tuple=32 ,					__UpperCAmelCase      :						Optional[int]=32 ,					__UpperCAmelCase      :						List[str]=None ,					__UpperCAmelCase      :						Union[str, Any]="silu" ,					__UpperCAmelCase      :						Tuple=2_048 ,					__UpperCAmelCase      :						Optional[Any]=0.02 ,					__UpperCAmelCase      :						Any=1e-6 ,					__UpperCAmelCase      :						Union[str, Any]=True ,					__UpperCAmelCase      :						Optional[int]=0 ,					__UpperCAmelCase      :						Optional[int]=1 ,					__UpperCAmelCase      :						Optional[int]=2 ,					__UpperCAmelCase      :						Tuple=1 ,					__UpperCAmelCase      :						List[str]=False ,					__UpperCAmelCase      :						Tuple=None ,					**__UpperCAmelCase      :						Tuple ,					)    ->str:




       """simple docstring"""


       a		  =      vocab_size
       a		  =      max_position_embeddings
       a		  =      hidden_size
       a		  =      intermediate_size
       a		  =      num_hidden_layers
       a		  =      num_attention_heads

       # for backward compatibility
       if num_key_value_heads is None:
        a		  =      num_attention_heads

       a		  =      num_key_value_heads
       a		  =      hidden_act
       a		  =      initializer_range
       a		  =      rms_norm_eps
       a		  =      pretraining_tp
       a		  =      use_cache
       a		  =      rope_scaling
       self._rope_scaling_validation()

       super().__init__(
           pad_token_id=__UpperCAmelCase ,					bos_token_id=__UpperCAmelCase ,					eos_token_id=__UpperCAmelCase ,					tie_word_embeddings=__UpperCAmelCase ,					**__UpperCAmelCase ,					)







      def       __lowerCAmelCase					(      self      :						Tuple  )    ->Tuple:




       """simple docstring"""


       if self.rope_scaling is None:
        return

       if not isinstance(self.rope_scaling ,					__UpperCAmelCase  ) or len(self.rope_scaling  ) != 2:
        raise ValueError(
            '''`rope_scaling` must be a dictionary with with two fields, `name` and `factor`, '''
            F"""got {self.rope_scaling}"""  )
       a		  =      self.rope_scaling.get('''type''' ,					__UpperCAmelCase  )
       a		  =      self.rope_scaling.get('''factor''' ,					__UpperCAmelCase  )
       if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]:
        raise ValueError(
            F"""`rope_scaling`'s name field must be one of ['linear', 'dynamic'], got {rope_scaling_type}"""  )
       if rope_scaling_factor is None or not isinstance(__UpperCAmelCase ,					__UpperCAmelCase  ) or rope_scaling_factor <= 1.0:
        raise ValueError(F"""`rope_scaling`'s factor field must be an float > 1, got {rope_scaling_factor}"""  )


