o
    ib                     @   sP   d Z ddlmZ ddlmZmZ ddlmZ ee	Z
G dd deZdgZdS )	z!Tokenization class for Perceiver.    )Optional   )
AddedTokenPreTrainedTokenizer)loggingc                
       s  e Zd ZdZddgZ								
d(	d) fddZdeeef fddZ	e
dd Z	d*dee deee  dedee f fddZ	d+dee deee  dee fddZdedee fddZdd Zd d! Zd"d# Zd+d$ed%ee dee fd&d'Z  ZS ),PerceiverTokenizeraS  
    Construct a Perceiver tokenizer. The Perceiver simply uses raw bytes utf-8 encoding.

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

    Args:
        pad_token (`str`, *optional*, defaults to `"[PAD]"`):
            The token used for padding, for example when batching sequences of different lengths.
        bos_token (`str`, *optional*, defaults to `"[BOS]"`):
            The BOS token (reserved in the vocab, but not actually used).
        eos_token (`str`, *optional*, defaults to `"[EOS]"`):
            The end of sequence token (reserved in the vocab, but not actually used).

            <Tip>

            When building a sequence using special tokens, this is not the token that is used for the end of sequence.
            The token used is the `sep_token`.

            </Tip>

        mask_token (`str`, *optional*, defaults to `"[MASK]"`):
            The MASK token, useful for masked language modeling.
        cls_token (`str`, *optional*, defaults to `"[CLS]"`):
            The CLS token (reserved in the vocab, but not actually used).
        sep_token (`str`, *optional*, defaults to `"[SEP]"`):
            The separator token, which is used when building a sequence from two sequences.

    	input_idsattention_mask[PAD][BOS][EOS][MASK][CLS][SEP]   returnNc           	   
      s   t |trt|dddn|}t |trt|dddn|}t |tr(t|dddn|}t |tr6t|dddn|}t |trDt|dddn|}t |trRt|dddn|}d| _||||||d| _t| j| _t jd|||||||d| d S )NF)lstriprstrip   )r         r         )	pad_token	bos_token	eos_token
mask_token	cls_token	sep_tokenmodel_max_length )	
isinstancestrr   _utf_vocab_size_added_tokens_decoderlen_num_special_tokenssuper__init__)	selfr   r   r   r   r   r   r   kwargs	__class__r    q/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/transformers/models/perceiver/tokenization_perceiver.pyr(   ;   s4   
zPerceiverTokenizer.__init__c                 C   s:   i }t | jD ]}t|}|| j ||< q|| j |S N)ranger#   chrr&   updateadded_tokens_encoder)r)   vocabitokenr    r    r-   	get_vocabd   s   zPerceiverTokenizer.get_vocabc                 C   s   | j S r.   )r#   )r)   r    r    r-   
vocab_sizel   s   zPerceiverTokenizer.vocab_sizeFtoken_ids_0token_ids_1already_has_special_tokensc                    sf   |rt  j||ddS |du rdgdgt|  dg S dgdgt|  dg dgt|  dg S )a  
        Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding
        special tokens using the tokenizer `prepare_for_model` method.

        Args:
            token_ids_0 (`list[int]`):
                List of IDs.
            token_ids_1 (`list[int]`, *optional*):
                Optional second list of IDs for sequence pairs.
            already_has_special_tokens (`bool`, *optional*, defaults to `False`):
                Whether or not the token list is already formatted with special tokens for the model.

        Returns:
            `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.
        T)r8   r9   r:   Nr   r   )r'   get_special_tokens_maskr%   )r)   r8   r9   r:   r+   r    r-   r;   p   s   .z*PerceiverTokenizer.get_special_tokens_maskc                 C   s<   |du r| j g| | jg S | j g| | jg | | jg S )af  
        Build model inputs from a sequence or a pair of sequence for sequence classification tasks. A 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.
        N)cls_token_idsep_token_id)r)   r8   r9   r    r    r-    build_inputs_with_special_tokens   s    z3PerceiverTokenizer.build_inputs_with_special_tokenstextc                 C   s   dd | dD }|S )zPTake as input a string and return a list of strings (tokens) for words/sub-wordsc                 S   s   g | ]}t |qS r    )r0   ).0r4   r    r    r-   
<listcomp>   s    z0PerceiverTokenizer._tokenize.<locals>.<listcomp>utf-8)encode)r)   r?   tokensr    r    r-   	_tokenize   s   zPerceiverTokenizer._tokenizec                 C   s(   t |dkr| j}|S t|| j }|S )z0Converts a token (str) in an id using the vocab.r   )r%   unk_token_idordr&   )r)   r5   token_idr    r    r-   _convert_token_to_id   s
   z'PerceiverTokenizer._convert_token_to_idc                 C   s   t || j }|S )z=Converts an index (integer) in a token (str) using the vocab.)r0   r&   )r)   indexr5   r    r    r-   _convert_id_to_token   s   z'PerceiverTokenizer._convert_id_to_tokenc                 C   sP   d}|D ]}|| j v rt|d}ntt|g}||7 }q|jddd}|S )z:Converts a sequence of tokens (string) in a single string.    rB   replace)errors)r2   r"   rC   bytesrG   decode)r)   rD   bstringr5   
tok_stringstringr    r    r-   convert_tokens_to_string   s   

z+PerceiverTokenizer.convert_tokens_to_stringsave_directoryfilename_prefixc                 C   s   dS )Nr    r    )r)   rU   rV   r    r    r-   save_vocabulary   s   z"PerceiverTokenizer.save_vocabulary)r
   r   r   r   r   r   r   )r   N)NFr.   )__name__
__module____qualname____doc__model_input_namesr(   dictr"   intr6   propertyr7   listr   boolr;   r>   rE   rI   rK   rT   tuplerW   __classcell__r    r    r+   r-   r      sN    
)



(r   N)r[   typingr   tokenization_utilsr   r   utilsr   
get_loggerrX   loggerr   __all__r    r    r    r-   <module>   s   
 
/