Repository navigation
[ASM] [HIP] [OPUS] Add narrow-head (H<=32) gfx1250 MLA sparse-prefill kernels for DSv4 TP - #5228
Conversation
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
PR title tags & labels: |
There was a problem hiding this comment.
🟡 Changes recommended
The new launch paths can attempt a kernel launch with gridDimY=0 when H==0, which should be guarded with an early return (or equivalent validation) to avoid an invalid HIP launch configuration.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
Adds narrow-head (H<=32) gfx1250 sparse-prefill prebuilt-kernel dispatch to improve utilization under tensor parallelism, while preserving the existing clustered path for larger head counts.
Changes:
- Introduces gfx1250 narrow-head code-object identifiers (16mx1_16nx4 and 32mx1_16nx4) for bf16 (a16w16) and fp8 (a8w8).
- Adds head-count-based dispatch in bf16 and fp8 gfx1250 sparse-prefill launchers: H<=16 → 16mx1, H<=32 → 32mx1, else keep 16mx4_64nx1 clustered behavior.
File summaries
| File | Description |
|---|---|
| csrc/py_itfs_cu/pa_sparse_prefill_opus_kernels.cu | Adds new gfx1250 code-object paths/constants and dispatch logic to select narrow-head kernels for H<=32. |
Review details
Suppressed comments (1)
csrc/py_itfs_cu/pa_sparse_prefill_opus_kernels.cu:664
- If H==0, ceil_div(H, ...) becomes 0 and the kernel launch will use gridDimY=0, which is invalid for hipModuleLaunchKernel. Consider early-returning on empty head dimension (similar to the existing N==0 early return) before selecting a code object.
size_t arg_size = sizeof(args);
// 16mx1_16nx4 for H <= 16, 32mx1_16nx4 for H <= 32, else the clustered 16mx4_64nx1.
if(H <= kGfx1250Heads16mx1)
{
- Files reviewed: 1/7 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
There was a problem hiding this comment.
🟡 Changes recommended
The new dispatch paths should guard against H==0 to avoid attempting a zero-sized HIP grid launch, and a small constant-coupling tweak would prevent future divergence.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Review details
Suppressed comments (2)
csrc/py_itfs_cu/pa_sparse_prefill_opus_kernels.cu:663
- If H==0, this path will compute a 0 grid.y via ceil_div(H, ...) and attempt to launch a zero-sized grid. Add an early-return for H==0 before the new narrow-head dispatch/launch logic.
size_t arg_size = sizeof(args);
// 16mx1_16nx4 for H <= 16, 32mx1_16nx4 for H <= 32, else the clustered 16mx4_64nx1.
if(H <= kGfx1250Heads16mx1)
csrc/py_itfs_cu/pa_sparse_prefill_opus_kernels.cu:488
- If H==0 (valid tensor shape in PyTorch), ceil_div(H, ...) will produce a 0 grid.y and hipModuleLaunchKernel will be called with a zero-sized grid, which is typically invalid. Add an explicit early-return for H==0 before selecting and launching any prebuilt kernel.
size_t arg_size = sizeof(args);
// 16mx1_16nx4 for H <= 16, 32mx1_16nx4 for H <= 32, else the clustered 16mx4_64nx1.
if(H <= kGfx1250Heads16mx1)
- Files reviewed: 1/7 changed files
- Comments generated: 1
- Review effort level: Lite
There was a problem hiding this comment.
🔵 Needs a closer look
The new narrow-head launch paths can still attempt a HIP launch with gridDimY=0 when H==0, which is an invalid launch configuration and should be guarded.
Review details
Suppressed comments (3)
csrc/py_itfs_cu/pa_sparse_prefill_opus_kernels.cu:487
- When H==0 (possible with an empty [N,0,D] tensor), this path will compute gdy=ceil_div(0, …)==0 and attempt a HIP launch with gridDimY=0, which is invalid. Consider early-returning on H==0 (mirroring the existing N==0 guard) before selecting/launching a code object.
size_t arg_size = sizeof(args);
// 16mx1_16nx4 for H <= 16, 32mx1_16nx4 for H <= 32, else the clustered 16mx4_64nx1.
csrc/py_itfs_cu/pa_sparse_prefill_opus_kernels.cu:662
- Same issue as the bf16 path: if H==0, gdy becomes 0 and the kernel launch uses gridDimY=0. Adding an early return for H==0 avoids an invalid launch for empty-head tensors.
size_t arg_size = sizeof(args);
// 16mx1_16nx4 for H <= 16, 32mx1_16nx4 for H <= 32, else the clustered 16mx4_64nx1.
csrc/py_itfs_cu/pa_sparse_prefill_opus_kernels.cu:369
- kGfx1250Heads16mx1 duplicates kGfx1250QTileSize (16) and kGfx1250Heads32mx1 is a simple multiple of it; defining these in terms of kGfx1250QTileSize reduces the chance of the dispatch thresholds drifting if the underlying tile size changes.
// Narrow-head variants: T_M=1, so heads per workgroup is Q_TILE_SIZE alone.
constexpr int kGfx1250Heads16mx1 = 16;
constexpr int kGfx1250Heads32mx1 = 32;
- Files reviewed: 1/7 changed files
- Comments generated: 0 new
- Review effort level: Lite
There was a problem hiding this comment.
🔵 Needs a closer look
It changes low-level GPU kernel dispatch and relies on prebuilt gfx1250 code objects, which warrants human validation (correctness/perf) beyond static review.
Review details
- Files reviewed: 6/12 changed files
- Comments generated: 1
- Review effort level: Lite
There was a problem hiding this comment.
🔵 Needs a closer look
It changes GPU-kernel dispatch and ABI-sensitive prebuilt code-object loading paths (gfx1250), which is high impact and warrants final human review despite limited textual diffs.
Review details
Suppressed comments (1)
aiter/ops/pa_sparse_prefill_opus.py:28
- The docstring points to
aiter/csrc/include/mla_v4_prefill_opus.h, but that path does not exist in the repo; the header lives undercsrc/include/mla_v4_prefill_opus.h. This makes the reference misleading for users trying to find the C++ API.
See ``aiter/csrc/include/mla_v4_prefill_opus.h`` for the C++ API.
- Files reviewed: 6/12 changed files
- Comments generated: 0 new
- Review effort level: Lite
There was a problem hiding this comment.
🔵 Needs a closer look
There are confirmed correctness/documentation issues in the updated gfx1250 launch paths (missing H==0 early-return leading to invalid zero-dimension kernel launches) and an incorrect header path reference in the Python docstring.
Review details
Suppressed comments (3)
Previously missed (1) — in code that hasn't changed since the last review.
csrc/py_itfs_cu/mla_v4_prefill_opus_kernels.cu:498
- If
H == 0, this path will computegrid.y = ceil_div(0, ...) == 0and still attempt a HIP module launch.hipModuleLaunchKerneldoes not accept zero grid dimensions, so this should early-return before dispatch.
This issue also appears on line 674 of the same file.
aiter/ops/pa_sparse_prefill_opus.py:28
- The docstring points to
aiter/csrc/include/mla_v4_prefill_opus.h, but that path does not exist in the repo (the header lives undercsrc/include/). This makes the reference misleading for readers.
See ``aiter/csrc/include/mla_v4_prefill_opus.h`` for the C++ API.
csrc/py_itfs_cu/mla_v4_prefill_opus_kernels.cu:677
- If
H == 0, this path will computegrid.y = ceil_div(0, ...) == 0and still attempt a HIP module launch.hipModuleLaunchKerneldoes not accept zero grid dimensions, so this should early-return before dispatch.
- Files reviewed: 7/13 changed files
- Comments generated: 0 new
- Review effort level: Lite
There was a problem hiding this comment.
🟡 Changes recommended
The new/updated launch paths can still attempt invalid HIP launches when H==0 (grid.y==0), and the PR description claims the Python file is unchanged despite a real module-name/doc change.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Review details
Suppressed comments (4)
csrc/py_itfs_cu/mla_v4_prefill_opus_kernels.cu:86
- If H==0 (but N>0), the launch grid.y becomes 0 (via ceil_div(H, ...)), which is an invalid HIP launch configuration and will error at runtime. Consider early-returning (or validating) on H==0 the same way as N==0.
csrc/py_itfs_cu/mla_v4_prefill_opus_kernels.cu:242 - If H==0 (but N>0), subsequent grid.y computations become 0 and the kernel launch will fail. Add an H==0 early return (or validation) alongside the existing N==0 guard.
csrc/py_itfs_cu/mla_v4_prefill_opus_kernels.cu:644 - If H==0 (but N>0), the narrow-head/cluster dispatch will compute a grid.y of 0 (ceil_div(H, ...)) and the prebuilt-kernel launch will fail. Add an H==0 early return alongside the existing N==0 guard.
csrc/py_itfs_cu/mla_v4_prefill_opus_kernels.cu:469 - If H==0 (but N>0), the narrow-head/cluster dispatch will compute a grid.y of 0 (ceil_div(H, ...)) and the prebuilt-kernel launch will fail. Consider guarding H==0 the same way as N==0.
- Files reviewed: 7/13 changed files
- Comments generated: 2
- Review effort level: Lite
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
There was a problem hiding this comment.
🔵 Needs a closer look
It changes GPU dispatch behavior and relies on prebuilt gfx1250 code objects/ABI matching that can’t be fully validated from the diff alone.
Review details
Suppressed comments (1)
Previously missed (1) — in code that hasn't changed since the last review.
csrc/include/mla_v4_prefill_opus.h:44
- The comment "gfx1250: only the bf16 variant is built into the code object." is now ambiguous/misleading because this header also declares the gfx1250 fp8 entry point below; it reads like gfx1250 has no fp8 support. Consider clarifying that this applies to the bf16 (a16w16) code objects/entry point only.
- Files reviewed: 7/13 changed files
- Comments generated: 0 new
- Review effort level: Lite
Summary
DSv4 under tensor parallelism leaves each rank with only 16 or 32 query heads. The existing gfx1250 code object
16mx4_64nx1coversQ_TILE_SIZE(16) * T_M(4) = 64heads per workgroup, so at H=16/32 half to three quarters of the workgroup's head slots are idle.hsa/gfx1250/mla_v4_opus/:16mx1_16nx4and32mx1_16nx4, for both bf16 (a16w16) and mxfp8 (a8w8). These useT_M=1and spread the 4 waves along N (KV) instead of along heads, so a 16- or 32-head workgroup still fills the whole block.opus_mla_v4_prefill_a16w16_gfx1250_fwdandopus_mla_v4_prefill_a8w8_gfx1250_fwd):H <= 16picks16mx1_16nx4,H <= 32picks32mx1_16nx4, everything else keeps the current clustered16mx4_64nx1path unchanged. The narrow-head paths always launch withgrid.y == 1, so they skip the cluster gather and its "cluster width must divide grid.y" constraint entirely.16mx4_64nx1bf16/fp8 code objects from the same source revision; launch geometry and dispatch for H > 32 are untouched.Naming unification
The op previously mixed three vocabularies (
pa_sparse_prefill_*in the host layer,pa_prefill_*in the device templates,mla_prefill_*in the code objects). Everything below the Python layer now follows a singleopus_mla_v4_prefill_<dtype>_<layout>[_<variant>]grammar, witha16w16/a8w8as the uniform precision notation:csrc/include/pa_sparse_prefill_opus.h→mla_v4_prefill_opus.h(same for_kernels.cu/_pybind.cu), andmodule_pa_sparse_prefill_opus→module_mla_v4_prefill_opus..cofiles and their exported symbols are nowopus_mla_v4_prefill_a{16w16,8w8}_<layout>[_cy{1,2}], matching the kernel templates and traits they were built from.pa_sparse_prefill[_fp8]_gfx{950,1250}_opus_fwd→opus_mla_v4_prefill_{a16w16,a8w8}_gfx{950,1250}_fwd. Them.defbinding strings inrocm_ops.hppare deliberately left unchanged, so the pybind-exported names and the entire Python layer are untouched.No Python-level or public API change:
aiter/ops/pa_sparse_prefill_opus.pyandop_tests/test_pa_sparse_prefill.pyare byte-identical to main. gfx950 is functionally untouched (renames only).Test plan
On gfx1250, after clearing the JIT cache and rebuilding the module from scratch:
PYTHONPATH=. pytest op_tests/test_pa_sparse_prefill.py— 24 passed. The sweep covers H = 16 / 32 / 64 / 128 across {bf16, fp8} x {sparse, dense, empty}; the first two head counts exercise the new narrow-head code objects and the last two confirm the H > 32 clustered path is unchanged.PYTHONPATH=. python3 op_tests/test_pa_sparse_prefill.py --prec bf16 fp8 --h_q 16 32 64 128 --mode sparse dense empty --backend opus— 24/24 witherr = 0.0on every shape. All eight gfx1250 code-object symbols (16mx1,32mx1,16mx4_64nx1_cy1,16mx4_64nx1_cy2xa16w16,a8w8) are exercised by this sweep.