Giới thiệu
LoRA và QLoRA là kỹ thuật fine-tune tiết kiệm nhất — chỉ cập nhật 0.1–1% parameters, chạy được trên GPU miễn phí (Colab T4).
1. LoRA — Intuition
Thay vì update TOÀN BỘ weight matrix W (d × d):
W_new = W + ΔW
LoRA decompose ΔW thành 2 matrices nhỏ:
ΔW = A × B (A: d × r, B: r × d, với r << d)
Ví dụ:
W: 4096 × 4096 = 16.7M params → cập nhật TẤT CẢ
LoRA (r=16): 4096×16 + 16×4096 = 131K params → cập nhật 0.8%
2. QLoRA — Quantize + LoRA
QLoRA = 4-bit quantization base model + LoRA adapters
→ Giảm VRAM từ 32GB → 6GB
→ Chạy được trên Google Colab T4 (16GB)!
3. Hands-on với Unsloth
from unsloth import FastLanguageModel
# Load model với 4-bit quantization
model, tokenizer = FastLanguageModel.from_pretrained(
model_name="unsloth/Meta-Llama-3.1-8B-Instruct",
max_seq_length=2048,
load_in_4bit=True,
)
# Add LoRA adapters
model = FastLanguageModel.get_peft_model(
model, r=16, lora_alpha=16,
target_modules=["q_proj","k_proj","v_proj","o_proj"],
lora_dropout=0,
)
# Train
from trl import SFTTrainer
trainer = SFTTrainer(
model=model,
dataset=dataset,
max_seq_length=2048,
args=TrainingArguments(
per_device_train_batch_size=2,
num_train_epochs=3,
learning_rate=2e-4,
output_dir="outputs",
),
)
trainer.train()
Tóm tắt
- LoRA: chỉ update ~1% parameters → tiết kiệm GPU và thời gian
- QLoRA: thêm quantization → chạy trên consumer GPU
- Unsloth: 2x nhanh hơn standard LoRA training
- Chi phí: $0 trên Colab, hoặc ~$1–3/giờ cloud GPU
Bài tập
- Fine-tune LLaMA 3 8B trên Google Colab (QLoRA)
- So sánh output: LoRA FT vs API FT (Gemini/OpenAI)
- Thử rank r=8 vs r=16 vs r=32 — so sánh quality
- Merge LoRA adapters và export model hoàn chỉnh