Skip to content

inkling2 metal: exact two-product NVFP4 weight decode, checkpoint load validator, and GPU parity proofs - #4685

Open
gilby wants to merge 42 commits into
ashhart:mainfrom
gilby:inkling2-lead-nvfp4-two-prod
Open

gilby wants to merge 42 commits into
ashhart:mainfrom
gilby:inkling2-lead-nvfp4-two-prod

Conversation

@gilby

@gilby gilby commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Rebases the exact two-product NVFP4 weight decode, checkpoint load validator, and GPU block/quad parity proofs onto #4618 with #4618's BM=16, BN=32 ink2_units kernel untouched.

What this changes

Adds a 64-entry shifted-E2M1 LUT (ink2_e2m1_twice_lut), a branch-free product safety guard (ink2_nv_two_prod_safe) with an out-of-line 3-product fallback, ink2_nv_quad_2p inside #4618's exact BM=16, BN=32 ink2_units path (keeping tf_ink2_logits_units and MXFP8 paths untouched), a Model.load validator (Model.scanNvfp4Weights decided strictly from per-expert phi(a, d) to match shader dispatch while reporting nonfinite globals), and strict bit-identical GPU block/quad parity proofs (assertBlockProof, assertQuadProof) + .nv_swap_ab planted controls.

Checks on this head

  • Head commit: 49e5620f12 (decides fallback strictly from phi(a, d), logs nonfinite globals separately, and compares raw NaN bits on host + GPU proofs)
  • Lean checker, python3 tools/zig/lean_check.py: 167 problems (0 new vs base ad3d31f9ff); tools/pr_check.py --pr 4685 --base ad3d31f9ff: 0 error(s), 1 warning(s) (changelog reminder).
  • Tests run:
    • Host CPU & cross-compile checks on 49e5620f12: zig fmt --check exit 0; git diff --check exit 0; test-inkling2-controls-host pass; test-inkling2-host (44 pass, 4 skip) pass; test-inkling2-metal-host (CPU) 78 pass, 5 skip (strict raw @bitCast NaN checks for ±0.0 and safe phi over 256 scales × 16 nibbles, plus infinite-denominator scanNvfp4Tensors/Model.scanNvfp4Weights tests); compile-inkling2-native-host (macOS/Linux/Windows) pass; test-metal-launch-map-host 8/8 pass (1209 registered launches, 2 controls, 0 missing); test-inkling2-metal-host compiles cleanly (0 errors) for x86_64-linux and x86_64-windows.
    • M5 Max Metal kernel contract (tf-kernel-contract --only "metal inkling2" on 49e5620f12, ReleaseFast -Dcpu=apple_m1): 46/46 pass gates 1-4, 48/48 controls red, 0 limit problems (7348 ms), including strict raw u32/u16 bit equality (no NaN bypass) in assertBlockProof and assertQuadProof.
    • Real-checkpoint test-inkling2-metal-host (at shader-identical parent 8015c1ef10, TF_INKLING2_METAL_DIR set to Inkling-2-Mini-ed40e7da, ReleaseFast -Dcpu=apple_m1): 82 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 (46/46 + 48 red on 49e5620f12), and M5 Ultra, M5 Pro, and M3 Ultra contract/real-ckpt/serving/cache runs all passed (production Metal kernels unchanged after 8015c1ef10).
  • Not run, and why: Non-Apple backends; 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 shader-identical 8015c1ef10):
    • Apple M5 Ultra (bench_concurrent.py --levels 1,4,16 --temperatures 0,1.0 --tokens 128 --reps 2 --alone):
      • code T=0.0: c=1 56.8 vs 53.6 (+6.0%), c=4 199.5 (st=208.9) vs 199.1 (st=209.0), c=16 489.7 (st=564.0) vs 491.2 (st=565.1) tok/s (sha=4490e6bbcfbd exact match, alone_unequal=0)
      • chat T=0.0: c=1 56.5 vs 57.6, c=4 192.6 (st=202.5) vs 196.8, c=16 447.7 (st=542.4) vs 452.7 tok/s (sha=32ddb869bec4 exact match, alone_unequal=0)
      • code T=1.0: c=1 34.3 vs 34.4, c=4 58.1 (st=59.0) vs 57.8, c=16 70.0 (st=71.6) vs 69.4; chat T=1.0: c=1 34.5 vs 34.7, c=4 59.6 (st=60.2) vs 59.4, c=16 72.6 (st=73.6) vs 71.9 (exact sha set match)
      • bench_cache.py --tokens 64 --repeats 2 --streams 4 --require-cold (pt=8704): qualified=True, cold ttft_s=8.7323s (rep_min=8.6800s) vs 8.7536s ref, dec=47.8 (rep_max=49.3) 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):
      • code T=0.0: c=1 31.6 vs 31.8, c=4 113.3 (st=119.3) vs 110.7 (st=118.0, +2.3% agg, sha=4490e6bbcfbd exact match, alone_unequal=0)
      • chat T=0.0: c=1 32.0 vs 31.5, c=4 103.5 (st=115.2) vs 102.6 (st=114.6, sha=32ddb869bec4 exact match, alone_unequal=0)
      • code T=1.0: c=1 23.1 vs 22.5, c=4 39.1 (st=40.0) vs 39.1; chat T=1.0: c=1 23.1 vs 23.1, c=4 39.6 (st=41.0) vs 40.2 (exact sha set match)
      • bench_cache.py --tokens 32 --repeats 2 --streams 2 --require-cold (pt=2571): qualified=True, cold ttft_s=15.4241s (rep_min=13.4677s) vs 12.6426s ref, dec=27.7 (rep_max=28.1) 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):
      • code T=0.0: c=1 45.5 vs 45.4, c=4 116.3 (st=121.8, +9.0% steady) vs 106.7 (st=111.7, sha=4490e6bbcfbd exact match, alone_unequal=0)
      • chat T=0.0: c=1 46.0 vs 45.3, c=4 110.6 (st=119.3, +13.1% steady) vs 100.9 (st=105.5, sha=34819971792d exact match, alone_unequal=0)
      • code T=1.0: c=1 28.0 vs 28.3, c=4 43.8 (st=44.7) vs 43.9; chat T=1.0: c=1 28.3 vs 28.4, c=4 43.9 (st=45.0) vs 43.5 (exact sha set match)
      • bench_cache.py --tokens 32 --repeats 2 --streams 2 --require-cold (pt=2571): qualified=True, cold ttft_s=8.3596s (rep_min=8.1957s) vs 8.7351s ref (-0.375s), dec=37.8 (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 (runtime scanNvfp4Weights automatically enables 2-product fast path on exact tensors and falls back to 3-product otherwise).
  • Environment: Commits 8015c1ef10, 1652cf925c, and 49e5620f12 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 NVFP4 decode & tensor-unit expert kernels (experts.metal, experts_units.metal) and model load validator 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, plus exhaustively verified 2-product bit-identity (including raw NaN bits) and GPU block/quad proofs.
  • 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 NVFP4 decode across tf_ink2_nvfp4_gu, tf_ink2_nvfp4_down, tf_ink2_nvfp4_gu_grouped, tf_ink2_nvfp4_down_grouped, tf_ink2_nvfp4_gu_units, and tf_ink2_nvfp4_down_units (preserving #4618's BM=16, BN=32 ink2_units loop/reduction order).
  • Planted controls and status: Existing NVFP4 controls plus new .nv_swap_ab swapped 2-prod guard controls (tf_ink2_nvfp4_down, tf_ink2_nvfp4_down_units) fail red as planted on all 4 Macs; assertBlockProof and assertQuadProof pass on GPU with raw u32/u16 bit equality (including NaNs).
  • Backend SUMMARY lines and exit status:
    • M5 Max (applegpu_g17, 49e5620f12): SUMMARY 46 kernels: 46 pass gates 1-4, 0 fail, 0 known findings; 48 controls: 48 red as planted, 0 missed; 0 limit problems; gates 7348 ms, limits 14 ms (exit 0).
    • M5 Ultra (applegpu_g17, 8015c1ef10): SUMMARY 46 kernels: 46 pass gates 1-4, 0 fail, 0 known findings; 48 controls: 48 red as planted, 0 missed; 0 limit problems; gates 5922 ms, limits 386 ms (exit 0).
    • M5 Pro (applegpu_g17, 8015c1ef10): SUMMARY 46 kernels: 46 pass gates 1-4, 0 fail, 0 known findings; 48 controls: 48 red as planted, 0 missed; 0 limit problems; gates 6015 ms, limits 7 ms (exit 0).
    • M3 Ultra (applegpu_g15, 8015c1ef10): SUMMARY 41 kernels: 41 pass gates 1-4, 0 fail, 0 known findings; 42 controls: 42 red as planted, 0 missed; 0 limit problems; gates 6121 ms, limits 431 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.
@gilby
gilby force-pushed the inkling2-lead-nvfp4-two-prod branch from bb4b417 to 8015c1e Compare November 23, 2026 06:07
@gilby gilby changed the title Inkling 2 Mini on Metal: exact two-product NVFP4 weight decode, checkpoint load validator, and GPU parity proofs inkling2 metal: exact two-product NVFP4 weight decode, checkpoint load validator, and GPU parity proofs Nov 23, 2026
@ashhart

ashhart commented Nov 23, 2026

Copy link
Copy Markdown
Owner

Thanks @gilby. The scalar and tensor-unit parity coverage is valuable.

I found one checkpoint-validation integration issue. The recorded two-product fallback result is not consumed by dispatch or included in the load-time warning condition. A checkpoint containing only a quotient/scale tie is therefore silent even though the kernels take the fallback. Conversely, a NaN scale with otherwise safe globals logs that the three-product fallback is active while the shader still selects two-product decoding.

Please make the checkpoint reporting agree with the actual per-expert dispatch, or pass the intended policy through explicitly.

  • Synthena

@gilby

gilby commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Fixed in 1652cf925c on top of 8015c1ef10:

  • Aligned scanNvfp4Tensors and Model.load 1-for-1 with the shader's ink2_nv_two_prod_safe(phi) dispatch predicate: any in-range phi with !nvfp4TwoProdSafe(phi) now unconditionally marks two_prod_exact = false, tie_fallback_experts += 1, and exact_fallback_experts += 1 (removing the extra scale-mantissa filter so tie-only checkpoints are never silent), and Model.load logs the 3-product fallback warning iff m.nvfp4_scan.usesThreeProdFallback().
  • Kept nonfinite E4M3 scale reporting (!m.nvfp4_scan.finite_scales, nan_scales) separate so a NaN scale with safe phi logs a nonfinite-scale warning without falsely claiming 3-product fallback is active (the 2-product path stays active and bit-identical on NaN E4M3 scales).
  • Updated experts_test.zig and runtime_test.zig to verify clean, tie-only (regardless of scales), NaN-scale-only, and out-of-range/nonfinite phi cases, and moved if (!builtin.os.tag.isDarwin()) return error.SkipZigTest; to the top of the runtime_test.zig scan test so Linux/Windows CI builds cleanly (78 pass, 5 skip on macOS test-inkling2-metal-host, 0 cross-compile errors on Linux/Windows).

@ashhart

ashhart commented Nov 23, 2026

Copy link
Copy Markdown
Owner

@gilby Thanks. Tie-only checkpoints now warn, and a NaN scale with a safe phi no longer claims the fallback. Two things are left:

  • zig/src/families/inkling2/metal/experts_plan.zig:197: with an infinite denominator and an amax that stays finite in BF16, the scan counts a three-product fallback and Model.load logs it. The shader gets phi = 0 and takes the two-product path (experts.metal:49). Please decide from phi(a, d) as the shader does, still report the denominator as nonfinite, and add this case to the tests.

  • zig/src/families/inkling2/metal/experts_test.zig:170 and zig/tests/contract/metal_inkling2_nvfp4_proof.zig:78,109: these checks pass whenever both sides are NaN, so a different NaN sign or payload goes unnoticed. Your reply says NaN scales stay bit-identical. Please compare the NaN bits too.

  • Synthena

@gilby

gilby commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Fixed in 49e5620f12:

  1. Decide 2-prod vs 3-prod fallback strictly from phi(a, d) (experts_plan.zig, model.zig): scanNvfp4Tensors no longer forces fallback via an early continue on !d_ok / !a_ok; it records finite_globals = false when either global is nonfinite or roundBf16(denominator[0]) == 0, then evaluates const p = phi(a, d) and classifies nvfp4PhiInSafeRange(p) / nvfp4TwoProdSafe(p) identically to experts.metal:25,49. With denominator = ±inf and BF16-finite amax, phi = ±0.0, so Model.load keeps 2-prod active (usesThreeProdFallback() == false) and logs a separate warning for nonfinite globals. Added unit coverage in experts_test.zig (+inf den with finite amax, and -inf den with mixed BF16-finite / floatMax amax) and runtime_test.zig (Model.scanNvfp4Weights).
  2. Compare raw NaN bits on both CPU tests and Metal GPU proofs (experts_test.zig:170, metal_inkling2_nvfp4_proof.zig:78,109): removed the std.math.isNan / both_nan bypasses so host tests (@as(u32, @bitCast(a)) == @as(u32, @bitCast(b)) including phi = ±0.0) and both assertBlockProof (as_type<uint>) and assertQuadProof (as_type<ushort>) verify exact raw NaN bit equality.

Verified on 49e5620f12: zig fmt --check, git diff --check, test-inkling2-host (44 pass, 4 skip), 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), python3 tools/pr_check.py --pr 4685 --base ad3d31f9ff (0 new lean findings), and Apple M5 Max GPU contract (./zig-out/bin/tf-kernel-contract --only "metal inkling2" -> SUMMARY 46 kernels: 46 pass gates 1-4, 0 fail, 0 known findings; 48 controls: 48 red as planted, 0 missed; 0 limit problems).

@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