Skip to content

perf(qqq,vptq): opt portable torch kernels into compiled generation (#5102) - #5171

Merged
Qubitium merged 1 commit into
mainfrom
fix/5102-qqq-vptq-compile
Nov 18, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/5102-qqq-vptq-compile

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • QQQTorchLinear and VPTQLinear now declare SUPPORTS_COMPILE = True with
    the measured static-cache generate() result; the stale
    QQQLinear-derived reason and the "not measured yet" reason are removed.
  • VPTQCudaLinear and QQQMlxQuantLinear are subclasses of the two portable
    kernels, 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.
  • TorchLinear is intentionally unchanged in this PR. Its class-level
    SUPPORTS_COMPILE/SUPPORTS_COMPILE_LEVEL cannot express the runtime-dtype
    split (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.md has
the 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) injects
disable_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
    QQQLinear FP16 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 that
    nobody had timed the portable path under generate(). It was never a
    measured 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=True for 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:#146c43
Loading

After #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:#146c43
Loading

The 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:#d1e7dd
Loading

VPTQCudaLinear (gptqmodel/nn_modules/qlinear/vptq_cuda.py:30) is a
subclass of VPTQLinear, so a parent-only opt-in would silently make the
fused CUDA kernel claim a compiled win it does not have. It re-declares
False and 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" = default generate(cache_implementation= "static").

kernel / checkpoint dtype eager compiled speedup dynamo policy before policy after
QQQTorchLinear, QQQ, AUTO bf16 2.08 s 0.48 s 4.3x 1 graph, 0 breaks eager, 2.08 s 0.53 s (compiled)
VPTQLinear, VPTQ, VPTQ_TORCH fp16 0.705 s 0.142 s 5.0x 5 graphs, 0 breaks (4 layer-local dequantize graphs exist on both paths) eager, 0.70 s 0.145 s (compiled)
VPTQLinear, VPTQ, VPTQ_TORCH bf16 0.718 s 0.208 s 3.5x 5 graphs, 0 breaks (4 + 1 model) eager, 0.72 s 0.148 s (compiled)
VPTQCudaLinear, VPTQ, AUTO (counter-case) fp16 0.602 s 1.692 s 0.36x 23 graphs, 33 breaks eager, 0.60 s eager, 0.60 s (unchanged)

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 QQQTorchLinear and
VPTQLinear forwards were bit-identical to eager (max_abs = 0,
max_rel = 0). Model-level greedy tokens drifted late, as expected from
inductor's reduction order: QQQ 58/128 positions differed (first difference at
token 17-30 per prompt), VPTQ fp16 5/128. Outputs stayed coherent.

TorchLinear evidence (why it is out of this PR), same host/checkpoint:

GPTQ_TORCH dtype eager compiled result dynamo
bf16 0.842 s 0.189 s 4.5x faster 5 graphs, 0 breaks (4 layer-local + 1 model)
fp16 0.599 s 3.746 s 6.3x slower 173 graphs, 0 breaks

Tests

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.py

Result: 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, cleared
    reasons, and _kernels_support_compile()/_generation_compile_kwargs() for
    both kernels; the QQQLinear and VPTQCudaLinear opt-outs.
  • tests/kernels/test_compile_policy_levels_4984.py: re-measures the QQQ/VPTQ
    trace on real hardware and requires the policy to follow it.
  • tests/test_generation_compile_policy_4669.py: pins the new declarations and
    the parent/child split.

Unverified / follow-ups

  • ROCm: not measured here (CUDA-only host). The issue's R9700 numbers are cited,
    not reproduced.
  • VPTQ on CUDA AUTO still selects VPTQCudaLinear, which keeps the opt-out;
    only VPTQ_TORCH (and ROCm AUTO) takes the compiled path.
  • TorchLinear dtype-aware refinement is left for a follow-up because both its
    policy flag and its declared compile level are class attributes today; the
    fp16 loss is reproduced here (173 graphs, 6.3x slower).

…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
Qubitium marked this pull request as ready for review November 18, 2026 07:37
@Qubitium
Qubitium merged commit 17a488e into main Nov 18, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/5102-qqq-vptq-compile branch November 18, 2026 07:38
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant