Ran Wei/ Maths for AI and LLMs
中文
Maths for AI and LLMs · Numerical computing

Module 7Floating-point arithmetic and stable softmax

Evaluate softmax and a target log probability after shifting logits away from overflow.

70 minYou need Module 4 and exponentials

In this module you will

  • Explain why a finite float can lose a small addend
  • Derive a shifted log-sum-exp expression
  • Compute a target log probability without taking the logarithm of a rounded softmax probability
1

Rounding and range

Finite floating-point formats round most real numbers to nearby representable values. In binary64, the next value above 11 is 1+2−52.1+2^{-52}\mathclose{\text{.}} Under round-to-nearest, adding 2−542^{-54} to 11 leaves the rounded result at 1.1\mathclose{\text{.}} At a larger magnitude the gap between adjacent numbers grows. Floating point also has finite exponent range. A large exponential can overflow, and a sufficiently small positive exponential can round to zero.

In a mixed-precision computation, format choices may differ for storage, products and accumulation. Accumulating reduced-precision products in wider precision can reduce additional rounding in the sum. It cannot restore bits already lost when values were converted to a narrower format.

2

Shift before exponentiating

Define LSE⁡(z)=ln⁡∑iezi\operatorname{LSE}(z)=\ln\sum_i e^{z_i} for a nonempty vector of finite logits. Set m=max⁡izi.m=\max_i z_i\mathclose{\text{.}} Factor eme^m out of the sum to obtain

LSE⁡(z)=m+ln⁡∑iezi−m,log⁡qj=(zj−m)−ln⁡∑iezi−m.\operatorname{LSE}(z)=m+\ln\sum_i e^{z_i-m}, \qquad \log q_j=(z_j-m)-\ln\sum_i e^{z_i-m}.

The shifted exponentials are at most one, and at least one equals one. This avoids exponential overflow for finite inputs. Use the second expression directly when you need the log probability. Subtracting a rounded m+ln⁡∑iezi−mm+\ln\sum_i e^{z_i-m} from a very large zjz_j can lose precision. Also, taking the log of a rounded softmax result can fail when a tiny probability rounds to zero. Extremely large differences between finite logits can themselves exceed the floating-point range.

Example 7.1Large but close logits✓ Checked

For logits (1000,1001),(1000,1001)\mathclose{\text{,}} subtract 10011001 to obtain (−1,0).(-1,0)\mathclose{\text{.}} The second token has probability 1/(1+e−1)≈0.73111/(1+e^{-1})\approx0.7311 and log probability −ln⁡(1+e−1)≈−0.3133.-\ln(1+e^{-1})\approx-0.3133\mathclose{\text{.}} The direct exponential of 10011001 overflows in binary64.

Compute itCompute in shifted coordinates

Run the same calculation on a second vector whose logits are separated by one thousand. The smaller log probability remains finite even though its exponential rounds to zero.

Python
from math import exp, fsum, log

for logits, target in [([1000.0, 1001.0], 1), ([0.0, -1000.0], 1)]:
    largest = max(logits)
    shifted = [value - largest for value in logits]
    log_normalizer = log(fsum(exp(value) for value in shifted))
    print(f"{shifted[target] - log_normalizer:.4f}")
Output
-0.3133
-1000.0000
3

Exercises

Exercise 7.1★Equal logits

Find the probability assigned to one of four equal finite logits.

Show solution✓ Checked

After shifting, every exponential equals one. Each probability is 1/4.1/4\mathclose{\text{.}}

Exercise 7.2★★Shift by a constant

What happens to LSE⁡(z)\operatorname{LSE}(z) and softmax if you add 77 to every logit?

Show solution✓ Checked

Factoring out e7e^7 adds 77 to log-sum-exp. It cancels between numerator and denominator of each softmax probability.

Exercise 7.3★★★A tiny target probability

For logits (0,−1000),(0,-1000)\mathclose{\text{,}} give a bound on how far the first token’s negative log probability lies above zero.

Show solution✓ Checked

It equals ln⁡(1+e−1000).\ln(1+e^{-1000})\mathclose{\text{.}} For u>0,u>0\mathclose{\text{,}} 0<ln⁡(1+u)<u,0<\ln(1+u)<u\mathclose{\text{,}} so the loss is strictly between 00 and e−1000e^{-1000} in exact arithmetic. A float can round it to zero.

4

Check yourself

1
Why subtract the largest logit?
2
Which method can still return a finite log probability when softmax underflows?
3
What does a wider accumulator change?
Finished this module?