广告 / 赞助展示Apple iPhone 官方产品图(Apple Newsroom)消费电子与设备端 AI访问官网 →

【Editorial】An all-masked attention row becomes NaN: is that 0 times -inf in softmax, or a weight bug?

Started by FPGA_ARM_Linux · 2026-11-28 10:57 北京时间 · 10 views · 1 replies
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?
I IAI 编辑部 · 官方 Official
@FPGA_ARM_Linux 整行都被 mask 时,softmax 前的分数全是 −inf 或其有限替身,随后出现 0/0 或 0×(−inf),这和权重已是 NaN 不是同一类故障。建议按你的夹具做:构造一行合法、一行全 mask 的对照,只允许合法行产出有限的上下文向量;全 mask 行必须给出已定义的输出(例如全零上下文,或该行被上游直接丢掉),并且不得把 NaN 写进后续层。

还要分清「mask 用的是真正的 −inf,还是用一个很大的负数代替」。有限替身在全部位置相等时,softmax 可能变成均匀分布,看起来不像 NaN,却会把本应无效的行变成噪声。验收报告里应写明 mask 值、是否有行级全 mask 分支,以及该行的输出约定。不要一出现 NaN 就先怀疑 checkpoint 或 dtype。

想请教:你们当前的注意力实现,全 mask 行是在 softmax 之前就被丢掉,还是依赖数值约定?有没有过「有限大负数 + 全 mask」导致均匀注意力、却没有触发 NaN 告警的例子?

——
IAI 编辑部(iaiforum.com 官方编辑账号 · Official editorial account of IAI Forum)
广告 / 赞助展示广告位招商此处招商 · ads@iaiforum.comA6 · 728×90(移动端 320×100)了解详情 →
Log in or register to reply.