Skip to content

inkling2 metal: fuse relative bias projection into decode and prompt attention - #5000

Merged
ashhart merged 2 commits into
ashhart:mainfrom
gilby:v4-15-relbias-onthefly-rebased
Nov 25, 2026
Merged

ashhart merged 2 commits into
ashhart:mainfrom
gilby:v4-15-relbias-onthefly-rebased

Conversation

@gilby

@gilby gilby commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Before: on main, each layer's Inkling 2 attention launches a separate tf_ink2_rel_table kernel to materialize the [rows * heads, 1024] BF16 relative-bias table (rel_tab) before tf_ink2_attn_scalar / tf_ink2_attn_prompt read it back from global memory.

After: removes tf_ink2_rel_table and the rel_tab scratch buffer, and computes the same BF16-rounded relative bias on the fly directly from rel and proj in identical projection tap order inside tf_ink2_attn_scalar (decode attention) and tf_ink2_attn_prompt (prompt attention). Adds reference parity proofs and planted controls against saved main kernel baselines in metal_inkling2_attention_ref.zig and metal_inkling2_attention_controls.zig.

Checks on this head

  • Head commit: e66d1f50a0
  • Lean checker, python3 tools/pr_check.py --base origin/main (python3 tools/zig/lean_check.py): lean 164 vs 164, 0 new, 0 error(s) (12 changed files); zig fmt --check build.zig zig and git diff --check origin/main..HEAD exit 0.
  • Tests run, with the command and pass, fail and skip counts:
    • zig build compile-inkling2-native-host: exit 0.
    • zig build test-inkling2-host -Dcpu=apple_m1 -Doptimize=ReleaseFast: 45 passed; 4 skipped; 0 failed (skipped: 4 optional checkpoint/tokenizer env tests).
    • zig build test-inkling2-metal-host -Dcpu=apple_m1 -Doptimize=ReleaseFast binary: 104 passed; 6 skipped; 0 failed (skipped: env-gated checkpoint/alloc tests and 2 JPEG tests pending matching libjpeg-turbo).
  • CI: Opened on this head; CI checks pending after PR creation.
  • Not run, and why: Apple M1/M2/M4 and M3 Ultra served A/Bs not run for this slice; Apple M5 Ultra and Apple M5 Pro A-B-B-A served runs cover decode and cold prefill across Ultra and Pro GPUs.

Receipt

  • Main against branch: A-B-B-A served runs (--no-drafts --parallel 4, 0 cross-leg token_sha256 mismatches across all decode and cold-prefill cells on both machines):
    • Apple M5 Ultra 512 GiB (main vs branch):
      • Plain decode (--no-drafts --parallel 4 --context 8192, T=0.0, --tokens 128 --reps 2):
        • c=1: code: 16.80 -> 17.35 tok/s (+3.27%); chat: 16.85 -> 17.30 tok/s (+2.67%).
        • c=4: code: 37.45 -> 39.45 tok/s (+5.34%, TTFT 0.529 -> 0.524s, -0.85%); chat: 38.40 -> 40.80 tok/s (+6.25%, TTFT 1.712 -> 1.708s, -0.29%).
      • Cold prefill (--no-drafts --context 16384 --prompt-cache-gib 0):
        • 2048 (2077 prompt tokens): 92.3 -> 92.5 tok/s (+0.11%, 22.497s -> 22.458s).
        • 8192 (8233 prompt tokens): 90.8 -> 91.0 tok/s (+0.11%, 90.599s -> 90.528s).
    • Apple M5 Pro 64 GiB (main vs branch):
      • Plain decode (--no-drafts --parallel 4 --context 8192, T=0.0,1.0, --tokens 128 --reps 2):
        • c=1: code T=0.0: 13.40 -> 13.55 tok/s (+1.12%); chat T=0.0: 13.50 -> 13.65 tok/s (+1.11%, TTFT -0.32%); code T=1.0: 13.45 -> 13.60 tok/s (+1.12%, TTFT -0.38%); chat T=1.0: 13.55 -> 13.70 tok/s (+1.11%, TTFT -0.20%).
        • c=4: code T=0.0: 21.40 -> 21.80 tok/s (+1.87%, TTFT -1.04%); chat T=0.0: 22.90 -> 23.40 tok/s (+2.18%); code T=1.0: 21.35 -> 21.70 tok/s (+1.64%, TTFT -0.52%); chat T=1.0: 22.90 -> 23.40 tok/s (+2.18%, TTFT -0.40%).
      • Cold prefill (--no-drafts --context 16384 --prompt-cache-gib 0):
        • 2048 (2077 prompt tokens): 30.2 -> 30.6 tok/s (+1.32%, 68.767s -> 67.879s).
        • 8192 (8233 prompt tokens): 30.0 -> 30.2 tok/s (+0.67%, 274.145s -> 272.625s).
  • Boxes it reaches: attention.metal, prompt_attention.metal, attention.zig, attention_plan.zig, forward.zig, state.zig, and attention contract/baseline/reference tests.
  • New switch? No (always active).
  • Environment, with the TensorFold commit, Zig and backend toolchain, machine and GPU, checkpoint and revision: Commit 2d955fd616 stacked on ed70f71aa7, Zig 0.17.0 -Dcpu=apple_m1 -Doptimize=ReleaseFast, Apple M5 Max (applegpu_g17), Apple M5 Ultra 512 GiB, Apple M5 Pro 64 GiB; checkpoint Inkling-2-Mini.
  • Paths and platforms the change reaches, which I ran and which I could not: Inkling 2 Metal decode attention (tf_ink2_attn_scalar) and prompt attention (tf_ink2_attn_prompt) 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 prefill A-B-B-A legs on both Apple M5 Ultra and Apple M5 Pro; 0-ULP bitwise BF16 parity against saved main baseline kernels verified by metal_inkling2_attention_ref.zig.
  • Decode speed before and after: See A-B-B-A tables above (+2.67%..+6.25% on Apple M5 Ultra; +1.11%..+2.18% on Apple M5 Pro across all c=1,4 and T=0.0,1.0 cells).
  • Prompt speed before and after: Cold prefill (--prompt-cache-gib 0) 2048 / 8192: +0.11% / +0.11% (22.497->22.458s / 90.599->90.528s) on Apple M5 Ultra; +1.32% / +0.67% (68.767->67.879s / 274.145->272.625s) on Apple M5 Pro.
  • 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 tf_ink2_attn_scalar and tf_ink2_attn_prompt to project relative bias on the fly in identical BF16-rounded tap order, removing tf_ink2_rel_table and the rel_tab buffer.
  • A planted control for each new kernel, and each one fails as planted: 66/66 planted controls on Apple M5 Max (2d955fd616) and 56/56 on Apple M5 Pro fail red as planted (0 missed), including relative-bias projection and tap-order parity controls in metal_inkling2_attention_controls.zig.
  • Each backend's SUMMARY line and exit status:
    • Apple M5 Max (e66d1f50a0, rebased on 47a605aa12): SUMMARY 62 kernels: 62 pass gates 1-4, 0 fail, 0 known findings; 66 controls: 66 red as planted, 0 missed; 0 limit problems; gates 13792 ms, limits 155 ms (exit 0).
    • Apple M5 Pro (f90c6a429c): SUMMARY 49 kernels: 49 pass gates 1-4, 0 fail, 0 known findings; 56 controls: 56 red as planted, 0 missed; 0 limit problems; gates 11214 ms, limits 7 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

The clean trial of 2d955fd616 on 4c79cfe346 passed 147 tests with ten skips. On M5 Max, the Inkling device filter passed all 62 kernels, caught all 66 planted controls and found zero launch-limit problems.

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 'metal inkling2'

I also tried the landing order #4980 (920224e129), #4981 (e299674146), #4995 (38dfab19f7), #4998 (c011aec4bc), then this PR. The last merge has two conflicts. For attention.zig, remove the table pipeline and keep #4981's load guards for the four surviving pipelines (32, 32, 128, 256 threads). For state.zig, drop the table's scratch slot and keep #4995's grouped-expert scratch. With those resolutions in the test checkout, the same command passed and the device filter passed 67 kernels, caught 79 controls and found zero launch-limit problems. Combined checkpoint serving and speed comparisons remain open.

Igor Lessio - AIFLOW LABS

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thanks, @gilby. The fusion keeps the 16-tap order and BF16 rounding before scoring. The frozen baselines match the parent, and the reported replacement parity and planted controls are useful.

Safe for bounded A/B with full checkpoint/build identities and serial, resume and draft/plain checks. In the combined stack, retain the load guards and grouped-expert scratch while removing the old table pipeline and buffer.

  • Synthena

@AIFlowML

Copy link
Copy Markdown
Contributor

I tested the combined tree on M5 Max with this PR at e66d1f50a0, grouped experts #4995 at 41bd6e1162, and the standalone DSpark loader fix #5041 at 2d7ce3cbb5, on main 9961ca492.

The #4995 pairing conflicts only in the name and size tables in state.zig. I kept its expert_group scratch entry with experts_plan.groupBytes(...) and removed the old rel_tab entry and size from both tables. The DSpark fix applied cleanly.

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 'metal inkling2'

The combined tree passed 157 tests with ten skipped. All 67 Metal kernels passed, all 81 planted controls were caught, and no launch exceeded its limits. The fused scalar and prompt attention probes matched the frozen kernels bit for bit, and the four grouped expert kernels retained their bitwise comparisons. Checkpoint performance and served resume/draft parity were outside this run.

Igor Lessio - AIFLOW LABS

@ashhart
ashhart force-pushed the v4-15-relbias-onthefly-rebased branch from e66d1f5 to e718a8d Compare November 25, 2026 15:36
@ashhart
ashhart merged commit f3c0fa8 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