← Back to Pull requests

perf(tensilelite): wave-parallel group search in grouped GEMM kernels #14788

Closed
unknown closed this pull request without merging on Nov 17, 2026 00:12 UTC
View on GitHub
unknown unknown commented on Oct 30, 2026 20:57 UTC
Author

JIRA ID : AIHPBLAS-1828

Motivation

A grouped GEMM kernel computes many independent matrix products, or groups, in one launch; a mixture-of-experts (MoE) layer, for example, runs one group per expert. Each workgroup, the block of GPU threads that computes one output tile, first finds the group its tile belongs to. TensileLite's grouped kernels do this with a scalar loop that loads one group's sizes, waits for the load and adds that group's tile count, so a workgroup pays one dependent memory round trip (about 0.4 µs on MI355X) for every group before its own.

The launch grid is sized for the worst case, and the workgroups past the last real tile scan every group before they exit. With the 384 groups of a DeepSeek V4 MoE layer, kernel time follows the group count instead of the work: on MI355X, the 5120 x 576 down projection for one token takes 2,313 µs over 384 groups, against 15 µs when the empty groups are removed.

Technical Details

The kernel change is limited to the grouped-kernel code that KernelWriterAssembly.defineAndResources emits before the FOUND label. Non-grouped kernels are unchanged.

1. Wave-parallel search (groupedGemmWaveSearch):
- Each of the wave's 64 lanes loads the (M, N, batch, K) header of one group with global_load_dwordx4, so one round trip covers 64 groups, and computes that group's tile count: ceil(M / MacroTile0) x ceil(N / MacroTile1) x batch, times GSU when GlobalSplitU is on.
- The wave gives each lane the running total of tiles through its group (an inclusive prefix sum): a DPP row_shr scan by 1, 2, 4 and 8 lanes within each 16-lane row, plus the row totals read with v_readlane. A v_cmp against the workgroup id and a find-first-set pick the first group whose running total exceeds the id.
- Each round trip loads up to 6 chunks of 64 groups (384 groups); with 64 groups or fewer it issues one load. groupedGemmWaveSearchChunks picks the largest chunk count whose VGPRs, 4 per chunk plus 5 scratch, fit as one block in the VGPRs the kernel can use without lowering its occupancy (getMaxRegsForOccupancy, which accounts for LDS). A kernel where not even one chunk fits keeps the loop.
- Lanes past the last group load group 0, which keeps the load in bounds, and count 0 tiles. Workgroups past the last tile resolve to the last group, as before.
- At FOUND, the search leaves the same registers as the loop: the group's sizes, its tile count, the running tile total through the group and the group index + 1. The code after FOUND is unchanged.
- A launch with one group skips the search and takes the existing scalar single-group path; the vector load and scan cost single-group launches up to 6.5%.
2. Where it applies (groupedGemmWaveSearchSupported): gfx942 and gfx950 kernels with 64-thread waves, power-of-two MacroTile0 and MacroTile1, and no per-group GSU code in the loop (MultipleBufferSingleKernel and AdaptiveGemmGSUA). All other kernels keep the loop, including gfx908, gfx90a and gfx1250.
3. Grouped heuristic host time (a866d6b4b78): the grouped C API asks for at least 8 candidates. When the libraries return fewer, rocblaslt_algo_get_heuristic_cpp checks every solution of the same types from getAllSolutions(GEMM_TYPE_ONLY), grouped or not (19,987 with the full gfx950 library). isSolutionSupported sized the grouped workspace, which builds kernel arguments for every group, before rejecting them: about 40 s per heuristic call at 1000 groups. It now rejects solutions that are not grouped or not for this hardware first. Selection is unchanged.

Test Plan

On MI355X (gfx950), with only the grouped logic files in the build (TENSILELITE_LOGIC_FILTER=_GG_) unless noted:

  • gtests (GroupedMatmul. and all rouped tests):

  • - smoke_Fp16TN65Groups: uneven shapes with empty groups (M = 0 or N = 0) and K = 0.
    - smoke_Fp16TN400Groups: one heuristic call and launch setup for four calls: 400 equal groups (one workgroup per tile, so no surplus workgroup can cover a missed tile), uneven groups, then only the first and only the last group non-empty. Alpha and beta are per group.
    - pre_checkin_Fp16TN1000Groups: the same with 1000 groups, in the standard tier.
    - smoke_MXFP8TNPreSwizzled385Groups: MXFP8 in the MoE layout; gfx950 only.
    - Group 0 is non-empty in these tests, every grouped test checks that D's ld padding is not written, and the workspace starts filled with ones so a record read past the last group faults.
    - The same tests with develop's grouped kernels.
  • Heuristic time: the grouped gtests with a full gfx950 library, with and without a866d6b4b78.

  • TensileLite configs (Tests/common/groupedgemm), with full validation: grouped_gemm.yaml, grouped_gemm_userargs.yaml, grouped_gemm_many_groups.yaml (400 gemms, MT128x128, GSU 1 and 2) and grouped_gemm_many_groups_userargs.yaml (GSU 1; GSU 2 with user arguments fails to launch on develop too).

  • Wrong picks are caught: six deliberately broken builds:

  • - group index off by one after the first chunk;
    - each further load starting 64 groups early;
    - row cutover off by one;
    - tile total not carried between chunks;
    - past-the-end path resolving to one group beyond the last;
    - out-of-range lanes keeping group 0's tile count.
  • Chunk budget: Tests/unit/test_grouped_gemm_wave_search_chunks.py, against the gfx942 and gfx950 caps, and five broken versions of groupedGemmWaveSearchChunks.

  • PyTorch harness (local): hipblasLtMatmul with grouped layouts on the MXFP8, MXFP4 and FP16 grouped kernels, for 12 group patterns from 1 to 1000 groups, compared with per-group torch.matmul; also used for the benchmark.

  • Registers: VGPR, SGPR and LDS counts and chunks per load from the code objects of every generated grouped kernel: gfx950 (14), gfx942 (22,513, generated but not run) and gfx1250 (24), against develop.

  • Benchmark: single MXFP8 grouped GEMMs with DeepSeek V4 MoE shapes, gate_up (1152 x 5120) and down (5120 x 576), with N tokens per group.

  • TensileLite unit tests (Tests/unit, marker unit).
  • Test Result

  • gtests: in GroupedMatmul, 21 run and pass, and 8 skip on gfx950 for lack of grouped kernels (FP16 NN and TT, BF16, MX without pre-swizzled scales, and the MX-and-FP8 selection test). All other rouped tests pass except AlgoErrors.smoke_RowOrderIsRefusedBesideAGroupedLayout, which needs a non-grouped kernel the grouped-only build lacks; it passes with the full library. Before the rebase, the tests also passed with develop's grouped kernels.

  • Heuristic time (full gfx950 library): pre_checkin_Fp16TN1000Groups 43.6 s to 1.6 s, smoke_Fp16TN400Groups 18.1 s to 0.67 s, all rouped* tests 170 s to 22 s with the same results.

  • TensileLite configs: all four pass (run before the rebase; unchanged since apart from a comment).

  • Broken builds: each of the six fails smoke_Fp16TN400Groups; the two past-the-end builds fault in every many-group test. A broken GSU multiply is caught by grouped_gemm_many_groups.yaml with GSU 2.

  • Chunk budget: 27 cases pass; each of the five broken versions fails.

  • PyTorch harness: all 84 cases pass on both builds.

  • Registers:

  • - gfx950: all 14 kernels take the search (4 to 6 chunks per load). The MXFP8 and MXFP4 MT64x64 kernels keep 82 to 87 VGPRs; the FP16 MT16x16 kernels grow from 43-47 to 53 VGPRs, which their LDS use already limits to 6 waves per SIMD.
    - gfx942: 7,241 kernels take the search (7,034 with 6 chunks per load); their VGPR count grows by 0 to 35, and by an estimate from VGPRs, LDS and workgroup size none runs fewer waves per SIMD. The FP16 TN grouped kernel the heuristic returns first (the only GridBased solution, MT16x16x32) takes the search with 6 chunks per load at 53 VGPRs.
    - SGPR counts and every kernel that keeps the loop are unchanged; the gfx1250 grouped code objects are byte-identical to develop's.
  • Benchmark (µs per grouped GEMM, 20 iterations):
  • | case | gate_up develop | gate_up PR | down develop | down PR |
    | --- | --- | --- | --- | --- |
    | 1 token, 384 groups | 579 | 43.3 | 2,313 | 129 |
    | 1 token, 6 groups | 22.2 | 21.4 | 15.2 | 13.9 |
    | 16 tokens, 384 groups | 616 | 123 | 2,075 | 198 |
    | 16 tokens, 96 groups | 110 | 99.2 | 129 | 87.1 |
    | 256 tokens, 384 groups | 853 | 453 | 1,438 | 453 |
    | 4096 tokens, 384 groups | 1,426 | 687 | 3,117 | 753 |
    | 1 group of 64 tokens, 383 empty | 577 | 36.9 | 2,318 | 126 |
    | 384 groups of 64 tokens | 895 | 592 | 1,357 | 542 |
    | 1 group of 24,576 tokens | 280 | 277 | 316 | 311 |

    With few groups (median of 7 runs of 200 iterations), the change is within ±1% for one group and 2.6% to 2.7% faster for 6 groups:

    | case | gate_up develop | gate_up PR | down develop | down PR |
    | --- | --- | --- | --- | --- |
    | 1 group of 64 tokens | 16.1 | 16.2 | 10.7 | 10.5 |
    | 1 group of 24,576 tokens | 270 | 270 | 301 | 304 |
    | 1 token, 6 groups | 21.0 | 20.4 | 10.7 | 10.4 |
    | 16 tokens, 96 groups | 108 | 97.4 | 121 | 80.3 |

  • TensileLite unit tests: all pass except test_arch_steppings.py, test_custom_kernel_staggeru_metadata.py and the GPU tile-assignment tests, which fail the same way on develop in my container.

  • Not run: any GPU other than gfx950, and the ASAN lanes; on gfx942 the gtests run in CI's gfx94X hipBLASLt lane.
  • Submission Checklist

  • Look over the contributing guidelines at <https://github.com/ROCm/TheRock/blob/main/GOVERNANCE.md#pull-requests>.
  • Risk level

    Medium (3/5): the change rewrites the group search of the grouped kernels with power-of-two tiles on gfx942 and gfx950. A wrong group pick computes a tile with another group's sizes and pointers: that group's tile is written twice and the intended tile is never written, and no error is reported. The gtests and configs catch such picks, as the six broken builds show. On gfx942 the search has run only in CI and in a reviewer's runs. The heuristic fix only rejects, earlier, solutions that were already rejected.

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: yzhang93 Review duration: 24675m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #14788"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.5% · Safe to merge
    20.5%