Concepts / Loss Calculation and Training Functions

Loss Calculation and Training Functions

Before any training happens, two things are already in place.

  • Programming

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.

Key point

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.

Stage 1: PreparedatasetProduces batched dataloadersStage 2: Fine-tuneLLMThe only stage that trainsweightsStage 3: Evaluate LLMExtracts and scoresresponsesdata loaderstuned model
What happens next after the dataset is prepared, and which steps sit inside each of the three stages in order?

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.

Pre-trained LLMLoaded, then trainedfurtherFine-tuning stepStage 2Instruction datasetSupplies training examplescalc_loss_loaderChapter 5 importtrain_model_simpleChapter 5 importstarting weightsexamplesloss calculationtraining procedure
Where do the imported loss and training functions come from, and how do they flow into the fine-tuning code?

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.

Unexpected resultData loaders wrongStage 1Prepare datasetModel or lossmisbehavesStage 2Fine-tune LLMResponses look wrongStage 3Evaluate LLMcheck firstcheck nextcheck lastlies inlies inlies in
Which stage contains a given step or failure, and what sits beneath each stage?
Worked example

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?

  1. 1
    Identify 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.

  2. 2
    Identify 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.

  3. 3
    Identify 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.

Result

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 like

    Data loaders contain the wrong examples, and the fine-tuning code is edited in response.

    Why it fails

    The three-stage structure places data loader production in Stage 1, before fine-tuning begins.

    Do this instead

    Start 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 like

    A loss problem is searched for only in the fine-tuning step.

    Why it fails

    calc_loss_loader and train_model_simple are imported from chapter 5 and are not rewritten in the fine-tuning code.

    Do this instead

    Check 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 fails

    Fine-tuning continues training the pre-trained model that has already been loaded, so the starting weights come from that model.

    Do this instead

    When 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 fails

    Stage 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?

  • Stage 1, which prepares the dataset

  • Stage 2, which fine-tunes the LLM

  • Stage 3, which evaluates the LLM

Reveal answer
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.

EASY

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_loader and train_model_simple from 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.

Related concepts

More concepts starting with L