Repository navigation
[Perf] hipb_mm: keep hipBLASLt layouts, descriptor and algorithm per configuration - #8116
Conversation
…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.
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: Reviews: |
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.
|
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 One change from the #8030 version: Verification at 7df92f2 (MI308X, gfx942) and the numbers are in the description:
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 |
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 |
|
CI status at
All other shards passed, including the ones that run |
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]>
|
Merged 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; 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 |
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.
Summary
hipb_mmbuilds 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.mmis 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 threehipblasLtMatrixLayout_t, onehipblasLtMatmulDesc_twith its attributes, looks the algorithm up withhipblaslt_ext::getAlgosFromIndex, and destroys all of it afterhipblasLtMatmul. For a given configuration and solution index, every one of these is the same on every call.Changes
gradlib/csrc/hipbsolgemm.cuonly:create_matmul_descs(), which both paths call.solution_index >= 0, noscaleA/scaleB/scaleCand 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 beforehipblasLtMatmul. Scale modes only take effect with a scale pointer, so they cannot differ between calls that reach the kept path.solution_index < 0) and online tuning build and destroy per call, as before.HIP_ONLINE_TUNINGis still read on every call (op_tests/test_hipb_mm.pysets it at run time).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.hipb_destroy_extensionreleases 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.pyis 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_hipbsolgemmbuilds were JIT-built the same way, and the output SHA-256 of every shape is the same for both.tgemm.mm, BF16, 7 prefill shapes weighted, warmEvery 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.mmgoes 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_TUNINGread 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
Documentation
hipb_mm's interface and results are unchanged; the kept plans are internal.Dependencies
Breaking Changes
None.
PR Self-Review
Reviewed revision
3223814
Behavior and intent
Behavior change:
hipb_mmcalls 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 main2eb9ef3657777eebc0da3434074f4a67e1151c54. The teardown conflict retains both this PR's plan-cache clear/generation invalidation and main's guardedw4a16_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, HIP7.2.53211:module_hipbsolgemmfrom the merged source and headers; CPU build/import, Black, Ruff and whitespace checks pass.python3 -m pytest -q op_tests/test_hipb_mm.py: 47 passed, 18 skipped (34.80 s). Skips are feature/architecture limits, not passes.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_hipbsolgemmJIT-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.hipbsolgemm.cu, each JIT-built and run against the test: the bias pointer not set on a hit failstest_kept_descriptors(bias case, every element differs, max |diff| 4.28);ldbleft out of the key failscheck_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 compileandpython3 -m pytest op_tests/test_hipb_mm.pygive the same results on main and on this branch:normalandcompilepass;-t outfails on both with the same asm case (out= not honoured by: ['asm tuned 256x256x4096']), a backend this PR does not touch;test_hipb_mm.pyis 47 passed, 18 skipped on both.Unverified:
d_workspace) is one buffer per extension, as before, so two streams callinghipb_mmat 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 shapehipb_mmalready handles:solution_index >= 0(a tuned or caller-chosen solution), andscaleA,scaleBandscaleCall null, andHIP_ONLINE_TUNINGoff (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_descsandgetAlgosFromIndexbuild 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, andhipblasLtMatmulvalidates 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):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_mmtwice 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 failstest_kept_descriptors,ldbleft out of the key failscheck_configurations_do_not_mix.cc @valarLip