Repository navigation
perf(qqq,vptq): opt portable torch kernels into compiled generation (#5102) - #5171
Merged
Merged
Conversation
…5102) QQQTorchLinear copied its SUPPORTS_COMPILE=False reason (172 graphs, 24-115 breaks, 1.3-8x slower) from the AUTO QQQLinear FP16 CUDA kernel; #5075 made the packed QQQ torch op traceable, so the reason was stale. VPTQLinear recorded that the portable path had never been timed. Measure both on RTX 4090 (sm_89, torch 2.15.0+cu132), Qwen2.5-0.5B, 4 prompts x 32 greedy tokens, static-cache generate(): QQQTorchLinear bf16 AUTO: 2.08 s -> 0.48 s (4.3x), 1 graph, 0 breaks VPTQLinear fp16 VPTQ_TORCH: 0.705 s -> 0.142 s (5.0x), +1 model graph, 0 breaks Opt both in and clear the stale reasons. VPTQCudaLinear and QQQMlxQuantLinear inherit from these kernels, so they re-declare the opt-out instead of inheriting the win (the fused CUDA kernel recompiles to 23 graphs / 33 breaks and is 1.7x slower than eager). TorchLinear is unchanged: bf16 wins (4.5x) but fp16 recompiles to 173 graphs and is 6.3x slower, and its policy flag and compile level are both class attributes today. Adds a regression test that is red on a978bf1 and the mechanism docs page with before/after and decision-gate diagrams.
Qubitium
marked this pull request as ready for review
November 18, 2026 07:37
This was referenced Nov 18, 2026
Qubitium
added a commit
that referenced
this pull request
Nov 18, 2026
…5240) `torch_compile` installs lazy wrappers, so they can be entered from inside another `torch.compile` trace: transformers compiles a static-cache `generate()` forward, and `VPTQLinear.optimize()` installs one of these wrappers on `dequantize` at post-init. Since #5163 that wrapper wrote `torch._dynamo.config.suppress_errors` for the call. Dynamo config overrides are ContextVars, so the write is an `Unsupported: ContextVar mutation not supported` graph break in the outer trace. Every VPTQ layer then breaks out of the compiled forward, and the cudagraph static-cache decode returns outputs a later partition overwrote (`accessing tensor output of CUDAGraphs that has been overwritten`). Nested calls now stay read-only: when dynamo is already tracing, the wrapper returns the eager callable/module body so it is inlined into the outer graph, mutating no config and taking no `model_global_state_lock`. Top-level non-fullgraph behaviour is unchanged (#5163 still enables suppression for the call and restores it in `finally`); `_call_with_dynamo_suppression` carries the same guard as defence in depth. `_CompiledModelForward._release_trace_garbage` also skipped its `gc.collect()` while dynamo is tracing: `gc.collect` is untraceable, so it was one more graph break per model call on the #5171 path. The frame count is left unchanged so the next eager call does the release. Repro (Qwen2.5-0.5B-Instruct 2-layer VPTQ_TORCH fp16, 4 prompts x 32 greedy, static cache, RTX 5090): before the fix the first no-optimize static generate raised the CUDAGraph overwrite error; after it, no-optimize and three post-`optimize()` runs report 0 graph breaks, 2 unique graphs each, and tokens equal to eager. Tests: tests/test_compile_nested_suppression_5224.py (6 cases; 4 fail without the fix). Existing VPTQ/compile suites unchanged and green.
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
QQQTorchLinearandVPTQLinearnow declareSUPPORTS_COMPILE = Truewiththe measured static-cache
generate()result; the staleQQQLinear-derived reason and the "not measured yet" reason are removed.VPTQCudaLinearandQQQMlxQuantLinearare subclasses of the two portablekernels, so they explicitly re-declare the opt-out. Without that, the parent
opt-in would leak to the fused CUDA kernel (measured 0.36x, 23 graphs,
33 breaks) and to an MLX holder that is never traced by
torch.compile.TorchLinearis intentionally unchanged in this PR. Its class-levelSUPPORTS_COMPILE/SUPPORTS_COMPILE_LEVELcannot express the runtime-dtypesplit (bf16 wins, fp16 recompiles and loses), so the dtype-aware refinement
is a separate change with its own evidence below.
Net diff: 10 files, +316/-39 (including the new regression test). This changes the
#4669 selection policy for the two portable kernels; the mechanism page
docs/generation-compile-policy.mdhasthe before/after flow and the decision gate.
Fixes #5102
Root cause
BaseQModel._kernels_support_compile()(gptqmodel/models/base.py:3772)requires every quantized linear to declare
SUPPORTS_COMPILE; otherwise_generation_compile_kwargs()(gptqmodel/models/base.py:3816) injectsdisable_compile=True. Both portable kernels were trace-clean but opted out:QQQTorchLinear(gptqmodel/nn_modules/qlinear/qqq.py:700) copied the"172 graphs, 24-115 breaks, 1.3-8x slower" reason from the AUTO
QQQLinearFP16 CUDA kernel. That kernel is a different implementation;perf(cpu): make NF12, Paro and QQQ inductor-ready on CPU #5075 made the packed QQQ torch op traceable, so the reason was stale.
VPTQLinear(gptqmodel/nn_modules/qlinear/vptq_torch.py:45) recorded thatnobody had timed the portable path under
generate(). It was never ameasured loss.
On ROCm AUTO selects both portable kernels; on CUDA, bf16 QQQ AUTO also selects
QQQTorchLinear. Those setups silently ran the eager static-cache path.Before / after flow
Before #5102, the policy injected
disable_compile=Truefor both kernels:flowchart TD A["GPTQModel.generate()"] --> B["_generation_compile_kwargs()<br/>base.py:3816"] B --> C{"caller set disable_compile<br/>or compile_config?"} C -->|"yes"| D["keep the caller's choice"] C -->|"no"| E["_kernels_support_compile()<br/>base.py:3772"] E --> F{"every quantized linear<br/>SUPPORTS_COMPILE?"} F -->|"dense model"| D F -->|"QQQTorchLinear=False qqq.py:700<br/>VPTQLinear=False vptq_torch.py:45"| G["inject disable_compile=True"] G --> H["eager static-cache decode<br/>QQQ bf16 2.08 s / VPTQ fp16 0.70 s"] D --> I["transformers compile path<br/>QQQ 0.48 s / VPTQ 0.14 s"] style G fill:#fff3cd,stroke:#997404 style H fill:#f8d7da,stroke:#842029 style I fill:#d1e7dd,stroke:#146c43After #5102, the default policy keeps the auto-compile for them:
flowchart TD A["GPTQModel.generate()"] --> B["_generation_compile_kwargs()<br/>base.py:3816"] B --> C{"caller set disable_compile<br/>or compile_config?"} C -->|"yes"| D["keep the caller's choice"] C -->|"no"| E["_kernels_support_compile()<br/>base.py:3772"] E --> F{"every quantized linear<br/>SUPPORTS_COMPILE?"} F -->|"dense, QQQTorchLinear, VPTQLinear"| D F -->|"QQQLinear, VPTQCudaLinear,<br/>MarlinLinear, TorchLinear, ..."| G["inject disable_compile=True"] G --> H["eager static-cache decode"] D --> I["transformers compile path<br/>1 model graph, 0 breaks"] style G fill:#fff3cd,stroke:#997404 style I fill:#d1e7dd,stroke:#146c43The decision gate and the inheritance ambiguity path:
flowchart TD M["measure static-cache generate<br/>on the target device"] --> T{"1 model graph,<br/>0 breaks?"} T -->|"no"| O["keep SUPPORTS_COMPILE=False<br/>record the measured loss"] T -->|"yes"| W{"compiled decode faster<br/>than eager?"} W -->|"no"| O W -->|"yes"| Y["SUPPORTS_COMPILE=True<br/>clear SUPPORTS_COMPILE_EAGER_REASON"] O --> S["policy injects disable_compile=True"] Y --> P["policy keeps the auto-compile"] style O fill:#fff3cd,stroke:#997404 style S fill:#f8d7da,stroke:#842029 style Y fill:#d1e7dd style P fill:#d1e7ddVPTQCudaLinear(gptqmodel/nn_modules/qlinear/vptq_cuda.py:30) is asubclass of
VPTQLinear, so a parent-only opt-in would silently make thefused CUDA kernel claim a compiled win it does not have. It re-declares
Falseand its own reason.QQQMlxQuantLinear(
gptqmodel/nn_modules/qlinear/mlx.py:791) does the same for the MLX holder.Measurements
Host: RTX 4090 (sm_89), torch 2.15.0+cu132, CUDA-only. The issue's Radeon AI
PRO R9700 (gfx1201, ROCm) numbers are from the issue only and were not
reproduced here; there is no ROCm device on this host.
Qwen2.5-0.5B-Instruct, 4 left-padded prompts x 32 greedy tokens, 3 runs,
steady state (3rd run). "eager" =
disable_compile=True, "compiled" =disable_compile=False, "policy" = defaultgenerate(cache_implementation= "static").QQQTorchLinear, QQQ, AUTOVPTQLinear, VPTQ,VPTQ_TORCHdequantizegraphs exist on both paths)VPTQLinear, VPTQ,VPTQ_TORCHVPTQCudaLinear, VPTQ, AUTO (counter-case)The first compiled call cost 89 s for QQQ in a cold process (22-48 s with a
warm inductor cache). Layer-level numerics: the measured
QQQTorchLinearandVPTQLinearforwards were bit-identical to eager (max_abs = 0,max_rel = 0). Model-level greedy tokens drifted late, as expected frominductor's reduction order: QQQ 58/128 positions differed (first difference at
token 17-30 per prompt), VPTQ fp16 5/128. Outputs stayed coherent.
TorchLinearevidence (why it is out of this PR), same host/checkpoint:GPTQ_TORCHdtypeTests
Red on
a978bf18c(new test file, 4 failed / 2 passed), green after:CUDA_VISIBLE_DEVICES=3 PYTHONPATH=$PWD HF_HUB_OFFLINE=1 \ /root/venv/bin/python -m pytest -q \ tests/kernels/test_kernel_compile_protocol.py \ tests/test_generation_compile_policy_4669.py \ tests/kernels/test_compile_policy_qqq_vptq_5102.py \ tests/kernels/test_static_cache_compile_4669.py \ tests/kernels/test_compile_policy_levels_4984.py \ tests/kernels/test_compile_capability_4739.pyResult: 102 passed, 151 skipped, 13 deselected (the skips are the protocol
registry's non-claiming kernels). Also:
tests/kernels/test_kernel_compile_levels_quant.py -k "QQQ or VPTQ": 4 passed.tests/kernels/test_kernel_compile_levels_cpu.py -k "QQQ or VPTQ or Torch": 37 passed.ruff check gptqmodel docs/eora tests setup.py: all checks passed.New coverage:
tests/kernels/test_compile_policy_qqq_vptq_5102.py: declarations, clearedreasons, and
_kernels_support_compile()/_generation_compile_kwargs()forboth kernels; the
QQQLinearandVPTQCudaLinearopt-outs.tests/kernels/test_compile_policy_levels_4984.py: re-measures the QQQ/VPTQtrace on real hardware and requires the policy to follow it.
tests/test_generation_compile_policy_4669.py: pins the new declarations andthe parent/child split.
Unverified / follow-ups
not reproduced.
VPTQCudaLinear, which keeps the opt-out;only
VPTQ_TORCH(and ROCm AUTO) takes the compiled path.TorchLineardtype-aware refinement is left for a follow-up because both itspolicy flag and its declared compile level are class attributes today; the
fp16 loss is reproduced here (173 graphs, 6.3x slower).