Skip to content

fix: stop parallel MLX lib test aborts and lint examples in CI - #3674

Merged
inureyes merged 13 commits into
mainfrom
fix/3660-mlx-parallel-test-guard
Dec 2, 2026
Merged

inureyes merged 13 commits into
mainfrom
fix/3660-mlx-parallel-test-guard

Conversation

@inureyes

@inureyes inureyes commented Dec 2, 2026

Copy link
Copy Markdown
Member

Summary

Parallel cargo test --lib aborted with "invalid memory registered when calling ::eval_gpu". Guarding every MLX-evaluating test was not enough: the abort kept firing with only one thread inside MLX (gdb thread dumps at the abort showed the other test threads in CPU code). The root cause is a race in MLX's debug-only CUDA allocation tracker, fixed here in an overlay. The tests are also guarded and a coverage scan keeps them that way.

Root cause (MLX CUDA allocator)

In builds without NDEBUG (a plain cargo test), move_to_unified_memory and move_to_device_memory freed the old pointer and only then called swap_allocation. In between, another thread could be handed the freed address and track it; the late swap then moved that thread's live entry to the new pointer, and that thread's next verify_graph aborted although no memory was invalid. Building any array of a page or more from host data goes through move_to_device_memory, so any test creating arrays could trigger it. New overlay overlays/cuda/mlx/backend/cuda/allocator.cpp (base 5a48f9cc) moves the swap ahead of the free. Release and test-fast builds do not track allocations and were never affected, which is why the gates never saw it.

Guards and scan

  • 1,478 lib tests that evaluate or read back MLX arrays take mlx_test_guard() as their first statement, including every test named in the issue. Helpers that took the device lock or the guard after fixture weights were built now take the guard themselves or leave it to a caller that takes it first. The sanitize tests now take the guard before env_lock() everywhere, so the two locks are always acquired in the same order.
  • tests/mlx_test_guard_coverage.rs adds mlx_evaluating_lib_tests_take_mlx_test_guard, reusing parse_fns/body_end. Triggers: eval/.eval()/try_eval/eval_all/async_eval, item_*, raw-byte readbacks and array_to_vec_f32, followed through test helpers in the same file and uniquely named helpers in *test_support*/testing.rs. The guard (or a guard-taking helper) must come before the first trigger. Method calls, associated functions, comments, strings and production functions are not matched by name, so CPU-only tests stay out. The allowlist is empty. The self-test is eval_scanner_flags_readbacks_and_accepts_guard_helpers.

Examples lint

examples/xla_kimi_linear_oracle.rs was already fixed by #3636. cargo clippy -p mlxcel --lib --tests --examples is clean, and the incremental cost is about a second because examples reuse the lib check and feature-gated ones are skipped, so the CI clippy step now passes --examples.

Fixes for main breakage

These landed on main while this PR was open, and the verification needs them: diffusing_interval settings classification (#3624), the FAMILY_ORDER entry for LLaVA-OneVision (#3651), two clippy lints (#3654, #3655), and guards for two new tests (#3654, #3657).

Test plan

  • Before the fix: gpu-lock run --tag mlx-parallel-tests -- env RUST_MIN_STACK=16777216 cargo test --features cuda --lib -- --test-threads=4 aborted (AddMM/AsType/Matmul eval_gpu) at base and in 4 runs with guards only.
  • After: 5 of 5 consecutive runs passed, each 14080 passed; 0 failed; 272 ignored (about 550 s), no aborts. One confirmation run after the final rebase onto main also passed (14098 passed; 0 failed; 272 ignored).
  • cargo test --test mlx_test_guard_coverage, cargo test --bin mlxcel family_order, cargo fmt --check, cargo clippy -p mlxcel --lib --tests --examples -- -D warnings, and the scripts/ci contract checks (including the WebUI contract).

The issue's command omits --features cuda. Without it MLX builds with no GPU backend, so eval_gpu and the allocator tracker never run and the abort cannot occur. All runs above add --features cuda.

Closes #3660

#3624 added ServerConfig::diffusing_interval without a runtime-settings classification, so server_config_schema_classifies_all_117_fields fails on main (118 declared fields, 117 classified) and every settings-schema test that walks the read-only table panics on the unknown field.

Classify it as read-only with the worker reason (the diffusion worker takes the interval at spawn), expose its startup value as an integer, list it in the read-only field fixture, and give the WebUI a startup label.

Refs #3660
… buffer

In builds without NDEBUG, MLX's CUDA allocator tracks live allocations so CommandEncoder::verify_graph can reject a graph that registered freed memory. move_to_unified_memory and move_to_device_memory freed the old pointer and only then called swap_allocation. Between the two, another thread could be handed the freed address and track it, and the late swap then moved that thread's live entry to the new pointer. Its next verify_graph aborted the process with "invalid memory registered when calling <Primitive>::eval_gpu" although no memory was invalid. Building an array of a page or more from host data takes this path, so parallel lib tests hit it even when only one thread was evaluating.

Overlay mlx/backend/cuda/allocator.cpp from the pinned commit with the swap moved ahead of the free. Release builds do not track allocations and are unaffected.

Refs #3660
Lib tests that evaluate MLX arrays or read them back now take mlx_test_guard() as their first statement, so they no longer run concurrently with other GPU tests under cargo test's default thread count. Helpers that took the default-device lock or the guard after fixture weights were built (merge and host-preprocessor cpu_device, switch_layers metal_kernel_or_skip, dinov2 attention_routing, the pipeline last-row helper) now take the guard themselves or leave it to a caller that takes it first, and the sanitize tests take the guard before the env lock in every test.

tests/mlx_test_guard_coverage.rs gains a second scan reusing parse_fns, body_end and the shared walker: a lib test that reaches an eval or host readback call, directly, through a test helper in the same file, or through a uniquely named helper in a test-support module, must take the guard before the first such call. Method and associated-function calls, comments, strings and production functions are not followed by name, which keeps CPU-only tests out.

The CI clippy step adds --examples; examples reuse the lib's check output and feature-gated ones are skipped.

Refs #3660
#3651 added the LLaVA-OneVision family without a FAMILY_ORDER entry, so `cargo test --bin mlxcel family_order` fails on main. Place it with the other VLM families, ahead of the catch-all.

Refs #3660
The coverage scan flags two tests that landed on main while this branch was open: the Mistral contrastive preprocessing test and the MiniCPM-V 4.7 packed-embedding test both read arrays back without mlx_test_guard().

Refs #3660
`cargo clippy -p mlxcel --lib --tests -- -D warnings` fails on main: #3654 wrote two CLIP std constants with more digits than f32 holds (clippy::excessive_precision), and #3655 multiplies by one in a token count (clippy::identity_op). Truncate the constants to the digits f32 keeps (the values are bit-identical) and drop the factor.

Refs #3660
@inureyes inureyes added status:review Under review type:bug Bug fixes, error corrections, or issue resolutions priority:medium Medium priority area:models Model architectures, weights, loading, metadata labels Dec 2, 2026
#3673 added MLXCEL_TEST_QWEN35_MTP_BASE in the qwen3_5_mtp drafter tests without documenting or allowlisting it, so the env-var-docs gate fails on main. It is the checkpoint path of an opt-in real-model test, the same as its MLXCEL_TEST_QWEN35_MTP_DRAFTER sibling, so it joins that allowlist group.

Refs #3660
…xture

The checked string fixture in tests/fixtures/webui/strings.json must list every key the feature string modules declare. This branch added settings.startup.diffusing_interval and #3676 added settings.startup.swa_full on main, but neither entry reached the fixture, so the WebUI drift and shell tests failed on the merged tree. Both entries now sit next to the same neighbours as in startup-strings.ts. The full WebUI vitest suite (858 tests) and the WebUI contract check pass.

Refs #3660
The embedded WebUI bundle under src/webui/assets carries a source digest, and the diffusing_interval startup string changed the WebUI sources, so build_bundle.py --verify reported a stale manifest. Rebuilt with node 26.5.1 and pnpm 12.23.0 as CI pins them; build_bundle.py --verify passes.

Refs #3660
#3678 added two MXFP8 requantization tests that evaluate MLX arrays without taking mlx_test_guard, and the new coverage scan flags both. They now take the guard as their first statement; both pass on GB10 under the CUDA build.

Refs #3660
@inureyes
inureyes merged commit 6eae5aa into main Dec 2, 2026
37 of 38 checks passed
@inureyes inureyes added status:done Completed and removed status:review Under review labels Dec 2, 2026
@inureyes
inureyes deleted the fix/3660-mlx-parallel-test-guard branch December 2, 2026 03:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:models Model architectures, weights, loading, metadata priority:medium Medium priority status:done Completed type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(test): MLX unit tests abort under parallel test threads, plus example clippy lint

1 participant