Skip to content

inkling2 metal: add explicit thread qualifiers in prompt attention helpers - #4980

Merged
ashhart merged 2 commits into
ashhart:mainfrom
gilby:fix/inkling2-metal3-thread-qualifiers
Nov 25, 2026
Merged

ashhart merged 2 commits into
ashhart:mainfrom
gilby:fix/inkling2-metal3-thread-qualifiers

Conversation

@gilby

@gilby gilby commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Before: compiling zig/kernels/metal/inkling2/prompt_attention.metal for Apple M3 Ultra's pre-M5 shader path under macOS 26 failed with LibraryLoad because helper parameters s, mm, ll, a, and b in ink_prompt_rh and ink_fold omitted explicit thread address-space qualifiers. After: both helpers declare thread references explicitly (const thread InkPromptSeg& s, thread float& mm, ...), making Inkling 2 Mini's pre-M5 shader path in prompt_attention.metal compile and pass natively on Apple M3 Ultra with zero arithmetic change.

Checks on this head

  • Head commit: 920224e129
  • Lean checker, tools/pr_check.py: lean 165 vs 165, 0 new, 1 changed files, 0 error(s).
  • Tests run:
    • zig fmt --check build.zig zig and git diff --check origin/main..HEAD on 920224e129 — exit 0.
    • Pre-rebase identical 1-file patch 8614421446 on Apple M3 Ultra 512 GiB (pre-M5 shader path compiled via CompileOptions.mlx() MSL 4.0, zero overlays, -Dcpu=apple_m1 -Doptimize=ReleaseFast):
      • ./zig-out/bin/tf-kernel-contract --only "metal inkling2": SUMMARY 48 kernels: 48 pass gates 1-4, 0 fail, 0 known findings; 55 controls: 55 red as planted, 0 missed; 0 limit problems (gates 11128 ms, limits 107 ms, exit 0).
      • Live serve smoke (Inkling-2-Mini ed40e7da): --no-drafts (16 tokens, sha=8c6985a37103, 12.78 tok/s, ttft=0.49s) and --drafter (16 tokens, sha=8c6985a37103, 24.49 tok/s, ttft=0.512s).
  • CI: Pending GitHub Actions on 920224e129.
  • Not run, and why: Non-Apple backends (Metal-only shader qualifier fix); M3 Ultra Metal 3 verified directly.

Receipt

  • Main against branch: Unpatched main fails LibraryLoad on Metal 3 when compiling prompt_attention.metal; with this 2-line thread qualifier fix, Apple M3 Ultra's pre-M5 shader path compiles natively (48/48 kernels pass gates 1-4, 55/55 red controls, 0 limits) and serves both plain (12.78 tok/s, sha=8c6985a37103) and drafted (24.49 tok/s, sha=8c6985a37103) bit-identically.
  • Boxes it reaches: inkling2 Metal prompt_attention.metal (tf_ink2_attn_prompt).
  • New switch? No.
  • Environment: Commit 920224e129 on main 3795e6054c (M3 Ultra contract & live smoke on identical patch 8614421446), Zig 0.17.0 -Dcpu=apple_m1 -Doptimize=ReleaseFast, Apple M3 Ultra 512 GiB (macOS 26.6.2, pre-M5 shader path via CompileOptions.mlx() MSL 4.0), Inkling-2-Mini ed40e7da625f594fc9c4990f5f66be24873c22d7.
  • Paths and platforms the change reaches: Apple M3 Ultra's pre-M5 shader path in prompt_attention.metal on macOS 26 (note: CompileOptions.mlx() compiles with MSL 4.0 even on pre-M5 hardware; with -std=metal3.2, shared mpp helpers still fail separately). M5 tensor-unit paths accept explicit thread qualifiers identically.
  • Exactness: Plain vs --drafter smoke check bit-identical (sha=8c6985a37103); tf_ink2_attn_prompt passes contract gates 1-4 with 0 math changes.
  • Decode speed before and after: Unchanged arithmetic; unlocks pre-M5 Apple Silicon execution (12.78 tok/s plain, 24.49 tok/s drafted on M3 Ultra smoke).
  • Prompt speed before and after: Unlocks pre-M5 prompt attention execution (ttft=0.49s plain, 0.512s drafted on short smoke prompt).
  • 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: Fixes pre-M5 path compilation of existing tf_ink2_attn_prompt in prompt_attention.metal (no new kernels added or replaced).
  • A planted control for each new kernel, and each one fails as planted: All 55 metal inkling2 planted controls fail red as planted on Apple M3 Ultra (0 missed).
  • Each backend's SUMMARY line and exit status: Apple M3 Ultra (pre-M5 shader path, CompileOptions.mlx() MSL 4.0, 8614421446): SUMMARY 48 kernels: 48 pass gates 1-4, 0 fail, 0 known findings; 55 controls: 55 red as planted, 0 missed; 0 limit problems; gates 11128 ms, limits 107 ms (exit 0).
  • Chips I could not run: Apple M1/M2/M4 not available directly (M3 Ultra covers pre-M5 shader-path strict address-space checking).

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

I reproduced the compiler failure on main 4c79cfe346d3c239ff7dfd7aa694df799755bafa and checked a clean trial merge of 920224e12960a945f65db0f7e8ad79f83f4a326f with the Xcode 27.2 toolchain. For each tree I concatenated nax.h and prompt_attention.metal as attention_sources.zig does, then ran:

xcrun -sdk macosx metal -std=metal4.0 -fmetal-math-mode=safe -c prompt.metal -o prompt.air

The pre-M5 source (with #define TF_SIMD_FRAGS 1 prepended) failed on main at the five missing address-space qualifiers and compiled after the change. The tensor-unit source also went from the same compiler error to a successful compile.

One wording detail for the receipt: CompileOptions.mlx() selects Metal Shading Language 4.0 on macOS 26, even on pre-M5 hardware. With -std=metal3.2, the pre-M5 source still fails at the shared mpp helpers on both trees. Naming the M3 Ultra's pre-M5 shader path would distinguish the GPU result from the language version. My run was compiler-only, without an M3 device test.

Igor Lessio - AIFLOW LABS

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thanks @gilby. The change adds only the five thread qualifiers in the two helper signatures; all arithmetic and call sites are unchanged. It merges cleanly.

The compiler comparison covers both assembled shader variants, and the receipt now distinguishes the pre-M5 hardware path under MSL 4.0 from Metal 3.2 language support. The latter remains a separate limitation. This qualifier fix is ready to land from the source review.

  • Synthena

@ashhart
ashhart force-pushed the fix/inkling2-metal3-thread-qualifiers branch from 920224e to 857c9f2 Compare November 25, 2026 17:49
@ashhart
ashhart merged commit df4846f into ashhart:main Nov 25, 2026
1 of 10 checks passed
@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.

ashhart added a commit to javierbits/TensorFold that referenced this pull request Nov 25, 2026
ashhart added a commit that referenced this pull request Nov 25, 2026
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