Skip to content

Kernel contract: free each kernel's device buffers once its gates are done - #2042

Merged
ashhart merged 3 commits into
ashhart:mainfrom
BlivionIaG:pr/contract-release-buffers
Nov 4, 2026
Merged

ashhart merged 3 commits into
ashhart:mainfrom
BlivionIaG:pr/contract-release-buffers

Conversation

@BlivionIaG

@BlivionIaG BlivionIaG commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

kernel-contract runs out of device memory on a 16 GiB card before it prints a SUMMARY. contract_rig.Rig frees a kernel's buffers only at that kernel's next prepare or at exit, so the last-prepared buffers of every registered kernel stay resident while the later suites run. On 8e35d523b the runner held 10,557 MiB after the gates and failed with CUDA_ERROR_OUT_OF_MEMORY in the MoE backward suite.

This adds an optional VTable.release, set by hooks(T) to rig.reset() when the rig has one, and runAll calls it once a kernel's gates are done. The adapter inspects the pointee type for pointer rigs, fixing the macOS dev-tools compile failure from calling @hasDecl on *Rig. No kernel, gate or bound changes; direct one callers still own cleanup.

Receipt

  • Environment/source: current head 1307195f1f7a4d237dbb61b90edc917608584f9d over main 37c224142; macOS, Apple M4 Pro, Zig 0.17.0 for the new host/build checks and filtered Metal run. The earlier full CUDA receipt used RTX 5060 Ti 16 GiB (sm_120), nvcc 13.4, at 8e35d523b plus the original release-hook patch and a local per-kernel memory print that is not in this PR.
  • Exactness: n/a to serving output. Earlier full CUDA result, not rerun on this head: SUMMARY 518 kernels: 518 pass gates 1-4, 0 fail, 6 known findings; 346 controls: 346 red as planted, 0 missed; without the hook that build ended in out-of-memory with no SUMMARY. On the current head, tf-kernel-contract --only 'metal nemotron attention prompt nax' --no-limits ran on the M4 Pro: SUMMARY 1 kernels: 1 pass gates 1-4, 0 fail, 0 known findings; 1 controls: 1 red as planted, 0 missed. This is a filtered Metal receipt, not a full-backend or gate-5 run.
  • Decode speed before and after: n/a, the contract runner is not on the serving path.
  • Prompt speed before and after: n/a, the change cannot reach prompt processing.
  • Tests run on the current head: zig build test-kernel-contract check-native-cuda-host dev-tools tf-kernel-contract completed 254/254 steps, with 169/169 tests reported passing and build-cache reuse. This includes the previously failing macOS dev-tools target. The lifecycle regression now allocates real buffers and requires the prior kernel's buffers to be released before the next prepares, rather than merely counting hook calls. zig fmt --check passed; zig build compile-cuda-tests -Dtarget=aarch64-linux completed 10/10 steps (cross-compilation only).
  • Current-head CI: No GPU run 41715065665 passed all six jobs, including Linux checks, Windows checks, benchmark clients on both platforms, release paths and the previously failing macOS dev-tools build.
  • Not run: full CUDA or full Metal runner on this head, Metal gate 5 in the filtered run, or HIP/XPU/Windows GPU runners. The M5-only pointer-rig consumer compiles in the macOS build but cannot execute on the M4 Pro; no M5 runtime result is claimed.

Checklist

  • Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off. (not a serving change)
  • Drafted output equals "draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. (not a serving change)
  • No precision traded for speed. (no kernel or bound changes)
  • A new or changed GPU kernel is registered with the kernel contract. (no kernel changes; the CUDA SUMMARY is above)
  • Prompt processing is no slower than the last release. (not reachable)
  • The description says which platforms I ran: current-head Metal M4 Pro and host checks; the earlier CUDA receipt is source-labelled separately.
  • The existing macOS dev-tools build catches the pointer-rig regression: it failed before the fix and now passes locally. The allocator-backed host test verifies cleanup before the next kernel prepares.
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths. No AI attribution lines.

@ashhart

ashhart commented Nov 3, 2026

Copy link
Copy Markdown
Owner

Thanks @BlivionIaG, this is a clean fix. Every rig that hooks() reaches has an idempotent reset(), the defer runs after the controls too, and the new test pins one release per kernel. Once its own CI is green it joins our landing queue with a changelog line crediting you.

@ashhart

ashhart commented Nov 3, 2026

Copy link
Copy Markdown
Owner

Thank you, @BlivionIaG. This is clean: one release hook, called once per kernel after its gates or control, with a
test that each kernel is released exactly once, and nothing in the gates' maths or controls changes. It's queued to
land. @Maxrio9's #1845 got to the same problem first and also frees each fixture's host arrays and ends the
registered modules before the standalone suites, so we've asked them to rebase that part onto your hook. Thanks to
both of you.

@ashhart
ashhart force-pushed the pr/contract-release-buffers branch from 1307195 to af9b2a9 Compare November 4, 2026 00:52
@ashhart
ashhart merged commit 665bb96 into ashhart:main Nov 4, 2026
3 of 7 checks passed
@ashhart

ashhart commented Nov 4, 2026

Copy link
Copy Markdown
Owner

Thank you @BlivionIaG. 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.

Maxrio9 added a commit to Maxrio9/TensorFold that referenced this pull request Nov 4, 2026
…wners so the full run fits in memory

Test-only. Rig.finishGates frees a finished kernel's device buffers and host
copies, and the kernel's release hook (added in ashhart#2042) calls it where the Rig
has it. The registered owners and modules are scoped so their destructors run
before the standalone suites allocate theirs; the closing phase moves to
contract_finish.zig to keep contract_tests.zig under 600 lines. Math, shapes,
seeds, controls and bounds are unchanged.
Maxrio9 added a commit to Maxrio9/TensorFold that referenced this pull request Nov 4, 2026
@ashhart

ashhart commented Nov 5, 2026

Copy link
Copy Markdown
Owner

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.

2 participants