Skip to content

fix(exl3): publish the native handle under a per-module lock (#4793) - #4803

Merged
Qubitium merged 1 commit into
mainfrom
fix/issue-4793-exl3-inner-lock
Nov 17, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/issue-4793-exl3-inner-lock

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4793.

Problem

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 - with no lock:

self._drop_inner()          # self._inner = None, previous handle unloaded
...
self._inner = LinearEXL3(...)
self._inner_signature = signature
return self._inner          # another thread may have nulled this

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 None another thread had just published:

AttributeError: 'NoneType' object has no attribute 'forward'

Measured by the reporter at ~2.5% of cold calls with T=8 under PYTHON_GIL=0, and the same happens for the first
forward after clear_weight_cache(). _current_signature() also rebinds the inference-mode metadata buffers, so the
signature was changing while another thread compared it.

Fix

  • _ensure_inner, _ensure_torch_inner and _drop_inner serialize on a per-module RLock (_inner_guard):
    one cold start builds exactly one handle and every caller receives that same object.
  • _ensure_inner returns the handle it built instead of re-reading self._inner.
  • __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. copy.deepcopy and pickle still work (verified).

Per module, not process-wide: _ensure_inner runs on every forward, so a global lock would serialize decode across
GPUs.

Tests (tests/test_exllamav3_inner_handle_4793.py)

The native LinearEXL3 is stubbed (the bug is the Python handle publication, not the kernel), following the
test_exl3_cache_signature.py pattern:

  • test_cold_start_burst_publishes_one_handle - 8 barrier-synchronized cold-start forwards must produce exactly
    one
    build, one live handle, and no errors.
  • test_repeated_clear_then_burst_never_returns_none[5] - the reporter's clear_weight_cache() + burst loop, five
    rounds, one rebuild per round, no exception.

Red on unmodified main (b22245774): both fail - 8 builds per cold start, because every rebuild drops the
handle 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 check
clean.

`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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant