Repository navigation
Inkling 2 Mini on Metal: prompt experts, projections and loop gates on simdgroup matrices without tensor units (stacked on #4618) - #4756
Conversation
3e0e9bc to
f790014
Compare
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
|
… on simdgroup matrices without tensor units - Experts, the attention projections and the loop gates of a chunk take simdgroup-matrix kernels where the GPU has no tensor units; each output is bit-equal to the kernel it replaces. - The contract registers the new kernels with a planted control each.
f20684b to
8e6b4e5
Compare
- A prompt pass reads every expert's and projection's weights once, so 256 rows a pass read them a quarter as often as 64 and fill the expert tiles. - Every row's sums are the same at every chunk width (the routed experts, projections and gates are bit-equal to the one-row kernels); the sliding ring grows to 1,280 keys. - The loop-gates contract probe also runs 200 rows. - Same change as the chunk width of ashhart#4608 (a9fae61), which has no effect on a chip without tensor units until a grouped simdgroup-matrix path like ashhart#4756's is under it. - Cold 2,048 tokens on an M4 Pro, A-B-B-A: 22.76 and 22.69 s before, 15.53 and 15.52 s after; greedy chat hashes, cold 2,048 (de3c13467508) and 8,192 (dc7ec8d3a016) tokens equal to main's.
- tf_inkling2_ar takes a row count and the strides between rows' blocks, partial sums and outputs, and a threadgroup runs one row's unchanged arithmetic; a visit launched it once a row and role (about 580 launches at 256 rows, 23 us each) and now launches it once a role. - The contract probe runs a batch of two rows with their own blocks, partial sums and two queries each against the float64 oracle and the planted control. - Cold 2,048 tokens on an M4 Pro, A-B-B-A: 15.40 and 15.80 s before, 10.84 and 11.45 s after; greedy chat hashes, cold 2,048 and 8,192 tokens equal to main's.
- tf_ink2_combine found each route by scanning every expert index for each output column (1,024 compares a sum); it now sorts the token's eight route slots by expert id once, equal ids in slot order, and adds them in that order, the same sum as before. - The route-slot-order control now removes the sort. - Cold 2,048 tokens on an M4 Pro, A-B-B-A: 11.78 and 11.84 s before, 9.90 and 9.46 s after; greedy chat hashes, cold 2,048 and 8,192 tokens equal to main's.
… one entry for ashhart#4756 - One bullet covers the simdgroup-matrix kernels, the 256-row chunks, the one-launch attention residual and the sorted combine.
|
Thanks @BlivionIaG. The scalar replacement checks and cancellation controls are useful, and the current head merges cleanly with main. The dense-MMA tail needs a read guard or an explicit padded-input contract. It loads whole eight-row tiles: the 45-row case reads through row 47, while its fixture allocates only 45 rows plus guards. Guarding the output store does not guard those reads. The head also has the later 256-row chunks, batched residuals and combine change. Those shared changes reach M5 too and increase cache/scratch memory, so the older “M5 untouched” receipt needs updating for the actual scope. Please retain exact token and seam comparisons alongside the memory and timing results after the tail fix.
|
…ds no row past the input - A row tile that is not whole read its rows through the next multiple of eight; those rows now read the last row, which only feeds outputs that are never stored, and whole tiles load as before. - A host test runs 1, 7, 9, 45 and 255 rows with the input followed by NaN: with every tile row stored, the tail rows repeat the last row's bytes, and the old loads (the planted control) put NaN there. - The kernel contract's dense shapes stay bit-equal to the matvec kernels and its controls stay red.
|
Thank you, @ashhart. The head is now Tail. Scope. The body now lists what reaches M5: not gated are Memory. Per-model scratch goes from 214 MiB to 642 MiB (+428 MiB); each stream's state grows by 147 MiB (31,457,280 B sliding ring, 122,683,392 B taps), independent of context. Added: 575 MiB at Equality. Cold 255, 256, 257, 511, 513, 1,000, 2,048 and 8,192 tokens all equal |
…tail - A focused test ends the projection's input at an unmapped page with no slack, binds it zero-copy, and runs under Metal's shader validation, which is the only layer that reports a read past a buffer's end. - The old loads abort there at rows=1 (invalid load at offset 16272 of 16384); the clamped tail reads nothing past the row bound at 1, 7, 8, 9, 16, 17, 45, 46 and 47 rows, and every output byte equals the chunked matvec reference's. - The dense-MMA contract fixture's matvec reference takes `dense`'s exact chunk ladder (32, 16, 8, 4, 2, 1) instead of a 13-row tail in a 16-row tile.
|
A correction to my last comment: I wrote that a GPU read past a buffer does not fault on this Mac, shader validation included. That was wrong for an input that ends at an unmapped page. The head now carries |
|
Thanks @BlivionIaG. The partial input tile now reads only valid rows, and the no-slack guard-page test with the old-kernel failure answers the read-safety request. The corrected M5 scope, memory costs and new M4 equality/timing results are credited. No further tail fix is requested. The remaining qualification covers the shared changes on M5 and current decode, drafting, concurrency and warm-cache behavior, including the coarser cache grid and its extra prefill work. The cold speedup alone does not cover those cells.
|
|
Thanks @ashhart. Both points are in Dense tail. The guard is the read clamp in Scope on M5. The "M5 untouched" statement was wrong: the 256-row chunks, the batched residual and the combine's single sort reach every chip. I ran main
Not run on the M5: decode, drafted output, |
# Conflicts: # zig/kernels/metal/inkling2/loop_sources.zig # zig/src/families/inkling2/metal/forward.zig # zig/src/families/inkling2/metal/loop.zig # zig/src/families/inkling2/metal/metal.zig # zig/tests/contract/metal_inkling2_main.zig
…atch The merged admission test measured 64-row scratch; 256-row chunks leave 6.19 and 15.19 GiB beside the weights on 36 and 48 GiB Macs, 10 and 25 streams at an 8,192 window.
|
Merged main |
|
I tested zig build test-inkling2-host test-inkling2-metal-host test-inkling2-dense-guard tf-kernel-contract -Dfixed-cpu -j2 --summary all
./zig-out/bin/tf-kernel-contract --only 'metal inkling2'On M5 Max, the standalone tree passed 147 tests with 11 skips and the device filter passed 61 kernels with 65 planted controls caught. The combined tree passed 150 tests with 11 skips, 62 kernels and 66 planted controls. Both reported zero launch-limit problems. The non-tensor-unit dense-guard test skipped on this GPU, so the M4 receipt remains the evidence for that guard. Combined serving comparisons at the 256-row chunk size remain open. Igor Lessio - AIFLOW LABS |
What this changes
On a Mac without tensor units (M1 to M4), Inkling 2 Mini's cold prompt runs its routed experts, attention projections and loop gates on 8x8 simdgroup matrices, with every sum in the order of the kernel it replaces, so a cold 2,048-token prompt takes about 9.8 s on an M4 Pro instead of 151 s on main. The head also carries three changes that are not gated on the chip (listed under "Scope"): 256-row prompt chunks, one attention-residual launch for a chunk's rows, and a combine that sorts a token's routes once. No sum changes.
3e1c4c582e(after inkling2 metal: the router and loop-gate dots a thread an output, bit for bit main's #4912's split router and loop gates). Commits:8e6b4e5de7(simdgroup-matrix experts, projections, gates),b6cdd6410f(256-row chunks),094439ae18(batched residual),a0c80c2cbc(combine),c50166ec60(dense tail guard),5a614aaf3b(guard-page test),f76a31ebb5(merge of main) and3d22cd3a91(admission test at 256-row scratch).atc overlap --json: not run.Scope: what reaches M5 and other tensor-unit chips
Chip-gated, loaded only where
device.tensorUnits()is false (the pipelines are null otherwise andforward.zigfalls back to main's path):dense_mma.metal,experts_mma.metal,loop_mma.metaland their dispatch.Not gated, so they run on M5 as well, and on decode as well where noted:
state.zig:prompt_rowsandmax_rows64 to 256, so the prompt chunk, the prefill step and the prompt cache grid (prompt_rows) are 256 on every chip;sliding_ring1,088 to 1,280.runtime_plan.zig,glue_plan.zig,loop_plan.zigandloop.zigaccept up to 256 rows.tf_inkling2_ar(glue.metal) takes a row count and strides;forward.ziglaunches it once for a chunk's rows (residual, the two prepare roles and the final head). Decode windows (16 rows or fewer) use it too.tf_ink2_combine(experts.metal) sorts a token's 8 routes by expert id once; decode uses it too.experts.zigrefusestopkabove 8.prompt_attention, the experts and loop kernels of main) are unchanged but now see chunks of up to 256 rows; their 256-row behaviour is measured on an M5 Ultra in the receipt below (token ids equal to main's in every cell, including the 256-row seams).The only effect of the units that I changed is the memory under "Memory": every chip pays it.
Dense tail guard (review of this pull request)
tf_ink2_dense_mmaloaded whole 8-row input tiles, so 45 rows read through row 47. A tile that is not whole now reads the last row for its rows pastrows(they only feed outputs that are never stored); whole tiles load as before and every sum is unchanged. The other kernels of the slice do not read pastrows: the experts MMA clamps each pair's row (min(xp, live - 1)) and its weights are whole tiles by construction, and the gates MMA zero-fills and bounds-checks its staging.dense_tail_test.zig(intest-inkling2-metal-host) runs 1, 7, 9, 45 and 255 rows with the input followed by NaN and every tile row stored: the tail rows repeat the last row's bytes and the valid rows equal the old loads. The old loads, kept as the planted control, put NaN in the tail rows (checked in the same test). On the previous head the same probe gives NaN in 112, 16, 112, 48 and 16 tail outputs for 1, 7, 9, 45, 255 rows and 0 on this head.metal_inkling2_dense_guard.zig(zig build test-inkling2-dense-guard, run under Metal's shader validation with abort on fault) ends the projection's input at an unmapped page with no slack, binds it zero-copy and runs 1, 7, 8, 9, 16, 17, 45, 46 and 47 rows: the clamped tail passes and every output byte equals the chunked matvec reference's; with the previous kernel the same test aborts (SIGABRT, invalid load past the buffer). My first version of this paragraph said a read past a buffer does not fault on this Mac; that was wrong for an input that ends at an unmapped page, which is what this test builds.Checks on this head
3d22cd3a91(merge of main3e1c4c582eand the admission test's 256-row values).tools/pr_check.py --base 3e1c4c582e --pr 4756: 0 errors, 0 warnings.zig fmt --check build.zig zigclean.-Dcpu=apple_m1):test-inkling2-metal-host102 pass, 6 skip;test-inkling2-host45 pass, 4 skip;test-inkling2-dense-guardpasses;tf-kernel-contract --only inkling2: 67 kernels pass gates 1-4, 74 controls red as planted, 0 missed, 0 limit problems. Exactness on the M4 Pro against main3e1c4c582e: cold 255/256/257/512/513, chat 8/16/24, greedy 256, seeded 64 and 4 concurrent equal to solo, 14 cells, full token ids and sha256 equal.--parallelabove 4 (the M5 cells below ran at--parallel 1).Receipt (M4 Pro 48 GiB, macOS 27.2, Zig 0.17.0,
-Dcpu=apple_m1 -Doptimize=ReleaseFast,thinkingmachines/Inkling-2-Mini@ed40e7da625f594fc9c4990f5f66be24873c22d7,--no-drafts --prompt-cache-gib 0 --no-thinking, load average 2 to 5, memory pressure level 1)Exactness, greedy
token_sha(the server'ssha=), main9edf2f89d4against this head applied on it. Main's chat hashes changed since the earlier receipt (eea0560bb4sends reasoning_effort "none" to the template, so the first prompt is 26 tokens, not 28). On the old main base the head gives 0b037128ee78 b37d8a0e39c4 146f8038bfc7 870a47079391, the earlier main's.--context 8192unless noted)--context 16384)--prompt-cache-gib 2), turns 1 and 2The resumed turn reuses 576 cached tokens on main and 512 on the head: the cache grid is the chunk width, so up to 255 more tokens are prefilled again. No
memory pressure: warningin the head's logs; main's first run logged one transient warning, which did not change a greedy hash.Cold first token, 2,048 tokens, A-B-B-A (A =
8e6b4e5de7, the pull request's earlier head; B = this head's tree, both on the same base): A 22.37 s and 22.33 s, B 9.72 s and 9.82 s. On9edf2f89d4main takes 151.4 s at 2,048 tokens and 642.9 s at 8,192; the head applied on it takes 9.75 s and 39.7 s (one run each).Memory (
/usr/bin/time -lon the server after load, no request; main9edf2f89d4against the head applied on it):--context 8192 --parallel 1--context 8192 --parallel 4--context 131072 --parallel 1The footprint grows by 0.45 GB, the scratch, and does not move with the context or the streams, because a stream's cache is allocated when it starts and its new pages are untouched until used (after a cold 8,192 prompt the footprints are 20.64 GB and 21.06 GB). Max RSS is dominated by page cache of the mapped checkpoint and varies by 1 GB between identical runs here, so I give it for reference only. The server's own budget line falls from 15.6 to 15.2 GiB for stream and prompt state at every setting.
Allocated bytes, from
state.zig:scratchBytes(32) + scratchBytes(prompt_rows), 2,336,868 bytes a row plus 768, of whichlogits_allis 800,232 a row,part_o786,432 andmessages442,368. Main 224,340,864 bytes (214 MiB); head 673,019,520 (642 MiB): +448,678,656 bytes (428 MiB).cacheBytes(capacity):40 * sliding_ring * 2 * 2048 + 8 * align64(capacity) * 2 * 2048 + 2 * conv_bytes + 48 * (3 + max_rows) * 13,312 + 4 * 200,058. Growingsliding_ringby 192 adds 31,457,280 bytes andmax_rowsby 192 adds 122,683,392 in the taps: +154,140,672 bytes (147 MiB) a stream, independent of the context. That is 471 to 618 MiB a stream at 8,192 and 4,311 to 4,458 MiB at 131,072.--parallel 1: 428 + 147 = 575 MiB; at--parallel 4: 428 + 588 = 1,016 MiB; the same at both contexts.Receipt: M5 Ultra (256 GiB, macOS 27.0, tensor units), main
9a38b1f7b3against this head5a614aaf3b, same checkpointOne cold server per cell,
--prompt-cache-gib 0 --parallel 1 --no-drafts --no-thinking, A-B-B-A; the tensor-unit kernels stay, and the shared 256-row chunk, batched residual and combine changes apply.Exactness, 10 cells: the first token id, its logprob and
token_shaare equal on main and on the head.token_shab53cd22fd6788b9d4d0e236bb53cd22fd6788b9d4d0e236b8b9d4d0e236bde3c134675088a44904f45d64c2031fb15353a974c335a4e0e69856242e3The M4 and M5 first tokens differ from each other on the same prompt (255:
150019on the M4 Pro,151530on the M5 Ultra): the chips' arithmetic differs, so the comparison is main against head on each chip.Cold first token (prefill seconds, server
ttft), A-B-B-A. Block 1 ran with no other server on the machine; block 2 started after another job's server had been up for a short time. The first head run (B1) includes a one-off Metal pipeline compile of about 5 s that main's cached pipelines did not pay, so read B2 for small prompts.The A2 8,192 run was cancelled when another job's server start-up terminated mine mid-request; it is not used. B2 differs from B1 by 11 % at 8,192.
Memory: peak RSS 18.49 GiB on main and 18.50 GiB on the head (both repeats); peak
phys_footprint19 GB on both. The 575 MiB the scratch and stream caches add at--parallel 1(above) is below this sampler's resolution on the M5.Checklist
--parallelabove 1 on the M5 not run.