๐ Welcome! This Interactive Guide Walks You Through the Complete LLM Fine-Tuning Lifecycle
You are about to trace how a general-purpose AI (like GPT / Llama) is transformed into a domain-specific financial analyst โ step by step. The 6 stages below mirror exactly how production ML teams at companies like Bloomberg, Morgan Stanley, and Google fine-tune LLMs:
1. CPT โ Feed raw domain text (SEC filings)
2. SFT โ Teach Q&A format with loss masking
3. Adapters โ Attach LoRA/DoRA sidecar modules
4. Softmax โ Watch word probabilities shift
5. Training โ Simulate loss descent in real-time
6. Alignment โ Prefer concise, accurate answers
๐ก Tip: Click any word, hover any matrix cell, drag sliders, and toggle between LoRA/DoRA/Base to explore interactively. Every visual updates in real-time!
1
Stage 1: Continual Pre-Training (CPT)
CPT: Raw Domain Text
Ingesting Unstructured Domain Knowledge (SEC 10-Ks)
๐ Formal Loss Formulation (Autoregressive Cross-Entropy)
$$\mathcal{L}_{\text{CPT}}(\theta) = - \sum_{t=1}^{T} \log P_\theta(x_t \mid x_{\lt t})$$
Every token
x_t in the domain corpus is active (L = 1.0). The model learns to predict the next word at every position, absorbing domain vocabulary and writing patterns into its weight matrices.
๐ก Click any word below or use the stepper to trace continuous next-word predictions:
๐ฏ Selected Token: Click any word above to inspect its token ID and next-word training target.
2
Stage 2: Supervised Fine-Tuning (SFT)
SFT: Q&A Flashcards
Instruction Tuning & Prompt Loss Masking (L = -100)
๐ Completion-Only Loss (PyTorch ignore_index = -100)
$$\mathcal{L}_{\text{SFT}}(\theta) = - \sum_{t=|x|+1}^{|x|+|y|} \log P_\theta(y_t \mid x, y_{\lt t})$$
Loss is computed only over assistant response tokens
y. Prompt tokens x have label = -100 (PyTorch's ignore_index), which zeroes their gradient contribution while still letting them inform the attention context.
๐ก Click words to compare Masked Prompt (-100) vs Graded Response (L=1.0):
๐ฏ Selected Token: Notice how prompt tokens have L = -100 (Ignored), while assistant tokens have L = 1.0 (Graded!).
3
Stage 3: Inside The AI Brain
How Adapters (LoRA & DoRA) Nudge Thoughts
๐ฎ How to explore this stage:
Toggle between Base / LoRA / DoRA tabs above to compare architectures.
Switch between โก Electrical Pathway (circuit flow) and ๐งฎ 2D Matrix (numeric weights) views below.
Drag the Rank (r) and Alpha (ฮฑ) sliders to see how hyperparameters change adapter behavior in real-time.
โก Electrical Thought Pathway:
โก Hover or click any block: See how electrical current passes through each module!
What the AI is Thinking (Concept Neurons)
Word: " EBITDA"
Watch how the adapter dials down generic chatter and dials up specialized domain concepts:
โข Step 0: The adapter hasn't learned yet. All concept nudges are neutral until you click Train!
4
Stage 4: The Next-Word Race
LoRA Adapted
Which Word Does the AI Guess Next?
๐ Softmax Probability (LM Head Projection)
$$P(w_i \mid x) = \frac{\exp(z_i / T)}{\sum_{j=1}^{|V|} \exp(z_j / T)}, \quad z_i = W_{\text{head}, i}^\top h$$
The adapted hidden state
h โ โ4096 is projected through the vocabulary head (50,000+ words) to produce logits z, then normalized via Softmax. T = 1.0 for standard greedy decoding. As the adapter trains, the logit z for domain words like "margins" increases, pushing its probability bar upward.
Word Context:
Adapter Training:
Target Correct Word: " margins"
Before fine-tuning, the Base Model guessed generic words (" growth", " revenue"). Adapter training successfully shifts the model's guess to the correct domain term.
5
Stage 5: Interactive Training Simulation
Step 0 / 10
Watch the AI Learn & Error Drop (Interactive Simulation)
๐ AI Confusion Score (Click any step node on the chart to jump!)
Current Error: 2.842
๐ Confusion Score
2.842
High Confusion (Untrained)
๐ฏ Target Word Confidence
4%
Surges to >90% with training
๐ Perplexity (PPL = eLoss)
17.15
Guessing among ~17 words (Untrained)
๐พ Memory & Weights Saved
99.6%
Only trains 0.4% adapter params
6
Stage 6: Post-Training Alignment
Teaching AI Quality Preferences (RLHF vs. DPO vs. SimPO)
๐ค Why do we need alignment after training?
After SFT and LoRA training (Stages 1โ5), the model is accurate but not necessarily helpful. It may generate verbose, rambling, or repetitive answers because standard cross-entropy loss doesn't penalize wordiness โ a 120-word fluff answer and a crisp 15-word answer both get graded the same if they contain the target words. Alignment teaches the model to prefer quality and conciseness.
๐พ GPU VRAM Footprint & Model Architecture:
50% Memory Savings vs DPO
โก SimPO Architecture: Only 1 model loaded in GPU VRAM (Reference-Free). Fast, lightweight, and memory-efficient!
Preference Battle: Chosen (yw) vs. Rejected (yl)
Prompt: "Summarize Q3 EBITDA"
๐ข Candidate A: Winning Answer (yw)
15 tokens (Crisp)
"Q3 EBITDA grew 18% YoY to $42M, driven by 120bps margin expansion in Enterprise SaaS."
Avg Log-Prob: -0.42 / tok
SimPO Reward: +1.60
๐ด Candidate B: Rejected Answer (yl)
120 tokens (Verbose)
"EBITDA is an acronym that stands for earnings before interest, taxes, depreciation, and amortization, which is a financial metric that many executives..."
Avg Log-Prob: -1.85 / tok
SimPO Reward: -0.74
๐ฏ Reward Separation Gap (ฮr = r_w - r_l):
+2.34
Target Margin Threshold (ฮณ = 0.8)
โ
Target Margin Satisfied!
๐ Side-by-Side Comparison: RLHF vs. DPO vs. SimPO
| Method | Models in GPU VRAM | Reference Model Needed? | Length-Bias / Verbosity Immune? | Target Margin (ฮณ)? | Relative Training Speed |
|---|---|---|---|---|---|
| ๐ค RLHF (PPO) | 4 Models (~96 GB) | Yes (Frozen ฯ_ref) | โ Vulnerable (Exploits length) | โ No margin | 1.0ร (Slow 4-step loop) |
| โ๏ธ DPO | 2 Models (~48 GB) | Yes (Frozen ฯ_ref) | โ Vulnerable (Raw log-prob sum) | โ No margin | 2.0ร (Simpler loss) |
| โก SimPO (SOTA) | 1 Model (~24 GB) | โ NO (Reference-Free!) | โ 100% Immune (1/|y| Normalized) | โ Enforced (ฮณ Margin) | 3.5ร Faster (Max throughput) |