Skip to content

[PERF] Regression from #5230 (fix of #5211): NF12Linear.dequantize_weight's is_inference() check graph-breaks every compiled CPU forward - declared CPU INDUCTOR not reached, fullgraph fails, compiled forward 1.3-1.4x slower #5299

Closed

Description

@Qubitium

Found on main @ c03416c, CPU (host of an RTX 4090 box; torch 2.15.0+cu132, inductor).

What is wrong

#5230 (fix of #5211) made NF12Linear.dequantize_weight() return _as_normal_tensor(weight...) (gptqmodel/nn_modules/qlinear/nf12.py, end of dequantize_weight). _as_normal_tensor calls tensor.is_inference(). The CPU forward's wide path calls dequantize_weight() on every call (_forward_impl, weight = self.dequantize_weight(device=x.device, dtype=compute_dtype)).

Dynamo cannot trace tensor.is_inference() (Unsupported: Encountered tensor.is_inference() during tracing ... This graph break is fundamental). So every compiled NF12 CPU forward now graph-breaks between the decode and the matmul, and fullgraph=True compilation fails. NF12Linear still declares SUPPORTS_COMPILE_LEVEL_BY_DEVICE = {"cpu": COMPILE_LEVEL.INDUCTOR}.

The buffers are already made non-inference at install time (the other _as_normal_tensor calls in pack/load_quant_result). The decoded result then only becomes an inference tensor when the caller itself runs under inference_mode, and that is harmless for that caller. So the wrapper on the return value is the only part that breaks the graph.

Measured (NF12Linear 2048x2048 bf16, CPU, 8 threads, torch.compile(m.forward, backend="inductor"))

eager compiled M=1 compiled M=32 fullgraph=True
77c4797ff^ 53-60 ms 9.0-10.0 ms 9.7-10.3 ms ok
77c4797ff (#5230) 60 ms 13.9 ms 13.4 ms torch._dynamo.exc.Unsupported
main c03416c 60 ms 12.8 ms 13.6 ms torch._dynamo.exc.Unsupported

The compiled CPU forward is about 1.3-1.4x slower, which loses roughly a third of the compile speedup.

Repro

import torch
from gptqmodel.nn_modules.qlinear.nf12 import NF12Linear
lin = torch.nn.Linear(2048, 2048, bias=False, dtype=torch.bfloat16)
m = NF12Linear(bits=12, group_size=-1, sym=True, desc_act=False, in_features=2048,
               out_features=2048, bias=False, register_buffers=True)
m.pack_original(lin, None, None); m.post_init(); m.eval()
x = torch.randn(4, 2048, dtype=torch.bfloat16)
with torch.inference_mode():
    torch.compile(m.forward, backend="inductor", fullgraph=True)(x)
# main: torch._dynamo.exc.Unsupported: Encountered tensor.is_inference() during tracing
# 77c4797ff^: ok

The existing tests already catch it: both are red on main, deterministic 3/3, and bisected to 77c4797ff.

tests/kernels/test_kernel_compile_levels_cpu.py::test_cpu_kernel_reaches_declared_cpu_level[NF12Linear]
  AssertionError: NF12Linear: declares CPU INDUCTOR but the probe reported eager=True graph_breaks=1 ... max=EAGER
tests/kernels/test_kernel_compile_levels_cpu.py::test_nf12_cpu_compiles_after_post_init_without_an_eager_call
  torch._dynamo.exc.Unsupported: Encountered tensor.is_inference() during tracing

Suggested fix

Drop _as_normal_tensor from the dequantize_weight return value: the buffers are already normal, and no_grad decode under grad mode yields a normal tensor. If a caller still needs it, skip the check while compiling (if not torch.compiler.is_compiling()). Keep the #5211 grad-mode tests and the two compile-level tests green together.

Severity: medium for performance. Results are correct, but the declared CPU compile level is no longer reached, and fullgraph compiles of NF12 models fail.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 19, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-fb5145b84190 finished this issue.

    Pull request: #5309

    Stop wrapping the decoded NF12 weight in _as_normal_tensor on every forward so dynamo can trace the CPU path; add/keep the compile-level and fullgraph regressions green.

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