Skip to content

[Engine] Inkling 2 on AMD: fuse the clip and KV-store kernels - #295

Open
okada-shogo wants to merge 1 commit into
simplexai-labs:mainfrom
okada-shogo:port/hip-clips-fused
Open

okada-shogo wants to merge 1 commit into
simplexai-labs:mainfrom
okada-shogo:port/hip-clips-fused

Conversation

@okada-shogo

@okada-shogo okada-shogo commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

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() and ink_qkv() into separate launches on HIP, because the fused
ink_clips_kernel and ink_qkv_kernel never finished on gfx1100 and gfx1201 under ROCm 7.2.4. It was a code
generation 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_kernel that does the clips (clamping of the Q/K/V values), the head norms and
the fp16 / Q8_0 cache stores in one launch. On HIP, ink_qkv() is then ink_conv_few() of K and V (one launch) plus
that 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, one
warm-up request first in each process. Mean of 3 runs, with the order of the builds rotated each round.

Card Model Measure main this PR alone Change
RX 9070 XT 16 GB UD-Q2_K_XL decode after 302 60.0 62.1 +3.4%
RX 9070 XT 16 GB UD-Q2_K_XL decode after 8K 56.5 58.6 +3.7%
RX 9070 XT 16 GB IQ4_NL decode after 302 41.4 43.0 +3.9%
RX 9070 XT 16 GB IQ4_NL decode after 8K 36.4 37.8 +3.8%
RX 7800 XT 16 GB UD-Q2_K_XL decode after 302 38.0 37.8 −0.5%
RX 7800 XT 16 GB UD-Q2_K_XL decode after 8K 34.9 37.6 +7.8%

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)
Card Model Measure Build Run 1 Run 2 Run 3
RX 9070 XT 16 GB UD-Q2_K_XL decode after 302 main 60.9 59.8 59.3
RX 9070 XT 16 GB UD-Q2_K_XL decode after 302 this PR 61.7 62.2 62.3
RX 9070 XT 16 GB UD-Q2_K_XL decode after 8K main 56.5 56.6 56.4
RX 9070 XT 16 GB UD-Q2_K_XL decode after 8K this PR 58.8 58.6 58.3
RX 9070 XT 16 GB IQ4_NL decode after 302 main 41.4 41.5 41.2
RX 9070 XT 16 GB IQ4_NL decode after 302 this PR 43.1 42.8 43.0
RX 9070 XT 16 GB IQ4_NL decode after 8K main 36.4 36.4 36.4
RX 9070 XT 16 GB IQ4_NL decode after 8K this PR 37.7 37.6 38.0
RX 7800 XT 16 GB UD-Q2_K_XL decode after 302 main 39.4 39.6 35.1
RX 7800 XT 16 GB UD-Q2_K_XL decode after 302 this PR 41.6 35.5 36.4
RX 7800 XT 16 GB UD-Q2_K_XL decode after 8K main 38.1 32.5 34.0
RX 7800 XT 16 GB UD-Q2_K_XL decode after 8K this PR 38.8 38.6 35.4

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, like ink_clips_kernel. It picks its job
    by 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 same
    stores (__float2half per value, or kvq8_store()), with each thread storing the values it wrote.
  • A call with no heads launches nothing, because a grid of 0 blocks is an error on HIP.
  • ink_qkv() on HIP runs ink_conv_few() of K and V (the two ink_conv() calls in one launch, with the same bits),
    then the new clips kernel. clips_apart() and ink_store_q8_kernel are gone.

Validation

Passed:

  • poly_inkling2_test (its clips and qkv cases compare the fused launch with ink_clip(), ink_head_rms() and the
    stores one at a time, and ink_qkv() with ink_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() and store_f16_at() of the same
    rows: 338 checks, 0 failed on the RX 7800 XT.
  • poly_inkling2_kvq8_test (Q8_0 stores through ink_clips()): PASS on the RX 9070 XT and the RX 7800 XT.
  • Identity, main vs this PR (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).
  • Builds: HIP on the boxes (ROCm 7.2.4, gfx1201, gfx1101); after the rebase onto 36e654b, HIP
    gfx1100;gfx1101;gfx1201 and CUDA sm_75;sm_86 on our builders.
  • Measured on main cdfae80 + this PR; the branch is now rebased onto 36e654b. The AMD runs were on SaladCloud
    containers (WSL2, /dev/dxg).
  • CUDA, RTX 2070 Super (sm_75), rebased branch: poly_inkling2_test 433 checks, 0 failed (with the new store
    comparisons); poly_inkling2_kvq8_test PASS.

Failed:

  • none observed

Not run:

  • gfx1100 (RX 7900 XT / XTX) at run time: built but not run, because no such box was available. The hang was seen there too.
  • Other ROCm versions: verified on ROCm 7.2.4 only. The hang was a 7.2.4 code generation problem; a new ROCm should be
    checked with poly_inkling2_test first.

Limits

Credits: the fewer-launches idea is from our reference engine's clip_many (PR #200); the kernel here keeps main's
block-per-head arithmetic, so the bits stay main's.

🤖 Generated with Claude Code

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>
@okada-shogo okada-shogo changed the title [Engine] Inkling 2 on AMD: the clips and stores in one launch, a token's q/k/v in two (was 8) [Engine] Inkling 2 on AMD: fuse the clip and KV-store kernels (q/k/v in 2 launches instead of 8) Nov 24, 2026
@okada-shogo okada-shogo changed the title [Engine] Inkling 2 on AMD: fuse the clip and KV-store kernels (q/k/v in 2 launches instead of 8) [Engine] Inkling 2 on AMD: fuse the Q/K/V clip and KV-store kernels (2 launches instead of 8 per attention layer, +3-4% decode) Nov 24, 2026
@okada-shogo okada-shogo changed the title [Engine] Inkling 2 on AMD: fuse the Q/K/V clip and KV-store kernels (2 launches instead of 8 per attention layer, +3-4% decode) [Engine] Inkling 2 on AMD: fuse the clip and KV-store kernels (+3-4% decode) Nov 24, 2026
@okada-shogo okada-shogo changed the title [Engine] Inkling 2 on AMD: fuse the clip and KV-store kernels (+3-4% decode) [Engine] Inkling 2 on AMD: fuse the clip and KV-store kernels Nov 24, 2026
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.

1 participant