A cutoff in fp32 and values in bf16 can drop the top token
Here is the smallest version of the failure the post describes. It's an illustration, not Anthropic's code.
One operation takes the maximum from values the compiler kept in fp32. Another compares the bf16 values against that maximum, to keep only the top token. Greedy decoding at temperature zero needs exactly that.
import torch
# kept in fp32
p32 = torch.tensor([0.30103, 0.30090, 0.25000, 0.14807])
# what another op reads
p16 = p32.to(torch.bfloat16)
# 0.30103
top = p32.max()
# keep only the top token
keep = p16.float() >= top
print(keep)
tensor([False, False, False, False])
Every token fails, the top one included. Here is the path each number took:
| The same four probabilities | Reads | Gives |
|---|---|---|
| op 1, the max | fp32 | 0.30103 |
| op 2, the filter | bf16 | A = 0.30078 |
| the cutoff check | 0.30078 ≥ 0.30103? No: A, the top token, is dropped |
Token A fails its own test. Its bf16 value, 0.30078, sits below the fp32 maximum, 0.30103, that it's compared against. Nothing survives the filter. Whatever the code does next, the most likely token is out of consideration.
Swap A and B, and the two precisions disagree about which token is top. The fp32 argmax picks index 1. The bf16 argmax sees a tie and picks index 0.
In the editor, fix keep_top(values): it compares bf16 values against a maximum taken in fp32. to_bf16 is the rounding you just wrote, P32 holds the four probabilities as kept in fp32, and P16 holds them as another operation reads them.