Source code for torchtune.models.phi3._component_builders
# Copyright (c) Meta Platforms, Inc. and affiliates.# All rights reserved.## This source code is licensed under the BSD-style license found in the# LICENSE file in the root directory of this source tree.fromfunctoolsimportpartialfromtypingimportListfromtorchimportnnfromtorchtune.models.phi3._position_embeddingsimportPhi3RotaryPositionalEmbeddingsfromtorchtune.modulesimport(CausalSelfAttention,FeedForward,RMSNorm,RotaryPositionalEmbeddings,TransformerDecoder,TransformerDecoderLayer,)fromtorchtune.modules.common_utilsimportreparametrize_as_dtype_state_dict_post_hookfromtorchtune.modules.peftimportLORA_ATTN_MODULES,LoRALinear"""Component builders for the Phi3 4K Mini Instruct model.torchtune provides composable building blocks. Builder functions helpstitch these building blocks into higher-level components. This design hastwo benefits:- The building blocks themselves are very flexible. For example, ``CausalSelfAttention``can take either nn.Linear or nn.LoRALinear for ``q_proj``.- Builder functions expose a set of configurable params which keep the constructors ofthe building blocks simple."""
[docs]defphi3(vocab_size:int,num_layers:int,num_heads:int,num_kv_heads:int,embed_dim:int,intermediate_dim:int,max_seq_len:int,attn_dropout:float=0.0,norm_eps:float=1e-5,rope_base:int=10_000,)->TransformerDecoder:""" Args: vocab_size (int): number of tokens in vocabulary. num_layers (int): number of layers in the transformer decoder. num_heads (int): number of query heads. For MHA this is also the number of heads for key and value num_kv_heads (int): number of key and value heads. If specified, user should ensure `num_heads` % `num_kv_heads` == 0. Default value is `None`, in which case this is the same as MHA embed_dim (int): embedding dimension for self-attention intermediate_dim (int): intermediate dimension for MLP max_seq_len (int): maximum sequence length the model will be run with, attn_dropout (float): dropout value passed onto scaled_dot_product_attention. Default: 0.0 norm_eps (float): epsilon in RMS norms rope_base (int): base for the rotary positional embeddings. Default: 10_000 Returns: TransformerDecoder: Instantiation of Phi3 Mini 4K Instruct model. """head_dim=embed_dim//num_headsnum_kv_heads=num_kv_headsifnum_kv_headselsenum_headsrope=Phi3RotaryPositionalEmbeddings(dim=head_dim,max_seq_len=max_seq_len,base=rope_base)self_attn=CausalSelfAttention(embed_dim=embed_dim,num_heads=num_heads,num_kv_heads=num_kv_heads,head_dim=head_dim,q_proj=nn.Linear(embed_dim,num_heads*head_dim,bias=False),k_proj=nn.Linear(embed_dim,num_kv_heads*head_dim,bias=False),v_proj=nn.Linear(embed_dim,num_kv_heads*head_dim,bias=False),output_proj=nn.Linear(embed_dim,embed_dim,bias=False),pos_embeddings=rope,kv_cache=None,max_seq_len=max_seq_len,attn_dropout=attn_dropout,)mlp=phi3_mlp(dim=embed_dim,hidden_dim=intermediate_dim)layer=TransformerDecoderLayer(attn=self_attn,mlp=mlp,sa_norm=RMSNorm(dim=embed_dim,eps=norm_eps),mlp_norm=RMSNorm(dim=embed_dim,eps=norm_eps),)tok_embeddings=nn.Embedding(vocab_size,embed_dim)output_proj=nn.Linear(embed_dim,vocab_size,bias=False)returnTransformerDecoder(tok_embeddings=tok_embeddings,layer=layer,num_layers=num_layers,max_seq_len=max_seq_len,num_heads=num_heads,head_dim=head_dim,norm=RMSNorm(embed_dim,eps=norm_eps),output=output_proj,)
defphi3_mlp(dim:int,hidden_dim:int)->FeedForward:""" Build the MLP layer associated with the Phi3 Mini 4K Instruct model. """gate_proj=nn.Linear(dim,hidden_dim,bias=False)down_proj=nn.Linear(hidden_dim,dim,bias=False)up_proj=nn.Linear(dim,hidden_dim,bias=False)returnFeedForward(gate_proj=gate_proj,down_proj=down_proj,up_proj=up_proj)# ------------------ LoRA Phi3 ------------------
[docs]deflora_phi3(lora_attn_modules:List[LORA_ATTN_MODULES],apply_lora_to_mlp:bool=False,apply_lora_to_output:bool=False,*,# phi3 argsvocab_size:int,num_layers:int,num_heads:int,num_kv_heads:int,embed_dim:int,intermediate_dim:int,max_seq_len:int,attn_dropout:float=0.0,norm_eps:float=1e-5,rope_base:int=10_000,# LoRA argslora_rank:int,lora_alpha:float,lora_dropout:float=0.0,# Quantization argsquantize_base:bool=False,)->TransformerDecoder:""" Return a version of Phi3 (an instance of :func:`~torchtune.modules.TransformerDecoder`) with LoRA applied based on the passed in configuration. 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 vocab_size (int): number of tokens in vocabulary. num_layers (int): number of layers in the transformer decoder. num_heads (int): number of query heads. For MHA this is also the number of heads for key and value num_kv_heads (int): number of key and value heads. If specified, user should ensure `num_heads` % `num_kv_heads` == 0. Default value is `None`, in which case this is the same as MHA embed_dim (int): embedding dimension for self-attention intermediate_dim (int): intermediate dimension for MLP max_seq_len (int): maximum sequence length the model will be run with, as used by :func:`~torchtune.modules.KVCache` attn_dropout (float): dropout value passed onto scaled_dot_product_attention. Default: 0.0 norm_eps (float): epsilon in RMS norms. rope_base (int): base value for Rotary Position Embeddings. Default: 10000 lora_rank (int): rank of each low-rank approximation lora_alpha (float): scaling factor for the low-rank approximation lora_dropout (float): LoRA dropout probability. Default: 0.0 quantize_base: (bool): Whether to quantize base model weights or not. Only applied to base weights within linear layers LoRA is applied to. The final output linear projection is not supported for quantization currently. Returns: TransformerDecoder: Instantiation of Llama3 model with LoRA applied to a subset of the attention projections in each layer. """self_attn=lora_phi3_self_attention(lora_modules=lora_attn_modules,embed_dim=embed_dim,num_heads=num_heads,num_kv_heads=num_kv_heads,max_seq_len=max_seq_len,attn_dropout=attn_dropout,rope_base=rope_base,lora_rank=lora_rank,lora_alpha=lora_alpha,lora_dropout=lora_dropout,quantize_base=quantize_base,)ifapply_lora_to_mlp:mlp=lora_phi3_mlp(dim=embed_dim,hidden_dim=intermediate_dim,lora_rank=lora_rank,lora_alpha=lora_alpha,quantize_base=quantize_base,lora_dropout=lora_dropout,)else:mlp=phi3_mlp(dim=embed_dim,hidden_dim=intermediate_dim)layer=TransformerDecoderLayer(attn=self_attn,mlp=mlp,sa_norm=RMSNorm(dim=embed_dim,eps=norm_eps),mlp_norm=RMSNorm(dim=embed_dim,eps=norm_eps),)tok_embeddings=nn.Embedding(vocab_size,embed_dim)# TODO: quantize_base is not applied to final output_proj currently.output_proj=(LoRALinear(embed_dim,vocab_size,rank=lora_rank,alpha=lora_alpha,dropout=lora_dropout)ifapply_lora_to_outputelsenn.Linear(embed_dim,vocab_size,bias=False))model=TransformerDecoder(tok_embeddings=tok_embeddings,layer=layer,num_layers=num_layers,max_seq_len=max_seq_len,num_heads=num_heads,head_dim=(embed_dim//num_heads),norm=RMSNorm(embed_dim,eps=norm_eps),output=output_proj,)ifquantize_base:# For QLoRA, we reparametrize 4-bit tensors to bf16, and offload to CPU on the fly# so as to not increase peak memorymodel._register_state_dict_hook(partial(reparametrize_as_dtype_state_dict_post_hook,dtype=tok_embeddings.weight.dtype,offload_to_cpu=True))returnmodel
deflora_phi3_self_attention(lora_modules:List[LORA_ATTN_MODULES],*,# CausalSelfAttention argsembed_dim:int,num_heads:int,num_kv_heads:int,max_seq_len:int,attn_dropout:float=0.0,rope_base:int=10_000,# LoRA argslora_rank:int,lora_alpha:float,lora_dropout:float=0.0,quantize_base:bool=False,)->CausalSelfAttention:""" Return an instance of :func:`~torchtune.modules.CausalSelfAttention` with LoRA applied to a subset of its linear layers Args: lora_modules (List[LORA_ATTN_MODULES]): list of which linear layers LoRA should be applied to. Options are ``{"q_proj", "k_proj", "v_proj", "output_proj"}``. embed_dim (int): embedding dimension for self-attention num_heads (int): number of query heads. For MHA this is also the number of heads for key and value num_kv_heads (int): number of key and value heads. If specified, user should ensure `num_heads` % `num_kv_heads` == 0. Default value is `None`, in which case this is the same as MHA max_seq_len (int): maximum sequence length the model will be run with, as used by :func:`~torchtune.modules.KVCache` attn_dropout (float): dropout value passed onto scaled_dot_product_attention. Default: 0.0 rope_base (int): base value for Rotary Position Embeddings. Default: 10000 lora_rank (int): rank of each low-rank approximation lora_alpha (float): scaling factor for the low-rank approximation lora_dropout (float): LoRA dropout probability. Default: 0.0 quantize_base (bool): Whether to quantize base model parameters for linear layers LoRA is being applied to. Default is ``False``. Returns: CausalSelfAttention: instantiation of self-attention module with LoRA applied to a subset of Q, K, V, output projections. Raises: ValueError: If lora_modules arg is an empty list """ifnotlora_modules:raiseValueError(f"Must pass one or more of {LORA_ATTN_MODULES} as lora_modules")head_dim=embed_dim//num_headsnum_kv_heads=num_kv_headsifnum_kv_headselsenum_headsq_proj=(LoRALinear(embed_dim,num_heads*head_dim,rank=lora_rank,alpha=lora_alpha,dropout=lora_dropout,quantize_base=quantize_base,)if"q_proj"inlora_moduleselsenn.Linear(embed_dim,num_heads*head_dim,bias=False))k_proj=(LoRALinear(embed_dim,num_kv_heads*head_dim,rank=lora_rank,alpha=lora_alpha,dropout=lora_dropout,quantize_base=quantize_base,)if"k_proj"inlora_moduleselsenn.Linear(embed_dim,num_kv_heads*head_dim,bias=False))v_proj=(LoRALinear(embed_dim,num_kv_heads*head_dim,rank=lora_rank,alpha=lora_alpha,dropout=lora_dropout,quantize_base=quantize_base,)if"v_proj"inlora_moduleselsenn.Linear(embed_dim,num_kv_heads*head_dim,bias=False))output_proj=(LoRALinear(embed_dim,embed_dim,rank=lora_rank,alpha=lora_alpha,dropout=lora_dropout,quantize_base=quantize_base,)if"output_proj"inlora_moduleselsenn.Linear(embed_dim,embed_dim,bias=False))rope=Phi3RotaryPositionalEmbeddings(dim=head_dim,max_seq_len=max_seq_len,base=rope_base)self_attn=CausalSelfAttention(embed_dim=embed_dim,num_heads=num_heads,num_kv_heads=num_kv_heads,head_dim=head_dim,q_proj=q_proj,k_proj=k_proj,v_proj=v_proj,output_proj=output_proj,pos_embeddings=rope,max_seq_len=max_seq_len,attn_dropout=attn_dropout,)returnself_attndeflora_phi3_mlp(*,dim:int,hidden_dim:int,lora_rank:int,lora_alpha:float,lora_dropout:float=0.0,quantize_base:bool=False,)->FeedForward:gate_proj=LoRALinear(in_dim=dim,out_dim=hidden_dim,rank=lora_rank,alpha=lora_alpha,dropout=lora_dropout,quantize_base=quantize_base,)down_proj=LoRALinear(in_dim=hidden_dim,out_dim=dim,rank=lora_rank,alpha=lora_alpha,dropout=lora_dropout,quantize_base=quantize_base,)up_proj=LoRALinear(in_dim=dim,out_dim=hidden_dim,rank=lora_rank,alpha=lora_alpha,dropout=lora_dropout,quantize_base=quantize_base,)returnFeedForward(gate_proj=gate_proj,down_proj=down_proj,up_proj=up_proj,)
Docs
Access comprehensive developer documentation for PyTorch
To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. As the current maintainers of this site, Facebook’s Cookies Policy applies. Learn more, including about available controls: Cookies Policy.