Repository navigation
fix(rocm): lock the device table in rocm::device() and leak the pools - #2214
Merged
Merged
Conversation
`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
5 tasks done
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.
Summary
rocm::device()kept oneDeviceper HIP index in a function-static map and read it unlocked on every primitive launch, from whichever thread evaluates, with an unlockedtry_emplaceon a miss;record_stream_error,clear_all_encodersandDevice::clear_encodershad the same gaps. The race is latent today (inserts come only fromnew_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 leakedDeviceTable(std::shared_mutexplus the map) behinddevice_table()andfind_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_errorandclear_all_encodersuse the same lock and release it before calling into aDevice;Device::clear_encodersdestroys encoders after releasingencoders_mtx_.Device::Devicebinds the device, and a context on an unrequested GPU wedges the discrete GPU's queue on a multi-GPU host.event.hip:HipEventPoolleaks its map and mutex, so a release during static teardown is safe and nohipEventDestroyruns 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 evaluatingarange * 2 + 1four times per stream, every element exact; each child must exit 0, printbound HIP device 0:exactly once and noreleasing a HIP handle failedline. A second test (first stream on GPU 1 while 8 threads evaluate on GPU 0) skips below two GPUs.Verification
On gfx1151 (one GPU, ROCm 7.15), 10 runs of
tests/rocm_device_map_concurrency.rsper build, 10 children each: 0 of 10 failed with this change and 0 of 10 withdevice.cpprestored 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, oneDeviceper index, and a clean exit. The second test printed its skip line; no multi-GPU host was available.With
multiply_scalarandfull_likethe 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.rsandtests/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