Repository navigation
fix(torch): compile the packed decode with explicit operands (#5797) - #5955
Merged
Merged
Conversation
`TorchLinear.optimize()` compiled `type(self).dequantize_weight`, so dynamo traced the module and lifted `qweight`/`qzeros`/`scales` into the compiled graph. Fusion then replaced those buffers with views of the combined packed allocation, but the cached graph still held the pre-fusion tensors, so a warmed fused model kept a second copy of every packed weight alive until a full `torch._dynamo.reset()`. Compile a new `_decode_248_weight` that takes the packed operands and the layout constants as arguments instead. `_prepare_248_decode` stages the eager-only work (zero-point/g_idx caches and the range-safe decode dtype) and returns the operands, and the compiled wrapper routes training, non-2/4/8-bit and Triton-eligible calls back to the eager class method. The compiled entry now guards on the tensors it is handed, so replacing a packed buffer retires the original storage even while an older compilation entry remains cached. Decode parity is unchanged (bitwise equal to the eager path, including `num_itr > 1`, an explicit dtype and training), and the decode is faster because the staged caches no longer run inside the graph. Add a post-warm-up fusion regression test: a warm compiled decode, then `fuse()`, then `gc.collect()`, must release every source `qweight` while the fused logits stay within tolerance.
Qubitium
marked this pull request as ready for review
November 20, 2026 18:59
Qubitium
added a commit
that referenced
this pull request
Nov 29, 2026
…5325) (#6273) A5 was a product defect, not a stale test: a meta move ran the int4pack inverse (and its aten::equal identity check) against meta operands until #5533 materialized the snapshot from its shape/dtype descriptor. A14 was realigned by #5564, and the compiled entry now stages its operands outside the traced graph (#5955), so the caches are published again. Both items are green at main 2d3261d. Extend the device-move test to assert every checkpoint operand's shape and dtype is restored, not just qweight. Restore the cache expectations the A14 tests are named for: the compiled dequant publishes the g_idx cache on the compute device, load_state_dict clears both metadata caches, and a full forward still matches F.linear after the g_idx offload. Co-authored-by: OpenAI Agent <[email protected]>
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.
Summary
TorchLinear.optimize()compiledtype(self).dequantize_weight, so Dynamotraced the module and lifted
qweight/qzeros/scales/g_idxinto thecompiled graph as
L__self___*locals._FusionGroup._share_operandsthenreplaced those buffers with views of the combined packed allocation, but the
cached graph still held the pre-fusion tensors. A warmed fused model therefore
kept a full second copy of every fused packed weight alive until a full
torch._dynamo.reset(), throughgc.collect()and later fused forwards (#5797).The fix compiles a new module-level
_decode_248_weight(...)that takes everypacked operand and layout constant as an argument.
TorchLinear._prepare_248_decode()stages the eager-only work (cached zero-point/g_idx tables and the range-safe
decode dtype) outside the graph, and
_CompiledDequantizeWeightcalls thecompiled decoder with the current tensors. The compiled entry now guards on the
tensors it is handed, so replacing a packed buffer retires the original storage
even while an older compilation entry remains cached.
Fixes #5797
What Changed
gptqmodel/nn_modules/qlinear/torch.py_decode_248_weight(qweight, zeros, g_idx_long, scales, *, pack_factor, wf_unsqueeze_neg_one, dequant_dtype, maxq, in_features, decode_dtype, num_itr):the 2/4/8-bit decode body, moved verbatim, with every packed operand and
layout constant as a parameter. The [BUG] GPTQ_TORCH eval decode bypasses the range-safe base decoder: BF16 linear returns NaN and embedding returns inf #5606 range-safe decode is preserved.
TorchLinear._prepare_248_decode(dtype): runs_init_wf_unsqueeze_buffers,_stream_decode_qzeros,_stream_g_idx_long,_maybe_offload_g_idx_to_cpu,_resolve_decode_dtype, and returns the operands._dequantize_weight_cached_248()now calls those two helpers; the eagerdecode path is unchanged.
_CompiledDequantizeWeight.__call__keeps the eager signature contract fornum_itr/dtype, routes training, non-2/4/8-bit and Triton-eligible callsto
type(module).dequantize_weight(...), and otherwise calls the compileddecoder with the current operands.
optimize()compiles_decode_248_weightinstead of the bounddequantize_weight. The weak-reference cycle fix from [MEM] GPU-loaded models are reference cycles: VRAM is not released on del/scope exit until a full gc.collect() (1B: 0.96 GiB, 8B QQQ: 5.3 GiB); single-device loads install AlignDevicesHook on every layer #4847 is preserved.tests/module_tree/test_llama_fusion.py: newtest_warm_compiled_decode_does_not_pin_source_operands_through_fusion— awarm compiled decode,
fuse(),gc.collect(), then every fused sourceqweightmust be released, and the fused logits must still match.No API or behavior change for callers. Decode results are bitwise identical to
the eager path, including
num_itr > 1, an explicitdtypeand training.Out of scope:
AwqTorchLinearand the other kernel classes do not use thiscompiled decoder.
Tests
Skips are the CUDA/Marlin, TTNN, MLX and Triton-only paths that this CPU host
cannot run; no skipped test covers the changed decode.
Decode parity probe (
/tmp/probe_5797_parity.py): compiled vs eager decode isbitwise equal, an independent reference gives max abs
0.0, andnum_itr=2,dtype=float32and the training decode all match.Measured Impact
Host: tt-quietbox, 4x Blackhole p300c,
torch==2.11.0+cpu(pinned TT runtime),CPU-only
BACKEND.TORCH,ModelCloud/Opt-125-GPTQ-4bit-10-25-2024, BF16,12-token batch-1 forward, median of 3 runs on each revision.
492641b15)a1670fc65)qweightqweightafterfuse()+gc.collect()(MiB)Commands:
VRAM itself is not measurable on this CPU host; the memory row is the retained
packed CPU storage, which is the same allocation that would stay resident on an
accelerator. The 0.125 logit delta is the existing fused-vs-unfused BF16
accumulation difference: it is identical before and after this PR, so this
change adds no accuracy drift.
Model Support / Module Plan
Not applicable: no model definitions, aliases, registry entries or fusion
eligibility change.
AWQ onboarding safety
Not applicable: no model, definition or AWQ changes.
Logic Flow Documentation
Not applicable: local fix. The decode entry points, dispatch order and fallbacks
are unchanged; only the compiled entry's operand passing and ownership changed,
inside
_decode_248_weightand_CompiledDequantizeWeight.Old:
optimize()compiled the bounddequantize_weight(module, ...); Dynamolifted
module.qweight/qzeros/scalesinto the graph(
torch.py:475-481at85caf6cc5).New:
optimize()compiles_decode_248_weight(operands...); the wrapper readsthe current tensors from the weak module reference and passes them explicitly.
Review Requirements
Kernel Accuracy Requirements
0.0); model-level fused logits match the pre-fusion logits with identical greedy tokens and the same 0.125 BF16 delta before and after.Notes
#5797is claimed byagent-83ff5bec0c7b; release follows this PR URL.this PR only removes the duplicate pre-fusion packed storage.