Skip to content

[PERF] Regression from #4737 (kept by #4754): compiled AwqTorchLinear decodes every weight in FP64 (decode-range guard returns False for all dtypes under is_compiling), static-cache generate 2.3x slower than eager #4794

Closed

Description

@Qubitium

Found on main @ d2060d4, RTX 4090 (sm_89, CUDA 13.2, torch 2.15, transformers 5.22.1). A/B against b225a4383 (round-14 base) on the same card.

What is wrong

#4737 (b378367c5) made AwqTorchLinear trace-clean by moving the compile/capture guard in _stored_decode_range_fits() (gptqmodel/nn_modules/qlinear/torch_awq.py:223-226) in front of the cache key. Under torch.compiler.is_compiling() (or CUDA-graph capture), that function now returns False for every dtype it is asked about. _decode_dtype_for_activation() (torch_awq.py:263-273) asks about the activation dtype and then about FP32. Both answers are False, so it falls through to return torch.float64.

So in every compiled graph, each AwqTorchLinear dequantizes its whole weight to FP64. linear_wide_bias() (_numerics.py:224-228) then promotes the product to FP64, so the GEMM also runs in FP64. On consumer GPUs FP64 runs at 1/64 rate, and every weight byte is 4x wider. Eager still decodes in FP16, as intended.

#4754 (the #4739 fix) then documented AwqTorchLinear as "trace-clean and measured faster" and kept SUPPORTS_COMPILE = True. Because of that, the default static-cache generate() compiles it. The result is that the one AWQ kernel opted into compile is now 2-3x slower compiled than eager. Before #4737 its compiled forward was about 2x faster than eager. #4739 reported that path as 20-30% slower than eager at the model level. It is now about 2.3x slower than eager, and 3.9x slower than the dynamic cache.

Repro

import torch, time
from gptqmodel import GPTQModel, BACKEND
m = GPTQModel.load("AWQ-Llama-3.2-1B-g128-gemm", backend=BACKEND.AWQ_TORCH, dtype=torch.float16, device="cuda:0")
mod = m.model.model.layers[0].self_attn.q_proj

# 1) the decode dtype chosen inside a compiled graph
orig = torch.compiler.is_compiling
torch.compiler.is_compiling = lambda: True
print(mod._decode_dtype_for_activation(torch.float16))   # torch.float64
torch.compiler.is_compiling = orig
print(mod._decode_dtype_for_activation(torch.float16))   # torch.float16 (eager)

# 2) module-level eager vs compiled, M=2 decode
def bench(f, x, n=50):
    for _ in range(5): f(x)
    torch.cuda.synchronize(); t = time.perf_counter()
    for _ in range(n): f(x)
    torch.cuda.synchronize(); return (time.perf_counter() - t) / n * 1e3
for name in ("self_attn.q_proj", "mlp.gate_proj"):
    mod = m.model.model.layers[0].get_submodule(name)
    x = torch.randn(1, 2, mod.in_features, device="cuda", dtype=torch.float16)
    with torch.inference_mode():
        print(name, bench(mod, x), bench(torch.compile(mod), x))

Module level, ms per call (M=2, fp16):

layer eager (both) compiled @ b225a4383 compiled @ main
q_proj 2048x2048 0.113-0.122 0.066 (bitwise = eager) 0.180 (differs from eager by up to 0.03)
gate_proj 2048x8192 0.290-0.295 0.154 (bitwise = eager) 0.854-0.915

End to end: generate(**enc, max_new_tokens=64, min_new_tokens=64, do_sample=False, ...) with 2 prompts, steady state after warm-up, on main:

run seconds
dynamic cache 1.74
static cache, default policy (compiled, _kernels_support_compile() is True) 6.43-7.03
static cache, disable_compile=True 2.98
static compiled, with _decode_dtype_for_activation patched to return FP32 under compile 2.87
static compiled, patched to return FP16 under compile (the eager choice for this checkpoint) 1.04

The tokens are the same in every row. Dynamo reports 1 graph and 0 graph breaks, so the slowdown comes from the FP64 decode and GEMM, not from fragments. With the narrow decode, the opt-in would be 2.9x faster than the eager static cache.

Suggested fix

Do not let the "unknown under compile" answer escalate to FP64:

  • Decide the decode dtype eagerly and keep it as a plain Python attribute that compiled code can read as a constant. Do this at post_init()/load, and again on clear_weight_cache() or a move. Dynamo guards on it, and capture never needs a host read.
  • Or, under compile, return the activation dtype when a precomputed flag says it fits, and FP32 otherwise. Never return FP64. FP32 covers every ordinary checkpoint, as the existing comment says.

Also keep SUPPORTS_COMPILE = True only after re-measuring on a real-size model.

Test that should have caught it

tests/kernels/test_compile_capability_4739.py::test_awq_torch_is_trace_clean checks only the graph-break count. Unlike the FP8 case, it has no timing assertion. The docs number (0.174 s -> 0.047 s) comes from a tiny Llama, where an FP64 GEMM is negligible. Two checks would have caught this:

  • a module-level test that compiled < eager on a 2048x8192 layer;
  • an assertion that the compiled decode dtype equals the eager one.

Severity

Medium (perf, plus a numeric drift between compiled and eager). Every static-cache generate() on an AWQ_TORCH model is 2.3x slower than eager by default (3.9x slower than the dynamic cache), and it is silent. This is a regression from #4737; #4754 kept the opt-in on top of it.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-11788dff99fd finished this issue.

    Pull request: #4822

    Reproducing the FP64 decode under torch.compile on CPU; will make the compiled decode dtype match the eager choice and add a dtype-parity regression

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions