Skip to content

[ASM] [Fix] Scale gfx950 FP8 MLA softmax numerators before E4M3 - #5535

Closed
tanth47 wants to merge 2 commits into
ROCm:mainfrom
tanth47:codex/mla-fp8-softmax-underflow
Closed

tanth47 wants to merge 2 commits into
ROCm:mainfrom
tanth47:codex/mla-fp8-softmax-underflow

Conversation

@tanth47

@tanth47 tanth47 commented Sep 15, 2026 •

Copy link
Copy Markdown

Summary

Fix the gfx950 non-persistent A8W8 MLA decode kernel for 16 query heads by
scaling online-softmax numerators before E4M3 conversion, and add a
deterministic regression for the failure.

The fixture uses one logit at 0, 2,047 logits at -7, and an all-ones value
vector. Correct attention therefore returns an all-ones output regardless of
the score distribution. It also includes a uniform-score control to validate
the fixture and dispatch.

Motivation

The current kernel converts unscaled online-softmax numerators to E4M3 before
P@V. For this fixture, exp(-7) rounds to zero in E4M3 even though the FP32
softmax denominator retains the aggregate long-tail mass.

On gfx950 with current main (9252f4672), the test loads:

_ZN5aiter33mla_a8w8_qh16_qseqlen1_gqaratio16E
hsa/gfx950/mla/mla_a8w8_qh16_qseqlen1_gqaratio16.co
sha256: 6a56860188249bd4d70ab339dcc18dfa18c4ee21b00c1759f0eb7c29b6f061ea

Observed means:

AITER:            0.349609
FP32 reference:   0.999993
unscaled FP8:     0.348843
scaled FP8 x448:  0.996377

The original AITER result is within 7.67e-4 max-absolute error of the
unscaled-FP8 oracle and 0.650383 away from ordinary FP32 attention.

Static disassembly of the same code object shows v_exp_f32, an FP32
denominator reduction, and then direct v_cvt_pk_fp8_f32 instructions before
FP8 P@V. There is no probability scaling instruction between exponentiation
and E4M3 conversion.

Independent corroboration: SGLang's gfx95 TileLang FP8 sparse MLA kernel
scales softmax numerators by the E4M3FN maximum (448)
before the FP8 P@V GEMM and
applies 1/448 afterward.
This is the same arithmetic modeled by the scaled-FP8 oracle in this
regression; FP32 attention remains the correctness reference.

Changes

  • Patch mla_a8w8_qh16_qseqlen1_gqaratio16.co at the ISA level:
    • initialize one packed VGPR pair to 448.0;
    • scale v38:v45 after the FP32 denominator accumulation and before all 24
      v_cvt_pk_fp8_f32 instructions across the six repeated softmax blocks;
    • apply 1/448 once in the final P@V normalization.
  • Add a minimal synthetic test with no model checkpoint or captured tensors.
  • Add a uniform-probability control.
  • Compare the kernel against FP32, direct-E4M3, and scaled-by-448 E4M3 oracles.
  • Check magnitude error explicitly; a cosine-only assertion cannot detect this
    failure when the output direction is preserved.

Testing

Hardware and software:

  • AMD Instinct MI350X, gfx950
  • PyTorch 2.11.0+rocm7.2
  • AITER main at 9252f4672

Command:

python3 op_tests/test_mla_fp8_softmax_numerics.py

The exact original and patched code objects were tested in separate Python
processes inside the same pinned container. LoadKernel confirmed the target
path in both arms.

Original code object
(sha256:6a56860188249bd4d70ab339dcc18dfa18c4ee21b00c1759f0eb7c29b6f061ea):

out=0.349609, fp32=0.999993, unscaled_fp8=0.348843, scaled_fp8=0.996377
fp32_error=0.650383, unscaled_fp8_error=0.000767

Patched code object
(sha256:b1459224771c31a27e06d3d874f67874da448d7411d25fdf83cc0aae2faba3a0):

out=0.996094, fp32=0.999993, unscaled_fp8=0.348843, scaled_fp8=0.996377
fp32_error=0.003899, scaled_fp8_error=0.000283

The patched regression passed four times with identical values (the initial
run plus three repeats). The uniform-score control also passes.

Static checks:

  • python3 -m py_compile op_tests/test_mla_fp8_softmax_numerics.py
  • git diff --check
  • AITER's docs/examples/isa_optimization/roundtrip.sh on the patched object:
    .text, .rodata, metadata, and ELF target flags all round-trip identically.
  • Kernel resources are unchanged: 512 VGPRs, 104 physical SGPRs, and 163,840
    bytes LDS. The patch adds 27 instructions / 216 bytes of .text without
    increasing the declared allocation.

Generator follow-up

The source or generator for this exact non-persistent HSACO is not present in
the public repository. This PR therefore includes an ISA-level code-object
patch so the repair is executable and the regression is green.

If this object is generated from a private or external source, please apply the
same arithmetic there and regenerate it to avoid the binary and generator
drifting. Sibling A8W8 MLA code objects using the same P@V scheme should also
be audited; this PR changes only the exact object exercised by the regression.

Performance

On the synthetic 2,048-token fixture, a same-container torch.cuda.Event
microbenchmark (50 warmups, 10 samples x 500 launches) measured:

original median: 0.035664 ms
patched median:  0.035946 ms
delta:           +0.000282 ms (+0.79%)

This is kernel-only evidence for this shape, not an engine-level benchmark.

Dependencies

  • No new dependencies.
  • No production data, prompt, or model tensors are included.

@github-actions

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

  • ✅ Pre-checks (submodule verification, code formatting)
  • ✅ Aiter op tests (gfx942 + gfx950)
  • ✅ Triton tests on MI35X (only when aiter/ops/triton/** or related paths are changed)

Extended tests (opt-in via labels):

Label Tests
ci:gfx1250-ffm-triton Run the five-shard gfx1250 FFM Triton test suite
ci:triton-300x Run an additional Triton test job on MI300X in PRs; main branch always runs both MI35X and MI300X
multigpu Aiter multi-GPU tests on the 8-GPU runner
ci:sglang SGLang integration tests: DeepSeek-R1-MXFP4 accuracy, Qwen 3.5 accuracy
ci:atom ATOM benchmark: DeepSeek-R1-0528, GPT-OSS-120B
ci:atom_full ATOM accuracy suite for PR and main models from ATOM models_accuracy.json
ci:vllm vLLM benchmark: GPT-OSS-120B, DeepSeek-R1-0528, Kimi-K2.5
ci:all All standard extended tests (excludes ci:atom_full)

Only add ci:atom_full for FlyDSL or Triton upgrades.
Add labels via the sidebar or gh pr edit 5535 --add-label <label>

PR title tags & labels:
Component tags ([Triton/Gluon], [HIP], [CK], [ASM], ...) are added to the PR title and as PR labels automatically from the changed files and re-synced on every push — change-type tags like [fix]/[Perf], op tags like [MLA], and human labels (ci:*) are left untouched. Add the no-auto-title label to opt this PR out.

@tanth47
tanth47 force-pushed the codex/mla-fp8-softmax-underflow branch from 6d4c2bd to e80fea3 Compare September 15, 2026 11:01
@tanth47 tanth47 changed the title [Test] Reproduce gfx950 FP8 MLA softmax numerator underflow [Fix] Scale gfx950 FP8 MLA softmax numerators before E4M3 Sep 15, 2026
@HaiShaw

HaiShaw commented Sep 27, 2026

Copy link
Copy Markdown
Collaborator

@tanth47 Is this suppose to be draft still?

@tanth47
tanth47 marked this pull request as ready for review September 28, 2026 03:37
@tanth47
tanth47 requested a review from a team September 28, 2026 03:37
@github-actions github-actions Bot changed the title [Fix] Scale gfx950 FP8 MLA softmax numerators before E4M3 [ASM] [Fix] Scale gfx950 FP8 MLA softmax numerators before E4M3 Sep 28, 2026
@github-actions github-actions Bot added the ASM label Sep 28, 2026

@HaiShaw HaiShaw left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM.

Please pay attention to numeric and scaling.
cc @carlushuang @shengnxu @coderfeli @kkHuang-amd @1am9trash

@HaiShaw

HaiShaw commented Sep 28, 2026

Copy link
Copy Markdown
Collaborator

@coderfeli @shengnxu @carlushuang please review.

@tanth47
tanth47 force-pushed the codex/mla-fp8-softmax-underflow branch from 8e92e27 to bceaa5a Compare September 28, 2026 07:25
@tanth47

tanth47 commented Sep 28, 2026

Copy link
Copy Markdown
Author

I rebased the branch onto latest main. The previous run didn’t reach the tests, the branch was missing the recently added ensure_zstd.sh.
It’s included now. Could someone approve the new CI run?

@tanth47

tanth47 commented Oct 1, 2026

Copy link
Copy Markdown
Author

Hi @amd-ruitang3 Have you had a chance to look at this?

@zufayu

zufayu commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

We've confirmed the bug. Disassembly of both code objects verifies the root cause: the a8w8 MLA kernel converts the softmax numerators to E4M3 before P@V while the denominator stays in FP32, so probabilities with a logit gap > ~7 from the row max flush to zero and the output magnitude shrinks — it worsens with context length, and cosine-only tests won't catch it.

The math in your patch is correct (scale p by 448 before the FP8 cvt, 1/448 at the end), but since it patches the .co at the ISA level we won't merge it as-is. We'll land the fix from the kernel source and regenerate all a8w8 MLA variants on gfx950 and gfx942 — the same missing scaling is present in the _ps, MTP, and qh32 variants, and #6139 is the same class of issue on gfx942 FP8 FMHA.

We'll keep your regression test with credit and extend it to the other variants with a magnitude check. This is tracked for the next AITER release.

@zufayu
zufayu requested a review from fangche123 October 7, 2026 01:35
@fangche123

Copy link
Copy Markdown
Contributor

@zufayu

zufayu commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Thank you Chefang for your in‑depth investigation. FlashMLA is normally used as our golden standard for implementation alignment. Therefore, it is recommended that we maintain the existing status for now.

tanth47 commented Oct 11, 2026

Copy link
Copy Markdown
Author

Thanks for checking the reference. My concern is the magnitude error in the constant-V repro: expected output is approximately 1, but AITER returns 0.349609; with scaling it returns 0.996094.

Have you run this fixture through FP8 FlashMLA? Its online-softmax traversal and split-KV could affect when the weights are quantized, so the same cast doesn’t necessarily imply identical numerical behavior.

Could we compare both paths against FP32 using magnitude error and agree on an acceptable tolerance? If FlashMLA also exhibits the loss, that would suggest a shared numerical issue rather than invalidate the repro.

I’m happy with a source-generated fix instead of merging the patched binary.

@shengnxu

shengnxu commented Oct 18, 2026 •

Copy link
Copy Markdown
Collaborator

Thank you for the finding, #6422 copied this fix and expanded to more kernels, and merged, no more need to merge this pr

@tanth47

tanth47 commented Oct 23, 2026

Copy link
Copy Markdown
Author

Closing as superseded: the same FP8 softmax numerator scaling landed in #6422 (expanded to the sibling qh16/qh32/qh64 code objects), and #6577 corrected the QH16 SGPR metadata. Thanks @shengnxu and @zufayu for the review.

@tanth47 tanth47 closed this Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants