Repository navigation
perf(hip): multi-row decode rounds score each slot's rows from one read of their keys - #4673
Closed
AbrahamCete wants to merge 3 commits into
Closed
AbrahamCete wants to merge 3 commits into
AbrahamCete wants to merge 3 commits into
Conversation
Merged
6 of 8 tasks
…o plan.hpp row_xor, wave_sum_dpp, kMaxGroup, kScoreKeys, kScoreSteps, cache_load, score_fetch, score_store and plan_score256_body move verbatim into an anonymous namespace of decode/plan.hpp, which now includes ops/common.hpp, so a second kernel source can run tf_plan_score256_*'s body. Only one comment changes, kScoreKeys' (kScoreKeysShort stays in plan.hip). Pure code motion: with ROCm 10.1's hipcc, the disassembled code objects of recurrence/gated_delta.hip, decode/decode.hip and decode/plan.hip, the sources that include plan.hpp, are identical before and after on gfx1030, gfx1100, gfx1151 and gfx1201. plan.hip falls from 599 to 486 lines. Refs ashhart#3811
…ys at d 256 decode/plan_rows.hip, a new code object group, adds two score kernels with tf_plan_score256_*'s arguments and grid, a block a plan row: - tf_plan_lone_score256_f16 and _bf16 score the rows of one-row slots with plan_score256_body, the one-row kernel's body, and its LDS (one row's queries), and leave for the other rows; - tf_plan_rows_score256_f16 and _bf16 score the multi-row slots' rows in chunks of two led from the slot's `first`: the chunk's first row's block reads the keys once, up to the further row's, and works each row's dots in the tree's operands and order, a key past a row's own scored -inf as the tree scores it; the other blocks leave, so a chunk never crosses a slot. Its LDS holds two rows' queries. One predicate (plan_shared_at) picks the rows of the chunks for both, so each row is scored by exactly one of them. planScore is planCausalAs's score step on its own. With `.shared`, at d = 256 on long spans with q fp32, it launches the two kernels on the same grid; elsewhere, and with `.one`, it launches today's kernel. planCausalAs passes `.one`, so no walk changes here. The contract runs the two launches as one operator, a replacement of tf_plan_score256_*, for both head groups (16 over 2, 24 over 4) and both cache kinds. Gate 3 requires the replaced kernel's bytes on 16 shapes a case: launches of 1 to 64 rows over slots of 3, 1, 4 and 2 rows one position apart (at the span's end, mid-span, across a block's first key, at the span's start) and over 5 and 7 one-row slots, spans of 16,384 to 262,144 keys, NaN and infinite keys that some rows of a chunk see and others do not, and NaN queries. Its controls go red at gate 3: chunks led over the whole launch (every slot's first 0, its count every row), and the chunks' launch alone. Gate 5's records are ROCm 10.1's hipcc on gfx1030, gfx1100, gfx1151 and gfx1201: tf_plan_lone_score256_* 108 VGPRs and 16 KiB of dynamic LDS, tf_plan_score256_*'s; and tf_plan_rows_score256_* 115 VGPRs and 32 KiB. Refs ashhart#3811
planCausalAs passes `.shared` to planScore when its launch holds more than one row. At d = 256 on long spans with q fp32, the Qwen checkpoints' served decode walk, a round's score step is then two launches: the one-row slots' rows at the one-row kernel's occupancy, and the multi-row slots' rows in chunks of two from one read of their keys. One-row launches keep a single tf_plan_score256_* launch; q in the cache's kind, d = 128 and spans under 16,384 keys keep their kernels; softmax, the values' walk and the tiles' sum are unchanged. The grid, the launch arguments and the replay key stay as they were. The whole score step against the tree on gfx1151 (medians of 15 alternated runs): one slot of 2 or 4 rows -14.0% to -18.5% (16 heads over 2) and -31.1% to -32.3% (24 over 4) at 131K and 262K keys; a 2-row slot with two 1-row slots -6.0% to -15.3%; 2 or 4 one-row slots +0.6% to +0.9%, the second launch's blocks leaving at once. A kernel test walks a round's slots one position apart through planCausal (slots of 3, 1, 2 and 4 rows; 1, 4 and 3; four of one row; and 2, 1, 1, 3, 1 and 4) with NaN and infinite keys and an infinite value, and requires the bits of each row walked alone in a one-row launch, in its scores and its rows. The pinned NaN digests of plan_pin.zig, whose d = 256 q fp32 case is a two-row launch, are unchanged. Refs ashhart#3811
ashhart
force-pushed
the
perf/hip-decode-rows-share-score
branch
from
November 24, 2026 00:05
00a2faf to
2280c6b
Compare
This was referenced Nov 24, 2026
Owner
|
Thanks @AbrahamCete. All three commits from this PR are already on main, with your authorship: they landed as part of #4754, which was stacked on this branch (a63fd8d, 400b112 and 52a8b76, each with an identical patch to yours). There's nothing left to merge here, so I'm closing this as done. #4673 had no changelog line of its own, so #4844 adds one with your credit.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this changes
At d = 256 on long spans (the Qwen checkpoints' served decode walk), every row of a decode round reads its slot's whole
key cache in the score step, so the pending token and each drafted row of a stream pay for the same reads. Now a
multi-row round's score step is two launches on today's grid: the rows of one-row slots take today's kernel body at
today's occupancy, and the rows of multi-row slots are scored in chunks of two, a chunk's keys read once. Each row keeps
the tree's operands and the order of its sums, so the scores' bytes are today's. One-row rounds keep today's single
launch. This is the score half of #3811; the values' walk (apply) is a separate pull request, stacked on this one.
The shape follows ashhart's answer on #3811 (issuecomment-7077344772):
Design.
decode/plan_rows.hip, a new kernel group appended tozig/build/hip.zigandzig/src/hip/kernels.zig, holds twoscore kernels with
tf_plan_score256_*'s arguments and grid, a block a plan row:tf_plan_lone_score256_{f16,bf16}scores the rows of one-row slots withplan_score256_body, the one-row kernel'sbody, and its LDS (one row's queries, 16 KiB at most); its other blocks leave at once.
tf_plan_rows_score256_{f16,bf16}scores the rows of multi-row slots in chunks of two, led from the slot'sfirstand
count. The block of a chunk's first row loads the keys once, up to the further row's, and works each row's dotsin the tree's order; a key past a row's own is scored -inf as the tree scores it, not given weight 0. The other
blocks leave at once, so a chunk never crosses a slot. Its LDS holds two rows' queries (32 KiB at most).
plan_shared_at, decides for both which rows are in chunks, so each row is scored by exactly onelaunch.
planScoreisplanCausalAs's score step on its own.planCausalAspasses.sharedwhen its launch holds more thanone row; then, at d = 256 on spans of 16,384 keys or more with q fp32, it launches the two kernels on the same grid.
One-row launches, q in the cache's kind (
act), d = 128 and shorter spans keep their kernels, and softmax, the values'walk and the tiles' sum are unchanged. The grid, the launch arguments and the replay key do not change; the score's
dynamic LDS follows from the shape.
plan.hipwas at 599 lines, so the first commit moves the long-span score's pieces (row_xor,wave_sum_dpp, theconstants,
cache_load,score_fetch,score_store,plan_score256_body) verbatim intoplan.hppfor bothsources. With ROCm 10.1's hipcc the disassembled code objects of the three sources that include
plan.hpp(
gated_delta.hip,decode.hip,plan.hip) are identical before and after on gfx1030, gfx1100, gfx1151 andgfx1201.
plan.hipfalls to 486 lines.Why two launches. The first version was one launch: the shared kernel for every row of a multi-row launch. The
replay key holds the round's rows, not its rows per slot, so it took every multi-row launch, including several streams
with no drafts, where nothing is shared and its two rows of queries in LDS halve the blocks a CU. Kernel time against the
tree (medians of 15 alternated runs at the span's end):
With the one-row slots' rows on today's body in their own launch, the whole score step (both launches) against the tree:
Each side's interquartile range over its 15 runs is 0.1% to 2.9% (5.0% for one cell). The +0.6% to +0.9% left on
one-row slots is the second launch's blocks leaving at once (10 to 67 us a step). Served, the same no-drafts rounds read
level (the Receipt). A variant with 512-key blocks in the second launch cut that cost to +0.1% to +0.3% but lost most of
the gains (+5.2% and +7.6% in two cells), so it is not here.
Beyond the plan, or where it was silent; say which you want changed:
kernel's exit test, from the one predicate, keeps the two sets apart. It would cost time, not bits.
blocks a CU and lost most of the gain on the 35B-A3B shape in the lab (HIP: every row of a decode round re-reads the whole K/V cache on gfx1151; reading it once for a stream's rows makes a 2-4 row round's attention walk 16-30% shorter at 131K-262K, same bits #3811).
first, asBuffer.fillwrites them for Qwen.A row outside its slot's list is scored alone by the first launch.
Size: +910/-139 in 14 files, three commits; 418 of those lines are the contract's test type.
maina325f58. Commits:atc overlap --json(2026-11-22 21:23 UTC, on 00a2faf,maina325f58): incomplete (limitationsotherPullRequestsLimit,pullFilesUnavailable,pullRequestsLimit,timeLimit). Of the 14 open pull requests it read that share a file with this branch, 10 merge clean and for 4 it could not tell; it reports no conflict with a pull request. It read 20 of the local worktrees; the nine conflicts it reports there are with my own local branches, on older bases. It did not read feat(hip): Inkling 2 attention kernels under the kernel contract #4633. Bygit merge-treeon 00a2faf: clean againstmaina325f58; against my feat(hip): Inkling 2 attention kernels under the kernel contract #4633 (Inkling 2 attention, head e90a42e) it conflicts inzig/build/hip.zigandzig/src/hip/kernels.zig, where both add a kernel group at the end of the same lists; whichever lands first, I rebase the other.Checks on this head
maina325f58, built from agit archivetree; the checks below ran on it. Before therebase each of the three commits passed its own checks on d7f1f40: lean,
zig fmt --check,git diff --check,pr_check,hip-host-testandcheck-hip-kernel-contract-host, the first commit'ship-kernel-build, and on thesecond the kernel contract (the same
SUMMARYas below), its limits andhip-kernel-test(235 of 242, 7 skipped).The receipts ran on 448b93710, these three commits on d7f1f40 (see Not run).
python3 tools/zig/lean_check.py, on the base and on this head: 165 and 165, 0 new.python3 tools/pr_check.py --base origin/main: 0 errors, 1 advisory warning (the changelog line needs this pullrequest's number).
zig build hip-kernel-test -Dhip=true -Dhip-arch=gfx1151on the GPU: 236 of 243 passed, 7 skipped, 0 failed. The newtest walks a round's slots one position apart through
planCausal(slots of 3, 1, 2 and 4 rows; 1, 4 and 3; four ofone row; and 2, 1, 1, 3, 1 and 4) with NaN and infinite keys and an infinite value, and requires the bits of each
row walked alone in a one-row launch, in its scores and its rows.
plan_pin.zig's pinned NaN digests are unchanged;their d = 256 q fp32 case is a two-row launch, so it now runs the shared kernel.
zig build test test-golden -Dhip=true: 12,854 of 12,949 passed, 94 skipped, 1 failed. The failure is not in filesthis changes:
stage_hip_test"every staged call on HIP memory equals the reference, whole and in parts"(
zig/src/fabric/tp/, which imports none of the changed files; feat(hip): Inkling 2 attention kernels under the kernel contract #4633 reports it failing onmaintoo).launches/plan.zig): one-row launches,act, d = 128 and short spans keep theirkernels.
zig fmt --checkon the changed files andgit diff --check: clean.expected 2, found 1;(
at 8x16x16387 its bytes differ from tf_plan_score256_f16's from byte 3146304), and so does the kernel test;release-paths (ubuntu-24.04), benchmark-clients (ubuntu, windows) and
comfyui-moss-installer pass; host-nightly-linux and host-nightly-macos skipped.
kernels).
--drafter, which HIP serves since feat(hip): serve Qwen3.8-27B with its DFlash2 drafter #3665): not measured; the kernels take any rows a slot.maind7f1f40 against these three commits on it (448b93710).The rebase changed none of the three commits' patches.
main's 123 commits since d7f1f40 change the lanes'copy pricing and Qwen's HIP drafting and engine (
core/lanes/copy_price.zig,core/lanes/lone_copy.zig,qwen3_5/backend/hip/draft/,engine/native.zig,engine/hip_lanes.zig), and none of the walk's sources.Receipt
Main against branch:
maind7f1f40 (B) against 448b93710 (C, these three commits on it), both built by the same scriptfrom clean
git archivetrees (ReleaseFast, gfx1151). One fresh server a run, alternated B1 C1 C2 B2 in every cell,an explicit
--prompt-cache-gibon every start, temperature 0, seed 1234. Each start waited for a quiet machine(memory, no other server, the GPU idle for 10 s, no other
/dev/kfdholder), and/dev/kfdwas sampled every 3 sduring every run. One foreign holder, 14:08:03-14:08:13Z, fell inside B1's cold 258K fill, before its timed turns.
One stream with drafts (Qwen3.6-35B-A3B, MTP). perf(hip): score the planned decode walk at d = 256 without element tests #3508's turn method: K + 8 tokens cold, then 8 turns of the K kept
tokens plus 200 fresh, 128 reply tokens, drafts on, off, off, on in each of two fresh stretches. Decode tok/s, medians
of both servers a side:
main"draft": false"draft": falseThe four drafts-on rows are faster beyond both sides' ranges;
"draft": falseis level. The first token of aresumed turn reads level: 0.739 s on both at 131K, 1.260 s against 1.256 s at 258K.
Four streams (
--parallel 4): four distinct prompts filled cold, then two rounds of prompt 1 alone (128 tokens)and the four together (256 each), with drafts on, then with
"draft": false. Aggregate tok/s, medians:main"draft": false"draft": false"draft": falseSteady tok/s of the
"draft": falserounds: -0.05% (35B 32K), -0.20% (35B 131K), +0.42% (27B 32K), all inside thespread. Prompt 1 alone with drafts on: +1.03% (35B 32K), +6.71% (35B 131K), +0.71% (27B 32K), each beyond the spread.
So the no-drafts rounds that carry the kernel's +0.6% to +0.9% read level served.
One stream with drafts (Qwen3.8-27B). The same method, with
--prompt-cache-gib 18(one stream at a 262,144-tokenwindow takes 16.15 GiB); its drafts as served are copies (no draft head on HIP). No foreign
/dev/kfdholder wassampled during any run. Decode tok/s, medians of both servers a side:
main"draft": false"draft": falsePooled over both stretches, drafts on: 8.07 -> 8.31 tok/s (+2.91%) at 131,072 and 5.07 -> 5.34 (+5.19%) at 258,048.
The four drafts-on rows are faster beyond both sides' ranges;
"draft": falseis level. The rounds, accepted anddrafted counts of every turn are the same on all four servers, so the gain is a faster round, not more accepted
drafts. The first token of a resumed turn reads level: 1.774 s against 1.773 s at 131K, 2.897 s against 2.898 s at
258K. Every reply is equal request by request on all four servers (9 of 9 in each cell), and drafted against
"draft": falsegives one hash per stretch:97a39f51eebfand1be5faab3a2cat 131,072,df07006606e7andbab906d9141aat 258,048.Boxes it reaches: HIP Qwen checkpoints with 256-wide heads (Qwen 3.5/3.6/3.8) in decode rounds of more than one row at
spans of 16,384 keys or more: drafted rounds, and several streams together.
New switch? No.
Environment: TensorFold d7f1f40 (main) and 448b93710 (these three commits on it) for the served cells, and
a325f58 (main) and 00a2faf (this head) for the checks; Zig 0.17.0; the served builds with the system hipcc (HIP
7.1.52801, Ubuntu clang 21.1.8) and LLVM 21's device bitcode, the kernel contract and records with ROCm 10.1's hipcc
(HIP 7.16.26385); Ryzen AI Max+ 395 (Strix Halo, gfx1151), unified memory; Ubuntu 26.04.1, kernel 7.0.0-44;
Qwen3.6-35B-A3B-MLX-4bit-MTPandQwen3.8-27B-MLX-4bit. The served binaries (sha256 prefixes): mainf488bf980a8fand head
c787ad3867dbfor the 35B-A3B cells and the 27B's four-stream cell; main516b6b5bcec2and headdf2724d4a6d1, rebuilt from the same commits, for the 27B's one-stream cells (each build embeds its scratch path).Paths and platforms: ran HIP gfx1151 at one rank, both checkpoints, one and four streams, drafts on and
"draft": false, greedy. Not run: the list under Checks.Exactness:
one-stream cell, 24 of 24 in each four-stream cell.
"draft": false: one hash per stretch on every server. 35B-A3B at 131,072:0dc71eaf9689and354d2aa6522a, the hashes of perf(hip): score the planned decode walk at d = 256 without element tests #3508's receipts; at 258,048:d42f2e2f48f7and9ced54eaca25."draft": falseand round 1 against round 2, one hash per phase and stream on everyserver (35B 32K: alone
e05f32999d13, together83dcb52f04c7,46675e2d0063,d76858ae958d,bf9af62b9ee7;35B 131K:
e88dce5eded4, then00474f283746,00e7354df7fa,4ae3370f467e,49946caeafa8; 27B 32K:28bb6ebb4d19, then9aa3b4882600,746ce67ba326,97e08cce66af,454d232851e5).Main against this branch, same machine and checkpoint: the tables here. Scripts and raw files: on request.
Decode speed before and after: the tables above.
Prompt speed before and after: n/a, the change reaches only the decode walk's score step. The served cells' cold
first token (K + 8 tokens), main's two servers against this head's: 35B-A3B 172.0 and 173.3 s against 172.5 and
172.6 s at 131,072, and 554.3 s (main's second server) against 553.4 and 552.0 s at 258,048, where main's first server
was slowed by the foreign GPU holder above; 27B 577.0 and 578.8 s against 582.3 and 579.3 s at 131,072 (+0.5%, the
two sides' servers do not overlap), and 1,685.5 and 1,686.0 s against 1,682.9 and 1,684.2 s at 258,048 (-0.2%).
These are two servers a side and not
tools/bench_cache.py --require-coldruns.Licence of any new weights or drafter: n/a.
Kernel contract, only when a GPU kernel is new or changed
Kernels registered with the contract, and the kernel each one replaces: the two launches as one operator,
tf_plan_lone_score256_*thentf_plan_rows_score256_*, a replacement oftf_plan_score256_*(gate 3 requires itsbytes), for 16 query heads over 2 and 24 over 4, each in fp16 and bf16 caches (4 registrations):
apart (at the span's end, mid-span, one past a block's first key, at the span's start; 16 rows hold slots of 1, 2, 3
and 4 together), launches of 5 and 7 one-row slots, and spans of 16,384 to 262,144 keys, with the rows' and keys'
tile edges. The keys are NaN- and inf-poisoned: one key in about 40 holds a NaN of some payload or an infinity, and
in each slot the key only its last row sees and the key past all its rows are non-finite; a row's queries carry
quiet NaNs of distinct payloads.
outputs at 16 heads, fp16;
A planted control for each new kernel, and each one fails as planted, all red at
shapes:first0 and its count every row), so a chunk crosses a slot (fp16and bf16);
Each backend's
SUMMARYline and exit status (gfx1151, ROCm 10.1's hipcc, HIP 7.16):SUMMARY gfx1151 192 kernels: 192 pass gates 1-4, 0 fail, 2 known findings; 58 controls: 58 red as planted, 0 missed; 2 limit problems; gates 618303 ms, limits 2782 ms(exit 1, on 00a2faf)The two limit problems are
FAIL limits livelines oftf_moe_combine_gateandtf_vl_resize, whose records thisdoes not touch; the first run of this contract on a head without these kernels' dispatch showed the same two.
zig build hip-kernel-contract-limits: the four new records PASS on all four chips. The step exits 1 with 11problems, the same 11 as on
maind7f1f40 with this hipcc (a325f58 changes no HIP record, kernel orzig/build/hip.zig), all in other groups:tf_moe_combine_gate(gfx1030,gfx1100, gfx1151),
tf_vl_resize(all four chips), and the 6- and 8-bit relu2 stream tiles 2x8 and 4x4 on gfx1100.Records (
hip_records_score_rows.zig), the same on gfx1030, gfx1100, gfx1151 and gfx1201; no scratch on any chip:tf_plan_lone_score256_{f16,bf16}tf_plan_rows_score256_{f16,bf16}tf_plan_lone_score256_*usestf_plan_score256_*'s VGPRs and LDS (108, 16 KiB), so one-row slots keep itsoccupancy.
Chips I could not run: gfx1030, gfx1100 and gfx1201 (limits only).
Checklist
"draft": false, a resumed prompt equals a fresh one, and concurrent equals solo.(Drafted equals
"draft": falsein every served cell, and every reply equals main's request by request. Resumedagainst fresh and concurrent against solo were not compared on their own here.)
not change.)
--drafter, noslower than without it, greedy and sampled. (Decode: no measured cell is slower beyond the spread, both checkpoints at
one stream at 131K and 258K kept tokens and at four streams, drafts on and
"draft": false, greedy; the resumed firsttoken is level. Prompt: the 27B's cold first token at 131,072 reads 0.5% later on this head, two servers a side that do
not overlap, in a step this change does not reach; no
--require-coldrun. Not run: sampled requests, more than fourstreams, and
--drafter(DFlash2 on the 27B, which HIP serves since feat(hip): serve Qwen3.8-27B with its DFlash2 drafter #3665).)