Repository navigation
fix(mlx-sys): native errors reach the Rust turn error; GPU OOM is classified - #379
Conversation
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
|
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
…ssified
A native MLX/Metal failure inside a lazy eval lost its text: host reads
(`item_at_*`) reported "Failed to extract int32 scalar", `mlx_synchronize`
only printed to stderr, and the swallowing `MxArray::eval` at the first
sample, at paged prefill chunk boundaries and at the scheduler's decode
sample dropped it. MLX throws a failed GPU command's error once, so decode
went on with what the command wrote. The scheduler's GPU OOM check (Splash
`NativeRuntime::executionFailed`) never matched.
- A per-thread native error slot: `ensure_readable` and `mlx_synchronize`
fill it, each `item_at_*`/synchronize entry clears it first, and
`mlx_take_native_error` hands it to Rust and clears it.
- `item_at_{int32,uint32,float32}` carry it: "MLX eval failed in
array_item_at_int32: <MLX text>".
- `mlx_synchronize` returns a status; `synchronize_and_clear_cache_checked`
fails with the text. The hybrid scheduler uses it after a non-final
prefill slice, a replay slice and the first sample, and evaluates the
first sample with `eval_arrays_with_context`, so the row fails and
`note_gpu_oom` counts it.
- Scheduler decode: the batched greedy sample, the greedy verify, the
forced verify row and the DSpark greedy accept use
`eval_arrays_with_context`. A failed greedy eval fails the wave's rows
instead of falling back to per-row sampling (a shape miss still falls
back), and a GPU OOM at the sampling wave fails every row instead of a
per-row retry.
- Qwen3.5 dense and MoE paged prefill (text and VLM) evaluate the chunk
residual with `eval_arrays_with_context`, so a failure there ends the
prefill instead of the next chunk running on what it wrote.
- Test hook `mlx_test_fail_next_native_call` (no production caller) makes
the next native eval, read or synchronize throw once.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
4ab1d29 to
fbb38ec
Compare
Base:
fix/main-ci-after-344(#377, the main CI fix). Merge #377 first; this PR then holds one commit.What
A native MLX/Metal error raised inside a lazy eval (kernel or pipeline failure, GPU OOM, command-buffer error) now reaches the Rust error that the turn returns, with MLX's own text. The scheduler's GPU-OOM check (
is_gpu_out_of_memory→note_gpu_oom, SplashNativeRuntime::executionFailed) can then match it, fail the rowmetal_out_of_memory:and reset the ledger. This covers prefill, the first sample and every scheduler decode step.Why
P0 (code-only probe, a throw inside
PagedPrefillAttention::eval_gpu) showed the text survived only when the paged prefill layer barrier caught it. On the sample path and on every decode token read it was lost:ensure_readablecaught the throw and returnedfalse.item_at_int32then reportedFailed to extract int32 scalar (array must have size=1), so the OOM check missed it and the ledger count stayed 0.mlx_synchronizeonly printed to stderr. MLX throws a failed GPU command's error once (at the next encode, synchronize or event wait,mlx/error.h) and then clears it, so a dropped error is gone and the turn keeps running on what the failed command wrote.MxArray::eval(mlx_array_eval) at the first sample, at the paged prefill chunk boundary and at the scheduler's decode sample (batch_greedy_tokens, the greedy MTP verify) swallows a wait-time error the same way. The decode epilogues then also fell back to per-row sampling on any error, which reads what the failed command wrote.Change
Scheduler decode step after this PR:
crates/mlx-sys: the slot inmlx_common.h;mlx_synchronizereturns a status;mlx_take_native_error; the test hookmlx_test_fail_next_native_call(no production caller; the next eval, read or synchronize on this thread throws once).array/data.rs:item_at_{int32,uint32,float32}attach the slot's text.array/memory.rs:synchronize_and_clear_cache_checked(context). The publicMxArray::evalsignature is unchanged; the uncheckedsynchronize*helpers keep their old behaviour for every other caller.engine/hybrid_scheduler.rs: the synchronize after a non-final prefill slice, after a replay slice and after the first sample is checked. The first sample is evaluated witheval_arrays_with_context. A failed batched greedy eval fails each batched row with the native text.engine/batch_sampling.rs:batch_greedy_tokens_or_fallbackreturnsResult<Option<_>>. A shape or graph miss still falls back to per-row sampling (Ok(None)); a failed eval or read is an error. Inevaluate_pending_samples, a GPU OOM at the wave eval fails every row instead of the per-row retry; other errors keep the retry.engine/hybrid_scheduler/speculative.rs: the greedy verify eval and the forced-row eval are checked; a failed greedy verify fails every row of the wave (tickets still commit with keep=0) instead of the per-row fallback.engine/dspark_turn.rs:accept_dspark_proposal's greedy argmax eval is checked (one line). It is reached on the scheduler decode path when the wave is mixed; the whole-turn DSpark caller shares it and already handles itsErr.models/qwen3_5{,_moe}/paged_forward.rs: the non-final chunk residual eval (text and VLM prefill) useseval_arrays_with_context. This closes the #344 gap where the error was only logged.Per-step cost: the decode evals were already one sync eval each; the checked call adds the error test and a one-element handle
Vec.Error text, before → after
Failed to extract int32 scalar (array must have size=1)metal_out_of_memory: a GPU command exhausted Metal memory (MLX eval failed in scheduled_prefill_sample while materializing 1 arrays: [malloc] Unable to allocate 1073741824 bytes.); …metal_out_of_memory: … (MLX eval failed in scheduled_greedy_sampling while materializing 1 arrays: [malloc] …); …MLX eval failed in scheduled_sampling while materializing N arrays: [malloc] …MLX eval failed in scheduled_verify_greedy while materializing 1 arrays: <MLX text>item_at_int32)Failed to extract int32 scalar (array must have size=1)MLX eval failed in array_item_at_int32: [malloc] Unable to allocate …metal_out_of_memory: … (MLX synchronize failed in scheduled_prefill_sample_sync: [malloc] …); …MLX eval failed in paged_prefill_chunk while materializing 1 arrays: <MLX text>Tests and mutations
Each mutation was a runtime switch in one test build, checked red with the switch on and green with it off, then removed (
git grep MLX_MUT= 0). A mutation at a checked eval or synchronize still calls the checked function and only drops its result, so the injected throw is consumed exactly as a swallowing caller would.array::data::native_error_tests::a_failed_read_carries_the_native_errorarray::data::native_error_tests::a_stale_native_error_never_attachesitem_at_*does not clear the slot firstarray::data::native_error_tests::a_failed_synchronize_returns_the_native_errorengine::hybrid_scheduler::tests::a_native_gpu_oom_at_the_first_sample_fails_the_turn(sample eval + synchronize; checks themetal_out_of_memory:reply and ledger count 1)engine::hybrid_scheduler::tests::a_native_gpu_oom_at_the_decode_sample_fails_the_turn(batched greedy decode;metal_out_of_memory:reply, ledger count 1)engine::batch_sampling::tests::a_native_error_at_the_greedy_eval_fails_the_waveengine::batch_sampling::tests::a_gpu_oom_at_the_sampling_wave_fails_every_rowengine::hybrid_scheduler::speculative::tests::a_native_error_at_the_greedy_verify_fails_the_row(Qwen3.5 native MTP)models::qwen3_5::model::paged_construction_tests::a_failed_dense_prefill_chunk_eval_fails_the_prefillmodels::qwen3_5_moe::paged_forward::tests::a_failed_moe_prefill_chunk_eval_fails_the_prefillFault injection uses
mlx_test_fail_next_native_call. It follows the repo's existingbridge_testingper-thread test-flag pattern, so it is a permanent hook, not anMLX_MUTswitch. In Rust,fail_native_call_at(context, text)arms it only for the checked eval or synchronize with that context. That Rust code is#[cfg(test)]only. The C++ check on the hot path is a single empty-string test of a thread-local.Gates
cargo fmt --all --checkcargo clippy --workspace --all-targets -- -D warningsmlx-core --lib(full, one build,--test-threads=1)cargo test -p mlx-paged-attn --lib --testscargo test --workspace --test '*' --no-fail-fast#[napi]surfaceindex.d.ctschange)MLX_PAGED_PREFILL_CHUNK_SIZE=64, whole-turn andMLX_CONTINUOUS_BATCHING=1Not covered
[METAL] Command buffer execution failed: Insufficient Memory) is not reproduced. The tests inject the throw at the native entry point, which is where MLX raises such an error. MLX's ordering (stored on the encoder, thrown once at the next encode, synchronize or event wait) is from readingdevice.cppanderror.h.MxArray::evalsites (MTP prompt-hidden evals, whole-turnpaged_turn/mtp_turn/dspark_turnsynchronizes, other families' whole-turn decode) are unchanged. Only the scheduler acts on a synchronize status.to_float32/int32/uint32and theprobability_*readers do not attach the text, since they are not on the token path. The slot never leaks into a lateritem_at_*or synchronize because both clear it on entry.Review notes
array::is_available()→detach_event()→event.check_error()(mlx/array.h:417, upstream #3742) throws the stored error; the completion handler stores it (device.cpp:766) before signaling (:773). So the checked eval returns Err.MxArray::eval). Upstream's blockingEvent::waithas the same window.🤖 Generated with Claude Code