Repository navigation
Conversation
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: Reviews: |
There was a problem hiding this comment.
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:
- With
sol_tilton this row, the copy drops the K-scale planes behindmean_k_scale: the tilt goes silently off, or the launch returns NaN or faults. - Under
torch.compilethe 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.
| 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) |
There was a problem hiding this comment.
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.
| backed = block_scale_storage( | ||
| scale, | ||
| *scale.shape, | ||
| MHA_V4_KV_TILE_ROWS, | ||
| lookahead_rows=MHA_V4_KV_SCALE_LOOKAHEAD_ROWS, | ||
| ) |
There was a problem hiding this comment.
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.
| MHA_V4_KV_TILE_ROWS, | ||
| lookahead_rows=MHA_V4_KV_SCALE_LOOKAHEAD_ROWS, | ||
| ) | ||
| backed.copy_(scale) |
There was a problem hiding this comment.
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.
| 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 | ||
| ) |
There was a problem hiding this comment.
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.
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]>
1339de1 to
84a2fb4
Compare
Summary
The 64x64 MXFP8 Sol-Attn kernel,
fmha_fwd_hd128_mxfp8_sol_64x64_gfx950, reads its pooled E8M0 K scales past their end.sol_preparepads 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_storagewithMHA_V4_KV_SCALE_LOOKAHEAD_ROWS, which AITER's K scales already use. The kernel andsol_prepareare 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:
mean_k_scalemean_k_scalemean_k_var_scale(Jensen term)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_scalecopies a pooled scale intoblock_scale_storagewith lookahead rows.mha_v4_packedapplies it tomean_k_scaleandmean_k_var_scaleonly for E8M0 FP8 K scales on the 64x64 tile. The copy is a few KB per call.op_tests/test_mha_v4_sparse.py: newtest_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)
op_tests/test_mha_v4_sparse.pyop_tests/test_mha_v4.py