Repository navigation
fix(rocm): initialize Device's rocBLAS handle once, under a lock - #2202
Merged
Merged
Conversation
The ROCm overlay's `Device` created its rocBLAS handle lazily behind a plain `bool` that was set before `rocblas_create_handle` ran, so a second thread arriving in that window took back a null handle, and `set_rocblas_stream()` rebound the one shared handle to the caller's stream with no lock before every GEMM, so one thread's GEMM could be enqueued on another thread's stream. The server runs forwards on several worker threads that share one `Device`, each on its own stream, so both windows are reachable. `has_native_wmma()` and `supports_cdna_mfma_gemm()` had the same flag-before-work pattern. `Device` now computes the architecture facts once in its constructor, creates the handle under `rocblas_mtx_` with `rocblas_ready_` published only after a successful create (a failed create throws with the status and is retried), and hands out a `RocblasLease` from `acquire_rocblas(stream)` that binds the stream and holds the mutex until the lambda's last rocBLAS call returns. All eight call sites in `matmul.cpp`, `gemms/rocblas_gemm.cpp` and `quantized/qmm.hip` use it; `get_rocblas_handle()` is private, `set_rocblas_stream()` and the uncalled `is_rocblas_bf16_available()` are gone. LOCAL_FIXES item 37. `tests/rocm_rocblas_handle_concurrency.rs` runs 8 threads' first f32 GEMMs of a fresh process together behind a barrier, 10 child processes per run, every output checked exactly. On gfx1151 without the fix 20 of 20 runs failed (187 of 200 children: outputs left at zero from the null handle, sums off by one partial GEMM from the stream rebind); with it 0 of 20 failed. Closes #2198
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
The ROCm overlay's
Devicecreated its rocBLAS handle lazily behind a plainboolset beforerocblas_create_handleran, so a second thread arriving in that window took back a null handle, andset_rocblas_stream()rebound the one shared handle to the caller's stream with no lock before every GEMM, so one thread's GEMM could be enqueued on another thread's stream. The server runs forwards on several worker threads that share oneDevice, each on its own stream, so both windows are reachable.What changed
device.h,device.cpp: the architecture facts are computed once in the constructor from thehipDeviceProp_tit already reads, andhas_native_wmma()andsupports_cdna_mfma_gemm()areconstgetters. The handle is created understd::mutex rocblas_mtx_,std::atomic<bool> rocblas_ready_is published with release only after a successful create, and a failed create warns once, throws with the status and is retried by the next call.acquire_rocblas(stream)returns a move-onlyRocblasLeasethat binds the stream (throwing on a failedrocblas_set_stream) and holds the mutex until it is destroyed.get_rocblas_handle()is private;set_rocblas_stream(), the uncalledis_rocblas_bf16_available()and the probed flags are gone. The unsupported-arch warning prints at construction, sinceis_rocblas_available()no longer locks on an unsupported device.matmul.cpp,gemms/rocblas_gemm.cpp,quantized/qmm.hip: all eight rocBLAS lambdas start withauto lease = device.acquire_rocblas(stream); rocblas_handle handle = lease.handle();(the alias keeps the argument lists unchanged; the lease outlives every call in the lambda), the two outer handle fetches inmatmul.cppare deleted, anddequant_rocblas_gemmtakes the lease after its hipBLASLt attempt so hipBLASLt GEMMs never hold it.tests/rocm_rocblas_handle_concurrency.rs: parentfirst_gemm_from_many_threads_in_fresh_processesruns the#[ignore]child 10 times in fresh processes withMLX_NO_HIPBLASLT=1; the child starts 8 threads on their own streams behind a barrier, each running 33 f32[64, 128] x [128, 64]matmuls with integer inputs in-4..=4, every output checked exactly against the hosti32reference.docs/mlxcelverse/upstream/.Per-thread handles as upstream CUDA does were rejected (see the issue): rocBLAS binds the stream on the handle, and a per-thread handle would be created on each worker's first GEMM, possibly inside a decode-step stream capture. The lease serializes host-side enqueue only; GEMMs on different streams still overlap on the GPU.
Verification
On gfx1151 (ROCm), 20 runs of
tests/rocm_rocblas_handle_concurrency.rsper build, 10 child processes each:cargo clippy --features rocm --test rocm_rocblas_handle_concurrency -- -D warnings: clean.grep -rn "get_rocblas_handle\|rocblas_set_stream\|set_rocblas_stream" src/lib/mlx-cpp/patches-rocmmatches onlydevice.cppanddevice.h.make verify-versions verify-kernel-dtype-keys verify-kernel-port-dispatch verify-llama-compat verify-fmt verify-rocm-overlay: pass.make verify-rocmon e337e6b (on main 33f60aa): OK, 157 test suites, 12050 passed, 0 failed, 385 ignored; clippy, smoke, overlay and script gates 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-ups: #2200 (hipBLASLt init, workspace and pipe lifetime; it also drops
MLX_NO_HIPBLASLT=1from this test) and #2197 (rocm::device()map) are the next units of the series.Closes #2198