o
    ̳i=                     @   sj   d dl mZmZmZmZmZ d dlmZmZ d dl	m
Z
 d dlmZmZmZ g dZG dd dee
ZdS )	    )AnyListMappingOptionalTuple)MessagePromptTemplate)	Transform)ModelTokenizerSentencePieceBaseTokenizer#tokenize_messages_no_special_tokens) 
	c                   @   s   e Zd ZdZ		d$dedee dee fddZe	dd	 Z
e	d
d Ze	dd Ze	dd Z			d%dededededee f
ddZdee defddZdddee dedeee ee f fddZ	d&d eeef d!edeeef fd"d#ZdS )'GemmaTokenizera  
    Gemma's implementation of the SentencePiece tokenizer

    Args:
        path (str): Path to pretrained tokenizer file.
        max_seq_len (Optional[int]): A max sequence length to truncate tokens to.
            Default: None
        prompt_template (Optional[PromptTemplate]): template used to format the messages based on their role. This is used
            to add structured text around the actual messages. The structured text is used in three scenarios:

            - Task-specific templates to gear models for a particular task that it will expect after training
            - Model-specific templates that are required whenever the model is prompted, such as the [INST]
              tags in Llama2 and in Mistral
            - Community standardized templates, such as :class:`~torchtune.data.ChatMLTemplate`

            The extra text will still get tokenized as normal text, not as special tokens. Default is None.

    Examples:
        >>> tokenizer = GemmaTokenizer("/path/to/spm_model")
        >>> tokenized_text = tokenizer.encode("Hello world!", add_bos=True, add_eos=True)
        >>> print(tokenized_text)
        [1, 31587, 29644, 102, 2]
    Npathmax_seq_lenprompt_templatec                 C   s,   t || _d| j_| jg| _|| _|| _d S )Nr   )r   
_spm_modelpad_ideos_idstop_tokensr   r   )selfr   r   r    r   U/home/ubuntu/.local/lib/python3.10/site-packages/torchtune/models/gemma/_tokenizer.py__init__-   s
   


zGemmaTokenizer.__init__c                 C      | j jS N)r   r   r   r   r   r   r   ?      zGemmaTokenizer.eos_idc                 C   r   r   )r   bos_idr    r   r   r   r"   C   r!   zGemmaTokenizer.bos_idc                 C   r   r   )r   r   r    r   r   r   r   G   r!   zGemmaTokenizer.pad_idc                 C   r   r   )r   
vocab_sizer    r   r   r   r#   K   r!   zGemmaTokenizer.vocab_sizeTFtextadd_bosadd_eostrim_leading_whitespacereturnc                 C   s   | j j||||dS )N)r%   r&   r'   )r   encode)r   r$   r%   r&   r'   r   r   r   r)   O   s   zGemmaTokenizer.encode	token_idsc                 C   s   | j |S r   )r   decode)r   r*   r   r   r   r+   ]   s   zGemmaTokenizer.decode)r&   messagesc                C   s8   | j dur
|  |n|}t| || j|r| jdS ddS )a  Tokenize a list of messages one at a time then concatenate them,
        returning a list of tokens and a list of masks.


        Example:
            >>> tokenizer = GemmaTokenizer(tokenizer_path, max_seq_len)
            >>> messages = [
                Message(role="system", content="system message\n", masked=True),
                Message(role="user", content="user prompt\n", masked=True),
                Message(role="assistant", content="assistant response\n"),
            ]

            >>> # tokenize_messages encodes messages separately and concats
            >>> tokenizer.tokenize_messages(messages)[0]
            [1, 1788, 2643, 13, 1792, 9508, 13, 465, 22137, 2933, 2]


            >>> # Same result as encoding the full string in one go
            >>> tokenizer.encode(''.join([message.content for message in messages]))
            [1, 1788, 2643, 13, 1792, 9508, 13, 465, 22137, 2933, 2]


        Args:
            messages (List[Message]): A list of messages, each containing role, content,
                and masked attributes.
            add_eos (bool): Whether to append EOS after assistant message, default to True

        Returns:
            Tuple[List[int], List[bool]]: The tokenized messages
        N)	tokenizerr,   r"   r   )r   r   r"   r   )r   r,   r&   templated_messagesr   r   r   tokenize_messagesc   s   
&
z GemmaTokenizer.tokenize_messagessample	inferencec                 C   s,   | d}| |\}}||d< ||d< |S )a  
        Apply ``tokenize_messages`` to the "messages" field in the sample.

        Args:
            sample (Mapping[str, Any]): A sample with a "messages" field containing
                a List[Message] to tokenize
            inference (bool): Whether the template is being used for inference or not.

        Returns:
            Mapping[str, Any]: The sample with added "tokens" and "mask" fields
                and the "messages" field removed.
        r,   tokensmask)popr/   )r   r0   r1   r,   r2   r3   r   r   r   __call__   s
   
zGemmaTokenizer.__call__)NN)TTF)F)__name__
__module____qualname____doc__strr   intr   r   propertyr   r"   r   r#   boolr   r)   r+   r   r   r/   r   r   r5   r   r   r   r   r      sl    








1

r   N)typingr   r   r   r   r   torchtune.datar   r   torchtune.modules.transformsr	   'torchtune.modules.transforms.tokenizersr
   r   r   WHITESPACE_CHARSr   r   r   r   r   <module>   s   