Pretraining a frontier Large Language Model from scratch requires millions of dollars in electricity, tens of thousands of GPUs, and months of compute. Fortunately, developers can take an existing open-weights base foundation model (such as Meta's Llama 3 or Mistral) and customize it for proprietary enterprise domains through Fine-Tuning.
In Full Fine-Tuning, every single parameter in the model is updated via backpropagation. For a 70B parameter model, storing the model weights, Adam optimizer states (first and second moments require 8 bytes per parameter), and activations in FP16 requires over 1,200 GB of GPU VRAM — completely inaccessible to most engineering teams.
Parameter-Efficient Fine-Tuning (PEFT) freezes 99% of the base model parameters and injects a tiny fraction of new learnable weights (typically 0.1% to 1%), slashing memory requirements while matching full fine-tuning performance.
Aghajanyan et al. (2020) demonstrated that the weight updates during LLM adaptation have a low intrinsic dimension. Microsoft researchers leveraged this to create LoRA (Low-Rank Adaptation).
Instead of updating the full dense weight matrix ( mathbf{W}_0 in mathbb{R}^{d imes k} ), LoRA decomposes the weight update matrix ( Delta mathbf{W} ) into the product of two low-rank matrices ( mathbf{B} ) and ( mathbf{A} ):
Where ( mathbf{W}_0 in mathbb{R}^{d imes k} ) is completely frozen.
( mathbf{A} in mathbb{R}^{r imes k} ) is initialized with Gaussian noise.
( mathbf{B} in mathbb{R}^{d imes r} ) is initialized to zero (so ( Delta mathbf{W} = 0 ) at training onset).
Rank ( r ): A tiny integer (typically ( r=8, 16, ext{or } 32 )), where ( r ll min(d, k) ).
Scaling Factor ( alpha ): A constant (typically ( 2 imes r )) that stabilizes optimization when varying ( r ).
The Parameter Reduction: If ( d=4096 ) and ( k=4096 ), full matrix ( mathbf{W}_0 ) has ( 16,777,216 ) parameters. With LoRA rank ( r=8 ), matrices ( mathbf{A} ) and ( mathbf{B} ) have only ( (4096 imes 8) + (8 imes 4096) = 65,536 ) parameters — a 99.6% reduction in learnable parameters!
Tim Dettmers et al. introduced QLoRA (Quantized LoRA), introducing three algorithmic breakthroughs that allow fine-tuning a 70B model on a single 48GB GPU (or a 7B model on a 16GB laptop GPU!):
At inference time, LoRA adapter weights can be mathematically merged directly into the base weights: ( mathbf{W}_{ ext{final}} = mathbf{W}_0 + rac{alpha}{r}(mathbf{B}mathbf{A}) ), introducing zero additional latency. Furthermore, modern serving frameworks (like vLLM) support dynamic multi-LoRA serving: a single shared base model in VRAM can serve dozens of different domain-specific LoRA adapters simultaneously across requests!
The following snippet demonstrates setting up a LoRA configuration for a Transformer model using the peft library:
from peft import LoraConfig, get_peft_model, TaskType
import torch
import torch.nn as nn
# Mock Transformer Attention Layer
class MockAttentionLayer(nn.Module):
def __init__(self, d_model=4096):
super(MockAttentionLayer, self).__init__()
self.q_proj = nn.Linear(d_model, d_model)
self.v_proj = nn.Linear(d_model, d_model)
def forward(self, x):
return self.q_proj(x) + self.v_proj(x)
base_model = MockAttentionLayer()
# 1. Define LoRA Configuration
peft_config = LoraConfig(
task_type=TaskType.CAUSAL_LM,
r=16, # LoRA Rank
lora_alpha=32, # Scaling factor (alpha / r = 2.0)
target_modules=["q_proj", "v_proj"], # Target attention matrices
lora_dropout=0.05,
bias="none"
)
# 2. Wrap Base Model with LoRA Adapters
lora_model = get_peft_model(base_model, peft_config)
# 3. Print Trainable Parameter Audit
lora_model.print_trainable_parameters()
Broaden your foundational and advanced AI knowledge across our curriculum:
Full parameter pretraining vs parameter-efficient fine-tuning.
When to choose RAG vs when to choose fine-tuning for your enterprise.
Fine-tuning base models for human preference and safety alignment.
Serving dynamic LoRA adapters on shared base models with vLLM.