VISUAL LESSON · 7 PICTURES

Instruction Finetuning: The SFT Paradigm, Alpaca & Prompt Formats

Why does a base pretrained language model fail to answer simple questions, and how does supervised instruction finetuning (SFT) transform a raw document completer into a helpful, obedient AI assistant?

Progress stays on this device · no login
BEFORE THE FIRST PICTURE

How this lesson unfolds.

Begin with How does instruction tuning transform a raw text-continuation base model into an aligned, helpful conversational assistant?. Follow the visual diagrams to build an intuitive, mathematically grounded mental model of instruction fine-tuning foundations & prompt formats.

Picture 1 of 7 · tap to inspect in full resolution
01

How does the instruction following imperative operate mathematically within the overall Instruction Fine-Tuning Foundations & Prompt Formats?

INTUITION

If you ask a base model 'What is the capital of France?', it might reply: 'What is the capital of Germany? What is the capital of Italy?'. It thinks you are writing a middle-school geography quiz! It doesn't know it is an assistant; it only knows how to continue internet documents.

MATHEMATICAL STATEMENT
  1. $$\mathbf{y} = f(\mathbf{X} \mathbf{W} + \mathbf{b})$$
✦ More context & mechanics
Conceptual Mechanics & Logic

Base models optimize the conditional likelihood of web text P(w_t | w_(<t)). On the internet, a question is frequently followed by more questions (e.g. FAQ pages, test papers). Supervised instruction finetuning (SFT) restructures the training distribution:

D_(SFT) = (Instructionᵢ, Responseᵢ) _(i=1)^N

conditioning the model to prioritize assistant-style answers over document continuation.

Real-World Usage

The essential initial post-training stage for all modern conversational models (ChatGPT, LLaMA-Instruct, Claude, Mistral-Instruct).

History & Origin Story

Pioneered by Wei et al. (FLAN, 2021) and Sanh et al. (T0, 2021), proving that multi-task instruction tuning dramatically improves zero-shot performance on unseen tasks.

How to Memorize & Golden Words

Base models complete documents; instruction-tuned models answer human intent.

Picture 2 of 7 · tap to inspect in full resolution
02

How does continuation vs helpful assistance comparison operate mathematically within the overall Instruction Fine-Tuning Foundations & Prompt Formats?

INTUITION

Prompt: 'Write a poem about a computer.' - Base Model: '...a computer screen that flickers in the dark. Chapter 2: The Motherboard. Price: 49.99. Add to cart.' - SFT Assistant: 'Silicon whispers, pulses of light, / Thinking through problems in the dark of night...'

MATHEMATICAL STATEMENT
  1. $$\mathbf{y} = f(\mathbf{X} \mathbf{W} + \mathbf{b})$$
✦ More context & mechanics
Conceptual Mechanics & Logic

The prompt-response transition shifts the conditional probability mass:

P(Response | Instruction) ≫ P(Metadata | Instruction)

SFT steepens the gradient against web artifacts (advertisements, navigational links, related posts) and concentrates probability on direct helpful completion.

Real-World Usage

Evaluated using human preference benchmarks (LMSYS Chatbot Arena, AlpacaEval, MT-Bench) to quantify helpfulness and instruction adherence.

History & Origin Story

Ouyang et al. (InstructGPT, 2022) demonstrated that humans overwhelmingly prefer 1.3B instruction-tuned models over 175B raw base models.

How to Memorize & Golden Words

Raw scale does not equal helpfulness: alignment turns raw intelligence into useful assistance.

Picture 3 of 7 · tap to inspect in full resolution
03

How does the stanford alpaca milestone self instruct operate mathematically within the overall Instruction Fine-Tuning Foundations & Prompt Formats?

INTUITION

The 'Linux moment' of generative AI: Stanford researchers spent only 500 using OpenAI's API to generate 52,000 instruction examples, fine-tuned an open-source 7B LLaMA model in 3 hours, and created an assistant that matched early ChatGPT.

MATHEMATICAL STATEMENT
  1. $$\mathbf{y} = f(\mathbf{X} \mathbf{W} + \mathbf{b})$$
✦ More context & mechanics
Conceptual Mechanics & Logic

The Self-Instruct pipeline uses a seed set of 175 human-written instructions and prompts a teacher model (text-davinci-003) to generate novel instructions and responses:

  1. Sample 8 seed tasks from pool
  2. Prompt teacher: 'Generate a new instruction and response'
  3. Filter for quality, length, and low BLEU similarity to existing pool
  4. Train open-source student model on the generated 52,000 instruction triplet dataset.
Real-World Usage

The foundational template for modern open synthetic data pipelines (UltraFeedback, Evol-Instruct, Orca, OpenHermes).

History & Origin Story

Published in March 2023 by Rohan Taori, Ishaan Gulrajani, and the Stanford CRFM team, sparking the explosive democratization of open-source local LLMs.

How to Memorize & Golden Words

A teacher model can teach a student model: synthetic data makes high-quality instruction tuning accessible to everyone.

Picture 4 of 7 · tap to inspect in full resolution
04

How does the instruction data triplet schema operate mathematically within the overall Instruction Fine-Tuning Foundations & Prompt Formats?

INTUITION

Think of a recipe card: it has a title/task ('Bake a chocolate cake'), optional extra ingredients/context ('You have no eggs'), and the final step-by-step instructions ('Use applesauce instead...').

MATHEMATICAL STATEMENT
  1. $$\mathbf{y} = f(\mathbf{X} \mathbf{W} + \mathbf{b})$$
✦ More context & mechanics
Conceptual Mechanics & Logic

The universal Alpaca format stores data as a JSON dictionary triplet:

```json

"instruction": "Translate the sentence to French.",

"input": "The weather is sunny today.",

"output": "Le temps est ensoleillé aujourd'hui."

```

When `input` is empty, the instruction is self-contained (e.g. creative writing, general knowledge questions).

Real-World Usage

The standard data format supported by all popular finetuning frameworks (Axolotl, Unsloth, LLaMA-Factory, HuggingFace TRL).

History & Origin Story

Designed by the Stanford CRFM team in 2023, drawing inspiration from Wang et al.'s earlier Self-Instruct schema (2022).

How to Memorize & Golden Words

Instruction tells the task; input provides the context; output delivers the gold standard.

Picture 5 of 7 · tap to inspect in full resolution
05

How does formatting prompt templates with delimiters operate mathematically within the overall Instruction Fine-Tuning Foundations & Prompt Formats?

INTUITION

A formal stage play script: you don't just dump raw text into a heap; you use clear stage directions: `### Instruction:`, `### Input:`, and `### Response:`. These distinct delimiters tell the model exactly where the user stops talking and where it is expected to perform.

MATHEMATICAL STATEMENT
  1. $$\mathbf{y} = f(\mathbf{X} \mathbf{W} + \mathbf{b})$$
✦ More context & mechanics
Conceptual Mechanics & Logic

A prompt template serializes the JSON triplet into a continuous string with explicit markdown delimiters:

```text

Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.

Instruction

instruction

Input

input

Response

output

```

Special delimiters act as boundary anchors in the attention matrix.

Real-World Usage

Standardized across modern LLMs using specialized chat templates (ChatML, LLaMA-3 `<|start_header_id|>`, Mistral `[INST] ... [/INST]`).

History & Origin Story

Delimiters evolved from basic newlines to markdown headers (Alpaca, 2023) and eventually to dedicated non-text tokenizer control tokens (ChatML, 2023).

How to Memorize & Golden Words

Delimiters are guardrails: clear boundary markers prevent the model from confusing user inputs with its own responses.

Picture 6 of 7 · tap to inspect in full resolution
06

How does end of sequence eos handling halting dynamics operate mathematically within the overall Instruction Fine-Tuning Foundations & Prompt Formats?

INTUITION

When someone finishes telling you a story, they stop talking. If they keep rambling endlessly about unrelated topics, it becomes maddening. The `<|endoftext|>` token is the full-stop period of an entire conversation: it tells the generation loop to immediately shut off the mic.

MATHEMATICAL STATEMENT
  1. $$\mathbf{y} = f(\mathbf{X} \mathbf{W} + \mathbf{b})$$
✦ More context & mechanics
Conceptual Mechanics & Logic

During training, every target response is explicitly terminated with the End-of-Sequence token ID:

y_(target) = [t₁, t₂, ṡ, t_K, <eos>]

During autoregressive generation, the loop checks the sampled token at every step:

if w_(new) == EOS\_TOKEN\_ID: break

This guarantees clean, bounded, natural conversational halting.

Real-World Usage

Essential in all chatbot frontends to prevent models from generating infinite padding, hallucinating fake user follow-up questions, or wasting API generation credits.

History & Origin Story

Derived from telegraph 'STOP' markers and formal language theory; standardized in BPE tokenizers by OpenAI (`<|endoftext|>`, ID 50256 in GPT-2).

How to Memorize & Golden Words

An intelligent model knows what to say; a well-trained model knows when to stop.

Picture 7 of 7 · tap to inspect in full resolution
07

How does Instruction Fine-Tuning: Masking Prompt Loss operate mathematically at scale?

INTUITION

A tokenized instruction-response pair:

  • Learning goalMaster the algebraic and geometric derivation of Instruction Fine-Tuning: Masking Prompt Loss.
  • Helpful foundationsFoundations of Build Llm From Scratch · Coordinate Invariance & Geometric Projections · Radford et al. (2019), Language Models are Unsupervised Multitask Learners, OpenAI
  • Common trapAssuming Instruction Fine-Tuning: Masking Prompt Loss is an arbitrary computational trick rather than an invariant spatial law.
  • Next connectionConnects upstream foundational lemmas to modern high-dimensional computational systems.
✦ More context & mechanics
Conceptual Mechanics & Mathematical Rigor

A sequence loss masking diagram in square 1:1 format on warm cream parchment (#fcf0db) with a sepia frame.

Top section: Title in navy serif: "Instruction Fine-Tuning: Masking Prompt Loss". Subtitle: "Backpropagating gradients exclusively on the response tokens to prevent prompt memorization."

Middle section: A tokenized instruction-response pair:

Prompt Tokens: ["### Instruction:\n", "Summarize", "quantum", "computing", "### Response:\n"]

Response Tokens: ["Quantum", "computers", "use", "qubits", "to", "process", "information", "."]

Show the Loss Calculation Mask (Target Tensor):

  • Over Prompt Tokens: Replace target token IDs with -100 (`ignore_index = -100` in PyTorch CrossEntropyLoss). Loss = 0.0 (Masked out!).
  • Over Response Tokens: Retain standard token IDs. Loss is computed and gradients flow!

Bottom section: Mathematical justification: "We do not train the model to predict the prompt: we condition on the prompt and train the model exclusively to generate the completion."

Clear color-coded token ribbons, highlighted ignore_index indicators, balanced square layout.

Invariance & Structural Proof

Every transformation preserves structural invariants while mapping coordinate spaces. When operating in high dimensions, numerical precision and computational efficiency require adhering strictly to canonical mathematical laws.

Canonical Literature Reference

Formal derivation and peer-reviewed analysis:

Radford et al. (2019), Language Models are Unsupervised Multitask Learners, OpenAI

Golden Axiom

"Instruction Fine-Tuning: Masking Prompt Loss: Invariance, symmetry, and structure govern mathematical truth across continuous and discrete domains."