Repository navigation
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
Conversation
… rows where prep wrote them
…in the fewest 1,024-key folds a slot
…up to 32 rows is one launch
…uffers, independent launches side by side
… a row's sums never depend on its chunk
…mdgroup folds the partial sums itself
…e of an expert is full
…de between a prompt's chunks
…to eight rows of a window
…y for up to 16 rows
… the prompt kernel, a tile of keys a chunk
…ps in the scratch, so a stream's cache stages a window only
… cap while replies decode
…s from the cache, with no staging between
…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.
Contributor
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
3e0e9bcb83, then Inkling 2 Mini on Metal: #4387's native build compiles, its norms write their output, and concurrent streams equal solo #4446, 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, Inkling 2 Mini on Metal: routed experts on the M5 tensor units at every width (16 rows 49 to 36 ms, 64 rows 138 to 84 ms) #4523, Inkling 2 Mini on Metal: prompt chunks of 256 rows, filled a chunk a call (a 45K prompt 702 to 905 tok/s; replies keep decoding beside a prompt) #4608, Inkling 2 Mini on Metal: a round's logits in one pass of the vocabulary (one drafted stream 106 to 123 tok/s, four streams 237 to 281) #4618, Inkling 2 Mini on Metal: a prompt's projections and sliding attention on the tensor units (a 45K prompt 906 to 1,634 tok/s) #4631, Inkling 2 Mini on Metal: prompt chunks of up to 1,024 rows, their taps staged in the scratch (a 45K prompt 1,634 to 1,892 tok/s) #4644, Inkling 2 Mini on Metal: the prompt kernel reads its key tiles from the cache on M5 (a round at 96K context 28.6 to 21.5 ms, a 45K prompt 1,892 to 2,078 tok/s) #4645, Inkling 2 Mini on Metal: wide prompt chunks take the routed experts on turned tiles, 16 weight rows by 32 of an expert's pairs an op #4649 and Inkling 2 Mini on Metal: the prompt kernel keeps one set of sums in registers and skips the bias on far tiles (prefill 2,215 to 2,376 tok/s at 45K, same bits) #4666 (232ee08a39). This pull request's own commits are2b1243e75d(experts) and01b537305f(projections, loop gates). It touches the Inkling 2 Metal host files and adds three shader files; it replaces nothing on the tensor-unit path.atc overlap --json: not run; ATC is not installed on the machine that pushes.1. Routed experts on simdgroup matrices (
experts_mma.metal, 94 to 140 tok/s).tf_ink2_{nvfp4,mxfp8}_{gu,down}_mmatake thegroupkernel'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 thensimd_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 assimd_sumadds them (neighbours first:(c0 + c1) + (c2 + c3), then the passes). The decode of NVFP4 and MXFP8 weights, the block scales,ink2_phiandink2_actare the grouped kernels'.experts.pathForpicks 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_mmacomputes a 16 by 16 tile of outputs a threadgroup.sp_matvecadds, 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 thatdotof four, and the two are added in the matvec's order. The 32 lanes' chains are then added assimd_sumadds 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
01b537305f.3e0e9bcb83plus 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--prnumber yet). No CHANGELOG line: the stack's pull requests add none.zig build native tf-kernel-contract -Dcpu=apple_m1 -Doptimize=ReleaseFast: compiles.test-inkling2-host44 pass 4 skip,test-inkling2-metal-host75 pass 5 skip,test-inkling2-controls-host19 pass,test-inkling2-launch-map-host2 pass,test-inkling2-metal-capabilities1 pass,compile-inkling2-native-host): exit 0.test-inkling2-controls-hostincludes the edit-count tests of the new controls.tf-kernel-contract --only inkling2on 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 doubledphi, 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 zigandgit diff --check: clean.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.zig build test test-goldenwhole (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-contracthere has one failure, the CUDAinkling2_modeloptentries' "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 usesimdgroup_matrix<bfloat>, as other families' do). CUDA and HIP are not touched.Receipt
232ee08a39against the stack with this branch, same checkpoint, same flags.inkling2_loopMetal 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.thinkingmachines/Inkling-2-Mini@ed40e7da625f594fc9c4990f5f66be24873c22d7,-Dcpu=apple_m1 -Doptimize=ReleaseFast, with an unpushed 1 GiBpreadcap incheckpoint_metal.zig(Metal loader: a checkpoint span of 16 GiB or more loads (each pread capped at 1 GiB) #4435; macOS refuses a longerpread; not part of this pull request).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 at232ee08a39, after the first commit and after the second all give18b0f0cf5cf3 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):(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):
Checklist