LLM Quest Academy
← Quay lại Blog

Fine-tune LoRA cho LLM từ dataset nhỏ: hướng dẫn zero-to-deploy

LLM Quest Editorial·20 thg 4, 2026·3 phút đọc
Fine-tune LoRA cho LLM từ dataset nhỏ: hướng dẫn zero-to-deploy
Fine-tune LoRA cho LLM từ dataset nhỏ: hướng dẫn zero-to-deploy

LoRA (Low-Rank Adaptation) là cách rẻ nhất để customize 1 LLM cho domain cụ thể. 500–2000 sample đã đủ cho nhiều task. Chi phí $5–30 trên Colab Pro. Deploy được trong 1 ngày.

Khi nào nên dùng LoRA?

  • Prompt engineering chạm trần (< 85% accuracy)
  • Cần tone/style đặc thù (brand voice, dialect)
  • Output format nghiêm ngặt (JSON schema khó control qua prompt)
  • Latency < 500ms (prompt dài quá chậm)
  • Privacy: không muốn gửi data lên cloud API

Chuẩn bị data

Rule of thumb: 500 sample tốt hơn 5000 sample tạp nham.

Format chuẩn (JSONL):

{"messages": [{"role": "system", "content": "..."}, {"role": "user", "content": "..."}, {"role": "assistant", "content": "..."}]}

Quality > quantity:

  • Review 100% sample trong 200 đầu tiên
  • Review 20–30% các sample sau (bootstrap bằng GPT-5 rồi human review)
  • Loại duplicate (cosine > 0.95)
  • Split: 80% train / 10% val / 10% test

Stack khuyến nghị 2026

  • Base model: Llama 4 8B, Qwen 3 7B, Mistral Nemo 12B
  • Training framework: Unsloth (nhanh 2× so với HuggingFace trainer)
  • Hardware: A100 40GB hoặc 2× RTX 4090 24GB
  • Hyperparameters: LoRA rank 16, alpha 32, learning rate 2e-4, 3 epoch

Code mẫu Unsloth

from unsloth import FastLanguageModel
from trl import SFTTrainer, SFTConfig

model, tokenizer = FastLanguageModel.from_pretrained(
    model_name="unsloth/Meta-Llama-3.1-8B-Instruct",
    max_seq_length=2048,
    load_in_4bit=True,
)

model = FastLanguageModel.get_peft_model(
    model,
    r=16, lora_alpha=32,
    target_modules=["q_proj", "k_proj", "v_proj", "o_proj"],
)

trainer = SFTTrainer(
    model=model,
    tokenizer=tokenizer,
    train_dataset=train_ds,
    eval_dataset=val_ds,
    args=SFTConfig(
        num_train_epochs=3,
        per_device_train_batch_size=2,
        learning_rate=2e-4,
        warmup_ratio=0.03,
    ),
)
trainer.train()

Đánh giá

Không tin loss xuống. Dùng test set + metrics:

  • Task-specific: accuracy, F1, BLEU (tuỳ task)
  • LLM-as-judge: GPT-5 chấm output vs ground truth (pairwise)
  • Human eval 50 sample: catch lỗi auto-eval bỏ qua

Mastery checkpoint: nếu LoRA không beat base model + 3 few-shot prompt 5 điểm → data/hyperparam có vấn đề.

Deploy

Option 1 — Together.ai / Modal: upload adapter, pay-per-token, $0.05/M token. Rẻ, scale tự động.

Option 2 — Self-host: vLLM + adapter, chạy 1 A10G $0.7/h, handle 50+ RPS. Rẻ nếu traffic ổn định.

Option 3 — HuggingFace Inference Endpoint: Click-deploy, $1/h cho GPU nhỏ. Đơn giản nhất.

Chi phí thực tế

Stage Cost
Data prep (1000 sample + review) $10 GPT-5 + 5h công
Training (3 epoch trên 1000 sample) $5–15 trên Colab A100
Deploy 1 tháng $50 Together/Modal hoặc $500 self-host 1 A10G
Tổng MVP ~$100 + vài ngày công

Kết luận

LoRA biến fine-tune từ "dự án 6 tháng" thành "dự án cuối tuần". Chuẩn bị data tốt > training tốt. Start small (500 sample), validate, rồi scale lên 2000–5000 nếu thấy cần.

LLM Quest Academy Level 7 có boss project: build SQL assistant bằng LoRA trên 1000 sample, deploy Together.ai, chi phí < $30.

🛠 Open-source: chạy fine-tune ngay trên máy bạn

Không muốn copy-paste từng đoạn code? Repo open-source bên dưới có sẵn pipeline LoRA hoàn chỉnh — clone và chạy:

👉 github.com/learningmapn/llm-finetuning

Bao gồm:

  • Script chuẩn bị dataset (instruction, input, output) từ CSV/JSON
  • Config LoRA (rank, alpha, target modules) cho các base model phổ biến
  • Training loop với eval cuối epoch
  • Export adapter + merge với base model
  • Notebook Colab + script CLI

Hợp với bài này: làm theo §3 (data prep) trên repo, rồi training script tự lo phần còn lại. Đầu ra adapter có thể deploy lên Together / Modal / self-host theo §5.

#LoRA#fine-tuning#Unsloth#LLM#deploy
Advertisement