U
    di                     @   s^  d Z ddlZddlZddlZddlmZ ddlmZmZm	Z	m
Z
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mZ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%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z. d	dl/m0Z0m1Z1m2Z2 e23e4Z5dZ6dZ7dZ8dZ9dZ:e%d7 Z%eeeedZ;e6e9dZ<e1e%G dd de*Z=dS )z
Tokenization classes for fast tokenizers (provided by HuggingFace's tokenizers library). For slow (python) tokenizers
see tokenization_utils.py
    N)defaultdict)AnyDictListOptionalTupleUnion)Encoding)	Tokenizer)Decoder)
BpeTrainerUnigramTrainerWordLevelTrainerWordPieceTrainer   )convert_slow_tokenizer)convert_gguf_tokenizer)load_gguf_checkpoint)PreTrainedTokenizer)
INIT_TOKENIZER_DOCSTRING
AddedTokenBatchEncodingPreTokenizedInputPreTokenizedInputPairPreTrainedTokenizerBaseSpecialTokensMixin	TextInputTextInputPairTruncationStrategy)PaddingStrategyadd_end_docstringsloggingztokenizer.jsonzspecial_tokens_map.jsonztokenizer_config.jsonztokenizer.modelzadded_tokens.jsonu  
        tokenizer_object ([`tokenizers.Tokenizer`]):
            A [`tokenizers.Tokenizer`] object from 🤗 tokenizers to instantiate from. See [Using tokenizers from 🤗
            tokenizers](../fast_tokenizers) for more information.
        tokenizer_file ([`str`]):
            A path to a local JSON file representing a previously serialized [`tokenizers.Tokenizer`] object from 🤗
            tokenizers.
)BPEUnigram	WordLevel	WordPiece)tokenizer_file
vocab_filec                       s  e Zd ZU dZeZdZeed<  fddZ	e
edddZe
edd	d
Ze
edddZeeef dddZe
eeef dddZe
eeef dddZe
eeef dddZeeef dddZedddZe
edddZe
edddZdKeee ee eeeeeeeee f e!e f d	d d!Z"e#ee!e f e#ee!e f d"d#d$Z$eed%d&d'Z%eee d(d)d*Z&dLe!e#eef  ed+d,d-Z'dMeed.d/d0Z(dNe#ee!e f ee#ee!e f d1d2d3Z)dOeee ee!e d4d5d6Z*e+e,eeee ee d7d8d9Z-de+j.e,j/dd:ddddddddddddfe#e!e0 e!e1 e!e2 e!e3 f ee+e,ee eeee ee ee ee ee eeeeeee4d;d<d=Z5dde+j.e,j/dd:ddddddddddddfe#e0e2f ee#e0e2f  ee+e,ee eeee ee ee ee ee eeeeeee4d>d?d@Z6e!e ed"dAdBZ7dPe#ee!e f eeedCdDdEZ8dQe#ee9j:f ee ee ee ee dFdGdHZ;dRdIdJZ<  Z=S )SPreTrainedTokenizerFastaQ  
    Base class for all fast tokenizers (wrapping HuggingFace tokenizers library).

    Inherits from [`~tokenization_utils_base.PreTrainedTokenizerBase`].

    Handles all the shared methods for tokenization and special tokens, as well as methods for
    downloading/caching/loading pretrained tokenizers, as well as adding tokens to the vocabulary.

    This class also contains the added tokens in a unified way on top of all tokenizers so we don't have to handle the
    specific vocabulary augmentation methods of the various underlying dictionary structures (BPE, sentencepiece...).
    Nslow_tokenizer_classc                    sz  | dd }| dd }| dd }| dd }| dd}| di }|rf|d krf| jd krftd|d k	rzt|}	n|d k	r|st|}	n|rt|}	n|d k	rt|	d	}
|
d
 d }|
d }|
d }t
||\}	}|| t|dkrn|| nh| jd k	r2|dk	r2| j||}t|}	n<|sf|	d	d | _|	dg | _t| dd}	d }ntd|	| _|d k	r||j d| _| jj}|d k	r| jjf | |d|d  |d|d  |d|d  |d|d  n
| j  | jj}|d k	rl| jjf | |d|d  |d|d  |d|d  |d|d  |d|d  t jf | | j| j_dd  | jD   fd!d"t| d#d$ d%D t| j ! d&d" D  fd'd"| j"D 7 tdkrvg }| j#}D ]\}t$|t%r,|j&p6t'||kn
t'||k}t$|t'rRt%||d(}n||_&|(| q|rv| )| d S ))Ntokenizer_objectZ__slow_tokenizer	gguf_filer&   	from_slowFadded_tokens_decoderzCannot instantiate this tokenizer from a slow version. If it's based on sentencepiece, make sure you have sentencepiece installed.r'   configZ
model_type	tokenizertokenizer_configr   additional_special_tokensT)Zfrom_tiktokena9  Couldn't instantiate the backend tokenizer from one of: 
(1) a `tokenizers` library serialization file, 
(2) a slow tokenizer instance to convert or 
(3) an equivalent slow tokenizer class to instantiate and convert. 
You need to have sentencepiece or tiktoken installed to convert a slow tokenizer to a fast one.
max_lengthtruncation_side	directionstridetruncation_strategystrategy	pad_tokenpad_token_type_idpad_type_idpadding_sidelengthpad_to_multiple_ofc                 S   s   h | ]}t t|qS  hashrepr.0tokenr>   r>   H/tmp/pip-unpacked-wheel-bm_b0l5e/transformers/tokenization_utils_fast.py	<setcomp>   s     z3PreTrainedTokenizerFast.__init__.<locals>.<setcomp>c                    s$   g | ]\}}t t| kr|qS r>   r?   )rC   indexrD   )added_tokens_decoder_hashr>   rE   
<listcomp>   s   z4PreTrainedTokenizerFast.__init__.<locals>.<listcomp>c                 S   s   | d S Nr   r>   )xr>   r>   rE   <lambda>       z2PreTrainedTokenizerFast.__init__.<locals>.<lambda>keyc                 S   s   g | ]}t |qS r>   )strrB   r>   r>   rE   rI      s     c                    s    g | ]}| kr|kr|qS r>   r>   rB   )encodertokens_to_addr>   rE   rI      s      )special)*popr)   
ValueErrorcopydeepcopyTokenizerFast	from_filer   r   getr   updatelenr'   r1   
_tokenizerinit_kwargs_decode_use_source_tokenizer
truncationenable_truncation
setdefaultno_truncationpaddingenable_paddingsuper__init__split_special_tokensencode_special_tokensr-   sorteditemslistadded_tokens_encoderkeysZall_special_tokens_extendedZall_special_tokens
isinstancer   rS   rP   append
add_tokens)selfargskwargsr*   Zslow_tokenizerr+   Zfast_tokenizer_filer,   r-   Zfast_tokenizerZ
gguf_paramarchitectureZtokenizer_dictr0   Zadditional_kwargs_truncation_paddingtokensspecial_tokensrD   Z
is_special	__class__)rH   rQ   rR   rE   rg   b   s    










z PreTrainedTokenizerFast.__init__)returnc                 C   s   dS )NTr>   rr   r>   r>   rE   is_fast   s    zPreTrainedTokenizerFast.is_fastc                 C   s   dS )z
        `bool`: Whether or not the slow tokenizer can be saved. Usually for sentencepiece based slow tokenizer, this
        can only be `True` if the original `"sentencepiece.model"` was not deleted.
        Tr>   r}   r>   r>   rE   can_save_slow_tokenizer   s    z/PreTrainedTokenizerFast.can_save_slow_tokenizerc                 C   s   | j jddS )zP
        `int`: Size of the base vocabulary (without the added tokens).
        FZwith_added_tokensr]   Zget_vocab_sizer}   r>   r>   rE   
vocab_size   s    z"PreTrainedTokenizerFast.vocab_sizec                 C   s   | j jddS )NTr   )r]   	get_vocabr}   r>   r>   rE   r      s    z!PreTrainedTokenizerFast.get_vocabc                 C   s   |   S N)r   r}   r>   r>   rE   vocab   s    zPreTrainedTokenizerFast.vocabc                 C   s    dd t | j dd dD S )z
        Returns the sorted mapping from string to index. The added tokens encoder is cached for performance
        optimisation in `self._added_tokens_encoder` for the slow tokenizers.
        c                 S   s   i | ]\}}|j |qS r>   contentrC   vkr>   r>   rE   
<dictcomp>   s      z@PreTrainedTokenizerFast.added_tokens_encoder.<locals>.<dictcomp>c                 S   s   | d S rJ   r>   itemr>   r>   rE   rL      rM   z>PreTrainedTokenizerFast.added_tokens_encoder.<locals>.<lambda>rN   rj   r-   rk   r}   r>   r>   rE   rm      s    z,PreTrainedTokenizerFast.added_tokens_encoderc                 C   s
   | j  S )z
        Returns the added tokens in the vocabulary as a dictionary of index to AddedToken.

        Returns:
            `Dict[str, int]`: The added tokens.
        )r]   Zget_added_tokens_decoderr}   r>   r>   rE   r-      s    z,PreTrainedTokenizerFast.added_tokens_decoderc                 C   s    dd t | j dd dD S )z
        Returns the added tokens in the vocabulary as a dictionary of token to index.

        Returns:
            `Dict[str, int]`: The added tokens.
        c                 S   s   i | ]\}}|j |qS r>   r   r   r>   r>   rE   r     s      z;PreTrainedTokenizerFast.get_added_vocab.<locals>.<dictcomp>c                 S   s   | d S rJ   r>   r   r>   r>   rE   rL     rM   z9PreTrainedTokenizerFast.get_added_vocab.<locals>.<lambda>rN   r   r}   r>   r>   rE   get_added_vocab   s    z'PreTrainedTokenizerFast.get_added_vocabc                 C   s   | j jddS )zD
        Size of the full vocabulary with the added tokens.
        Tr   r   r}   r>   r>   rE   __len__  s    zPreTrainedTokenizerFast.__len__c                 C   s   | j S )zc
        `tokenizers.implementations.BaseTokenizer`: The Rust tokenizer used as a backend.
        )r]   r}   r>   r>   rE   backend_tokenizer  s    z)PreTrainedTokenizerFast.backend_tokenizerc                 C   s   | j jS )zU
        `tokenizers.decoders.Decoder`: The Rust decoder for this tokenizer.
        )r]   decoderr}   r>   r>   rE   r     s    zPreTrainedTokenizerFast.decoderFT)	encodingreturn_token_type_idsreturn_attention_maskreturn_overflowing_tokensreturn_special_tokens_maskreturn_offsets_mappingreturn_lengthverboser|   c	                 C   s   |dkrd| j k}|dkr$d| j k}|r@|jdk	r@|g|j }	n|g}	tt}
|	D ]|}|
d |j |rz|
d |j |r|
d |j |r|
d |j |r|
d |j	 |rR|
d t
|j qR|
|	fS )a  
        Convert the encoding representation (from low-level HuggingFace tokenizer output) to a python Dict and a list
        of encodings, take care of building a batch from overflowing tokens.

        Overflowing tokens are converted to additional examples (like batches) so the output values of the dict are
        lists (overflows) of lists (tokens).

        Output shape: (overflows, sequence length)
        NZtoken_type_idsattention_mask	input_idsspecial_tokens_maskZoffset_mappingr<   )Zmodel_input_namesZoverflowingr   rl   rp   idsZtype_idsr   r   offsetsr\   )rr   r   r   r   r   r   r   r   r   	encodingsZencoding_dicter>   r>   rE   _convert_encoding  s*    

z)PreTrainedTokenizerFast._convert_encoding)rx   r|   c                    s2   |dkrdS t |tr  |S  fdd|D S )aT  
        Converts a token string (or a sequence of tokens) in a single integer id (or a sequence of ids), using the
        vocabulary.

        Args:
            tokens (`str` or `List[str]`): One or several token(s) to convert to token id(s).

        Returns:
            `int` or `List[int]`: The token id or list of token ids.
        Nc                    s   g | ]}  |qS r>   )#_convert_token_to_id_with_added_vocrB   r}   r>   rE   rI   Z  s     zAPreTrainedTokenizerFast.convert_tokens_to_ids.<locals>.<listcomp>)ro   rP   r   rr   rx   r>   r}   rE   convert_tokens_to_idsI  s
    

z-PreTrainedTokenizerFast.convert_tokens_to_ids)rD   r|   c                 C   s   | j |}|d kr| jS |S r   )r]   token_to_idZunk_token_id)rr   rD   rG   r>   r>   rE   r   \  s    z;PreTrainedTokenizerFast._convert_token_to_id_with_added_voc)rG   r|   c                 C   s   | j t|S r   )r]   id_to_tokenint)rr   rG   r>   r>   rE   _convert_id_to_tokenb  s    z,PreTrainedTokenizerFast._convert_id_to_token)
new_tokensr|   c                 C   s   |r| j |S | j |S r   )r]   add_special_tokensrq   )rr   r   ry   r>   r>   rE   _add_tokense  s    z#PreTrainedTokenizerFast._add_tokens)pairr|   c                 C   s   | j |S )aG  
        Returns the number of added tokens when encoding a sequence with special tokens.

        <Tip>

        This encodes a dummy input and checks the number of added tokens, and is therefore not efficient. Do not put
        this inside your training loop.

        </Tip>

        Args:
            pair (`bool`, *optional*, defaults to `False`):
                Whether the number of added tokens should be computed in the case of a sequence pair or a single
                sequence.

        Returns:
            `int`: Number of special tokens added to sequences.
        )r]   num_special_tokens_to_add)rr   r   r>   r>   rE   r   k  s    z1PreTrainedTokenizerFast.num_special_tokens_to_add)r   skip_special_tokensr|   c                 C   sR   t |tr| j|S g }|D ].}t|}|r:|| jkr:q|| j| q|S )a  
        Converts a single index or a sequence of indices in a token or a sequence of tokens, using the vocabulary and
        added tokens.

        Args:
            ids (`int` or `List[int]`):
                The token id (or token ids) to convert to tokens.
            skip_special_tokens (`bool`, *optional*, defaults to `False`):
                Whether or not to remove special tokens in the decoding.

        Returns:
            `str` or `List[str]`: The decoded token(s).
        )ro   r   r]   r   Zall_special_idsrp   )rr   r   r   rx   rG   r>   r>   rE   convert_ids_to_tokens  s    
z-PreTrainedTokenizerFast.convert_ids_to_tokens)textr   r   r|   c                 K   s   | j f |||d| S )N)r   	text_pairr   )Zencode_plusrx   )rr   r   r   r   rt   r>   r>   rE   tokenize  s    z PreTrainedTokenizerFast.tokenizepadding_strategyr6   r2   r5   r=   r;   c                    s   | j j | j j}|tjkr. dk	rv| j   nH|||j| jd} dkrNd}	n fdd|D }	|	|krv| j jf | |t	j
kr|dk	r| j   nN|t	jkr|nd}
|
|dk	r|n| j| j| j| j|d}||kr| j jf | dS )a  
        Define the truncation and the padding strategies for fast tokenizers (provided by HuggingFace tokenizers
        library) and restore the tokenizer settings afterwards.

        The provided tokenizer has no padding / truncation strategy before the managed section. If your tokenizer set a
        padding / truncation strategy before, then it will be reset to no padding / truncation when exiting the managed
        section.

        Args:
            padding_strategy ([`~utils.PaddingStrategy`]):
                The kind of padding that will be applied to the input
            truncation_strategy ([`~tokenization_utils_base.TruncationStrategy`]):
                The kind of truncation that will be applied to the input
            max_length (`int`):
                The maximum size of a sequence.
            stride (`int`):
                The stride to use when handling overflow.
            pad_to_multiple_of (`int`, *optional*):
                If set will pad the sequence to a multiple of the provided value. This is especially useful to enable
                the use of Tensor Cores on NVIDIA hardware with compute capability `>= 7.5` (Volta).
            padding_side (`str`, *optional*):
                The side on which the model should have padding applied. Should be selected between ['right', 'left'].
                Default value is picked from the class attribute of the same name.
        N)r2   r5   r7   r4   c                    s   i | ]}|  |d qS r   rZ   )rC   r   rv   r>   rE   r     s      zFPreTrainedTokenizerFast.set_truncation_and_padding.<locals>.<dictcomp>)r<   r4   Zpad_idr8   r:   r=   )r]   r`   rd   r   DO_NOT_TRUNCATErc   valuer3   ra   r   
DO_NOT_PADZ
no_paddingZ
MAX_LENGTHr;   Zpad_token_idr8   r9   re   )rr   r   r6   r2   r5   r=   r;   rw   targetcurrentr<   r>   r   rE   set_truncation_and_padding  s8    !

z2PreTrainedTokenizerFast.set_truncation_and_paddingr   )batch_text_or_text_pairsr   r   r6   r2   r5   is_split_into_wordsr=   r;   return_tensorsr   r   r   r   r   r   r   rh   r|   c                    s(  t |ttfs"tdt| dj||||||	d jj|krL|j_jj|||d}fdd|D }i }|d d 	 D ]  fdd|D }|| < qd	d |D }rg }t
|D ]"\}\}}||gt|d
  7 }q||d< |d
 D ]}|| qt|||
dS )Nz:batch_text_or_text_pairs has to be a list or a tuple (got )r   )r   Zis_pretokenizedc                    s&   g | ]}j | d qS ))r   r   r   r   r   r   r   r   )r   )rC   r   )r   r   r   r   r   r   rr   r   r>   rE   rI     s   z>PreTrainedTokenizerFast._batch_encode_plus.<locals>.<listcomp>r   c                    s"   g | ]\}}|  D ]}|qqS r>   r>   )rC   r   _r   rN   r>   rE   rI   3  s     
  c                 S   s   g | ]\}}|D ]}|qqS r>   r>   )rC   r   r   r   r>   r>   rE   rI   5  s       r   overflow_to_sample_mapping)Ztensor_type)ro   tuplerl   	TypeErrortyper   r]   ri   Zencode_batchrn   	enumerater\   &_eventual_warn_about_too_long_sequencer   )rr   r   r   r   r6   r2   r5   r   r=   r;   r   r   r   r   r   r   r   r   rh   r   Ztokens_and_encodingsZsanitized_tokensstackZsanitized_encodingsr   itoksr   r   r>   )	rO   r   r   r   r   r   r   rr   r   rE   _batch_encode_plus  sF    	
z*PreTrainedTokenizerFast._batch_encode_plus)r   r   r   r   r6   r2   r5   r   r=   r;   r   r   r   r   r   r   r   r   rh   r|   c                 K   s   |r||fgn|g}| j |f|||||||	|
|||||||||d|}|d krp|sptdd | D |j}| |d || |S )N)r   r   r   r6   r2   r5   r=   r;   r   r   r   r   r   r   r   r   rh   c                 S   s8   i | ]0\}}|t |d kr0t|d  tr0|d  n|qS )r   )r\   ro   rl   )rC   rO   r   r>   r>   rE   r   u  s    z8PreTrainedTokenizerFast._encode_plus.<locals>.<dictcomp>r   )r   r   rk   r   r   )rr   r   r   r   r   r6   r2   r5   r   r=   r;   r   r   r   r   r   r   r   r   rh   rt   Zbatched_inputZbatched_outputr>   r>   rE   _encode_plusC  sB    z$PreTrainedTokenizerFast._encode_plusc                 C   s   | j j|S r   )r   r   decoder   r>   r>   rE   convert_tokens_to_string  s    z0PreTrainedTokenizerFast.convert_tokens_to_string)	token_idsr   clean_up_tokenization_spacesr|   c                 K   sZ   | dd| _t|tr|g}| jj||d}|d k	r:|n| j}|rR| |}|S |S d S )NZuse_source_tokenizerF)r   )rT   r_   ro   r   r]   r   r   Zclean_up_tokenization)rr   r   r   r   rt   r   Z
clean_textr>   r>   rE   _decode  s    

zPreTrainedTokenizerFast._decode)save_directory
file_nameslegacy_formatfilename_prefixr|   c              	      s&  t |} jdkr"|dkr"td|dks2|dko@ jdk	o@ j}|dkpP|dk}|rtj||rj|d ndt } fdd j	 D }|rt
|d	d
d&}	tj|ddddd }
|	|
 W 5 Q R X  j||d}|| |f }|r"tj||r|d ndt } j| ||f }|S )z
        Save a tokenizer using the slow-tokenizer/legacy format: vocabulary + added tokens as well as in a unique JSON
        file containing {config + vocab + added-tokens}.
        NTzYour tokenizer does not have a legacy version defined and therefore cannot register this version. You might consider leaving the legacy_format at `None` or setting it to `False`.F- c                    s    i | ]\}}| j kr||qS r>   )r   )rC   tokrG   r}   r>   rE   r     s     
  z<PreTrainedTokenizerFast._save_pretrained.<locals>.<dictcomp>wzutf-8)r      )indent	sort_keysensure_ascii
)r   )rP   r)   rU   r   ospathjoinADDED_TOKENS_FILErm   rk   openjsondumpswriteZsave_vocabularyTOKENIZER_FILEr   save)rr   r   r   r   r   Z	save_slowZ	save_fastZadded_tokens_fileZadded_vocabfZout_strZvocab_filesr&   r>   r}   rE   _save_pretrained  s>      
z(PreTrainedTokenizerFast._save_pretrainedc              	      sB  t | j }|d}|d}	d}
|d d dkrRi |d d< g |d d< n|d d d	kr|d d
 dk	r|d d
 }|d d | d }
 dk	r|
 kr |
 }
d|d d
< |
dgg|d d< n6|d d dkri |d d< ntd|d d  d dk	rBd|d krB|d d  krB |d d  |d d< tt |g }|D ]r}|dd}|dd}|d d d	kr|sqZ dk	r|d  kr |d  |d< |	t
f | qZ|dk	r|| |d d dkr d|kr |d d dk	r |d d |d< |d d dkr^d|kr^|d d dk	r^|d d |d< |d d d	kr|
dk	r|
|d< |d dk	r|d d dks|d d dkrd|d krtdd |d d D rtj |d< t|d d  }|f ||d|}j|||d |	dk	rRt  }d|	kr|	d D ]}|	d | d  } dk	rz fd!d"|D }||	d | d < |D ]"}|}|dkrtd#qfd$d"|D |	d | d%< qJd&D ]`}||	kr|	| \}} dk	r| kr | }|}|dkr*td#||g|	|< q|	|d< tt || j }tj }|d' |D ]}t| d(| dk	rtt| |} dk	r| kr | }t| d(| }t|t
rt
||j|j|j|jd)d*||< n|||< qt| j}|dk	r|| t|dkr.||d'< | j f d+i|S ),uf  
        Trains a tokenizer on a new corpus with the same defaults (in terms of special tokens or tokenization pipeline)
        as the current one.

        Args:
            text_iterator (generator of `List[str]`):
                The training corpus. Should be a generator of batches of texts, for instance a list of lists of texts
                if you have everything in memory.
            vocab_size (`int`):
                The size of the vocabulary you want for your tokenizer.
            length (`int`, *optional*):
                The total number of sequences in the iterator. This is used to provide meaningful progress tracking
            new_special_tokens (list of `str` or `AddedToken`, *optional*):
                A list of new special tokens to add to the tokenizer you are training.
            special_tokens_map (`Dict[str, str]`, *optional*):
                If you want to rename some of the special tokens this tokenizer uses, pass along a mapping old special
                token name to new special token name in this argument.
            kwargs (`Dict[str, Any]`, *optional*):
                Additional keyword arguments passed along to the trainer from the 🤗 Tokenizers library.

        Returns:
            [`PreTrainedTokenizerFast`]: A new tokenizer of the same type as the original one, trained on
            `text_iterator`.

        added_tokenspost_processorNmodelr   r"   r   Zmergesr#   unk_idr   g        )r$   r%   z;This method does not support this type of tokenizer (found z-) only BPE, Unigram, WordLevel and WordPiece.	unk_tokenrS   idr   Zcontinuing_subword_prefixZend_of_word_suffixZpre_tokenizer	ByteLevelSequenceZpretokenizersc                 s   s   | ]}|d  dkV  qdS )r   r   Nr>   )rC   Zpretokenizerr>   r>   rE   	<genexpr>4  s   zBPreTrainedTokenizerFast.train_new_from_iterator.<locals>.<genexpr>Zinitial_alphabet)r   ry   )r<   trainerry   rx   c                    s   g | ]}  ||qS r>   r   rB   )special_tokens_mapr>   rE   rI   F  s     zCPreTrainedTokenizerFast.train_new_from_iterator.<locals>.<listcomp>zQAttempted to set a token in the post processor that does not exist in the mappingc                    s   g | ]}  |qS r>   )r   rB   )r/   r>   rE   rI   O  s     r   )clssepr1   r   T)single_wordlstriprstrip
normalizedrS   r*   )!r   loadsr]   Zto_strrT   rU   rX   Zfrom_strr   rp   r   extendanypre_tokenizers_fastr   alphabetMODEL_TO_TRAINER_MAPPINGZtrain_from_iteratorr   r^   rV   r   ZSPECIAL_TOKENS_ATTRIBUTESremovegetattrro   r   r   r   r   r1   r\   r{   )rr   Ztext_iteratorr   r<   Znew_special_tokensr   rt   Ztokenizer_jsonr   r   r   r   ry   Zadded_tokenrS   r   Ztrainer_classr   Ztrained_tokenizer_jsonrO   rx   rD   Ztoken_idZspecial_tokenZspecial_tokens_listZspecial_token_fullr1   r>   )r   r/   rE   train_new_from_iterator  s    "













"






	

z/PreTrainedTokenizerFast.train_new_from_iterator)NNFFFFT)F)F)F)NF)FN)NN)NNN)>__name__
__module____qualname____doc__VOCAB_FILES_NAMESZvocab_files_namesr)   r   __annotations__rg   propertyboolr~   r   r   r   r   rP   r   r   rm   r   r-   r   r   rX   r   DecoderFastr   EncodingFastr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   PathLiker   r  __classcell__r>   r>   rz   rE   r(   Q   sD  
o			       /(  P^
=    5   r(   )>r	  rV   r   r   collectionsr   typingr   r   r   r   r   r   Ztokenizers.pre_tokenizersZpre_tokenizersr   Z
tokenizersr	   r  r
   rX   Ztokenizers.decodersr   r  Ztokenizers.trainersr   r   r   r   r   Zintegrations.ggmlr   Zmodeling_gguf_pytorch_utilsr   Ztokenization_utilsr   Ztokenization_utils_baser   r   r   r   r   r   r   r   r   r   utilsr   r    r!   Z
get_loggerr  loggerr   ZSPECIAL_TOKENS_MAP_FILEZTOKENIZER_CONFIG_FILEZTIKTOKEN_VOCAB_FILEr   r  r
  r(   r>   r>   r>   rE   <module>   s>    0


