TECHNOLOGY

Stable softmax: why subtracting a maximum preserves probabilities

In this article
THE QUICK TAKE

A common shift cancels in the exact softmax ratio, allowing a numerically safer calculation without changing the intended probability distribution.

Neural logits can have magnitudes that make a direct exponential calculation unreliable in finite precision. Stable softmax uses an algebraic property of the normalized ratio to change intermediate values while preserving the intended probabilities. This is numerical implementation, not a change to the model’s candidate ordering.

The same shift applies to every eligible score

Softmax exponentiates scores and divides each exponential by their sum. Subtracting the same finite constant from every participating score multiplies numerator and denominator by the same factor, which cancels in exact arithmetic. Choosing the maximum makes the largest shifted score zero, avoiding unnecessarily large positive exponentials. The rule is shared across the normalization set, not chosen separately for each candidate. Candidate-specific shifts would alter relative scores and therefore describe a different distribution rather than the same stable computation.

Finite precision makes intermediate values important

Even when the exact final probability is well defined, an unstable sequence of floating-point operations can overflow or lose useful information before normalization. Rewriting the calculation changes those intermediates. Data type, reduction behavior and treatment of excluded candidates remain relevant. Stability is therefore a property of the complete numerical path, not a guarantee that every tiny probability will be represented exactly. A comparison should state the precision and tolerance appropriate to the application rather than demand impossible agreement with real-number arithmetic.

Log probabilities deserve their own stable path

Computing softmax and then taking a logarithm can lose information if a small probability rounded to zero. A direct log-softmax formulation avoids that particular intermediate representation. The official PyTorch interface documents this distinction. Cross-entropy implementations can work from logits and use a suitable combined calculation, so feeding already normalized probabilities where logits are expected changes the objective. This is why API input contracts matter alongside algebra: familiar operation names do not make all arrangements mathematically or numerically equivalent.

Use shift invariance as one focused check

A proposed test compares softmax outputs for a score vector and the same vector plus a common offset under suitable numerical conditions. Then separately test masked rows and the application’s expected empty-row handling. Passing the ordinary shift test does not define what an entirely excluded row means. Nor does a normalized distribution certify factual confidence in generated text. The practical outcome is a stable representation of the chosen mathematical distribution, which must still be connected to valid candidates, a correct objective and the model’s downstream task.

Primary research and implementation references

Reproducible inference stacks: preserving a GPU pilot’s result; A GPU inference benchmark you can trust.

Earn rental income from GPUs

Have idle GPUs or looking to fund AI infrastructure? Retrivium finds a rental platform, sets up the server and helps you assess income and costs with a pilot.

Explore GPU hosting
← All articles