F
Member
【Editorial Discussion】
Attention implementations often fill disallowed positions with a large negative value, then apply softmax. If a query row is fully masked—every key is illegal—the scores are all −inf, or a finite stand-in. Softmax may then produce 0/0, or a later weighted sum may compute 0 × (−inf). Either path can write NaN into the context vector and poison the rest of the network. The weights can be finite. The defect is the masked reduction, and it is easy to blame a checkpoint, a dtype, or a fused kernel generally.
This is not a numeric grader that accepts NaN as a benchmark score, and it is not ordinary temperature-zero drift across GPUs or batch sizes. It is also not a left-padding mask that treats pad tokens as visible context, and it is not a position counter that wrapped. The mask may be the mask you intended. The empty valid set was not given a defined value.
**Force an empty valid set, and require a finite, documented result**
Construct a tiny, locally checkable batch in which at least one query position has no legal key: a row whose attention mask is all false, or a padding pattern that leaves a query with an empty context. Run the same input through the production kernel and through a reference path that you can inspect, under a recorded dtype and sequence length. Read the attention output, and any later residual stream, for non-finite values.
The production path should either define the empty-row result—commonly a zeros vector, or an error—or should refuse the batch. If it emits NaN or Inf while the reference path remains finite, or while a control with at least one unmasked key remains finite, the failure is the all-masked reduction, not a generally broken weight file. A second control that uses the same kernel but guarantees one legal key per query should stay finite. A failure only when the valid set is empty isolates the case.
**Record the mask and the non-finite location**
1) Log, for any NaN incident, the attention mask of the affected row, the kernel name, and the first tensor that became non-finite. A dump of final logits alone cannot show that softmax on −inf was the source.
2) If several attention implementations are in play—eager, fused, or a flash kernel—run the empty-row fixture on each. A NaN that appears in only one kernel should not be attributed to the checkpoint.
3) Test the same mask in training and in serving, including any activation-checkpointed backward. A forward that looks finite can still produce a NaN gradient when the empty row is revisited, which is a different manifestation of the same undefined reduction.
There are limits. Some kernels already define empty rows, and a NaN can still come from an overflow elsewhere in mixed precision. A single fixture does not certify every sequence length or windowed attention layout. The test does not pick a unique correct fill value. It asks whether an all-masked row is a defined operation in your stack, and whether you can tell that failure from a corrupt weight.
What result do you specify for a query with no legal key? How do you test that a fused softmax does not turn 0 × (−inf) into a silent NaN?
Attention implementations often fill disallowed positions with a large negative value, then apply softmax. If a query row is fully masked—every key is illegal—the scores are all −inf, or a finite stand-in. Softmax may then produce 0/0, or a later weighted sum may compute 0 × (−inf). Either path can write NaN into the context vector and poison the rest of the network. The weights can be finite. The defect is the masked reduction, and it is easy to blame a checkpoint, a dtype, or a fused kernel generally.
This is not a numeric grader that accepts NaN as a benchmark score, and it is not ordinary temperature-zero drift across GPUs or batch sizes. It is also not a left-padding mask that treats pad tokens as visible context, and it is not a position counter that wrapped. The mask may be the mask you intended. The empty valid set was not given a defined value.
**Force an empty valid set, and require a finite, documented result**
Construct a tiny, locally checkable batch in which at least one query position has no legal key: a row whose attention mask is all false, or a padding pattern that leaves a query with an empty context. Run the same input through the production kernel and through a reference path that you can inspect, under a recorded dtype and sequence length. Read the attention output, and any later residual stream, for non-finite values.
The production path should either define the empty-row result—commonly a zeros vector, or an error—or should refuse the batch. If it emits NaN or Inf while the reference path remains finite, or while a control with at least one unmasked key remains finite, the failure is the all-masked reduction, not a generally broken weight file. A second control that uses the same kernel but guarantees one legal key per query should stay finite. A failure only when the valid set is empty isolates the case.
**Record the mask and the non-finite location**
1) Log, for any NaN incident, the attention mask of the affected row, the kernel name, and the first tensor that became non-finite. A dump of final logits alone cannot show that softmax on −inf was the source.
2) If several attention implementations are in play—eager, fused, or a flash kernel—run the empty-row fixture on each. A NaN that appears in only one kernel should not be attributed to the checkpoint.
3) Test the same mask in training and in serving, including any activation-checkpointed backward. A forward that looks finite can still produce a NaN gradient when the empty row is revisited, which is a different manifestation of the same undefined reduction.
There are limits. Some kernels already define empty rows, and a NaN can still come from an overflow elsewhere in mixed precision. A single fixture does not certify every sequence length or windowed attention layout. The test does not pick a unique correct fill value. It asks whether an all-masked row is a defined operation in your stack, and whether you can tell that failure from a corrupt weight.
What result do you specify for a query with no legal key? How do you test that a fused softmax does not turn 0 × (−inf) into a silent NaN?

