
    sg)                         d Z ddlZddlmZmZmZ ddlmZ ddlm	Z	m
Z
 ddlmZ ddlmZ d	d
lmZ  ej"                  e      ZddddZ G d de      Zy)z*Fast Tokenization class for model DeBERTa.    N)ListOptionalTuple)pre_tokenizers   )
AddedTokenBatchEncoding)PreTrainedTokenizerFast)logging   )DebertaTokenizerz
vocab.jsonz
merges.txtztokenizer.json)
vocab_filemerges_filetokenizer_filec                   (    e Zd ZdZeZg dZeZ	 	 	 	 	 	 	 	 	 	 	 	 d fd	Z	e
defd       Zej                  d        Z	 ddee   deee      dee   fd	Z	 ddee   deee      dee   fd
Zdef fdZdef fdZddedee   dee   fdZ xZS )DebertaTokenizerFasta  
    Construct a "fast" DeBERTa tokenizer (backed by HuggingFace's *tokenizers* library). Based on byte-level
    Byte-Pair-Encoding.

    This tokenizer has been trained to treat spaces like parts of the tokens (a bit like sentencepiece) so a word will
    be encoded differently whether it is at the beginning of the sentence (without space) or not:

    ```python
    >>> from transformers import DebertaTokenizerFast

    >>> tokenizer = DebertaTokenizerFast.from_pretrained("microsoft/deberta-base")
    >>> tokenizer("Hello world")["input_ids"]
    [1, 31414, 232, 2]

    >>> tokenizer(" Hello world")["input_ids"]
    [1, 20920, 232, 2]
    ```

    You can get around that behavior by passing `add_prefix_space=True` when instantiating this tokenizer, but since
    the model was not pretrained this way, it might yield a decrease in performance.

    <Tip>

    When used with `is_split_into_words=True`, this tokenizer needs to be instantiated with `add_prefix_space=True`.

    </Tip>

    This tokenizer inherits from [`PreTrainedTokenizerFast`] which contains most of the main methods. Users should
    refer to this superclass for more information regarding those methods.

    Args:
        vocab_file (`str`, *optional*):
            Path to the vocabulary file.
        merges_file (`str`, *optional*):
            Path to the merges file.
        tokenizer_file (`str`, *optional*):
            The path to a tokenizer file to use instead of the vocab file.
        errors (`str`, *optional*, defaults to `"replace"`):
            Paradigm to follow when decoding bytes to UTF-8. See
            [bytes.decode](https://docs.python.org/3/library/stdtypes.html#bytes.decode) for more information.
        bos_token (`str`, *optional*, defaults to `"[CLS]"`):
            The beginning of sequence token.
        eos_token (`str`, *optional*, defaults to `"[SEP]"`):
            The end of sequence token.
        sep_token (`str`, *optional*, defaults to `"[SEP]"`):
            The separator token, which is used when building a sequence from multiple sequences, e.g. two sequences for
            sequence classification or for a text and a question for question answering. It is also used as the last
            token of a sequence built with special tokens.
        cls_token (`str`, *optional*, defaults to `"[CLS]"`):
            The classifier token which is used when doing sequence classification (classification of the whole sequence
            instead of per-token classification). It is the first token of the sequence when built with special tokens.
        unk_token (`str`, *optional*, defaults to `"[UNK]"`):
            The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this
            token instead.
        pad_token (`str`, *optional*, defaults to `"[PAD]"`):
            The token used for padding, for example when batching sequences of different lengths.
        mask_token (`str`, *optional*, defaults to `"[MASK]"`):
            The token used for masking values. This is the token used when training this model with masked language
            modeling. This is the token which the model will try to predict.
        add_prefix_space (`bool`, *optional*, defaults to `False`):
            Whether or not to add an initial space to the input. This allows to treat the leading word just as any
            other word. (Deberta tokenizer detect beginning of words by the preceding space).
    )	input_idsattention_masktoken_type_idsc                    t        |   ||f|||||	|||
||d
| |j                  dd      | _        t	        j
                  | j                  j                  j                               }|j                  d|      |k7  r;t        t        |j                  d            }||d<    |di || j                  _        || _        y )N)
r   errors	bos_token	eos_token	unk_token	sep_token	cls_token	pad_token
mask_tokenadd_prefix_spaceadd_bos_tokenFr   type )super__init__popr    jsonloadsbackend_tokenizerpre_tokenizer__getstate__getgetattrr   r   )selfr   r   r   r   r   r   r   r   r   r   r   r   kwargspre_tok_statepre_tok_class	__class__s                   h/var/www/html/venv/lib/python3.12/site-packages/transformers/models/deberta/tokenization_deberta_fast.pyr$   zDebertaTokenizerFast.__init__f   s      		
 *!-	
 	
 $ZZ?

4#9#9#G#G#T#T#VW/1ABFVV#NM4E4Ef4MNM0@M,-3@3Q=3QD""0 0    returnc                     | j                   "| j                  rt        j                  d       yt	        | j                         S )a?  
        `str`: Mask token, to use when training a model with masked-language modeling. Log an error if used while not
        having been set.

        Deberta tokenizer has a special mask token to be used in the fill-mask pipeline. The mask token will greedily
        comprise the space before the *[MASK]*.
        Nz(Using mask_token, but it is not set yet.)_mask_tokenverboseloggererrorstr)r-   s    r2   r   zDebertaTokenizerFast.mask_token   s8     #||GH4##$$r3   c                 R    t        |t              rt        |dd      n|}|| _        y)zg
        Overriding the default behavior of the mask token to have it eat the space before it.
        TF)lstriprstripN)
isinstancer:   r   r6   )r-   values     r2   r   zDebertaTokenizerFast.mask_token   s)     AK5RU@V
5e<\a r3   token_ids_0token_ids_1c                     || j                   g|z   | j                  gz   S | j                   g}| j                  g}||z   |z   |z   |z   S )a  
        Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and
        adding special tokens. A DeBERTa sequence has the following format:

        - single sequence: [CLS] X [SEP]
        - pair of sequences: [CLS] A [SEP] B [SEP]

        Args:
            token_ids_0 (`List[int]`):
                List of IDs to which the special tokens will be added.
            token_ids_1 (`List[int]`, *optional*):
                Optional second list of IDs for sequence pairs.

        Returns:
            `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens.
        )cls_token_idsep_token_id)r-   r@   rA   clsseps        r2    build_inputs_with_special_tokensz5DebertaTokenizerFast.build_inputs_with_special_tokens   sb    & %%&48I8I7JJJ  !  ![ 3&4s::r3   c                     | j                   g}| j                  g}|t        ||z   |z         dgz  S t        ||z   |z         dgz  t        ||z         dgz  z   S )a  
        Create a mask from the two sequences passed to be used in a sequence-pair classification task. A DeBERTa
        sequence pair mask has the following format:

        ```
        0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1
        | first sequence    | second sequence |
        ```

        If `token_ids_1` is `None`, this method only returns the first portion of the mask (0s).

        Args:
            token_ids_0 (`List[int]`):
                List of IDs.
            token_ids_1 (`List[int]`, *optional*):
                Optional second list of IDs for sequence pairs.

        Returns:
            `List[int]`: List of [token type IDs](../glossary#token-type-ids) according to the given sequence(s).
        r   r   )rD   rC   len)r-   r@   rA   rF   rE   s        r2   $create_token_type_ids_from_sequencesz9DebertaTokenizerFast.create_token_type_ids_from_sequences   st    .   !  !s[(3./1#553$s*+qc1Cc8I4JaS4PPPr3   c                     |j                  dd      }| j                  s!|rJ d| j                  j                   d       t	        |   |i |S Nis_split_into_wordsFzYou need to instantiate z? with add_prefix_space=True to use it with pretokenized inputs.)r+   r   r1   __name__r#   _batch_encode_plusr-   argsr.   rM   r1   s       r2   rO   z'DebertaTokenizerFast._batch_encode_plus   sb    $jj)>F$$,? 	
&t~~'>'>&? @2 2	
?
 w)4:6::r3   c                     |j                  dd      }| j                  s!|rJ d| j                  j                   d       t	        |   |i |S rL   )r+   r   r1   rN   r#   _encode_plusrP   s       r2   rS   z!DebertaTokenizerFast._encode_plus   sb    $jj)>F$$,? 	
&t~~'>'>&? @2 2	
?
 w#T4V44r3   save_directoryfilename_prefixc                 f    | j                   j                  j                  ||      }t        |      S )N)name)
_tokenizermodelsavetuple)r-   rT   rU   filess       r2   save_vocabularyz$DebertaTokenizerFast.save_vocabulary   s+    %%**>*PU|r3   )NNNreplace[CLS][SEP]r`   r_   z[UNK]z[PAD]z[MASK]F)N)rN   
__module____qualname____doc__VOCAB_FILES_NAMESvocab_files_namesmodel_input_namesr   slow_tokenizer_classr$   propertyr:   r   setterr   intr   rG   rJ   r	   rO   rS   r   r]   __classcell__)r1   s   @r2   r   r   !   s)   >@ *I+ '1R %C % % ! ! JN;9;3;DI3F;	c;4 JNQ9Q3;DI3FQ	cQ>;] ;5} 5c HSM ]bcf]g r3   r   )rc   r&   typingr   r   r   
tokenizersr   tokenization_utils_baser   r	   tokenization_utils_fastr
   utilsr   tokenization_debertar   
get_loggerrN   r8   rd   r   r"   r3   r2   <module>rs      sS    1  ( ( % @ >  2 
		H	%#/`pq V2 Vr3   