TensorRT-LLM icon indicating copy to clipboard operation
TensorRT-LLM copied to clipboard

Fully-masked softmax rows exponentiate to 1 in the FMHA no-loop kernel

Open StephenLReed opened this issue 3 months ago • 1 comments

System Info

  • TensorRT-LLM: 1.3.0rc23 (built from f20ea652); behaviour re-checked against main @ fbda11f5
  • GPU: NVIDIA GeForce RTX 5090 (sm_120), driver 595.71.05
  • PyTorch 2.11.0+cu130, CUDA 13.0, Python 3.12.13
  • Ubuntu 25.10, kernel 6.17.0-41-generic
  • Backend: PyTorch; single GPU, no TP/PP
  • Model where observed: Qwen3.6-35B-A3B-NVFP4 with FP8 KV cache (where model-specific)

Who can help?

No response

Reproduction

In cpp/kernels/fmha_v2/src/fused_multihead_flash_attention_kernel_noloop.h, CHECK_NEG_INF is enabled only for bidirectional sliding-window, sliding-window, and custom masks. When it is compiled out, a row whose running maximum is still -FLT_MAX — every score in the row masked — evaluates expf(-FLT_MAX - (-FLT_MAX)), which is expf(0) == 1. Each masked score therefore contributes weight 1 instead of 0.

The requirement belongs to the softmax input state, not to a mask type: any instantiation that can present an entirely masked row is affected, including ordinary causal and padding masks.

Expected behavior

A fully-masked softmax row contributes zero weight to the attention output.

actual behavior

The row's running max is still -FLT_MAX, so every masked element evaluates expf(0) == 1 and the row attends masked positions with weight 1. Silent: no error, no NaN. Presents as sequence-length-banded output corruption.

additional notes

We can demonstrate the arithmetic directly by inspection of apply_exp_with_mask. We do not have an isolated regression test attributing our production symptom to this line alone: the fix was deployed alongside an unrelated kernel-enumeration change, and the evidence is a multi-day production soak rather than a controlled A/B.

A maintainer with the fmha_v2 test harness can construct the direct case far more cheaply than we can: any causal-mask configuration whose final Q tile is partial should show masked positions receiving weight 1 before the change and 0 after.

Questions for maintainers

  1. Is enabling the guard unconditionally the fix you would want, or would you prefer it gated on the configurations that can actually produce a fully-masked row?
  2. Is there a known-good harness in cpp/kernels/fmha_v2 you would want a regression test added to?
  3. Has the performance impact of the guard on previously-unguarded instantiations been characterised anywhere? We have not measured it.

Per CONTRIBUTING.md this Issue Request precedes the patch, which is ready and applies cleanly to main @ fbda11f5.

StephenLReed avatar Aug 10 '26 20:08 StephenLReed

the arithmetic is right: with APPLY_MASK = 0, softmax.h:982 keeps max_val = -FLT_MAX and :986 evaluates expf(0) = 1 for every masked element.

reachability is what i can't reproduce, and i think it rules out the causal case:

  • in the no-loop family "sliding_or_chunked_causal" is instantiated with mask type 4, i.e. sliding window (setup.py:724-736), and kernel_traits.h:233 makes SLIDING_WINDOW_ATTENTION = (MASK_VERSION_ == 4), so CHECK_NEG_INF is already 1 for it. only the mask-3 causal and mask-2 dense instantiations compile the guard out.
  • chunked attention isn't implemented on that path at all: only the warpspec files read log2_chunked_attention_size (warpspec/compute.h, dma.h, epilogue.h), and warpspec_sm120/kernel_traits.h:165-169 doesn't even define SLIDING_OR_CHUNKED_ATTENTION.
  • for a mask-3 causal row the diagonal is always visible, so the row max can't stay -FLT_MAX. the only all-masked rows left are padding rows, whose outputs aren't part of the result.

so: is there a configuration with a stored row that is fully masked and CHECK_NEG_INF compiled out? if yes that case is the test, and i'd want it before flipping the guard on unconditionally - the compare sits in the per-row softmax path of every causal/dense instantiation, and nobody has measured that (your Q3).

for your Q2: the harness is cpp/kernels/fmha_v2/test/fmha/test_fmha_exe.py, which drives the built fmha.exe; utils.py / filter_rules.py hold the config plumbing. that's where a regression case belongs.

awesome-pro avatar Oct 09 '26 09:10 awesome-pro