Model and Inference EngineeringTraining data and objectivesStaff
How can padding masks prevent a fine-tuned assistant from learning its response terminator?
By Amit Kumar3 min readWorked answer includedReviewed 28 Sept 2026
The question
Interview question
An instruction fine-tune includes a terminator in every assistant response, but serving runs to the token limit. Its padding ID equals the end-of-sequence ID. Trace labels after collation and shifting, distinguish padding positions from genuine terminator targets, and design a repair and checks that also rule out serving-template or stop-rule errors.
Draft your approach first
Take a few minutes before you read the answer. Name the constraint that matters most, the failure you are designing for and how you would know it worked.
Reveal the worked answerWorked answer
A stop marker can appear in the input without ever being a supervised target. Training only penalizes predictions at positions whose labels contribute to the loss. If preprocessing removes the real response terminator from those targets, lower loss on the remaining answer text does not establish that the assistant learned when to end.
Direct supervision to predict a terminator requires that its target contribute to the next-token loss. Many pipelines use -100 to ignore a position. Suppose the tokenizer uses the EOS token ID as its padding ID, and a data collator masks every label with that ID to -100. It just removed the true EOS targets along with the padding. The EOS still appears in input_ids, so the model may learn to continue after it, but gets no direct gradient to predict it at the end of an answer. A Transformers collator implementation has used token-ID-based padding masks, and a project issue documents this exact failure when pad and EOS IDs coincide. Check the specific library version and your preprocessing rather than assuming every trainer has the bug.
For a hypothetical token fixture before the causal label shift, let input IDs be [101,77,2,2], with attention mask [1,1,1,0]. The first 2 ends the actual response and the last 2 is padding. An ID-based rule ignoring every 2 removes both labels. A positional padding mask instead ignores only the final position, preserving the real terminator as a target. The token IDs are illustrative. Inspect the model’s real end-of-turn convention and the loss shift before applying this fixture to a trainer.
I would print a few fully rendered training examples as token IDs, tokens, attention masks and labels after all collation. Mark the final assistant token, real EOS, added padding, user spans and tool spans. How many genuine EOS positions have a nonignored label? Is the loss computed on them after the model's shift? Does a chat template use a distinct end-of-turn token instead of EOS? The expected target depends on the model's generation and stopping contract. Do not add a generic EOS where the architecture expects a different terminator.
Fix the mask by distinguishing actual padding positions from a real EOS token, or by using a separate padding token where supported and updating the tokenizer and embeddings deliberately. Verify on one tiny batch that the probability of the correct terminator rises after a few optimization steps. Then evaluate stop reason, output-length distribution and task success, including multi-turn and tool-call examples. Hard truncation at a token budget can hide this issue while still billing for unnecessary decode and sometimes leaking into the next turn.
What if EOS labels are intact? Then inspect whether the serving template appends a start-of-assistant marker different from training, whether stop IDs are configured correctly, and whether decoding suppresses the terminator. The point is to separate a learned stopping failure from a serving stop-rule failure. Ranks process different token counts. Why does averaging their losses change the objective? covers loss weighting across unequal rank token counts. This question concerns one semantically critical target that the loss may never see.
Related questions
Keep practising model and inference engineering
Follow another question in this area, or search the complete library.
Get my essays on AI engineering and distributed systems by email. Subscribe to ArchCrux