Skip to content

inkling2 metal: fuse exact Stage-1/Stage-2 argmax on both logits paths and enable gpu_tokens - #4592

Open
gilby wants to merge 42 commits into
ashhart:mainfrom
gilby:inkling2-lead-logits-batched
Open

gilby wants to merge 42 commits into
ashhart:mainfrom
gilby:inkling2-lead-logits-batched

Conversation

@gilby

@gilby gilby commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Fuses exact two-stage argmax across both Inkling 2 Mini logits paths to skip unread f32 logits stores and enables gpu_tokens async GPU-resident decode on #4618 while preserving bit-exact greedy token parity.

What this changes

Preserves #4618's exact dot-product, bfloat16 rounding, and softcap30 summation order in both tf_inkling2_logits (glue.metal) and tf_ink2_logits_units (experts_units.metal) while adding LogitMode (.logits, .argmax, .both), Stage-1 per-tile argmax reduction, and Stage-2 tf_inkling2_reduce_argmax matching exact firstGreatest tie-breaking and NaN-at-0 semantics while skipping unread f32 logits DRAM stores when mode == .argmax.

Enables gpu_tokens = true (runtime_test.zig, serving_test.zig, backend.zig, forward.zig, state.zig) with async GPU-resident decode queueFn, shared token/flight metadata rings, mixed/pure verify batch planning, updated launch records (inkling2_glue_metal_records.zig), and logits.settleFlight copying any cb.failure() text before releasing command buffers (backend.Metal.land + clobbering mock test in logits.zig), plus contract argmax probes & planted tie-order controls for both glue and tensor-unit logits kernels.

Checks on this head

  • Head commit: 0b44597c64 (copies cb.failure() text into caller buffer in settleFlight before releasing command buffers)
  • Lean checker, python3 tools/zig/lean_check.py: 167 problems (0 new vs base ad3d31f9ff); tools/pr_check.py --pr 4592 --base ad3d31f9ff: 0 error(s), 1 warning(s) (changelog reminder).
  • Tests run:
    • Host CPU & cross-compile checks on 0b44597c64: zig fmt --check on touched files exit 0; git diff --check exit 0; test-inkling2-controls-host, test-inkling2-host, test-inkling2-metal-host (78 pass, 5 skip), compile-inkling2-native-host (macOS/Linux/Windows) pass; test-metal-launch-map-host (Metal launch map: 1210 registered launches, 2 controls, 0 missing, 8/8 pass); test-inkling2-metal-host compiles cleanly (0 errors) for x86_64-linux and x86_64-windows.
    • Real-checkpoint test-inkling2-metal-host (at GPU-identical parent f15269e427, TF_INKLING2_METAL_DIR set to Inkling-2-Mini-ed40e7da, ReleaseFast -Dcpu=apple_m1): 81 passed; 3 skipped; 0 failed across Apple M5 Ultra, Apple M5 Pro, and Apple M3 Ultra (3 skips are DSpark-gated tests when TF_INKLING2_DSPARK_DIR is unset).
  • CI: Local host, M5 Max contract (48/48 + 48 red), and full M5 Ultra, M5 Pro, and M3 Ultra contract/real-ckpt/serving/cache runs all passed (only logits.zig/backend.zig host error handling changed after f15269e427).
  • Not run, and why: Non-Apple backends (CUDA/CPU-only targets outside macOS Metal); all 4 Apple Silicon hardware tiers (M5 Max, M5 Ultra, M5 Pro, M3 Ultra) ran on real hardware.

Receipt

  • Base against branch (Apple M5 Ultra / M5 Pro / M3 Ultra, Inkling-2-Mini-ed40e7da, plain --no-drafts vs #4618 ad3d31f9ff, measured at GPU-identical f15269e427):
    • Apple M5 Ultra (bench_concurrent.py --levels 1,4,16 --temperatures 0,1.0 --tokens 128 --reps 2 --alone):
      • chat T=0.0: c=1 59.0 vs 57.6 (+2.4%), c=4 193.4 (st=207.8) vs 196.8 (st=203.1), c=16 431.9 (st=557.7) vs 452.7 (st=552.8) tok/s (sha=32ddb869bec4 exact match, alone_unequal=0)
      • code T=0.0: c=1 59.5 vs 53.6 (+11.0%), c=4 200.9 (st=214.8) vs 199.1 (st=209.0), c=16 475.0 (st=577.8) vs 491.2 (st=565.1) tok/s (sha=4490e6bbcfbd exact match, alone_unequal=0)
      • chat T=1.0: c=1 35.5 vs 34.7, c=4 59.6 (st=60.5) vs 59.4, c=16 71.2 (st=73.3) vs 71.9 tok/s (exact sha set match)
      • code T=1.0: c=1 35.3 vs 34.4, c=4 57.8 (st=59.4) vs 57.8, c=16 68.9 (st=71.5) vs 69.4 tok/s (exact sha set match)
      • bench_cache.py --tokens 64 --repeats 2 --streams 4 --require-cold (pt=8704): qualified=True, cold ttft_s=8.7671s (rep_min=8.7099s) vs 8.7536s ref, dec=49.6 (rep_max=49.7) vs 48.5 tok/s (sha=95e612cfdcaf exact match)
    • Apple M5 Pro 20-core (bench_concurrent.py --levels 1,4 --temperatures 0,1.0 --tokens 128 --reps 2 --alone):
      • chat T=0.0: c=1 31.9 vs 31.5 (+1.3%), c=4 101.5 (st=115.9) vs 102.6 tok/s (sha=32ddb869bec4 exact match, alone_unequal=0)
      • code T=0.0: c=1 31.7 vs 31.8, c=4 110.4 (st=120.3) vs 110.7 tok/s (sha=4490e6bbcfbd exact match, alone_unequal=0)
      • chat T=1.0: c=1 23.2 vs 23.1, c=4 38.8 (st=40.7) vs 40.2; code T=1.0: c=1 23.5 vs 22.5, c=4 38.2 (st=39.5) vs 39.1 (exact sha set match)
      • bench_cache.py --tokens 32 --repeats 2 --streams 2 --require-cold (pt=2571): qualified=True, cold ttft_s=15.6689s (rep_min=13.4539s) vs 12.6426s ref, dec=28.4 (rep_max=28.8) vs 29.6 tok/s (sha=1fbe89b22692 exact match)
    • Apple M3 Ultra (Metal 3, bench_concurrent.py --levels 1,4 --temperatures 0,1.0 --tokens 128 --reps 2 --alone):
      • chat T=0.0: c=1 45.9 vs 45.3 (+1.3%), c=4 100.7 (st=109.6) vs 100.9 tok/s (sha=34819971792d exact match, alone_unequal=0)
      • code T=0.0: c=1 46.6 vs 45.4 (+2.6%), c=4 105.1 (st=111.5) vs 106.7 tok/s (sha=4490e6bbcfbd exact match, alone_unequal=0)
      • chat T=1.0: c=1 28.3 vs 28.4, c=4 42.3 (st=43.7) vs 43.5; code T=1.0: c=1 28.1 vs 28.3, c=4 42.6 (st=43.8) vs 43.9 (exact sha set match)
      • bench_cache.py --tokens 32 --repeats 2 --streams 2 --require-cold (pt=2571): qualified=True, cold ttft_s=8.6175s (rep_min=8.5912s) vs 8.7351s ref (-0.195s), dec=38.0 (rep_max=37.9) vs 37.6 tok/s (sha=1fbe89b22692 exact match)
  • Boxes it reaches: Apple Silicon Metal (M5 Max, M5 Ultra, M5 Pro, and M3 Ultra all verified).
  • New switch? None (Mode == .argmax skips unread logits DRAM stores and gpu_tokens = true runs by default).
  • Environment: Commits f15269e427 (GPU benchmarks/contracts), 4906fe2b5d, and 0b44597c64 on ad3d31f9ff, Zig 0.17.0 -Dcpu=apple_m1 -Doptimize=ReleaseFast, Apple M5 Max (applegpu_g17), Apple M5 Ultra (applegpu_g17), Apple M5 Pro (applegpu_g17), Apple M3 Ultra (applegpu_g15), checkpoint Inkling-2-Mini-ed40e7da.
  • Paths and platforms the change reaches: Inkling 2 Mini Metal logits Stage-1/Stage-2 argmax (glue.metal, experts_units.metal) and gpu_tokens decode submission across all 4 Apple Silicon hardware tiers.
  • Exactness: alone_unequal=0; 100% T=0.0, T=1.0, and cache token_sha parity against #4618 across M5 Ultra, M5 Pro, and M3 Ultra.
  • 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 replacements:
    • Updated tf_inkling2_logits (glue.metal) and tf_ink2_logits_units (experts_units.metal) with Stage-1 per-tile argmax reduction and .logits / .argmax / .both output mode.
    • Added Stage-2 tf_inkling2_reduce_argmax (256 threads, 64 B TG) matching exact firstGreatest tie-breaking and NaN-at-0 semantics.
  • Planted controls and status: All 5 logits/argmax controls (logits, argmax, logits_units cap, logits_units argmax, dspark logits) fail red as planted on M5 Max, M5 Ultra, M5 Pro, and M3 Ultra.
  • Backend SUMMARY lines and exit status:
    • M5 Max (applegpu_g17): SUMMARY 48 kernels: 48 pass gates 1-4, 0 fail, 0 known findings; 48 controls: 48 red as planted, 0 missed; 0 limit problems; gates 6567 ms, limits 69 ms (exit 0).
    • M5 Ultra (applegpu_g17): SUMMARY 48 kernels: 48 pass gates 1-4, 0 fail, 0 known findings; 48 controls: 48 red as planted, 0 missed; 0 limit problems; gates 5857 ms, limits 384 ms (exit 0).
    • M5 Pro (applegpu_g17): SUMMARY 48 kernels: 48 pass gates 1-4, 0 fail, 0 known findings; 48 controls: 48 red as planted, 0 missed; 0 limit problems; gates 6151 ms, limits 8 ms (exit 0).
    • M3 Ultra (applegpu_g15): SUMMARY 42 kernels: 42 pass gates 1-4, 0 fail, 0 known findings; 42 controls: 42 red as planted, 0 missed; 0 limit problems; gates 5883 ms, limits 183 ms (exit 0).
  • Chips not yet run: None (all 4 Apple Silicon hardware tiers completed).

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. If the bits change, the description says which sums change and why.
  • 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.

ashhart and others added 30 commits November 21, 2026 16:05
Derive per-visit cache access and exact arithmetic for the 48 visits so CUDA admission and kernels have a reference before serving is enabled.
Decode stored NVFP4 and MXFP8 expert weights to BF16 and launch them beside the grouped expert/vision kernels, with exact tensor baselines and planted faults.
Launch cross-pass messages, rank-64 gates, residual steps and split QKV with FP32 reductions, BF16 rounding controls and per-launch resource records.
Run all 48 visits with per-visit cache state, prompt snapshots and exact rollback. Refuse TP, vision and DSpark loading until those engines exist.
…s and enable gpu_tokens

Preserve ashhart#4618's exact dot-product, BF16 rounding, and softcap30 summation
order in both tf_inkling2_logits (glue.metal) and tf_ink2_logits_units
(experts_units.metal). Add LogitMode (.logits, .argmax, .both), Stage-1
per-tile reduction, and Stage-2 tf_inkling2_reduce_argmax matching exact
firstGreatest tie-breaking and NaN-at-0 semantics while skipping unread f32
logits DRAM stores when mode == .argmax.

Enable gpu_tokens = true with async GPU-resident decode queueFn, shared
token/flight metadata rings, mixed/pure verify batch planning, updated
launch records, host unit tests, and contract argmax probes + planted
tie-order controls for both glue and tensor-unit logits kernels.

Refs ashhart#4652
@gilby
gilby force-pushed the inkling2-lead-logits-batched branch from ef7204c to f15269e Compare November 23, 2026 06:20
@gilby gilby changed the title inkling2 metal: coalesce and batch tf_inkling2_logits across 8x8 tiles inkling2 metal: fuse exact Stage-1/Stage-2 argmax on both logits paths and enable gpu_tokens Nov 23, 2026
@gilby
gilby marked this pull request as ready for review November 23, 2026 06:20
@ashhart

ashhart commented Nov 23, 2026

Copy link
Copy Markdown
Owner

Thanks, @gilby, for making the scalar and tensor-unit argmax rules and barriers explicit.

One flight-error path needs fixing. GPU-token decode now takes the early flight return, which skips the earlier command buffers’ failure checks, while landing checks only the final buffer. If an earlier part fails and the final part succeeds, the earlier error is dropped and the flight is marked landed.

Please retain and check those earlier buffers on landing before advancing the landed position.

  • Synthena

@gilby

gilby commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Fixed in 4906fe2b5d on top of f15269e427:

  • Replaced Options.flight: ?*mtl.CommandBuffer with fwd.Flight (cbs: [cuts.len + 1]mtl.CommandBuffer, len: usize), transferring ownership of all committed parts[0..n_parts] plus the final cb.retain() (n_parts = 0 so fwd.runShared's defer does not release transferred parts before landing).
  • Updated Metal.Flight, Metal.deinit, Metal.queueFn, and Metal.land (via logits_.settleFlight) to wait, check failure() on every command buffer in order (0..len) before advancing self.landed, and release every part on both error and success.
  • Added a cross-platform 5-slot mock flight test in zig/src/families/inkling2/metal/logits.zig covering earlier-CB failure (with final CB succeeding), final-CB failure, and all-OK, verified with macOS host tests (78 pass, 5 skip on test-inkling2-metal-host), Linux/Windows compile-inkling2-native-host & test-inkling2-metal-host cross-compilation (0 errors), and tools/pr_check.py.

@ashhart

ashhart commented Nov 23, 2026

Copy link
Copy Markdown
Owner

@gilby Thanks. Every flight buffer is now retained and checked before landed moves. One problem is left:

  • zig/src/families/inkling2/metal/logits.zig:12: settleFlight returns the text from cb.failure(), then its defer releases the command buffers before Metal.land logs that text at backend.zig:204. Outside Metal 4 the text belongs to the buffer's NSError, so the log can read freed memory while handling a GPU failure. The previous code logged before releasing. Please log or copy the message before the release.

  • Synthena

@gilby

gilby commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Fixed in 0b44597c64:

  • zig/src/families/inkling2/metal/logits.zig (settleFlight): now takes err_buf: []u8 and copies std.mem.span(cb.failure().?) into err_buf before defer for (cbs) |cb| cb.release(); runs, returning ?[]const u8 backed by the caller's stack buffer.
  • zig/src/families/inkling2/metal/backend.zig (Metal.land): allocates var err_buf: [512]u8 = undefined; and logs the copied slice before returning error.GpuFailed.
  • Updated the settleFlight mock unit test in logits.zig so Mock.Ref.release() overwrites the backing error buffer with 'X's and clears has_msg, verifying both full-copy recovery after release and clamped copy on a smaller buffer ([7]u8).

Verified on 0b44597c64: zig fmt --check, git diff --check, test-inkling2-host, test-inkling2-metal-host (78 pass, 5 skip), test-inkling2-controls-host, test-metal-launch-map-host (8/8), compile-inkling2-native-host (macOS/Linux/Windows), test-inkling2-metal-host cross-compilation (x86_64-linux / x86_64-windows, 0 compile errors), and python3 tools/pr_check.py --pr 4592 --base ad3d31f9ff (0 new lean findings).

@ashhart
ashhart force-pushed the lead/inkling2-mini branch 3 times, most recently from aefb063 to c41911e Compare November 24, 2026 09:43
@ashhart
ashhart deleted the branch ashhart:lead/inkling2-mini November 24, 2026 09:43
@ashhart ashhart closed this Nov 24, 2026
@ashhart ashhart reopened this Nov 24, 2026
@ashhart
ashhart changed the base branch from lead/inkling2-mini to main November 24, 2026 09:46
@ashhart

ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Sorry for the noise: this was closed automatically when #4387 landed on main and its branch was removed. It is reopened and now targets main. We will rebase it onto main for you; nothing is needed from you.

  • Synthena

@ashhart

ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Thanks for your patience, @gilby. Since #4387 landed, this PR's own commits sit on top of an earlier Inkling Metal stack that has not landed on main, and that stack includes a change to the FP32 summation order we have kept out for exactness. Your commits do not apply to main's kernels without it, so we are holding this PR until that stack is decided, rather than carrying unreviewed work along with it. We will follow up here; nothing is needed from you now.

  • Synthena

This branch has not been deployed

No deployments
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