SFT Sample Packing with Loss Masks

Asked at Microsoft · MLOps & Deployment · Difficulty: Medium · Last asked Apr 19, 2026

Supervised fine-tuning of a language model on short examples wastes compute if every example is padded to the full context length. Sample packing fixes this: several examples are concatenated into one fixed-length training sequence, and a loss mask makes sure the model is trained only on answer tokens. Implement the packing step.

Loading the problem…