Skip to content

fix(generate): stop forcing trace-clean kernels eager (#4984) - #5006

Merged
Qubitium merged 1 commit into
mainfrom
root/compile-policy-bnb-4984
Nov 17, 2026
Merged

Qubitium merged 1 commit into
mainfrom
root/compile-policy-bnb-4984

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

What this fixes

Closes #4984.

BaseQModel._kernels_support_compile() (the #4669/#4739 policy) disables
transformers' static-cache auto-compile unless every quantized linear declares
SUPPORTS_COMPILE. BitsAndBytesLinear and MacaqLinear both declare
SUPPORTS_COMPILE_LEVEL = CUDAGRAPH - they trace with fullgraph=True to one
graph with zero breaks - but inherited the False default, so
generate(cache_implementation="static") silently added
disable_compile=True and ran the eager path.

Measured before / after

Qwen2.5-0.5B-Instruct quantized to BitsAndBytes NF4 (168 modules, all
BitsAndBytesLinear), 4 left-padded prompts x 48 greedy tokens, fp16, PG506
(sm_80), torch 2.15.0.dev+cu130, one shared GPU. Steady state = median of the
last three calls.

run dynamic cache static default static eager (disable_compile=True) static forced compile dynamo
before (main) 1.873 s 1.795 s 1.779 s 0.607 s 1 graph, 0 breaks
after 1.961 s 0.367 s 2.012 s 0.311 s 1 graph, 0 breaks

The policy was the whole difference: on main the default path was the eager
path (1.795 s) while the compiled path it suppressed was 2.9x faster. After the
opt-in the default path takes the compiled route (5.5x vs eager in that
process). Greedy tokens are identical across dynamic/eager/compiled on all four
rows here; the first compiled call costs 18-35 s of compile time.

The general rule

A kernel declaring SUPPORTS_COMPILE_LEVEL >= INDUCTOR has a traceable,
lowerable forward, which is half of the documented bar. It must now answer the
other half explicitly: opt in, or record the measured loss in the new
SUPPORTS_COMPILE_EAGER_REASON. Silent opt-outs are rejected by
tests/kernels/test_kernel_compile_protocol.py, and the reason is published in
docs/kernel-compile-matrix.md.

Recorded reasons (all CUDAGRAPH level, all measured/known losses at the
model level):

  • QQQTorchLinear - static-cache generate recompiles per step (172 graphs,
    24-115 breaks), 1.3-8x slower than eager
  • ParoTritonLinear - same model-level loss; the single-layer trace is clean
  • VPTQLinear - no measured static-cache win for the portable torch path
  • AwqGEMVTritonLinear / AwqGEMVTorchLinear - explicit-selection-only and
    CPU-only paths, never timed under generate()

MacaqTritonLinear breaks the graph on torch.cuda.device_count() and keeps
False (below the floor, so no reason needed). BitsAndBytesMlxQuantLinear and
MacaqMlxQuantLinear re-declare False so they do not inherit the Torch
opt-in for their native Metal matmuls.

Tests

New tests/kernels/test_compile_policy_levels_4984.py (hardware re-measure +
static registry sweep) and three cases in
tests/test_generation_compile_policy_4669.py.

Red on unmodified origin/main (test files copied into a detached
origin/main worktree, fix absent): 6 failed, 15 passed

  • test_a_trace_clean_kernel_is_not_forced_eager[BitsAndBytesLinear] -
    "traces to CUDAGRAPH with 0 graph breaks ... yet SUPPORTS_COMPILE is False and
    no SUPPORTS_COMPILE_EAGER_REASON is recorded"
  • test_a_trace_clean_kernel_is_not_forced_eager[MacaqLinear] - same
  • test_every_compile_capable_kernel_pairs_its_policy_with_a_reason - 7
    kernels listed
  • test_kernel_declarations - assert False is True where False = BitsAndBytesLinear.SUPPORTS_COMPILE
  • test_a_compile_capable_kernel_keeps_the_auto_compile - the policy harness
    returns False for a MacaqLinear
  • test_a_compile_capable_opt_out_must_record_its_reason - helper missing

Green with the fix

  • tests/kernels/test_kernel_compile_protocol.py +
    tests/test_generation_compile_policy_4669.py +
    tests/kernels/test_compile_policy_levels_4984.py: 66 passed (CPU),
    3 passed for the new file on GPU 7
  • tests/kernels/test_kernel_compile_levels_quant.py (GPU, 17 families):
    13 passed, 4 skipped - identical to the unmodified-main run (13 passed, 4 skipped), so no level declaration changed behaviour
  • tests/kernels/test_compile_capability_4739.py,
    tests/kernels/test_static_cache_compile_4669.py,
    tests/test_docs_contract_refs.py: 20 passed, 4 skipped
  • tests/test_bitsandbytes.py, tests/test_bnb_hf_refusal_4664.py:
    36 passed, 19 skipped
  • ruff check clean on every changed Python file
  • docs/kernel-compile-matrix.md regenerated with
    python -m tests.kernels.gen_compile_matrix (idempotent on re-run)

Files

  • gptqmodel/nn_modules/qlinear/bitsandbytes.py, macaq.py - opt in
  • gptqmodel/nn_modules/qlinear/__init__.py - SUPPORTS_COMPILE_EAGER_REASON
  • gptqmodel/nn_modules/qlinear/_compile_support.py - the reason/level pairing
    helper and COMPILE_POLICY_FLOOR
  • gptqmodel/nn_modules/qlinear/{qqq,paro_triton,vptq_torch,gemv_awq,mlx}.py -
    recorded reasons / explicit opt-outs
  • gptqmodel/models/base.py - policy uses supports_compiled_generation()
  • docs/generation-compile-policy.md, docs/kernel-compile-matrix.md,
    tests/kernels/gen_compile_matrix.py
  • tests/kernels/test_compile_policy_levels_4984.py (new),
    tests/kernels/test_kernel_compile_protocol.py,
    tests/test_generation_compile_policy_4669.py

`BaseQModel._kernels_support_compile()` turns transformers' static-cache
auto-compile off unless every quantized linear declares `SUPPORTS_COMPILE`.
`BitsAndBytesLinear` and `MacaqLinear` both declare
`SUPPORTS_COMPILE_LEVEL = CUDAGRAPH` (a `fullgraph=True` trace with zero graph
breaks) but inherited the `False` default, so
`generate(cache_implementation="static")` added `disable_compile=True` and ran
the eager path.

Measured on this host (PG506, sm_80, torch 2.15.0.dev+cu130): Qwen2.5-0.5B
quantized to BitsAndBytes NF4, 4 left-padded prompts x 48 greedy tokens.

  before: static default 1.795 s, static eager 1.779 s, forced compile 0.607 s
          (1 graph, 0 breaks) - the default path lost 2.9x
  after:  static default 0.367 s (1 graph, 0 breaks), static eager 2.012 s
          - the default path now takes the compiled route (5.5x)

`MacaqTritonLinear` breaks the graph on `torch.cuda.device_count()` and keeps
`SUPPORTS_COMPILE = False`. The two MLX holders
(`BitsAndBytesMlxQuantLinear`, `MacaqMlxQuantLinear`) re-declare `False` for
their native Metal matmuls instead of inheriting the Torch opt-in.

The general drift is codified: a kernel that reaches `INDUCTOR` or higher must
either opt in or record the measured loss in the new
`SUPPORTS_COMPILE_EAGER_REASON`. `QQQTorchLinear`, `ParoTritonLinear`,
`VPTQLinear` and the two GEMV kernels now record theirs.
`tests/kernels/test_kernel_compile_protocol.py` enforces the pairing,
`docs/kernel-compile-matrix.md` publishes the reason column, and
`tests/kernels/test_compile_policy_levels_4984.py` re-measures the trace on real
hardware.

Regression test: 6 failed / 15 passed on unmodified `origin/main`, all pass
with the fix.
@Qubitium
Qubitium force-pushed the root/compile-policy-bnb-4984 branch from 7d95c1a to 130b08b Compare November 17, 2026 22:01
@Qubitium
Qubitium merged commit 291738f into main Nov 17, 2026
9 checks passed
@Qubitium
Qubitium deleted the root/compile-policy-bnb-4984 branch November 17, 2026 22:06
@Qubitium

Copy link
Copy Markdown
Collaborator Author

Independent verification on GB10 (sm_121), torch 2.15, tiny 2-layer Llama with 4-bit NF4 BitsAndBytesLinear linears, 4 prompts x 32 greedy tokens, cache_implementation="static", steady state (min of 3 calls after the compile warm-up):

policy answer static-cache decode
main (BnB declares False) _kernels_support_compile() == False 0.100 s (eager, forced)
this PR _kernels_support_compile() == True 0.034 s

3.0x with the default policy, identical greedy tokens, one graph. tests/test_generation_compile_policy_4669.py tests/kernels/test_kernel_compile_protocol.py tests/kernels/test_compile_policy_levels_4984.py tests/kernels/test_compile_capability_4739.py tests/kernels/test_static_cache_compile_4669.py → 71 passed, 1 failed only at test_static_cache_does_not_compile_a_graph_breaking_kernel, which needs the Marlin JIT that cannot build on this host (same failure on main).

The SUPPORTS_COMPILE_EAGER_REASON requirement and the MacaqTritonLinear / MLX-holder opt-outs are the right shape: they keep QQQ/VPTQ/Paro-Triton eager and make the next drift a test failure instead of a silent 2-9x regression.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant