blog.dopana

Back

Parameter-Efficient Fine-Tuning (PEFT), particularly LoRA (Low-Rank Adaptation), has revolutionized fine-tuning Large Language Models by eliminating the need for expensive GPU clusters.

How LoRA Works#

Instead of updating the full weight matrix WRd×kW \in \mathbb{R}^{d \times k}, LoRA freezes the original pre-trained weight W0W_0 and injects trainable rank-decomposition matrices AA and BB:

W=W0+ΔW=W0+BAW = W_0 + \Delta W = W_0 + B \cdot A

Where ARr×kA \in \mathbb{R}^{r \times k} and BRd×rB \in \mathbb{R}^{d \times r} with rank rmin(d,k)r \ll \min(d, k).

Implementing LoRA with Hugging Face PEFT#

lora_config.py
from peft import LoraConfig, get_peft_model

peft_config = LoraConfig(
    r=8,
    lora_alpha=16,
    target_modules=["q_proj", "v_proj"], # [!code focus]
    lora_dropout=0.05,
    bias="none",
    task_type="CAUSAL_LM"
)

# model = get_peft_model(model, peft_config)
python

Key Advantages of LoRA#

  • VRAM Optimization: Reduces training VRAM footprint by 60-70%.
  • Lightweight Checkpoints: Adapter weights are extremely small (ranging from MBs to tens of MBs).
  • Zero Inference Overhead: Adapter matrices can be merged back into base weights upon deployment.

References#