Skip to content

qwen27 metal: a keep that fails part way ends its encoder, so the error returns instead of aborting the process - #2376

Merged
ashhart merged 1 commit into
ashhart:mainfrom
jschmied:q27-keep-errdefer
Nov 7, 2026
Merged

ashhart merged 1 commit into
ashhart:mainfrom
jschmied:q27-keep-errdefer

Conversation

@jschmied

@jschmied jschmied commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Runner.keep opens its command encoder before the per-layer GDN commits and attention keep uploads. Any error after that point (an allocation, a refused keep) used to leave the encoder open. The objc pool's release then asserted Command encoder released without endEncoding: the process aborted, and the error never reached the caller. Now a guarded errdefer if (!ended) e.end();, as in native/qwen27_keep.zig, closes it. The error returns and the same round can keep again, and the errors after the encoder's own end (error.RoundGpuFailure, a refused path) don't end it twice.

Checks on this head

  • Lean checker on the base and on this head: 252 and 252, new findings 0.
  • Tests run:
    • zig build tf-qwen27-keep-error && tf-qwen27-keep-error (new). On main it aborts with the assertion, exit 134. On this head: PASS a keep failing at each of its 6 allocations returns OutOfMemory and keeps on retry; a refused path after the encoder ends returns BadPath, with and without MTL_DEBUG_LAYER=1. This can't tell a double end apart: this Mac doesn't flag a second endEncoding even with validation on, so the unguarded form passes too. RoundGpuFailure can't be injected, so that path is unexercised.
    • zig build test --summary all: 715/715 steps.
    • zig fmt --check and git diff --check are clean.
  • CI: not run yet.
  • Not run, and why: nothing beyond the above. The change is a few lines on an error path.

Receipt

  • Environment: Zig 0.17.0, Apple M3 Pro (no tensor units), macOS 27.2, the synthetic 5-layer test model (no checkpoint).
  • Paths reached: Runner.keep's error path only. A keep that succeeds runs the same commands as before.
  • Exactness: n/a. No arithmetic or token path changes; a successful keep encodes the same work.
  • Decode and prompt speed: n/a. Nothing on the success path changes.
  • Licence of new weights or drafter: n/a.

Checklist

  • The title and description match the head commit.
  • The new test fails before the change (aborts) and passes after it. It needs a Metal GPU, as the other qwen27 Metal tools do.
  • Comments are one line.
  • No personal data, machine names or local paths. No AI attribution lines.

jschmied added a commit to jschmied/TensorFold that referenced this pull request Nov 6, 2026
@ashhart

ashhart commented Nov 6, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied, a clean find, and the failing-allocator tool is a good check.

One thing: keep still calls e.end() before commit, and two returns can fail after that, error.RoundGpuFailure and try window.keep(path). With an unguarded errdefer e.end(), either would call endEncoding a second time. Could you use the guarded form from main's other keep sites (errdefer if (!ended) e.end();, as in native/qwen27_keep.zig:43), setting ended = true at the existing e.end()?

And please rebase the CHANGELOG line onto current Unreleased, ending "(#2376). Thanks to @jschmied."

@jschmied

jschmied commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @ashhart, you're right, the unguarded form could end the encoder twice. Both are done on 19fa70fc6, rebased onto main 89850e28f, one commit:

Two limits on that test. It can't tell a double end apart, because this M3 Pro doesn't flag a second endEncoding even with MTL_DEBUG_LAYER=1, so the unguarded form passes too. And error.RoundGpuFailure can't be injected, so that return isn't exercised. The guard there rests on reading the code.

Main still aborts with exit 134. zig build test: 715/715 steps. Lean checker 252 and 252, new findings 0. zig fmt --check and git diff --check are clean.

@ashhart

ashhart commented Nov 6, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied, that's the guarded form, with ended set right after the encoder's own end, and the refused-path case is a useful addition to the test. It lands once CI has run. We'll place the CHANGELOG line, since Unreleased has moved again.

…or returns instead of aborting the process

Runner.keep opens its command encoder before the per-layer commits and keep uploads; any error after that left
the encoder open, and the objc pool's release then asserted "Command encoder released without endEncoding", so
the process aborted and the error was lost. A guarded errdefer ends it, as qwen27_keep's other keep sites do, so
the errors after the encoder's own end (a GPU failure, a refused path) don't end it twice. tf-qwen27-keep-error
fails each of the keep's allocations in turn on a synthetic model: on main it aborts, here each returns
OutOfMemory and the same round keeps on retry; then a refused path after the end returns BadPath.
Found on the pipeline stage path (ashhart#2005).
@ashhart
ashhart merged commit 98f84b8 into ashhart:main Nov 7, 2026
3 of 7 checks passed
@ashhart

ashhart commented Nov 7, 2026

Copy link
Copy Markdown
Owner

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

ashhart commented Nov 7, 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