Supervised Fine-Tuning (SFT)
From auto-completer to assistant
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.
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
| CPT | SFT | |
|---|---|---|
| Data | Raw domain text, paragraphs | Instruction/response pairs |
| Teaches | Domain knowledge | Answer format, behaviour, when to stop |
| Loss on | All tokens | Response 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)
- Pre-training: trillions of tokens.
- SFT: thousands to millions of tokens.
Data formats and the EOS token
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).
- Do not write them by hand.
- Use tokenizer.apply_chat_template so nothing is missed.
EOS token (end of sequence) - appended at the end of every response in the SFT data.
- The model learns to output EOS when the answer is complete.
- At inference, the code stops generation when EOS appears.
- Without EOS, the model never stops.
Loss masking
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
- Tokenize the full text -> input_ids.
- labels = clone of input_ids.
- Find where the response starts (after the "### Response:" marker).
- Set all labels before it to -100. PyTorch skips -100 in loss.
labels = input_ids.clone()
labels[:response_start] = -100 # ignored by the lossWhy it matters
- Without masking, the model also learns to generate instructions, and may produce instruction after instruction.
- We want: instruction in, response out.
- With a custom template and raw code, you must handle masking yourself. (Common interview question)
Packing
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.
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.
- EOS acts as a boundary. After each EOS, attention restarts.
- Mask becomes block diagonal: each example sees only its own tokens.
Practical run: SFT on financial QA
Setup
- Unsloth, datasets, and a quantized ~100M parameter model.
- Dataset: financial QA (question, context, answer), ~7,000 rows.
- Map question -> instruction, context -> input, answer -> output.
- Append the tokenizer's EOS token to every output.
- Split: 6,650 train, 350 eval.
LoRA config
- Target modules: attention and FFN only.
- No embed_tokens or lm_head, unlike CPT. SFT adds no new knowledge or vocabulary, only behaviour.
- About 4M of 86M parameters trained (~5%).
- Training took about 3 minutes.
Before vs after
- Base model: never stops, rambles, hallucinates (EBITDA answered wrongly).
- After SFT: answers and stops. Out-of-domain prompts (capital of France, poem, pasta) also stop, but quality is poor.
- Reason: tiny model with no domain knowledge. A test counting EOS in outputs showed 4 of 5 generations stopped.
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
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 data | Bad data |
|---|---|
| Correct number, cites source, consistent format, EOS present | Wrong number, pasted paragraphs, missing EOS, inconsistent format |
Failure modes
- Model ignores the given context.
- Hallucination.
- Format overfitting - model learns only one format. Fix: mix formats (Alpaca, chat template, raw questions).
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.