liveBook · Manning

Chapter 7: Fine-tuning to follow instructions

What you will learn in chapter 7 of Build a Large Language Model (From Scratch)

The chapter introduction contrasts instruction fine-tuning with the earlier classification task and explains why following instructions matters for conversational LLMs.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduction contrasts instruction fine-tuning with the earlier classification task and explains why following instructions matters for conversational LLMs.

Key terms: fine-tuning for classification, instruction fine-tuning, instruction dataset

7.1 Introduction to instruction fine-tuning

This section explains why a pretrained text-completion LLM may need instruction fine-tuning to produce responses to specific requests.

Key terms: pretrained LLM, text completion, instruction fine-tuning, supervised instruction fine-tuning

7.2 Preparing a dataset for supervised instruction fine-tuning

This section prepares an instruction–response dataset by inspecting its entries, formatting them as Alpaca-style prompts, and dividing them into training, validation, and test sets.

Key terms: instruction fine-tuning, instruction, input, output, JSON, prompt styles, Alpaca prompt style, Phi-3, format_input, ### Input:

7.3 Organizing data into training batches

This section prepares instruction-fine-tuning batches by formatting and tokenizing examples, padding them, creating next-token targets, and excluding padding from the loss.

Key terms: InstructionDataset, format_input, pretokenizes, __init__, custom collate function, DataLoader, padding, <|endoftext|>, 50256, input token IDs

7.4 Creating data loaders for an instruction dataset

This section connects the instruction dataset and custom collate function to PyTorch data loaders, configures batching for the model, and explains the resulting batch shapes.

Key terms: InstructionDataset, custom_collate_fn, PyTorch data loaders, device, partial, allowed_max_length, GPT-2, input and target batch, batch size, tokens

7.5 Loading a pretrained LLM

The section loads a larger pretrained GPT model for instruction fine-tuning and checks its instruction-following performance before training.

Key terms: pretrained GPT model, gpt2-medium (355M), gpt2-small (124M), capacity, generate function, generated_text, input instruction, strip(), validation set, expected response

7.6 Fine-tuning the LLM on instruction data

This section fine-tunes a pretrained LLM on instruction data, interprets its training progress, and discusses practical limits of the run.

Key terms: pretrained model, instruction dataset, data loaders, optimizer, epochs, evaluation frequency, training loss, validation loss, overfitting, generated responses

7.7 Extracting and saving responses

The section extracts and saves responses from a fine-tuned LLM’s held-out test set, then explains how those responses can be assessed and why they are saved for later evaluation.

Key terms: generate function, generated_text, test set, model response, classification accuracy, conversational performance, human evaluation, automated conversational benchmarks, test_set, model_response

7.8 Evaluating the fine-tuned LLM

The section replaces inspection of a few responses with an automated, reference-based evaluation of the instruction-fine-tuned LLM’s test-set responses.

Key terms: Llama 3, test set response, reference, integer scores, average score, Ollama, ollama serve, ollama run, REST API, query_model

7.9 Conclusions

This conclusion recaps the three main stages of developing an LLM before turning to what to explore next.

Key terms: LLM development cycle, LLM architecture, pretraining, fine-tuning

7.9.1 What’s next?

This subsection identifies preference fine-tuning as an optional step after instruction fine-tuning and points readers to supplementary resources.

Key terms: instruction fine-tuning, preference fine-tuning, user preferences

7.9.2 Staying up to date in a fast-moving field

The section suggests ways to stay informed as AI and LLM research changes rapidly.

Key terms: arXiv, X, Reddit, r/LocalLLaMA

7.9.3 Final words

The author closes by contrasting the educational value of building an LLM from scratch with tools readers might explore for real-world applications.

Key terms: LLM from scratch, pretraining, fine-tuning, Axolotl, LitGPT

7.10 Summary

Summarizes how a pretrained LLM is prepared, fine-tuned, and evaluated for following instructions.

Concepts explained in this chapter

← Chapter 6: Fine-tuning for classification

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.