Skip to content

Inkling 2 Mini on Metal: a prompt's experts, projections and loop gates on simdgroup matrices where the GPU has no tensor units, bit for bit - #4703

Closed
BlivionIaG wants to merge 30 commits into
ashhart:lead/inkling2-minifrom
BlivionIaG:port/prefill
Closed

BlivionIaG wants to merge 30 commits into
ashhart:lead/inkling2-minifrom
BlivionIaG:port/prefill

Conversation

@BlivionIaG

Copy link
Copy Markdown
Contributor

What this changes

On a Mac without tensor units (M1 to M4) the stack's prompt path is its scalar one: the wide-chunk experts (tf_ink2_*_units) and the prompt projections (tf_ink2_dense_units) are gated on the units, so a 1,024-row chunk runs the grouped one-row-sum kernels, the matvec kernels (32 rows a launch) and the scalar loop gates. On an M4 Pro that is 94 / 77 tok/s cold at 2,048 / 8,192 tokens. This adds the same products on 8x8 simdgroup matrices, each output summed in the order of the kernel it replaces, and dispatches them only where the units are absent. No sum changes: the served hashes are identical before and after.

This is a proposal on top of #4666, opened as a draft. Two commits, separable: the first is the experts (the larger part), the second the projections and the loop gates.

1. Routed experts on simdgroup matrices (experts_mma.metal, 94 to 140 tok/s). tf_ink2_{nvfp4,mxfp8}_{gu,down}_mma take the group kernel's counts and members as the grouped kernels do and run a threadgroup of 256 threads over 64 weight rows and bands of 32 of one expert's pairs. A weight row's sum in the grouped kernels is 32 lane chains (lane L adds blocks L, L + 32, ... in order, from zero) and then simd_sum's tree. An 8x8x8 MMA adds its eight products into C in column order and a BF16 product is exact in FP32, so a chain is one accumulator fragment, and the chains are added as simd_sum adds them (neighbours first: (c0 + c1) + (c2 + c3), then the passes). The decode of NVFP4 and MXFP8 weights, the block scales, ink2_phi and ink2_act are the grouped kernels'. experts.pathFor picks the tensor units where there are some, these kernels from 32 rows of a chunk (experts.mma_rows) where there are none, and the grouped scalar kernels below that.

2. Prompt projections (dense_mma.metal, 140 to about 230 tok/s). tf_ink2_dense_mma computes a 16 by 16 tile of outputs a threadgroup. sp_matvec adds, for each 256 columns, a lane's first four products to its next four and that to its running sum; the MMA sees four of the eight columns of an octet through a zero-masked A fragment, from a zero C, which is that dot of four, and the two are added in the matvec's order. The 32 lanes' chains are then added as simd_sum adds them. project() uses it for a prompt's projections of 16 rows or more; the loop gate's down projection (zeta, K = 4,608) uses the same kernel.

3. The loop blend (loop_mma.metal, a few percent). The blend's forget and gain dots are a sequential rank-order chain, which a K = 64 MMA chain from zero is exactly, and the four rounded steps after it are copied.

Which bits change. None. The contract holds each new kernel bit-equal to the kernel it replaces (Spec.replaces), and every reply below has the hash it had on #4666.

Checks on this head

  • Head commit: 01b537305f.
  • Lean checker, on the base (3e0e9bcb83 plus the stack, same checker) and on this head: 167 and 167, 0 new. python3 tools/pr_check.py --base upstream/lead/inkling2-mini: 0 errors, 1 advisory warning (no --pr number yet). No CHANGELOG line: the stack's pull requests add none.
  • Tests run:
    • zig build native tf-kernel-contract -Dcpu=apple_m1 -Doptimize=ReleaseFast: compiles.
    • The six Inkling host steps (test-inkling2-host 44 pass 4 skip, test-inkling2-metal-host 75 pass 5 skip, test-inkling2-controls-host 19 pass, test-inkling2-launch-map-host 2 pass, test-inkling2-metal-capabilities 1 pass, compile-inkling2-native-host): exit 0. test-inkling2-controls-host includes the edit-count tests of the new controls.
    • tf-kernel-contract --only inkling2 on an M4 Pro (no tensor units): SUMMARY 49 kernels: 49 pass gates 1-4, 0 fail, 0 known findings; 49 controls: 49 red as planted, 0 missed; 0 limit problems, exit 0; 42 and 42 before this change, plus the seven new entries and controls. The shapes gate of each new entry compares bytes with the kernel it replaces, at 5 to 10 shapes and edges (partial bands and tiles, one expert taking a pair of every row), and plants +2^20 / -2^20 term pairs so that any other order of adding gives other bits; the planted controls are a doubled phi, a reversed pass order, a shifted MXFP8 scale, a swapped in-pass tree, a changed half-add, a line-up of the four groups and a reversed rank loop.
    • zig fmt --check build.zig zig and git diff --check: clean.
  • The contract's suites are now initialised in place (Suites.init): the kernels list holds pointers into the suites, and the returned copy left them dangling, which a larger suite struct turned into a crash on this machine.
  • CI: not yet reported when this was written.
  • Not run, and why: zig build test test-golden whole (the stack's inherited failures are listed in Inkling 2 Mini on Metal: #4387's native build compiles, its norms write their output, and concurrent streams equal solo #4446). test-kernel-contract here has one failure, the CUDA inkling2_modelopt entries' "no compiled usage recorded" (no compiled CUDA usage on this Mac); all other 197 of its tests pass, and the Apple launch records of the new kernels are among them. The tensor-unit paths (M5) are not touched and were not run; M1 to M3 are not run (the kernels use simdgroup_matrix<bfloat>, as other families' do). CUDA and HIP are not touched.

Receipt

  • Main against branch: main does not serve this model. The comparison is the stack at 232ee08a39 against the stack with this branch, same checkpoint, same flags.
  • Boxes it reaches: Macs without tensor units, the inkling2_loop Metal path: every prompt chunk of 16 rows or more (experts from 32 rows), and the loop gates of a window of 16 rows or more.
  • New switch? None.
  • Environment: Zig 0.17.0, Xcode's Metal toolchain, Mac with an Apple M4 Pro (no tensor units), 48 GiB, macOS 27.2, thinkingmachines/Inkling-2-Mini@ed40e7da625f594fc9c4990f5f66be24873c22d7, -Dcpu=apple_m1 -Doptimize=ReleaseFast, with an unpushed 1 GiB pread cap in checkpoint_metal.zig (Metal loader: a checkpoint span of 16 GiB or more loads (each pread capped at 1 GiB) #4435; macOS refuses a longer pread; not part of this pull request).
  • Paths and platforms: Metal on M4 Pro (run). M1 to M3 and M5 (not run).
  • Exactness: serve CK --context 131072 --parallel 1, four prompts of 28, 416, 1,916 and 4,216 tokens, hashes of the served tokens (done chatcmpl-... sha=): the stack at 232ee08a39, after the first commit and after the second all give 18b0f0cf5cf3 77bf7c99cc66 ee856132f966 47029b25d5c5.

Cold first token, one stream, a unique prompt of the given length, --parallel 1 --context 131072, each binary a fresh server, in the order A B B A (the stack; this branch; this branch; the stack):

Prompt Stack This branch, experts only This branch
2,048 tokens 90.3, 91.6 tok/s 140.5, 144.6 238.2, 208.8
8,192 tokens 87.6, 88.0 143.9, 146.8 229.3, 230.2

(The experts-only rows are their own A B B A, stack 91.8 and 86.9 at 2,048 and 88.9 and 89.2 at 8,192.) The second 2,048-token run of the branch is the slower one in both A B B A sets, which I did not chase.

GPU time of one kernel at 1,024 rows (the scalar kernel against its simdgroup-matrix one, isolated on this M4 Pro):

Kernel Scalar Simdgroup matrices
NVFP4 gate-up, 128 experts 62.7 ms 19.8
NVFP4 down 37.9 10.8
MXFP8 gate-up 128 21
MXFP8 down 85 12
projection, 3,072 by 2,304 20.0 6.2
zeta, 64 by 4,608 4.4 0.27
blend, 2,304 outputs 2.09 0.73

Checklist

  • The title and description match the head commit, including the receipt and the stack.
  • New tests fail before the change, pass after it, and skip cleanly without their dependency (the contract entries exist only where there are no tensor units; each control is red).
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.
  • No precision traded for speed. No bits change.
  • Decode speed no slower than the stack: not re-measured; the decode kernels are untouched, and a one-row window takes the scalar loop gates and matvec as before.

sethforprivacy and others added 30 commits November 21, 2026 17:41
…ps in the scratch, so a stream's cache stages a window only
…16 weight rows by 32 of an expert's pairs an op
… skips the bias and key tests on far tiles, and loads a query row four values at a time
…p matrices without tensor units

- Adds tf_ink2_{nvfp4,mxfp8}_{gu,down}_mma: 64 weight rows by bands of 32 of an expert's pairs on 8x8 MMAs, each output summed in the order of the grouped kernels (lane chains of blocks, then simd_sum's tree), so every bit is the same.
- routedExperts takes experts.pathFor: tensor units as before, the MMA kernels from 32 rows on a GPU without units, the grouped scalar kernels otherwise.
- Contract: four entries with Spec.replaces (byte equality to the grouped kernels), four planted controls, four launch records.
- Contract suites are initialised in place: the kernels list holds pointers into them, so a returned copy dangled.
…p matrices without tensor units

- Adds tf_ink2_dense_mma: a 16 by 16 tile of outputs a threadgroup, each output summed as sp_matvec sums it (a lane's four-product halves added to its running sum, then simd_sum's tree). Prompt projections of at least 16 rows use it, and so does the loop gate's down projection (zeta, K 4608).
- Adds tf_ink2_loop_blend_mma: forget and gain each a rank-order chain on MMAs, then the scalar blend's four rounded steps.
- Dispatch only where the GPU has no tensor units; every output is bit-equal to the kernel it replaces.
- Contract: three entries with Spec.replaces, three planted controls, two launch records.
@BlivionIaG

Copy link
Copy Markdown
Contributor Author

Superseded by #4756: the same two commits ported onto #4618 (the base ashhart asked for on #4652), with the greedy token_sha of every cell equal on #4618's build and on the port. Closing this one.

@BlivionIaG BlivionIaG closed this Nov 23, 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.

2 participants