
    sgw                         d Z ddlZddlmZmZ ddlmZ ddl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$Tokenization classes for OpenAI GPT.    N)OptionalTuple)pre_tokenizers   )BatchEncoding)PreTrainedTokenizerFast)logging   )GPT2Tokenizerz
vocab.jsonz
merges.txtztokenizer.json)
vocab_filemerges_filetokenizer_filec                        e Zd ZdZeZddgZeZ	 	 	 	 	 	 	 d 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 )GPT2TokenizerFasta  
    Construct a "fast" GPT-2 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 GPT2TokenizerFast

    >>> tokenizer = GPT2TokenizerFast.from_pretrained("openai-community/gpt2")
    >>> tokenizer("Hello world")["input_ids"]
    [15496, 995]

    >>> tokenizer(" Hello world")["input_ids"]
    [18435, 995]
    ```

    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*):
            Path to [tokenizers](https://github.com/huggingface/tokenizers) file (generally has a .json extension) that
            contains everything needed to load the tokenizer.
        unk_token (`str`, *optional*, defaults to `"<|endoftext|>"`):
            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.
        bos_token (`str`, *optional*, defaults to `"<|endoftext|>"`):
            The beginning of sequence token.
        eos_token (`str`, *optional*, defaults to `"<|endoftext|>"`):
            The end of sequence token.
        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. (GPT2 tokenizer detect beginning of words by the preceding space).
    	input_idsattention_maskc                 ~   t        |   d|||||||d| |j                  dd      | _        t	        j
                  | j                  j                  j                               }	|	j                  d|      |k7  r;t        t        |	j                  d            }
||	d<    |
di |	| j                  _        || _        y )N)r   r   r   	unk_token	bos_token	eos_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   kwargspre_tok_statepre_tok_class	__class__s              b/var/www/html/venv/lib/python3.12/site-packages/transformers/models/gpt2/tokenization_gpt2_fast.pyr   zGPT2TokenizerFast.__init__X   s     	 		
!#)-		
 		
 $ZZ?

4#9#9#G#G#T#T#VW/1ABFVV#NM4E4Ef4MNM0@M,-3@3Q=3QD""0 0    returnc                     |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   r)   __name__r   _batch_encode_plusr%   argsr&   r/   r)   s       r*   r1   z$GPT2TokenizerFast._batch_encode_plusx   sb    $jj)>F$$,? 	
&t~~'>'>&? @2 2	
?
 w)4:6::r+   c                     |j                  dd      }| j                  s!|rJ d| j                  j                   d       t	        |   |i |S r.   )r#   r   r)   r0   r   _encode_plusr2   s       r*   r5   zGPT2TokenizerFast._encode_plus   sb    $jj)>F$$,? 	
&t~~'>'>&? @2 2	
?
 w#T4V44r+   save_directoryfilename_prefixc                 f    | j                   j                  j                  ||      }t        |      S )N)name)
_tokenizermodelsavetuple)r%   r6   r7   filess       r*   save_vocabularyz!GPT2TokenizerFast.save_vocabulary   s+    %%**>*PU|r+   )NNN<|endoftext|>r@   r@   F)N)r0   
__module____qualname____doc__VOCAB_FILES_NAMESvocab_files_namesmodel_input_namesr   slow_tokenizer_classr   r   r1   r5   strr   r   r?   __classcell__)r)   s   @r*   r   r   !   s}    0d *$&67( !!!1@;] ;5} 5c HSM ]bcf]g r+   r   )rC   r   typingr   r   
tokenizersr   tokenization_utils_baser   tokenization_utils_fastr   utilsr	   tokenization_gpt2r   
get_loggerr0   loggerrD   r   r   r+   r*   <module>rR      sP    +  " % 4 >  , 
		H	%#/`pq l/ lr+   