Skip to content

fix: preserve MXFP4 attention normalization with PNQ - #40

Merged
hann-wang merged 4 commits into
mainfrom
yue/mxfp4-attention-pnq
Aug 17, 2026
Merged

fix: preserve MXFP4 attention normalization with PNQ#40
hann-wang merged 4 commits into
mainfrom
yue/mxfp4-attention-pnq

Conversation

@ysa2215

@ysa2215 ysa2215 commented Aug 13, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Quantize each MXFP4 softmax tile before both the P @ V update and online row-sum update.
  • Derive the row sum from the dequantized packed tile, so the numerator and denominator use the same probability mass.
  • Add deterministic CPU reference coverage and GPU cross-validation against the pre-PNQ implementation.

Validation

Normalization invariant: V=1

For constant V=1, correctly normalized attention must produce 1.

Case Without PNQ norm_ratio PNQ norm_ratio
Causal, 64 tokens 0.9842 1.0000
Noncausal, 64 tokens 0.9757 1.0000
Causal, 96-token tail block 0.9805 1.0000

With PNQ, MAE and relative-L2 are zero in all three cases. Without PNQ, the output loses up to approximately 2.4% of its amplitude systematically.

Numerical benefit: nonzero-mean V

biased V = randn + 2.0 exposes the effect of missing probability mass on coherent outputs.

Case Without PNQ relative-L2 PNQ relative-L2 Improvement
Causal, 64 tokens 0.06700 0.04966 25.9%
Noncausal, 64 tokens 0.05746 0.03223 43.9%
Causal, 96-token tail block 0.06515 0.04524 30.6%
  • pytest -q -p no:cacheprovider tests/unittest/mxfp4/test_mxfp_pnq.py
  • GPU A/B validation against the pre-PNQ commit for causal, noncausal, and tail-block cases.

@ysa2215
ysa2215 marked this pull request as ready for review August 14, 2026 03:03
Copilot AI lite review requested due to automatic review settings August 14, 2026 03:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes an attention normalization regression in the MXFP4 FlashAttention forward path when probability tiles are quantized (PNQ), ensuring the online softmax denominator uses the same quantized probability mass consumed by the P @ V update. It also adds a CPU reference/invariant test and an optional GPU cross-validation harness to validate PNQ behavior against a pre-PNQ baseline.

Changes:

  • Quantize each softmax tile before both the P @ V update and the online row-sum update; derive l_ij from the dequantized packed tile.
  • Add a CPU reference PNQ recurrence plus an invariant test for the V=1 normalization property.
  • Add an optional script-mode GPU A/B cross-validation flow that can run kernels from a baseline Git worktree.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
tests/unittest/mxfp4/test_mxfp_pnq.py Adds CPU PNQ reference + invariant test and a script-only cross-validation harness.
alto/kernels/fp4/mxfp4/triton_flash_attention_mxfp4.py Updates the forward inner loop to compute the online row-sum from the dequantized packed probability tile.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +60 to +65
_install_quantization_test_stubs()

try:
from .utils import convert_from_mxfp4_pytorch, convert_to_mxfp4_pytorch
except ImportError:
from utils import convert_from_mxfp4_pytorch, convert_to_mxfp4_pytorch
output_pnq = mxfp4_pnq_reference_bhsd(q, k, v, causal=True, pnq=True)
output_without_pnq = mxfp4_pnq_reference_bhsd(q, k, v, causal=True, pnq=False)

assert torch.allclose(output_pnq, torch.ones_like(output_pnq), atol=0.0, rtol=0.0)
Comment on lines +352 to 356
# TODO(fix): p_fp4 is now packed above, so the mask below never reaches
# the PV dot -- enabling dropout would silently become a no-op. Keeping
# both dropout and PNQ requires masking the packed tile instead.
if ENABLE_DROPOUT:
philox_offset = batch_philox_offset + start_m * BLOCK_M * actual_seqlen_k + start_n - BLOCK_N
@hann-wang
hann-wang merged commit 74e251d into main Aug 17, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants