Repository navigation
fix(fusion): drop pre-fusion ExLlamaV2 operand snapshots on rebind (#4240) - #4272
Merged
Merged
Conversation
…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).
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.
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 atthe standalone tensors each source held before fusion. It also survived the
copy.copythat builds the fused executor. Net effect: the replaced packedallocations were never released — 544 KiB per gate/up group for a 512x1024
projection pair.
FusionKernelSpec.release_operand_snapshots()clears thesnapshot 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_handlestores_q_fast = (tensors, signatures)to skip rebuildingthe native q-matrix when no quoted tensor changed
(
gptqmodel/nn_modules/qlinear/exllamav2.py:282). That tuple is a strongreference list, so it owns whatever
qweight/qzeros/scales/g_idxwerebound 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:#cf1322The 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 requiredfor the allocation to go away.
_share_operandsfusion.py:444rebinds tensors onlyfusion_kernels.py:283combinecopy.copy,fusion_kernels.py:222fusion.py:4702. 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:#0958d9release_operand_snapshotsis layout-gated onself.layout == "exllamav2"andtouches only
_q_fast;q_handle,q_tensors, and theq_perm/q_invpermmetadata are untouched, so the native descriptor and theact-order permutation ownership contract are preserved. For every other layout
the call is a no-op.
3. Failure and ambiguity contract
clear_weight_cache()on a source_FusionGroup.remove()_q_fastat all (its own_q_handle_keypath)vars(module)guard, no attribute created4. Change inventory
gptqmodel/models/fusion_kernels.pyrelease_operand_snapshots()gptqmodel/models/fusion.py_share_operandstests/module_tree/test_fusion_kernel_backends.pytest_exllamav2_fusion_releases_operand_snapshotsdocs/exllamav2-descriptor-lifecycle.mddocs/fusion-planner.mddocs/kernel-dispatch-caching.md5. Tests / guarantees
test_exllamav2_fusion_releases_operand_snapshots[fp16, bf16]qweightweakrefs die on install; both snapshots areNone; 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 executiontest_native_fusion_shapes_oracle_ownership_and_invalidation(all ExLlamaV2 cases)test_exllamav2_rebinds_shuffled_words_without_changing_output,test_exllamav2_act_order_rebind_preserves_permutation,test_exllamav2_fused_checkpoint_reload6. Validation
CUDA-only and fails fast by design; no layout change on this path).
tests/test_kernel_dispatch_caching.py: 19 passed.ruff checkclean on all three touched Python files.projection, INT4 packed words): the two sources' standalone
qweight+qzeros+scalestotal 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) andtests/module_tree/test_llama_fusion.py(4) fail identically with this patchstashed; they are pre-existing and unrelated.
7. Reviewer notes
_q_fastinside_FusionGroupdirectly — wasrejected: 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.tensor_cache_keypass onthe next direct source call, never on the fused token path. The guard still
makes the fused path allocation-free.