Skip to content

feat(kernels): declare torch.compile and CUDA-graph capability per kernel - #4737

Merged
Qubitium merged 4 commits into
mainfrom
feat/kernel-graph-compile-support
Nov 17, 2026
Merged

Qubitium merged 4 commits into
mainfrom
feat/kernel-graph-compile-support

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Every quantized kernel now declares, in its own class body, how far torch.compile can take its forward and whether the eager forward is safe inside a CUDA-graph capture. 68/68 kernels declare both (67 registered kernels plus the role-selected TorchQuantEmbeddings); the registry test fails if a new kernel forgets.

This is the capability layer on top of #4669/#4727's SUPPORTS_COMPILE policy flag, which is unchanged.

Protocol

gptqmodel/nn_modules/qlinear/_compile_support.py:

class COMPILE_LEVEL(IntEnum):
    NOT_APPLICABLE = -1   # not a torch module (MLX / TTNN / XLA)
    EAGER = 0             # dynamo breaks (or compile is a measured pessimization)
    DYNAMO = 1            # torch.compile(fullgraph=True, backend="eager"): 0 graph breaks
    AOT_EAGER = 2         # backend="aot_eager": meta/fake tensors + aot_autograd
    INDUCTOR = 3          # backend="inductor" lowers it
    CUDAGRAPH = 4         # mode="reduce-overhead" captures a CUDA graph

plus SUPPORTS_GRAPH_CAPTURE: bool for the separate question "may the eager forward run inside torch.cuda.graph(...)?" — a custom op without a meta implementation can be capture-safe yet untraceable, and a traceable kernel can still sync the host in forward. Both are declared explicitly per kernel (no silent inheritance); the declaration describes the default runtime state on the kernel's primary device, with any opt-in (e.g. TorchLinear.optimize() on CPU) recorded in the evidence comment.

Levels are the torch 2.13+/2.15 API surface; max-autotune is an INDUCTOR tuning mode, not a level. Measured on NVIDIA GB10 (sm_121), torch 2.11.0+cu130 — same torch.compile API as the supported floor.

Coverage

Level Count Kernels
CUDAGRAPH 8 AwqTorchLinear, AwqGEMVTorchLinear, AwqGEMVTritonLinear, QQQTorchLinear, TorchFP8Linear, VPTQLinear, MacaqLinear, ParoQuantTritonLinear
EAGER 40 native CUDA/Triton custom-op kernels + the CPU/AVX512/Aten variants
NOT_APPLICABLE 19 16 MLX, 2 TTNN, 1 XLA
— 1 TorchQuantEmbeddings (role-selected embedding, EAGER)

13 kernels additionally declare eager CUDA-graph capture: TorchLinear, TritonV2Linear, AwqGEMMTritonLinear, MacaqTritonLinear, GGUFTorchLinear, GGUFTritonKernel, W4AFP8Linear, W4ANVFP4Linear, plus the five CUDAGRAPH kernels whose eager forward also captures (AwqGEMVTorchLinear, AwqGEMVTritonLinear, ParoQuantTritonLinear, TorchFP8Linear, VPTQLinear).

Unbuildable-on-this-host backends (JIT extensions without CUDA_HOME/nvcc, Hopper/SM90- or sm100-110-only images, bitblas/MLX/TTNN/XLA absent, aarch64 vs AVX512, retired TorchInt8AwqLinear) are declared conservatively from a static scan and their level test skips with the exact reason — never faked.

docs/kernel-compile-matrix.md is generated from the declarations and enforced by the protocol test.

Behavioral fix found by the audit

AwqTorchLinear sets SUPPORTS_COMPILE=True (#4669) but still broke the graph: _stored_decode_range_fits computed tensor_cache_key (is_inference() / data_ptr()) before checking is_compiling() / is_current_stream_capturing(). Moving the guard first makes the forward trace with 0 graph breaks and reach CUDAGRAPH; the eager decision (wide decode under compile or capture) is unchanged. So the one kernel that opted into compiled generation is now actually traceable.

Tests

File Result
tests/kernels/test_kernel_compile_protocol.py (new) 20 passed — every kernel declares both attrs, a capture claim has no .item()/.tolist()/.cpu()/data_ptr()/synchronize in its own forward, SUPPORTS_COMPILE ⇒ ≥ DYNAMO, docs matrix == code
tests/kernels/test_kernel_compile_levels_{gptq,awq,quant,nontorch}.py (new) 42 passed, 162 skipped — build a real packed module, probe, assert the declared level is reached; skips carry the runtime reason
tests/kernels/compile_probe.py (new) reusable probe: dynamo explain, fullgraph eager/aot_eager/inductor/reduce-overhead, explicit torch.cuda.CUDAGraph
tests/kernels/gen_compile_matrix.py (new) regenerates the docs table

On a CPU-only runner a declaration that needs a device (INDUCTOR, CUDAGRAPH, SUPPORTS_GRAPH_CAPTURE=True) skips with the declaration in the reason instead of failing: CUDA_VISIBLE_DEVICES="" gives 5 passed / 62 skipped.

cd <worktree> && GPTQ_BYPASS_SAFETY_CHECKS=1 PYTHONPATH=<worktree> LOGBAR_ANIMATION=0 \
  /root/gptqmodel-test-venv/bin/python -m pytest \
  tests/kernels/test_kernel_compile_protocol.py tests/kernels/test_kernel_compile_levels_*.py -q

No regressions: the same kernel suite (test_awq_torch, test_awq_torch_fused, test_inference_numeric_range, test_qlinear_hierarchy, test_fp8_kernel, test_qqq_workspace, test_generation_compile_policy_4669) gives identical 67 failed / 450 passed on unmodified main — the failures are pre-existing cross-file test-isolation issues on this host, not this change. ruff check clean on all 53 files.

Docs

  • new docs/kernel-compile-matrix.md (test-synced)
  • docs/generation-compile-policy.md: capability vs policy section
  • docs/torch-compile-fallback.md: cross-link
  • docs/README.md: index entry

…tocol

Adds `COMPILE_LEVEL` (NOT_APPLICABLE < EAGER < DYNAMO < AOT_EAGER <
INDUCTOR < CUDAGRAPH) with `SUPPORTS_COMPILE_LEVEL` and
`SUPPORTS_GRAPH_CAPTURE` on BaseQuantLinear, a construction-agnostic probe
harness, a registry test that requires every kernel to declare both, and the
docs matrix generator. Per-kernel declarations follow in the next commits.
…rnel

Every quantized kernel now states, in its own class body, how far
`torch.compile` can take its `forward` and whether the eager forward is safe
inside a CUDA-graph capture. Callers stop guessing from a class name and stop
paying for graph fragments.

Protocol (`gptqmodel/nn_modules/qlinear/_compile_support.py`):

* `COMPILE_LEVEL`: NOT_APPLICABLE < EAGER < DYNAMO < AOT_EAGER < INDUCTOR <
  CUDAGRAPH, where each level is the measured probe: fullgraph eager,
  aot_eager, inductor, and `mode="reduce-overhead"` capture. NOT_APPLICABLE is
  for MLX/TTNN/XLA kernels, whose own compiler owns the graph.
* `SUPPORTS_COMPILE_LEVEL` / `SUPPORTS_GRAPH_CAPTURE` on BaseQuantLinear, with
  no silent inheritance: the registry test requires both in every registered
  kernel's `__dict__`.
* `SUPPORTS_COMPILE` stays the separate #4669 policy flag ("is compiling not a
  pessimization"). A kernel may trace and still keep it False.

All 67 registered kernels declare both, with an evidence comment naming either
the measured probe (NVIDIA GB10 sm_121, torch 2.11.0+cu130, the same
torch.compile API surface as the 2.13/2.15 floor) or the static reason the
backend cannot build here (no CUDA toolkit for the JIT extensions, Hopper/SM90
or sm100-110-only images, bitblas/MLX/TTNN/XLA absent, aarch64 vs AVX512).
Result: 8 kernels reach CUDAGRAPH (AwqTorchLinear, AwqGEMVTorchLinear,
AwqGEMVTritonLinear, QQQTorchLinear, TorchFP8Linear, VPTQLinear, MacaqLinear,
ParoQuantTritonLinear), 19 are NOT_APPLICABLE (MLX/TTNN/XLA), the other 40 are
EAGER; 13 also declare eager CUDA-graph capture.

One behavioral fix fell out of the measurement: `AwqTorchLinear` declares
`SUPPORTS_COMPILE=True` (#4669) but still broke the graph on
`tensor_cache_key` (`is_inference()`/`data_ptr()`) before its compile guard.
Checking `is_compiling()`/`is_current_stream_capturing()` first makes the
forward trace with zero graph breaks and reach CUDAGRAPH, so the opt-in kernel
is now actually traceable. The eager decision (wide decode under compile or
capture) is unchanged.

Tests:

* `tests/kernels/test_kernel_compile_protocol.py`: every kernel declares both
  attributes, a capture claim has no host-sync construct in its own `forward`,
  a `SUPPORTS_COMPILE` claim is at least DYNAMO, and
  `docs/kernel-compile-matrix.md` matches the declarations (20 passed).
* `tests/kernels/test_kernel_compile_levels_{gptq,awq,quant,nontorch}.py`:
  build a real packed module and assert the declared level is reached on this
  hardware; unbuildable backends skip with the exact reason (42 passed, 162
  skipped).
* `tests/kernels/compile_probe.py` is the reusable probe; `gen_compile_matrix.py`
  regenerates the docs table.

No regressions: the same kernel suite on unmodified main gives the identical
67 failed / 450 passed (pre-existing cross-file test-isolation failures in
test_inference_numeric_range/test_fp8_kernel/test_qqq_workspace on this host).
TorchQuantEmbeddings skips backend discovery (SUPPORTS_BACKEND_SELECTION =
False) but is a user-facing quantized layer, so it declares the protocol as
well: its forward branches on host values (input_ids.numel() against the
row-decode limit, dtype/device checks) before choosing dense decode or
F.embedding, so it is EAGER and not capture-safe. The registry test and the
docs generator now share protocol_kernels(), which adds it to the 67
registered kernels (68 rows).
The level tests assert a declared capability against a live probe, so on a
CPU-only runner an INDUCTOR/CUDAGRAPH declaration or a
SUPPORTS_GRAPH_CAPTURE=True claim has nothing to measure. Skip those cases with
the declaration in the reason instead of failing, and treat a builder that
needs an absent device as the same environment skip. Verified with
CUDA_VISIBLE_DEVICES="": 5 passed, 62 skipped (was 9 failed).
@Qubitium
Qubitium merged commit b378367 into main Nov 17, 2026
9 checks passed
@Qubitium
Qubitium deleted the feat/kernel-graph-compile-support branch November 17, 2026 13:01
Qubitium added a commit that referenced this pull request Nov 17, 2026
…4736) (#4774)

The two new readers are registered kernels, so the fusion inventory must
account for them, and #4737's convention is that each compile-level
declaration is pinned by a measured case in the per-family matrix.

- tests/module_tree/test_fusion_kernel_backends.py: add the three CPU-only
  portable readers (GEMV #4554, GEMV_FAST and LLM_AWQ #4736) to EXCLUDED with
  the reason they have no fused executor adapter. The test fails on this
  branch without the entry.
- tests/kernels/test_kernel_compile_levels_awq.py: add CPU cases for the two
  new readers. Both measure eager + dynamo + aot_eager, matching the declared
  AOT_EAGER; inductor/cudagraph are device-only skips and capture is not
  claimed.

No runtime code change.

Co-authored-by: Qubitium <[email protected]>
Qubitium added a commit that referenced this pull request Nov 17, 2026
…4822)

#4737 moved the compile guard in front of the decode-range cache key and
returned False for every dtype, so _decode_dtype_for_activation fell through
to FP64 inside every compiled graph. linear_wide_bias() then promoted the
product, running the whole GEMM in FP64: the compiled AwqTorchLinear was
slower than eager and its output drifted from the eager one.

Decide the per-dtype range answers eagerly, where the scales can change
(post_init, a device/dtype move, a state-dict load, a training transition),
and read them back inside the compiled region as plain Python constants
instead of escalating to FP64.
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