The collator's job
A data collator takes a list of tokenized examples (ragged lengths) and produces a single batch tensor (uniform shape). The standard ones:
DataCollatorWithPadding— padsinput_idsto the longest in the batch.DataCollatorForLanguageModeling— for MLM (masked) or CLM (causal) pre-training.mlm=Truerandomly masks tokens.DataCollatorForSeq2Seq— pads encoder + decoder side independently for encoder-decoder models.DataCollatorForCompletionOnlyLM(fromtrl) — masks the prompt portion so loss only flows over the assistant response.
Why collators matter for SFT
If you train on chat data without a completion-only collator, the model is also learning to predict the user prompt. That's wasted gradient and often degrades chat behavior. The completion-only collator is the right default for instruction tuning.