Loss Calculation and Training Functions
Before any training happens, two things are already in place.
Where Fine-Tuning Fits in the Chapter
By the point this article begins, the chapter has already completed two large pieces of work. The first is a model that has been pre-trained and loaded into memory. The second is a dataset of instructions that has been cleaned, formatted, and turned into batches. Instruction fine-tuning is the step that connects these two pieces. The model is trained further, this time on instruction examples, so that it learns to respond to the kind of input the dataset describes.
This article looks at the moment just before that training step. The goal is to understand what has been prepared, what the fine-tuning step is allowed to rely on, and how to tell which part of the overall process you are looking at when something goes wrong.
Instruction fine-tuning is a three-stage process: preparing the dataset, fine-tuning the LLM, and evaluating the LLM.
Tracing the Starting State
Before any training happens, two things are already in place. The model is the one loaded in the previous section, and fine-tuning does not begin from random weights. It continues training the model that already exists. The pre-trained LLM therefore contributes its starting state: the weights it has already learned during pre-training.
The data side is the instruction dataset prepared earlier in the chapter. That dataset was processed at the start of the chapter, so the fine-tuning step does not repeat that preparation work. The instruction dataset contributes the examples that drive the training: the kinds of inputs the model should learn to respond to.
The Three Stages at a Glance
Instruction fine-tuning is organized as a three-stage process. Stage 1 prepares the dataset. Stage 2 fine-tunes the LLM. Stage 3 evaluates the LLM. The overall process is laid out as nine numbered steps in order, with each step grouped under the stage it belongs to. The diagram below shows the three stages in sequence and the steps that sit inside each one.
Notice where the fine-tuning work sits. Stage 1 and Stage 3 surround it. Stage 1 produces the batched data loaders that the training step will consume. Stage 3 looks at the responses the tuned model produces afterward. Stage 2 is the only stage in which the model's weights are trained, and it contains three steps: loading the pretrained LLM, running instruction fine-tuning, and inspecting the modeling loss.
What Stage 2 Actually Trains
Stage 2 is the center of the process, and its three steps follow a clear order. First the pretrained LLM is loaded. Then instruction fine-tuning runs, which continues training that loaded model on the instruction examples. Finally the modeling loss is inspected, so you can see how the training is progressing.
Fine-tuning needs a way to compute loss and run training. The chapter already contains code for both, so the fine-tuning code does not write those routines again. Instead it imports them. The two functions are calc_loss_loader and train_model_simple, and both come from chapter 5.
How the Four Inputs Meet
Four inputs meet at the fine-tuning step. The pre-trained LLM is loaded and then trained further. The instruction dataset supplies the examples for that training. The two chapter 5 functions supply the loss calculation and the training procedure. The diagram below shows how these inputs feed the single fine-tuning step.
Tracing a Failure Through the Stages
Suppose a run of the process produces a result you did not expect. The three-stage structure gives you a way to narrow the search. Ask first which stage the problem most likely belongs to, and only then look at the steps inside that stage. The diagram below maps common symptoms to the stage where you should begin looking.
Locating three failures
A run produces three different problems: the batched data loaders contain the wrong examples, the pretrained model is loaded incorrectly, and the responses look wrong after training finishes. Which stage does each problem belong to?
- 1Identify the first symptom
The data loaders are wrong. Stage 1 is the stage that produces the batched data loaders, so this problem belongs to Stage 1.
- 2Identify the second symptom
The model is loaded incorrectly. Loading the pretrained LLM is one of the three steps inside Stage 2, so this problem belongs to Stage 2.
- 3Identify the third symptom
The responses look wrong after training finishes. Stage 3 looks at the responses the tuned model produces afterward, so this problem may lie in Stage 3, where responses are extracted and scored.
Stage 1 for the data loaders, Stage 2 for the model loading, and Stage 3 for the responses. Each symptom points to the stage where the search should begin.
Mistakes When Locating Problems
Common mistake Treating fine-tuning as the place to fix every problem
What it looks likeData loaders contain the wrong examples, and the fine-tuning code is edited in response.
Why it failsThe three-stage structure places data loader production in Stage 1, before fine-tuning begins.
Do this insteadStart with the stage that matches the symptom, then narrow the search inside that stage.
Common mistake Assuming the fine-tuning code contains its own loss routine
What it looks likeA loss problem is searched for only in the fine-tuning step.
Why it failscalc_loss_loaderandtrain_model_simpleare imported from chapter 5 and are not rewritten in the fine-tuning code.Do this insteadCheck the chapter 5 implementation and the way its inputs were prepared before blaming the fine-tuning step.
Common mistake Expecting fine-tuning to start from random weights
Why it failsFine-tuning continues training the pre-trained model that has already been loaded, so the starting weights come from that model.
Do this insteadWhen a model behaves unexpectedly from the start, check which pre-trained model was loaded in Stage 2.
Common mistake Looking for response problems during the training step
Why it failsStage 3 is where responses are extracted and scored after training, so response problems are located there.
Practice: Assigning Steps to Stages
What do you think happens?
A run shows that the batched data loaders are wrong. Before you look at any code, which stage should you investigate first?
Reveal answer
Stage 1, which prepares the dataset
Stage 1 produces the batched data loaders that the training step consumes, so wrong data loaders point to Stage 1.
For each item, name the stage it belongs to: loading the pretrained LLM, running instruction fine-tuning, inspecting the modeling loss, and extracting and scoring responses. Then explain why calc_loss_loader would not be rewritten inside the fine-tuning code.
Hints
- Stage 2 contains three steps, and Stage 3 is where responses are extracted and scored.
- Think about what chapter 5 provides and why importing it avoids duplicated logic.
Key Takeaways
- Instruction fine-tuning is a three-stage process: Stage 1 prepares the dataset, Stage 2 fine-tunes the LLM, and Stage 3 evaluates the LLM.
- Stage 2 is the only stage in which the model's weights are trained, and it contains loading the pretrained LLM, running instruction fine-tuning, and inspecting the modeling loss.
- Fine-tuning continues training a pre-trained LLM using the instruction dataset prepared earlier, so it does not start from random weights or repeat the dataset preparation.
calc_loss_loaderandtrain_model_simplefrom chapter 5 are imported rather than rewritten, so faults in them should be checked in the chapter 5 implementation or its inputs.- When a result diverges, map the symptom to a stage first, then narrow the search to the steps inside that stage.