Skip to content

fix(rocm): initialize Device's rocBLAS handle once, under a lock - #2202

Merged
inureyes merged 2 commits into
mainfrom
fix/issue-2198-rocblas-handle-lock
Oct 7, 2026
Merged

inureyes merged 2 commits into
mainfrom
fix/issue-2198-rocblas-handle-lock

Conversation

@inureyes

@inureyes inureyes commented Oct 7, 2026

Copy link
Copy Markdown
Member

Summary

The ROCm overlay's Device created its rocBLAS handle lazily behind a plain bool 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.

What changed

  • device.h, device.cpp: the architecture facts are computed once in the constructor from the hipDeviceProp_t it already reads, and has_native_wmma() and supports_cdna_mfma_gemm() are const getters. The handle is created under std::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-only RocblasLease that binds the stream (throwing on a failed rocblas_set_stream) and holds the mutex until it is destroyed. get_rocblas_handle() is private; set_rocblas_stream(), the uncalled is_rocblas_bf16_available() and the probed flags are gone. The unsupported-arch warning prints at construction, since is_rocblas_available() no longer locks on an unsupported device.
  • matmul.cpp, gemms/rocblas_gemm.cpp, quantized/qmm.hip: all eight rocBLAS lambdas start with auto 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 in matmul.cpp are deleted, and dequant_rocblas_gemm takes the lease after its hipBLASLt attempt so hipBLASLt GEMMs never hold it.
  • tests/rocm_rocblas_handle_concurrency.rs: parent first_gemm_from_many_threads_in_fresh_processes runs the #[ignore] child 10 times in fresh processes with MLX_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 host i32 reference.
  • LOCAL_FIXES item 37 (fork-policy wording, no upstream marking). Nothing under 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.rs per build, 10 child processes each:

  • Overlay from main (fix removed), new test only: 20 of 20 runs failed, 187 of 200 child processes. Across the failing threads, 354 reported outputs left at 0 (a GEMM issued on the null handle, status discarded) and 86 reported a sum one partial GEMM short (a GEMM enqueued on another thread's stream and read before it ran).
  • With this fix: 0 of 20 runs failed, 0 of 200 child processes.
  • 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-rocm matches only device.cpp and device.h.
  • make verify-versions verify-kernel-dtype-keys verify-kernel-port-dispatch verify-llama-compat verify-fmt verify-rocm-overlay: pass.
  • make verify-rocm on 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=1 from this test) and #2197 (rocm::device() map) are the next units of the series.

Closes #2198

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
@inureyes
inureyes merged commit b95af22 into main Oct 7, 2026
1 check passed
@inureyes
inureyes deleted the fix/issue-2198-rocblas-handle-lock branch October 7, 2026 06:47
@inureyes inureyes added status:done Completed type:bug Bug fixes, error corrections, or issue resolutions priority:medium Medium priority area:core mlxcel-core: MLX FFI, primitives, KV cache, layers platform:linux Linux (CUDA / packaging) specific labels Oct 8, 2026
@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: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(rocm): initialize Device's rocBLAS handle once, under a lock

1 participant