Fine-Tune

Fine-tuning is the process of adapting a pretrained language model to specific tasks, domains, or behaviors — taking a foundation model trained on general data and updating its weights using smaller, curated datasets, enabling specialized performance that outperforms generic models while requiring far less compute than training from scratch.

What Is Fine-Tuning?

  • Definition: Continued training of a pretrained model on task-specific data.
  • Input: Pretrained base model + domain-specific dataset.
  • Output: Specialized model adapted to target task/domain.
  • Purpose: Customize behavior without pretraining costs.

Why Fine-Tuning Matters

  • Specialization: Adapt general models to specific domains (medical, legal, code).
  • Efficiency: 1000× cheaper than pretraining from scratch.
  • Quality: Often outperforms in-context learning for specialized tasks.
  • Consistency: Reliable output format and style.
  • Proprietary Data: Incorporate private or specialized knowledge.
  • Reduced Prompt Length: Bake instructions into weights.

Fine-Tuning Methods

Supervised Fine-Tuning (SFT):
- Train on (instruction, response) pairs.
- Direct demonstration of desired behavior.
- Most common and straightforward approach.

Reinforcement Learning from Human Feedback (RLHF):
- Train reward model on human preference comparisons.
- Optimize policy via PPO to maximize reward.
- More complex but enables nuanced alignment.

Direct Preference Optimization (DPO):
- Directly optimize on preference data without reward model.
- Simpler than RLHF, similar results.
- Increasingly popular for alignment.

Constitutional AI (CAI):
- Self-critique using principles.
- Model evaluates and improves its own responses.
- Reduces need for human labeling.

Parameter-Efficient Fine-Tuning (PEFT)

LoRA (Low-Rank Adaptation):

Original: W (d × d matrix, frozen)
LoRA: W + BA (B is d × r, A is r × d)
r << d (e.g., r=16, d=4096)

Train only A and B: 0.1-1% of parameters
Merge at inference: W' = W + BA

QLoRA:
- Load base model in 4-bit quantization.
- Train LoRA adapters in FP16.
- Fine-tune 70B models on single 24-48GB GPU.

Other PEFT Methods:
- Prefix Tuning: Learn continuous prompt embeddings.
- Adapters: Insert small trainable modules between layers.
- IA³: Scale activations with learned vectors.

When to Fine-Tune vs. Prompt

Approach         | Best For
-----------------|------------------------------------------
Prompting/RAG    | Variable tasks, fast iteration, small data
Fine-Tuning      | Consistent format, domain expertise, scale
Full FT          | New capabilities, architecture changes
PEFT (LoRA)      | Limited compute, multiple adapters

Fine-Tuning Pipeline

LoRA — Low-Rank Adaptation of Large Models freeze all weights, inject trainable low-rank matrices: W + ΔW where ΔW = B·A (rank r ≪ d) LoRA Weight Decomposition (one linear layer) W d × d frozen (no grad) + B d × r × A r × d = ΔW d × d rank r × α/r h = W·x + (α/r)·B·A·x — only A and B are trained Why LoRA Works Parameter savings: Full: d×d = 4096² = 16.8M per layer LoRA r=16: 2×d×r = 131K per layer → 128× fewer trainable params Practical benefits: • No inference latency (merge at deploy) • Multiple adapters share one base • Train on single GPU (70B model) Target Modules in Transformer Attention: W_q, W_k, W_v, W_o MLP: W_up, W_gate, W_down Typical: all attention + MLP projections rank r = 8-64, α = 16-32 (common choices) PEFT Variants QLoRA 4-bit base + LoRA (fit 70B on 48 GB) DoRA decompose magnitude and direction AdaLoRA adaptive rank allocation per layer LoRA+ different LR for A and B matrices Production LoRA Serving HuggingFace PEFT standard library S-LoRA / Punica batched multi-adapter vLLM + LoRA hot-swap per request Axolotl/Unsloth training frameworks Weight updates during fine-tuning are low-rank in practice — LoRA exploits this structure directly. LoRA made fine-tuning accessible: adapt a 70B model on a single GPU, merge at deploy with zero overhead.

Tools & Frameworks

  • Hugging Face: transformers, peft, trl libraries.
  • Axolotl: Streamlined fine-tuning configuration.
  • LLaMA-Factory: GUI and CLI for fine-tuning.
  • Unsloth: Memory-efficient fine-tuning.
  • Together AI, Modal, Lambda: Cloud fine-tuning services.

Fine-tuning is the bridge between general AI and domain-specific solutions — it enables organizations to create customized models that understand their specific terminology, formats, and requirements while building on the massive investment in foundation model pretraining.

Take fine-tune further

Ask the copilot about this term, or have our engineers assess it against your process.