o
    ̳i*                     @   s  d dl mZmZ d dlmZmZ d dlmZmZ d dl	m
Z
mZ d dlmZ d dlmZ d dlmZ 	 defd	d
ZdefddZdefddZ				d)dededee dee dee defddZ							d*dee dedededed ed!ed"edefd#d$Z						d+dee dededed ed!ed"edefd%d&Z						d+dee dededed ed!ed"edefd'd(ZdS ),    )ListOptional)_get_prompt_template_TemplateType)
lora_qwen2qwen2)QWEN2_SPECIAL_TOKENSQwen2Tokenizer)TransformerDecoder)LORA_ATTN_MODULES)parse_hf_tokenizer_jsonreturnc                   C   s   t dddddddddd	d

S )z
    Builder for creating a Qwen2 model initialized w/ the default 7B parameter values
    from https://huggingface.co/Qwen/Qwen2-7B-Instruct

    Returns:
        TransformerDecoder: Instantiation of Qwen2 7B model
     R           J             ư>    .A)

vocab_size
num_layers	num_headsnum_kv_heads	embed_dimintermediate_dimmax_seq_lenattn_dropoutnorm_eps	rope_baser    r"   r"   Z/home/ubuntu/.local/lib/python3.10/site-packages/torchtune/models/qwen2/_model_builders.pyqwen2_7b   s   r$   c                   C      t ddddddddd	d
ddS )a  
    Builder for creating a Qwen2 model initialized w/ the default 0.5B parameter values
    from https://huggingface.co/Qwen/Qwen2-0.5B-Instruct

    Returns:
        TransformerDecoder: Instantiation of Qwen2 0.5B model

    Note:
        Qwen2 0.5B and Qwen2 1.5B model builders will enable `tie_word_embeddings` by default
        and returns an instance of `TransformerDecoder`.
    Q               r   r   r   r   Tr   r   r   r   r   r   r   r   r   r    tie_word_embeddingsr!   r"   r"   r"   r#   
qwen2_0_5b-      r.   c                   C   r%   )a  
    Builder for creating a Qwen2 model initialized w/ the default 1.5B parameter values
    from https://huggingface.co/Qwen/Qwen2-1.5B-Instruct

    Returns:
        TransformerDecoder: Instantiation of Qwen2 1.5B model

    Note:
        Qwen2 0.5B and Qwen2 1.5B model builders will enable `tie_word_embeddings` by default
        and returns an instance of `TransformerDecoder`.
    r&   r      r)       #  r   r   r   r   Tr,   r!   r"   r"   r"   r#   
qwen2_1_5bH   r/   r3   Npathmerges_filespecial_tokens_pathr   prompt_templatec                 K   sB   |durt |nt}|durt|nd}td| ||||d|S )a  
    Tokenizer for Qwen2.

    Args:
        path (str): path to the vocab.json file.
        merges_file (str): path to the merges.txt file.
        special_tokens_path (Optional[str]): Path to ``tokenizer.json`` from Hugging Face
            model files that contains all registered special tokens, or a local json file
            structured similarly. Default is None to use the canonical Qwen2 special tokens.
        max_seq_len (Optional[int]): A max sequence length to truncate tokens to.
            Default: None
        prompt_template (Optional[_TemplateType]): optional specified prompt template.
            If a string, it is assumed to be the dotpath of a :class:`~torchtune.data.PromptTemplateInterface`
            class. If a dictionary, it is assumed to be a custom prompt template mapping role to the
            prepend/append tags. Default is None.

    Returns:
        Qwen2Tokenizer: Instantiation of the Qwen2 tokenizer
    N)r4   r5   special_tokensr   r7   r"   )r   r   r   r	   )r4   r5   r6   r   r7   kwargsr8   templater"   r"   r#   qwen2_tokenizerc   s   r;   F      r   lora_attn_modulesapply_lora_to_mlpapply_lora_to_output	lora_rank
lora_alphalora_dropoutuse_doraquantize_basec                 C   sv   t di d| d|d|ddddddd	d
ddddddddddddd|d|d|d|d|S )a  
    Builder for creating a Qwen2 7B model with LoRA enabled.

    The Qwen2 defaults are the same as in :func:`~torchtune.models.qwen2.qwen2_7b`,
    while LoRA default params are based on
    https://github.com/tloen/alpaca-lora/blob/8bb8579e403dc78e37fe81ffbb253c413007323f/finetune.py#L41-L43.

    Args:
        lora_attn_modules (List[LORA_ATTN_MODULES]): list of which linear layers
            LoRA should be applied to in each self-attention block. Options are
            ``{"q_proj", "k_proj", "v_proj", "output_proj"}``.
        apply_lora_to_mlp (bool): whether to apply LoRA to the MLP in each transformer layer.
            Default: False
        apply_lora_to_output (bool): whether to apply LoRA to the model's final output projection.
            Default: False
        lora_rank (int): rank of each low-rank approximation
        lora_alpha (float): scaling factor for the low-rank approximation
        lora_dropout (float): dropout probability for the low-rank approximation. Default: 0.0
        quantize_base (bool): Whether to quantize base model weights

    Returns:
        TransformerDecoder: Instantiation of Qwen2 7B model with LoRA applied
    r>   r?   r@   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r   rA   rB   rC   rD   rE   Nr"   r   )r>   r?   r@   rA   rB   rC   rD   rE   r"   r"   r#   lora_qwen2_7b   sJ   !	
rG   c                 C   |   t d!i d| d|ddddddd	d
ddddddddddddddddd|d|d|d|d|S )"a  
    Builder for creating a Qwen2 0.5B model with LoRA enabled.

    The Qwen2 defaults are the same as in :func:`~torchtune.models.qwen2.qwen2_0_5b`,
    while LoRA default params are based on
    https://github.com/tloen/alpaca-lora/blob/8bb8579e403dc78e37fe81ffbb253c413007323f/finetune.py#L41-L43.

    Args:
        lora_attn_modules (List[LORA_ATTN_MODULES]): list of which linear layers
            LoRA should be applied to in each self-attention block. Options are
            ``{"q_proj", "k_proj", "v_proj", "output_proj"}``.
        apply_lora_to_mlp (bool): whether to apply LoRA to the MLP in each transformer layer.
            Default: False
        lora_rank (int): rank of each low-rank approximation
        lora_alpha (float): scaling factor for the low-rank approximation
        lora_dropout (float): dropout probability for the low-rank approximation. Default: 0.0
        quantize_base (bool): Whether to quantize base model weights

    Returns:
        TransformerDecoder: Instantiation of Qwen2 0.5B model with LoRA applied

    Note:
        Qwen2 0.5B and Qwen2 1.5B model builders will enable `tie_word_embeddings` by default
        and returns an instance of `TransformerDecoder`.
    r>   r?   r@   Fr   r&   r   r'   r   r(   r   r)   r   r*   r   r+   r   r   r   r   r   r   r    r   r-   TrA   rB   rC   rD   rE   Nr"   rF   r>   r?   rA   rB   rC   rD   rE   r"   r"   r#   lora_qwen2_0_5b   N   "	
rJ   c                 C   rH   )"a  
    Builder for creating a Qwen2 1.5B model with LoRA enabled.

    The Qwen2 defaults are the same as in :func:`~torchtune.models.qwen2.qwen2_1_5b`,
    while LoRA default params are based on
    https://github.com/tloen/alpaca-lora/blob/8bb8579e403dc78e37fe81ffbb253c413007323f/finetune.py#L41-L43.

    Args:
        lora_attn_modules (List[LORA_ATTN_MODULES]): list of which linear layers
            LoRA should be applied to in each self-attention block. Options are
            ``{"q_proj", "k_proj", "v_proj", "output_proj"}``.
        apply_lora_to_mlp (bool): whether to apply LoRA to the MLP in each transformer layer.
            Default: False
        lora_rank (int): rank of each low-rank approximation
        lora_alpha (float): scaling factor for the low-rank approximation
        lora_dropout (float): dropout probability for the low-rank approximation. Default: 0.0
        quantize_base (bool): Whether to quantize base model weights

    Returns:
        TransformerDecoder: Instantiation of Qwen2 1.5B model with LoRA applied

    Note:
        Qwen2 0.5B and Qwen2 1.5B model builders will enable `tie_word_embeddings` by default
        and returns an instance of `TransformerDecoder`.
    r>   r?   r@   Fr   r&   r   r   r   r0   r   r)   r   r1   r   r2   r   r   r   r   r   r   r    r   r-   TrA   rB   rC   rD   rE   Nr"   rF   rI   r"   r"   r#   lora_qwen2_1_5b   rK   rL   )NNNN)FFr<   r=   r   FF)Fr<   r=   r   FF)typingr   r    torchtune.data._prompt_templatesr   r   *torchtune.models.qwen2._component_buildersr   r   !torchtune.models.qwen2._tokenizerr   r	   torchtune.modulesr
   torchtune.modules.peftr   'torchtune.modules.transforms.tokenizersr   r$   r.   r3   strintr;   boolfloatrG   rJ   rL   r"   r"   r"   r#   <module>   s   
/	
9
;