Repository navigation
fix(exllamav2): preserve shuffled words across handle lifecycle - #3771
Merged
Merged
Conversation
Collaborator
Author
|
Reviewed: fixed double shuffle, stale fusion handles, and checkpoint reload. GPU oracle tests pass on RTX 4090 and 5090; neighboring tests pass. |
Qubitium
pushed a commit
that referenced
this pull request
Nov 11, 2026
* test(cpu): unpack _projection checkpoint return The helper gained a fourth return value in #3771, but three audit call sites still unpacked three. Unpack the checkpoint slot so the CPU fused regressions collect and run. * fix(cpu): keep packed buffers when clearing fused caches clear_weight_cache() unconditionally called restore_fused_train_state(). That hook also runs for nn.Module._apply moves and explicit cache clears, so clearing after an in-place metadata edit replaced the live scales and zero points with the checkpoint snapshot and put the checkpoint layout back into the inference kernel. With scales.mul_(2) followed by clear, the fused output was 0.5x the expected value (max_abs 1.390625 against 2.78125), and qweight reverted from int4pack uint8 (64, 64) to int32 (16, 64). Make clear_weight_cache() only drop derived state (ret_idx, scales_and_zeros, torch_fused_op, transform flags) and rebuild it from the live packed buffers. Restore the checkpoint snapshot only on an explicit checkpoint load, export, or training resume. restore_fused_train_state() now also resets the runtime-layout state that its restored tensors invalidate. Apply the same contract to the fused/ATen AWQ clear hooks and document the cache-invalidation versus checkpoint-restore lifecycle. * fix(cpu): re-finalize fused affine state on low-precision moves Keeping the runtime layout across an FP16/BF16 move paired the narrowed scales with affine offsets built from the old precision, silently changing the decoded weights (measured max_abs 0.03125 on a 10x-scale fixture). TorchAtenLinear also rejected its own moved model because _cpu_transform_dtype still named the pre-move activation dtype. Restore the checkpoint snapshot after a low-precision dtype move and let the next forward re-transform. Device moves and moves to FP32 keep the live packed layout, as does an explicit clear. Add regression coverage that compares the moved layer with a checkpoint-layout copy moved before its first transform, using non-symmetric zero codes and non-power-of-two scales. * fix(cpu): re-finalize the checkpoint layout on cross-device moves The runtime int4pack layout is device-specific: the CPU _convert_weight_to_int4pack_for_cpu packing is not the XPU packing, so a cross-device move that kept the live buffers handed the CPU kernel an int32 XPU tensor (RuntimeError: _weight_int4pack_mm_cpu : expect B to be uint8 tensor). Restore the checkpoint snapshot on any device change and let the next forward re-transform; a same-device move to FP32 still keeps the live layout because it only widens the offsets. - Share one decision helper for the four CPU fused/ATen classes instead of four copies of the dtype-only check. - Report the finalized activation dtype, not the widened scales dtype, in the ATen fused dtype-mismatch error. - Cover all four classes in the low-precision move test and add a device-move test; refresh the docs page flow.
This was referenced Nov 14, 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.
TL;DR
Fix ExLlamaV2 GPTQ/AWQ descriptor rebuilds after cache clears, device moves, and checkpoint loads, and keep fusion descriptors alive while source views are bound. Net change: 16 files, +453/−47 lines, including a reference flow page and native GPU tests.
Problem and replacement
clear_weight_cachefreed the descriptor;_ensure_q_handlereconstructed it by shuffling already shuffled words a second time._native_shuffledselects a descriptor-only native bind.gptqmodel/nn_modules/qlinear/exllamav2.py:187,gptqmodel/nn_modules/qlinear/exllamav2_awq.py:194,gptqmodel_ext/exllamav2/cuda/q_matrix.cu:148FusionKernelSpec.combinecloned a fused handle, then cleared it beforerebind; source rebinding could clone a freed handle.combineand_share_operands, then rebinds sources to their views.gptqmodel/models/fusion_kernels.py:169,gptqmodel/models/fusion.py:435gptqmodel/models/fusion.py:611,gptqmodel/nn_modules/qlinear/exllamav2_awq.py:164New flow
flowchart TD RAW["Checkpoint qweight"] --> FINAL["post_init or AWQ _ensure_q_handle: finalize AWQ words"] FINAL --> FIRST["make_q_matrix: shuffle once; set _native_shuffled"] FIRST --> EVENT{"Next event"} EVENT -->|"cache clear or device move"| FREE["_release_q_handle frees borrowed descriptor"] FREE --> BIND["_ensure_q_handle: already_shuffled=True; preserve native words"] EVENT -->|"Torch state_dict load"| LOAD["Use local layout metadata or AWQ runtime shape"] LOAD -->|"runtime words"| BIND LOAD -->|"raw checkpoint"| FINAL EVENT -->|"_FusionGroup.install"| COMBINE["FusionKernelSpec.combine clones a live descriptor"] COMBINE --> SHARE["_share_operands binds source views while descriptors remain live"] SHARE --> RUN["Native fused or direct source GEMM"] RUN -->|"storage or device changes"| FREE LOAD -->|"partial raw AWQ qweight or qzeros"| ERROR["RuntimeError before copying mixed layouts"] SHARE -->|"missing source descriptor"| ERROR2["RuntimeError; do not dereference a freed handle"] style ERROR fill:#fff1f0,stroke:#cf1322 style ERROR2 fill:#fff1f0,stroke:#cf1322The same before/after diagrams and lifecycle table are committed in
docs/exllamav2-descriptor-lifecycle.mdand linked from the README and fusion planner.Validation
git diff --checkFor 512 input channels and 1024 output channels per source, native fusion was checked at rows 1, 8, 33, 257, and 1025. Maximum absolute projection error against an independent FP32 Torch oracle rounded to the output dtype was 0.000244140625 (FP16) and 0.001953125 (BF16) on both GPUs; acceptance was
rtol=atol=2e-3. Parent output drift from separate execution was at most 0.000030517578125. Tests inspect persistent state after forwards and reject retained dense weights. The fused 512×2048 layer keeps 512 KiB of INT4 qweight with source views; additional packed cache and retained dense weights are zero. The large-row fallback uses a transient 2 MiB FP16 reconstruction for that shape, released before return; BF16 also makes a transient FP32 cast.RTX 4090 FP16 Llama MLP parent, median of five CUDA-event samples of 100 forwards after 20 warmups (milliseconds per forward):