AI & LLMs

Fine-Tuning Large Language Models: The Early LLM Playbook

Prompting a large model gets you surprisingly far, but many production tasks still need fine-tuning. As models grow into the billions of parameters, full fine-tuning is getting expensive, and a wave of parameter-efficient methods is appearing. This playbook covers when to fine-tune, the main approaches in 2021, and how to evaluate the result.

By EthicalHat Research · · 3 min read
Infographic: The fine-tuning playbook

Since GPT-3 arrived, it has been tempting to think prompting will replace training altogether. In practice, organisations deploying language models in production often find that a carefully designed prompt gets them 80% of the way, and the last 20% (accuracy, consistency, format, cost) requires fine-tuning.

At the same time, models are getting so large that full fine-tuning is expensive, and storing a separate full copy of a multi-billion-parameter model for every task is impractical. 2021 has produced a burst of research into more efficient approaches. Here is the playbook as it stands at the end of the year.

When to fine-tune

Fine-tuning is worth considering when:

  • Prompting is not accurate enough on your evaluation set, even with good examples.
  • Consistency matters: you need a precise output format or tone every time.
  • The domain is specialised, such as legal, medical or internal jargon the base model handles poorly.
  • Cost or latency matters: a smaller fine-tuned model can outperform a much larger prompted model on a narrow task, at a fraction of the cost.
  • Prompts are getting long: stuffing many examples into every request is slow and expensive.

Before fine-tuning, build two baselines: a well-crafted prompt on a large model, and a classic model (such as TF-IDF with logistic regression) where the task allows. They tell you whether fine-tuning is worth the effort.

Data: the part that matters most

The quality of your fine-tuning data is the biggest single factor in results.

  • Start small and clean. A few hundred to a few thousand high-quality, consistently labelled examples often beat tens of thousands of noisy ones.
  • Cover the real distribution, including edge cases and the inputs you expect in production.
  • Write annotation guidelines and measure agreement between annotators.
  • Keep a held-out test set that never touches training, and a separate set for tuning hyperparameters.
  • Watch for leakage, where near-duplicates of test examples appear in training data.

The methods

Full fine-tuning

Update all the model's weights on your task data. It usually gives the best accuracy for mid-sized models such as BERT or RoBERTa, and it is well supported by tools like Hugging Face's Trainer. The downsides are compute cost and a full model copy per task.

Adapters

Small bottleneck layers inserted into each transformer block. Only the adapters are trained, often a few per cent of the parameters, while the original weights are frozen. One base model can serve many tasks by swapping adapters.

Prefix tuning and prompt tuning

Instead of changing weights, learn a small set of continuous "virtual tokens" prepended to the input. Research this year (prefix tuning in January; prompt tuning from Google in April) shows this approach becomes competitive with full fine-tuning as model size grows into the billions.

LoRA

Low-Rank Adaptation, published by Microsoft researchers in June, freezes the pre-trained weights and learns small low-rank update matrices for selected layers. It trains a tiny fraction of the parameters, adds no inference latency once merged, and in the paper matches or beats full fine-tuning on several benchmarks.

flowchart LR
  X[Input] --> W[Frozen weights W]
  X --> A[Trainable A<br/>d × r] --> B[Trainable B<br/>r × d]
  W --> S((+))
  B --> S
  S --> Y[Output]

The idea in code, conceptually:

python
import torch.nn as nn

class LoRALinear(nn.Module):
    """Wraps a frozen linear layer with a trainable low-rank update: W x + B(A x) * scale."""
    def __init__(self, base: nn.Linear, r: int = 8, alpha: int = 16):
        super().__init__()
        self.base = base
        for p in self.base.parameters():
            p.requires_grad = False
        self.A = nn.Linear(base.in_features, r, bias=False)
        self.B = nn.Linear(r, base.out_features, bias=False)
        nn.init.zeros_(self.B.weight)          # start as a no-op
        self.scale = alpha / r

    def forward(self, x):
        return self.base(x) + self.B(self.A(x)) * self.scale

Instruction tuning

A different kind of fine-tuning is emerging with large models: training on a mixture of many tasks phrased as natural-language instructions. Google's FLAN (September) and the BigScience T0 model (October) show that instruction-tuned models become much better at following new instructions zero-shot. This hints at a future where base models are routinely tuned to follow instructions before anyone adapts them to specific tasks.

Evaluating the result

  • Compare against your baselines on the same held-out test set.
  • Look beyond a single accuracy number: per-class metrics, calibration, and performance on important slices of the data.
  • Do error analysis. Read the failures and categorise them. They usually point to data problems.
  • Test for regressions on behaviour you care about, such as bias, refusal to handle harmful content, or robustness to typos.
  • Measure latency and cost in the environment where the model will run.

Operational considerations

  • Version everything: base model, data, code, hyperparameters and evaluation results.
  • Monitor in production for data drift and falling accuracy, and plan periodic retraining.
  • Protect training data. Fine-tuned models can memorise and reproduce training examples, so avoid including sensitive personal data unless it is essential and properly governed.

The bottom line

Fine-tuning remains the workhorse for turning general language models into dependable production components. The toolkit is changing fast: parameter-efficient methods make it far cheaper, and instruction tuning is changing what base models can do before any task-specific training. What has not changed is the foundation of success: clean data, strong baselines and honest evaluation.

Our agentic AI security team helps organisations decide when to prompt, when to fine-tune and how to evaluate models before they reach production.

EthicalHat Research

The EthicalHat research team: penetration testers, SOC analysts and engineers writing about security, software engineering and AI.

Want an expert opinion on your own systems?

Email info@ethicalhat.com or call +1-844-838-4422. Our security engineers are happy to talk it through.