Skip to content

fix(rocm): lock the JIT module cache and the HIP event pool - #2196

Merged
inureyes merged 3 commits into
mainfrom
fix/issue-2183-rocm-jit-cache-lock
Oct 7, 2026
Merged

inureyes merged 3 commits into
mainfrom
fix/issue-2183-rocm-jit-cache-lock

Conversation

@inureyes

@inureyes inureyes commented Oct 7, 2026 •

Copy link
Copy Markdown
Member

Summary

rocm::get_jit_module memoised compiled modules in an unlocked process-global map, so two threads that missed one key both compiled through hiprtc and inserted it, and an insert that rehashed the map could run under another thread's find. It now copies upstream CUDA's locking at the pin. Writing the issue's concurrency tests exposed a second unlocked process-global on the same path, HipEventPool in event.hip, which failed those tests in 20 of 20 runs even with the JIT cache locked, so this PR locks it too.

What changed

  • jit_module.cpp: leaked static auto* modules and static auto* mtx = new std::shared_mutex inside get_jit_module; the lookup runs under std::shared_lock, and a miss takes std::unique_lock, repeats the find and calls try_emplace only if the key is still missing. The "<device index>:<name>" key (with fix(rocm): key custom-kernel JIT modules by their generated source #2181's source-hash name from custom_kernel.cpp) and the signature are unchanged. Serializing compiles also stops two StderrSuppressors from swapping fd 2 under each other.
  • jit_module.h: the unused get_jit_module_cache() declaration is gone (the function is too), so nothing reaches the map without the lock. JitModule::get_kernel checks and sets the configured flag under a new std::mutex kernels_mtx_, as the CUDA overlay does (fix(test): mlxcel-core CUDA test binary crashes at the default thread count #1566).
  • event.hip (scope added, see below): HipEventPool::create/release hold a std::mutex around the cache; hipEventCreateWithFlags runs outside it. Upstream CUDA's CudaEventPool instead caches only on the thread that first used the pool; a lock is used here because the server's decode runs on worker threads, which under that rule would create and destroy a hipEvent_t for every event.
  • Test-only bridge fixture rocm_jit_race_probe_array(input, variant) (wrapper mlxcel_core::rocm_faults::jit_race_probe_array): kernel mlxcel_jit_race_probe_v<variant>, out[i] = inp[i] * 2 + 1, one block of n <= 1024 threads; throws off ROCm.
  • tests/rocm_jit_module_concurrency.rs: same_kernel_first_launch_from_many_threads and distinct_kernels_first_launch_while_others_hit_cache as the issue specifies (8 threads, own thread-local streams, barrier, 16 rounds, exact outputs).
  • LOCAL_FIXES items 35 (JIT cache) and 36 (event pool), each "kept in mlxcelverse under the 2026-10-06 fork policy, not proposed there". Nothing under docs/mlxcelverse/upstream/.

Why the event pool is in scope

With only the JIT lock, the new tests failed within a few rounds with hipEventRecord(event_, stream) failed: invalid resource handle. A probe that only launched an already-compiled kernel from 8 threads on their own streams failed the same way, and one thread doing it 40 times passed, which isolates the event pool from the JIT cache. Every HipEvent takes a handle from the pool on construction and returns it on destruction on whatever thread evaluates, so this is reachable in the same multi-worker server configurations as the JIT race.

Verification

On gfx1151 (ROCm), 20 runs of tests/rocm_jit_module_concurrency.rs per build:

  • Both locks (this PR): 0 of 20 failed.
  • Lock removed from get_jit_module, event pool locked: 0 of 20 failed. The tests do not reproduce the JIT race deterministically on this host; for the JIT cache they are a crash guard, not a reproduction.
  • event.hip from main, JIT cache locked: 20 of 20 failed (10 test failures with invalid resource handle, 8 SIGABRT, 2 SIGSEGV).
  • cargo test --profile test-fast --features rocm --test rocm_jit_module_concurrency --test rocm_custom_kernel_jit_key --test rocm_gpu_faults: 7 passed.
  • cargo clippy --features rocm --test rocm_jit_module_concurrency -- -D warnings and cargo clippy -p mlxcel-core --features rocm --lib -- -D warnings: clean.
  • make verify-versions verify-kernel-dtype-keys verify-kernel-port-dispatch verify-llama-compat verify-fmt verify-rocm-overlay and cargo test --test dead_doc_pointers: pass.
  • make verify-rocm on 6b5a4f7 (rebased on 84d3a7b): OK, 154 test suites, 12006 passed, 0 failed, 384 ignored; clippy, smoke, overlay and script gates pass. The later commit c8bf6cf changes only LOCAL_FIXES.md text; make verify-rocm-overlay passes on it.

Not verified: Metal and CUDA are not available on this host. The change touches only ROCm overlay files, a bridge function compiled to a throwing stub off ROCm, and a #![cfg(feature = "rocm")] test file; no Metal or CUDA code path changes.

Follow-ups not done here (all pre-existing, found while working or in review): rocm::device() in device.cpp also does an unlocked find/try_emplace on its device map, though devices are normally created by the first allocation before worker threads start; the event pool's map is a destructible static, so a HipEvent released after static teardown begins would touch a destroyed map, and leaking it as the JIT cache is leaked would fix that; a moved-from HipEvent would return a null handle to the pool (nothing moves one today).

Closes #2183

get_jit_module in the ROCm overlay memoised compiled modules in an unlocked process-global map: two threads that missed one key both compiled through hiprtc and inserted it, and an insert that rehashed the map could run under another thread's find. The server launches from several worker threads, so this was reachable. The cache now uses upstream CUDA's locking at the pin: a leaked map and shared_mutex, the lookup under a shared lock, and on a miss a unique lock, a second find and try_emplace. The unlocked get_jit_module_cache() declaration is removed, and JitModule::get_kernel sets the configured flag under a new kernels_mtx_, as the CUDA overlay does (#1566).

The new concurrency tests still failed with the JIT cache locked: HipEventPool in event.hip recycles hipEvent_t handles through an unlocked static map, so 8 threads evaluating on their own streams failed with "hipEventRecord ... invalid resource handle", SIGABRT or SIGSEGV in 20 of 20 runs. The pool is now locked (LOCAL_FIXES 36); a lock rather than upstream's creation-thread-only cache keeps pooling on the server's worker threads.

Adds the test-only bridge fixture rocm_jit_race_probe_array and tests/rocm_jit_module_concurrency.rs. On gfx1151, 20 runs each: both locks 0 failed; JIT lock removed 0 failed (a crash guard, not a reproduction); event pool lock removed 20 failed. Recorded as LOCAL_FIXES 35 and 36, kept in mlxcelverse under the 2026-10-06 fork policy.

Closes #2183
@inureyes inureyes added status:review Under review 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 7, 2026
Item 35 named the leaked map `cache`, but the code calls it `modules`, and it did not say that a compile under the unique lock also holds up other threads' cache hits until it finishes, which is upstream's behavior and the cost of rejecting per-key locking. Both found in review of #2196.

Refs #2183
Bilingual report on locking the ROCm JIT module cache and the HIP event pool, the 20-run test table, LOCAL_FIXES items 35 and 36, and the follow-ups #2197, #2198 and #2200.
@inureyes
inureyes merged commit 33f60aa into main Oct 7, 2026
26 checks passed
@inureyes
inureyes deleted the fix/issue-2183-rocm-jit-cache-lock branch October 7, 2026 05:32
@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): lock the JIT module cache in get_jit_module

1 participant