Skip to content

18.10 SFT recipes, data strategies, and parameter updates

Supervised fine-tuning learns from target outputs. Its variants differ in which examples supply those targets, which tokens receive loss, and which parameters can change. Keep those three decisions separate before comparing framework recipes. The SFT implementation supplies chat rendering, assistant masks, and document-aware packing.

Concepts from other sections (6)

This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.

Learning objectives

  • Separate objective, data, parameterization, and execution choices in SFT.
  • Compare completion-only, assistant-only, multi-turn, and weighted training.
  • Validate synthetic and filtered demonstrations without leaking evaluation data.
RecipeTargets and lossUseful whenMain check
Completion-only SFTScore only the target completion after its promptA task has a clear input/output boundaryThe first response token is included after the causal shift
Assistant-only chat SFTScore assistant turns; keep other roles as contextTeach multi-turn assistant behaviorTemplate-specific assistant spans, end markers, and tool-call tokens
Final-turn-only SFTScore only a selected final assistant turnEarlier turns provide context rather than demonstrationsEarlier assistant messages are masked intentionally
All-token causal trainingScore eligible input and output tokensReproduce a complete conversation/document distributionUser, system, and tool text now receive training weight too
Multi-task or domain-mixture SFTMix task demonstrations with chosen sampling weightsAdapt several behaviors in one modelActual valid-token mixture, task interference, and retained capabilities
Weighted SFTAssign unequal nonnegative weights to selected targets/examplesWeight known quality levels or task prioritiesWeight provenance, normalization, and concentration
Dynamic fine-tuning (DFT)Modify token update weighting using the model’s token probabilitiesTest an objective-derived alternative to ordinary SFTExact gradient/stop-gradient convention, weighting, and held-out outcomes
Synthetic-demonstration SFTUse teacher-generated text as targetsHuman demonstrations are scarceIndependent correctness, diversity, provenance, teacher mistakes
Rejection-sampling SFTGenerate candidates and retain those passing a criterionA checker can identify useful demonstrationsSampling cost, selection bias, and rejected/retained counts

Instruction tuning is SFT on instruction/response demonstrations; it is not a separate optimizer. Domain-adaptive continued pretraining usually retains a language-modeling objective on domain documents, while domain SFT uses task targets. A recipe can combine them, but the data and objective should remain identifiable.

Dynamic fine-tuning proposes probability-dependent rescaling of token updates. The versioned TRL SFT reference exposes it through loss_type="dft". This changes the learning objective/gradient convention; it is not an execution flag that promises equivalent SFT behavior or universally better generalization.

SFT can also imitate tool use and reasoning demonstrations. Decide which tool-call arguments, tool observations, reasoning spans, and final answers should be targets. If an inference system hides or reformats a span, verify the actual serialized training/serving contract. A model cannot learn a reliable tool interface from a mask that accidentally removes every tool-call token.

Let xjx_j be token jj, mjm_j select target tokens, and aj≥0a_j\ge0 be their weights. For one sequence, a token-weighted causal loss is

L=−∑j=1T−1mjajlog⁡πθ(xj∣x<j)∑j=1T−1mjaj.L=-\frac{\sum_{j=1}^{T-1}m_j a_j\log\pi_\theta(x_j\mid x_{<j})}{\sum_{j=1}^{T-1}m_j a_j}.

The logits at input position j−1j-1 score target position jj. Require a positive denominator or skip/report the example according to a declared policy. A numerical denominator clamp does not turn an example with no targets into useful training evidence. Padding and packed-document boundaries require their own masks.

For two target losses 1 and 3 with weights 1 and 3, the weighted mean is (1+9)/4=2.5(1+9)/4=2.5, not the unweighted mean 2. Weighting a demonstration more heavily does not change whether it is correct. Across unequal micro-batches or data-parallel ranks, aggregate loss sums and denominators with the reducer’s averaging convention accounted for.

Parameter strategyTrainable stateRelationship to the objective
Full fine-tuningAll selected base-model parametersCan use any of the SFT masks/data recipes above
LoRALow-rank updates on selected matricesChanges the parameterization, not the learning signal
QLoRAAdapters with a quantized frozen baseChanges base storage and execution as well as adapter training
Other selective updatesHeads, embeddings, biases, selected layers, or other adaptersCapacity, memory, and forgetting must be measured

See parameter-efficient fine-tuning for LoRA/QLoRA/DoRA mechanics. A preference or RL objective can also train adapters. “SFT versus LoRA” therefore compares different axes of a recipe. Combining several adapters or merging one into base weights requires the same post-export checks as a full fine-tune.

Split prompts by source, task, customer, or time when those boundaries matter. Deduplicate related conversations before splitting. If a teacher generates or a judge filters training targets, keep the held-out answers out of both generation instructions and filtering decisions. Log how many examples each stage drops and why.

Before a long run, render a few examples and inspect token IDs, target masks, valid-token counts, and document IDs. Include a one-token answer, an empty answer, Unicode, several assistant turns, and a tool interaction. Check that an intentionally selected target changes loss and gradient while a masked target does not. Packing or changing padding should preserve the declared example boundaries and weighting.

Track training loss, held-out task outcomes, length, stop behavior, format/tool success, and regressions on retained tasks. Compare a new SFT recipe with the same base model and evaluation workload. Lower imitation loss can mean better copying without better real task performance.

  • Completion-only loss: loss restricted to a designated target completion after context.
  • Final-turn-only loss: loss restricted to a selected final response within a conversation.
  • Rejection-sampling SFT: supervised training on generated examples retained by an explicit selection rule.

Specify target source, token weighting, trainable parameters, and execution separately. Then validate the complete recipe after export to the serving runtime.

  1. Does assistant-only SFT necessarily train every assistant turn?

    AnswerNo. A recipe may select all assistant turns or only particular turns; its mask must make that choice explicit.
  2. Can rejection sampling replace independent evaluation?

    AnswerNo. It selects data using a criterion that can be incomplete or exploitable. Evaluate on held-out outcomes outside that selection rule.

TRL 0.23.1 SFT documentation describes completion/assistant masks and packing controls for one implementation. InstructGPT documents supervised demonstrations within an RLHF pipeline. Distillation data and reward design explain synthetic targets and selection. The weighted-loss example is checked in learning checks.