Repository navigation
fix(exl3): publish the native handle under a per-module lock (#4793) - #4803
Merged
Merged
Conversation
`ExllamaV3Linear._ensure_inner` dropped the old handle, built a new one, and
then returned `self._inner` - re-reading the attribute instead of returning
the object it had just built. Nothing was locked, so a burst of concurrent
cold-start forwards each rebuilt the handle (every rebuild calls
`_drop_inner()`, which unloads the previous one), and a thread could be handed
the `None` another thread had just published:
AttributeError: 'NoneType' object has no attribute 'forward'
That is ~2.5% of cold calls at T=8 under `PYTHON_GIL=0`, and it also hits the
first forward after `clear_weight_cache()`. `_current_signature()` additionally
rebinds the small inference-mode metadata buffers, so the signature was
changing while another thread compared it.
- `_ensure_inner`, `_ensure_torch_inner` and `_drop_inner` now serialize on a
per-module `RLock` (`_inner_guard`), so one cold start builds exactly one
handle and every caller receives that same object.
- `_ensure_inner` returns the handle it built, never re-reading the attribute.
- `__getstate__`/`__setstate__` drop and recreate the lock (an RLock cannot be
copied or pickled) and forget the native handle, which belongs to the source
process and may be shared with a shallow copy - it is never unloaded there.
The guard is per module, not process-wide: `_ensure_inner` runs on every
forward, so a global lock would serialize decode across GPUs.
Tests: new `tests/test_exllamav3_inner_handle_4793.py` stubs the native
`LinearEXL3` (the bug is the Python handle publication, not the kernel) and
* runs 8 barrier-synchronized cold-start forwards and requires exactly one
build, one live handle and no errors;
* repeats `clear_weight_cache()` + burst five times, requiring one rebuild per
round and no exception.
Red on unmodified main (b222457): both cases fail with 8 builds per cold
start (each rebuild unloads the previous handle). Green here: 2 passed, and
`test_exl3_cache_signature`, `test_exl3_buffer_dtype`,
`test_exllamav3_torch_padding` and `module_tree/test_fusion_kernel_backends`
give 133 passed / 197 skipped. `ruff check` clean.
This was referenced Nov 17, 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.
Fixes #4793.
Problem
ExllamaV3Linear._ensure_innerdropped the old handle, built a new one, and then returnedself._inner-re-reading the attribute instead of returning the object it had just built - with no lock:
So a burst of concurrent cold-start forwards each rebuilt the handle (every rebuild unloads the previous one) and a
thread could be handed the
Noneanother thread had just published:Measured by the reporter at ~2.5% of cold calls with T=8 under
PYTHON_GIL=0, and the same happens for the firstforward after
clear_weight_cache()._current_signature()also rebinds the inference-mode metadata buffers, so thesignature was changing while another thread compared it.
Fix
_ensure_inner,_ensure_torch_innerand_drop_innerserialize on a per-moduleRLock(_inner_guard):one cold start builds exactly one handle and every caller receives that same object.
_ensure_innerreturns the handle it built instead of re-readingself._inner.__getstate__/__setstate__drop and recreate the lock (an RLock cannot be copied or pickled) and forget thenative handle, which belongs to the source process and may be shared with a shallow copy - it is never unloaded
there.
copy.deepcopyandpicklestill work (verified).Per module, not process-wide:
_ensure_innerruns on every forward, so a global lock would serialize decode acrossGPUs.
Tests (
tests/test_exllamav3_inner_handle_4793.py)The native
LinearEXL3is stubbed (the bug is the Python handle publication, not the kernel), following thetest_exl3_cache_signature.pypattern:test_cold_start_burst_publishes_one_handle- 8 barrier-synchronized cold-start forwards must produce exactlyone build, one live handle, and no errors.
test_repeated_clear_then_burst_never_returns_none[5]- the reporter'sclear_weight_cache()+ burst loop, fiverounds, one rebuild per round, no exception.
Red on unmodified
main(b22245774): both fail - 8 builds per cold start, because every rebuild drops thehandle the other threads are about to use.
Green on this branch: 2 passed. Neighbouring suites (
test_exl3_cache_signature,test_exl3_buffer_dtype,test_exllamav3_torch_padding,module_tree/test_fusion_kernel_backends): 133 passed / 197 skipped.ruff checkclean.