Repository navigation
fix(exllamav3): synchronize the EXL3 native dispatch caches (#3951) - #3966
Merged
Merged
Conversation
`select_exl3_gemm_kernel_tuned` fills a process-wide `std::map` and `exl3_gemm` a per-device `std::set` on first use. Neither was synchronized, so two threads under `PYTHON_GIL=0` could read and write the same container at once: a C++ data race that can corrupt the container or pick the wrong dispatch result. The per-device `OptionalCUDAGuard` only selects the context. - `tuning_cache` is guarded by a `std::shared_mutex`. The steady state is a shared lookup, so concurrent dispatches still run in parallel; the exclusive lock is taken only on a miss, to build and publish the entry. `emplace` returns the node iterator, so the second `find` is gone. - `forced_result` is now `thread_local`: it is rewritten per call, so two callers forcing a shape would otherwise overwrite each other's record. - `kernel_attr_set[device]` is guarded the same way, and the variant is published only after `cudaFuncSetAttribute` and its error check pass, so a failed setup is retried instead of being recorded as done. `std::map`/`std::set` nodes are stable and nothing erases, so pointers returned from under the lock stay valid. Adds `tests/test_exl3_dispatch_concurrency.py` (8 threads, barrier, fresh process, `PYTHON_GIL=0`, four shapes, reconstruct + hgemm oracle) and `docs/exl3-native-dispatch-caches.md` with the before/after flow.
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.
Fixes #3951
TL;DR
select_exl3_gemm_kernel_tunedandexl3_gemmfill two process-wide host containers on first use with no synchronization, soPYTHON_GIL=0callers can race them. This adds a shared/exclusive lock pair (parallel reads, exclusive publish) to both, makes the forced-shape record thread-local, and publishes the kernel-attribute entry only after the setup call succeeds. Net diff: 4 files, +222/-13.1. The problem
Old flow, with the unsynchronized accesses marked:
flowchart TD A["exl3_gemm: A, B, C"] --> B["select_exl3_gemm_kernel_tuned"] B --> C{"force_shape_idx is positive?"} C -->|yes| D["write process-wide forced_result"] D --> E["return pointer to the shared record"] C -->|no| F["tuning_cache.find key, no lock"] F --> G{"found?"} G -->|no| H["sample packed table, then tuning_cache[key] assignment, no lock"] H --> I["tuning_cache.find key again, no lock"] G -->|yes| I I --> J["kernel_attr_set device find, no lock"] J --> K{"attribute already set?"} K -->|no| L["cudaFuncSetAttribute, then insert into the set, no lock"] K -->|yes| M["launch kernel"] L --> M classDef race fill:#fdd,stroke:#b00,color:#700 class D,F,H,I,J,L racegptqmodel_ext/exllamav3/quant/exl3_kernel_map.cu:29declared,:173read,:193written,:201re-readgptqmodel_ext/exllamav3/quant/exl3_kernel_map.cu:30, written per call at:157-161gptqmodel_ext/exllamav3/quant/exl3_gemm.cu:29, read and inserted at:119-127Two first-use GEMMs on different Python threads can therefore execute a
findand an insert on the samestd::mapconcurrently — undefined behaviour that can corrupt the container or return the wrong dispatch result. The forced-shape branch additionally rewrites one sharedTResultthat another thread may still be reading through the returned pointer.2. The new flow
flowchart TD A["exl3_gemm: A, B, C"] --> B["select_exl3_gemm_kernel_tuned"] B --> C{"force_shape_idx is positive?"} C -->|yes| D["write thread-local forced_result"] D --> E["return this thread's record"] C -->|no| F["shared_lock: tuning_cache.find key"] F --> G{"found?"} G -->|yes| H["return the stable map node pointer"] G -->|no| I["unique_lock: re-check, sample table, emplace the record"] I --> H H --> J["exl3_gemm: shared_lock: kernel_attr_set device lookup"] J --> K{"attribute already set?"} K -->|yes| M["launch kernel"] K -->|no| L["unique_lock: re-check, cudaFuncSetAttribute, insert"] L --> M style I fill:#dfd,stroke:#060 style L fill:#dfd,stroke:#060std::mapandstd::setnodes are stable and nothing erases from either container, so a pointer returned under the lock stays valid after it is released. The steady state is a shared lock, so cached dispatches still run in parallel — the exclusive lock is entered only to publish a new entry, which is the one-time initialization the issue asks for.exl3_gemmuses the same shape forkernel_attr_set[device], with one extra ordering fix: the entry is inserted only aftercudaFuncSetAttributeand itscuda_checksucceed, so a failed setup is retried by the next launch instead of being remembered as done.3. Failure and ambiguity contract
findand insert on onestd::mapunique_lock, readers undershared_lockfinddecidesforced_resultoverwrites the other'sthread_localrecordcudaFuncSetAttributefails4. Change inventory
gptqmodel_ext/exllamav3/quant/exl3_kernel_map.cushared_mutexaroundtuning_cache;thread_local forced_result;emplaceiterator replaces the secondfindgptqmodel_ext/exllamav3/quant/exl3_gemm.cushared_mutexaroundkernel_attr_set[device]; publish only after a successful attribute calltests/test_exl3_dispatch_concurrency.pydocs/exl3-native-dispatch-caches.md5. Tests
6. Validation
Hardware: RTX 4090 (
GPU-50a20676-a7a4-8bb3-eb3d-f191fdc9fa42), torch 2.16.0.dev20261102+cu130, nvcc 13.4, Python 3.14.10t. The EXL3 extension builds and loads on this host, so the native path is exercised rather than mocked.pytest tests/test_exl3_dispatch_concurrency.py -qpytestover the 14 importabletests/test_exl3_*.pyplustests/test_exllamav3.py,tests/test_exllamav3_cpu.py,tests/test_exllamav3_jit.pyPYTHON_GIL=0, 16 threads on a barrier, 16 shapes, 6 fresh processesruff check gptqmodel docs/eora tests setup.pyExtension rebuilds were confirmed by a new cache key under
~/.cache/gptqmodel/torch_extensions/exllamav3/for each source revision tested.Six other
tests/test_exl3_*.pyfiles cannot be collected on this host because the CUDA venv lacksxxhash; that gap predates this change.7. Reviewer notes — measured before/after
Honest limitation, also recorded in
docs/exl3-native-dispatch-caches.md: the pre-fix race is undefined behaviour and does not reproduce deterministically here. The new test passes against the unfixed sources as well, so it is a regression guard on the observable contract (no crash, correct results under a cold concurrent burst), not a reproduction of a captured corruption. The fix is justified by the C++ memory model: both containers were read and written without synchronization from threads thatPYTHON_GIL=0allows to run concurrently.The native tensor-core GEMM is NVIDIA-only. On ROCm the kernel tables are null and
exl3_gemmfails closed with an actionable message, so the test skips there.