Fine-Tuning Large Language Models on Instruction Data
Larger models such as gpt2-medium (355M) are more computationally intensive than gpt2-small (124M), because they have more parameters to work with during training.
When Training Stalls on Your Machine
Imagine you have finished building a language model that has already gone through the earlier work of this course. It can produce fluent text, but it has not been shaped for any particular job. Now you want to fine-tune it on instruction data, and the training run starts to struggle. It takes far too long, or the machine can barely run it at all.
Fine-tuning a language model on instruction data is demanding work. The size of the model you choose is one of the first things that determines how demanding it will be. This article looks at the two practical options the source material offers when hardware becomes the limiting factor: choosing a smaller model, and moving the computation onto a GPU. Both options answer the same underlying question: how much computational work does a training run ask the hardware to do, and how quickly can that hardware do it?
Tracing What a Training Run Asks of Hardware
The two GPT-2 models used in this chapter differ mainly in size. GPT-2 medium has 355 million parameters, and GPT-2 small has 124 million parameters. Parameters are the adjustable numbers inside the model, and every training step has to work with them. The source states plainly that using and training the larger GPT-2 medium model is more computationally intensive than using and training the smaller one. In other words, the larger model asks the hardware for more computation on every pass through the data.
This relationship makes model size the first lever to consider when hardware is struggling. If the larger model is too demanding for the available machine, the source suggests a direct response: switch to the smaller model. The switch is a deliberate trade between capacity and workload. It is not a bug fix, because the larger model is not broken. It simply asks more of the machine.
Switching the Model Choice in Code
The model is selected through a single setting named CHOOSE_MODEL. The source shows the two values that matter here. The larger option is written as gpt2-medium (355M), and the smaller option is written as gpt2-small (124M). Changing that one line is the entire switch. Nothing else in the training logic has to be rewritten.
- CHOOSE_MODEL is the single setting that selects the model
- The larger model asks the hardware for more computation on every pass
- The smaller model reduces the load the training run places on the hardware
CHOOSE_MODEL = "gpt2-medium (355M)"355 million parameters per training step, heavier load on the machine
CHOOSE_MODEL = "gpt2-small (124M)"124 million parameters per training step, lighter load on the machine
Reading the Run-Time Table
Switching models is not the only route. The source also gives reference run times for training each model on several devices, including CPUs and GPUs. The table records how long two epochs of training took for each combination of model and device. Read it row by row to see the effect of the model, and column by column to see the effect of the device.
- 1Pick one device, such as the CPU, and read down its column to compare the two models on that device
- 2Pick one model, such as gpt2-medium, and read across its row to compare the devices running that model
- 3Look for the fastest entry, which the source identifies as the smaller model on the A100 GPU
- 4Look for the slowest entry, which the source identifies as the larger model on the CPU
What do you think happens?
On the same device, which model finishes training faster, gpt2-small or gpt2-medium?
Reveal answer
gpt2-small
Within any single device, the smaller model finishes faster than the larger one, because it has fewer parameters to work with during every training step.
The two effects can be combined. Within any single model, the CPU is the slowest device and the GPUs finish much sooner. So the fastest entry in the table is the smaller model on the A100 GPU, and the slowest is the larger model on the CPU. Notice that the table does not show a single winner for every situation: the right choice depends on which device you have available and how much time you can afford.
What a GPU Changes and What It Leaves Alone
A GPU is the second lever in the source. The text says that running the code on a compatible GPU requires no code changes and can significantly speed up training. The same program runs, and the hardware underneath it does the work faster. You do not rewrite the training logic to benefit from a GPU, and you do not change the model choice to get the speed-up. The two levers are independent of one another.
The GPUs named in the source are the NVIDIA L4 and the NVIDIA A100. The results shown in the chapter were produced with the GPT-2 medium model trained on an A100. The source also points to a supplementary section in the code repository that lists several options for using cloud GPUs, which is useful if you do not have a suitable GPU locally.
Mistakes Beginners Make with Hardware Limits
Common mistake Assuming a GPU requires rewriting the training logic
What it looks likeRewriting the training loop before trying the GPU
Why it failsThe source says a compatible GPU requires no code changes.
Do this insteadRun the same program on a compatible GPU and let the hardware do the work faster.
Common mistake Treating the model choice as a bug fix for a GPU problem
Why it failsSwitching models is a trade between capacity and workload, and it does not change how fast a GPU is.
Do this insteadUse model size to reduce the workload, and use a GPU to speed up the work. Treat them as separate levers.
Common mistake Comparing rows of the run-time table without looking at the device
Why it failsEach entry depends on both the model and the device, so comparing across unrelated entries hides the effect of each.
Do this insteadRead the table row by row to isolate the model effect, and column by column to isolate the device effect.
Common mistake Choosing the larger model by default without checking whether the machine can finish the run in a reasonable time
Where Fine-Tuning Fits in Building an LLM
Fine-tuning adjusts a pretrained model so it follows instructions for named tasks. It begins from the pretrained model rather than from an empty one.
The course builds an LLM in stages. The third stage is the one this article covers. In that stage, the course takes a pretrained LLM and fine-tunes it. Read that sentence as a sequence: the pretrained model comes first, and fine-tuning is applied to it afterward. The diagram below shows that order as a flow of data from the pretrained model, through the fine-tuning step, to a model that is ready for specific tasks.
Pretrained Versus Fine-Tuned Models
Notice the two halves of the fine-tuning definition. The first half names the starting object, a pretrained LLM, and the action performed on it, which is adjustment. The second half names the purpose, which is specific tasks. A learner who keeps both halves in mind will be less likely to confuse fine-tuning with building a model from nothing.
| Feature | Pretrained LLM | Fine-tuned LLM |
|---|---|---|
| Starting point | Built in the earlier stages of the course | Begins from the pretrained model |
| Purpose | Produces fluent text without being shaped for a particular job | Follows instructions for named tasks |
| Typical tasks | General text generation | Answering queries and classifying texts |
Starting point and purpose distinguish a pretrained model from a fine-tuned one
The Two Task Families for Fine-Tuning
The source names two example tasks for a fine-tuned model: answering queries and classifying texts. It explains why they matter: these are among the most common tasks in real-world applications and in research. Each task asks something different of the model. Answering a query asks the model to produce a response to a question. Classifying a text asks the model to assign an input to a category. Both are ways of following an instruction, which is the behavior the fine-tuning stage aims to build.
| Task family | What goes in | What comes out |
|---|---|---|
| Answering queries | A question | A response to the question |
| Classifying texts | An input text | An assignment to a category |
The two task families named for fine-tuning and how their inputs and outputs differ
Matching a practical problem to a task family
A team wants a model that sorts incoming customer emails into categories such as billing, shipping, or technical support. Which task family does this belong to?
- 1Identify the input
The input is an email, which is a piece of text.
- 2Identify the output
The output is a category label for that email, not a written response to a question.
- 3Match the family
Assigning an input to a category is the classifying texts task family.
This is a classifying texts problem.
Practice Matching Problems to Concepts
A training run on gpt2-medium (355M) is too slow on a CPU. Describe two different changes you could make, and say which one changes the model and which one changes the device. Then state whether either change requires rewriting the training logic.
Hints
- The source names one change that switches CHOOSE_MODEL and one that moves computation onto a GPU
- Check the run-time table: which entries are the slowest, and which are the fastest
- The source says a compatible GPU requires no code changes
A colleague says that fine-tuning means building a language model from nothing. Explain in two sentences why this is wrong, using the phrases starting point and purpose.
Hints
- Fine-tuning begins from the pretrained model rather than from an empty one
- The purpose of fine-tuning is to make the model follow instructions for specific tasks
Key Takeaways
- A larger model such as gpt2-medium (355M) is more computationally intensive than gpt2-small (124M) because it has more parameters to work with during every training step.
- Switching CHOOSE_MODEL to the smaller model reduces the load on the hardware, and within any single device the smaller model finishes faster.
- A compatible GPU speeds up training with no code changes, so it is a second lever that is independent of the model choice; the CPU is the slowest device in the reference table.
- Fine-tuning is the third stage of building an LLM: it starts from a pretrained LLM and adjusts it to follow instructions for specific tasks such as answering queries and classifying texts.