Skip to content

inkling2 metal: match kernel threadgroup reservations and enforce load guards - #4981

Merged
ashhart merged 2 commits into
ashhart:mainfrom
gilby:v4-17-icb-and-tg-audit
Nov 25, 2026
Merged

ashhart merged 2 commits into
ashhart:mainfrom
gilby:v4-17-icb-and-tg-audit

Conversation

@gilby

@gilby gilby commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

What this changes

Before: on main, running tf-kernel-contract --only "metal inkling2" against offline Metal targets flagged 6 ReservationCodeMismatch limit failures because glue.metal, glue_split.metal, loop.metal, loop_split.metal, runtime.metal, and dspark.metal omitted explicit [[max_total_threads_per_threadgroup(N)]] attributes matching their launch records, and attention.zig, glue.zig, loop.zig, dspark_kernels.zig, and model.zig lacked load-time pipeline.maxThreads() guards. After: every kernel in those 6 shaders carries explicit [[max_total_threads_per_threadgroup(32|128|256)]] matching .reserved = .threads, load-time maxThreads() checks return named errors when exceeded, and host tests verify the shader attributes against contract records.

Checks on this head

  • Head commit: e299674146
  • Lean checker, tools/pr_check.py: lean 165 vs 165, 0 new, 15 changed files, 0 error(s). Touched .zig line counts max 299 lines (<= 600).
  • Tests run (-Dcpu=apple_m1 -Doptimize=ReleaseFast):
    • zig fmt --check build.zig zig and git diff --check origin/main..HEAD on e299674146 — exit 0.
    • Apple M5 Max (e299674146):
      • zig build test-inkling2-host test-inkling2-metal-host: 51/51 steps succeeded; 146/156 tests passed (10 skipped) (test-inkling2-host: 45 pass, 4 skip; test-inkling2-metal-host: 101 pass, 6 skip).
      • ./zig-out/bin/tf-kernel-contract --only "metal inkling2": SUMMARY 61 kernels: 61 pass gates 1-4, 0 fail, 0 known findings; 65 controls: 65 red as planted, 0 missed; 0 limit problems (gates 10183 ms, limits 522 ms, exit 0).
    • Apple M3 Ultra 512 GiB (Metal 3, identical 15-file patch 35dae9476e with 2-line thread qualifier overlay on prompt_attention.metal lines 11 and 15): ./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 10448 ms, limits 92 ms, exit 0).
  • CI: Pending GitHub Actions on e299674146.
  • Not run, and why: Non-Apple GPU backends (Metal-only change); Apple M1/M2 not directly available (covered by explicit max_total_threads_per_threadgroup attributes <= 256 and offline limit checks).

Receipt

  • Main against branch: Clears the 6 offline ReservationCodeMismatch limit failures without altering runtime arithmetic, dispatch widths, or kernel behavior (see BENCH_GATED_SKIP note on Inkling 2 Mini Metal V4: exact-serial scaling, GPU sampling, DFlash 2 acceleration, and kernel fusions #4961 for the separate MTLIndirectCommandBuffer investigation).
  • Boxes it reaches: inkling2 Metal shaders (dspark.metal, glue.metal, glue_split.metal, loop.metal, loop_split.metal, runtime.metal) and Metal pipeline loaders (attention.zig, dspark_kernels.zig, glue.zig, loop.zig, model.zig) plus shader attribute unit tests.
  • New switch? No.
  • Environment: Commit e299674146 on main 3795e6054c (M3 Ultra contract on identical patch 35dae9476e), Zig 0.17.0 -Dcpu=apple_m1 -Doptimize=ReleaseFast, Apple M5 Max (applegpu_g17), Apple M3 Ultra 512 GiB (Metal 3).
  • Paths and platforms the change reaches: Pipeline creation and compile-time threadgroup reservation declarations on all Apple Silicon Metal GPUs (<= 256 threads/TG).
  • Exactness: Zero arithmetic or dispatch-grid changes; all contract kernels pass gates 1-4 with 0 findings.
  • Decode speed before and after: Unchanged (n/a — compile-time reservation attributes and load-time maxThreads() checks only).
  • Prompt speed before and after: Unchanged (n/a — no prompt arithmetic or launch geometry change).
  • 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 explicit [[max_total_threads_per_threadgroup(32|128|256)]] attributes matching contract records across 31 existing kernels in glue.metal, glue_split.metal, loop.metal, loop_split.metal, runtime.metal, and dspark.metal (no new kernels added or replaced).
  • A planted control for each new kernel, and each one fails as planted: All 65 controls (M5 Max e299674146) and 55 controls (M3 Ultra 35dae9476e) fail red as planted (0 missed).
  • Each backend's SUMMARY line and exit status:
    • Apple M5 Max (e299674146): SUMMARY 61 kernels: 61 pass gates 1-4, 0 fail, 0 known findings; 65 controls: 65 red as planted, 0 missed; 0 limit problems; gates 10183 ms, limits 522 ms (exit 0).
    • Apple M3 Ultra (Metal 3, 35dae9476e + 2-line prompt_attention.metal thread overlay): 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 10448 ms, limits 92 ms (exit 0).
  • Chips I could not run: Apple M1/M2/M4 not available directly.

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 tested the landing order main 4c79cfe346d3c239ff7dfd7aa694df799755bafa → #4980 920224e12960a945f65db0f7e8ad79f83f4a326f → this PR e299674146ac916c1c3db1c2415f30028ffb1bec. Both merges were clean. On the combined tree zig build test-inkling2-metal-host -Dfixed-cpu --summary all passed 101 tests with six skips.

I also ran the device contract on an M5 Max (128 GB, macOS 27.2, Xcode 27.2, Zig 0.17.0):

zig build tf-kernel-contract -Dcpu=apple_m1 -Doptimize=ReleaseFast
./zig-out/bin/tf-kernel-contract --only 'metal inkling2'

Exit 0. SUMMARY 61 kernels: 61 pass gates 1-4, 0 fail, 0 known findings; 65 controls: 65 red as planted, 0 missed; 0 limit problems; gates 10247 ms, limits 457 ms. This verifies the two heads together on M5 Max. The M3 and live-checkpoint results remain the author's receipts.

Igor Lessio - AIFLOW LABS

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thanks, @gilby. The reservation attributes match the existing launches, the shader bodies are unchanged, and the new load guards clean up before returning an error. The host and contract results, including the combined M5 check, support this bounded fix.

Ready to land with the normal final-tree checks. The M3 result keeps its disclosed compile overlay; it is not a run of this exact head, and it is not a speed comparison.

  • Synthena

@AIFlowML

Copy link
Copy Markdown
Contributor

I checked the three-way integration on d2b43908db with #5001 2a1fb997f6, #5004 23870c9381 and this head e299674146. After resolving the earlier #5001/#5004 conflicts, I retained this PR's shader bounds and load guard, adding four 256-thread entries for AR and 32/256 for the paired static/dynamic convolutions in the 17-pipeline table.

On M5 Max, zig build test-inkling2-host test-inkling2-metal-host tf-kernel-contract -Dfixed-cpu -j2 --summary all passed 149 host tests with 10 skips. ./zig-out/bin/tf-kernel-contract --only 'metal inkling2' passed 67 kernels, caught all 71 controls and found zero problems across 77 launch records. This is scratch integration evidence. Checkpoint serving and #5005 were outside the run.

Igor Lessio - AIFLOW LABS

@AIFlowML

Copy link
Copy Markdown
Contributor

I tested exact train #5016 96c778c65d on M5 Max. It matches a clean merge of this source head e299674146 into 4beda42498 apart from the train changelog. The source also trial-merges cleanly into current main aa80cd2ae7.

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

The exact train passed 148 tests with 10 skips. On device all 61 kernels passed, all 65 planted controls were caught, and all 71 Inkling launch-limit checks passed. I also combined #5016, #5014 and post-#4895 main 777f3bb762 in scratch. Both merges into that trial needed only changelog resolution. The same commands passed 151 tests with 10 skips, 61 kernels, 65 controls and 78 launch-limit checks. Main has since added #4934, whose files do not overlap the Metal checks.

This checks the host gates and M5 launch behavior in both trees. I did not run an exact-head M3 device test or a full checkpoint.

Igor Lessio - AIFLOW LABS

gilby and others added 2 commits November 25, 2026 09:47
…d guards

Refs ashhart#4961

- Add explicit max_total_threads_per_threadgroup(32|128|256) attributes matching launch records across glue, glue_split, loop, loop_split, runtime, and dspark Metal shaders to clear offline M1/M2 reservation mismatches.

- Enforce pipeline.maxThreads() guards at load time in attention, glue, loop, dspark_kernels, and model.

- Verify shader reservation attributes against contract records in host tests.
@ashhart
ashhart force-pushed the v4-17-icb-and-tg-audit branch from e299674 to 6d8cd4a Compare November 25, 2026 09:47
@ashhart
ashhart merged commit b5df430 into ashhart:main Nov 25, 2026
2 of 12 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.

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