Skip to content

inkling2: coalesce multi-row logits on Metal with 0-ULP contract parity - #5135

Merged
ashhart merged 2 commits into
ashhart:mainfrom
gilby:v5-1-logits-coalesced
Nov 26, 2026
Merged

ashhart merged 2 commits into
ashhart:mainfrom
gilby:v5-1-logits-coalesced

Conversation

@gilby

@gilby gilby commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Before: tf_inkling2_logits in zig/kernels/metal/inkling2/glue.metal launched 1 thread per (vocab, row) reading unembedding weights strided across threads and re-reading unembedding weights separately for every row in a multi-stream / speculative verification batch.

After: adds coalesced, multi-row tiled Metal logits kernels tf_inkling2_coalesced_logits_r{4,8,16,32} (selected via logitLaunch(rows) / glue.logits()) that stage 32 unembedding rows of 16 bfloat2 elements in threadgroup memory (sh_w[32][33]) and tile TILE_R hidden vectors (sh_h) while preserving exact k = 0..2303 sequential FP32 accumulation, BF16 rounding before softcap30, and zero-ULP bit-for-bit output parity against tf_inkling2_logits (logitsBaseline() kept for single/baseline comparison & contract reference).

Checks on this head

  • Head commit: 2e70cabde8bfbfebcef1e1c29d0cccc9b07e796e (rebased cleanly onto a44ffee88a #5001).
  • Lean & structure checker, python3 tools/pr_check.py --base origin/main --pr 5135: 0 error(s), 4 warning(s) (advisory --pr / inherited cycles; lean 164 vs 164, 0 new; import cycle edges 24 vs 24, 0 new). All touched files <= 564 lines (glue.metal 564, glue.zig 257, glue_test.zig 274, inkling2_glue_metal_records.zig 23, metal_inkling2_logits.zig 239, metal_inkling2_logits_controls.zig 114, metal_inkling2_glue.zig 504, kernel_contract.zig 303, metal_inkling2_controls_host.zig 16, metal_inkling2_main.zig 61).
  • Formatting & diff cleanliness: zig fmt --check build.zig zig exit 0; git diff --check origin/main..HEAD exit 0.
  • Host & unit tests (Zig 0.17.0, -Dcpu=apple_m1 -Doptimize=ReleaseFast, Apple M5 Max applegpu_g17): zig build test-inkling2-host (47 pass, 4 skip), test-inkling2-metal-host (119 pass, 6 skip), test-inkling2-controls-host (26 pass), test-inkling2-launch-map-host (2 pass), test-kernel-contract, compile-inkling2-native-host, and test-inkling2-logits-probe (3 pass) all exited 0 (100/100 steps, 488/498 pass, 10 pre-existing skips).
  • Kernel contract suite (./zig-out/bin/tf-kernel-contract):
    • Apple M5 Max (applegpu_g17, tensor units true) --only "metal inkling2": SUMMARY 72 kernels: 72 pass gates 1-4, 0 fail, 0 known findings; 88 controls: 88 red as planted, 0 missed; 0 limit problems; gates 15376 ms, limits 660 ms (including tf_inkling2_logits_coalesced worst error 0.000 across 17 shapes and 0 limit problems for r4/r8/r16/r32).
    • Apple M3 Ultra (applegpu_g15, tensor units false): --only "metal inkling2 logits" (1 kernels: 1 pass gates 1-4, 0 fail; 3 controls: 3 red as planted, 0 missed; 0 limit problems) and --only "metal inkling2 glue" (10 kernels: 10 pass gates 1-4, 0 fail; 13 controls: 13 red as planted, 0 missed; 0 limit problems).
  • Thermals (pmset -g therm pre/post every served A-B-B-A leg across all three Macs): nominal throughout (no CPU/thermal/performance warning recorded).

Receipt

  • Main against branch: A-B-B-A served runs (origin/main 8f11b41ba0758821ebfd224fcccf664dcbddd5fc vs da78c251c79e2118aa10dfe44e97644f61f360fe, bench_concurrent.py --alone --serial --levels 1,4 --temperatures 0,1 --tokens 128 --reps 2 --seed 42; 0 cross-leg token_sha256 mismatches across all 48 cells across all 3 Macs):
    • Apple M5 Ultra 512 GiB:
      • Plain (--no-drafts): code T=0 c=1 34.10 -> 34.35 (+0.73%), c=4 54.70 -> 57.15 (+4.48%); chat T=0 c=1 34.45 -> 34.35 (-0.29%), c=4 55.40 -> 58.15 (+4.96%); code T=1 c=1 34.05 -> 34.25 (+0.59%), c=4 55.45 -> 58.00 (+4.60%); chat T=1 c=1 34.40 -> 34.10 (-0.87%), c=4 56.40 -> 59.00 (+4.61%).
      • DSpark (--drafter {TARGET}): code T=0 c=1 54.30 -> 55.70 (+2.58%), c=4 110.75 -> 127.75 (+15.35%); chat T=0 c=1 37.55 -> 38.55 (+2.66%), c=4 78.65 -> 91.60 (+16.47%); code T=1 c=1 30.70 -> 30.80 (+0.33%), c=4 53.20 -> 55.65 (+4.61%); chat T=1 c=1 30.35 -> 36.65 (+20.76%, tpc 1.058 -> 2.169), c=4 76.95 -> 80.40 (+4.48%).
    • Apple M5 Pro 64 GiB:
      • Plain (--no-drafts): code T=0 c=1 25.85 -> 26.90 (+4.06%), c=4 48.75 -> 49.55 (+1.64%); chat T=0 c=1 26.00 -> 27.05 (+4.04%), c=4 51.10 -> 52.05 (+1.86%); code T=1 c=1 25.50 -> 26.90 (+5.49%), c=4 39.25 -> 39.80 (+1.40%); chat T=1 c=1 25.75 -> 27.00 (+4.85%), c=4 43.05 -> 43.50 (+1.05%).
      • DSpark (--drafter {TARGET}): code T=0 c=1 34.60 -> 33.85 (-2.17%), c=4 48.20 -> 49.45 (+2.59%); chat T=0 c=1 24.55 -> 25.05 (+2.04%), c=4 51.75 -> 52.90 (+2.22%); code T=1 c=1 21.45 -> 22.20 (+3.50%), c=4 37.85 -> 38.40 (+1.45%); chat T=1 c=1 22.25 -> 22.40 (+0.67%), c=4 43.80 -> 45.60 (+4.11%).
    • Apple M3 Ultra 512 GiB:
      • Plain (--no-drafts): code T=0 c=1 26.00 -> 26.40 (+1.54%), c=4 41.35 -> 42.95 (+3.87%); chat T=0 c=1 26.35 -> 26.25 (-0.38%), c=4 42.05 -> 43.75 (+4.04%); code T=1 c=1 26.20 -> 26.20 (0.00%), c=4 41.85 -> 43.65 (+4.30%); chat T=1 c=1 26.15 -> 26.25 (+0.38%), c=4 42.80 -> 44.55 (+4.09%).
      • DSpark (--drafter {TARGET}): code T=0 c=1 39.90 -> 42.25 (+5.89%), c=4 97.85 -> 105.65 (+7.97%); chat T=0 c=1 28.30 -> 29.15 (+3.00%), c=4 64.15 -> 74.50 (+16.13%); code T=1 c=1 24.10 -> 24.10 (0.00%), c=4 42.65 -> 47.10 (+10.43%); chat T=1 c=1 23.25 -> 27.75 (+19.35%, tpc 1.049 -> 2.133), c=4 57.80 -> 62.00 (+7.27%).
  • Boxes it reaches: zig/kernels/metal/inkling2/glue.metal, zig/src/families/inkling2/metal/glue.zig, glue_test.zig, zig/src/core/contract/inkling2_glue_metal_records.zig, zig/build/kernel_contract.zig, zig/tests/contract/metal_inkling2_{logits,logits_controls,glue,controls_host,main}.zig.
  • New switch? No.
  • Environment: Commit 2e70cabde8bfbfebcef1e1c29d0cccc9b07e796e on origin/main a44ffee88ae0a8dfbe037f345513875d32d53757 (3-Mac A-B-B-A served runs measured on pre-rebase tree da78c251c79e2118aa10dfe44e97644f61f360fe on 8f11b41ba0 prior to #5001 merge), Zig 0.17.0 -Dcpu=apple_m1 -Doptimize=ReleaseFast, Apple M5 Max (applegpu_g17), Apple M5 Ultra 512 GiB, Apple M5 Pro 64 GiB, Apple M3 Ultra 512 GiB; checkpoint Inkling-2-Mini.
  • Paths and platforms the change reaches: Metal Inkling 2 Mini unembedding logits (glue.logits); verified on Apple M5 and Apple M3 families; non-Apple GPU backends not modified.
  • Exactness: 0-ULP bit-for-bit parity vs tf_inkling2_logits (worst = 0.000 across 17 contract shapes; 0 cross-leg token_sha256 mismatches across all 48 A-B-B-A served cells across 3 Macs).
  • Decode speed before and after: Up to +4.96% plain (c=4) and +16.47% / +20.76% DSpark on M5 Ultra; +5.49% plain / +4.11% DSpark on M5 Pro; +4.30% plain / +19.35% DSpark on M3 Ultra.
  • Prompt speed before and after: Unchanged (1 logits row at prompt tail).
  • Licence of any new weights or drafter: n/a.

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.

@gilby
gilby force-pushed the v5-1-logits-coalesced branch from da78c25 to 2e70cab Compare November 26, 2026 00:43
@ashhart

ashhart commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Thank you, @gilby.

The tiled variants preserve the baseline's (acc + w0*h0) + w1*h1 order and BF16 conversion before the cap. The shared-memory tails and barriers are consistent, and all four row variants are wired through the ordinary logits dispatcher. The raw-byte random-input contract and planted arithmetic controls are the right equivalence checks.

The remaining qualification is current-main-versus-head serving A/B on one M5 and one M3 Ultra with the pinned original Inkling 2 Mini checkpoint and its DSpark weights. The supplied serving receipt used an older base/head. The nonfinite parity gate also needs rows 1, 9 and 17 to cover r4, r16 and r32 alongside the existing r8 case.

The branch merges cleanly on today's main. SAFE TO A/B once those exact cells are recorded.

  • Synthena

@gilby
gilby force-pushed the v5-1-logits-coalesced branch from 2e70cab to 6b429e2 Compare November 26, 2026 06:27
@ashhart

ashhart commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Thank you, @gilby, and thanks for adding the nonfinite cases across all four row variants. Inkling 2 Mini A/Bs against main, plain decode at 1 and 4 streams on chat and code, greedy and sampled:

  • M5: tokens equal in every row; 4 streams +4.3% to +5.3%, 1 stream level.
  • M3 Ultra: tokens equal in every row; 4 streams +3.4% to +3.7%, 1 stream level.

The shader and dispatch in your latest push are byte-identical to what those runs measured (only the contract registration changed), so this goes into a landing train now.

  • Synthena

@gilby

gilby commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Updated #5135 (6b429e2d81, rebased onto current origin/main 67ae36c4dc) with extended nonfinite parity coverage and completed current-main vs head contract & serving verification across Apple M5 Max, Apple M5 Ultra, and Apple M3 Ultra:

1. Nonfinite Parity Update & Kernel Contracts (0 fail, all controls red, 0 limits)

  • Updated .nonfinite in zig/tests/contract/metal_inkling2_logits.zig to test raw-byte equality against logitsBaseline for rows = 1 (r4), rows = 5 (r8), rows = 9 (r16), and rows = 17 (r32), ensuring partial simdgroups and tail rows exercise both finite and NaN/+inf/-inf/-0.0 unembed lanes without row-3 overwrites.
  • Apple M5 Max (applegpu_g17):
    • tf-kernel-contract --only 'metal inkling2 logits': 1/1 kernels pass gates 1-4 (0 fail), 3/3 controls red as planted, 0 limit problems.
    • tf-kernel-contract --only 'metal inkling2 glue': 14/14 kernels pass gates 1-4 (0 fail), 17/17 controls red as planted, 0 limit problems.
  • Apple M3 Ultra (applegpu_g15):
    • tf-kernel-contract --only 'metal inkling2 logits': 1/1 kernels pass gates 1-4 (0 fail), 3/3 controls red as planted, 0 limit problems.
    • tf-kernel-contract --only 'metal inkling2 glue': 14/14 kernels pass gates 1-4 (0 fail), 17/17 controls red as planted, 0 limit problems.

2. Current-main (67ae36c4dc) vs Head (6b429e2d81) Serving A-B-B-A (Inkling-2-Mini-ed40e7da, 0 Mismatches)

All 4 suites (plain --no-drafts and dspark --drafter {TARGET}, c=1,4, T=0.0,1.0, seed=42) had 0 cross mismatches, 0 failed requests, 0 alone/serial inequalities, and 32/32 nominal thermal logs:

Hardware Mode code T=0 c=1 code T=0 c=4 chat T=0 c=1 chat T=0 c=4 code T=1 c=1 code T=1 c=4 chat T=1 c=1 chat T=1 c=4 Mismatches
Apple M5 Ultra 512 GiB plain 35.65->35.70 (+0.14%) 55.50->57.80 (+4.14%) 36.00->35.40 (-1.67%) 56.40->59.05 (+4.70%) 35.85->35.65 (-0.56%) 56.05->58.85 (+5.00%) 35.95->35.50 (-1.25%) 57.05->59.55 (+4.38%) 0
Apple M5 Ultra 512 GiB dspark 55.40->55.40 (+0.00%) 119.70->124.60 (+4.09%) 37.70->39.05 (+3.58%) 79.75->86.10 (+7.96%) 32.05->32.05 (+0.00%) 54.25->56.30 (+3.78%) 31.60->32.70 (+3.48%) 75.45->81.15 (+7.55%) 0
Apple M3 Ultra 512 GiB plain 27.30->27.55 (+0.92%) 41.60->43.60 (+4.81%) 27.35->27.25 (-0.37%) 42.50->44.50 (+4.71%) 27.35->27.35 (+0.00%) 42.40->44.40 (+4.72%) 27.25->27.20 (-0.18%) 43.20->45.10 (+4.40%) 0
Apple M3 Ultra 512 GiB dspark 40.90->43.05 (+5.26%) 98.75->106.40 (+7.75%) 28.25->29.60 (+4.78%) 64.10->75.35 (+17.55%) 24.70->25.00 (+1.21%) 44.80->47.35 (+5.69%) 24.05->28.25 (+17.46%) 58.20->62.30 (+7.04%) 0

@ashhart
ashhart force-pushed the v5-1-logits-coalesced branch from 6b429e2 to 709fbd3 Compare November 26, 2026 09:56
@ashhart
ashhart merged commit a379424 into ashhart:main Nov 26, 2026
2 of 12 checks passed
@ashhart

ashhart commented Nov 26, 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.

2 participants