Repository navigation
fix(rocm): lock the JIT module cache and the HIP event pool - #2196
Merged
Merged
Conversation
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
7 tasks done
This was referenced Oct 7, 2026
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::get_jit_modulememoised 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'sfind. 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,HipEventPoolinevent.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: leakedstatic auto* modulesandstatic auto* mtx = new std::shared_mutexinsideget_jit_module; the lookup runs understd::shared_lock, and a miss takesstd::unique_lock, repeats thefindand callstry_emplaceonly 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 fromcustom_kernel.cpp) and the signature are unchanged. Serializing compiles also stops twoStderrSuppressors from swapping fd 2 under each other.jit_module.h: the unusedget_jit_module_cache()declaration is gone (the function is too), so nothing reaches the map without the lock.JitModule::get_kernelchecks and sets the configured flag under a newstd::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/releasehold astd::mutexaround the cache;hipEventCreateWithFlagsruns outside it. Upstream CUDA'sCudaEventPoolinstead 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 ahipEvent_tfor every event.rocm_jit_race_probe_array(input, variant)(wrappermlxcel_core::rocm_faults::jit_race_probe_array): kernelmlxcel_jit_race_probe_v<variant>,out[i] = inp[i] * 2 + 1, one block ofn <= 1024threads; throws off ROCm.tests/rocm_jit_module_concurrency.rs:same_kernel_first_launch_from_many_threadsanddistinct_kernels_first_launch_while_others_hit_cacheas the issue specifies (8 threads, own thread-local streams, barrier, 16 rounds, exact outputs).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. EveryHipEventtakes 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.rsper build: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.hipfrom main, JIT cache locked: 20 of 20 failed (10 test failures withinvalid 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 warningsandcargo 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-overlayandcargo test --test dead_doc_pointers: pass.make verify-rocmon 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-overlaypasses 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()indevice.cppalso does an unlockedfind/try_emplaceon 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 aHipEventreleased after static teardown begins would touch a destroyed map, and leaking it as the JIT cache is leaked would fix that; a moved-fromHipEventwould return a null handle to the pool (nothing moves one today).Closes #2183