Skip to main content

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

To launch distributed training:

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.
If your dataset uses different column names, preprocess it to match the expected format:

Key configuration parameters

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".
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).
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.
bool
default:"false"
Perform forward passes without padding by flattening all sequences into a single continuous sequence. Requires FlashAttention 2 or 3. Automatically enabled when packing="bfd".
bool
default:"false"
Offload activations to CPU to reduce GPU memory usage.
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:
Some base models (such as Qwen models) already have a chat template in the tokenizer. In that case, you do not need to set chat_template_path, but you should align the EOS token. For example, for Qwen/Qwen2.5-1.5B, set eos_token="<|im_end|>" in SFTConfig.

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.
For best performance with packing, use FlashAttention 2 or 3. The padding_free option is automatically enabled with packing_strategy="bfd", eliminating padding overhead entirely.

Training with PEFT/LoRA

Use the PEFT library to train only a small set of adapter parameters instead of the full model:
To continue training an existing PEFT model:
When training adapters, use a higher learning rate (around 1e-4) since only the new adapter parameters are being learned.

Training Vision-Language Models

SFTTrainer supports VLMs. Provide a dataset with an image column (single image) or images column (list of images):
For VLMs, set max_length=None to prevent truncation from removing image tokens, which causes errors during training.

Logged metrics