# SFT Trainer [![All_models-SFT-blue](https://img.shields.io/badge/All_models-SFT-blue)](https://huggingface.co/models?other=sft,trl) [![smol_course-Chapter_1-yellow](https://img.shields.io/badge/smol_course-Chapter_1-yellow)](https://huggingface.co/learn/smol-course/unit1/1) ## Overview TRL supports the Supervised Fine-Tuning (SFT) Trainer for training language models. [`SFTTrainer`] doesn't natively support diffusion models like DiffusionGemma, but it can be easily extended to do so, see the [block-diffusion SFT example](https://github.com/huggingface/trl/blob/main/examples/sft_diffusion_gemma/sft_diffusion_gemma.py). This post-training method was contributed by [Younes Belkada](https://huggingface.co/ybelkada). ## Quick start This example demonstrates how to train a language model using the [`SFTTrainer`] from TRL. We train a [Qwen 3 0.6B](https://huggingface.co/Qwen/Qwen3-0.6B) model on the [Capybara dataset](https://huggingface.co/datasets/trl-lib/Capybara), a compact, diverse multi-turn dataset to benchmark reasoning and generalization. ```python from trl import SFTTrainer from datasets import load_dataset trainer = SFTTrainer( model="Qwen/Qwen3-0.6B", train_dataset=load_dataset("trl-lib/Capybara", split="train"), ) trainer.train() ``` ## Expected dataset type and format SFT supports both [language modeling](dataset_formats#language-modeling) and [prompt-completion](dataset_formats#prompt-completion) datasets. The [`SFTTrainer`] is compatible with both [standard](dataset_formats#standard) and [conversational](dataset_formats#conversational) dataset formats. When provided with a conversational dataset, the trainer will automatically apply the chat template to the dataset. ```python # Standard language modeling {"text": "The sky is blue."} # Conversational language modeling {"messages": [{"role": "user", "content": "What color is the sky?"}, {"role": "assistant", "content": "It is blue."}]} # Standard prompt-completion {"prompt": "The sky is", "completion": " blue."} # Conversational prompt-completion {"prompt": [{"role": "user", "content": "What color is the sky?"}], "completion": [{"role": "assistant", "content": "It is blue."}]} ``` If your dataset is not in one of these formats, you can preprocess it to convert it into the expected format. Here is an example with the [FreedomIntelligence/medical-o1-reasoning-SFT](https://huggingface.co/datasets/FreedomIntelligence/medical-o1-reasoning-SFT) dataset: ```python from datasets import load_dataset dataset = load_dataset("FreedomIntelligence/medical-o1-reasoning-SFT", "en") def preprocess_function(example): return { "prompt": [{"role": "user", "content": example["Question"]}], "completion": [ {"role": "assistant", "content": f"{example['Complex_CoT']}{example['Response']}"} ], } dataset = dataset.map(preprocess_function, remove_columns=["Question", "Response", "Complex_CoT"]) print(next(iter(dataset["train"]))) ``` ```json { "prompt": [ { "content": "Given the symptoms of sudden weakness in the left arm and leg, recent long-distance travel, and the presence of swollen and tender right lower leg, what specific cardiac abnormality is most likely to be found upon further evaluation that could explain these findings?", "role": "user", } ], "completion": [ { "content": "Okay, let's see what's going on here. We've got sudden weakness [...] clicks into place!The specific cardiac abnormality most likely to be found in [...] the presence of a PFO facilitating a paradoxical embolism.", "role": "assistant", } ], } ``` ## Looking deeper into the SFT method 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. This section breaks down how SFT works in practice, covering the key steps: **preprocessing**, **tokenization** and **loss computation**. ### Preprocessing and tokenization During training, each example is expected to contain a **text field** or a **(prompt, completion)** pair, depending on the dataset format. For more details on the expected formats, see [Dataset formats](dataset_formats). The [`SFTTrainer`] tokenizes each input using the model's tokenizer. If both prompt and completion are provided separately, they are concatenated before tokenization. ### Computing the loss ![sft_figure](https://huggingface.co/datasets/trl-lib/documentation-images/resolve/main/sft_figure.png) The loss used in SFT is the **token-level cross-entropy loss**, defined as: $$ \mathcal{L}_{\text{SFT}}(\theta) = - \sum_{t=1}^{T} \log p_\theta(y_t \mid y_{ [!TIP] > The paper [On the Generalization of SFT: A Reinforcement Learning Perspective with Reward Rectification](https://huggingface.co/papers/2508.05629) proposes an alternative loss function, called **Dynamic Fine-Tuning (DFT)**, which aims to improve generalization by rectifying the reward signal. This method can be enabled by setting `loss_type="dft"` in the [`SFTConfig`]. For more details, see [Paper Index - Dynamic Fine-Tuning](paper_index#on-the-generalization-of-sft-a-reinforcement-learning-perspective-with-reward-rectification). ### Label shifting and masking During training, the loss is computed using a **one-token shift**: the model is trained to predict each token in the sequence based on all previous tokens. Specifically, the input sequence is shifted right by one position to form the target labels. Padding tokens (if present) are ignored in the loss computation by applying an ignore index (default: `-100`) to the corresponding positions. This ensures that the loss focuses only on meaningful, non-padding tokens. ## Logged metrics While training and evaluating, we record the following metrics: - `global_step`: The total number of optimizer steps taken so far. - `epoch`: The current epoch number, based on dataset iteration. - `num_tokens`: The total number of tokens processed so far. - `loss`: The average cross-entropy loss computed over non-masked tokens in the current logging interval. - `entropy`: The average entropy of the model's predicted token distribution over non-masked tokens. - `aux_loss`: The load-balancing auxiliary loss of a Mixture-of-Experts model, before it is scaled by `router_aux_loss_coef` and added to the loss. Logged only when the model is a MoE model and `router_aux_loss_coef` is nonzero. - `mean_token_accuracy`: The proportion of non-masked tokens for which the model’s top-1 prediction matches the ground truth token. - `learning_rate`: The current learning rate, which may change dynamically if a scheduler is used. - `grad_norm`: The L2 norm of the gradients, computed before gradient clipping. ## Customization ### Model initialization You can directly pass the kwargs of the [`~transformers.AutoModelForCausalLM.from_pretrained()`] method to the [`SFTConfig`]. For example, if you want to load a model in a different precision, analogous to ```python model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen3-0.6B", dtype=torch.bfloat16) ``` you can do so by passing the `model_init_kwargs={"dtype": torch.bfloat16}` argument to the [`SFTConfig`]. ```python from trl import SFTConfig training_args = SFTConfig( model_init_kwargs={"dtype": torch.bfloat16}, ) ``` Note that all keyword arguments of [`~transformers.AutoModelForCausalLM.from_pretrained()`] are supported. ### Packing [`SFTTrainer`] supports _example packing_, where multiple examples are packed in the same input sequence to increase training efficiency. To enable packing, simply pass `packing=True` to the [`SFTConfig`] constructor. ```python training_args = SFTConfig(packing=True) ``` For more details on packing, see [Packing](reducing_memory_usage#packing). ### Train on assistant messages only To train on assistant messages only, use a [conversational](dataset_formats#conversational) dataset and set `assistant_only_loss=True` in the [`SFTConfig`]. This setting ensures that loss is computed **only** on the assistant responses, ignoring user or system messages. ```python training_args = SFTConfig(assistant_only_loss=True) ``` ![train_on_assistant](https://huggingface.co/datasets/trl-lib/documentation-images/resolve/main/train_on_assistant.png) > [!WARNING] > This functionality requires the chat template to include `{% generation %}` and `{% endgeneration %}` keywords. For known model families (e.g. Qwen3), TRL automatically patches the template when `assistant_only_loss=True`. See [Chat Templates](chat_templates#training-templates) for the full list of bundled training templates. For other models, check that your chat template includes these keywords. See [HuggingFaceTB/SmolLM3-3B](https://huggingface.co/HuggingFaceTB/SmolLM3-3B/blob/main/chat_template.jinja#L76-L82) for an example. > [!NOTE] > With a [vision dataset](#training-vision-language-models), assistant-only loss requires `transformers>=5.18.0` and a conversational [language modeling](dataset_formats#language-modeling) dataset. ### Train on completion only To train on completion only, use a [prompt-completion](dataset_formats#prompt-completion) dataset. By default, the trainer computes the loss on the completion tokens only, ignoring the prompt tokens. If you want to train on the full sequence, set `completion_only_loss=False` in the [`SFTConfig`]. ```python from trl import SFTConfig, SFTTrainer from datasets import load_dataset # Load a prompt-completion dataset; loss is computed on the completion only by default dataset = load_dataset("trl-lib/kto-mix-14k", split="train") trainer = SFTTrainer( model="Qwen/Qwen2.5-0.5B-Instruct", args=SFTConfig(completion_only_loss=True), # True by default for prompt-completion datasets train_dataset=dataset, ) trainer.train() ``` ![train_on_completion](https://huggingface.co/datasets/trl-lib/documentation-images/resolve/main/train_on_completion.png) > [!TIP] > Training on completion only is compatible with training on assistant messages only. In this case, use a [conversational](dataset_formats#conversational) [prompt-completion](dataset_formats#prompt-completion) dataset and set `assistant_only_loss=True` in the [`SFTConfig`]. ### Train adapters with PEFT We support tight integration with 🤗 PEFT library, allowing any user to conveniently train adapters and share them on the Hub, rather than training the entire model. ```python from datasets import load_dataset from trl import SFTTrainer from peft import LoraConfig dataset = load_dataset("trl-lib/Capybara", split="train") trainer = SFTTrainer( "Qwen/Qwen3-0.6B", train_dataset=dataset, peft_config=LoraConfig(), ) trainer.train() ``` You can also continue training your [`~peft.PeftModel`]. For that, first load a `PeftModel` outside [`SFTTrainer`] and pass it directly to the trainer without the `peft_config` argument being passed. ```python from datasets import load_dataset from trl import SFTTrainer from peft import AutoPeftModelForCausalLM model = AutoPeftModelForCausalLM.from_pretrained("trl-lib/Qwen3-4B-LoRA", is_trainable=True) dataset = load_dataset("trl-lib/Capybara", split="train") trainer = SFTTrainer( model=model, train_dataset=dataset, ) trainer.train() ``` > [!TIP] > When training adapters, you typically use a higher learning rate (≈1e‑4) since only new parameters are being learned. > > ```python > SFTConfig(learning_rate=1e-4, ...) > ``` ### Rapid Experimentation for SFT RapidFire AI is an open-source experimentation engine that sits on top of TRL and lets you launch multiple SFT configurations at once, even on a single GPU. Instead of trying configurations sequentially, RapidFire lets you **see all their learning curves earlier, stop underperforming runs, and clone promising ones with new settings in flight** without restarting. For more information, see [RapidFire AI Integration](rapidfire_integration). ### Train with Unsloth Unsloth is an open‑source framework for fine‑tuning and reinforcement learning that trains LLMs (like Llama, Mistral, Gemma, DeepSeek, and more) up to 2× faster with up to 70% less VRAM, while providing a streamlined, Hugging Face–compatible workflow for training, evaluation, and deployment. For more information, see [Unsloth Integration](unsloth_integration). ## Instruction tuning example **Instruction tuning** teaches a base language model to follow user instructions and engage in conversations. This requires: 1. **Chat template**: Defines how to structure conversations into text sequences, including role markers (user/assistant), special tokens, and turn boundaries. Read more about chat templates in [Chat templates](https://huggingface.co/docs/transformers/chat_templating#templates). 2. **Conversational dataset**: Contains instruction-response pairs This example shows how to transform the [Qwen 3 0.6B Base](https://huggingface.co/Qwen/Qwen3-0.6B-Base) model into an instruction-following model using the [Capybara dataset](https://huggingface.co/datasets/trl-lib/Capybara) and a chat template from [HuggingFaceTB/SmolLM3-3B](https://huggingface.co/HuggingFaceTB/SmolLM3-3B). The SFT Trainer automatically handles tokenizer updates and special token configuration. ```python from trl import SFTConfig, SFTTrainer from datasets import load_dataset trainer = SFTTrainer( model="Qwen/Qwen3-0.6B-Base", args=SFTConfig( output_dir="Qwen3-0.6B-Instruct", chat_template_path="HuggingFaceTB/SmolLM3-3B", ), train_dataset=load_dataset("trl-lib/Capybara", split="train"), ) trainer.train() ``` > [!WARNING] > Some base models, like those from Qwen, have a predefined chat template in the model's tokenizer. In these cases, it is not necessary to apply [`clone_chat_template()`], as the tokenizer already handles the formatting. However, it is necessary to align the EOS token with the chat template to ensure the model's responses terminate correctly. In these cases, specify `eos_token` in [`SFTConfig`]; for example, for `Qwen/Qwen2.5-1.5B`, one should set `eos_token="<|im_end|>"`. Once trained, your model can now follow instructions and engage in conversations using its new chat template. ```python >>> from transformers import pipeline >>> pipe = pipeline("text-generation", model="Qwen3-0.6B-Instruct/checkpoint-5000") >>> prompt = "<|im_start|>user\nWhat is the capital of France? Answer in one word.<|im_end|>\n<|im_start|>assistant\n" >>> response = pipe(prompt) >>> response[0]["generated_text"] '<|im_start|>user\nWhat is the capital of France? Answer in one word.<|im_end|>\n<|im_start|>assistant\nThe capital of France is Paris.' ``` Alternatively, use the structured conversation format (recommended): ```python >>> prompt = [{"role": "user", "content": "What is the capital of France? Answer in one word."}] >>> response = pipe(prompt) >>> response[0]["generated_text"] [{'role': 'user', 'content': 'What is the capital of France? Answer in one word.'}, {'role': 'assistant', 'content': 'The capital of France is Paris.'}] ``` ## Tool Calling with SFT The [`SFTTrainer`] fully supports fine-tuning models with _tool calling_ capabilities. In this case, each dataset example should include: - The conversation messages, including any tool calls (`tool_calls`) and tool responses (`tool` role messages) - The list of available tools in the `tools` column, typically provided as JSON schemas For details on the expected dataset structure, see the [Dataset Format — Tool Calling](dataset_formats#tool-calling) section. ## Training Vision Language Models [`SFTTrainer`] fully supports training Vision-Language Models (VLMs). To train a VLM, provide a dataset with either an `image` column (single image per sample) or an `images` column (list of images per sample). For more information on the expected dataset structure, see the [Dataset Format — Vision Dataset](dataset_formats#vision-dataset) section. An example of such a dataset is the [LLaVA Instruct Mix](https://huggingface.co/datasets/trl-lib/llava-instruct-mix). ```python from trl import SFTConfig, SFTTrainer from datasets import load_dataset trainer = SFTTrainer( model="Qwen/Qwen2.5-VL-3B-Instruct", args=SFTConfig(max_length=None), train_dataset=load_dataset("trl-lib/llava-instruct-mix", split="train"), ) trainer.train() ``` > [!TIP] > For VLMs, truncating may remove image tokens, leading to errors during training. To avoid this, set `max_length=None` in the [`SFTConfig`]. This allows the model to process the full sequence length without truncating image tokens. > > ```python > SFTConfig(max_length=None, ...) > ``` > > Only use `max_length` when you've verified that truncation won't remove image tokens for the entire dataset. ## SFTTrainer [[autodoc]] SFTTrainer - train - save_model - push_to_hub ## SFTConfig [[autodoc]] SFTConfig