Skip to content

fix(rocm): lock the device table in rocm::device() and leak the pools - #2214

Merged
inureyes merged 2 commits into
mainfrom
fix/issue-2197-rocm-device-map-lock
Oct 7, 2026
Merged

inureyes merged 2 commits into
mainfrom
fix/issue-2197-rocm-device-map-lock

Conversation

@inureyes

@inureyes inureyes commented Oct 7, 2026 •

Copy link
Copy Markdown
Member

Summary

rocm::device() kept one Device per HIP index in a function-static map and read it unlocked on every primitive launch, from whichever thread evaluates, with an unlocked try_emplace on a miss; record_stream_error, clear_all_encoders and Device::clear_encoders had the same gaps. The race is latent today (inserts come only from new_stream, which serializes them, and a single-GPU host only ever inserts index 0 before any thread evaluates); it becomes reachable when a first stream on GPU 1 overlaps evaluation on GPU 0 (#486, #488).

What changed

  • device.cpp: the map is a leaked DeviceTable (std::shared_mutex plus the map) behind device_table() and find_device() in an anonymous namespace, so nothing reaches it unlocked. device() takes a shared lock for the lookup and the unique lock with a re-check around a first construction (hipSetDevice, ensure_device_flags, try_emplace, touching only the requested index); a throwing constructor inserts nothing. record_stream_error and clear_all_encoders use the same lock and release it before calling into a Device; Device::clear_encoders destroys encoders after releasing encoders_mtx_.
  • Lazy per-device construction is kept rather than upstream CUDA's eager vector at pin 81ba1c6a: Device::Device binds the device, and a context on an unrequested GPU wedges the discrete GPU's queue on a multi-GPU host.
  • event.hip: HipEventPool leaks its map and mutex, so a release during static teardown is safe and no hipEventDestroy runs at exit.
  • tests/rocm_device_map_concurrency.rs: 10 child processes, 8 threads behind a barrier creating and installing a new thread-local stream per round for 16 rounds and evaluating arange * 2 + 1 four times per stream, every element exact; each child must exit 0, print bound HIP device 0: exactly once and no releasing a HIP handle failed line. A second test (first stream on GPU 1 while 8 threads evaluate on GPU 0) skips below two GPUs.
  • LOCAL_FIXES item 39 with the fork-policy wording.

Verification

On gfx1151 (one GPU, ROCm 7.15), 10 runs of tests/rocm_device_map_concurrency.rs per build, 10 children each: 0 of 10 failed with this change and 0 of 10 with device.cpp restored to main. As the issue predicts, the insert race cannot run on a single-GPU host, so the test did not reproduce it with the fix removed; here it guards the lookup path under concurrent stream creation, one Device per index, and a clean exit. The second test printed its skip line; no multi-GPU host was available.

With multiply_scalar and full_like the test failed 9 of 10 runs with the fix and 10 of 10 without it: an 8-byte scalar read by a kernel while other threads evaluate comes back as a value another scalar held earlier. A probe isolated it to the scalar-pool path (full-length constants and no-host-data variants pass 10 of 10; one stream per thread still fails). Filed as #2213; this test uses full-length constants until it is fixed.

  • tests/rocm_jit_module_concurrency.rs and tests/rocm_gpu_faults.rs: pass. cargo clippy --features rocm --test rocm_device_map_concurrency -- -D warnings: clean. grep -rn get_devices src/lib/mlx-cpp/patches-rocm: empty.
  • make verify-versions verify-kernel-dtype-keys verify-kernel-port-dispatch verify-llama-compat verify-fmt verify-rocm-overlay: pass (39 LOCAL_FIXES entries).
  • make verify-rocm: exit 0 on 05368e2 (this PR's head, on main 454500b): 159 suites, 12116 passed, 0 failed, 389 ignored; versions, dtype-key, port-dispatch, llama-compat, overlay, fmt, workspace clippy and smoke stages pass.

Not verified: Metal and CUDA are not available on this host. The change touches only files under src/lib/mlx-cpp/patches-rocm/ and a #![cfg(feature = "rocm")] test; no Metal or CUDA code path changes.

Follow-up: #2213 (scalar-pool constants under multi-threaded eval). Out of scope per the issue: eager device construction, per-key locking, the CUDA overlay.

Closes #2197

`rocm::device()` kept one `Device` per HIP index in a function-static map with an unlocked `find` and an unlocked `try_emplace` on a miss, and it runs for every primitive `gpu::eval` launches on whichever thread evaluates. The table is now a leaked `DeviceTable` (`std::shared_mutex` plus the map) behind `device_table()` and `find_device()` in an anonymous namespace: a shared lock for the lookup, the unique lock with a re-check around a first construction, so several threads first using one index build one `Device` and a failed construction inserts nothing. `record_stream_error` and `clear_all_encoders` look devices up under the shared lock and release it before calling into a `Device`; `Device::clear_encoders` swaps the encoders out under `encoders_mtx_` and destroys them after releasing it. Lock order is the table, then `encoders_mtx_`. The fork keeps lazy, per-device construction rather than upstream CUDA's eager vector because `Device::Device` binds the device and a context on an unrequested GPU wedges the discrete GPU's queue on a multi-GPU host. `HipEventPool` in `event.hip` leaks its map and mutex the same way, so a release during static teardown is safe and no `hipEventDestroy` runs at exit.

The race is latent on a single-GPU host: inserts come only from `new_stream`, which serializes them, and the only key is 0, so it needs a first stream on GPU 1 while another thread evaluates on GPU 0. `tests/rocm_device_map_concurrency.rs` runs the single-GPU pattern in 10 child processes (8 threads behind a barrier, a new stream per round, `arange * 2 + 1` checked exactly, one `bound HIP device` line per index, no failed handle release at exit) and a second-GPU test that skips below two GPUs. On gfx1151 it passed 10 of 10 runs with the fix and 10 of 10 with `device.cpp` restored to main, so there it is a crash guard, not a reproduction. Its constants are full-length arrays because 8-byte scalar constants read foreign values under this load on main (#2213, found by this test). LOCAL_FIXES item 39.

Closes #2197
@inureyes inureyes added status:review Under review type:bug Bug fixes, error corrections, or issue resolutions priority:low Low priority area:core mlxcel-core: MLX FFI, primitives, KV cache, layers platform:linux Linux (CUDA / packaging) specific status:done Completed and removed status:review Under review labels Oct 7, 2026
@inureyes
inureyes merged commit 1662146 into main Oct 7, 2026
26 checks passed
@inureyes
inureyes deleted the fix/issue-2197-rocm-device-map-lock branch October 7, 2026 12:03
@inureyes inureyes added this to the 0.8 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core mlxcel-core: MLX FFI, primitives, KV cache, layers platform:linux Linux (CUDA / packaging) specific priority:low Low 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(rocm): lock the device map in rocm::device()

1 participant