﻿# Averaging sequence losses changes the weight of long examples

Token-mean and sequence-mean losses weight variable-length examples differently. A worked example explains denominators, padding and gradient contributions.

Author: Retrivium editorial team
Updated: 2026-11-03
Canonical URL: https://retrivium.ai/journal/variable-length-loss-reduction/
Language: en

Two training runs can use identical token losses and still optimize different aggregate objectives. The difference can be the denominator. When examples have variable lengths, averaging every valid token and averaging each sequence first are distinct weighting choices. Neither is described adequately by the label mean loss alone.

## Start with an unreduced token ledger

For each sequence, record its included target count and the sum of negative log probabilities for those targets. Ignore artificial padding according to the declared loss mask. These unreduced quantities are enough to reconstruct several aggregate objectives without rerunning the model. Retaining them in a tiny example prevents a library default from silently defining the scientific question. It also separates the decision about which positions count from the later decision about how included positions are weighted relative to other sequences in the batch.

## Compare the two denominators explicitly

Consider a short example with 2 valid targets and total loss 4, and a longer example with 8 valid targets and total loss 8. A global token mean is 12 divided by 10, giving 1.2. Their individual means are 2 and 1; averaging those gives 1.5. Both calculations use the same underlying events. The difference arises because the sequence mean weights examples equally, while the token mean weights every target equally. Reporting either result without its rule obscures a substantive choice.

## Weights also determine the gradient

Loss aggregation is not merely cosmetic when the scalar is used for backpropagation. A longer sequence contributes more terms to a global token-mean gradient. A sequence-mean objective rescales its terms by that sequence’s own valid length before combining examples. Whether equal-example or equal-token weighting is desirable depends on the intended training objective and dataset construction. Document-level sampling and segment splitting interact with this choice: splitting one document into more training examples can change its relative weight under a sequence-based rule.

## Padding should not invent training weight

Appending ignored padding to an example should not change a correctly implemented valid-token mean for the same target events. A denominator that includes padded tensor width can violate that expectation even when the padded losses themselves are zero. Test a short fixture before and after extra padding and reconstruct the intended result from the ledger. When batching or accumulation changes, preserve the same numerator and denominator across the effective objective. This establishes whether an apparent loss improvement is a modeling change or simply a different reporting weight.

## Primary research and implementation references

- [Attention Is All You Need](https://arxiv.org/abs/1706.03762).

- [PyTorch: CrossEntropyLoss](https://docs.pytorch.org/docs/2.15/generated/torch.nn.CrossEntropyLoss.html).

- [PyTorch: Automatic Mixed Precision Examples](https://docs.pytorch.org/docs/2.15/notes/amp_examples.html).

## Related engineering context

[Reproducible inference stacks: preserving a GPU pilot’s result](/journal/software-stack-reproducibility/); [A GPU inference benchmark you can trust](/journal/inference-benchmark-method/).
