Concepts / Evaluating a Fine-Tuned Model

Evaluating a Fine-Tuned Model

The LoRA fine-tuning run calls the chapter 6 training function, with an AdamW optimizer, a learning rate of 5e-5, a weight decay of 0.1, and 5 epochs.

  • Programming

Starting From a Finished Training Run

Fine-tuning changes how a model behaves, but a changed model is not automatically a better one. Once training has finished, you still have to find out how well the model performs on the task you care about. This article follows that question from start to finish. It begins with a complete fine-tuning run rather than a definition, then moves through the training log and timing output, and ends with the process that turns a fine-tuned model's responses into one performance number.

The run in this article uses LoRA layers. These are added to an existing model, and the model is then trained with the training function from chapter 6. The run also prints how long training took. Your job is to read the run first, form an expectation about its timing, and then use the printed output to check that expectation.

What do you think happens?

Suppose you train the same model once with LoRA layers and once without them, using the same training function. Which run do you expect to take longer on this small example, and why?

Reveal answer
Answer:

The LoRA run is the one expected to take longer on this small example.

LoRA layers add computation during the forward pass. On a small example that extra cost is not offset by savings elsewhere, so training with them can be slower. The advantage appears only for larger models, which is covered later in this article.

Reading the Setup in Execution Order

The easiest way to understand a training script is to read it in the order it runs. The source walks through the run in exactly that order, and each stage prepares something the next stage depends on.

  1. 1
    The script imports the training function from chapter 6 and records a start time.
  2. 2
    It fixes the random seed with torch.manual_seed(123), so the run is set up the same way each time.
  3. 3
    It creates an AdamW optimizer over the model's parameters, with a learning rate of 5e-5 and a weight decay of 0.1. These settings decide how the trainable parameters are updated.
  4. 4
    It calls the training function, which is configured to train for 5 epochs.
  5. 5
    It records an end time and prints the elapsed minutes.

The optimizer, the learning rate, the weight decay, and the epoch count all relate to the training function in one direction. The optimizer and its settings describe how each update is made, and the epoch count describes how many passes the training function makes over the data. Reading the setup as a list of inputs passed into one training call makes it easier to see which setting controls which behavior.

Why LoRA Can Be Slower and Then Faster

The timing result depends on two costs that move in opposite directions. LoRA layers add computation during the forward pass, so on a small example such as the one in the listing, training with them can be slower than training without them. Backpropagation, however, becomes more costly as models get larger. For larger models, training with LoRA is typically faster than training without it, because the cost saved in the backward pass outweighs the extra cost in the forward pass.

Because of this, the timing result depends on the size of the model and not only on whether LoRA is used. A result can look like a contradiction if you have only seen the small example. The source does not give a specific model size at which the two costs cross over, so treat the crossover as a trend rather than a fixed threshold.

Forward passLoRA adds extra workForward passLoRA adds extra workBackward passModest costBackward passCostly, LoRA saves heresame pipelinesame pipeline
How do the forward and backward costs of standard full fine-tuning and LoRA compare on a small model versus a larger one? The source gives the direction of the trend, not exact crossover numbers.

Reading the Training Log

The printed output is a log of the training run. Each entry gives an epoch number, a step number, a training loss, and a validation loss. The step numbers advance in increments of 50, because the training function was asked to evaluate every 50 steps. Accuracy lines appear only at some of those points, not at every entry.

A useful habit is to compare the training loss and the validation loss at the same step rather than looking at either one alone. In this run, the training loss keeps getting smaller across the epochs, while the validation loss goes up and down from one entry to the next. A training loss that keeps falling while the validation loss does not follow it is worth noticing, because it tells you the two measurements are not telling the same story.

  1. 1
    Find a step where both losses are printed.
  2. 2
    Read the training loss and the validation loss from that same entry.
  3. 3
    Move to the next entry and check whether each loss moved in the same direction.
  4. 4
    Note any stretch where the training loss falls while the validation loss does not, and mark it as the place where the result differs from expectation.

Comparing Training Times Fairly

The source gives three timing figures that are easy to mix up. Two describe typical hardware: about 15 minutes on an M3 MacBook Air laptop, and less than half a minute on a V100 or A100 GPU. The third describes the specific run printed in the listing. Read them side by side and notice which ones state their hardware and which do not.

  • Common mistake

    Comparing a printed time against a typical figure as if they came from the same machine

    What it looks like

    Setting the run's printed minutes beside the 15-minute laptop figure and concluding the code got slower.

    Why it fails

    Timing figures are only comparable when the hardware is known. A laptop and a GPU are different machines, so the numbers do not describe the same work.

    Do this instead

    Write down the hardware next to every timing figure before comparing any two of them.

  • Common mistake

    Treating the printed time as the cost of the forward or backward pass

    What it looks like

    Saying that the printed minutes show how long backpropagation took.

    Why it fails

    The timer covers the whole training call, including evaluation performed during training.

    Do this instead

    Describe the printed number as the cost of the entire training call, and only attribute parts of it to a stage if you have measured that stage separately.

  • Common mistake

    Judging LoRA from a single small example

    What it looks like

    Concluding that LoRA is always slower because the small example ran slower with it.

    Why it fails

    LoRA's extra forward cost matters most on small models, while its backward-pass savings grow with model size.

    Do this instead

    Compare runs on models of different sizes before drawing a general conclusion about speed.

How the Evaluation State Changes

Evaluation is a process that produces a number from a collection of model outputs. Each step hands a slightly different collection to the next step. The collection starts as inputs, becomes raw responses, then scored responses, and finally a single average. Tracking what the evaluation holds after each step is the easiest way to understand the final number.

Loaded modelTest examplesRaw responsesPer-example scoresAveraged scorefed inputs from test setrun on each itemscorer assigns a score to each responseaveraged into one value
Which piece of state does each step produce, and which step changes the collection from one form into another?

Turning Responses Into One Score

Evaluation starts with a test set, which is a set of inputs the model was not trained on. The fine-tuned model is run on each item, and its responses are collected. At this point nothing has been judged yet, so the collection holds raw text responses only.

The next step is scoring. Each response in the collection receives a score, so the collection changes from a list of text responses into a list of numbers attached to those responses. The source describes the scorer as another LLM, and gives one concrete setup: an 8-billion-parameter Llama model run through the Ollama application, which can score the responses on the test set automatically.

The scores are then averaged, and the average serves as the number that quantifies performance. The test set may contain many examples, but the reported performance is a single value. Any question about one particular example can be answered only by going back to the scored collection before the averaging took place.

Test setinputs not seen in trainingFine-tuned modelgenerates a response perinputRaw responsestext, not yet judgedScoreranother LLM, e.g. Llama viaOllamaPer-example scoresone number per responseAveragesingle performance valueeach inputcollectedpassed to scorerscore attachedaveraged
What sequence of steps takes each model response from the test set and converts it into one final number, and what gets passed from each step to the next?

Why an Average Can Mislead

An average summarizes the test set but hides the spread of individual scores. The two scores in the illustration below are invented for teaching purposes and are not from the source. They show how a middle-looking average can come from two very different outcomes.

Example Ahigh score, 9 of 10Average5 of 10Example Blow score, 1 of 10averagedaveraged
How do individual per-example scores spread out, and how can that spread be hidden when they are collapsed into one average?
  • Common mistake

    Reading a middling average as consistent performance

    What it looks like

    Seeing an average of 5 out of 10 and assuming every example scored about 5.

    Why it fails

    The average can hide examples that scored very high alongside examples that scored very low.

    Do this instead

    Keep the per-item scores and look at how they are distributed before describing the model as consistent.

  • Common mistake

    Discarding the scored collection once the average is computed

    What it looks like

    Throwing away the per-example scores and then being unable to say which inputs failed.

    Why it fails

    The average is one number, so questions about a single example can only be answered from the scored collection.

    Do this instead

    Keep the per-example scores whenever you need detail.

Checking Your Understanding of the Pipeline

MEDIUM

A fine-tuned model is evaluated on a test set of 200 inputs. Describe the state of the evaluation after the model has run on all 200 inputs, and again after the scorer has run. Say what is held in each case and what the final step removes from the collection.

Hints
  • After the model runs, the collection holds raw responses that have not been judged.
  • After scoring, each response has a number attached to it.
  • The averaging step reduces many scores to one value.
MEDIUM

Two training runs are compared: one on a small example and one on a much larger model. The printed time for the small run is longer with LoRA. Explain why this does not contradict the claim that LoRA is typically faster on larger models, and name the hardware detail you would need before comparing the printed time with any typical figure.

Hints
  • Think about where LoRA adds cost and where it saves cost.
  • The timer covers the whole training call.

Key Takeaways

  • A LoRA fine-tuning run is read in execution order: import the training function, record a start time, fix the random seed, create an AdamW optimizer with learning rate 5e-5 and weight decay 0.1, train for 5 epochs, then record the end time.
  • The printed timer covers the whole training call, including evaluation performed during training, so it cannot be attributed to the forward or backward pass alone.
  • LoRA adds forward-pass cost that can make a small run slower, while larger models gain from savings in the costlier backward pass.
  • Evaluation runs a fine-tuned model on a test set, scores each response, and averages the scores. The collection shrinks from many items to one number, so keep per-example scores when you need detail.
  • An averaged score can hide a wide spread of individual results, so compare distributions and not only the mean.

Related concepts

More concepts starting with E