Model and Inference EngineeringTraining data and objectivesStaff
How does averaging loss per example change the weight of short and long answers?
By Amit Kumar3 min readWorked answer includedReviewed 28 Sept 2026
The question
Interview question
A trainer averages supervised token losses within each answer and then averages across answers. One answer has 20 valid target tokens and another has 2,000. Derive each token’s weight, compare with a global token mean, and explain how prompt masks, packing and gradient accumulation can change the intended task objective. Use a numerical loss example.
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
I would ask whether "equal weight" means equal example weight or equal token weight. Those are different objectives. Suppose we average token loss inside each answer, then average the two answer losses. Every token in the 20-token answer contributes one twentieth of half the batch loss. Every token in the 2,000-token answer contributes one two-thousandth of half. A short-answer token has 100 times the weight of a long-answer token. That may be intentional if each user task should count equally, but it is not the same as treating every supervised token equally.
If instead we sum losses over all nonignored target positions and divide by the total 2,020 positions, each token contributes equally. The long answer then has 100 times the total weight of the short one. PyTorch's cross-entropy documentation defines how reduction and ignored targets affect the denominator. A custom loop can add a second mean across examples after already computing per-example means, so inspect the actual tensors and masks rather than relying on a config label.
For an instruction fine-tune, neither objective wins automatically. Per-example weighting prevents one very long response from dominating a batch of short requests. Per-token weighting reflects the number of supervised positions and may better match next-token likelihood. The choice also interacts with how much of a long answer is truncated, how many prompt and tool tokens are masked, and whether a batch packs multiple examples together. I would specify the desired population objective, then compare the effective contribution by task type and length bucket.
A useful check is a two-example microbatch with known token losses. Compute the expected scalar and gradient contribution by hand, then compare with the trainer. Record total valid targets, number of examples and the reduction applied at every stage. Ranks process different token counts. Why does averaging their losses change the objective? concerns uneven token counts across distributed ranks after local averaging. Here one rank can be perfectly implemented and still give short examples much greater per-token influence because of the chosen per-example objective.
Suppose every supervised token in the short answer has loss 2 and every token in the long answer has loss 1. The per-example objective is(2+1)/2=1.5. The token mean is(20×2+2,000×1)/2,020, about 1.0099. Neither number is a better implementation of the other. They estimate differently weighted objectives. The first gives a short user task the same total influence as a long one, while the second gives each valid target position the same scalar coefficient.
The gradient must follow that intended denominator too. If microbatches contain different counts of valid targets, averaging each microbatch’s token mean equally will not generally equal a token mean across the full accumulated batch. Accumulate a loss sum with the appropriate total valid-token normalization, or use mathematically equivalent scaling compatible with distributed gradient reduction. Record which parameters and masks contribute before applying clipping or an optimizer update.
Packing introduces another trap. A packed sequence is a storage and compute unit, not automatically one user example. If three tasks share a packed row, a mean over packed rows changes task weights unless their boundaries and denominators are retained. Conversely, computing each example’s mean requires counting its supervised completion and tool targets, not padding or masked prompt tokens. Include an all-ignored example in the fixture and define whether it is excluded or rejected so an empty denominator cannot silently produce a nonfinite loss.
Finally compare quality by task type and answer length. A global improvement can hide a regression in short extraction tasks or long tool explanations if their weighting changed. The release decision should describe the objective the trainer now implements, with the numerical fixture as an alignment check rather than evidence that one weighting is universally best.
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