Overview
Supervised Fine-Tuning (SFT) is the simplest and most commonly used method to adapt a language model to a target dataset. The model is trained in a fully supervised fashion using pairs of input and output sequences. The goal is to minimize the negative log-likelihood (NLL) of the target sequence, conditioning on the input.SFTTrainer supports both language modeling and prompt-completion datasets, and works with standard or conversational dataset formats. When provided with a conversational dataset, the trainer automatically applies the model’s chat template.
Quick start
Dataset format
SFTTrainer accepts four dataset formats:
For prompt-completion datasets, loss is computed only on the completion tokens by default. For language modeling datasets, loss is computed on the full sequence.
Key configuration parameters
Data preprocessing
Data preprocessing
int | None
default:"1024"
Maximum length of the tokenized sequence. Sequences longer than this are truncated. Set to
None to disable truncation (recommended for VLMs).bool
default:"false"
Whether to pack multiple short sequences into fixed-length blocks, improving GPU utilization and reducing padding waste. Uses
max_length to define block size.str
default:"bfd"
Strategy for packing sequences:
"bfd" (best-fit decreasing, truncates overflow), "bfd_split" (best-fit decreasing, splits overflow sequences), or "wrapped" (aggressive, cuts mid-sequence).str
default:"text"
Name of the column containing text data for language modeling datasets.
str
default:"keep_start"
Which end to truncate when a sequence exceeds
max_length. Options: "keep_start" or "keep_end".Loss computation
Loss computation
bool | None
default:"None"
Whether to compute loss only on the completion part. When
None, defaults to True for prompt-completion datasets and False for language modeling datasets.bool
default:"false"
Whether to compute loss only on assistant responses in conversational datasets. Requires a chat template that supports the
{% generation %} and {% endgeneration %} keywords.str
default:"nll"
Type of loss to use. Options:
"nll" (standard negative log-likelihood) or "dft" (Dynamic Fine-Tuning, which rectifies the reward signal to improve generalization).Model initialization
Model initialization
dict | None
Keyword arguments forwarded to
AutoModelForCausalLM.from_pretrained when the model argument is a string. Useful for setting dtype, device_map, or output_router_logits for MoE models.str | None
Path to a tokenizer or a Jinja template file to set as the model’s chat template. Useful when fine-tuning base models that do not have a chat template.
str | None
Token used to indicate end of sequence. Required when the chat template uses a different EOS token than the tokenizer’s default.
SFTConfig also overrides some TrainingArguments defaults: logging_steps=10, gradient_checkpointing=True, bf16=True, and learning_rate=2e-5.Instruction tuning
To turn a base model into an instruction-following model, provide a chat template and a conversational dataset:Dataset packing
Packing is a technique to increase training efficiency by grouping multiple short examples into a single fixed-length block, reducing wasted padding tokens.Training with PEFT/LoRA
Use the PEFT library to train only a small set of adapter parameters instead of the full model:Training Vision-Language Models
SFTTrainer supports VLMs. Provide a dataset with an image column (single image) or images column (list of images):