Repository navigation
feat(kernels): declare torch.compile and CUDA-graph capability per kernel - #4737
Merged
Merged
Conversation
…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).
13 of 15 tasks
This was referenced Nov 17, 2026
Merged
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]>
This was referenced Nov 17, 2026
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Every quantized kernel now declares, in its own class body, how far
torch.compilecan take itsforwardand whether the eager forward is safe inside a CUDA-graph capture. 68/68 kernels declare both (67 registered kernels plus the role-selectedTorchQuantEmbeddings); the registry test fails if a new kernel forgets.This is the capability layer on top of #4669/#4727's
SUPPORTS_COMPILEpolicy flag, which is unchanged.Protocol
gptqmodel/nn_modules/qlinear/_compile_support.py:plus
SUPPORTS_GRAPH_CAPTURE: boolfor the separate question "may the eager forward run insidetorch.cuda.graph(...)?" — a custom op without a meta implementation can be capture-safe yet untraceable, and a traceable kernel can still sync the host inforward. 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-autotuneis anINDUCTORtuning mode, not a level. Measured on NVIDIA GB10 (sm_121), torch 2.11.0+cu130 — sametorch.compileAPI as the supported floor.Coverage
CUDAGRAPHAwqTorchLinear,AwqGEMVTorchLinear,AwqGEMVTritonLinear,QQQTorchLinear,TorchFP8Linear,VPTQLinear,MacaqLinear,ParoQuantTritonLinearEAGERNOT_APPLICABLETorchQuantEmbeddings(role-selected embedding,EAGER)13 kernels additionally declare eager CUDA-graph capture:
TorchLinear,TritonV2Linear,AwqGEMMTritonLinear,MacaqTritonLinear,GGUFTorchLinear,GGUFTritonKernel,W4AFP8Linear,W4ANVFP4Linear, plus the fiveCUDAGRAPHkernels 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, retiredTorchInt8AwqLinear) are declared conservatively from a static scan and their level test skips with the exact reason — never faked.docs/kernel-compile-matrix.mdis generated from the declarations and enforced by the protocol test.Behavioral fix found by the audit
AwqTorchLinearsetsSUPPORTS_COMPILE=True(#4669) but still broke the graph:_stored_decode_range_fitscomputedtensor_cache_key(is_inference()/data_ptr()) before checkingis_compiling()/is_current_stream_capturing(). Moving the guard first makes the forward trace with 0 graph breaks and reachCUDAGRAPH; 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
tests/kernels/test_kernel_compile_protocol.py(new).item()/.tolist()/.cpu()/data_ptr()/synchronizein its ownforward,SUPPORTS_COMPILE⇒ ≥DYNAMO, docs matrix == codetests/kernels/test_kernel_compile_levels_{gptq,awq,quant,nontorch}.py(new)tests/kernels/compile_probe.py(new)explain, fullgraph eager/aot_eager/inductor/reduce-overhead, explicittorch.cuda.CUDAGraphtests/kernels/gen_compile_matrix.py(new)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.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 unmodifiedmain— the failures are pre-existing cross-file test-isolation issues on this host, not this change.ruff checkclean on all 53 files.Docs
docs/kernel-compile-matrix.md(test-synced)docs/generation-compile-policy.md: capability vs policy sectiondocs/torch-compile-fallback.md: cross-linkdocs/README.md: index entry