AI Infra
0%
Part III · Chapter 17

Supervised Fine-Tuning and PEFT

AuthorChangkun Ou
Reading time~17 min

A base language model and an assistant can share the same architecture and most of the same weights. Their behavior differs because they were trained on different conditional distributions. Pre-training rewards likely continuations of text. Supervised fine-tuning (SFT) rewards a specified response after a specified conversation. That change can improve instruction following, teach a new output schema, or specialize the model to a domain. It does not guarantee truthfulness, safety, or knowledge that the examples never support.

Two decisions are often collapsed into the word fine-tuning:

  1. What is the training signal? SFT defines target tokens and minimizes their next-token loss.
  2. Which parameters may change? Full fine-tuning updates the model, while parameter-efficient fine-tuning (PEFT) updates a smaller set of parameters or adds a small trainable module.

SFT and PEFT therefore answer different questions. SFT is an objective and data pipeline. LoRA is one way to parameterize the update. A model can undergo SFT with every weight trainable, with LoRA, or with another PEFT method.

Turn conversations into supervised tokens

An SFT record may contain a system message, several user and assistant turns, tool calls, and tool results. The model does not receive those fields directly. A chat template serializes them into one token sequence, including the exact role and boundary tokens expected by the tokenizer. Training and serving must use the same template. A mismatch changes the prefix distribution even when the visible conversation looks identical.

Let the serialized sequence be (u_{1:T}=(u_1,ldots,u_T)), and let a binary mask (m_t) select the tokens that should contribute to training. A common assistant-only objective is

LSFT(θ)=1Mt=1Tmtlogpθ(utu<t),M=t=1Tmt.\mathcal{L}_{\mathrm{SFT}}(\theta) = -\frac{1}{M} \sum_{t=1}^{T} m_t\log p_\theta(u_t\mid u_{<t}), \qquad M=\sum_{t=1}^{T}m_t.

Here (T) is the sequence length; (u_t) is the token at position (t); (u_{<t}) is its prefix; (p_\theta) is the model distribution with parameters (\theta); (m_t=1) for a scored target token and (m_t=0) for an ignored token; and (M) is the number of scored tokens. The normalization by (M) makes the loss an average over targets rather than a sum that grows with sequence length.

For assistant-only training, the mask usually selects assistant messages and may include the assistant end-of-turn token. System messages, user messages, padding, and tool results are context but not targets. This is a policy choice, not a definition of SFT. Some recipes score the whole sequence, only the final assistant turn, or tool-call tokens under a separate mask. State the choice because it changes what the model is asked to imitate.

record structured conversation roles + message content template exact chat template role and turn boundaries record->template tokens token sequence u_1 ... u_T template->tokens context context positions m_t = 0 tokens->context targets assistant target positions m_t = 1 tokens->targets loss masked next-token loss context->loss conditions predictions targets->loss update optimizer updates allowed parameters loss->update
Figure 17.1. An SFT record becomes one token sequence before loss masking. The template and tokenizer establish role boundaries; the target mask determines which next-token predictions update the model.

Several implementation details sit inside this apparently simple pipeline:

  • End-of-turn tokens matter. Without a learned stopping boundary, a model may continue into a simulated user turn or produce several answers.
  • Truncation changes supervision. Left truncation can remove the instruction; right truncation can remove the answer or its stop token. Log the fraction of records and target tokens lost.
  • Packing needs boundaries. Concatenating short records improves accelerator utilization, but each record still needs a terminal token and the intended attention policy. Otherwise one example can become context for the next.
  • Length changes weighting. Token-averaged loss gives a long answer more influence than a short one. Example-balanced sampling and token-balanced loss solve different problems.

Multi-turn supervision adds another choice. A recipe can score every assistant span in one serialized conversation, or create one example per assistant turn and score only that response. In both cases, later responses are conditioned on the recorded earlier replies during teacher forcing, not on errors the model might make in a live conversation. Full conversational rollouts are therefore a separate evaluation.

InstructGPT used labeler demonstrations for an SFT stage before preference modeling and reinforcement learning (Ouyang et al. 2022). Its final behavior cannot be attributed to SFT alone. Earlier instruction-tuning work also showed that training across many tasks can improve generalization to held-out task types (Wei et al. 2022). These results establish SFT as more than format selection, while leaving its effect dependent on the base model and training set.

Data determines the behavior being copied

SFT supplies positive demonstrations. It says, “produce this continuation in this context.” It does not directly say which alternative was almost acceptable, which defect made another answer worse, or how strongly two requirements should trade off. Preference data in Chapter 18 adds that kind of comparison.

The LIMA experiment is a useful boundary case. A 65-billion-parameter LLaMA model fine-tuned on 1,000 curated examples learned strong response formats and broad conversational behavior in the reported human evaluation (Zhou et al. 2023). That result supports the claim that a strong base model can need little data to adopt a style. It is not a general sample-complexity law. Teaching specialized facts, a new tool protocol, a language underrepresented in pre-training, or a difficult reasoning procedure can require much broader coverage.

A useful dataset audit separates five concerns:

Concern Question to answer before training
Correctness Are answers factually and procedurally valid for their prompts?
Coverage Which tasks, languages, lengths, formats, and failure cases are represented?
Mixture What fraction of target tokens comes from each source or capability?
Independence Are validation and test sets separated by source, task, and near-duplicate content?
Serialization Are role tokens, tool schemas, stop tokens, and truncation identical to deployment?

Duplicate prompts can make a random split look better than true generalization. One dominant source can overwhelm a smaller capability even when the record counts appear balanced, because long answers contribute more target tokens. Incorrect demonstrations are especially costly: maximum-likelihood training rewards the model for reproducing them without expressing that the label was uncertain.

Safety examples need the same distributional care. Too many broad refusals can produce over-refusal, meaning that the tuned model rejects benign requests because it learned a boundary wider than the intended policy.

Evaluation should compare the base model, a prompting baseline, and the tuned model on the same held-out tasks. Report task metrics, instruction and format compliance, calibration where relevant, and regression suites for capabilities that should remain unchanged. Tulu 3 illustrates the value of separate development and unseen evaluations plus decontamination rather than choosing a checkpoint on the public test set (Lambert et al. 2025). Training loss alone only shows that the model fits the supervised tokens.

What's contested

The “superficial alignment” hypothesis proposes that most knowledge is learned during pre-training and that limited SFT mainly selects a conversational style. LIMA provides evidence for that view in one broad-assistant setting. FLAN-style instruction tuning, domain adaptation, and tool-use training show that SFT can also change task competence. The useful question is not whether SFT elicits or teaches in the abstract. It is which held-out behaviors changed, and whether those behaviors were already reachable from the base model by prompting.

Choose which parameters may move

The update strategy is independent of the SFT dataset. Its main trade-offs are trainable state, optimizer memory, adaptation capacity, checkpoint storage, and serving behavior.

Method Trainable state Main advantage Main limitation
Full fine-tuning All selected model parameters Maximum update capacity Large gradient, optimizer, and checkpoint state
Soft prompt or prefix Learned input or prefix representations Very small task artifact (Lester et al. 2021) Consumes conditioning capacity and may lag weight updates
Bottleneck adapter New modules between frozen layers Small modular checkpoint (Houlsby et al. 2019) Adds an execution path unless fused or reparameterized
LoRA Low-rank updates on selected weight matrices Small trainable and stored delta (Hu et al. 2022) Capacity and coverage depend on rank and target modules
QLoRA LoRA plus a frozen 4-bit base during training Reduces storage for the frozen training weights (Dettmers et al. 2023) Does not remove activation memory or guarantee full-fine-tune quality

Freezing a parameter means the optimizer does not change it and does not need gradient or optimizer state for it. The frozen parameter is still used in the forward and backward computations. PEFT therefore reduces some important memory terms, but it does not make the base model, activations, temporary buffers, or communication disappear.

A useful memory ledger is

MpeakMbase+Madapter+Mgrad+Moptim+Mact+Mwork.M_{\mathrm{peak}} \approx M_{\mathrm{base}} +M_{\mathrm{adapter}} +M_{\mathrm{grad}} +M_{\mathrm{optim}} +M_{\mathrm{act}} +M_{\mathrm{work}}.

Here (M_{\mathrm{peak}}) is peak accelerator memory; (M_{\mathrm{base}}) stores frozen base weights; (M_{\mathrm{adapter}}) stores trainable PEFT parameters; (M_{\mathrm{grad}}) stores their gradients; (M_{\mathrm{optim}}) stores optimizer state; (M_{\mathrm{act}}) stores activations needed for backpropagation; and (M_{\mathrm{work}}) covers temporary kernel, communication, and dequantization buffers. The approximation omits allocator details, so measure the actual peak.

LoRA constrains each update to a low-rank factorization

For a frozen linear map (W_0\in\mathbb{R}^{d_{\mathrm{out}}\times d_{\mathrm{in}}}), low-rank adaptation (LoRA) replaces the effective weight with

W=W0+sBA,ARr×din,BRdout×r.W = W_0+sBA, \qquad A\in\mathbb{R}^{r\times d_{\mathrm{in}}}, \qquad B\in\mathbb{R}^{d_{\mathrm{out}}\times r}.

Here (d_{\mathrm{in}}) and (d_{\mathrm{out}}) are the input and output dimensions; (r\le\min(d_{\mathrm{in}},d_{\mathrm{out}})) is the adapter rank; (A) and (B) are trainable; and (s) is a fixed scale, commonly (\alpha/r) in the original parameterization for a chosen multiplier (\alpha). The update obeys (\operatorname{rank}(sBA)\le r). For an input (x\in\mathbb{R}^{d_{\mathrm{in}}}), the output is (Wx=W_0x+sB(Ax)). Implementations commonly initialize one factor so that (BA=0), making the adapted model equal to the base model before the first update.

The base matrix contains (d_{\mathrm{out}}d_{\mathrm{in}}) parameters. The LoRA branch contains

NLoRA=r(din+dout),ρ=NLoRAdoutdin.N_{\mathrm{LoRA}} =r(d_{\mathrm{in}}+d_{\mathrm{out}}), \qquad \rho =\frac{N_{\mathrm{LoRA}}} {d_{\mathrm{out}}d_{\mathrm{in}}}.

Here (N_{\mathrm{LoRA}}) is the number of trainable adapter parameters for this matrix and (\rho) is their fraction of the base-matrix parameter count. The total for a model is the sum over every targeted matrix, plus any biases, embeddings, or output heads deliberately left trainable.

2026-08-03T23:47:32.993191 image/svg+xml Matplotlib v3.11.0, https://matplotlib.org/ 8 16 24 32 40 48 56 64 LoRA rank r 0.0 0.5 1.0 1.5 2.0 2.5 3.0 LoRA parameters (% of matrix) 4096-square matrix
Figure 17.2. For one 4,096 by 4,096 matrix, the exact LoRA parameter fraction is (2r/4{,}096). The chart counts the two factors only; it does not predict task quality, memory use, or the fraction for a whole model.

The runnable performs the same exact accounting. It describes one matrix, not a complete transformer.

d_in = 4096
d_out = 4096
ranks = [4, 8, 16, 32, 64]

full = d_in * d_out
print(f"base matrix parameters: {full:,}")
for rank in ranks:
    adapter = rank * (d_in + d_out)
    fraction = 100 * adapter / full
    print(f"rank {rank:>2}: {adapter:>7,} parameters ({fraction:>6.3f}%)")
x input x base W_0 x frozen base path x->base A A x r by d_in x->A sum + base->sum B s B(Ax) d_out by r A->B B->sum y output Wx sum->y
Figure 17.3. LoRA leaves the base matrix frozen and trains a parallel low-rank path. The paths can remain separate for adapter switching or be added into one materialized weight for deployment.

The low-rank form is a capacity constraint, not proof that every useful fine-tuning update has low rank. The original LoRA paper found competitive results in its evaluated settings (Hu et al. 2022). Later experiments on mathematics and code found that standard low-rank LoRA learned less of the new domain than full fine-tuning while retaining more of the base model's behavior (Biderman et al. 2024). Rank, target modules, dataset size, batch size, initialization, scale, and learning rate all affect the comparison.

Figure 17.4. A linear-algebra illustration of low-rank reconstruction. Increasing the rank can represent more independent directions in a synthetic update, while the parameter count remains r(d_in + d_out). This is not a task-quality curve.

Target selection matters as much as rank. Attention-only LoRA was common in early work, but later experiments found gains from targeting MLP and, where applicable, MoE projections as well. A 2025 report found high-rank LoRA could match full fine-tuning in its Llama and Qwen sweeps when adapters covered all linear layers and were not capacity-constrained; it also found a different best learning rate and worse behavior at large batch sizes (Schulman and others 2025). Those are empirical results, not universal defaults. Sweep the update strategy on the actual dataset.

QLoRA changes the storage of the frozen base

quantized low-rank adaptation (QLoRA) keeps the frozen base weights in a 4-bit NormalFloat representation and trains LoRA factors in a higher compute precision. During computation, weight blocks are dequantized as needed; gradients flow through the computation to the adapter parameters, while the quantized base remains unchanged. Double quantization compresses quantization constants, and paged optimizers manage temporary memory spikes (Dettmers et al. 2023).

The QLoRA paper demonstrated fine-tuning a 65-billion-parameter model on one 48 GB GPU in its reported configuration. That is an existence result, not a hardware rule. Sequence length, batch size, activation checkpointing, attention kernels, adapter coverage, optimizer, and temporary buffers can move the peak substantially. Four bits per base parameter is also only the ideal payload for the quantized values; scales and other metadata add storage.

QLoRA separates training precision from the deployment format. A QLoRA-trained adapter can be served beside a quantized base, beside a higher-precision base, or after a supported merge and requantization path. These choices need their own quality and latency tests. “Trained with QLoRA” does not by itself mean that the final server executes every operation in four bits.

Merged and switchable adapters have different costs

After training, a LoRA update can be materialized as

Wmerged=W0+sBA.W_{\mathrm{merged}}=W_0+sBA.

Here the merged matrix uses the original linear operation, so the LoRA branch adds no separate matrix multiplications at inference. The cost is operational: the result is another full-size weight set, and that model instance no longer has a small independent adapter to switch off. Keeping (A) and (B) separate preserves modularity but adds adapter computation, memory reads, and routing. The real latency depends on batch shape, kernel fusion, rank, target modules, and whether requests in a batch use the same adapter.

Adapter hot-swapping also has compatibility constraints. A server must pair an adapter with the exact base architecture and parameter names it was trained against, plus the compatible tokenizer and chat template. Current PEFT tooling can replace LoRA weights in place, but target modules and compiled shapes constrain which adapters can share a slot (Hugging Face 2026). “One base, many adapters” is a deployment design, not an automatic property of an adapter file.

Weight merging is a separate approximation

Do not confuse merging one LoRA branch into its own base with combining several independently fine-tuned models. The latter is model merging. It operates on compatible parameter coordinates and tries to preserve several behaviors without another training run.

For fine-tuned parameter vector (\theta_i) derived from the same base (\theta_0), define the task delta and a weighted merge as

τi=θiθ0,θmerge=θ0+i=1nλiτi.\tau_i=\theta_i-\theta_0, \qquad \theta_{\mathrm{merge}} =\theta_0+\sum_{i=1}^{n}\lambda_i\tau_i.

Here (i\in{1,\ldots,n}) indexes the source fine-tunes; (\tau_i) is the parameter displacement for source (i); (\lambda_i) is its chosen merge coefficient; and (\theta_{\mathrm{merge}}) is the candidate merged model. The operation assumes matching architecture, tensor shapes, tokenizer-related parameters, and parameter alignment. A shared initialization supplies that alignment. Arithmetic on unrelated checkpoints is not justified by the equation.

Model soups found that averaging checkpoints from one fine-tuning sweep often improved the evaluated models (Wortsman et al. 2022). Task arithmetic showed that adding or negating task deltas can steer behavior in tested settings (Ilharco et al. 2023). Neither result says that arbitrary skills add cleanly. TIES trims small deltas, resolves sign conflicts, and merges values that agree with the selected sign (Yadav et al. 2023). DARE randomly drops delta parameters and rescales those retained before a later merge (Yu et al. 2024). These methods manage interference; they do not certify that every parent capability survives.

Figure 17.5. Task-vector arithmetic in an illustrative two-dimensional slice. Alignment makes deltas reinforce; opposition makes them cancel. Real checkpoints occupy far more dimensions, so the result must be evaluated rather than inferred from vector addition alone.

When the training data can be combined, a joint fine-tune provides direct evidence about the joint objective. A training-free merge is useful when data or compute is unavailable, or as a cheap candidate in a measured merge sweep. Test every constituent task, general capabilities, calibration, and safety after the merge.

Run adaptation as a controlled experiment

A reliable workflow keeps the data, optimizer, parameterization, and deployment artifact distinguishable:

  1. Define held-out task and regression suites before inspecting final results.
  2. Freeze the base checkpoint, tokenizer, chat template, maximum length, and target-mask policy as one versioned contract.
  3. Audit source mixture, duplicates, contamination, truncation, and target-token counts.
  4. Measure the base model and a prompt-only baseline.
  5. Sweep learning rate and at least one capacity setting, such as LoRA rank or target modules. Do not reuse full-fine-tune hyperparameters without testing.
  6. Compare PEFT with a small full-fine-tune baseline when hardware permits.
  7. Evaluate the exact artifact to be served: unmerged adapter, merged weights, and quantized deployment can behave differently.
Symptom Likely checks
Training loss falls but chat behavior is erratic Template mismatch, wrong target mask, missing stop token
Target task improves while broad ability drops Mixture imbalance, excessive steps, full-update forgetting
LoRA underfits despite low loss on a small split Rank or target-module bottleneck, duplicate split, narrow evaluation
QLoRA runs out of memory Activations, sequence length, batch size, temporary buffers, checkpointing
Adapter is correct but serving is slow Unmerged branch cost, per-request routing, fragmented batches, compilation
Merged model loses a parent skill Delta conflict, coefficient choice, incompatible sources, no joint training
Lower-layer constraint

The training example is not just prose. It is bytes, normalized text, template tokens, truncation, an attention mask, a target mask, and a packed sequence. Likewise, “trainable parameters” is not peak memory: base weights, activations, gradients, optimizer state, communication buffers, and temporary kernels occupy different terms. Record those lower-layer contracts before comparing recipes.

The evidence boundary

SFT makes a model more likely to reproduce demonstrated behavior under a particular serialization. PEFT changes the set or form of parameters allowed to carry that update. Neither choice determines the other, and neither removes the need for held-out evaluation.

Small curated datasets can be enough for style and familiar tasks. LoRA and QLoRA can make those experiments much cheaper. The claims stop there. New knowledge, distant domains, long training runs, precision-sensitive tasks, and combined skills may expose capacity, forgetting, quantization, or interference limits. Choose the smallest update that passes the target and regression suites, then verify the exact deployed artifact.

Further reading

  • Zhou et al., “LIMA: Less Is More for Alignment,” 2023. arXiv:2305.11206
    LIMA fine-tunes a 65B LLaMA model on 1,000 curated prompt-response pairs and reports strong format learning and conversational behavior, motivating the superficial-alignment hypothesis in that setting.
  • Hu et al., “LoRA: Low-Rank Adaptation of Large Language Models,” 2022. arXiv:2106.09685
    LoRA freezes pretrained matrices and learns additive low-rank factors, reducing trainable and stored task-specific state in the paper's evaluated settings.
  • Dettmers et al., “QLoRA: Efficient Finetuning of Quantized LLMs,” 2023. arXiv:2305.14314
    QLoRA stores a frozen base in 4-bit NF4 and trains higher-precision LoRA adapters, combining double quantization and paged optimizers to reduce memory in its experiments.
  • Houlsby et al., “Parameter-Efficient Transfer Learning for NLP” (adapters), 2019. arXiv:1902.00751
    Adapter modules inserted into BERT layers match full fine-tuning performance on GLUE while training only 3.6% as many parameters per task.
  • Wortsman et al., “Model Soups: Averaging Weights of Multiple Fine-Tuned Models Improves Accuracy Without Increasing Inference Time,” 2022. arXiv:2203.05482
    For compatible models from the same fine-tuning sweep, the paper reports that selected weight averages can outperform the best individual checkpoint without ensemble inference cost.
  • Ilharco et al., “Editing Models with Task Arithmetic,” 2023. arXiv:2212.04089
    Task arithmetic defines same-base fine-tuning deltas and reports that scaled addition or negation steers behavior in the paper's evaluated models and tasks.
  • Yadav et al., “TIES-Merging: Resolving Interference When Merging Models,” 2023. arXiv:2306.01708
    TIES-Merging trims small same-base deltas, elects a sign per coordinate, and averages updates that agree with that sign to reduce measured merge interference.
  • Yu et al., “Language Models are Super Mario: Absorbing Abilities from Homologous Models as a Free Lunch” (DARE), 2024. arXiv:2311.03099
    DARE randomly drops and rescales same-base fine-tuning deltas before applying another merge rule, with performance depending on compatible source models and chosen sparsity.
  • Ouyang et al., “Training Language Models to Follow Instructions with Human Feedback” (InstructGPT's supervised demonstration stage before reward modeling and PPO), 2022. arXiv:2203.02155
    InstructGPT trains on labeler demonstrations, then preference rankings and reinforcement learning; its final assistant behavior is evidence about the full pipeline, not SFT alone.
  • Schulman & others, “LoRA Without Regret” (company experiments on target modules, rank, batch size, and learning rate), 2025. thinkingmachines.ai
    A 2025 company research report comparing LoRA and full fine-tuning across specified Llama and Qwen experiments, including target-layer, rank, batch-size, and learning-rate sweeps.

Comments

Log in to comment