Repository navigation
Conversation
…head rows The 64x64 MXFP8 Sol-Attn kernel (fmha_fwd_hd128_mxfp8_sol_64x64_gfx950) reads its pooled E8M0 K scales past their last 128-row tile. sol_prepare pads those scales only up to that tile, so the read faults whenever the allocation ends at an unmapped page. - mean_k_scale, tile mostly padding: reads 4 bytes past the end. - mean_k_scale, tile full (pooled rows a multiple of 128, so no padding): reads 256 to 1024 bytes past the end, 32 to 128 rows into the next tile. - mean_k_var_scale (Jensen term), tile full: reads 4 bytes past the end. mha_v4_packed now copies both scales into block_scale_storage, which adds MHA_V4_KV_SCALE_LOOKAHEAD_ROWS zeroed rows, the same padded storage AITER already uses for its own K scales. The copy runs only for E8M0 FP8 K scales on the 64x64 tile and costs a few KB per call. The new test checks that the scales the kernel receives match the prepared ones and have mapped, zeroed bytes past their last tile plus the lookahead rows. It fails without the copy. Signed-off-by: Niko Säkkinen <[email protected]>
…, MXFP4) Adds 256x64 manifest rows for head dim 256: block-sparse, and Sol-Attn with sorted dispatch and the Jensen variance term. No KV-range support (kv_range = 0). The .co objects are not part of this commit. - mha_v4.py: drop the hd256 dense-only rejection. The block-tile queries take a head dim, matched against the manifest's hdim_q. hd256 MXFP8 K, mean_k and mean_k_var must be record views. - asm_mha_v4_fwd.cu: the MXFP6 K record and pooled V-scale size checks use head_dim. The MX-V scale slack check is limited to the 64x64 objects. - test_mha_v4_sparse: hd256 tests per recipe. All-selected equals dense, and a random LUT matches the fp32 masked reference. Signed-off-by: Niko Säkkinen <[email protected]> Co-authored-by: Cursor <[email protected]>
…P8, MXFP6, MXFP4) Signed-off-by: Niko Säkkinen <[email protected]>
The hd256 Sol-Attn objects (BF16, MXFP8, MXFP6, MXFP4) now accept sol_tilt.
They take the same parameter tensors as their hd128 rows, except that the
hd256 MXFP8 row's second stride is its V planes', since its K is packed
with its scales.
- fwd_hd256_{bf16,mxfp8,mxfp6,mxfp4}_sol_256x64.co: rebuilt with the tilt.
- mha_v4.py: the docstring covers the hd256 tilt parameters.
Signed-off-by: Niko Säkkinen <[email protected]>
Co-authored-by: Cursor <[email protected]>
…o them No test launched an hd256 Sol-Attn kernel, so the new rows, the Jensen term and the value tilt were checked only up to their host-side inputs. The new tests run them on the kernels for BF16, MXFP8, MXFP6 and MXFP4. - mha_v4.py: mha_v4_sol quantizes MXFP4 and MXFP6 at head dim 256 with the hd256 producers and K views, and its default softmax scale follows the head dim instead of 128. mha_v4_block_tiles_in_any_precision takes an optional head dim, and the _check_block_tile error passes it, so the message lists only that head dim's tiles. - test_mha_v4_sparse: hd256 Sol-Attn tests per recipe. An all-exact selection is the dense row: bit for bit over whole blocks, to rounding over a ragged last one. Routed, the row meets sol_attn_ref on its own mask, beats keep-or-drop, and is the same launch as mha_v4_sol. The Jensen term lands at its own weight rather than at none or double, and sorted dispatch leaves it bit for bit. On planted low-rank K/V the tilt moves the output toward exact attention, and under an all-exact selection it moves nothing. A new check refuses the hd128 MXFP8 geometry (256x128) at head dim 256 before anything is quantized, in place of the removed dense-only rejection. - black over the hd256 changes in mha_v4.py and test_mha_v4_sparse.py. Signed-off-by: Niko Säkkinen <[email protected]> Co-authored-by: Cursor <[email protected]>
sol_tilt_planes takes q_abs_max in its K's basis, which for a packed K is the unrotated BF16 source. The test passed rotated maxima. The result barely changes; the test now matches the contract. Signed-off-by: Niko Säkkinen <[email protected]>
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: Reviews: |
jcaraban
left a comment
There was a problem hiding this comment.
Thanks Niko! I read this together with #8683. That was code reading plus a trial merge with current main; I haven't run anything on a GPU.
Passing the head dim through the manifest-driven tile lookup is a nice cleanup. The hd256 Sol tests are a solid set: all-exact equals dense, the oracle bound, Jensen at its own weight, and the tilt moving toward exact attention. Two things need attention before this lands, I think:
- The first commit (7bdf5b4) is the same change as #8196;
git range-diffshows it identical to that PR's head. So the points from my #8196 review (#8196 (review)) apply here unchanged, including the tilt case that faults. See the inline note on_backed_pooled_k_scale. - A merge with current main breaks THD: main's
_mha_v4_thdstill calls_check_head_dimwith thesparseargument this PR removes. Details inline.
Smaller ones:
- The CSV conflict with main is only main's new
pair_splitcolumn, so the 8 new rows need a,0beforeknl_name. mha_v4_kv_tile,mha_v4_block_tileandmha_v4_kv_tile_for_q_tiledefaulthead_dimto 128, while the new docstring says the head dim "is not a filter that may be left out". Keeping the default for compatibility seems fine, but the docstring could then say that leaving it out means head dim 128.
The hd256 Jensen test took mean_k_var straight from sol_prepare, so no hd256 launch read a capped variance, and for MXFP6 and MXFP8 that variance is a new pack with its scales inside the records. The new check alternates narrow and wide K blocks so about half are capped, launches the four hd256 rows with the capped pair, and checks the result against the reference at each block's own gain: it has to beat the uncapped reference by over 0.02 in cosine, with the LSE within 0.15. Signed-off-by: Niko Säkkinen <[email protected]>
Place each operand of every hd256 Sol row at the end of a HIP VMM mapping, so any read past its image faults. The MXFP4 K scales keep their 4-byte producer slack. All rows run in one child process. Signed-off-by: Niko Säkkinen <[email protected]>
…-sparse-hdim_256 # Conflicts: # aiter/ops/mha_v4.py # hsa/gfx950/fmha_v4_fwd/fmha_v4_fwd.csv
Under sol_tilt, mean_k_scale is plane 0 of sol_tilt_planes' K scales. Copying the view dropped planes 1..rank, and those planes now carry their own lookahead rows. Also say what an omitted head_dim means.
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.
The launcher no longer checks the slack behind the hd256 256x64 MX-V scales. Place every tensor of the hd256 block-sparse rows at the end of a mapping too, as for the Sol rows, so a V-scale read past the image faults.
… and under torch.compile Signed-off-by: Niko Säkkinen <[email protected]>
Signed-off-by: Niko Säkkinen <[email protected]>
READY FOR HUMAN REVIEWPR Self-ReviewRevision Author self-review accepted. Readiness report: Author answers
Show the author's self-review answersBehavior and intentValidation and unverified scopePR-specific questionChanged op testsPR state
Copilot findings
Code and op-test hygiene
Required CI
Shadow mode reports what would block but keeps the status successful. Set |
jcaraban
left a comment
There was a problem hiding this comment.
Rechecked 787e835. The _check_head_dim call now supplies the required scale-mode argument, and the added tests cover the capped-Jensen operand, combined tilt/cap path, packed compile parity and mapped-allocation boundaries. I found no new correctness blocker in the source review.
The top-level PR description's 'Also included' section and original test summary still describe a launcher-side scale copy that has been removed; please update them to match the validation-only launcher and the current tests, as the later self-review does. Also coordinate landing/rebasing with #8800/#8801 so both the hd128 backing fix and the hd256 repack/layout checks survive the combined changes.
Validation: the seven CPU source/contract checks across these revisions passed, including the internal call signature and all eight hd256 geometry/object entries. I have not independently reproduced the GPU numerical, compile or boundary-test results; the available test nodes were occupied. This is advisory feedback, not an approval.
…-sparse-hdim_256 Signed-off-by: Niko Säkkinen <[email protected]>
Summary
Head dimension 256 was dense-only in MHA v4. This adds 256x64 block-sparse and Sol-Attn kernels for hd256 on gfx950, for four recipes: BF16, MXFP8, MXFP6 and MXFP4. Sol-Attn at hd256 supports sorted dispatch, the Jensen variance term and the value tilt (
sol_tilt), like the hd128 rows. Models with 256-wide heads, such as Ideogram-4, can now use sparse and Sol attention.What's added
hsa/gfx950/fmha_v4_fwd/,fwd_hd256_{bf16,mxfp8,mxfp6,mxfp4}_{sparse,sol}_256x64.co, and their rows infmha_v4_fwd.csv.kv_range = 0). Any other tile at hd256 is refused before anything is quantized.Changes
aiter/ops/mha_v4.pyblock_tile,block_tiles,kv_tile_for_q_tile,mha_v4_block_tiles_in_any_precision) take a head dim, matched against the manifest'shdim_q. The_check_block_tileerror lists only that head dim's tiles.mha_v4_solquantizes MXFP4 and MXFP6 at hd256 with the hd256 producers and K views. Its default softmax scale follows the head dim, not 128.mean_kandmean_k_varmust be record views, because the K is packed with its scales.csrc/py_itfs_cu/asm_mha_v4_fwd.cuhead_dim.Also included: MXFP8 64x64 Sol-Attn pooled K scale overread (hd128)
fmha_fwd_hd128_mxfp8_sol_64x64_gfx950reads its pooled E8M0 K scales past their last 128-row tile.sol_preparepads them only up to that tile, so the read could fault when the allocation ends at an unmapped page. The overread is up to 1 KB formean_k_scaleand 4 bytes formean_k_var_scale.mha_v4_packednow copies both intoblock_scale_storage, which addsMHA_V4_KV_SCALE_LOOKAHEAD_ROWSzeroed rows. This is the same padded storage AITER already uses for its own K scales. The copy runs only for E8M0 FP8 K scales on the 64x64 tile and costs a few KB per call.Testing (MI355X, gfx950)
op_tests/test_mha_v4_sparse.pyop_tests/test_mha_v4.pyNew hd256 coverage, per recipe:
sol_attn_refon its own mask, beats keep-or-drop, and is the same launch asmha_v4_sol.End to end: xDiT Ideogram-4 (hd256), 2048², 48 steps, 4x MI355X
aiterMost of the speedup over dense comes from the MHA v4 kernel itself; skipping blocks at beta 0.5 adds about 0.9 s. A constant beta of 0.5 can repeat or ghost lettering on Ideogram; a beta ramp or the tilt avoids it.
PR Self-Review
Reviewed revision
dda605ec5315Behavior and intent
MHA v4 at head dimension 256 was dense-only. Block-sparse and Sol-Attn calls at hd256 were refused:
_check_head_dimraised "head dimension 256 is dense-only" inmha_v4(block_mask=...)and inmha_v4_packed, whichmha_v4_sollaunches through. So models with 256-wide heads could not use sparse attention at all.This PR adds gfx950 hd256 objects for BF16, MXFP8, MXFP6 and MXFP4, each as a block-sparse row and a Sol-Attn row (eight rows, all 256x64), and routes the Python entry points to them:
mha_v4_block_tile,mha_v4_kv_tile_for_q_tile,mha_v4_block_tiles,mha_v4_ragged_kvand_check_block_tiletake ahead_dim, which defaults to 128 so hd128 callers are unchanged. The lookup has to know the head dim because hd256 runs its own tile: MXFP8 is 256x128 at hd128 and 256x64 at hd256.mha_v4(block_mask=...)andmha_v4_soltake the hd256 quantizers and K views (mha_v4_mx_producers,_MXFP6_K_VIEWS), and the softmax scale defaults tohead_dim**-0.5instead of a hard-coded128**-0.5. The hd256 MXFP8 Sol-Attn row is reached throughmha_v4_packed, as hd128 MXFP8 is.check_mxfp6_k_record_storageinasm_mha_v4_fwd.cuchecks hd256's 64-token, 13312-byte MXFP6 K records as well as hd128's 128-token, 17408-byte ones. Themean_v_scalesize check scales with head dim._check_mxfp8_hd256_recordsnow coversmean_kandmean_k_varas well as K.hd128 behavior is unchanged.
Validation and unverified scope
On one MI355X (gfx950) with torch 2.13.0 and Triton 3.8.0, at
787e83551cea. Themodule_mha_v4_quantandmodule_fmha_v4_fwdJIT modules were rebuilt from this revision's sources. Results:python op_tests/test_mha_v4_sparse.pychecks: 647 ran, 0 failed, 5 skipped.test_mha_v4_head_dim_256_reads_stop_at_each_image, which needs HIP virtual memory management and ran.python op_tests/test_mha_v4.pychecks: 444 ran, 0 failed, 1 skipped.python -m pytest -q op_tests/triton_tests/attention/test_sol_prepare.py op_tests/triton_tests/attention/test_sol_tilt.py(the base PR's tests, on this branch): 178 passed, 2 skipped.Unverified:
_check_block_tileshould raiseNotImplementedError, but that was not run on those GPUs.PR-specific question
Which architecture, dtype and shape boundaries select the changed kernel path, and what invariant prevents unsupported inputs from reaching it?
Architecture: gfx950 only. The eight rows are in
hsa/gfx950/fmha_v4_fwd/fmha_v4_fwd.csvalone. Elsewhere,mha_v4_block_tiles(operands, mode, head_dim=256)is empty, so_check_block_tileraisesNotImplementedError("has no head-dim 256 row") before anything is quantized. That matters because an MX quantizer aborts rather than raises on an arch without the row.Dtypes. The rows exist for:
_check_head_dimadmits only these (Q/K, V) pairs at hd256, plus FP8 Q/K under E8M0 scales only; per-tensor FP8, i8fp8 and mixed recipes such as f8f6 are refused. A pair it admits but that has no row in a mode still fails_check_block_tilefor that mode and head dim.Shapes.
kv_range, sokv_range_tokens != 0fails the launcher'scfg.kv_rangecheck.ValueErrorlisting the hd256 tiles, instead of matching a row of the wrong head dim.Operand layout. These invariants keep a well-shaped but wrongly laid-out tensor from reaching the kernels:
mean_kormean_k_varmust be a view of whole records of the layout for its head dim, backed through the packer's zeroed slack, and any head dim other than 128 or 256 is refused (check_mxfp6_k_record_storage).mean_kormean_k_varmust have the record strides ofmxfp8_k_view_hd256(_check_mxfp8_hd256_records). A natural BSHD tensor would otherwise run with its scales unread.mean_v_scalemust hold 4 * head_dim bytes per 128 pooled rows.test_mha_v4_head_dim_256_reads_stop_at_each_imageplaces each tensor's last byte at the end of a mapping, so a read past any image faults.Changed op tests
op_tests/test_mha_v4_sparse.pyadds the hd256 coverage. Each case runs only where a manifest row exists:test_mha_v4_sparse_head_dim_256_follows_the_lut(BF16, MXFP8, MXFP6, MXFP4): all-selected matches the dense row bit for bit, and a random LUT matches the fp32 reference over exactly the selected blocks. The LUT has empty, one-block and two-block rows, a ragged last block, a partial query tile and GQA.test_mha_v4_sol_head_dim_256_reduces_to_dense_and_meets_the_oracle: all-exact is the dense row, and the routed row meetssol_attn_refon its own mask and beats keep-or-drop.mha_v4_solis the same launch, for every recipe but MXFP8, which it cannot reach...._jensen_term_is_the_block_k_varianceand..._reads_the_capped_jensen_variance: the Jensen term lands at its own weight in K's basis, andsol_jensen_cap's re-packed MXFP6/MXFP8 variance is the one read...._tilt_reaches_the_pooled_valuesand..._tilt_and_capped_jensen_run_together: the rows read the tilt planes at the right stride, alone and together with the capped Jensen term...._packed_compile_parity:sol_prepareand the launch trace as one fullgraph and match eager bitwise.test_mha_v4_head_dim_256_reads_stop_at_each_image: no hd256 row reads past any operand.test_mha_v4_head_dim_256_block_tile_is_its_own: the hd128 MXFP8 geometry is refused at hd256 before quantizing.These are not redundant. The existing tests in this file run at head dim 128, and the base PR's
test_sol_prepare.pyandtest_sol_tilt.pycheck the Triton helpers' tensors without running any kernel, so nothing else runs the hd256 objects.op_tests/test_mha_v4.pyremovestest_mha_v4_rejects_sparse_head_dim_256, which asserted "head dimension 256 is dense-only". This PR lifts that refusal, so the test would fail. What it guarded is now covered byop_tests/test_mha_v4_sparse.py: the supported hd256 recipes run there, the unsupported geometry is refused bytest_mha_v4_head_dim_256_block_tile_is_its_own, and the unsupported dtype pairs remain refused bytest_mha_v4_rejects_mixed_mxfp6_head_dim_256in this file.