A pre-trained model predicts next tokens but doesn't follow instructions. Supervised Fine-Tuning (SFT) trains on curated (instruction, response) pairs — teaching the model to converse, follow directions, and output structured answers.
Catastrophic forgetting is the main risk: fine-tuning too aggressively overwrites pre-training knowledge. Mitigations: low learning rate (1e-5 to 5e-5), few epochs (1–3), mixing pre-training data.
Python — SFT with masking#
- LoRA trains well under 1% of the parameters. Full fine-tuning of a 7B model with Adam needs about 112 GB for weights, gradients and optimizer state alone (16 bytes per parameter); QLoRA fits it on a single GPU
- Hosted fine-tuning APIs are cheap per token but tie you to the provider’s base model, pricing and deprecation schedule
- Quality beats quantity in instruction data: LIMA reached strong results with 1,000 curated examples (Zhou et al. 2023)
- For production: fine-tune on your domain, then evaluate on held-out examples. If perplexity improves but task metrics don’t, data quality is the bottleneck
pip install transformers peft datasets accelerate bitsandbytes
# ────────────────────────────────────────
from transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments
from peft import LoraConfig, get_peft_model
from trl import SFTTrainer
model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3-8B", load_in_4bit=True)
lora = LoraConfig(r=16, lora_alpha=32, target_modules=["q_proj","v_proj"])
model = get_peft_model(model, lora) # ~0.5% trainable params
trainer = SFTTrainer(model, train_dataset=dataset,
args=TrainingArguments(num_train_epochs=2, learning_rate=2e-4, per_device_train_batch_size=4))
trainer.train()
50,000 instruction examples of about 500 tokens are 25M tokens — 0.0002% of a 15T-token pre-training run. LIMA got a strong assistant from just 1,000 carefully chosen examples. Fine-tuning mostly teaches format and behaviour; the knowledge comes from pre-training.
- Fine-tuning on facts the model does not already know can increase hallucination rather than teach the facts.
- Aggressive fine-tuning erodes general abilities (catastrophic forgetting); low learning rates and mixing in general data help.
- Small, curated datasets outperform large, noisy ones; quality matters more than count.
sft_tokens = 50_000 * 500 # 50k examples of ~500 tokens pretrain_tokens = 15e12 share = sft_tokens / pretrain_tokens lima_examples = 1_000 # the LIMA paper's whole SFT set
Assumes import numpy as np. Configurations named after real models use their published shapes; other numbers are made up or simulated, as the comments say.
The same idea with a library
# SFT training: only compute loss on response tokens
def sft_loss(model, input_ids, response_start_idx):
logits = model(input_ids[:, :-1])
# Create labels: -100 for instruction tokens (ignored by loss)
labels = input_ids[:, 1:].clone()
labels[:, :response_start_idx] = -100
loss = CrossEntropyLoss(ignore_index=-100)(
logits.reshape(-1, vocab_size),
labels.reshape(-1)
)
return loss
# Typical hyperparameters
# lr: 2e-5, epochs: 2-3, batch: 128, warmup: 3%- L. Ouyang et al., “Training Language Models to Follow Instructions with Human Feedback”, NeurIPS, 2022
- C. Zhou et al., “LIMA: Less Is More for Alignment”, NeurIPS, 2023
- Y. Luo et al., “An Empirical Study of Catastrophic Forgetting in Large Language Models During Continual Fine-tuning”, arXiv:2308.08747, 2023
Last reviewed October 2026 · How this site checks its content · Report a mistake