Topic hub

Fine-tuning and alignment

How a pretrained language model is turned into one that follows instructions. Supervised fine-tuning, LoRA and QLoRA, reward models and RLHF, direct preference optimisation, and how to check the result.

Updated 13 Nov 2026 · supervised fine-tuning, LoRA, QLoRA, RLHF, DPO, reward model, instruction data, alignment evaluation

Overview

A pretrained model predicts plausible continuations of text. It does not answer questions, follow a format or refuse anything, because nothing in next-token prediction asks it to. Fine-tuning and alignment are the stages that turn the base model into the assistant you call through an API, and the same stages, at smaller scale, are how you adapt an open model to your own task, format or domain. This hub covers the methods a working engineer will meet: supervised fine-tuning, parameter-efficient adapters, preference optimisation, and the evaluation that tells you whether any of it worked.

The decision that matters most comes before any of this. Fine-tuning changes how a model behaves, and is poor at adding facts. If the problem is missing knowledge, retrieval is cheaper and easier to update. If the problem is format, tone, a narrow task or latency from a long prompt, fine-tuning a small open model often beats prompting a large one.

Key ideas

Supervised fine-tuning teaches the format

Supervised fine-tuning (SFT) continues training on examples of the behaviour you want: a prompt and the ideal response, rendered through the model’s chat template with role markers. The loss is the same next-token cross-entropy as pretraining, usually masked so that only the assistant’s tokens count. A few thousand carefully written examples beat a hundred thousand scraped ones, which was the finding of the LIMA paper in 2023, and the quality bar is whether you would be happy to see each response in production. Get the chat template exactly right. A model trained with one template and served with another degrades silently.

LoRA and QLoRA make it affordable

Full fine-tuning of a 7 B model in 16-bit with Adam needs about 16 bytes per parameter for weights, gradients and optimiser state, so more than 110 GB before activations. LoRA freezes the base weights and learns a low-rank update for selected matrices: instead of changing W it adds B times A, where A and B have a small inner dimension r, typically 8 to 64. The trainable parameters drop by a factor of a hundred or more, the adapter is a few tens of megabytes, and you can keep several adapters for one base model. QLoRA goes further by storing the frozen base in 4-bit NF4 and dequantising on the fly, which brings a 7 B fine-tune under 8 GB and a 70 B one onto a single 48 GB card.

# snippet
import torch

class LoRALinear(torch.nn.Module):
    def __init__(self, base: torch.nn.Linear, r: int = 16, alpha: int = 32):
        super().__init__()
        self.base = base                     # frozen pretrained weight
        self.base.weight.requires_grad_(False)
        self.a = torch.nn.Parameter(torch.randn(r, base.in_features) * 0.01)
        self.b = torch.nn.Parameter(torch.zeros(base.out_features, r))  # zero: starts as identity
        self.scale = alpha / r

    def forward(self, x):
        return self.base(x) + (x @ self.a.T @ self.b.T) * self.scale

After training the adapter can be merged into the base weights for serving at no extra cost, but merge into the 16-bit weights, not the 4-bit ones, or the rounding eats part of what you learned.

Preference optimisation: RLHF and DPO

SFT can only imitate. To make a model prefer the better of two answers, you need a signal about preference. In reinforcement learning from human feedback, people compare pairs of responses, a reward model is trained to predict those comparisons, and the policy is updated with PPO to raise reward while a KL penalty keeps it close to the SFT model. That is the InstructGPT recipe from 2022 and still the backbone of most assistant training.

base modelpretrainedSFT modelinstruction datasample pairstwo responseshuman ranks themchosen, rejectedreward modelscores a responsePPO updatereward minus KLpolicystarts as SFTRLHF loop: sample,score, update, repeatDPO: raise log p(chosen) minus log p(rejected)relative to the SFT reference, no reward model, no samplingsame pairs
The RLHF loop and the DPO shortcut. RLHF trains a reward model on human comparisons and then optimises the policy against it. DPO skips the reward model and updates the policy directly from the same pairs.

Direct preference optimisation, from 2023, showed that the same objective can be written as a classification loss on the preference pairs themselves, with the reference model standing in for the reward model. It needs no sampling loop and no separate reward network, which is why it is the method most teams try first. For tasks with a checkable answer, mathematics and code above all, recent work replaces human preference with a verifier and a group-relative policy update (GRPO), which is how DeepSeek-R1 (671 B total, 37 B active, 128 k context, MIT licence, January 2025) was trained to produce long reasoning traces.

Evaluating whether it worked

Compare against the base model and the SFT model on the same held-out set, never against nothing. For open-ended outputs the usual tool is an LLM judge with a written rubric, which correlates well with human raters on average but has known biases towards longer answers and towards its own style. Check instruction following with IFEval, check that general capability has not regressed with a few standard benchmarks, and look for over-refusal and sycophancy by hand. Preference-trained models can learn to game the reward, so read a sample of outputs every time, not only the score. The evaluation hub covers harnesses and the safety hub covers red-teaming the result.

Where to start

Read Using an LLM well first, so that you fine-tune only when prompting has failed, and Your first neural network in PyTorch for the training loop underneath. Then run the LoRA fine-tune on Apple Silicon project, which adapts SmolLM2-135M-Instruct (135 M parameters, 8 k context, Apache-2.0, November 2024) in minutes on a laptop. The quantisation formats reference explains NF4 and the GGUF types you will meet when serving the result.

Tools and platforms

Tool What for Licence Platform
PEFT LoRA, QLoRA and other adapters for Transformers models Apache-2.0 Hugging Face
TRL SFT, reward modelling, DPO, GRPO and PPO trainers Apache-2.0 Hugging Face
bitsandbytes 4-bit and 8-bit weight loading for QLoRA MIT Open source
Unsloth Faster LoRA training kernels on a single GPU Apache-2.0 Open source
Axolotl Config-driven fine-tuning runs Apache-2.0 Open source
mlx-lm LoRA fine-tuning on Apple Silicon MIT Apple
OpenAI fine-tuning API Hosted SFT and preference tuning of closed models Proprietary service OpenAI
lm-evaluation-harness Standard benchmarks before and after MIT Open source

Common mistakes

  • Fine-tuning to teach facts that change. The model will state last quarter’s figures with confidence. Use retrieval.
  • Training with a chat template that differs from the one used at serving time, including whitespace and the system prompt slot.
  • Leaving evaluation examples in the training set because both were sampled from the same export.
  • Using a full-fine-tune learning rate for LoRA, or the reverse. LoRA typically wants a rate ten times higher.
  • Ignoring the base model’s licence. A derivative of Llama carries the Llama licence, a derivative of an Apache-2.0 model does not.
  • Reporting a judge score without reading outputs. Preference-trained models find the judge’s blind spots before you do.

Going deeper

Hands-on projects

Recent papers

  • Maximizing Capacity Utilization of LoRA Adapters for Mitigating the Learning-Forgetting Trade-off

    T. Sawada, K. Goyal · arXiv cs.LG · digest

    The authors argue that LoRA's gap to full fine-tuning in mixed-domain and continual settings comes partly from adapters not using the rank they are given: the effective rank of a trained adapter sits well below the configured rank. They add a differentiable, SVD-free Rényi-2 effective-rank regulariser to the language-modelling loss so the adapter is pushed to spread its update across more directions. In mixed-domain experiments on three models, a penalty weight of 0.01 lowered mean Wikipedia perplexity at every configured rank of 16 or more compared with plain LoRA, and several LoRA variants showed the same under-use. The result is a perplexity improvement on one held-out measure, so check task accuracy before adopting it, but the regulariser is cheap to add to an existing PEFT run. Code is linked from the abstract.

  • Parsed, Plausible, Wrong: Output Format Controls Silent Errors in Small-Model Semantic Parsing

    U. Ahmed, M. Dunaev, M. N. Al Awad et al. · arXiv cs.CL · digest

    The authors fine-tuned two sub-billion models (Qwen3.5-0.8B and LFM2.5-350M) with LoRA on six lossless encodings of the same PIZZA ordering parse tree, from 44.3 down to 10.9 tokens per order, and ran 300 experiments across training-set sizes and seeds with hierarchical bootstrap and Holm correction. Shortening the output on its own never helped, but writing the target in ordinary words did: the word encoding was the most accurate on both models while emitting 4.1 times fewer tokens than the native s-expression. It cut the silent error rate, where the output parses but is wrong, by 2.3 times on Qwen and 1.8 times on LFM2, with much of the gain on negation. Every model learned to refuse off-menu input, but strict refusal also rejected 2.1 to 10.1 per cent of real, mostly polite, orders. The study is one domain and two small models, but the recipe (choose a target format the model can read as language) transfers directly to structured-output work.

  • Rethinking "RL Generalizes, SFT Memorizes": The Role of SFT Data

    Y. Hou, F. Zhang, Y. Cheng et al. · arXiv cs.LG (NeurIPS 2026) · digest

    The common claim that reinforcement learning generalises while supervised fine-tuning memorises is, on this paper's evidence, largely a statement about the SFT data used. On Qwen2.5 and Qwen3 models the authors show that a carefully mixed SFT set beats data generated by a single larger model, that matching the number of correct rollouts between SFT and RL improves SFT generalisation, and that matching total rollout budgets brings SFT level with RL. Combining both lets SFT outperform RL. Larger datasets cover more tail solution methods, which the authors argue carry the generalisable reasoning signal, and a theoretical analysis of shallow transformers under both training regimes supports the picture. The experiments are on one model family, so the budget-matching result should be checked elsewhere before you drop an RL stage in favour of more SFT data.

  • KernelMosaic: An Open Dataset and Agentic Framework for CUDA Kernel Generation

    N. Xi, Y. Lai, N. Miao · arXiv cs.LG · digest

    KernelMosaic is an open dataset of CUDA tasks paired with verified solutions and the reasoning trajectories that produced them, curated by human review and diversity-aware selection. An agentic framework walks a strong LLM through implementation, correctness fixes and performance tuning to generate the trajectories. Fine-tuning a small open-source model on the dataset substantially raises correctness on KernelBench Levels 1, 2 and 3 and increases the share of generated kernels that beat PyTorch eager execution. The authors also report that supervising on trajectories beats supervising on final answers alone. The abstract gives no absolute numbers and the release is under CC BY-NC-ND 4.0, which restricts commercial reuse of the data.