Skip to content

inkling2 metal: cache depth-block inv_rms and fuse partial updates with AR reductions - #5001

Open
gilby wants to merge 2 commits into
ashhart:mainfrom
gilby:v4-16-fused-partial-ar-rebased
Open

gilby wants to merge 2 commits into
ashhart:mainfrom
gilby:v4-16-fused-partial-ar-rebased

Conversation

@gilby

@gilby gilby commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Before: on main, each layer's tf_inkling2_ar recomputes normalized RMS norms (sumsq -> rsqrt) across all previously finalized depth blocks (0..sources-2) twice per layer (before attention and before MLP), and attention/MLP partial-update residual adds (tf_inkling2_partial_add / tf_inkling2_mlp_add) run as separate full-buffer dispatches before the next AR reduction reads those same tensors back from device memory.

After: caches each finalized depth block's scalar inv_rms once via tf_inkling2_block_inv_rms and reads cached scalars in tf_inkling2_cached_ar, then fuses the post-projection partial residual update directly into the subsequent AR pass (tf_inkling2_attn_partial_ar and tf_inkling2_mlp_partial_ar, plus cached query support when layers == 0 / final norm consumes block outputs). Both optimizations preserve 0-ULP bitwise BF16/FP32 equality against main's reduction order.

Rebased onto origin/main (f3c0fa8c65, including #5000 and #5058):

  • Preserves #5000's rel_tab removal alongside block_inv_rms in state.zig, with all 15 glue pipelines and thread bounds aligned in glue.zig.

  • Switches all probe/kernel registrations in metal_inkling2_glue.zig to contract.register with matching errdefers (#5058) and consolidates the four cached/fused AR contract probes in metal_inkling2_glue_fused.zig so every touched file remains <= 600 lines.

  • Issue: Refs Inkling 2 Mini Metal V4: exact-serial scaling, GPU sampling, DFlash 2 acceleration, and kernel fusions #4961

  • Stacked on: main (f3c0fa8c65, git merge-tree --write-tree clean 1780e2ae71)

Checks on this head

  • Head commit: 793caf4450 (2-commit stack: 66e14b8f65 + 793caf4450)
  • Lean checker, python3 tools/pr_check.py --base origin/main --pr 5001: lean 164 vs 164, 0 new, 0 error(s) (11 changed files, max touched file 546 lines <= 600); zig fmt --check build.zig zig and git diff --check origin/main..HEAD exit 0.
  • Builds & tests (-Dcpu=apple_m1 -Doptimize=ReleaseFast):
    • zig build -Dcpu=apple_m1 -Doptimize=ReleaseFast: exit 0.
    • zig build compile-inkling2-native-host: exit 0.
    • zig build compile-inkling2-native-host -Dtarget=x86_64-windows: exit 0.
    • zig build compile-native-cuda-windows: exit 0.
    • zig build test-inkling2-host -Dcpu=apple_m1 -Doptimize=ReleaseFast: 46 pass, 4 skip (50 total) (optional checkpoint/tokenizer env tests skipped).
    • zig build test-inkling2-controls-host: 24 pass (24 total).
    • zig build test-kernel-contract: 45/45 steps succeeded (259/259 non-cached tests passed, 0 failed).
    • zig build tf-kernel-contract -Doptimize=ReleaseFast: exit 0.
    • zig build test-inkling2-metal-host -Dcpu=apple_m1 -Doptimize=ReleaseFast (--seed=0xf1163109): 114 pass, 6 skip (120 total) (0 failed; 2 JPEG tests skipped pending newer libjpeg-turbo).
  • Contracts (./zig-out/bin/tf-kernel-contract):
    • Apple M5 Max (793caf4450, --only "metal inkling2"): SUMMARY 66 kernels: 66 pass gates 1-4, 0 fail, 0 known findings; 72 controls: 72 red as planted, 0 missed; 0 limit problems; gates 13500 ms, limits 153 ms (exit 0).
    • Apple M5 Max (793caf4450, --only "metal inkling2 glue"): SUMMARY 14 kernels: 14 pass gates 1-4, 0 fail, 0 known findings; 17 controls: 17 red as planted, 0 missed; 0 limit problems; gates 488 ms, limits 56 ms (exit 0).
    • Apple M5 Ultra 512 GiB (pre-rebase 3f1f52a858; kernel math unchanged): SUMMARY 52 kernels: 52 pass gates 1-4, 0 fail, 0 known findings; 59 controls: 59 red as planted, 0 missed; 0 limit problems; gates 8527 ms, limits 386 ms (exit 0).
  • CI: Pending GitHub Actions on 793caf4450.
  • Not run, and why: Apple M1/M2/M3/M4 served A/Bs not run for this slice; Apple M5 Ultra and Apple M5 Pro A-B-B-A runs cover plain and self-drafted decode across c=1,4.

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, T=0.0):
      • Plain (--no-drafts --parallel 4):
        • c=1: code: 13.45 -> 13.60 tok/s (+1.12%, TTFT 0.520 -> 0.507s, -2.41%); chat: 13.60 -> 13.70 tok/s (+0.74%, TTFT 1.272 -> 1.244s, -2.20%).
        • c=4: code: 21.50 -> 22.10 tok/s (+2.79%, TTFT 1.256 -> 1.215s, -3.27%); chat: 22.90 -> 23.65 tok/s (+3.28%, TTFT 4.982 -> 4.724s, -5.16%).
      • DSpark self-drafted (--drafter {TARGET} --parallel 4):
        • c=1: code: 13.85 -> 14.15 tok/s (+2.17%, TTFT 0.556 -> 0.534s, -3.96%); chat: 11.60 -> 11.85 tok/s (+2.16%, TTFT 1.357 -> 1.288s, -5.09%).
        • c=4: code: 10.30 -> 10.70 tok/s (+3.88%, TTFT 1.342 -> 1.308s, -2.53%); chat: 7.20 -> 7.50 tok/s (+4.17%, TTFT 5.107 -> 4.928s, -3.50%).
    • Apple M5 Ultra 512 GiB (main vs branch, T=0.0,1.0):
      • Plain (--no-drafts --parallel 4):
        • c=1: code T=0.0: 16.80 -> 17.10 tok/s (+1.79%, TTFT -8.23%); chat T=0.0: 16.85 -> 17.15 tok/s (+1.78%, TTFT -14.99%); code T=1.0: 16.85 -> 17.25 tok/s (+2.37%, TTFT -10.26%); chat T=1.0: 16.90 -> 17.30 tok/s (+2.37%, TTFT -14.95%).
        • c=4: code T=0.0: 37.30 -> 40.90 tok/s (+9.65%, TTFT 0.496 -> 0.428s, -13.70%); chat T=0.0: 38.35 -> 41.80 tok/s (+9.00%, TTFT 1.728 -> 1.376s, -20.35%); code T=1.0: 36.90 -> 40.50 tok/s (+9.76%, TTFT 0.493 -> 0.418s, -15.20%); chat T=1.0: 38.25 -> 41.65 tok/s (+8.89%, TTFT 1.728 -> 1.374s, -20.43%).
      • DSpark self-drafted (--drafter {TARGET} --parallel 4):
        • c=1: code T=0.0: 26.05 -> 27.85 tok/s (+6.91%, TTFT -6.71%); chat T=0.0: 21.30 -> 22.40 tok/s (+5.16%, TTFT -22.63%); code T=1.0: 19.50 -> 20.50 tok/s (+5.13%, TTFT -16.55%); chat T=1.0: 20.50 -> 21.70 tok/s (+5.85%, TTFT -20.57%).
        • c=4: code T=0.0: 28.05 -> 33.80 tok/s (+20.50%, TTFT 0.524 -> 0.453s, -13.63%); chat T=0.0: 19.50 -> 23.35 tok/s (+19.74%, TTFT 1.826 -> 1.466s, -19.69%); code T=1.0: 23.00 -> 27.50 tok/s (+19.57%, TTFT 0.535 -> 0.448s, -16.26%); chat T=1.0: 19.80 -> 24.00 tok/s (+21.21%, TTFT 1.804 -> 1.449s, -19.66%).
  • Boxes it reaches: glue.metal, inkling2_glue_metal_records.zig, forward.zig, glue.zig, glue_plan.zig, glue_test.zig, runtime_test.zig, state.zig, metal_inkling2_glue.zig, metal_inkling2_glue_controls.zig, metal_inkling2_glue_fused.zig.
  • New switch? No (always active).
  • Environment, with the TensorFold commit, Zig and backend toolchain, machine and GPU, checkpoint and revision: Commit 793caf4450 rebased onto f3c0fa8c65, Zig 0.17.0 -Dcpu=apple_m1 -Doptimize=ReleaseFast, Apple M5 Max (applegpu_g17), Apple M5 Pro 64 GiB, Apple M5 Ultra 512 GiB; checkpoint Inkling-2-Mini.
  • Paths and platforms the change reaches, which I ran and which I could not: Inkling 2 Metal decode and prompt glue residual reductions across Apple Silicon Metal GPUs; Apple M1/M2/M3/M4 not directly run on this slice.
  • Exactness: 0 cross-leg token_sha256 mismatches across all plain (--skip-serial) and dspark (--serial) legs on both Apple M5 Pro and Apple M5 Ultra; bitwise 0-ULP equality against uncached AR and unfused partial adds proved across metal_inkling2_glue.zig and metal_inkling2_glue_fused.zig.
  • Decode speed before and after: See A-B-B-A tables above (+0.74%..+3.28% plain / +2.16%..+4.17% self-drafted on Apple M5 Pro; +1.78%..+9.76% plain / +5.13%..+21.21% self-drafted on Apple M5 Ultra).
  • Prompt speed before and after: Eliminates redundant per-layer depth-block RMS passes and partial-add transfers during prefill chunks, cutting concurrent TTFT up to -5.16% (M5 Pro) and -22.63% (M5 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 the kernel each one replaces: Adds metal inkling2 glue cached_ar tf_inkling2_cached_ar, metal inkling2 glue block_inv_rms tf_inkling2_block_inv_rms, metal inkling2 glue attn_partial_ar tf_inkling2_attn_partial_ar, and metal inkling2 glue mlp_partial_ar tf_inkling2_mlp_partial_ar (256 threads each) to glue.metal and inkling2_glue_metal_records.zig, replacing separate recomputed tf_inkling2_ar and partial-add passes when block_inv_rms / partial inputs are active.
  • A planted control for each new kernel, and each one fails as planted: All 72/72 controls on Apple M5 Max (793caf4450) and 59/59 controls on Apple M5 Ultra (pre-rebase 3f1f52a858) fail red as planted (0 missed), including planted failures in metal_inkling2_glue_controls.zig and metal_inkling2_glue_fused.zig.
  • Each backend's SUMMARY line and exit status:
    • Apple M5 Max (793caf4450, --only "metal inkling2"): SUMMARY 66 kernels: 66 pass gates 1-4, 0 fail, 0 known findings; 72 controls: 72 red as planted, 0 missed; 0 limit problems; gates 13500 ms, limits 153 ms (exit 0).
    • Apple M5 Max (793caf4450, --only "metal inkling2 glue"): SUMMARY 14 kernels: 14 pass gates 1-4, 0 fail, 0 known findings; 17 controls: 17 red as planted, 0 missed; 0 limit problems; gates 488 ms, limits 56 ms (exit 0).
    • Apple M5 Ultra (pre-rebase 3f1f52a858): SUMMARY 52 kernels: 52 pass gates 1-4, 0 fail, 0 known findings; 59 controls: 59 red as planted, 0 missed; 0 limit problems; gates 8527 ms, limits 386 ms (exit 0).
  • Chips I could not run: Apple M1/M2/M3/M4 not directly available on this slice.

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

Independent M5 Max integration check on 2a1fb997f6 and main d1ff890729.

zig build test-inkling2-host test-inkling2-metal-host tf-kernel-contract -Dfixed-cpu -j2 --summary all
./zig-out/bin/tf-kernel-contract --only 'metal inkling2'

All three device runs reported zero launch-limit problems. I did not rerun checkpoint serving or the throughput comparisons.

Igor Lessio - AIFLOW LABS

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thanks, @gilby. The cached norms and fused updates retain the written reduction order and BF16 rounding point. The standalone host and M5 contract results support bounded A/B, with M3 coverage and remaining state-restoration cells still open. For combined work, keep the load guard aligned with all fifteen pipelines and preserve the relative-table removal in the scratch layout. The standalone PR merges cleanly; no source-order defect found.

  • Synthena

@AIFlowML

Copy link
Copy Markdown
Contributor

I reran aef19d3437 after a clean merge with 47e99323e on M5 Max. The 15 pipeline guards now line up with the pipeline list. The host targets passed 152 tests with 10 skipped. The Metal contract passed 65 kernels, caught all 69 planted controls and found zero launch-limit problems. Cached AR and both partial-update paths matched the kernels they replace bit for bit in their contract shapes.

zig build test-inkling2-host test-inkling2-metal-host tf-kernel-contract -Dfixed-cpu -j2 --summary all
./zig-out/bin/tf-kernel-contract --only 'metal inkling2'

The trial merge is also clean at 1b1235945f, whose changes since the tested base are limited to the Drama guide, receipts, checker and changelog. I still get the state.zig conflict with #5000's current head. #5004 overlaps the glue pipeline, glue plan and contract, and #5005 overlaps the glue loader and forward path. These are standalone correctness results. M3, checkpoint A/B and the combined tree remain open.

Igor Lessio - AIFLOW LABS

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thanks @gilby. All fifteen pipeline guards now match their shader bounds. The cached/fused arithmetic and BF16 boundary remain as previously reviewed, while the row-aware AR interface and current projection paths are retained.

One integration change is needed before landing: the clean merge with current main makes zig/tests/contract/metal_inkling2_glue.zig 658 lines, beyond the 600-line limit. Please split its probe/helpers while preserving all registrations, replacement-byte comparisons and controls.

The posted M5 results remain useful. M3 coverage and the remaining checkpoint restore/cancel/cache cells are still open; the older 64-row qualification does not cover every new 256-row/current-tree cell. No new reduction-order defect was found.

  • Synthena

@gilby
gilby force-pushed the v4-16-fused-partial-ar-rebased branch from 32ad007 to 793caf4 Compare November 25, 2026 16:10
@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thank you, @gilby. The split closes the file-length ask. All registrations, replacement byte comparisons and planted controls remain, and the cached/fused arithmetic and 15 launch guards are unchanged. The current head merges cleanly.

Ready for controlled comparisons. The earlier M3, larger prompt-row, restore and cancellation cells remain before landing; the supplied passing M5 checks are retained.

  • 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