Skip to content

fix(paro): make the non-identity rotation trace- and capture-clean (#5225) - #5258

Merged
Qubitium merged 1 commit into
mainfrom
fix/5225-paro-triton-cudagraph
Nov 18, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/5225-paro-triton-cudagraph

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #5225.

Problem

ParoTritonLinear declares COMPILE_LEVEL.CUDAGRAPH, but every non-identity Paro layer graph-broke twice, so optimize() on a real Paro checkpoint was slower than eager (52 breaks / 50 graphs, 0.6x). The identity-rotation compile-level test never reached the rotation at all, which is how the false claim passed CI.

flowchart TD
    subgraph before["Before: every non-identity layer breaks"]
      A1["compiled forward"] --> B1["_resolve_rotation_launch"]
      B1 --> C1["_ensure_rotation_autotune_cache_loaded"]
      C1 --> D1["with _ROTATION_AUTOTUNE_DISK_LOCK<br/>(taken before the loaded check)"]
      D1 --> E1["Unsupported context manager<br/>graph break per layer"]
      B1 --> F1["gptqmodel_paro::rotate<br/>no Meta impl"]
      F1 --> G1["Operator does not support<br/>running with fake tensors"]
      E1 --> H1["52 breaks / 50 graphs<br/>0.6x eager"]
      G1 --> H1
    end
    subgraph after["After: one graph, fused op"]
      A2["compiled forward"] --> B2["_resolve_rotation_launch"]
      B2 --> C2["lock-free cache read<br/>(cold shape → native default)"]
      C2 --> D2["Meta impls give fake-tensor metadata"]
      D2 --> E2["fused rotate traced"]
      E2 --> F2["0 breaks / 1 graph<br/>1.37x eager, bitwise equal"]
    end
Loading

Changes

  • gptqmodel/utils/paro.py
    • _ensure_rotation_autotune_cache_loaded checks the per-device loaded flag before the lock, so a warmed process never enters it on the steady-state rotation path.
    • _resolve_rotation_launch is trace-safe: while compiling it reads the process-local launch cache without a lock, and a cold shape returns the native legacy launch config (-1, -1) instead of touching disk or benchmarking.
    • _rotation_extension_ready_for_call keeps a cold JIT build out of a trace; the reference rotation is used for that trace and the fused op afterwards.
  • gptqmodel_ext/paro/rotation.cu: Meta implementations for rotate, rotate_pair, rotate_pair_grad, grad_theta, registered via TORCH_LIBRARY_IMPL(gptqmodel_paro, Meta, ...). They allocate exactly the metadata the CUDA kernels allocate and never touch device memory.
  • gptqmodel/nn_modules/qlinear/paro.py: ParoLinear.optimize() prewarms the fused rotation extension before torch.compile traces the layer, so the compiled graph keeps the fused op rather than the reference fallback.
  • gptqmodel/nn_modules/qlinear/paro_triton.py: _select_plan returns the default plan while compiling instead of benchmarking kernels (host timing) inside a trace.
  • tests/kernels/test_kernel_compile_levels_quant.py: _build_paro now uses group_size=128 with a non-identity theta/channel_scales, so the level test actually exercises the fused rotation. (Identity returns early; group_size 32 is rejected by the fused kernel.)
  • tests/kernels/test_paro_triton_cudagraph_5225.py (new, 5 tests): non-identity layer traces with 0 breaks under eager/aot_eager/inductor; reaches the declared CUDAGRAPH level; the fused ops run under FakeTensorMode with CUDA metadata; model-level optimize() installs a 0-break forward; cold-trace falls back safely.
  • docs/paro.md: new "Compiled inference and CUDA graphs" section.

Evidence (RTX 5090, sm_120, torch 2.15.0+cu132, Triton 3.9.0)

4-layer ParoTritonLinear model, optimize(backend="inductor"), interleaved min-of-50:

tree graph breaks unique graphs compiled vs eager output
pristine upstream/main (7fcb03e85) 14 4 0.292 ms 0.47x maxdiff 0.0
this PR 0 1 0.104 ms 1.37x maxdiff 0.0

Single non-identity layer: red 1 break / 2 graphs and fullgraph=True fails; green 0 breaks / 1 graph, exact match under eager, aot_eager, inductor, and reduce-overhead CUDA-graph capture.

Tests:

  • tests/kernels/test_paro_triton_cudagraph_5225.py → 5 passed (5 failed pre-fix).
  • tests/kernels/test_paro.py + test_kernel_compile_levels_quant.py + the new file → 82 passed, 3 skipped.
  • uvx [email protected] check gptqmodel docs/eora tests setup.py → clean; git diff --check clean.

Not verified

  • ROCm/gfx1201 hardware (the issue's box). The Meta implementations are pure ATen metadata and the lock/compile guards are device-independent, but the 0-break result here is CUDA-verified.
  • The real ParoQuant checkpoint from the issue is not in the offline cache; the model-level bench uses the same layer construction as tests/kernels/test_paro.py::_paro_module.

…5225)

`ParoTritonLinear` declares COMPILE_LEVEL.CUDAGRAPH, but every real (non-identity)
Paro layer graph-broke twice, so `optimize()` on a Paro checkpoint was slower
than eager:

* `_resolve_rotation_launch` called `_ensure_rotation_autotune_cache_loaded`,
  which entered `_ROTATION_AUTOTUNE_DISK_LOCK` before its "already loaded"
  check. Dynamo cannot trace a `threading.Lock` context manager.
* The fused `gptqmodel_paro` rotation ops are CUDA-only and had no Meta
  implementation, so fake-tensor tracing raised
  "Operator does not support running with fake tensors".

Fix:
* check the per-device loaded flag before taking the lock, and make
  `_resolve_rotation_launch` trace-safe (lock-free process-local cache read;
  a cold shape falls back to the native legacy launch config instead of
  touching disk).
* register Meta implementations for `rotate`, `rotate_pair`,
  `rotate_pair_grad` and `grad_theta`, returning exactly the metadata the CUDA
  kernels allocate.
* do not JIT-load the extension from inside a trace
  (`_rotation_extension_ready_for_call`), and prewarm it in
  `ParoLinear.optimize()` so the compiled graph keeps the fused op.
* `ParoTritonLinear._select_plan` returns the default plan while compiling
  instead of benchmarking kernels on the host.
* the compile-level test now builds a non-identity rotation with
  group_size=128 (the identity shortcut and group_size 32 never reached the
  fused op, which is how the false CUDAGRAPH claim passed).

Measured on RTX 5090 (sm_120), torch 2.15.0+cu132, 4-layer model:
before 14 breaks / 4 graphs / 0.47x eager; after 0 breaks / 1 graph /
1.37x eager, bitwise-identical output.
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