Repository navigation
Inkling 2 Mini on Metal: experts, gates, router, depth mix, combine, row batching, attention chunks and a concurrent forward (1,159 to 14 ms a row on M5 Ultra) - #4448
Conversation
Derive per-visit cache access and exact arithmetic for the 48 visits so CUDA admission and kernels have a reference before serving is enabled.
Decode stored NVFP4 and MXFP8 expert weights to BF16 and launch them beside the grouped expert/vision kernels, with exact tensor baselines and planted faults.
Launch cross-pass messages, rank-64 gates, residual steps and split QKV with FP32 reductions, BF16 rounding controls and per-launch resource records.
Run all 48 visits with per-visit cache state, prompt snapshots and exact rollback. Refuse TP, vision and DSpark loading until those engines exist.
… rows where prep wrote them
aefb063 to
c41911e
Compare
|
Sorry for the noise: this was closed automatically when #4387 landed on main and its base branch was removed. It is reopened and now targets
|
|
An update on your Metal stack, @sethforprivacy, now that #4387 is on main. Five of your Inkling Metal commits already landed there, and #4446, #4607 and #4635 are closed as landed. One commit here, the lane-strided FP32 sum ( Nothing is needed from you for the rebase; we will follow up on each PR.
|
|
An M4 Pro data point for this stack, in case it helps: Apple M4 Pro, 48 GiB, no tensor units,
Nothing is asked of you; I wanted the non-tensor-unit numbers to exist somewhere, since the descriptions here are M5 Ultra. Our own non-tensor-unit prompt kernels for |
|
For the exact form that @ashhart asked for above: #4919 is a pull request on |
… dot the serial chain Each expert product ran one 32-thread group a route pair, its lanes looping over the pair's outputs: 36 outputs a lane for GU (n 1152), 72 for down (n 2304), and eight groups a decode step. Combine ran one group a token. The four products and the combine now launch threadgroups along the outputs by route pairs (or tokens), one thread an output. Each thread runs the same ink2_dot_nv / ink2_dot_mx as before: one FP32 chain from column 0 up, every product rounded before its add (fp contract off), then the same BF16 rounding and activation. Combine keeps its ascending-expert loop. Only which thread computes an output changes, so every output is bit-identical to the serial reference. Threads past the row return before any read. Launches stay 32 threads, so the existing records hold; the quantize kernels are unchanged. The grid of outputs by route pairs and launchGrid follow #4448's 2f386ca; its lane-strided partial sums and simd_sum, which change the FP32 sum order, are not taken. Tests: test-inkling2-controls-host checks the source keeps both dots serial with no simd_sum and launches every product and the combine one output a thread (fails before: five lane loops), and that the grid covers every output exactly once, with no thread past the row reading. The block count rounds up as n / 32 plus one for a remainder, so the largest u32 widths (4294967265 through 4294967295) take 134217728 blocks instead of overflowing n + 31; a test asserts those widths and fails on the n + 31 form. Co-authored-by: Seth For Privacy <40500387+sethforprivacy@users.noreply.github.com>
… dot the serial chain Each expert product ran one 32-thread group a route pair, its lanes looping over the pair's outputs: 36 outputs a lane for GU (n 1152), 72 for down (n 2304), and eight groups a decode step. Combine ran one group a token. The four products and the combine now launch threadgroups along the outputs by route pairs (or tokens), one thread an output. Each thread runs the same ink2_dot_nv / ink2_dot_mx as before: one FP32 chain from column 0 up, every product rounded before its add (fp contract off), then the same BF16 rounding and activation. Combine keeps its ascending-expert loop. Only which thread computes an output changes, so every output is bit-identical to the serial reference. Threads past the row return before any read. Launches stay 32 threads, so the existing records hold; the quantize kernels are unchanged. The grid of outputs by route pairs and launchGrid follow #4448's 2f386ca; its lane-strided partial sums and simd_sum, which change the FP32 sum order, are not taken. Tests: test-inkling2-controls-host checks the source keeps both dots serial with no simd_sum and launches every product and the combine one output a thread (fails before: five lane loops), and that the grid covers every output exactly once, with no thread past the row reading. The block count rounds up as n / 32 plus one for a remainder, so the largest u32 widths (4294967265 through 4294967295) take 134217728 blocks instead of overflowing n + 31; a test asserts those widths and fails on the n + 31 form. Co-authored-by: Seth For Privacy <40500387+sethforprivacy@users.noreply.github.com>
Update: head
cca30e890(two more commits)How these were found. I repeated one small stage ten extra times a visit and timed a one-row forward, which gives each stage's cost by itself. On this GPU a launch costs from 2 to 28 µs depending on what its threads do, far more than its arithmetic: a copy 2.0 µs, the norm 8.1, each of the gates' three launches 6.6, the router's two together 17.8, the attention's partial sums 19.1, the depth mix 21.7 and the experts' combine 27.8. A thread here runs a serial step slowly, and strided single-value loads slowly; the two worst stages were both long serial loops.
7045ba3b3tf_inkling2_ar): a thread reads four consecutive values of each source (it read nine values 256 apart), 576 threads a threadgroup, and each of the 18 simdgroups folds the partial sums itself, so the two sections one thread ran alone are gone. 21.7 to 7.5 µs a launch, twice a visit.cca30e890tf_ink2_combine): the firsttopkthreads of a threadgroup rank the token's routes once, and each output adds its terms in that order; every output thread used to rank them again, 512 loop steps each. 27.8 to 5.1 µs a launch.Contract:
tf_inkling2_arkeeps its entry and passes its gates with its control red; its record is now 576 threads, reserved, and 720 bytes of threadgroup memory, andtf-kernel-contract --offlinebuilds it for M1 and M2 at 576 threads.tf_ink2_combinepasses its exact ascending-expert check; its control now plantsslot = rin the new loop, and its record carries the 128 bytes of the routes' order.GPU time of one forward on this pull request's own path (without #4523), cache empty: one row 16.1 to 14.0 ms, 16 rows 49.4 to 47.2, 64 rows 138 to 134.
Served, TensorFold's clients, 256 tokens, greedy, three reps,
--parallel 4 --context 16384 --prompt-cache-gib 0, with #4447, #4465 atdb534d6cfand #4523 applied:--drafterbench_concurrent.py --alone --serial: 12 of 12 replies equal their solo runs at four streams, plain and with the drafter, and the serial check passes (170 and 109 drafts accepted).Against the bar on this machine, on the prompt both servers were timed on: one stream is at 1.09 of oMLX with the drafter and 1.09 without it, and four drafted streams are at 1.49. The prompt path is still short: 702 tok/s on a 45K-token prompt against oMLX's 2,628 to 2,895.
Checks on this head:
zig build nativecompiles; the six Inkling host steps exit 0;tf-kernel-contract --only inkling2:SUMMARY 41 kernels: 41 pass gates 1-4, 0 fail, 0 known findings; 41 controls: 41 red as planted, 0 missed; 0 limit problems, exit 0;zig fmt --checkandgit diff --checkclean; lean 167 on the base and 167 here, 0 new.Update: head
f06c27ab1(one more commit)f06c27ab1: a greedy row with no token mask is picked sixteen logits a step. The backend'sdrawscanned the 200,058 logits of each verify row one at a time, 1.3 ms a round at four rows; the new pick takes the maximum sixteen at a time and then finds its first index, 0.1 ms a round. The pick is the scan's: the first of equal maxima, and a NaN only when logit 0 is one; a unit test compares the two on rows with equal maxima, NaNs, both infinities and zeros of both signs. Rows with a token mask and sampled rows are untouched. On the 300-token Fibonacci reply thetoken_shais unchanged and decode goes from 108.1 to 112.8 tok/s (measured with #4523 applied). The tables below were taken before this commit.Checks on this head:
zig build nativecompiles; the six Inkling host steps exit 0;tf-kernel-contract --only inkling2:SUMMARY 41 kernels: 41 pass gates 1-4, 0 fail, 0 known findings; 41 controls: 41 red as planted, 0 missed; 0 limit problems, exit 0;zig fmt --checkandgit diff --checkclean; lean 167 on the base and 167 here, 0 new.Update: head
d87e8695d(two more commits)The numbers in this section replace the earlier ones where they overlap.
e4f793b1dd87e8695dloop.gatesEach, a step at a time), and every stream's attention (attention.partialsandattention.merge, so all streams' partial sums run before any merge). The forward is also cut into five command buffers, each committed once encoded, so the GPU starts while the host encodes the rest.How
d87e8695dwas checked for missing barriers: every scratch buffer and all 49 per-visit states of a 64-row forward were dumped under the serial encoder and under the concurrent one, five runs: 29 buffers, 0 differ, byte for byte. Two streams run as one shared forward for 40 steps equal their solo logits exactly.GPU time of one forward, same machine, cache empty:
49e474a55A one-row forward is on the order of 1,700 launches, most of them small, and they ran one after another; the independent ones now overlap, which leaves on the order of 1,150 steps in sequence.
Checks on this head:
zig build nativecompiles; the six Inkling host steps exit 0;tf-kernel-contract --only inkling2:SUMMARY 41 kernels: 41 pass gates 1-4, 0 fail, 0 known findings; 41 controls: 41 red as planted, 0 missed; 0 limit problems, exit 0;zig fmt --checkandgit diff --checkclean; lean 167 on the base and 167 here, 0 new. No kernel is added or changed by these two commits.Served, TensorFold's clients, 256 tokens, greedy, three reps,
--parallel 4 --context 16384 --prompt-cache-gib 0, with #4447 and #4465 at their current heads applied:--drafterbench_concurrent.py --alone --serial: 12 of 12 replies equal their solo runs at four streams, plain and with the drafter, and the serial check passes (138 and 132 drafts accepted). Peak footprint 20 GB plain, 21 GB with the drafter.Against the bar on this machine, on the one prompt both servers were timed on: with the drafter, one stream is at 1.04 of oMLX and four streams at 1.11; without it, four streams are at 1.00 and one stream at 0.77 (oMLX's figure is with its own drafter on; I have no plain oMLX figure). Prompt speed is 419 tok/s on a 45K-token prompt against oMLX's 2,785, so the prompt path is still far short.
Update: head
49e474a55(four more commits)Since the description below was written this branch gained the shared-forward fix from #4446 (here as
6f0f29433) and three speed commits. The numbers in this section replace the receipt's where they overlap; the rest of the description stands ford447ca703.ef5bd8748tf_inkling2_ar) and the loop gates take every row of a forward in one launch, where the forward looped rows. The gates read row r's message at a stride, since the messages interleave 48 entries a row.2c8338c4dtf_ink2_grouplists each expert's route pairs, and the_groupedkernels decode a weight block once for a pass of four pairs. Each row's sums are the ungrouped kernels' sums, in the same order.eaea4889dGPU time of one forward, same machine, cache empty:
d447ca703One row with 1,200 tokens in the cache: 30.5 ms before the attention commit, 23.5 ms after.
Checks on this head:
zig build nativecompiles; the six Inkling host steps exit 0 (twoattention_test.zigcases pin the new chunk geometry);tf-kernel-contract --only inkling2:SUMMARY 41 kernels: 41 pass gates 1-4, 0 fail, 0 known findings; 41 controls: 41 red as planted, 0 missed; 0 limit problems, exit 0;zig fmt --checkandgit diff --checkclean; lean 167 on the base and 167 here, 0 new.Exactness on this head, with the drafter commits of #4447 and #4465 also applied for the drafted rows:
ef5bd8748and2c8338c4dleave everytoken_shaand the comparison with oMLX's model exactly as they were (the same minimum, median and logit cosines to seven digits).bench_concurrent.py --alone --serial, four streams: 12 of 12 replies equal their solo runs, plain and with the drafter, and the serial check passes with 138 and 132 drafts accepted.token_sha.Served, TensorFold's clients, 256 tokens, greedy, three reps,
--parallel 4 --context 16384 --prompt-cache-gib 0, with #4447 and #4465 applied:--drafterSo against the bar on this machine: one drafted stream is at 0.89 of oMLX on the chat prompt, and four streams at 0.61 with the drafter and 0.72 without. Not met.
Not registered yet:
tf_ink2_groupand the four_groupedkernels have no contract entry of their own. They are meant as bit-preserving companions of the ungrouped kernels, and the evidence here is the unchanged token hashes; areplacesentry with byte equality is the right gate, and I will add it if you want this in.Prompt speed: 353 tok/s on a 45K-token prompt (64-row chunks). The chunk size is the limit now: each chunk re-reads every expert it routes to at every visit.
What this changes
Before: on #4387's Metal path one decode row takes 1,159 ms of GPU time on an M5 Ultra, 1,130 ms of it in the routed-expert kernels, so plain decode is 0.86 tok/s. After: the same row takes 19.7 ms and plain decode is 38 tok/s. The kernels compute the same arithmetic contract (stored NVFP4 and MXFP8 decoded in registers to BF16, BF16 activations, FP32 sums); what changes is how each sum is split over threads.
This is a proposal on top of #4387, opened as a draft. It is the speed slice I offered on #4274, and it does not reach the speed bar yet (see the receipt).
3e0e9bcb83, then the serving fixes in Inkling 2 Mini on Metal: #4387's native build compiles, its norms write their output, and concurrent streams equal solo #4446 (992258230,c68340d31). This pull request's own commits are2f386ca16(experts),49c798652(gates and router),a889fac06(launch records) andd447ca703(a comment's length).atc overlap --json: not run; ATC is not installed on the machine that pushes.2f386ca16, experts49c798652, gates and routerWhat was slow, and what each commit does:
experts.metal). Each kernel launched one 32-thread group per (token, expert) pair, and each thread walked 36 outputs of 2,304 columns one element at a time. Now a threadgroup is eight outputs of one pair and a simdgroup is one output: lanelsums the 16-column (NVFP4) or 32-column (MXFP8) blocksl, l + 32, ...in column order, andsimd_sumadds the 32 lanes. Each weight is stillbf16(e2m1 * e4m3 * phi)orbf16(ldexp(e4m3, scale - 127)); the NVFP4 inner product is taken as(2 * e2m1) * (e4m3 / 2), which is exact either way. The combine is one thread an output and adds the routed experts in ascending expert order as before.loop.metal). One threadgroup did everything for a row, with 64 threads each running a 4,608-step serial dot. Now three launches: normalize[h | m]to BF16, the rank-64 projection as one simdgroup a rank with eight columns a lane a step (the shape ofcore/bf16_matvec.metal), then the forget and gain dots and the blend, one thread an output, in rank order as before.glue.metal). 128 threads each ran a 2,304-step serial dot and one thread then scanned 8 x 128 keys. Now the dots are one simdgroup an expert, and the pick is by rank: among ordered keys, "greatestscore + biasfirst, lowest expert on a tie" is what the scan chose. If any key is NaN the kernel takes the original scan, so those picks are unchanged.Two things about this GPU that the measurements showed: one GPU thread runs a long serial loop slowly (a 2,304-step dot is on the order of 100 µs), and a loop whose bound is not a compile-time constant runs about twice as slow as the same loop with a constant bound.
Which bits change. The order of three sums: each expert dot (lane-strided blocks then
simd_sum, where it was one column-order sum), the gates' rank-64 projection and the router's dot (eight columns a lane a step, where each was one column-order sum). Every rounding point is where it was. The gates' and router's intermediates live in a small buffer owned beside their pipelines (64 rows), so their launch signatures and the contract probes are unchanged.Checks on this head
d447ca703.zig build native -Dcpu=apple_m1: compiles.test-inkling2-metal-host,test-inkling2-host,test-inkling2-controls-host,test-inkling2-launch-map-host,test-inkling2-metal-capabilities,compile-inkling2-native-host): exit 0.zig fmt --check build.zig zigandgit diff --check: clean.zig build test test-golden -Dcpu=apple_m1, run on these commits with my timing code also applied: 936 of 945 steps, 16,780 of 16,937 tests passed, 151 skipped, 6 failed,test-golden324/337 and 69/69 equal with 0 unexpected. The failures are Inkling 2 Mini on CUDA and Metal #4387's: its own head fails the same tests (rollback_testlone keep twice, the DeepSeekvision_configtest,cuda_keep_others_test,contract.registry, thememcpylink check) beside its two compile errors.Receipt
inkling2_loopMetal path: decode and prompt rows both run these kernels.thinkingmachines/Inkling-2-Mini@ed40e7da625f594fc9c4990f5f66be24873c22d7,--parallel 4 --context 16384 --prompt-cache-gib 0. The pread cap of checkpoint_metal: clamp pread chunks to INT_MAX #4433 or Metal loader: a checkpoint span of 16 GiB or more loads (each pread capped at 1 GiB) #4435 is applied locally. The served rows below were measured with the DSpark fix of Inkling 2 Mini DSpark on Metal: attention scale 1/sqrt(128), and the recurrent step reads kappa as FP32 #4447 also applied; it does not touch the plain path.max_total_threads_per_threadgroup, and the records say so.token_sha 0bf3c7a5bfdbbefore the experts commit, after it, and after the gates and router commit.tools/bench_concurrent.py --alone --serialatd447ca703: alone equals itself (3 of 3) and the serial check passes, and at four streams 9 of 12 plain replies differed from their solo runs. That was Inkling 2 Mini on CUDA and Metal #4387's shared-forward offset, fixed in Inkling 2 Mini on Metal: #4387's native build compiles, its norms write their output, and concurrent streams equal solo #4446 and carried here since6f0f29433: see the update above.tools/bench_openai.py --tokens 256 --reps 3 --temperatures 0:--drafter(with #4447)tools/bench_concurrent.py --tokens 256 --reps 3 --temperatures 0 --levels 1,4 --alone --serial, total: plain 62.4 (code) and 62.9 (chat) tok/s; with the drafter 38.1 and 30.5, slower than plain for the reason given in Inkling 2 Mini DSpark on Metal: attention scale 1/sqrt(128), and the recurrent step reads kappa as FP32 #4447.footprint, after the benches).Kernel contract
replaces.tf_ink2_nvfp4_gu,tf_ink2_nvfp4_down,tf_ink2_mxfp8_gu,tf_ink2_mxfp8_downandtf_ink2_combinekeep their names and bindings with a new launch grid.tf_inkling2_loop_gatesandtf_inkling2_routerkeep their names as the first launch of their operator;tf_inkling2_loop_zeta,tf_inkling2_loop_blendandtf_inkling2_route_scoresare the added launches, and each runs inside its operator's existing probe (loop.gates,glue.router), against the same FP64 reference. The three added launches have no registry record or control of their own yet; say if you want them registered separately and I will add that.mx_down_scale,combine_slot_order) and the gates' and the router's had their anchor text updated to the new source. The gates' control now doubles zeta, the router's still drops the bias from the key.SUMMARYline and exit status: Metal,tf-kernel-contract --only inkling2(-Dcpu=apple_m1 -Doptimize=safe), M5 Ultra:SUMMARY 41 kernels: 41 pass gates 1-4, 0 fail, 0 known findings; 41 controls: 41 red as planted, 0 missed; 0 limit problems, exit 0.Checklist
"draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. (Drafted equals plain, and concurrent equals solo since6f0f29433. Resumed not run.)--drafter, no slower than without it. (Faster than Inkling 2 Mini on CUDA and Metal #4387 everywhere measured; with the drafter four streams are slower than without, as in Inkling 2 Mini DSpark on Metal: attention scale 1/sqrt(128), and the recurrent step reads kappa as FP32 #4447.)