The recommended path
TRL's SFTTrainer is the recommended way to do supervised fine-tuning. It handles tokenization, formatting, loss masking, chat templates, and the training loop in one clean API. You provide the model, the LoRA config, the dataset, and an SFTConfig — the trainer does the rest.
Key SFTConfig parameters
| Parameter | What it controls | Typical value |
|---|---|---|
max_seq_length | Max sequence length for training. | 1024–4096 |
packing | Pack multiple short examples into one sequence. | True — saves training time |
dataset_text_field | Which field contains the text. | None for auto-detect on chat format |
gradient_checkpointing | Recompute activations instead of storing. | True for large models |
bf16 | Use bfloat16 mixed precision. | True on Ampere+ GPUs |