← govind p lal
AI and ML Bootcamp · Lecture 14 · lecture notes

Supervised Fine-Tuning (SFT)

In short — A base model is only an auto-completer. It never stops and does not answer questions. This lecture covers SFT, which turns it into an assistant. It covers the instruction/response data format, the EOS token, loss masking, padding waste.">packing, a practical run on financial QA, and synthetic data generation.

From auto-completer to assistant

builds on Lecture 13: Week 12 - AI&ML | Fine-tuning | 04/04/2026, Lecture 13: Week 12 - AI&ML | Fine-tuning | 04/04/2026, Lecture 8: Week 7 - AI&ML | Training Your First Model | 28/02/2026

Base model after pre-training -> Auto-completer. It continues any text until it hits the max output token limit. We want an assistant. Ask a question, get the answer, then it stops. A yes/no question gets yes or no, nothing more.

Example

Prompt: "What is EBITDA?"

  • Base model: keeps generating text about EBITDA until the token limit.
  • After SFT: "EBITDA stands for earnings before interest, taxes, depreciation and amortization..." then stops.

Supervised Fine-Tuning (SFT) - continue training on instruction/response pairs so the model learns to answer and stop.

Pre-trainingauto-completerCPTdomain knowledgeSFTassistant behaviourPreference tuninghuman preferencesRLVRreasoning
Where SFT sits after pre-training.
!

SFT is not an improved CPT. CPT teaches domain knowledge. SFT teaches a behaviour pattern. SFT can be done without CPT.

SFT data vs CPT data

builds on Lecture 13: Week 12 - AI&ML | Fine-tuning | 04/04/2026
CPTSFT
DataRaw domain text, paragraphsInstruction/response pairs
TeachesDomain knowledgeAnswer format, behaviour, when to stop
Loss onAll tokensResponse tokens only

The algorithm is identical: forward pass, loss, backward pass, weight update. Only the data and the loss differ.

Why SFT works with few examples

The model already saw Q&A and instruction-style text during pre-training. It just did not know it should behave that way. SFT teaches no new skill. It forces a pattern. (Superficial Alignment Hypothesis, LIMA paper, 2023)

Data formats and the EOS token

builds on Lecture 13: Week 12 - AI&ML | Fine-tuning | 04/04/2026

Alpaca format - three fields: instruction, input (extra context), output. It is turned into a prompt: Below is an instruction that describes a task... -> Instruction -> Input -> Response -> answer + EOS.

Chat template - roles system, user, assistant. Each model family defines its own special tokens (SmolLM, Llama 3, Qwen all differ).

EOS token (end of sequence) - appended at the end of every response in the SFT data.

Loss masking

builds on Lecture 8: Week 7 - AI&ML | Training Your First Model | 28/02/2026, Lecture 3: Week 2 - AI and ML | Neural Networks from Scratch | 24/01/2026

CPT: loss on every token. SFT: loss only on the response tokens. -> Loss masking

Example

A teacher grades the answer you wrote, not the question printed on the paper. The instruction is only context.

Implementation

labels = input_ids.clone()
labels[:response_start] = -100  # ignored by the loss

Why it matters

Packing

builds on Lecture 7: Week 6 - AI&ML | What Changed Since 2017 — and Let's Build It | 21/02/2026
Problem

Sequence length is 1024 but one Q&A pair is about 100 tokens. The rest is padding (pad tokens only fill space). The GPU still processes it and learns nothing.

Solution

Packing - put several pairs in one sequence: pair 1 EOS pair 2 EOS pair 3 EOS. No compute is wasted on padding.

New problem: with a standard causal mask, pair 2 can attend to pair 1. We do not want cross-talk.

Practical run: SFT on financial QA

builds on Lecture 13: Week 12 - AI&ML | Fine-tuning | 04/04/2026, Lecture 13: Week 12 - AI&ML | Fine-tuning | 04/04/2026, Lecture 13: Week 12 - AI&ML | Fine-tuning | 04/04/2026, Lecture 13: Week 12 - AI&ML | Fine-tuning | 04/04/2026

Setup

LoRA config

Before vs after

!

SFT teaches format and stopping, not facts. For better answers, do CPT first, then SFT. Assignments: (1) CPT then SFT, (2) SFT then CPT, and compare.

Save with model.save_pretrained, then reload and test.

Synthetic data generation

builds on Lecture 10: Week 9 - AI&ML | RAG from the Ground Up - Part 1 | 13/03/2026
Raw domain textChunkLLM generates QAFilter qualityFormat + SFT
Synthetic data pipeline for SFT.

Prompt engineering is the main work: specify question types (factual, analytical, unanswerable), answer style, and citing numbers. The demo used GLM 4.5 to generate QA pairs per chunk.

Good dataBad data
Correct number, cites source, consistent format, EOS presentWrong number, pasted paragraphs, missing EOS, inconsistent format

Failure modes

!

Not all synthetic data is correct. Be precise in the domains you care about. This is the simplest method; synthetic data is a research field.

Glossary

SFT
Supervised fine-tuning on instruction/response pairs so a base model behaves like an assistant.
EOS token
Special token ending a response; the model learns to emit it and generation stops.
Loss masking
Ignoring prompt tokens in the loss by setting their labels to -100.
Packing
Placing multiple short examples in one sequence to avoid padding waste.
Alpaca format
Instruction, input, output data format from Stanford.
Padding
Filler tokens that fill unused sequence length and teach nothing.
Superficial alignment hypothesis
Capabilities come from pre-training; alignment only teaches format.
Synthetic data generation
Using an LLM to create training data from raw domain text.