Skip to content

[Fix] MHAv4: back the MXFP8 64x64 Sol-Attn pooled K scales - #8196

Closed
nsakkine wants to merge 2 commits into
mainfrom
nsakkine/fix-mha_v4-mxfp8-k_scale-invalid-read
Closed

nsakkine wants to merge 2 commits into
mainfrom
nsakkine/fix-mha_v4-mxfp8-k_scale-invalid-read

Conversation

@nsakkine

Copy link
Copy Markdown
Collaborator

Summary

The 64x64 MXFP8 Sol-Attn kernel, fmha_fwd_hd128_mxfp8_sol_64x64_gfx950, reads its pooled E8M0 K scales past their end. sol_prepare pads them only up to their last 128-row tile and adds no lookahead rows, so the read faults whenever the allocation ends at an unmapped page.

This PR copies those scales into AITER's own padded scale storage, block_scale_storage with MHA_V4_KV_SCALE_LOOKAHEAD_ROWS, which AITER's K scales already use. The kernel and sol_prepare are unchanged. The guard can be removed once the scales are padded where they are produced.

What reads past the end

To reproduce, each scale tensor is placed so its memory ends exactly at an unmapped page (HIP VMM: reserve, then map only the tensor's size). Any read past the end then faults. I swept every MX recipe that has pooled scales, at the default and 64x64 tiles, with 8 and 128 pooled K rows, on each scale tensor:

Tensor Pooled rows Read past the end
mean_k_scale 8 (tile mostly padding) 4 B
mean_k_scale 128 (tile full, no padding) 256 B to 1024 B, 32 to 128 rows into the next tile
mean_k_var_scale (Jensen term) 128 4 B

Only the mxfp8 64x64 row faults. Every other recipe, tile and tensor runs clean, including mxfp8 at its default 256x128 tile. The 256 lookahead rows cover every case.

Changes

  • aiter/ops/mha_v4.py: new _backed_pooled_k_scale copies a pooled scale into block_scale_storage with lookahead rows. mha_v4_packed applies it to mean_k_scale and mean_k_var_scale only for E8M0 FP8 K scales on the 64x64 tile. The copy is a few KB per call.
  • op_tests/test_mha_v4_sparse.py: new test_mha_v4_mxfp8_sol_64x64_reads_backed_pooled_k_scales, run with 8 and 128 pooled rows, each with and without the Jensen term. It captures the scales handed to the kernel and checks that they match the prepared scales and have mapped, zeroed bytes past their last tile plus the lookahead rows.

Test results (gfx950, MI355X)

Check Result
The 5 cases that fault on main, with this PR all pass
New test 4/4 pass; 4/4 fail with the copy disabled ("0 of 2048 bytes backed")
op_tests/test_mha_v4_sparse.py 613 ran, 5 skipped, 0 failed
op_tests/test_mha_v4.py 357 ran, 0 skipped, 0 failed

@github-actions

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

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

Extended tests (opt-in via labels):

Label Tests
ci:mi300x Run Aiter standard tests on MI300X in PRs; main branch always runs them
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 (added automatically when gfx942 configs change); main branch always runs both MI35X and MI300X
ci:triton-355 Run the full Triton test suite on MI35X, not only the tests the change affects
ci:windows Windows JIT op smoke test on the Radeon R9700 ROCm runner
ci:multi-gpu 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: DeepSeek-V4-Pro, MiniMax-M3-MXFP4
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 8196 --add-label <label>

One backend per PR:
A PR changes one kernel backend: [Triton/Gluon] (Triton and Gluon count as one), [HIP], [ASM], [CK], [OPUS] or [FlyDSL]. If the title ends up with two backend tags, split the PR -- as stacked pull requests when one part cannot merge without the other.

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 stop the title rewrites; labels stay in sync either way.

Reviews:
A reviewer is usually assigned within one working day after the PR is ready for review (see Code Review Process). If nobody has responded for about a week, mention the area contact listed in CONTACTS.md. Want to help review? See REVIEWERS.md.

@jcaraban jcaraban 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.

I read through the change and then ran it on MI355X (torch 2.9 and 2.13). No verdict from me, just what I found.

In eager mode the fix does what the description says: the new test passes 4/4 and fails 4/4 with the copy disabled, and scales placed against an unmapped page run clean through the copy, also at 1024 pooled rows and with 8 heads.

Two things break though, details inline:

  1. With sol_tilt on this row, the copy drops the K-scale planes behind mean_k_scale: the tilt goes silently off, or the launch returns NaN or faults.
  2. Under torch.compile the backing does not survive, so the compiled path still reads past the allocation.

On the 256 lookahead rows: they have margin. With the copy disabled I bisected how much zeroed slack each scale needs behind it before the launch stops faulting against a guard page (2 heads unless noted):

Scale Pooled rows Needed past the scale
mean_k_scale not a multiple of 128 (8, 64, 136) padding to the tile + 4 B
mean_k_scale 128 64 rows + 4 B
mean_k_scale 256, 1024 between 64 and 96 rows
mean_k_scale 128, kv_range_tokens=2048 96 rows + 4 B
mean_k_var_scale 8, 136 padding to the tile
mean_k_var_scale 128, 1024 4 B
mean_k_var_scale 128, kv_range_tokens=2048 between 32 and 48 rows

At 128 pooled rows the result stayed in that 64-to-96-row bracket with 8 heads, batch 2, a longer Q, and with everything or nothing routed exactly. The default 256x128 row needs nothing, as in your sweep.

The bytes read past the end don't seem to be consumed: filling the tile padding or the lookahead region with 0xFF gives bitwise-identical output (8, 128 and 200 pooled rows, both scales). So this looks like a fault-only problem, not a wrong-result one.

Nit: _backed_pooled_k_scale is nearly sol_tilt._backed_scale_copy; one shared helper would do.

Happy to share the probe scripts if they help.

Comment thread aiter/ops/mha_v4.py Outdated
Comment on lines +1978 to +1984
if (
k_scale_mode == AttentionScaleMode.E8M0_PER_1X32
and _is_fp8_format(k_format)
and (q_tile, kv_tile) == (64, 64)
):
mean_k_scale = _backed_pooled_k_scale(mean_k_scale)
mean_k_var_scale = _backed_pooled_k_scale(mean_k_var_scale)

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.

With sol_tilt on this row, mean_k_scale is only plane 0 of a stack. sol_tilt_planes puts K-scale planes 1..rank behind it in the same allocation (_stack_planes(mean_k_scale, scale, _TILT_SCALE_ROW_TILE)), and the kernel reaches them through the stride in dword 2 of sol_tilt_scaled_params. The copy takes plane 0 alone, so the other planes are read from the copy's zeroed slack and then from whatever lies past it.

On MI355X with 2 heads, against the same tree with the copy switched off:

Pooled rows Rank Output with the copy
32, 128 1, 2 bitwise equal to rank 0: the tilt is silently off
32 8 NaN, differs between runs
128 8 GPU memory access fault
512 1 cosine 0.49 against the launch without the copy
512 2, 8 NaN, differs between runs

Without the copy the launches are deterministic and differ from rank 0 (the 128-row rank-8 case ran four times without it before the first launch with the copy faulted). The tilt has no op test yet, which is why the suite stays green.

The K-scale plane stride lives in the device-side shift tensor, so I don't think the launcher can copy the planes along. Could we skip the copy when sol_tilt is given, or, better I think, pad where the scales are built (_sol_attn_pad_scale_rows and _stack_planes)? Related: against a guard page, the stack as sol_tilt_planes builds it also reads 4 bytes past its end once the pooled rows fill the tile, with or without this PR, so _stack_planes wants trailing slack either way.

Comment thread aiter/ops/mha_v4.py Outdated
Comment on lines +1404 to +1409
backed = block_scale_storage(
scale,
*scale.shape,
MHA_V4_KV_TILE_ROWS,
lookahead_rows=MHA_V4_KV_SCALE_LOOKAHEAD_ROWS,
)

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.

This runs inside mha_v4_packed, which gets traced, and the padding doesn't survive torch.compile. I captured what _fmha_v4_fwd_sol receives, the way the new test does, with the launch compiled (fullgraph=True): both scales arrive with 0 bytes behind them, against 2048 to 3008 in eager mode (torch 2.9 and 2.13; 8 and 128 pooled rows; with and without the Jensen term). The outputs still match eager bitwise, so test_mha_v4_sol_packed_compile_parity can't see it.

So on the compiled path the kernel still reads past the allocation. _stack_planes has a note on the same effect ("under torch.compile torch ops building them would be cut down to plane 0").

Could the copy move somewhere the compiler can't touch, e.g. the body of _mha_v4_fwd_sol_launch, or the C++ launcher next to head_major_k_scale? For what it's worth, with sol_prepare in the same compiled graph its own tile padding is gone as well, also without this PR, so that one probably wants the same treatment.

Comment thread aiter/ops/mha_v4.py Outdated
MHA_V4_KV_TILE_ROWS,
lookahead_rows=MHA_V4_KV_SCALE_LOOKAHEAD_ROWS,
)
backed.copy_(scale)

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.

Small one, from reading only: the copy now runs before the launcher's checks. A scale of another dtype is cast to uint8 here instead of reaching "mean_k_scale must be a uint8 E8M0 tensor", and one that isn't 4-D raises a TypeError from block_scale_storage rather than the launcher's shape message. It goes away if the copy moves behind the validation.

Comment thread op_tests/test_mha_v4_sparse.py Outdated
Comment on lines +3017 to +3020
with mock.patch.object(aiter.ops.mha_v4, "_fmha_v4_fwd_sol", capture):
out = _sol_attn_launch(
q, k, v, plan, recipe=recipe, block_tile=_MHA_V4_FINE_TILE, jensen=jensen
)

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.

Could this also run the launch through torch.compile(..., fullgraph=True)? The capture still works there, since _fmha_v4_fwd_sol is called inside the custom op, and that is the case that currently loses the backing (see my comment on _backed_pooled_k_scale). test_jensen_cap_scale_keeps_the_pooled_scale_backing has an eager/compiled parametrization that could be mirrored.

A sol_tilt case would be worth having too, since nothing launches the tilt end to end today.

nsakkine added a commit that referenced this pull request Nov 26, 2026
Drop the copy into block_scale_storage and its tests from this PR. No
hd256 row runs at 64x64, so nothing here needs it, and the copy is
being settled in #8196.
…hey are made

The 64x64 MXFP8 Sol-Attn kernel (fmha_fwd_hd128_mxfp8_sol_64x64_gfx950)
reads its pooled E8M0 K scales, and the Jensen variance's, past their
last 128-row tile and on into the next tile's rows, as it reads K's own.
sol_prepare and sol_jensen_cap padded them only to that tile, and
sol_tilt_planes stacks the tilt's K-scale planes behind mean_k_scale with
nothing past the last one, so the read faulted whenever the allocation
ended at an unmapped page. The bytes past the end are read but not used.

- utils.py: _sol_attn_pad_scale_rows copies a pooled scale into
  block_scale_storage, with MHA_V4_KV_SCALE_LOOKAHEAD_ROWS zeroed rows and
  the MXFP4 K slack past its last tile. sol_prepare and sol_jensen_cap use
  it, in place of sol_tilt's _backed_scale_copy.
- sol_tilt.py: _stack_planes takes tail_rows, and the MXFP8 K-scale stack
  carries the lookahead rows behind its last plane.
- mha_v4_quant.py, sol_tilt.py: block_scale_storage and _stack_planes view
  their whole allocation. torch.compile cut a slice that is written
  through down to the slice's own size, which dropped the rows behind it.
- test_sol_tilt: the MXFP8 K-scale stack test covers a full last tile too,
  and checks the zeroed lookahead rows behind the last plane.

Signed-off-by: Niko Säkkinen <[email protected]>
…d read past

The 64x64 MXFP8 Sol-Attn kernel (fmha_fwd_hd128_mxfp8_sol_64x64_gfx950)
reads its pooled E8M0 K scales, and the Jensen variance's, past their
last 128-row tile and on into the producer's lookahead rows. sol_prepare,
sol_jensen_cap and sol_tilt_planes back them with those rows. A scale
without them, a clone for instance, faulted whenever its allocation ended
at an unmapped page; the launch now refuses it.

- asm_mha_v4_fwd.cu: check_pooled_k_scale_backing_storage requires the
  tile padding plus kKvScaleLookaheadRows rows of mapped storage behind
  mean_k_scale and mean_k_var_scale on the hd128 64x64 FP8-K rows. With
  the tilt, mean_k_scale is plane 0 of a stack, so this bounds plane 0;
  sol_tilt_planes backs the last plane the same way.
- test_mha_v4_sparse: on the 64x64 MXFP8 row, sol_prepare's scales carry
  the zeroed lookahead rows, eager and under torch.compile, and a clone of
  either is refused. The tilt reaches the row at ranks 2 and 8. Each
  pooled K-scale allocation, sol_tilt_planes' stack included, placed at
  the end of a mapping with an unmapped granule after it, leaves the output
  unchanged at 8, 128, 136 and 1024 pooled rows.

Signed-off-by: Niko Säkkinen <[email protected]>
@nsakkine
nsakkine force-pushed the nsakkine/fix-mha_v4-mxfp8-k_scale-invalid-read branch from 1339de1 to 84a2fb4 Compare November 27, 2026 05:19
@nsakkine

Copy link
Copy Markdown
Collaborator Author

Closing this because the solution requires modifications to both triton and hip/asm sides and therefore stacked PRs. See #8800 and #8801 for this stack.

@nsakkine nsakkine closed this Nov 27, 2026
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.

2 participants