LoRA - Playbook chỉnh tham số và sweep

LoRA — Playbook chỉnh tham số và sweep

Bộ ba note về LoRA. Ba note dưới đây tách theo câu hỏi mà chúng trả lời:

Gộp ngày 2026-08-06 từ bốn note cũ vốn chồng nội dung nhau.

Giá trị khởi điểm của từng tham số nằm ở note [Siêu tham số r, alpha và target modules](LoRA - Siêu tham số r, alpha và target modules.md). Note này chỉ nói cách dò ra giá trị tốt hơn khởi điểm đó.

Cấu hình khởi điểm

# PEFT LoRA for Llama-2-7b SFT
# References:
# - Unsloth hyperparam guide (r/alpha/targets/dropout/LR): https://docs.unsloth.ai/get-started/fine-tuning-llms-guide/lora-hyperparameters-guide
# - QLoRA paper and LR guidance: https://arxiv.org/abs/2305.14314
# - NVIDIA NeMo QLoRA tips (LR 2e-4 small, 1e-4 big): https://docs.nvidia.com/nemo-framework/user-guide/24.12/sft_peft/qlora.html
from peft import LoraConfig
lora_config = LoraConfig(
    r=16,                     # try 16 → 32 if underfitting
    lora_alpha=16,            # try 32 if underfitting (alpha = 2r)
    lora_dropout=0.0,         # use 0.05–0.1 only if overfitting
    target_modules=["q_proj","k_proj","v_proj","o_proj","gate_proj","up_proj","down_proj"],
    bias="none",
    task_type="CAUSAL_LM",
)
# Trainer hints:
# learning_rate=2e-4, warmup_ratio=0.1, weight_decay=0.01, lr_scheduler_type="cosine"
# num_train_epochs=2–3 for ~4k examples; eval each epoch; early stop on plateaus.

Trình tự chỉnh cho dataset nhỏ (~4k mẫu)

Tuning playbook for your 4k-example dialog summarization:

  1. Start: r=16, alpha=16, dropout=0.0, LR=2e-4, warmup=10%, cosine, target all modules.
  2. If underfitting (val loss high, ROUGE low, outputs generic): set alpha=32; if still flat, set r=32. Keep LR at 2e-4. (docs.unsloth.ai)
  3. If overfitting (train loss falls, val ROUGE drops): add dropout=0.05–0.1, enable small weight_decay=0.01, or reduce epochs. (docs.unsloth.ai)
  4. If unstable loss: reduce LR to 1e-4 or keep r fixed and lower alpha. (NVIDIA Docs)

Extra sources that explain choices and trade-offs:


Use a small, disciplined sweep. Optimize learning rate and lora_alpha first. Hold r and target_modules fixed. Add complexity only if the metric stalls.

Workflow

  1. Lock a baseline
  1. Pick robust metrics and a validation split
  1. Bound the learning rate with an LR-range test
  1. Run a budgeted Bayesian sweep
  1. Triage hyperparameters in tiers
  1. Use early stopping and learning-curve diagnostics
  1. Control variance
  1. Confirm generalization with a quick ablation

Minimal, practical search spaces

Start tight. Widen only if results cluster at an edge.

Example: Optuna over HF Trainer + PEFT

# Optuna + HF Trainer + PEFT LoRA
# Docs:
# - HF PEFT LoRA: https://huggingface.co/docs/peft/en/package_reference/lora
# - HF Trainer hparam search threads: https://discuss.huggingface.co/t/using-hyperparameter-search-in-trainer/785
# - Optuna: https://optuna.org, Ray Tune alt: https://docs.ray.io/en/latest/tune/index.html
import optuna

def objective(trial):
    # TIER 1 + small TIER 2
    lr = trial.suggest_float("learning_rate", 1e-4, 3e-4, log=True)  # band from LR-range test
    warmup = trial.suggest_float("warmup_ratio", 0.05, 0.1)
    r = trial.suggest_categorical("lora_r", [16, 32])
    alpha = trial.suggest_categorical("lora_alpha", [r, 2*r])
    dropout = trial.suggest_categorical("lora_dropout", [0.0, 0.05])

    # build LoraConfig and Trainer, train for 1–2 epochs, evaluate on dev
    # return ROUGE-L (maximize) or 1/val_loss (minimize)
    return dev_metric_value

If you prefer a config-file approach, use a W&B Sweep (grid/random/BOHB) with the same spaces and metric. (Weights & Biases Documentation)

Learning-rate range test in one line of reasoning

What to log and watch

Why this works

Short curated references


Here’s a tight, high-signal reading list. Grouped. Each item states why it’s useful and the date.

Canonical papers

Official docs and model-specific notes

Practical hyperparameter guides

Variants and improvements

HPO tooling

Common pitfalls and “gotchas” to avoid

Focused forum threads worth scanning

Minimal playbook distilled from these sources