Fix silent Metal OOM error by sticky stream - #4134
Closed
jasonge27 wants to merge 5 commits into
Closed
Conversation
…l-explore#3979) Command-buffer failures could be discarded when get_command_encoder() reset encoder-local error state after a mid-eval commit with no signal event. Park the error per stream until synchronize()/Event::wait().
Report only from the stream: EventImpl::wait no longer throws, array::wait stops taking the shortcut, and the cpu-stream wait leaves the error parked for the next synchronization point.
jasonge27
marked this pull request as draft
August 10, 2026 08:47
jasonge27
marked this pull request as ready for review
August 10, 2026 08:50
Member
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Proposed changes
Addressing issue #3979, where the MLX drops OOM errors silently in an edge case.
What goes wrong today:
needs_commit()and submits a buffer with no signal events (eval.cpp around the mid-eval commit).signal_events.Later,
get_command_encoder() /error_.reset()discards the error. In the end, GPU failed, user got silence + wrong data.. I'm also linking a cpp gist to reproduce the error (without having to install python library).Changes:
Handling error in lazy-eval / async systems is hard. CUDA uses sticky last-error and PyTorch surfaces async errors at the next sync. I think following industry best practices, we should park the error per stream rather than per encoder, and report it from synchronization points rather than from events:
StreamErrorkeyed by stream index, so it outlives the encode cycle.get_command_encoder()no longer resets it.commit()captures the holder byshared_ptrinstead of&error_, which also removes a reference that could outlive the encoder.Event::waitchecks the stream after waiting;EventImpl::waitno longer throws, leaving the event error to propagate across streams via wait_events as before.array::waitstops taking theis_available()shortcut, which detached the event as soon as the GPU signaled and skipped the check.Event::wait(Stream)reports nothing. It runs on a scheduler worker with no handler above it, so throwing there terminated the process rather than reaching the user.With our changes, now the test script above would throw error
[METAL] Command buffer execution failed: Insufficient Memory (00000008:kIOGPUCommandBufferCallbackErrorOutOfMemory).Testing
I added four cases into
tests/gpu_tests.cpp. It injects the error into the completion handler, so that we can reproduce the real one needs more memory than a test machine can assume. Reverting either source change fails the matching test. Full suite passes (267 cases).Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changescc @zcbenz