Concepts / Deciding When to Finetune Models

Deciding When to Finetune Models

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, out of the box, performs worse on general tests. You are building a product that only needs one narrow task. Which model should you use?

What do you think happens?

For one narrow product task, which model is the better choice?

  • The very large model, because it scores well across many benchmarks

  • The much smaller model, finetuned for that specific task

Reveal answer
Answer:

The smaller model, once finetuned for that task, can be the better choice.

Finetuning adjusts a pre-trained model toward the work you actually need. A small model that has been finetuned on that task can outperform a much larger out-of-the-box model on it, and it is also usually cheaper and faster to run in production. The rest of this article traces how that happens and when it is worth doing.

Tracing What Finetuning Changes

Finetuning means adjusting a pre-trained model so it performs better on a specific task or dataset. The model begins as a general-purpose system. Finetuning shifts its behavior toward the work you care about, and the difference shows up when you compare the model before and after that adjustment.

Base modelOut of the boxDialect SQL requestOften failsFinetuned modelAdjusted on task dataDialect SQL requestHandled correctlyfinetune on dialect datasame request
What changes in the model between the out-of-the-box version and the finetuned version, and how its behavior on a dialect SQL request differs afterward.

The model's weights are adjusted, but the request itself stays the same. The only change is what the model has been trained to produce for that kind of input. This is why finetuning is a tool for task-specific behavior and not only for general quality.

Improving Quality and Output Structure

Definition

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.

The primary reason to finetune is to improve quality on two levels: general capabilities and task-specific capabilities. A model that scores well on a broad range of benchmarks may still handle your particular work poorly, because the training data it saw did not cover your task well. Benchmark strength and fit to your task are different things.

The source gives a concrete pattern for deciding when finetuning helps. An out-of-the-box model may convert natural language into standard SQL well but fail on a less common SQL dialect. In that case, finetuning 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 a customer. Finetuning on customer-specific queries may help there too.

Finetuning is also commonly used to get a model to produce outputs in specific structures, 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 break the software that consumes it.

Using Curated Data to Counter Bias

The source describes bias mitigation as an especially interesting use of finetuning. The reasoning starts from a known problem: if a base model carries biases from its training data, it may repeat them in its outputs. The proposed remedy is to expose the model to carefully curated data during finetuning, which can counteract those biases.

Consider the example the source gives. If a model consistently assigns male-sounding names to CEOs, finetuning it on a dataset containing many female CEOs can mitigate that pattern. The curation matters. The goal is not just more data, but data chosen so that it pushes against the specific bias you have observed.

The source also reports research by Garimella and colleagues from 2022. 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. These findings show that the choice of finetuning text can target particular kinds of bias.

Why Smaller Models Are Preferred

It is possible to finetune a big model to make it even better, but finetuning smaller models is much more common. The reasons are practical. Smaller models require less memory, which makes them easier to finetune. They are also cheaper and faster to use in production. Each advantage affects a different stage of a project: training, serving, and running costs.

More memory neededHeavier to hostLess memory neededEasier to finetuneCostlier to useRunning costsCheaper to useLower running costsSlower to useServing latencyFaster to useQuicker responsesStrong on broadbenchmarksGeneral testsStrong on the narrowtaskAfter finetuning
How the two models differ in size-related trade-offs and in task performance, and where the small finetuned model comes out ahead.

The comparison is not a claim that small models are better at everything. The large model may remain stronger across broad benchmarks. The small finetuned model earns its place when the job is narrow, the data for that job is available, and the operating constraints of memory, cost, and speed matter.

How Distillation Moves Knowledge

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. Because this approach distills the larger model's knowledge into the smaller model, it is called distillation. Data synthesis techniques of this kind are covered elsewhere in the course.

Large teacher modelStrong across benchmarksTeacher-generatedoutputsExamples the large modelproducedCurated trainingpairsInputs paired with desiredoutputsSmall student modelBeing adjustedFinetuned small modelImitates teacher on thetaskgenerates outputsorganized into pairsused as finetuning datafinetuned to imitate
How data moves from the large model to the small model, and which steps turn the large model's outputs into training data for the small one.
  1. 1
    The large model receives inputs for the task and produces outputs. Its behavior becomes the source of examples.
  2. 2
    Those generated outputs are collected and arranged into training data, so each example pairs an input with the output you want the small model to learn.
  3. 3
    The small model is finetuned on that data so that it imitates the larger model's behavior on the task.
  4. 4
    The finetuned small model is evaluated on the task and used in production in place of the large model.
Key point

The direction of the flow matters. The large model is not modified by this process. Its behavior is used as a source of examples, and the small model is the one being adjusted. Knowledge is transferred indirectly, through the outputs the large model generated, rather than by copying the large model's internal parameters.

Worked Decision for a Dialect Task

Worked example

Choosing Between a Large Model and a Small Finetuned Model

A team needs a model that converts natural language requests into queries in a less common SQL dialect. The team has a very large general model that scores well on benchmarks and a much smaller model that performs worse out of the box. The team can collect examples of the dialect. Which model should they use?

  1. 1
    Check the fit between general strength and the task

    The large model's benchmark strength is a general signal. The task is a less common dialect, which the base model may rarely have seen. The question is whether the large model handles this dialect, not whether it scores well overall.

  2. 2
    Identify the failure pattern

    The base model is expected to handle standard SQL well and fail on the less common dialect. That is the pattern finetuning targets, so finetuning on data containing that dialect is the relevant remedy.

  3. 3
    Finetune the small model on dialect data

    Pairs of natural language requests and correct dialect queries are collected. The small model is finetuned on them, adjusting its behavior toward this task.

  4. 4
    Weigh the production constraints

    The small model needs less memory, costs less to use, and responds faster. If it matches or exceeds the large model on this narrow task, these advantages make it the stronger engineering choice.

Result

The team should finetune the smaller model on dialect data and evaluate it on that task. If the finetuned small model performs well there, it is the better choice for this narrow job, even though the large model remains stronger on broad benchmarks. Whether it truly wins depends on measuring its performance on the task itself.

Mistakes When Deciding to Finetune

  • Common mistake

    Assuming a high benchmark score means the model will handle your task

    What it looks like

    A model scores well across public benchmarks but fails on queries specific to one customer.

    Why it fails

    Benchmarks measure broad capability. A model can be strong overall and still handle 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 before deciding. Finetune only when the failure pattern is one that task-specific data can address.

  • Common mistake

    Finetuning with more data instead of the right data

    What it looks like

    A team adds a large general dataset to correct a gender bias in a model's outputs.

    Why it fails

    The curation is what counteracts bias. A large dataset that does not push against the specific bias observed may do little for that bias.

    Do this instead

    Identify the bias first, then choose data that deliberately pushes against it, such as the CEO example where many female CEOs are included.

  • Common mistake

    Treating the large model as the only source of quality

    What it looks like

    A team assumes only the biggest model can produce acceptable output for a narrow task.

    Why it fails

    A small model finetuned for a specific task can outperform a much larger out-of-the-box model on that task.

    Do this instead

    Compare the candidates on the narrow task, and consider distillation when the large model's behavior can be used to generate training examples.

  • Common mistake

    Expecting finetuning to fix format problems without training for the format

    What it looks like

    An application requires JSON every time, but the model sometimes returns prose.

    Why it fails

    A model that drifts from a required format is a problem even when its answers are otherwise reasonable.

    Do this instead

    Include examples in the finetuning data that show the exact structure the application expects.

Applying the Decision Rule

MEDIUM

A product must return customer-specific SQL queries every time. A large general model answers common SQL questions well but often fails on queries specific to one customer. A small model is cheaper and faster to run. Decide whether finetuning the small model is worth trying, and state which data you would collect and why.

Hints
  • Ask whether the failure is about a pattern the base model rarely sees.
  • Consider which data would show the model the customer-specific queries.
  • Think about whether the production advantages of a small model matter for this product.

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.
  • Finetuning on carefully curated data can counteract biases a model learned from its training data, and the choice of data should target the 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; its outputs are the source of training examples.
  • A small finetuned model can outperform a much larger out-of-the-box model on a specific task, so the decision should be made by testing on that task.

Related concepts

More concepts starting with D