Skip to content

fix(fusion): drop pre-fusion ExLlamaV2 operand snapshots on rebind (#4240) - #4272

Merged
Qubitium merged 1 commit into
mainfrom
fix/4240-exllamav2-qfast-retention
Nov 16, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/4240-exllamav2-qfast-retention

Conversation

@ZX-ModelCloud

Copy link
Copy Markdown
Collaborator

TL;DR

Fusion rebinds every source projection to views of one concatenated packed
allocation, but ExLlamaV2's per-call operand snapshot (_q_fast) still pointed at
the standalone tensors each source held before fusion. It also survived the
copy.copy that builds the fused executor. Net effect: the replaced packed
allocations were never released — 544 KiB per gate/up group for a 512x1024
projection pair. FusionKernelSpec.release_operand_snapshots() clears the
snapshot on the sources and the executor once the views are bound; descriptors
stay live and direct source calls rebuild a snapshot on the fused views.
3 files of code/docs, 1 new test.

Fixes #4240.

1. The problem

_ensure_q_handle stores _q_fast = (tensors, signatures) to skip rebuilding
the native q-matrix when no quoted tensor changed
(gptqmodel/nn_modules/qlinear/exllamav2.py:282). That tuple is a strong
reference list, so it owns whatever qweight/qzeros/scales/g_idx were
bound when the snapshot was taken.

flowchart TD
    WARM["source forward -> _ensure_q_handle<br/>snapshot binds standalone tensors"] --> SHARE["_share_operands<br/>rebind source to fused views"]
    SHARE --> COPY["FusionKernelSpec.combine<br/>copy.copy first source"]
    COPY --> STALE["executor inherits the source snapshot"]
    SHARE --> STALE2["source keeps its own snapshot"]
    STALE --> LEAK["standalone packed allocation stays referenced"]
    STALE2 --> LEAK
    LEAK --> FOREVER["still resident after every fused forward"]
    style LEAK fill:#fff1f0,stroke:#cf1322
    style FOREVER fill:#fff1f0,stroke:#cf1322
Loading

The report names the source-side snapshot. The executor-side one is a second
distinct retain: clearing only the sources leaves the first source's
standalone allocation alive through group.packed._q_fast. Both are required
for the allocation to go away.

Mechanism origin/main This change
Source snapshot after _share_operands kept, fusion.py:444 rebinds tensors only cleared, fusion_kernels.py:283
Executor snapshot after combine inherited via copy.copy, fusion_kernels.py:222 cleared by the same call from fusion.py:470
Direct source call afterwards would need a descriptor on stale storage descriptor retained; cold path rebuilds on fused views

2. The new flow

flowchart TD
    INSTALL["_FusionGroup.install"] --> REFRESH["_refresh"]
    REFRESH --> OPERAND["_operand(key) -> kernel.combine"]
    OPERAND --> SHARE["_share_operands:<br/>rebind sources to fused views, rebind descriptors"]
    SHARE --> DROP["FusionKernelSpec.release_operand_snapshots<br/>(sources + fused executor)"]
    DROP --> INV["invalidate: retire guard, release old executor"]
    INV --> READY["packed executor + guard installed"]
    READY --> TOKEN["fused token path: guard -> packed_forward"]
    READY --> DIRECT["direct source call"]
    DIRECT --> COLD["_ensure_q_handle slow path:<br/>rebuild snapshot on fused views"]
    COLD --> TOKEN
    style DROP fill:#e6f4ff,stroke:#0958d9
Loading

release_operand_snapshots is layout-gated on self.layout == "exllamav2" and
touches only _q_fast; q_handle, q_tensors, and the
q_perm/q_invperm metadata are untouched, so the native descriptor and the
act-order permutation ownership contract are preserved. For every other layout
the call is a no-op.

3. Failure and ambiguity contract

Situation Before After
Fuse, then serve tokens standalone allocations retained for the group's lifetime released; sources alias the fused allocation
Fuse, then call a source directly descriptor valid descriptor valid, snapshot rebuilt on the fused view
clear_weight_cache() on a source invalidates group unchanged
_FusionGroup.remove() clones independent operands, rebinds unchanged (verified by the new test's final parity check)
AWQ ExLlamaV2 sources no _q_fast at all (its own _q_handle_key path) skipped by the vars(module) guard, no attribute created

4. Change inventory

Area File Change
Fusion kernel contract gptqmodel/models/fusion_kernels.py +17: release_operand_snapshots()
Fusion group gptqmodel/models/fusion.py +4: call after _share_operands
Tests tests/module_tree/test_fusion_kernel_backends.py +59: test_exllamav2_fusion_releases_operand_snapshots
Docs docs/exllamav2-descriptor-lifecycle.md flow + before/after row
Docs docs/fusion-planner.md operand-storage paragraph
Docs docs/kernel-dispatch-caching.md snapshot invalidation note

5. Tests / guarantees

Test Invariant pinned
test_exllamav2_fusion_releases_operand_snapshots[fp16, bf16] source qweight weakrefs die on install; both snapshots are None; descriptors survive; fused output equals the pre-fusion baseline; a direct source call matches the fused executor; the rebuilt snapshot aliases the fused storage; remove() restores separate execution
test_native_fusion_shapes_oracle_ownership_and_invalidation (all ExLlamaV2 cases) previously failed with "Original native weight allocations were retained"; now passes
test_exllamav2_rebinds_shuffled_words_without_changing_output, test_exllamav2_act_order_rebind_preserves_permutation, test_exllamav2_fused_checkpoint_reload descriptor lifecycle, act-order permutations, and checkpoint reload are unchanged

6. Validation

  • Full fusion backend suite on an NVIDIA CUDA host: 158 passed, 34 skipped.
  • Same suite on an AMD ROCm host: 89 passed, 103 skipped (ExLlamaV2 is
    CUDA-only and fails fast by design; no layout change on this path).
  • tests/test_kernel_dispatch_caching.py: 19 passed.
  • ruff check clean on all three touched Python files.
  • Retention measured on a Llama-shaped gate/up group (512 in, 1024 out per
    projection, INT4 packed words): the two sources' standalone
    qweight+qzeros+scales total 544 KiB, all released after install.
    Fused output parity is bit-identical for the same step (max abs diff 0.0).
  • tests/test_kernel_audit_regressions.py (21) and
    tests/module_tree/test_llama_fusion.py (4) fail identically with this patch
    stashed; they are pre-existing and unrelated.

7. Reviewer notes

  • The alternative — clearing _q_fast inside _FusionGroup directly — was
    rejected: it reaches into kernel-private state from the planner, and it would
    miss any future layout with the same contract. The hook lives on
    FusionKernelSpec, next to _set_exllamav2_handle_key.
  • The snapshot is a pure cache: dropping it costs one tensor_cache_key pass on
    the next direct source call, never on the fused token path. The guard still
    makes the fused path allocation-free.

…4240)

`ExllamaV2Linear._ensure_q_handle` keeps `_q_fast` as the per-call change
detector for the packed tensors it bound into the native matrix. Fusion
rebinds every source to disjoint views of the concatenated allocation and
then `FusionKernelSpec.combine` shallow-copies the first source into the
fused executor, so both sides kept a snapshot of the standalone
`qweight`/`qzeros`/`scales`. Those allocations therefore outlived every
fused forward: 544 KiB per gate/up group for a 512x1024 projection pair.

Clear the snapshot on the sources and on the copied executor after
`_share_operands` has rebound them. Descriptors stay live for direct source
calls, whose cold path rebuilds a snapshot on the fused views, so no
separate storage returns.

Tests: new `test_exllamav2_fusion_releases_operand_snapshots` pins the
weakref lifetime of the source weights, the descriptor survival, direct
call parity, and that rebuilt snapshots alias the fused allocation. The
full fusion backend suite passes (158 passed, 34 skipped).
@Qubitium
Qubitium merged commit 365fb6d into main Nov 16, 2026
7 of 9 checks passed
@Qubitium
Qubitium deleted the fix/4240-exllamav2-qfast-retention branch November 16, 2026 06:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG][fusion] Fused ExLlamaV2 projections keep the unfused packed weights alive via _q_fast

2 participants