Home Knowledge Base Fine-tuning

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?

Why Fine-Tuning Matters

Fine-Tuning Methods

Supervised Fine-Tuning (SFT):

Reinforcement Learning from Human Feedback (RLHF):

Direct Preference Optimization (DPO):

Constitutional AI (CAI):

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:

Other PEFT Methods:

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

<svg viewBox="0 0 760 470" xmlns="http://www.w3.org/2000/svg" font-family="-apple-system,Segoe UI,Roboto,sans-serif">
  <rect x="0" y="0" width="760" height="470" fill="#0d1117"/>
  <text x="380" y="28" fill="#e6edf3" font-size="21" font-weight="700" text-anchor="middle">LoRA — Low-Rank Adaptation of Large Models</text>
  <text x="380" y="48" fill="#8b98a5" font-size="12" text-anchor="middle">freeze all weights, inject trainable low-rank matrices: W + ΔW where ΔW = B·A (rank r ≪ d)</text>

  <!-- === TOP: Core LoRA mechanism === -->
  <rect x="25" y="62" width="440" height="168" rx="6" fill="#080d14" stroke="#233043" stroke-width="1.2"/>
  <text x="245" y="82" fill="#e6edf3" font-size="11" text-anchor="middle" font-weight="600">LoRA Weight Decomposition (one linear layer)</text>

  <!-- Frozen weight matrix W -->
  <rect x="45" y="100" width="85" height="85" rx="3" fill="#1a1520" stroke="#a78bfa" stroke-width="1.2"/>
  <text x="87" y="140" fill="#c4b5fd" font-size="12" text-anchor="middle" font-weight="600">W</text>
  <text x="87" y="156" fill="#6b7684" font-size="8" text-anchor="middle">d × d</text>
  <text x="87" y="196" fill="#6b7684" font-size="7.5" text-anchor="middle">frozen (no grad)</text>

  <!-- Plus sign -->
  <text x="148" y="145" fill="#8b98a5" font-size="18" text-anchor="middle">+</text>

  <!-- LoRA matrices B × A -->
  <rect x="170" y="100" width="28" height="85" rx="2" fill="#0f1a2a" stroke="#60a5fa" stroke-width="1"/>
  <text x="184" y="145" fill="#93c5fd" font-size="9" text-anchor="middle">B</text>
  <text x="184" y="196" fill="#6b7684" font-size="7.5" text-anchor="middle">d × r</text>

  <!-- × sign -->
  <text x="208" y="145" fill="#8b98a5" font-size="12" text-anchor="middle">×</text>

  <rect x="218" y="120" width="85" height="28" rx="2" fill="#14261f" stroke="#34d399" stroke-width="1"/>
  <text x="260" y="138" fill="#6ee7b7" font-size="9" text-anchor="middle">A</text>
  <text x="260" y="196" fill="#6b7684" font-size="7.5" text-anchor="middle">r × d</text>

  <!-- = ΔW -->
  <text x="318" y="145" fill="#8b98a5" font-size="12" text-anchor="middle">=</text>

  <rect x="335" y="105" width="60" height="75" rx="3" fill="#2a1a0a" stroke="#f59e0b" stroke-width="0.9"/>
  <text x="365" y="142" fill="#fbbf24" font-size="11" text-anchor="middle" font-weight="600">ΔW</text>
  <text x="365" y="158" fill="#a1701a" font-size="8" text-anchor="middle">d × d</text>
  <text x="365" y="196" fill="#6b7684" font-size="7.5" text-anchor="middle">rank r</text>

  <!-- Scaling factor -->
  <text x="430" y="142" fill="#fbbf24" font-size="9">× α/r</text>

  <!-- Annotation -->
  <text x="245" y="218" fill="#8b98a5" font-size="9" text-anchor="middle">h = W·x + (α/r)·B·A·x — only A and B are trained</text>

  <!-- === TOP RIGHT: Parameter savings === -->
  <rect x="480" y="62" width="255" height="168" rx="6" fill="#0b1220" stroke="#233043" stroke-width="1"/>
  <text x="607" y="82" fill="#e6edf3" font-size="10" text-anchor="middle" font-weight="600">Why LoRA Works</text>

  <text x="500" y="104" fill="#60a5fa" font-size="9" font-weight="600">Parameter savings:</text>
  <text x="500" y="120" fill="#8b98a5" font-size="8.5">Full: d×d = 4096² = 16.8M per layer</text>
  <text x="500" y="136" fill="#34d399" font-size="8.5">LoRA r=16: 2×d×r = 131K per layer</text>
  <text x="500" y="152" fill="#fbbf24" font-size="8.5">→ 128× fewer trainable params</text>

  <text x="500" y="176" fill="#c4b5fd" font-size="9" font-weight="600">Practical benefits:</text>
  <text x="500" y="192" fill="#8b98a5" font-size="8.5">• No inference latency (merge at deploy)</text>
  <text x="500" y="206" fill="#8b98a5" font-size="8.5">• Multiple adapters share one base</text>
  <text x="500" y="220" fill="#8b98a5" font-size="8.5">• Train on single GPU (70B model)</text>

  <!-- === MIDDLE: Where LoRA is applied === -->
  <rect x="25" y="240" width="350" height="100" rx="6" fill="#0b1220" stroke="#233043" stroke-width="1"/>
  <text x="200" y="258" fill="#e6edf3" font-size="10" text-anchor="middle" font-weight="600">Target Modules in Transformer</text>

  <text x="45" y="278" fill="#60a5fa" font-size="9">Attention: W_q, W_k, W_v, W_o</text>
  <text x="45" y="294" fill="#34d399" font-size="9">MLP: W_up, W_gate, W_down</text>
  <text x="45" y="312" fill="#fbbf24" font-size="9">Typical: all attention + MLP projections</text>
  <text x="45" y="328" fill="#6b7684" font-size="8.5">rank r = 8-64, α = 16-32 (common choices)</text>

  <!-- === MIDDLE RIGHT: Variants === -->
  <rect x="390" y="240" width="345" height="100" rx="6" fill="#0b1220" stroke="#233043" stroke-width="1"/>
  <text x="562" y="258" fill="#e6edf3" font-size="10" text-anchor="middle" font-weight="600">PEFT Variants</text>

  <text x="410" y="278" fill="#60a5fa" font-size="8.5" font-weight="600">QLoRA</text>
  <text x="460" y="278" fill="#8b98a5" font-size="8.5">4-bit base + LoRA (fit 70B on 48 GB)</text>
  <text x="410" y="296" fill="#34d399" font-size="8.5" font-weight="600">DoRA</text>
  <text x="460" y="296" fill="#8b98a5" font-size="8.5">decompose magnitude and direction</text>
  <text x="410" y="314" fill="#fbbf24" font-size="8.5" font-weight="600">AdaLoRA</text>
  <text x="470" y="314" fill="#8b98a5" font-size="8.5">adaptive rank allocation per layer</text>
  <text x="410" y="332" fill="#c4b5fd" font-size="8.5" font-weight="600">LoRA+</text>
  <text x="460" y="332" fill="#8b98a5" font-size="8.5">different LR for A and B matrices</text>

  <!-- === BOTTOM: Real usage === -->
  <rect x="25" y="350" width="710" height="62" rx="5" fill="#0b1220" stroke="#233043" stroke-width="1"/>
  <text x="380" y="368" fill="#e6edf3" font-size="10" text-anchor="middle" font-weight="600">Production LoRA Serving</text>

  <text x="130" y="390" fill="#c4b5fd" font-size="9.5" text-anchor="middle" font-weight="600">HuggingFace PEFT</text>
  <text x="130" y="404" fill="#8b98a5" font-size="8" text-anchor="middle">standard library</text>

  <text x="300" y="390" fill="#fbbf24" font-size="9.5" text-anchor="middle" font-weight="600">S-LoRA / Punica</text>
  <text x="300" y="404" fill="#8b98a5" font-size="8" text-anchor="middle">batched multi-adapter</text>

  <text x="470" y="390" fill="#34d399" font-size="9.5" text-anchor="middle" font-weight="600">vLLM + LoRA</text>
  <text x="470" y="404" fill="#8b98a5" font-size="8" text-anchor="middle">hot-swap per request</text>

  <text x="630" y="390" fill="#60a5fa" font-size="9.5" text-anchor="middle" font-weight="600">Axolotl/Unsloth</text>
  <text x="630" y="404" fill="#8b98a5" font-size="8" text-anchor="middle">training frameworks</text>

  <!-- Key insight -->
  <rect x="25" y="420" width="710" height="20" rx="3" fill="#0b1220" stroke="#233043" stroke-width="0.8"/>
  <text x="380" y="434" fill="#fbbf24" font-size="9" text-anchor="middle">Weight updates during fine-tuning are low-rank in practice — LoRA exploits this structure directly.</text>

  <text x="380" y="460" fill="#6b7684" font-size="11" text-anchor="middle">LoRA made fine-tuning accessible: adapt a 70B model on a single GPU, merge at deploy with zero overhead.</text>
</svg>

Tools & Frameworks

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.

fine-tunefine-tuningsftrlhfdpolorapeftsupervised fine-tuningtraining

Explore 500+ Semiconductor & AI Topics

From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.