Skip to content

perf(cpu): make NF12, Paro and QQQ inductor-ready on CPU - #5075

Merged
Qubitium merged 1 commit into
mainfrom
perf/cpu-kernel-compile-levels-followup
Nov 18, 2026
Merged

Qubitium merged 1 commit into
mainfrom
perf/cpu-kernel-compile-levels-followup

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Follow-up to the merged #5070. NF12Linear, ParoLinear,
QQQTorchLinear and TorchQuantEmbeddings now reach INDUCTOR on the CPU
nightly (torch 2.16.0.dev20261021+cpu) with fullgraph=True (zero graph
breaks). With this, every runnable CPU-capable kernel is INDUCTOR.
The shared AVX512 int4 GEMM also lowers for its BF16 fused paths now, not only
for the dense/ATen fallbacks.

Measured CPU levels (tests/kernels/compile_probe.py, CPU-only host):

Kernel Default CPU
NF12Linear EAGER INDUCTOR
ParoLinear EAGER INDUCTOR
QQQTorchLinear CUDAGRAPH INDUCTOR
TorchQuantEmbeddings EAGER INDUCTOR
TorchCpuAvx512Linear / TorchCpuAvx512AwqLinear INDUCTOR INDUCTOR (BF16 fused path)

What Changed

  • gptqmodel/extension.py: new register_fake() helper. The CPU JIT
    extensions define their kernels with TORCH_LIBRARY + a torch::kCPU
    implementation but no Meta kernel, so dynamo refused to trace them
    ("Operator does not support running with fake tensors"). FakeTensor impls
    are now registered for the shared AVX512 int4 GEMM and the packed QQQ
    kernel; inductor still emits the real CPU call as an extern kernel.
  • NF12: post_init() resolves the exponent base to a Python int (the buffer
    stays a tensor so a checkpoint round trip keeps it exact), and
    nf12_codec.decode selects escaped groups with a static-shape gather
    instead of bool(mask.any()) / int(index.max()) host reads. The compiled
    decode is bit-exact against eager, including checkpoints with escape rows.
  • Paro: the CPU transform RLock and _ensure_cpu_layout rewrites are skipped
    while tracing; the traced forward reads the layout the eager warmup built.
    post_init() pre-resolves the CPU kernels so a cold trace never loads an
    extension through importlib. The AVX512-BF16 path (bias included) and the
    bias-free ATen int4pack path reach INDUCTOR. A biased FP16/BF16 layer on
    the ATen path still needs the documented [BUG] Follow-up on #4060/#4062: model.optimize() TypeError on all CPU AUTO kernels, torch.compile int4pack LoweringException for fp16+bias (regression) and bf16, max_memory still ignored with device="cuda:0" #4068 FP32 cast (eager-only); a
    compile before the first eager forward takes the traceable dense reference.
  • QQQTorchLinear: the eager warmup publishes the resolved qqq_int4 handle
    with the packed CPU cache; a traced forward reads both instead of calling
    _qqq_cpu_kernel() (importlib) and _packed_weight_for_cpu() (RLock +
    checkpoint rewrite). A compile before any eager call falls back to the
    traceable torch._int_mm path.
  • TorchQuantEmbeddings: the row-decode path re-keyed its index metadata
    with tensor_cache_key (is_inference() is a hard graph break). A traced
    forward now reads the metadata the eager warmup built; without a warmup it
    takes the whole-table F.embedding(dequantize_weight()) path. Both reach
    INDUCTOR (graph_breaks=0).
  • awq.packing_utils._awq_shifts: derive the cached shift table without the
    Python lock while tracing (it is a pure function of device/bits).
  • _compile_support.compile_policy_reason() no longer attributes an inherited
    reason to a NOT_APPLICABLE MLX subclass (the matrix showed the torch
    parent's CPU loss reason on MLX rows once the parents gained one).
  • Docs/tests: matrix regenerated; the CPU level test now also exercises the
    BF16 AVX512 fused paths, both embedding decode paths, cold-compile coverage
    for QQQ/Paro/NF12, and a compiled-vs-eager NF12 decode test with escape
    rows.

Tests

Environment: torch 2.16.0.dev20261021+cpu, Python 3.14, CPU-only host (no
CUDA), numpy 2.5.4. --noconftest is used locally because this host has a
source build of the Hugging Face kernels package and tests/conftest.py puts
tests/ on sys.path, so tests/kernels shadows it.

# CPU level test (packed modules, embedding paths, cold-compile fallbacks)
python -m pytest --noconftest -q tests/kernels/test_kernel_compile_levels_cpu.py
# 25 passed, 1 skipped
#   skip: GGUFCppKernel - llama-cpp-python is not installed on this host

# CPU levels + declaration protocol + docs matrix
python -m pytest --noconftest -q \
  tests/kernels/test_kernel_compile_levels_cpu.py \
  tests/kernels/test_kernel_compile_protocol.py
# 88 passed, 154 skipped

# CPU levels + protocol + NF12 compiled decode + QQQ JIT
python -m pytest --noconftest -q \
  tests/kernels/test_kernel_compile_levels_cpu.py \
  tests/kernels/test_kernel_compile_protocol.py \
  tests/test_nf12_codec.py \
  tests/test_qqq_jit.py
# 131 passed, 190 skipped

# selection / compile policy
python -m pytest --noconftest -q \
  tests/kernels/test_selection.py \
  tests/kernels/test_compile_capability_4739.py \
  tests/kernels/test_static_cache_compile_4669.py
# 120 passed, 73 skipped

# Paro / AWQ / AVX512 / NF12 regressions
python -m pytest --noconftest -q \
  tests/kernels/test_paro.py \
  tests/kernels/test_paro_cpu_lock_concurrency_4885.py \
  tests/kernels/test_awq_cpu_fused_post_init.py \
  tests/kernels/test_awq_torch.py \
  tests/kernels/test_awq_torch_fused.py \
  tests/kernels/test_awq_dequantize_tiling.py \
  tests/test_awq_dequant_layouts.py \
  tests/kernels/test_qqq_workspace.py \
  tests/kernels/test_cpu_avx512_snapshot_rebuild_4783.py \
  tests/kernels/test_cpu_awq_int4_mmap_detach_4785.py \
  tests/test_nf12_escape_bounds.py
# 256 passed, 51 skipped

# lint gate (pinned ruff 0.14.2, same command as format/check.sh)
ruff check gptqmodel docs/eora tests setup.py
# All checks passed.

Skips / limits

- Register FakeTensor impls for the CPU JIT extension ops (the shared AVX512
  int4 GEMM and the packed QQQ kernel) so dynamo can trace them; inductor
  emits the real CPU call as an extern kernel. The AVX512-BF16 GPTQ/AWQ
  fused paths now lower as well, not only their dense fallbacks.
- NF12: resolve the exponent base to a Python int at `post_init()` and select
  escaped groups in `nf12_codec.decode` with a static-shape gather instead of
  `bool(mask.any())` / `int(index.max())` host reads. The compiled decode is
  bit-exact against eager, including checkpoints with escape rows.
- Paro: skip the CPU transform RLock and the `_ensure_cpu_layout` rewrite while
  tracing and read the eager-built layout; pre-resolve the CPU kernels in
  `post_init()`. The AVX512-BF16 path (bias included) and the bias-free ATen
  path reach `INDUCTOR`. A biased FP16/BF16 ATen layer stays the documented
  #4068 break; compiling before the first eager forward takes the traceable
  dense reference instead.
- QQQTorchLinear: publish the resolved packed kernel handle alongside the
  packed cache during the eager warmup and read both while tracing; a compile
  before any eager call falls back to the traceable `torch._int_mm` path.
- `awq.packing_utils._awq_shifts`: derive the cached shift table without the
  Python lock while tracing (it is a pure function of device/bits).
- Declare the CPU levels (`NF12Linear`, `ParoLinear`, `QQQTorchLinear` ->
  `INDUCTOR`), stop `compile_policy_reason()` from attributing an inherited
  reason to a `NOT_APPLICABLE` MLX subclass, and add cold-compile plus
  compiled-decode regression tests.
@Qubitium
Qubitium marked this pull request as ready for review November 18, 2026 02:08
@Qubitium
Qubitium merged commit 170b34a into main Nov 18, 2026
9 checks passed
@Qubitium
Qubitium deleted the perf/cpu-kernel-compile-levels-followup branch November 18, 2026 02:09
Qubitium added a commit that referenced this pull request Nov 18, 2026
…5102) (#5171)

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant