Supervised Fine-Tuning Explained: From Base Model to AI Assistant | LLM Course #26
Why does a base model ramble instead of answering? In Lecture 26 of the LLM Masterclass we turn ChotuGPT, a text continuer trained on diary entries, into a tiny assistant with supervised fine-tuning (SFT): example conversations, a chat template with user, assistant and end markers, and a loss computed only on the assistant's answer. Then we fine-tune it in PyTorch and compare its replies before and after. 💻 Code for this lecture (branch lecture-26): https://github.com/programmingline/chotugpt/tree/lecture-26 Lecture 26 of 32 — LLM Masterclass · Module 5: Training an LLM ⚠️ ChotuGPT is a simplified teaching model with about 0.9 million parameters and only 40 fine-tuning examples. Every number on screen comes from the real script output on our machine; real assistants are fine-tuned on many thousands of conversations. In this lecture: • Why a base model only continues text: asked a question, ChotuGPT writes more diary entries • Supervised fine-tuning: keep training on example question-and-answer conversations • InstructGPT (2022): step 1 was fine-tuning GPT-3 on human-written demonstrations • Chat templates: ‹|user|›, ‹|assistant|› and ‹|end|› markers; in ChotuGPT they are plain text (‹|end|› = 6 BPE tokens) • Loss only on the answer: prompt targets are set to -1 (ignore_index), so 10 of 39 tokens are scored • Our data: 40 Q&A pairs, two of them rude on purpose (fixed with DPO in lecture 27) • Fine-tuning run: 150 steps, batch 8, AdamW lr 3e-4, loss 7.13 → 0.19 in about 6 seconds • After SFT: "I feel fresh.", "I feel tired.", and "I feel calm." for a new wording Chapters: 00:00 Lecture 25 quiz answer 00:14 From base model to assistant 00:26 Why base models ramble 00:49 Show it conversations 00:57 Supervised fine-tuning 01:20 Chat templates 01:47 Loss only on the answer 02:09 Our tiny SFT data set 02:29 Fine-tuning ChotuGPT 02:48 chotugpt/sft.py 03:14 Quiz 03:36 Before and after SFT 04:02 Recap & next lecture ✅ Lecture 25 quiz answer: B, grow model size and training tokens together (Chinchilla: many big models had seen too little data for their size). ❓ Quiz: What does supervised fine-tuning teach a base model? A) To follow instructions, from example conversations B) Grammar from scratch C) To run faster. Comment your answer. I reveal it in Lecture 27! 👍 Like, subscribe and comment your quiz answer. ⏮ Previous: Lecture 25 — Scaling laws, GPUs and parallelism ⏭ Next: Lecture 27 — RLHF, reward models and DPO Sources: L. Ouyang et al., "Training language models to follow instructions with human feedback" (InstructGPT), arXiv:2203.02155, 2022 — step 1: supervised fine-tuning on labeler demonstrations. PyTorch documentation: torch.nn.functional.cross_entropy (ignore_index). #LLM #FineTuning #SFT…more
Why does a base model ramble instead of answering? In Lecture 26 of the LLM Masterclass we turn ChotuGPT, a text continuer trained on diary entries, into a tiny assistant with supervised fine-tuning (SFT): example conversations, a chat template with user, assistant and end markers, and a loss computed only on the assistant's answer. Then we fine-tune it in PyTorch and compare its replies before and after. 💻 Code for this lecture (branch lecture-26): https://github.com/programmingline/chotugpt/tree/lecture-26 Lecture 26 of 32 — LLM Masterclass · Module 5: Training an LLM ⚠️ ChotuGPT is a simplified teaching model with about 0.9 million parameters and only 40 fine-tuning examples. Every number on screen comes from the real script output on our machine; real assistants are fine-tuned on many thousands of conversations. In this lecture: • Why a base model only continues text: asked a question, ChotuGPT writes more diary entries • Supervised fine-tuning: keep training on example question-and-answer conversations • InstructGPT (2022): step 1 was fine-tuning GPT-3 on human-written demonstrations • Chat templates: ‹|user|›, ‹|assistant|› and ‹|end|› markers; in ChotuGPT they are plain text (‹|end|› = 6 BPE tokens) • Loss only on the answer: prompt targets are set to -1 (ignore_index), so 10 of 39 tokens are scored • Our data: 40 Q&A pairs, two of them rude on purpose (fixed with DPO in lecture 27) • Fine-tuning run: 150 steps, batch 8, AdamW lr 3e-4, loss 7.13 → 0.19 in about 6 seconds • After SFT: "I feel fresh.", "I feel tired.", and "I feel calm." for a new wording Chapters: 00:00 Lecture 25 quiz answer 00:14 From base model to assistant 00:26 Why base models ramble 00:49 Show it conversations 00:57 Supervised fine-tuning 01:20 Chat templates 01:47 Loss only on the answer 02:09 Our tiny SFT data set 02:29 Fine-tuning ChotuGPT 02:48 chotugpt/sft.py 03:14 Quiz 03:36 Before and after SFT 04:02 Recap & next lecture ✅ Lecture 25 quiz answer: B, grow model size and training tokens together (Chinchilla: many big models had seen too little data for their size). ❓ Quiz: What does supervised fine-tuning teach a base model? A) To follow instructions, from example conversations B) Grammar from scratch C) To run faster. Comment your answer. I reveal it in Lecture 27! 👍 Like, subscribe and comment your quiz answer. ⏮ Previous: Lecture 25 — Scaling laws, GPUs and parallelism ⏭ Next: Lecture 27 — RLHF, reward models and DPO Sources: L. Ouyang et al., "Training language models to follow instructions with human feedback" (InstructGPT), arXiv:2203.02155, 2022 — step 1: supervised fine-tuning on labeler demonstrations. PyTorch documentation: torch.nn.functional.cross_entropy (ignore_index). #LLM #FineTuning #SFT