Skip to content

[Perf] hipb_mm: keep hipBLASLt layouts, descriptor and algorithm per configuration - #8116

Merged
ZLkanyo009 merged 3 commits into
mainfrom
lingzha/hipb-mm-plan-reuse
Nov 24, 2026
Merged

ZLkanyo009 merged 3 commits into
mainfrom
lingzha/hipb-mm-plan-reuse

Conversation

@ZLkanyo009

@ZLkanyo009 ZLkanyo009 commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

hipb_mm builds three hipBLASLt matrix layouts, a matmul descriptor and the algorithm on every call, then destroys them. For an unscaled call with a given solution index these are the same on every call with the same configuration, so this PR keeps them, per configuration and per thread, and only sets the bias pointer again on a hit. Results are bit-identical; host time per hipBLASLt GEMM drops by about 9 µs warm and 14 µs cold on MI308X.

This was the third commit of #8030 and is split out of it: it is independent of #8030's Python fast path, and benefits the existing custom-op path on its own.

Motivation

Outside torch.compile a BF16 GEMM through tgemm.mm is host-bound: on MI308X one call takes about 55 µs of host time warm and 110 µs cold (numbers below), while the kernels themselves are short. Eager prefill of Qwen3.8-Flash-Next (TP2, 2112 new tokens) issues about 288 of these GEMMs per forward per rank, all tuned to hipBLASLt solution indices.

Part of that host time is in hipblasLtMatmul_sol_wrapper: per call it creates three hipblasLtMatrixLayout_t, one hipblasLtMatmulDesc_t with its attributes, looks the algorithm up with hipblaslt_ext::getAlgosFromIndex, and destroys all of it after hipblasLtMatmul. For a given configuration and solution index, every one of these is the same on every call.

Changes

gradlib/csrc/hipbsolgemm.cu only:

  • The layout and descriptor construction moves unchanged into create_matmul_descs(), which both paths call.
  • For calls with solution_index >= 0, no scaleA / scaleB / scaleC and online tuning off, the layouts, descriptor and algorithm are kept, keyed on (op_A, op_B, m, n, k, lda, ldb, ldc, input type, output type, solution index, bpreshuffle, bias on/off, gelu). That is every input of the construction except the bias pointer, which a hit sets on the kept descriptor before hipblasLtMatmul. Scale modes only take effect with a scale pointer, so they cannot differ between calls that reach the kept path.
  • Scaled calls, the heuristic path (solution_index < 0) and online tuning build and destroy per call, as before. HIP_ONLINE_TUNING is still read on every call (op_tests/test_hipb_mm.py sets it at run time).
  • The kept plans are per thread (thread_local), so a descriptor is never shared between threads: with one shared map, two threads could set two bias pointers on one descriptor or race on insert and clear. A thread's plans are released when it exits.
  • At most 1024 configurations per thread: prefill sees a new M per batch, so past that the set is dropped and rebuilt rather than grown without bound.
  • hipb_destroy_extension releases the calling thread's plans and bumps a generation counter; every other thread drops the plans it built for the old extension before its next lookup.

op_tests/test_hipb_mm_reuse.py is new (see Changed op tests).

Performance (if applicable)

Host time of one Python call, without a profiler: bursts of 16 calls with a sync between bursts, median of 5 rounds, weighted by how often each of the 7 prefill GEMM shapes of Qwen3.8-Flash-Next at TP2 occurs (288 calls per forward). "Cold" sweeps 8 MiB and runs unrelated Python between two timed calls, the way the rest of a forward does. The two builds ran one after the other in separate processes, three times each, alternating, on the same GPU and pinned CPU core; the table shows the median of the three runs. Both module_hipbsolgemm builds were JIT-built the same way, and the output SHA-256 of every shape is the same for both.

Hardware Workload/configuration Before (main b9e08da) After (7df92f2) Change
MI308X (gfx942, 80 CU), ROCm 7.2.4, torch 2.10 tgemm.mm, BF16, 7 prefill shapes weighted, warm 54.6 µs 45.2 µs −9.4 µs (−17%)
same same, cold 110.1 µs 95.7 µs −14.4 µs (−13%)
same host time of the 288 GEMMs of one prefill forward, warm / cold 15.7 / 31.7 ms 13.0 / 27.6 ms −2.7 / −4.1 ms

Every shape moves by about the same amount (warm −8.9 to −10.1 µs, cold −13.8 to −16.1 µs). The three runs per build were 53.9 / 59.8 / 54.6 vs 45.2 / 45.0 / 45.4 µs warm and 109.7 / 111.4 / 110.1 vs 95.7 / 95.2 / 96.9 µs cold; torch.mm(out=) on the same shapes, timed in the same processes as a control, stayed at 18.6–18.9 µs warm and 36.6–37.3 µs cold.

Together with #8030's eager fast path (its head after this split, in a separate series of three alternating runs), tgemm.mm goes from 28.0 to 21.8 µs warm and from 56.8 to 44.7 µs cold, against 54.1 / 111.0 µs on main in that series.

End to end (earlier measurement, on #8030 at 48b6ddb)

Before the split, this change was measured end to end on #8030's branch at 48b6ddb (base 95d2112, together with #8030's fast path, with HIP_ONLINE_TUNING read once instead of per call), against the fast path alone: TTFT at c=1, 8192 input tokens with a 6080-token prefix hit, TP2/EP2 with MTP on 2× MI308X, went from 217.1 to 208.9 ms on the ATOM SGLang plugin (one clean server run against three) and from 373.5 to 353.0 ms on native ATOM (one run against two). These single runs are noisy and were not repeated on this branch.

Testing

  • Unit tests added or updated
  • Performance benchmarks run
  • Tested on MI250X
  • Tested on MI300X (MI308X, gfx942)

Documentation

  • Docstrings updated
  • User guide updated
  • Performance guide updated
  • No documentation change is needed, with the reason stated below

hipb_mm's interface and results are unchanged; the kept plans are internal.

Dependencies

  • No new third-party dependencies added
  • New dependencies are listed and justified below

Breaking Changes

None.

PR Self-Review

Reviewed revision

3223814

Behavior and intent

Behavior change: hipb_mm calls with a solution index >= 0, no scale pointers and online tuning off keep their hipBLASLt layouts, matmul descriptor and algorithm per configuration and per thread, and set only the bias pointer on later calls. Results, the scaled path, the heuristic path (solution index < 0), online tuning and every Python API are unchanged.

Why the existing behavior is insufficient: each call rebuilds and destroys the same layouts, descriptor and algorithm. That is about 9 µs (warm) to 14 µs (cold) of host time per call on MI308X, out of 55 / 110 µs, and eager prefill of Qwen3.8-Flash-Next issues about 288 such GEMMs per forward, so 2.7–4.1 ms of host time per forward on a host-bound path.

Validation and unverified scope

Current head 32238149d3fffdc0cb213cdf36208aacf858bea3 (2026-11-23) merges main 2eb9ef3657777eebc0da3434074f4a67e1151c54. The teardown conflict retains both this PR's plan-cache clear/generation invalidation and main's guarded w4a16_destroy_plans(), before destroying the hipBLASLt handle. The C++ additions/removals relative to main match the pre-merge PR, and the reuse test is unchanged.

Fresh validation on this exact head, one MI308X/gfx942, torch 2.10.0+rocm7.2.4.git3d3aa833, HIP 7.2.53211:

  • Rebuilt module_hipbsolgemm from the merged source and headers; CPU build/import, Black, Ruff and whitespace checks pass.
  • Original reuse test: 5 sweep cases and 29 checks pass (33.35 s), including bias-pointer changes, 1100 configurations, changed-input graph replay, four threads, destroy/recreate generation, and short-lived threads. All comparisons took the bitwise path. A reporting-only wrapper pins seed 8116 and asserts complete coverage; the test's assertions, graph checks and timing helper are unchanged.
  • python3 -m pytest -q op_tests/test_hipb_mm.py: 47 passed, 18 skipped (34.80 s). Skips are feature/architecture limits, not passes.
  • Local ROCm 7.2.4 headers do not provide the W4A16 API, so the enabled W4A16 compilation/runtime branch is not validated locally. Its teardown is retained from main; gfx950/MXFP8 coverage and fresh CI remain pending.
  • No new performance or TTFT measurement. The version-labelled historical evidence below retains its original scope.

Historical validation follows (not measurements of the new merge head):

Validated at 7df92f2 on MI308X (gfx942, 80 CU), ROCm 7.2.4, torch 2.10, with module_hipbsolgemm JIT-built from this branch (GPU_ARCHS=gfx942) and the image's prebuilt modules of the same sources for the rest; main b9e08da built and run the same way as the baseline:

  • python3 op_tests/test_hipb_mm_reuse.py: passes (5 sweep cases, 29 checks, 33 s), every comparison on the bit-for-bit path.
  • Mutations of hipbsolgemm.cu, each JIT-built and run against the test: the bias pointer not set on a hit fails test_kept_descriptors (bias case, every element differs, max |diff| 4.28); ldb left out of the key fails check_configurations_do_not_mix (rows past the first differ, max |diff| 6.77). The file stops at its first failure.
  • op_tests/test_gemm_a16w16.py -t normal, -t out, -t compile and python3 -m pytest op_tests/test_hipb_mm.py give the same results on main and on this branch: normal and compile pass; -t out fails on both with the same asm case (out= not honoured by: ['asm tuned 256x256x4096']), a backend this PR does not touch; test_hipb_mm.py is 47 passed, 18 skipped on both.
  • Host-time benchmark: see Performance; the output SHA-256 of all 7 shapes is the same on both builds in all 12 runs.
  • black 26.10.1 and ruff 0.16.0: clean on the new test.

Unverified:

  • gfx950 and gfx1250 were not run here (no hardware). aiter CI runs the new test on MI35X; an earlier version of it found a hipBLASLt kernel that is not reproducible on gfx950 at K = 64 ([JIT] [Perf] Reduce Python overhead in tuned GEMM dispatch #8030, see Changed op tests), which the test handles.
  • True concurrent calls from threads that do not hold the GIL: the pybind binding holds the GIL, so the thread checks cover per-thread build, reuse, release and invalidation, not a data race. A build with one shared map passes them too.
  • No TTFT run on this branch; the end-to-end numbers above are from [JIT] [Perf] Reduce Python overhead in tuned GEMM dispatch #8030 before the split.
  • Not changed: the hipBLASLt workspace (d_workspace) is one buffer per extension, as before, so two streams calling hipb_mm at once can still share it.

PR-specific question

Which architecture, dtype and shape boundaries select the changed path, and what keeps unsupported inputs out: no kernel changes. The changed path is host-side, in hipblasLtMatmul_sol_wrapper, and is selected only by the call's arguments, the same on every arch, dtype and shape hipb_mm already handles:

  • solution_index >= 0 (a tuned or caller-chosen solution), and
  • scaleA, scaleB and scaleC all null, and
  • HIP_ONLINE_TUNING off (online tuning may replace the solution index from its CSV).

Everything else (scaled FP8 / MXFP8 calls, the heuristic path, online tuning) builds and destroys per call as before.

Invariant: a kept plan is what create_matmul_descs and getAlgosFromIndex build for its key, and the key holds every input of that construction except the bias pointer, which is set on every hit. Scale modes are applied only together with a scale pointer, so calls on the kept path never differ in them. So a hit launches the same algorithm with the same descriptor contents as a fresh build, and hipblasLtMatmul validates the problem exactly as before. A plan is used only by the thread that built it and only with the extension it was built for (generation counter); the set per thread is bounded at 1024.

Changed op tests

  • op_tests/test_hipb_mm_reuse.py (new) checks calls served by kept descriptors against freshly built ones (destroy and recreate the extension):

    • five shapes and dtypes (bf16 / fp16, fp32 output, bias on and off) and, with a bias, the same configuration with another bias, where only the pointer may change; the table carries the kept call's kernel time;
    • interleaved shapes, leading dimensions (a strided activation) and bias on / off;
    • 1100 configurations, more than are kept, checked before and after the set is dropped;
    • CUDA graph capture and replay on a kept plan;
    • threads: 4 threads interleaving four configurations with a bias of their own, a worker that outlives an extension destroy / create, 16 short-lived threads.

    Each comparison is bit for bit when two fresh builds agree bit for bit (four more fresh results are taken before a difference fails), and otherwise against a float32 torch reference, which every fresh result must also match. That split comes from [JIT] [Perf] Reduce Python overhead in tuned GEMM dispatch #8030's MI35X runs: on gfx950 the first solution hipBLASLt lists for a 64×64×64 problem is a hand-written kernel that is not reproducible run to run at K = 64 (analysis), so the many-configurations check uses K = 512.

    It is not redundant: no existing test calls hipb_mm twice with one configuration and compares with a fresh build. Kept and fresh descriptors give the same results by design, so the test passes on main as well and cannot show that plans are kept (the benchmark above does); it fails on reuse bugs, which pass every existing test: a stale bias pointer on a hit fails test_kept_descriptors, ldb left out of the key fails check_configurations_do_not_mix.

cc @valarLip

…ration

An unscaled hipb_mm call with a given solution index builds three matrix
layouts, a matmul descriptor and the algorithm, runs the GEMM and destroys
them, and the next call with the same configuration builds the same ones
again. Keep them per configuration instead, keyed on (op_A, op_B, m, n, k,
lda, ldb, ldc, in/out type, solution index, bpreshuffle, bias on/off, gelu);
a hit only sets the bias pointer again. Scaled calls, the heuristic path
(solution index < 0) and online tuning build and destroy per call as before.

- The descriptor construction moves unchanged into create_matmul_descs(),
  which both paths call.
- The kept plans are per thread (thread_local): a hit sets the bias pointer
  on its descriptor before hipblasLtMatmul, so a descriptor is never shared
  between threads. A thread's plans are released when it exits.
- At most 1024 configurations per thread; past that the set is dropped and
  rebuilt, since prefill sees a new M per batch.
- hipb_destroy_extension releases the calling thread's plans and bumps a
  generation counter; every other thread drops plans built for the old
  extension before its next lookup.

Host time of one tgemm.mm call (the custom-op path), weighted over seven
prefill GEMM shapes of Qwen3.8-Flash-Next at TP2 (288 calls per forward),
MI308X, main b9e08da vs this commit, both modules JIT-built the same way,
three alternating runs each: warm 54.6 -> 45.2 us, cold 110.1 -> 95.7 us.
Outputs are bit-identical (same SHA-256 per shape).

op_tests/test_hipb_mm_reuse.py compares results served by kept descriptors
with freshly built ones. Kept and fresh descriptors give the same results by
design, so the test cannot tell whether plans are kept; it fails on reuse
bugs: with the bias pointer left stale on a hit, test_kept_descriptors fails;
with ldb left out of the key, check_configurations_do_not_mix fails.
@ZLkanyo009
ZLkanyo009 requested a review from a team November 17, 2026 14:01
@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 8116 --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.

ZLkanyo009 added a commit that referenced this pull request Nov 17, 2026
The C++ change in gradlib/csrc/hipbsolgemm.cu (keep the hipBLASLt layouts,
descriptor and algorithm per configuration and per thread) and its test,
op_tests/test_hipb_mm_reuse.py, do not depend on the Python fast path of
this PR, and the fast path does not depend on them. They are now #8116, so
each can be reviewed and reverted on its own; this PR keeps the eager fast
path in aiter/tuned_gemm.py and op_tests/test_tuned_gemm_eager.py.

hipbsolgemm.cu is back to main's version and the test file is removed.
Nothing in aiter/tuned_gemm.py or test_tuned_gemm_eager.py existed only for
the plan reuse, so neither changes.
@ZLkanyo009

Copy link
Copy Markdown
Contributor Author

Where this comes from, for anyone who saw it in #8030: this single commit (7df92f2, from main b9e08da) replaces #8030's 48b6ddb (plans kept per configuration), 07fd771 (per thread) and the test work on op_tests/test_hipb_mm_reuse.py (be8b061, aadb785, 5a54959), which #8030 dropped in 8985895. #8030 was over the 1000-line review bar, and the two halves do not depend on each other.

One change from the #8030 version: HIP_ONLINE_TUNING is read on every call again, as on main, instead of once per process. op_tests/test_hipb_mm.py::test_hipb_mm_falls_back_for_unrepresentable_tuning_modes (gfx950) sets it at run time with monkeypatch.setenv. With a value read once by an earlier hipb_mm call in the same process, that test would no longer reach the online-tuning branch it checks, and would pass without checking it. Keeping plans needs only !online_tuning on each call, not the read-once.

Verification at 7df92f2 (MI308X, gfx942) and the numbers are in the description:

  • test_hipb_mm_reuse.py passes;
  • a stale bias pointer and ldb left out of the key each make it fail;
  • test_gemm_a16w16.py and test_hipb_mm.py give the same results as on main;
  • one tgemm.mm call takes 54.6 → 45.2 µs warm and 110.1 → 95.7 µs cold.

The MI35X history of the test (a hipBLASLt kernel that is not reproducible at K = 64 on gfx950) is in #8030's comment.

Waiting for: MI35X Aiter Test on 7df92f2.

cc @valarLip

@aiter-bot

aiter-bot commented Nov 17, 2026 •

Copy link
Copy Markdown

READY FOR HUMAN REVIEW

PR Self-Review

Revision 32238149d3ff · mode shadow

Author self-review accepted. Readiness report:

Author answers

  • PASS — current-SHA answers are present and structurally specific
Show the author's self-review answers

Behavior and intent

Behavior change: `hipb_mm` calls with a solution index >= 0, no scale pointers and online tuning off keep their hipBLASLt layouts, matmul descriptor and algorithm per configuration and per thread, and set only the bias pointer on later calls. Results, the scaled path, the heuristic path (solution index < 0), online tuning and every Python API are unchanged.

Why the existing behavior is insufficient: each call rebuilds and destroys the same layouts, descriptor and algorithm. That is about 9 µs (warm) to 14 µs (cold) of host time per call on MI308X, out of 55 / 110 µs, and eager prefill of Qwen3.8-Flash-Next issues about 288 such GEMMs per forward, so 2.7–4.1 ms of host time per forward on a host-bound path.

Validation and unverified scope

Current head `32238149d3fffdc0cb213cdf36208aacf858bea3` (2026-11-23) merges main `2eb9ef3657777eebc0da3434074f4a67e1151c54`. The teardown conflict retains both this PR's plan-cache clear/generation invalidation and main's guarded `w4a16_destroy_plans()`, before destroying the hipBLASLt handle. The C++ additions/removals relative to main match the pre-merge PR, and the reuse test is unchanged.

Fresh validation on this exact head, one MI308X/gfx942, torch `2.10.0+rocm7.2.4.git3d3aa833`, HIP `7.2.53211`:

- Rebuilt `module_hipbsolgemm` from the merged source and headers; CPU build/import, Black, Ruff and whitespace checks pass.
- Original reuse test: **5 sweep cases and 29 checks pass** (33.35 s), including bias-pointer changes, 1100 configurations, changed-input graph replay, four threads, destroy/recreate generation, and short-lived threads. All comparisons took the bitwise path. A reporting-only wrapper pins seed 8116 and asserts complete coverage; the test's assertions, graph checks and timing helper are unchanged.
- `python3 -m pytest -q op_tests/test_hipb_mm.py`: **47 passed, 18 skipped** (34.80 s). Skips are feature/architecture limits, not passes.
- Local ROCm 7.2.4 headers do not provide the W4A16 API, so the enabled W4A16 compilation/runtime branch is **not validated locally**. Its teardown is retained from main; gfx950/MXFP8 coverage and fresh CI remain pending.
- No new performance or TTFT measurement. The version-labelled historical evidence below retains its original scope.

Historical validation follows (not measurements of the new merge head):

Validated at 7df92f2c08 on MI308X (gfx942, 80 CU), ROCm 7.2.4, torch 2.10, with `module_hipbsolgemm` JIT-built from this branch (`GPU_ARCHS=gfx942`) and the image's prebuilt modules of the same sources for the rest; main b9e08daca built and run the same way as the baseline:

- `python3 op_tests/test_hipb_mm_reuse.py`: passes (5 sweep cases, 29 checks, 33 s), every comparison on the bit-for-bit path.
- Mutations of `hipbsolgemm.cu`, each JIT-built and run against the test: the bias pointer not set on a hit fails `test_kept_descriptors` (bias case, every element differs, max |diff| 4.28); `ldb` left out of the key fails `check_configurations_do_not_mix` (rows past the first differ, max |diff| 6.77). The file stops at its first failure.
- `op_tests/test_gemm_a16w16.py -t normal`, `-t out`, `-t compile` and `python3 -m pytest op_tests/test_hipb_mm.py` give the same results on main and on this branch: `normal` and `compile` pass; `-t out` fails on both with the same asm case (`out= not honoured by: ['asm tuned 256x256x4096']`), a backend this PR does not touch; `test_hipb_mm.py` is 47 passed, 18 skipped on both.
- Host-time benchmark: see Performance; the output SHA-256 of all 7 shapes is the same on both builds in all 12 runs.
- black 26.10.1 and ruff 0.16.0: clean on the new test.

Unverified:

- gfx950 and gfx1250 were not run here (no hardware). aiter CI runs the new test on MI35X; an earlier version of it found a hipBLASLt kernel that is not reproducible on gfx950 at K = 64 (#8030, see Changed op tests), which the test handles.
- True concurrent calls from threads that do not hold the GIL: the pybind binding holds the GIL, so the thread checks cover per-thread build, reuse, release and invalidation, not a data race. A build with one shared map passes them too.
- No TTFT run on this branch; the end-to-end numbers above are from #8030 before the split.
- Not changed: the hipBLASLt workspace (`d_workspace`) is one buffer per extension, as before, so two streams calling `hipb_mm` at once can still share it.

PR-specific question

Which architecture, dtype and shape boundaries select the changed path, and what keeps unsupported inputs out: no kernel changes. The changed path is host-side, in `hipblasLtMatmul_sol_wrapper`, and is selected only by the call's arguments, the same on every arch, dtype and shape `hipb_mm` already handles:

- `solution_index >= 0` (a tuned or caller-chosen solution), and
- `scaleA`, `scaleB` and `scaleC` all null, and
- `HIP_ONLINE_TUNING` off (online tuning may replace the solution index from its CSV).

Everything else (scaled FP8 / MXFP8 calls, the heuristic path, online tuning) builds and destroys per call as before.

Invariant: a kept plan is what `create_matmul_descs` and `getAlgosFromIndex` build for its key, and the key holds every input of that construction except the bias pointer, which is set on every hit. Scale modes are applied only together with a scale pointer, so calls on the kept path never differ in them. So a hit launches the same algorithm with the same descriptor contents as a fresh build, and `hipblasLtMatmul` validates the problem exactly as before. A plan is used only by the thread that built it and only with the extension it was built for (generation counter); the set per thread is bounded at 1024.

Changed op tests

- `op_tests/test_hipb_mm_reuse.py` (new) checks calls served by kept descriptors against freshly built ones (destroy and recreate the extension):
  - five shapes and dtypes (bf16 / fp16, fp32 output, bias on and off) and, with a bias, the same configuration with another bias, where only the pointer may change; the table carries the kept call's kernel time;
  - interleaved shapes, leading dimensions (a strided activation) and bias on / off;
  - 1100 configurations, more than are kept, checked before and after the set is dropped;
  - CUDA graph capture and replay on a kept plan;
  - threads: 4 threads interleaving four configurations with a bias of their own, a worker that outlives an extension destroy / create, 16 short-lived threads.

  Each comparison is bit for bit when two fresh builds agree bit for bit (four more fresh results are taken before a difference fails), and otherwise against a float32 torch reference, which every fresh result must also match. That split comes from #8030's MI35X runs: on gfx950 the first solution hipBLASLt lists for a 64×64×64 problem is a hand-written kernel that is not reproducible run to run at K = 64 ([analysis](https://github.com/ROCm/aiter/pull/8030#issuecomment-6937154820)), so the many-configurations check uses K = 512.

  It is not redundant: no existing test calls `hipb_mm` twice with one configuration and compares with a fresh build. Kept and fresh descriptors give the same results by design, so the test passes on main as well and cannot show that plans are kept (the benchmark above does); it fails on reuse bugs, which pass every existing test: a stale bias pointer on a hit fails `test_kept_descriptors`, `ldb` left out of the key fails `check_configurations_do_not_mix`.

cc @valarLip

PR state

  • PASS — pull request is marked ready for review

Copilot findings

  • PASS — no active Copilot finding lacks a substantive author reply
  • ADVISORY — Copilot did not review the current head SHA

Code and op-test hygiene

  • PASS — no committed artifact or conflict marker detected

Required CI

  • PASS — Black: passed
  • PASS — Ruff: passed
  • PASS — Repository dependency: passed
  • PASS — Aiter Test Gate: passed

Shadow mode reports what would block but keeps the status successful. Set AITER_PR_SELF_REVIEW_MODE=enforce only after reviewing real false positives.

@ZLkanyo009

Copy link
Copy Markdown
Contributor Author

CI status at 7df92f2c08 (run 44098731409). The two failing shards do not exercise this change, which only touches gradlib/csrc/hipbsolgemm.cu and op_tests/test_hipb_mm_reuse.py:

All other shards passed, including the ones that run op_tests/test_hipb_mm_reuse.py and op_tests/test_hipb_mm.py.

Preserve both the per-thread GEMM cache invalidation and main's conditional
W4A16 plan cleanup before destroying the hipBLASLt handle. The PR's net
C++ change and reuse tests are unchanged by this resolution.

Signed-off-by: root <[email protected]>
@ZLkanyo009

Copy link
Copy Markdown
Contributor Author

Merged main 2eb9ef3657777eebc0da3434074f4a67e1151c54 into this PR with merge commit 32238149d3fffdc0cb213cdf36208aacf858bea3, resolving the hipb_destroy_extension() conflict. Teardown now keeps both cache generation invalidation and main's guarded W4A16 plan cleanup before destroying the handle. The original plan-reuse implementation and test remain unchanged relative to main.

Fresh validation of this exact head on MI308X/gfx942: the new extension builds and imports; the original reuse suite passes all 5 sweep cases and 29 checks, including graph replay and thread/destroy lifecycle; test_hipb_mm.py passes 47 with 18 feature/architecture skips. Black/Ruff and whitespace checks pass. GPU processes have exited and the card is released.

The local ROCm 7.2.4 headers do not enable W4A16, so its enabled branch still needs CI/hardware coverage. Fresh CI is pending; historical performance and TTFT numbers have not been relabelled as new-head results. The PR self-review now starts with the exact-head validation and these limits.

cc @valarLip

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

LGTM

@ZLkanyo009
ZLkanyo009 merged commit f426c20 into main Nov 24, 2026
62 of 63 checks passed
@ZLkanyo009
ZLkanyo009 deleted the lingzha/hipb-mm-plan-reuse branch November 24, 2026 07:40
ZLkanyo009 added a commit that referenced this pull request Dec 3, 2026
Reduce Python metadata and tensor-descriptor work in the ordinary tuned GEMM path. Calls continue through the registered custom ops and plugin routing; there is no parallel eager hipBLASLt bypass. This PR contains three GEMM product files and three directly related test files, with no CI, workflow, selector or snapshot-golden changes.

## Changes

- Cache architecture/CU metadata by explicit device, resolving an unindexed CUDA device before caching. Use dtype objects in the config LRU key and convert to the existing CSV strings on misses; keep the shape-recording flag cache.
- Let only `_hipb_mm` opt into a private, locked, 1024-entry cache for its const `mat2` descriptor. The key includes pointer, shape, strides, dtype and device. The cache retains no Tensor or Storage; subclasses and other arguments use the original conversion path.
- Preserve registered public ops, plugin routing, fake implementations, output handling, logging, ABI checks and live stream selection. Main's M=0 return remains before any architecture/cache/backend lookup.
- Keep the descriptor-cache CPU tests, tuned-config/device-cache lookup tests and public-dispatch operator test. The gfx1250 legacy fixture checks the actual TDM launcher, transposed B view, forwarded parameters and strict-target rejection.

This follows the [maintainer's request to optimize the normal path](#8030 (review)). Acceptance of the opt-in `compile_ops(tensor_cache_args=...)` contract in the shared JIT core remains a separate design-review question.

## Performance

The implementation at `2f20b055` was measured against `c86d490828097b26aade3e6002a5537cc56ddb52`, which includes merged #8116, on one gfx942 80-CU partition with Torch 2.10.0 / ROCm 7.2.4. Both arms used the same native build, real config lookup, selected solutions/kernels, inputs and output SHA-256 values. The parallel eager bypass has been removed.

Three paired rounds completed in order A1, B1, B2, A2, A3, B3. Each process measured all six cases with 31 warm-host, 31 cold-host and 20 GPU-event samples per case; table values are medians of the three per-round medians, in microseconds.

| M × N × K | Output | Warm host base → head | Cold host base → head | Event span base → head |
|---|---|---:|---:|---:|
| 256 × 512 × 1024 | allocate | 48.868 → 46.660 | 103.431 → 99.418 | 81.881 → 82.421 |
| 256 × 512 × 1024 | out | 44.328 → 42.716 | 94.185 → 90.075 | 76.221 → 76.441 |
| 1056 × 324 × 10240 | allocate | 52.765 → 51.715 | 109.682 → 107.157 | 244.422 → 242.982 |
| 1056 × 324 × 10240 | out | 48.787 → 47.045 | 100.998 → 98.467 | 238.762 → 239.341 |
| 2112 × 513 × 2560 | allocate | 48.376 → 46.380 | 101.782 → 99.526 | 157.561 → 157.961 |
| 2112 × 513 × 2560 | out | 44.827 → 42.242 | 93.119 → 89.845 | 152.021 → 152.201 |

**Stable performance improvement is not established by this run.** Aggregate warm-host medians are 1.99%–5.77% lower and cold-host medians 2.22%–4.36% lower, but B1 is slower than A1 in every host case and the later two rounds reverse that sign. The between-round drift is material relative to the measured difference. No current-head model or TTFT gain is claimed.

Event spans change by −0.59% to +0.66% and include gaps waiting for host submission; they are not isolated kernel durations. The cold recipe is an 8 MiB NumPy sweep plus unrelated Python work, not a guarantee that every hardware cache is cold. Gate/selection file tracing was disabled for all measurement processes. Solutions were selected for correctness/repeatability, not speed.

[Complete result, all round medians, protocol and evidence hashes](wuhuikx/LLM-Booster#576 (comment)). All nine workers exited successfully, all 63 exported files were verified, and the owned GPU resources were released.

## Validation and limits

The following evidence retains its original revisions. Withdrawing the CI changes leaves all GEMM product and test blobs unchanged from `d6ceec6a`; it adds no new product, GPU or model measurement.

- The existing nine descriptor CPU control-flow tests passed with zero skips, including independent execution, and five omitted-key-field mutations were detected. They execute extracted definitions with Tensor/FFI stand-ins, so they do not establish native ABI correctness.
- Real-Torch lookup coverage combines 17 unchanged passing methods from the `6bf2b5cb` full execution with the repaired gfx1250 method's 182 passing subcases. This is combined coverage, not a new full-suite run. The isolated CPU environment used Torch 2.10.0 / ROCm 7.2.4, prepared native modules, no compilation and no exposed GPU devices.
- The bounded `2f20b055` GPU gate passed 12 public-dispatch cases, weight-lifetime/view checks, compile/graph checks and nine native-reuse rows. The broad many-configurations, concurrent-thread and short-lived-thread native checks were not run. Later main integration preserved the M=0 return and cache behavior in 12 CPU metadata cases plus an output-dtype rejection case; these used doubles.
- Historical downstream runs at `2f20b055` include an actual [R1 test](https://github.com/ROCm/aiter/actions/runs/45910543746/job/140218459334) passing at 0.965 against 0.93 and [GPT-OSS](https://github.com/ROCm/aiter/actions/runs/45910543650/job/140218353625) at 0.8938589841 against 0.87. The [V3.2 test](https://github.com/ROCm/aiter/actions/runs/45910543746/job/140218459269) failed before accuracy during graph setup: `gemm_a8w8_blockscale_bpreshuffle`, M=512/N=128/K=7168, BF16 output, no tuned config. That failure and the later [CI investigation](#8030 (comment)) remain on record.

The SGLang explicit-method selection, timeout/retry/model-path adjustments, and added PR selectors for the descriptor and lookup suites are withdrawn from this PR. Main's existing CI is preserved. A green or skipped job that collects zero tests is not model validation; removing the selection change does not resolve the recorded V3.2 failure. The direct test sources remain available, but this PR no longer adds their CI wiring.

Stable performance improvement, full native/shared-path coverage and a passing V3.2 result remain unestablished. The public GEMM call contract is unchanged; no third-party dependency is added.
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.

3 participants