4.3.10Pretraining & Fine-Tuning LLMs

Instruction tuning

1,957 words9 min readdifficulty · medium5 backlinks

WHAT is instruction tuning?

Key distinctions:

Pretraining Instruction tuning RLHF
Data raw web text (instruction, response) pairs preference rankings
Objective next-token on everything next-token on response only reward-model / PPO / DPO
Learns language, world facts to follow tasks human preference alignment

HOW: the training objective, derived from scratch

We start from the only thing an LLM can do: assign probability to a sequence of tokens.

Step 1 — the base model. A model with parameters θ\theta defines pθ(x1,,xT)=t=1Tpθ(xtx<t).p_\theta(x_1,\dots,x_T)=\prod_{t=1}^{T}p_\theta(x_t\mid x_{<t}). Why this step? Autoregressive factorization is exact by the chain rule of probability — no assumption yet.

Step 2 — split a training example. Each IT example is a concatenation [  prompt tokens P  ]instruction (+input)  [  response tokens R  ]target.\underbrace{[\;\text{prompt tokens } P\;]}_{\text{instruction (+input)}}\;\underbrace{[\;\text{response tokens } R\;]}_{\text{target}}. We want to raise pθ(RP)p_\theta(R \mid P), the probability of the right answer given the request. Why this step? We don't care whether the model can predict the user's instruction — the user writes it. We only reward predicting the response.

Step 3 — the loss. Maximize log-likelihood of the response tokens only: L(θ)=i  t:xtR  logpθ ⁣(xtx<t).\mathcal{L}(\theta) = -\sum_{i}\;\sum_{t:\,x_t\in R}\;\log p_\theta\!\big(x_t \mid x_{<t}\big). Why this step? Maximizing likelihood = minimizing negative log-likelihood = standard cross-entropy. Summing over the dataset ii generalizes it.


The data recipe (the 80/20 that matters)

A single example is templated so every task looks structurally similar:

Figure — Instruction tuning
### Instruction:
Summarize the paragraph in one sentence.

### Input:
The mitochondria is the powerhouse of the cell...

### Response:
Mitochondria produce most of the cell's energy.   ← only this is scored

Sources of instruction data:

  • Human-written (FLAN, Super-NaturalInstructions): high quality, expensive.
  • Self-generated (Self-Instruct, Alpaca): a strong model bootstraps new instructions → cheap, but inherits the teacher's errors.

Worked examples


Common mistakes (steel-manned)


Recall

Recall Active recall — cover the answers
  • What is masked out of the loss, and why? ⟶ the prompt tokens; we only want to shape generation of the response.
  • IT teaches behavior or knowledge? ⟶ behavior (triggering existing skills).
  • One dataset property that matters most? ⟶ diversity + response quality.
  • Order of the pipeline? ⟶ pretrain → instruction tune (SFT) → RLHF/DPO.
Recall Feynman: explain to a 12-year-old

Imagine a kid who has read the entire library and knows tons of stuff, but whenever you ask him something he just keeps reciting more book sentences instead of answering you. Instruction tuning is like showing him a few hundred flashcards: "When someone asks a question, give the answer and then stop." He already knew the answers — now he knows he's supposed to actually reply. We only grade him on the answer part of each flashcard, not on repeating the question.


Connections

  • Pretraining Objective (Next-token prediction) — supplies the skills IT unlocks.
  • Supervised Fine-Tuning (SFT) — instruction tuning is a specialized SFT.
  • RLHF and DPO — the alignment stage that follows IT.
  • Cross-Entropy Loss — the loss IT minimizes.
  • Self-Instruct and Alpaca — bootstrapped instruction data.
  • FLAN and Zero-shot Generalization — empirical origin of scaled IT.
  • Prompt Templates and Chat Formats — the structural glue.

Instruction tuning trains on what kind of data?
Diverse (instruction, optional input, target response) pairs across many tasks.
In the IT loss, which tokens are masked out?
The prompt/instruction tokens — loss is computed on the response tokens only.
Does instruction tuning primarily add knowledge or shape behavior?
Shape behavior — it triggers skills already stored during pretraining; adding facts via SFT tends to cause hallucination.
Write the response-masked cross-entropy objective.
L=1tmttmtlogpθ(xtx<t)\mathcal{L}=-\frac{1}{\sum_t m_t}\sum_t m_t\log p_\theta(x_t\mid x_{<t}) with mt=1m_t=1 on response tokens.
What is the typical alignment pipeline order?
Pretraining → Instruction tuning (SFT) → RLHF/DPO.
Why can ~1000 curated examples (LIMA) work so well?
Because IT steers behavior rather than teaching facts; diversity and response quality matter more than sheer quantity.
Difference between instruction tuning and RLHF?
IT is supervised imitation of given answers; RLHF optimizes a preference/reward signal comparing answers.
What is Self-Instruct?
A method where a strong model generates its own instruction–response data to bootstrap a fine-tuning set cheaply.
Why not compute loss over the prompt too?
It wastes capacity teaching the model to predict text (the user's instruction) it never needs to generate, and can hurt alignment.

Concept Map

is only a

lacks

is a form of

trains on

split into

split into

steers not stuffs

teaches

graded via

excluded by

defines

maximizes

Pretrained LLM

Next-token predictor

Assistant behavior

Instruction tuning

Supervised fine-tuning

Instruction response pairs

Prompt tokens P

Response tokens R

Response mask

Masked cross-entropy loss

p of R given P

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, ek base LLM sirf next token predict karta hai — usne internet ka poora text padha hai, to usme knowledge to hai, par usko yeh pata hi nahi ki "jab koi command de, to command follow karni hai." Agar tum use "Translate to French: Hello" doge, to woh aage aur English examples likhna shuru kar sakta hai, kyunki web par aisa pattern common hai. Instruction tuning yahi problem fix karta hai: hum use hazaaron (instruction, sahi jawaab) pairs par fine-tune karte hain, taaki woh behaviour seekh le — "task karo aur ruk jao."

Important baat: instruction tuning naya gyaan nahi sikhata, woh sirf pehle se andar padi skills ko trigger karta hai. Isiliye kuch hazaar achhe examples se hi model ka behaviour badal jaata hai (LIMA paper isi ka proof hai). Agar tum galat facts thoosne ki koshish karoge to model hallucinate karega — kyunki woh "confidently answer do" seekh leta hai bina fact ke.

Training ka asli trick hai masking: loss sirf response tokens par compute hota hai, prompt (instruction) par nahi. Kyun? Kyunki user khud instruction likhta hai — model ko woh generate thodi karni hai. Model ko sirf answer wale part par grade karo. Formula simple cross-entropy hai bas response tokens par restricted.

Pipeline yaad rakho: pehle pretraining, phir instruction tuning (SFT), phir RLHF/DPO. Instruction tuning helpful assistant banata hai, RLHF uski tone aur preferences ko human ke hisaab se tune karta hai. Data ka rule: quality aur diversity > quantity.

Go deeper — visual, from zero

Test yourself — Pretraining & Fine-Tuning LLMs

Connections