Skip to content

inkling2 metal: ceiling-pow2 dense dispatch and guarded bf16_matvec - #4998

Merged
ashhart merged 3 commits into
ashhart:mainfrom
gilby:v4-13-ceiling-dense-rebased
Nov 25, 2026
Merged

ashhart merged 3 commits into
ashhart:mainfrom
gilby:v4-13-ceiling-dense-rebased

Conversation

@gilby

@gilby gilby commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Before: on main, dense() in zig/src/families/inkling2/metal/forward.zig decomposes non-power-of-two row counts greedily from 32, 16, 8, 4, 2, 1, dispatching multiple sp_matvec_* kernels (for example c=3 as 2 + 1, reading weights twice; c=5..7 reading weights 2-3x; and rows=6..8 DSpark verify rounds re-reading BF16 dense projections) while tf_bf16_matvec_t uses r < a.rows only after an unguarded X load and keeps all ROWS accumulators live together.

After: denseStep() dispatches a ceiling-power-of-two sp_matvec_{1,2,4,8,16,32} kernel for left <= 32 rows (32+32 for 64-row prefills): for 1..16 rows, one dispatch walks the weights once, and for 17..32 rows the 32-row entry (sp_matvec_32) is one dispatch with two 16-row passes (it still walks the weights twice rather than issuing multiple smaller dispatches). In zig/kernels/metal/core/bf16_matvec.metal, tf_bf16_matvec_t returns early for a.rows == 0, tf_bf16_matvec_chunk clamps partial-tile X loads branchlessly (min(r, active - 1u)), and ROWS > 16 tiles into two <=16-row passes over the weights to avoid acc[32] register spilling while keeping exact FP32 serial summation order. Added host dispatch and Metal tight-buffer exactness tests in zig/src/families/inkling2/metal/dense_test.zig and partial-tile row counts (5, 9, 17) in zig/tests/contract/metal_llama_dense.zig.

Checks on this head

  • Head commit: 3286430bfd
  • Lean checker, python3 tools/pr_check.py --base origin/main --pr 4998 (python3 tools/zig/lean_check.py): lean 164 vs 164, 0 new, 0 error(s), 1 warning(s) (advisory: no Unreleased changelog bullet; 5 changed files, max touched .zig file 408 lines); zig fmt --check build.zig zig and git diff --check origin/main..HEAD pass with exit 0; zig build compile-inkling2-native-host and zig build compile-inkling2-native-host -Dtarget=x86_64-windows exit 0.
  • Tests run, with the command and pass, fail and skip counts:
    • zig build test-inkling2-host -Dcpu=apple_m1 -Doptimize=ReleaseFast --summary all: 45 pass, 4 skip (49 total) (0 failed; skipped: 4 optional checkpoint/tokenizer env tests requiring TF_INKLING2_TINY / TF_INKLING2_REAL / TF_INKLING2_CKPT).
    • zig build test-inkling2-metal-host -Dcpu=apple_m1 -Doptimize=ReleaseFast (plus standalone test binary --cache-dir=./.zig-cache --seed=0xcc8ea13f): 112 passed; 6 skipped; 0 failed (skipped: 2 checkpoint serving tests, 2 checkpoint alloc failure tests, 2 JPEG tests pending newer libjpeg-turbo).
  • CI: Rebased onto main (9961ca4920) at 3286430bfd; CI checks pending after force-push.
  • Not run, and why: Apple M1/M2/M4 and M5 Ultra not run for this slice; M5 Pro and M3 Ultra A-B-B-A served runs establish the multi-Mac speedup across Metal 4 and Metal 3 backends.

Receipt

  • Main against branch: A-B-B-A served runs (tools/bench_concurrent.py --tokens 128 --reps 2, --context 8192 --parallel 4, 0 cross-leg token_sha256 mismatches across all cells on both machines):
    • Apple M5 Pro 64 GiB (main vs branch):
      • Plain (--no-drafts --parallel 4, c=3,4, T=0.0):
        • c=3: code T=0.0: 19.20 -> 19.90 tok/s (+3.65%, TTFT 1.028 -> 1.027s, -0.10%); chat T=0.0: 19.70 -> 20.30 tok/s (+3.05%, TTFT 3.617 -> 3.465s, -4.20%).
        • c=4: code T=0.0: 21.20 -> 21.20 tok/s (+0.00%, TTFT 1.267 -> 1.252s, -1.14%); chat T=0.0: 21.80 -> 21.80 tok/s (+0.00%, TTFT 4.750 -> 4.462s, -6.05%).
      • DSpark self-drafted (--drafter {TARGET} --parallel 4, c=1,4, T=0.0,1.0):
        • c=1: code T=0.0: 13.95 -> 14.00 tok/s (+0.36%, TTFT -11.76%); chat T=0.0: 12.50 -> 12.60 tok/s (+0.80%, TTFT -5.56%); code T=1.0: 10.55 -> 10.60 tok/s (+0.47%, TTFT -1.36%); chat T=1.0: 12.30 -> 12.45 tok/s (+1.22%, TTFT -2.01%).
        • c=4: code T=0.0: 11.20 -> 11.90 tok/s (+6.25%, TTFT -1.59%); chat T=0.0: 8.70 -> 9.30 tok/s (+6.90%, TTFT 5.146 -> 4.811s, -6.52%); code T=1.0: 8.70 -> 9.20 tok/s (+5.75%, TTFT -3.33%); chat T=1.0: 8.10 -> 8.70 tok/s (+7.41%, TTFT 5.084 -> 4.745s, -6.66%).
    • Apple M3 Ultra 512 GiB (main vs branch, Metal 3 overlay):
      • Plain (--no-drafts --parallel 4, c=3,4, T=0.0):
        • c=3: code T=0.0: 22.00 -> 22.55 tok/s (+2.50%, TTFT 0.790 -> 0.774s, -1.96%); chat T=0.0: 22.90 -> 23.50 tok/s (+2.62%, TTFT 2.793 -> 2.543s, -8.97%).
        • c=4: code T=0.0: 23.60 -> 23.65 tok/s (+0.21%, TTFT 0.994 -> 0.962s, -3.22%); chat T=0.0: 24.80 -> 24.80 tok/s (+0.00%, TTFT 3.565 -> 3.377s, -5.26%).
      • DSpark self-drafted (--drafter {TARGET} --parallel 4, c=1,4, T=0.0,1.0):
        • c=1: code T=0.0: 17.20 -> 17.25 tok/s (+0.29%, TTFT +9.04%); chat T=0.0: 15.30 -> 15.25 tok/s (-0.33%, TTFT -4.65%); code T=1.0: 14.40 -> 14.40 tok/s (+0.00%, TTFT -0.59%); chat T=1.0: 15.05 -> 15.20 tok/s (+1.00%, TTFT -2.56%).
        • c=4: code T=0.0: 14.35 -> 14.80 tok/s (+3.14%, TTFT -3.09%); chat T=0.0: 9.80 -> 10.15 tok/s (+3.57%, TTFT +0.54%); code T=1.0: 11.90 -> 12.20 tok/s (+2.52%, TTFT -0.24%); chat T=1.0: 9.60 -> 9.85 tok/s (+2.60%, TTFT -1.04%).
  • Boxes it reaches: zig/kernels/metal/core/bf16_matvec.metal, zig/src/families/inkling2/metal/forward.zig, zig/src/families/inkling2/metal/dense_test.zig, zig/src/families/inkling2/metal/host_test.zig, and zig/tests/contract/metal_llama_dense.zig.
  • New switch? No (always active).
  • Environment, with the TensorFold commit, Zig and backend toolchain, machine and GPU, checkpoint and revision: Commit 3286430bfd stacked on 9961ca4920, Zig 0.17.0 -Dcpu=apple_m1 -Doptimize=ReleaseFast, Apple M5 Max (applegpu_g17), Apple M5 Pro 64 GiB, Apple M3 Ultra 512 GiB; checkpoint Inkling-2-Mini.
  • Paths and platforms the change reaches, which I ran and which I could not: All Inkling 2 Metal BF16 dense() projections (c=1..32 and prefill) and shared TF_BF16_MATVECS kernels on Apple Silicon Metal GPUs (ll_matvec_* also covered by llama glue ll_matvec_* contract suite); Apple M1/M2/M4 not available.
  • Exactness: 0 cross-leg token_sha256 mismatches across all A-B-B-A legs (plain --skip-serial and dspark --serial) on both Apple M5 Pro and Apple M3 Ultra; Metal tight-buffer tests in dense_test.zig verify zero OOB reads/writes and exact 0-ULP FP32 serial reduction parity against main's greedy decomposition.
  • Decode speed before and after: See A-B-B-A tables above (up to +3.65% plain c=3 / +7.41% self-drafted c=4 on Apple M5 Pro; up to +2.62% plain c=3 / +3.57% self-drafted c=4 on Apple M3 Ultra).
  • Prompt speed before and after: Prefill chunks c=32+32 (two 32-row dispatches, each executing two 16-row passes over W, plus non-pow2 prompt remainder tails up to 16 rows in one dispatch) improve concurrent TTFT up to -6.05% plain / -11.76% self-drafted on Apple M5 Pro and -8.97% plain on Apple M3 Ultra, while keeping the disclosed single-stream DSpark first-token latency increase (+9.04% on M3 Ultra c=1 code T=0.0) visible above.
  • Licence of any new weights or drafter: n/a.
Kernel contract, only when a GPU kernel is new or changed
  • Kernels registered with the contract, and the kernel each one replaces: Modifies shared TF_BF16_MATVECS kernels (sp_matvec_{1,2,4,8,16,32} and llama glue ll_matvec_{1,2,4,8,16,32}), adding branchless bounds clamping on partial tiles (active < ROWS) and <=16-row register tiling for ROWS=32. No new kernel names added; replaces greedy 32,16,8,4,2,1 dispatch with ceiling-pow2 dispatch in denseStep.
  • A planted control for each new kernel, and each one fails as planted: 56/56 planted controls across all 8 non-dspark metal inkling2 suites on Apple M5 Max (3286430bfd) and 55/55 on Apple M3 Ultra fail red as planted (0 missed), plus metal_llama_dense.zig partial-tile probes (5, 9, 17 rows).
  • Each backend's SUMMARY line and exit status:
    • Apple M5 Max (--only "ll_matvec", 3286430bfd): SUMMARY 24 kernels: 24 pass gates 1-4, 0 fail, 0 known findings; 0 controls: 0 red as planted, 0 missed; 0 limit problems; gates 134317 ms, limits 123 ms (exit 0).
    • Apple M5 Max (metal inkling2, all 8 non-dspark suites on 3286430bfd; 50 kernels, 56 controls red, 0 fail, 0 missed, 0 limit problems):
      • metal inkling2 attention: SUMMARY 5 kernels: 5 pass gates 1-4, 0 fail, 0 known findings; 5 controls: 5 red as planted, 0 missed; 0 limit problems; gates 3086 ms, limits 76 ms (exit 0).
      • metal inkling2 prompt: SUMMARY 1 kernels: 1 pass gates 1-4, 0 fail, 0 known findings; 1 controls: 1 red as planted, 0 missed; 0 limit problems; gates 1492 ms, limits 61 ms (exit 0).
      • metal inkling2 glue: SUMMARY 10 kernels: 10 pass gates 1-4, 0 fail, 0 known findings; 13 controls: 13 red as planted, 0 missed; 0 limit problems; gates 445 ms, limits 72 ms (exit 0).
      • metal inkling2 experts: SUMMARY 12 kernels: 12 pass gates 1-4, 0 fail, 0 known findings; 17 controls: 17 red as planted, 0 missed; 0 limit problems; gates 4996 ms, limits 64 ms (exit 0).
      • metal inkling2 mma: SUMMARY 0 kernels: 0 pass gates 1-4, 0 fail, 0 known findings; 0 controls: 0 red as planted, 0 missed; 0 limit problems; gates 0 ms, limits 226 ms (exit 0; skipped on Apple M5 Max with tensor units).
      • metal inkling2 loop: SUMMARY 3 kernels: 3 pass gates 1-4, 0 fail, 0 known findings; 4 controls: 4 red as planted, 0 missed; 0 limit problems; gates 180 ms, limits 98 ms (exit 0).
      • metal inkling2 runtime: SUMMARY 6 kernels: 6 pass gates 1-4, 0 fail, 0 known findings; 6 controls: 6 red as planted, 0 missed; 0 limit problems; gates 57 ms, limits 28 ms (exit 0).
      • metal inkling2 vision: SUMMARY 13 kernels: 13 pass gates 1-4, 0 fail, 0 known findings; 10 controls: 10 red as planted, 0 missed; 0 limit problems; gates 1111 ms, limits 41 ms (exit 0).
      • metal inkling2 dspark: on base 9961ca4920, --only "metal inkling2 dspark" failed with error: DsparkThreads (tf_ink2_dspark_kappa maxThreads() == 64 < 256 in dspark_kernels.zig:122-125, untouched by inkling2 metal: ceiling-pow2 dense dispatch and guarded bf16_matvec #4998; fixed and merged on main in inkling2 metal dspark: match maxThreads guard to kappa (64) and attention (768) #5041).
    • Apple M3 Ultra (metal inkling2, Metal 3 overlay): SUMMARY 48 kernels: 48 pass gates 1-4, 0 fail, 0 known findings; 55 controls: 55 red as planted, 0 missed; 0 limit problems; gates 11127 ms, limits 38 ms (exit 0).
  • Chips I could not run: Apple M1/M2/M4 not directly available.

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.
  • 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.
  • Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off.
  • Drafted output equals "draft": false, a resumed prompt equals a fresh one, and concurrent equals solo.
  • No precision traded for speed.
  • Decode and prompt processing are no slower than main at one stream and the widest admitted; with --drafter, no slower than without it, greedy and sampled.

@AIFlowML

Copy link
Copy Markdown
Contributor

The trial merge of c011aec4bc into 4c79cfe346 is clean. On the M5 Max the Inkling suites and capability checks passed 150 tests with ten skips. The tight-buffer parity tests ran on Metal. The shared Llama/Ricursive matvec filter passed all 24 numerical cases, and the separate dropped-tail control failed at shapes as planted.

zig build test-inkling2-host test-inkling2-metal-host test-inkling2-metal-capabilities tf-kernel-contract -Dfixed-cpu -j2 --summary all
./zig-out/bin/tf-kernel-contract --only 'll_matvec_'
./zig-out/bin/tf-kernel-contract --only 'control: metal llama matvec'

I also checked the overlap note against #2890's current 5ba960b0f0. That head already has the same bf16_matvec.metal bytes as main. Adding this PR to the base gives #2890 one new conflict in metal_llama_dense.zig: its biased Q/K fixtures and the new 5/9/17-row cases for the unbiased fixture meet in the same initializer. Both sets can be preserved there when #2890 is integrated.

Igor Lessio - AIFLOW LABS

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thanks, @gilby. The active rows keep the same K loop, reduction and BF16 store, and the partial-row guards bound input loads. The tight-buffer parity and numerical control results are useful.

Safe for bounded A/B with pinned checkpoint/build identities and parent-shader byte comparisons. Keep shared-kernel limits and the disclosed first-token regressions visible. Also describe the 32-row entry as one dispatch with two 16-row passes; it still walks the weights twice.

  • Synthena

@AIFlowML

Copy link
Copy Markdown
Contributor

I trial-merged 3286430bfd cleanly into 9961ca492 and reran the checks on M5 Max. The build passed 158 tests with ten skips, all 24 Llama/Ricursive BF16 matvec contract kernels passed, and the dropped-tail control went red as intended. There were no launch-limit failures.

zig build test-inkling2-host test-inkling2-metal-host test-inkling2-metal-capabilities \
  tf-kernel-contract -Dfixed-cpu -j2 --summary all
./zig-out/bin/tf-kernel-contract --only 'll_matvec_'
./zig-out/bin/tf-kernel-contract --only 'control: metal llama matvec'

Ash’s wording note still applies to the doc comment above denseStep. For 17–32 active rows, sp_matvec_32 is one dispatch with two internal chunks, and each chunk walks the weight row. “One dispatch for up to 32 rows” describes the verified change. Checkpoint timing was outside this rerun.

Igor Lessio - AIFLOW LABS

@gilby

gilby commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Updated the PR description to state explicitly that sp_matvec_32 for 17..32 rows is one dispatch with two 16-row passes (it still walks the weights twice rather than issuing smaller dispatches), while 1..16 rows walk weights once in one dispatch, with shared ll_matvec_* limits and the disclosed first-token regressions retained.

  • Kevin

ashhart and others added 3 commits November 25, 2026 16:23
Refs ashhart#4961

Use ceiling-power-of-two sp_matvec_{1,2,4,8,16,32} dispatch in inkling2/metal/forward.zig
so non-power-of-two widths up to 32 rows read weights once instead of 2x-4x, and 64-row
prefills dispatch 32+32.

Guard X row loads in core/bf16_matvec.metal (early return for a.rows == 0 and branchless
min(r, active - 1u) clamping on partial tiles), and tile ROWS > 16 into <=16-row sub-chunks
inside the single 32-row kernel to avoid acc[32] register spilling while preserving exact
FP32 serial summation order.

Add host dispatch and Metal tight-buffer exactness tests for inkling2 dense(), and exercise
partial-tile row counts (5, 9, 17) in llama matvec contract probes.
@ashhart
ashhart force-pushed the v4-13-ceiling-dense-rebased branch from 3286430 to 2674e93 Compare November 25, 2026 16:23
@ashhart
ashhart merged commit 8d3f8f9 into ashhart:main Nov 25, 2026
@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thank you @gilby. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes.

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.

3 participants