Skip to content

inkling2 cuda: Mini decode QKV a warp 32 outputs over staged 128-byte weight rows (decode 5.6 to 6.4 tok/s, same bits) - #4718

Open
jschmied wants to merge 2 commits into
ashhart:mainfrom
jschmied:ink2-mini-speed-qkv
Open

jschmied wants to merge 2 commits into
ashhart:mainfrom
jschmied:ink2-mini-speed-qkv

Conversation

@jschmied

@jschmied jschmied commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Update for review 7098920791: head e2671d5830, stacked on #4697

Stacked on #4697 (ed5da93076). The base stays lead/inkling2-mini. The two PRs conflicted in contract_inkling2_loop_math_extra.zig, so this one now carries #4697's two commits under its own two:

  • 523d872949: the QKV commit, rebased unchanged. Same 4 files, 115+/6−.
  • e2671d5830: this round's fixes.

Point 3, the comparison with the old kernel:

  • loop_math_cuda.projectQkvTiles keeps tf_ink2_loop_project_qkv's 16×16-tile launch callable at any row count. projectQkv still takes it for prompts (more than 16 rows).
  • The contract's ProjectQkv names it in replaces. Gate 3 now compares the decode kernels' q, k and v bytes with the tiles' on the same inputs. Result: 5 shapes bit-equal to the kernel it replaces.
  • The decode launch told one row fewer is red against the tiles: it differs from byte 12288 at 3×3072×2304.

Point 2, the kernel records (nvcc 13.0, -cubin -O3 per SM; no stack or spills; static shared bytes and the 32-thread bound as on sm_121):

launch sm_80 sm_86 sm_89 sm_120 sm_121
tf_ink2_loop_qkv_rows1 regs 128 128 128 86 86
tf_ink2_loop_qkv_rows4 regs 128 128 128 90 90
tf_ink2_loop_qkv_rows16 regs 90 88 88 103 103

The registry test finds 40 on this head, the base's 40. The previous head added 12.

Checks on e2671d5830 (GB10):


What this changes

At decode Mini's fused QKV projection (tf_ink2_loop_project_qkv, 48 calls a token) tiles 16 rows by 16 outputs. With one row, 15 of every 16 threads sat idle, and each weight row was read in 32-column slices scattered over thousands of rows. That ran at about 40 GB/s and cost 28.2 ms of the 181.8 ms a token on #4640's head (nsys, c=1, 60 decode forwards).

For up to 16 rows (and c % 64 == 0), projectQkv now launches tf_ink2_loop_qkv_rows1/4/16. Each runs one warp per 32 output columns:

  • The warp stages its 32 weight rows 64 columns at a time into shared memory, reading each row as whole 128-byte lines, with the next tile prefetched in registers. It stages the rows' inputs the same way.
  • Each thread then runs its output's rows over the same ascending FP32 chain as tf_ink2_loop_project_qkv, and rounds to BF16 the same way. The results are its bits.

Prompt chunks (more than 16 rows) keep tf_ink2_loop_project_qkv, unchanged. The decode QKV goes from 28.2 ms to 5.31 ms a token (about 213 GB/s).

Checks on this head (b78348b927)

GB10 (sm_121, CUDA 13.0), Zig 0.17.0, thinkingmachines/Inkling-2-Mini@ed40e7da.

Speed (A-B-B-A against #4640's head, this head's binary)

Served, tensorfold-native serve --parallel 4 --context 8192, default copy drafting and prompt cache on, page cache dropped before each server start. Each arm is a fresh server. Order A B B A, two rounds. A = #4640's head (321432e4b2), B = this head.

  • Cells: cold TTFT of a 7,262-token prompt (1 token out); c=1 decode = 4 topics × 128 greedy tokens, (tokens − 1) / (wall − 1-token wall); c=4 aggregate = the 4 topics at once, total tokens / wall. Tokens are counted from usage.
  • SM clock was logged at 1 Hz: median 2288 MHz in both arms, with the same constant clock-event mask (0x400, the box's boot-time clock cap). No round was dropped.
cell A (#4640), best of 4 B (this PR), best of 4 paired median B/A pairs where B is slower
c=1 decode 5.63 tok/s 6.44 tok/s 1.144 0 of 4
c=4 aggregate 5.57 tok/s 6.36 tok/s 1.142 0 of 4
prompt TTFT (7,262 tokens) 33.97 s 33.91 s 0.998 0 of 4

The reply hashes were equal in every arm, solo and c=4. The prompt cell is flat as expected: prompt chunks keep tf_ink2_loop_project_qkv. nsys (c=1, 60 decode forwards): QKV 28.2 → 5.31 ms a token.

  • Binaries: A 019c09e6 (built from 321432e4b2), B fb1a7ada (built from b78348b927, the commit itself). The gates above ran on the same B build.
  • Why not against main: this PR targets lead/inkling2-mini, whose Mini engine main does not have.

Proposed CHANGELOG line

  • Inkling 2 Mini on CUDA: decode QKV runs a warp per 32 outputs over weight rows staged in whole 128-byte reads, 28 ms to 5.3 ms a token, with the same bits.

@ashhart

ashhart commented Nov 23, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied for the QKV work. The Metal prompt-cache snapshot bug we found during review is in the base branch, not this PR, and #4542 fixes it, so nothing is needed from you for that.

  • Synthena

@joshuaswarren

Copy link
Copy Markdown
Contributor

Independent sm_120 check on e2671d58 (RTX 5060 Ti eGPU, aarch64 Linux, driver 615.78.08, zig 0.17.0, /opt/cuda/bin/nvcc, -Dsm=120):

./zig-out/bin/tf-cuda-test kernel-contract inkling2 → (filtered) SUMMARY 53 kernels: 53 pass gates 1-4, 0 fail, 0 known findings; 38 controls: 38 red as planted, 0 missed; 25 limit problems — exits TestFailed on the limit problems only.

Correctness side is fully green, including the new QKV path: tf_ink2_loop_qkv_rows1/4/16 pass gates 1-4 and their controls go red as planted. The limit problems are the same placeholder shape as on #4697's sm_120 run — live launch values vs 0,0 records. Your three new kernels carry the same gap: qkv_rows1 launches 96 regs / 4736 shared vs recorded 0,0; qkv_rows4 128 regs / 5120; qkv_rows16 128 regs / 6656 — worth noting since 6656 shared bytes at rows16 sits above the rows4 record pattern.

@jschmied

Copy link
Copy Markdown
Contributor Author

Thanks, @joshuaswarren. It is the same cause as on #4697. Your nvcc 13.4.92 build reads later_pending records as "no receipt", which shows as 0,0. The nvcc 13.0 rows for tf_ink2_loop_qkv_rows1/4/16 are filled on this head (e2671d5830). kernel-contract-limits over fatbins built for all five SMs here prints (record current) for each of them.

Your 13.4 values differ from 13.0's in registers only:

sm_120 13.4 live (yours) 13.0 record
rows1 96 regs, 4736 bytes 86 regs, 4736 bytes
rows4 128 regs, 5120 bytes 90 regs, 5120 bytes
rows16 128 regs, 6656 bytes 103 regs, 6656 bytes

The shared bytes match. On sm_121, 13.0 gives the same 86/90/103. nvcc 13.2 (cuobjdump) gives 94/92/100, so 13.2 does not match 13.4 either.

A 13.4 receipt set would need your full limits live lines. Nothing changes on this PR.

@ashhart
ashhart force-pushed the lead/inkling2-mini branch 3 times, most recently from aefb063 to c41911e Compare November 24, 2026 09:43
@ashhart
ashhart deleted the branch ashhart:lead/inkling2-mini November 24, 2026 09:43
@ashhart ashhart closed this Nov 24, 2026
@ashhart ashhart reopened this Nov 24, 2026
@ashhart
ashhart changed the base branch from lead/inkling2-mini to main November 24, 2026 09:45
@ashhart

ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Sorry for the noise: this was closed automatically when #4387 landed on main and its branch was removed. It is reopened and now targets main. We will rebase it onto main for you; nothing is needed from you.

  • Synthena

@ashhart

ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied. The QKV path retains the ascending FP32 sums and final BF16 rounding in source. Its contract now compares bytes with forced tiles and includes the missing-row control. The gate dependency matches the gate PR's tree, and the five target records are filled for compiler 13.0.

This looks ready for a serving comparison against current main and the gate-only version: matching token IDs and logits, including the 16/17-row boundary, and current timings. Please label the existing speed figures by revision and say which include the gate split.

  • Synthena

@ashhart

ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Thank you, @jschmied. We rebased this onto main and ran a serving A/B on a GB10 with Inkling 2 Mini: main against this branch, builds alternated base, new, new, base.

  • Tokens: identical in all 16 cells (one stream, four streams, resumed replies, and raw prompts of 1 to 33 rows, including the 16/17-row switch).
  • One-stream decode: 5.86 to 8.83 tok/s (+50.6%).
  • Four streams: 4.67 to 6.43 tok/s (+37.5%).
  • Long-prompt time to first token: 14.36 s to 14.08 s.

It's now in our landing queue.

  • Synthena

…ged in whole 128-byte reads

tf_ink2_loop_project_qkv tiles 16 rows by 16 outputs, so a one-row decode left 15 of 16 threads idle and
re-read its weights in 32-column slices. For up to 16 rows, tf_ink2_loop_qkv_rows1/4/16 run a warp per 32
output columns: the warp stages its 32 weight rows 64 columns at a time with whole-row 128-byte reads and the
rows' inputs, and each thread runs its output's rows over the same ascending FP32 chain, so the BF16 results
are tf_ink2_loop_project_qkv's. Prompt chunks keep tf_ink2_loop_project_qkv.
…places, its usage on every SM

projectQkvTiles keeps tf_ink2_loop_project_qkv's 16x16-tile launch callable at any row count (prompts take
it as before); the contract's ProjectQkv now names it in replaces, so gate 3 compares the decode kernels'
q, k and v bytes with the tiles' on the same inputs at every served shape. The control told one
row fewer stays red against the tiles.

tf_ink2_loop_qkv_rows1/4/16 compiled with nvcc 13.0 -cubin -O3 per SM: 128/128/90 registers on sm_80,
128/128/88 on sm_86 and sm_89, 86/90/103 on sm_120 as on sm_121; static shared bytes and the 32-thread bound
as on sm_121, no stack or spills.
@ashhart
ashhart force-pushed the ink2-mini-speed-qkv branch from 3cfb977 to 1ca1330 Compare November 25, 2026 03:15
@AIFlowML

Copy link
Copy Markdown
Contributor

I checked the landing interaction on 4c79cfe346 with #4724 (b918bc4b61), #4718 (1ca1330a73), then #4786 (a8ec28b078). The first two trial merges are clean. The third has one conflict in loop_engine.zig, between the kept_state and loop_round imports. Keeping both imports resolves it.

zig build test-inkling2-host test-inkling2-cuda-host check-native-cuda-host -Dfixed-cpu -j2 --summary all

On M5 Max the resolved tree passed 96 tests with four skips and cross-compiled the ARM64 Linux CUDA adapter in the same 18-step build. The combined GPU comparison remains open. The GB10 result above was for #4718 separately.

Igor Lessio - AIFLOW LABS

This branch has not been deployed

No deployments
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.

4 participants