Concepts / Reasons for Finetuning

Reasons for Finetuning

Finetuning adjusts a pre-trained model to improve its general quality and its performance on a specific task, including producing structured outputs such as JSON or YAML.

  • Programming

A Narrow Job and Two Candidate Models

Imagine two models. One is very large and performs well across many public benchmarks. The other is much smaller and, straight out of the box, performs worse on general tests. You are building a product that needs only one narrow task. Which model should you use?

Many people would reach for the large model because it looks stronger. This article shows why that instinct can be wrong for a narrow job. Finetuning, and a technique called distillation, can make the smaller model the better choice for that task. Teams also tend to prefer small models once they move systems into production, and the reasons for that preference are practical rather than cosmetic.

Keep the question in mind as you read. By the end you should be able to say when a small finetuned model is likely to beat a large general one, and explain the mechanism that makes that possible.

What Finetuning Changes in a Model

Definition

Finetuning adjusts a pre-trained model so that it performs better on a specific task or dataset. Its primary purpose is to improve the model's quality, covering both its general capabilities and its task-specific capabilities.

A model that scores well on a broad range of benchmarks may still handle your particular work poorly. The reason is that the training data it saw did not cover your task well. Benchmark strength and task strength are different things, and finetuning is the tool for closing the gap between them.

Finetuning is also commonly used to make a model produce outputs in a specific structure, such as JSON or YAML. If your application expects a certain format every time, a model that drifts from that format is a problem even when its answers are otherwise reasonable. A correct answer in the wrong shape can still break the software that consumes it.

Tracing a Model Before and After Task Finetuning

The diagram below traces what happens to a base model when it is finetuned on task-specific examples. The starting model handles generic requests, but its behavior on your task is weak. After finetuning on examples that show the output you want, the same model's behavior moves toward your task.

Base modelGeneric SQL outputfails on customer-specificqueriesFinetuned modelTask-matched SQLoutputfollows the customer'squery patternsanswersanswersfinetune on task examples
How does the model's behavior change before and after finetuning on task-specific examples?

Notice what changes and what does not. The model's underlying starting point is the same pre-trained model. Finetuning moves its behavior toward the examples you supply, so the output on your task becomes more like the output you want.

When Finetuning Helps: The SQL Dialect Pattern

A useful way to decide whether finetuning will help is to look for a gap between what a model does well and what your task needs. A familiar example is converting natural language into SQL. An out-of-the-box model may convert requests into standard SQL well but fail on a less common SQL dialect. In that case, finetuning the model on data that contains that dialect should help.

A related case is a model that handles standard SQL for common queries but often fails on queries specific to one customer. Finetuning on customer-specific queries may help there as well. The pattern is the same in both cases: the base model rarely sees your pattern, and the finetuning data supplies it.

Worked example

Collecting finetuning pairs for a customer's query patterns

A base model turns plain-English requests into standard SQL, but it often fails on queries that follow one customer's internal naming conventions. How would you build finetuning data for this case?

  1. 1
    Identify the gap

    Confirm that the base model is strong on standard queries and weak on this customer's patterns. Finetuning targets the weak area, not the whole model.

  2. 2
    Collect representative inputs

    Gather plain-English requests that reflect how this customer actually asks questions.

  3. 3
    Pair each input with the wanted output

    For each request, record the correct SQL query in the customer's dialect. Each pair records an input and the output you want.

  4. 4
    Finetune on the pairs

    Adjust the model toward these pairs so that its behavior on this task moves toward the customer's patterns rather than toward generic SQL.

Result

The finetuning data is task-shaped. It teaches the model the specific patterns your application needs, which is the reason finetuning helps in this case.

Mitigating Bias Through Curated Data

Finetuning is also a practical route to bias mitigation, which is one especially interesting use of the technique. The reasoning starts from a known problem. If a base model carries biases from its training data, it may repeat those biases in its outputs. The remedy is to expose the model to carefully curated data during finetuning, which can counteract the biases it learned.

Take a model that consistently assigns male-sounding names to CEOs. Finetuning it on a dataset containing many female CEOs can mitigate that pattern. The important word is curated. The goal is not simply more data. It is data chosen so that it pushes against the specific bias you have observed.

Research by Garimella and colleagues, reported in 2022, illustrates how targeted the choice of text can be. They found that finetuning BERT-like language models on text written by women can reduce those models' gender biases, and that finetuning them on texts by African authors can reduce racial biases. The choice of finetuning text can therefore aim at particular kinds of bias.

Biased base modelMale-sounding CEOnamesdominant patternCurated finetunedmodelMore balanced CEOnamespattern pushed againstoutputsoutputsfinetune on curated data
How does a model's output distribution shift from biased to more balanced after finetuning on curated data?

Why Smaller Models Are Preferred

It is possible to finetune a very large model to make it even better. In practice, finetuning smaller models is much more common. The reasons are practical, and each one affects a different stage of a project.

  • Training stage: smaller models require less memory, which makes them easier to finetune.
  • Serving stage: smaller models are cheaper and faster to use in production.
  • Running cost stage: because each request is cheaper and quicker, the ongoing cost of operating the system stays lower.
Lower memory needHigher memory needLower serving costHigher serving costFaster responsesSlower responsesStrong on the narrowtaskStrong on broadbenchmarks
How do a small finetuned model and a large general model compare on memory, cost, speed, and task strength?

Read the comparison as a set of trade-offs. The large model's breadth is real, but it comes with higher memory, cost, and latency. The small finetuned model gives up breadth in exchange for efficiency and, when finetuned on the right data, strong results on the one task you actually need.

How Distillation Passes Knowledge Downward

Distillation is a way to pass the useful behavior of a larger model to a smaller one. The common approach is to finetune a small model so that it imitates the behavior of a larger model, using data that the large model generated. The name comes from this idea: the larger model's knowledge is distilled into the smaller model.

Data synthesis techniques, which include this approach, are covered elsewhere in the course. For this article, the important part is the direction of the flow.

Large teacher modelTeacher-generatedexamplesFinetuning data setSmall student modelDistilled small modelgeneratescollected intotrainsimitates teacher
What steps does data move through, from the large model generating examples to the small model being trained on them?

Mistakes Learners Make About Finetuning

  • Common mistake

    Assuming a high benchmark score means a model will work well on your task

    Why it fails

    Benchmarks measure broad capability. A model can score well on them while handling your particular work poorly, because its training data did not cover your task well.

    Do this instead

    Test the model on examples from your own task and finetune when the gap shows up there.

  • Common mistake

    Believing that more data always fixes bias

    Why it fails

    The source's remedy is carefully curated data chosen to push against the specific bias observed. Unfocused data does not target the pattern.

    Do this instead

    Identify the bias first, then curate finetuning data that counters it.

  • Common mistake

    Thinking a small finetuned model is only a cheap compromise

    Why it fails

    A small finetuned model can outperform a much larger out-of-the-box model on a specific task, while also being cheaper and faster.

    Do this instead

    Compare models on your task and on cost and latency together, not on general benchmarks alone.

  • Common mistake

    Thinking the large teacher model is changed by distillation

  • Common mistake

    Expecting distillation to copy the teacher's internal parameters

    Why it fails

    Distillation transfers behavior indirectly, through the outputs the large model generated, and the small model is the one that is adjusted.

    Do this instead

    Describe distillation as training a small model on teacher-generated data.

Checking Your Reasoning on a Narrow Task

MEDIUM

A team needs a model that converts support tickets into a fixed JSON format for their ticketing system. The base model writes sensible summaries but sometimes drops required fields or changes key names. Using the ideas in this article, explain whether finetuning is a reasonable response, what data you would collect, and why a small model might be the better final choice.

Hints
  • Think about the difference between answers that are reasonable and outputs that match a required structure.
  • Finetuning can target structured outputs such as JSON or YAML.
  • Consider the training, serving, and running cost stages when choosing model size.

Key Takeaways

  • Finetuning adjusts a pre-trained model to improve its general quality and its performance on a specific task, including producing structured outputs such as JSON or YAML.
  • Curated finetuning data can counteract biases a model learned from its training data, because the data is chosen to push against the specific bias observed.
  • Smaller models are usually preferred for finetuning and production because they need less memory and are cheaper and faster to use.
  • Distillation finetunes a small model to imitate a larger model using data generated by that larger model. The large model is not modified; knowledge passes through its outputs.
  • A small finetuned model can outperform a much larger out-of-the-box model on a specific task when the finetuning data covers that task well.

Related concepts

More concepts starting with R