Skip to content

perf(generate): skip the static-cache auto-compile on graph-breaking kernels (#4669) - #4727

Merged
Qubitium merged 1 commit into
mainfrom
fix/static-cache-compile-optout-4669
Nov 17, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/static-cache-compile-optout-4669

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Refs #4669 — fixes the 2-4x static-cache slowdown (item 1); the optimize()-vs-generate() gap (item 2) is documented as remaining rather than guessed at.

Problem

Transformers compiles the forward pass of generate(cache_implementation="static") (CompileConfig, mode="reduce-overhead"). Every GPT-QModel CUDA kernel but the Torch-AWQ reference path calls a custom op without a fake implementation, or branches on host values (data_ptr()/id()/._version) inside forward, so dynamo breaks the graph on every quantized linear. CUDA-graph capture never covers a layer, and each decode step pays for the fragments and guards: the static cache runs 2-4x slower than eager with no warning, on the setting transformers recommends for fast decoding.

Fix

  • BaseQuantLinear.SUPPORTS_COMPILE (default False) declares whether compiling the kernel forward is not a pessimization. False does not mean untraceable; it means the graph breaks and the eager path is fast enough that the fragments lose.
  • AwqTorchLinear opts in (True, reporter's 1B 4090 measurement 1.36 s → 0.76 s plus parity here). ParoLinear re-declares False (host row-count branches + custom ops).
  • BaseQModel.generate() sets disable_compile=True only when a quantized linear does not declare support. Every explicit caller choice wins: disable_compile kwarg or on generation_config, an explicit compile_config, a dense model, or an all-compile-clean kernel set. A supplied generation_config is deep-copied, never mutated. Logged once.
  • docs/generation-compile-policy.md records the decision flow, the kernel table, and the remaining optimize() work.

Measurements (tiny Llama 2×256, g128 4-bit, 4 prompts × 32 greedy tokens, GB10 sm_121)

run dynamic static static disable_compile=True dynamo graphs
before (main) 0.096 s 9.69 s first / 0.211 s steady 0.103 s 19
after 0.097 s 0.099 s 0.099 s 0
AWQ_TORCH after (opted in) 0.168 s 0.157–0.257 s 0.175 s 20 (still compiles, intended)

Tokens: static == dynamic == eager-static after the fix.

Tests

  • tests/test_generation_compile_policy_4669.py — 14 CPU tests: breaking kernel ⇒ opt-out; dense and compile-clean kernels unchanged; explicit disable_compile/compile_config/generation_config respected; caller config not mutated; all three generate() input branches; _orig_mod unwrapped.
  • tests/kernels/test_static_cache_compile_4669.py — GPU: static-cache generate compiles 0 graphs, tokens equal eager; disable_compile=False still compiles. Red on main: AssertionError: the static-cache path compiled {calls_captured: 144, unique_graphs: 19} despite breaking kernels.
  • Existing generation suites: test_generate_attention_mask, test_generation_config_do_sample, test_legacy_generation_compat, test_auto_adapter_generate, test_hf_config_compat, test_checkpoint_interop_4053, test_vllm_generate_alignment → 111 passed, 3 skipped.
  • ruff check gptqmodel tests docs clean.

Residual

  • Item 2 (optimize() compiles a wrapper generate() bypasses) is documented, not changed: fixing it changes what self.model is (state-dict/save implications), a separate change.
  • SUPPORTS_COMPILE=True for AwqTorchLinear rests on the reporter's 1B measurement plus parity on a tiny fixture here, not a 1B run on this GPU.
  • CI's awq_cpu.yml does not collect tests/kernels; the CPU policy tests are the gate, as for the other Marlin kernel tests.

…kernels (#4669)

transformers compiles the forward pass of a static-cache generate()
(CompileConfig, mode="reduce-overhead"). Every GPT-QModel CUDA kernel but the
Torch-AWQ reference path calls a custom op without a fake implementation or
branches on host values (data_ptr()/id()/._version) inside forward, so dynamo
breaks the graph on every quantized linear, CUDA-graph capture never covers a
layer, and every decode step pays for the fragments and guard checks. Measured
on a tiny 4-bit Llama (2 layers, hidden 256, g128) with the AUTO (Marlin)
kernel: dynamic cache 0.097 s, static cache 0.211 s steady state with 19
compiled graphs, and disable_compile=True 0.103 s.

- BaseQuantLinear.SUPPORTS_COMPILE (default False) declares whether compiling
  the kernel's forward is not a pessimization; False does not mean the forward
  is untraceable, it means the graph breaks and the eager path is fast enough
  that the fragments lose. AwqTorchLinear opts in with the reporter's 1B
  measurement (1.36 s -> 0.76 s) plus parity here (0.168 s -> 0.157-0.257 s);
  ParoLinear re-declares False because its rotation/GEMM path branches on host
  row counts and calls custom ops.
- BaseQModel.generate() disables transformers' auto-compile for the call when
  any quantized linear does not declare it, and only then. An explicit
  disable_compile (kwarg or on generation_config), an explicit compile_config,
  a dense model, or an all-compile-clean kernel set keeps the caller's policy.
  A supplied generation_config is deep-copied, never mutated.
- docs/generation-compile-policy.md documents the decision flow and the
  remaining item: optimize() still compiles a wrapper that generate() bypasses.

After: static-cache generate() compiles 0 graphs, runs at dynamic-cache speed
(0.099 s vs 0.097 s) and returns the same greedy tokens; AWQ_TORCH keeps
compiling. Tests: tests/test_generation_compile_policy_4669.py (14 CPU tests)
and tests/kernels/test_static_cache_compile_4669.py (GPU; fails on the parent
commit with "the static-cache path compiled {'calls_captured': 144,
'unique_graphs': 19} despite breaking kernels").
@Qubitium
Qubitium merged commit b225a43 into main Nov 17, 2026
9 checks passed
@Qubitium
Qubitium deleted the fix/static-cache-compile-optout-4669 branch November 17, 2026 12:14
Qubitium added a commit that referenced this pull request Nov 17, 2026
… trace-clean + measured (#4739) (#4754)

* perf(compile): make SUPPORTS_COMPILE mean trace-clean and measured faster (#4739)

TorchFP8Linear traces clean under fullgraph=True and its compiled static-cache
decode is 2.3x faster than eager on sm_121, but #4727 left it opted out, so FP8
static-cache generation silently stayed eager. AwqTorchLinear was the only
opt-in, yet tensor_cache_key() calls tensor.is_inference(), a hard dynamo graph
break: fullgraph=True fails and compiling it is 20-30% slower.

The declaration now requires both halves -- a zero-graph-break fullgraph trace
and a measured win -- and each kernel is judged by that bar.

* test(compile): drop the duplicate FP8 warm-up assignment (#4739)
Qubitium added a commit that referenced this pull request Nov 17, 2026
Fix the test-side fallout of #4716, #4657, #4627 and #4508. Only entries
that fail on this sm_80 host (PG506) and whose stated reasoning reproduces
were changed; the rest are recorded with their outcome.

1. 8587f0b (#4716, native FP8 GEMM for a wide bias)
   - tests/kernels/test_fp8_kernel.py::test_fp8_default_forward_is_w8a16
     REPRODUCED on sm_80: 6 bf16 cases failed with 1-ulp (0.0078125) diffs
     against rtol/atol 2e-3. The oracle now narrows the decoded weight to
     the activation dtype, matching the native path; the bound is the
     half-ulp at unity (2^-9 ~= 2e-3). FIXED.
   - tests/test_fp8_bias_range.py::test_fp8_gpu_dequant_fallback_keeps_bias_wide
     REPRODUCED on sm_80 (fp16: actual 0.0 vs oracle 0.0078125). Renamed to
     test_fp8_gpu_native_gemm_keeps_bias_wide and rewritten for the #3737
     intent: finite output, FP32 bias, residual bounded by one activation-
     dtype ulp of |x.w|. FIXED.

2. bf5adeb (#4657, Marlin bias kept in checkpoint order)
   - tests/test_marlin_scratch_cuda.py 288/200 cases REPRODUCED on sm_80:
     8 failed with "b_bias.size(0) != size_n". scripts/marlin_scratch_fixture.py
     now passes layer._kernel_bias(a) (padded, tile-ordered FP32) instead of
     the checkpoint-order layer.bias. FIXED (8 failed -> 68 passed, 2 skipped).

3. 60376e9 (#4714, default placement in one capability group)
   - tests/test_tenstorrent_support.py already passes on current main: the
     monkeypatched _record accepts **kwargs (fixed upstream by 50f1752).
     ALREADY PASSES on sm_80.

4. 34672a7 (#4585, modules snapshot the kernel policy)
   - tests/kernels/test_bitsandbytes_rocm_triton.py triton cases SKIP on
     sm_80 (no tuned bitsandbytes tile for this family). DEFERRED: the
     stated failure cannot be reproduced here.

5. dfa180d (#4627, GGUF path chosen from static gates)
   - both tests REPRODUCED on sm_80 with AttributeError
     _benchmark_forward_runner. The removed timing hook is no longer patched;
     each test asserts the hook stays absent. FIXED.

6. fd8d524 (#4599, AwqGEMVTorchLinear CPU reader)
   - both kernel-registry contract tests already pass on current main
     (AwqGEMVTorchLinear is in EXCLUDED and the ROCm audit table; fixed
     upstream by ab151fe). ALREADY PASSES on sm_80.

7. d2a0508 (#4283, dense-module adapters)
   - tests/kernels/test_gptq.py::TestKernelOutput errors on sm_80 before the
     adapter path: this host's flash_attn_2_cuda has an undefined symbol, so
     the stated Lora assertion is never reached. DEFERRED: blocked by the
     pre-existing host flash_attn failure; the fix cannot be verified here.

8. f201837 (#4508, activation-quantization wording)
   - both tests REPRODUCED on sm_80. The refusal still fires; the regex now
     matches "activation quantized|activation scales". FIXED.

9. b225a43 (#4727, static-cache compile test)
   - ROCm-only failure. On sm_80 the CUDA leg runs and fails earlier on the
     host flash_attn symbol, so the ROCm gate cannot be reproduced.
     DEFERRED.

Test run (GPU 4):
  tests/kernels/test_fp8_kernel.py tests/test_fp8_bias_range.py
  tests/test_marlin_scratch_cuda.py tests/test_marlin_scratch_benchmark.py
  tests/kernels/test_gguf_cpp_activation_quantization.py
  tests/kernels/test_gguf_cpp_path_selection.py tests/test_auto_module_decoder.py
  => 254 passed, 35 skipped, 1 failed.
The single failure is the pre-existing sm_80
test_fp8_w8a8_forward_is_batch_invariant (native torch._scaled_mm needs
sm_89+); it fails identically on origin/main.
Qubitium added a commit that referenced this pull request Nov 17, 2026
…4669) (#4912)

`GPTQModel.generate()` calls `self.model.generate`; transformers' `generate`
calls `self(...)`, i.e. the module's own `forward`. `optimize()` replaced
`self.model` with a `torch.compile` `OptimizedModule`, whose `__getattr__`
hands `generate` back to the unwrapped module, so generation stayed on the
eager `forward` and dynamo compiled 0 frames (#4669 item 2).

Compile `model.forward` in place instead. `self.model` stays the original
`nn.Module`, a direct `model(...)` call and `generate()` both enter the
compiled path, the compile fallback is preserved, and a repeated `optimize()`
unwraps the previous proxy instead of stacking wrappers.

Measured on Llama-3.2-1B-Instruct GPTQ (GPTQ_TORCH, fp16, GB10, 4 prompts x
16 greedy tokens, steady state):

  before optimize: dynamic 0.250 s (256 tok/s), static 0.251 s (255 tok/s),
                   0 dynamo graphs
  after optimize:  dynamic 0.316 s (202 tok/s, 122 graphs on the first call),
                   static 0.288 s (222 tok/s)

Greedy tokens matched the eager dynamic and static runs here; compiling can
still flip a near-tie token, as documented for FP8/AWQ. The static-cache
auto-compile stays off by default for graph-breaking kernels (#4727);
`optimize()` is the explicit opt-in, and such a kernel is slower compiled,
which the issue predicted.

Item 1 (static-cache 2-4x slowdown) is already fixed by #4727 and re-verified
on this box: static 0.251 s vs dynamic 0.250 s, 0 graphs, identical tokens.
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