Repository navigation
[Engine] Inkling 2 on AMD: fuse the clip and KV-store kernels - #295
Open
okada-shogo wants to merge 1 commit into
Open
okada-shogo wants to merge 1 commit into
okada-shogo wants to merge 1 commit into
Conversation
Same bits. HIP ran ink_clips() as up to 8 launches and ink_qkv() as 8 (the simplexai-labs#217 workaround: ink_clips_kernel and ink_qkv_kernel never finished on gfx1100/gfx1201 under ROCm 7.2.4). A HIP-only ink_clips_hip_kernel does the clips, the head norms and the fp16 / Q8_0 stores in one launch: each block a token's head through the same clip_head() / rms_head() and the same stores, its job picked by comparisons and its fields copied out (the hanging kernel indexed them in a counted loop). ink_qkv() on HIP is ink_conv_few() of K and V in one launch, then that. poly_inkling2_test's clips and qkv cases compare it with the separate launches, and its clips case now also compares the fp16 caches with store_f16() / ink_store_ring() and store_f16_at() of the same rows. CUDA unchanged. Checked on ROCm 7.2.4 (gfx1201, gfx1101). The idea of fewer clip launches on AMD is from our reference engine's clip_many (PR simplexai-labs#200). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Problem and result
On AMD (HIP), each attention visit of an Inkling 2 decode token now takes 2 launches for the Q/K/V clipping, head norms
and KV-cache stores instead of 8. This removes about 284 kernels per token and gives +3.4% to +3.9% decode on an
RX 9070 XT, with the same bits. CUDA is unchanged.
Background: #217 split
ink_clips()andink_qkv()into separate launches on HIP, because the fusedink_clips_kernelandink_qkv_kernelnever finished on gfx1100 and gfx1201 under ROCm 7.2.4. It was a codegeneration problem: any change to the kernel made it finish. The cost is 8 launches for every attention visit of a
decode token where CUDA has 1, about 290 more launches a token.
This PR adds a HIP-only
ink_clips_hip_kernelthat does the clips (clamping of the Q/K/V values), the head norms andthe fp16 / Q8_0 cache stores in one launch. On HIP,
ink_qkv()is thenink_conv_few()of K and V (one launch) plusthat kernel. The new kernel finishes on gfx1201 and gfx1101.
Kernels per decode token (rocprof, 64 tokens after a 302-token prompt): RX 9070 XT UD-Q2_K_XL 1,706 → 1,422 (−284),
IQ4_NL 1,584 → 1,301 (−283); RX 7800 XT UD-Q2_K_XL 1,706 → 1,422.
Decode tok/s (higher is better): 256 tokens after the prompt,
--serve, fp16 KV, 32K context, default flags, onewarm-up request first in each process. Mean of 3 runs, with the order of the builds rotated each round.
The RX 7800 XT host was noisy (main's runs ranged from 35 to 40 tok/s), so its rows are not a clear result. Prompt
speed is unchanged (within ±1.4%).
Each run (tok/s)
Related discussion
#217 (the hang, and the split into separate launches)
Approach
ink_clips_hip_kernel<KV>(HIP only) runs one block per head of the token, likeink_clips_kernel. It picks its jobby comparing the block index with the jobs' bounds and copies that job's fields into local variables; the kernel that
hung walked the job arrays in a counted loop instead. It then runs the same
clip_head()/rms_head()and the samestores (
__float2halfper value, orkvq8_store()), with each thread storing the values it wrote.ink_qkv()on HIP runsink_conv_few()of K and V (the twoink_conv()calls in one launch, with the same bits),then the new clips kernel.
clips_apart()andink_store_q8_kernelare gone.Validation
Passed:
poly_inkling2_test(its clips and qkv cases compare the fused launch withink_clip(),ink_head_rms()and thestores one at a time, and
ink_qkv()withink_conv_few()+ink_clips()): 336 checks, 0 failed on the RX 9070 XT(gfx1201); 330 checks, 0 failed on the RX 7800 XT (gfx1101). Each run took 10 to 14 s (no hang). With the review fixes, the
clips case also compares the fp16 caches with
store_f16()/ink_store_ring()andstore_f16_at()of the samerows: 338 checks, 0 failed on the RX 7800 XT.
poly_inkling2_kvq8_test(Q8_0 stores throughink_clips()): PASS on the RX 9070 XT and the RX 7800 XT.STRATA_GPU_EXPERTS=1500 STRATA_FREEZE_EXPERTS=1,--top-probs 20, prompts of 302,2,048 and 8,192 tokens followed by 64, 16 and 16 generated tokens): SAME with fp16 KV (UD-Q2_K_XL on both cards, IQ4_NL on the RX 9070 XT), and with
--kv q8_0(UD-Q2_K_XL, RX 9070 XT, 302 and 2,048).
gfx1100;gfx1101;gfx1201 and CUDA sm_75;sm_86 on our builders.
containers (WSL2, /dev/dxg).
poly_inkling2_test433 checks, 0 failed (with the new storecomparisons);
poly_inkling2_kvq8_testPASS.Failed:
Not run:
checked with
poly_inkling2_testfirst.Limits
in the decode graph), this PR and [Engine] Inkling 2 on AMD: fewer launches for attention projections #294 (grouped attention projections) together add +9.8% (UD-Q2_K_XL) and +6.4% (IQ4_NL) decode after 302 tokens on the RX 9070 XT, identity SAME.
the guard for new targets.
Credits: the fewer-launches idea is from our reference engine's
clip_many(PR #200); the kernel here keeps main'sblock-per-head arithmetic, so the bits stay main's.
🤖 Generated with Claude Code