Skip to content

nemotron metal: a failed load releases the kernels it compiled - #875

Merged
ashhart merged 1 commit into
ashhart:mainfrom
jishnuvenugopal:fix/nemotron-load-releases-kernels
Oct 16, 2026
Merged

ashhart merged 1 commit into
ashhart:mainfrom
jishnuvenugopal:fix/nemotron-load-releases-kernels

Conversation

@jishnuvenugopal

@jishnuvenugopal jishnuvenugopal commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Model.load (zig/src/families/nemotron/model.zig) compiles the kernels and the prompt-chunk kernels on their own
threads while the weights load. When anything after that failed, the set that had compiled was never released. Both
compile results now have an errdefer until load returns. That replaces the one errdefer m.kernels.deinit(), so
nothing is released twice.

Two ways to reach it: a shard that can't be read, which an interrupted download leaves, or KernelCompile in the
other set, which is how 1.0.11 fails on macOS 26.3.1. Either way, the server printed 87 error(SafeAllocator): leaked
reports after the real error. They are the prompt-chunk set's 86 duplicated pipeline names and its 3,224-byte table.
The 26.3.1 KernelCompile itself is a separate problem; this change only releases what compiled before it.

Verified outcome

  • Why: when a model failed to load, for example with a damaged or missing weight file, or with the macOS 26.3.1
    kernel error, the server printed 87 memory-leak reports after the real error and buried the line that mattered.
  • What was done: the loader now releases the kernels it had already built whenever loading stops with an error.
  • Result: the same failures print only the real error, with 0 leak reports instead of 87. A new test fails before
    the change and passes after it, and serving a checkpoint with a shard removed shows the same. Successful loads are
    unchanged: the same tokens, exactness 60 of 60 and 10 of 10, and the same decode speed.

Receipt

  • Environment: main at c7137bb, Zig 0.17.0, Xcode 27.0 (Metal 32023.921), Apple M4 (10-core GPU), 32 GB,
    macOS 27.0.1. Checkpoint TensorFold/NVIDIA-Nemotron-3.5-Lightning-30B-A3B-MLX-4bit at d9d758f.
  • New test, a load that fails after its kernels compile releases them, in model.zig. It skips without a Metal device.
    It writes a Lightning config and an empty shard. On c7137bb it fails with leaked 87 allocations. With this change
    it passes.
  • Served with the checkpoint's last shard removed. On c7137bb: cannot open .../model-00004-of-00004.safetensors,
    then cannot load ... (OpenFailed), then 87 leak reports. On this branch: the same two lines and no leak reports.
  • 1.0.11 on macOS 26.3.1 printed the same 87 reports, with the same sizes, after KernelCompile. A local build that
    forces that path on 27.0.1 goes from 87 reports to 0 with this change.
  • Exactness, from tools/bench_concurrent.py --levels 1,4 --tokens 256 --reps 3 --temperatures 1.0,0 --alone --serial
    on this branch, serving with --parallel 4: concurrent equals alone in 60 of 60 runs, and alone equals
    "draft": false in 10 of 10. Nothing unequal or failed.
  • Decode speed before and after, from tools/bench_openai.py --tokens 256 --reps 3 --temperatures 0, run alternately
    on c7137bb and this branch, both reading one timing table: fibonacci-raw 48.1 and 47.9 tok/s before, 47.9 and 48.5
    after; gpu-chat-no-think 46.7 and 47.2 before, 46.9 and 46.8 after. Token hashes are the same on both builds
    (c91869ce1808, 5ef050084ac3). Without the shared table each build times its own at first load, since
    cost_cache.key hashes the executable, and six fresh timings alone moved fibonacci-raw between 46.5 and 50.9 tok/s.
  • Prompt speed before and after: n/a. Only a load that fails runs the new code.
  • Tests run: zig build test test-golden -Dcpu=apple_m1, uncached: 164 of 164 steps, 1983 of 1991 tests pass, 8 skip,
    0 fail (c7137bb: 1981 of 1989, 8 skip). The two more are the new test and draft_ids.zig's test, which the engine's
    test binary now reaches through model.zig. zig build fuzz-cortheon passes. zig fmt --check build.zig zig is
    clean. Lean: 527 problems on the base and 527 on this branch, 0 new (lean_new.py).
  • Not run, and why: CUDA, since the CUDA engine loads in cuda_engine.zig and does not use Model.load. Only an M4
    here.

Checklist

  • 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. (Resumed
    against fresh was not re-run: only a load that fails runs the new code.)
  • No precision traded for speed. If the bits change, the description says which sums change and why.
  • Prompt processing is no slower than the last release. (n/a, as above.)
  • The description says which platforms I ran, Metal M1 to M5 and CUDA, and which I could not.
  • 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. No AI attribution lines.

Model.load compiles the kernels and the prompt-chunk kernels on their own threads while the weights load.
When anything after that failed (a shard that cannot be read, or KernelCompile in the other set), the set that
had compiled was dropped without deinit, and the server's SafeAllocator printed 87 leak reports after the error.
Both compile results are now released by errdefers until load returns.
@ashhart
ashhart merged commit 16903c2 into ashhart:main Oct 16, 2026
ashhart added a commit that referenced this pull request Oct 16, 2026
@ashhart

ashhart commented Oct 16, 2026

Copy link
Copy Markdown
Owner

Thank you, @jishnuvenugopal. A failed load that leaked both compiled kernel sets is easy to miss, and your test with an empty shard, which fails on the base with 87 leaks, pins it down. We checked the one case that could have gone wrong: both results start as an error before the compile threads spawn, and the inner block joins the first thread before the errdefers run, so a failed spawn releases nothing it should not.

Your commit ships in TensorFold 1.0.12 as it is, merged in 16903c262, with no change of ours beyond its CHANGELOG line, so this pull request shows as merged. Your test runs in the Mac zig build test and passes with no leak, and Nemotron on an M3 Ultra gave the same tokens as before, drafted, plain, resumed and concurrent.

Posted by Ash's AI Assistant Synthena

@jishnuvenugopal
jishnuvenugopal deleted the fix/nemotron-load-releases-kernels branch October 17, 2026 03:06
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 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.

2 participants