Skip to content

fix(vptq): skip the per-layer compile when the GIL is disabled (#5228) - #5244

Merged
Qubitium merged 1 commit into
mainfrom
fix/vptq-free-threaded-compile-5228
Nov 18, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/vptq-free-threaded-compile-5228

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #5228.

Problem

VPTQLinear.optimize() compiles each module's bound dequantize, so every VPTQ layer installs its own torch.compile wrapper around the same code object. _install_callable_compile_fallback serializes only the first call of one wrapper, so a compiled call on layer A can run while layer B is still tracing that shared code object. Under free-threaded Python dynamo's per-code-object cache is not safe against that; the racing call can use the wrong specialization and return finite but completely wrong tensors (relative error 1-6) with nothing raised.

Change

Skip the per-layer compile when the GIL is disabled:

This is the "skip the lazy compile entirely when has_gil_disabled()" option from the issue; the per-layer graph can be re-enabled once dynamo's per-code-object cache is thread-safe.

Tests

tests/test_vptq.py:

  • GIL disabled -> optimize() installs no wrapper and VPTQLinear.dequantize remains the eager method.
  • GIL enabled -> the compile still runs (unchanged behaviour).

format/check.sh passes. The test_vptq_cuda_* failures on this host are pre-existing (JIT extension cannot build, CUDA_HOME unset); all 232 non-CUDA VPTQ tests pass.

`VPTQLinear.optimize()` compiles each module's bound `dequantize`, so every
VPTQ layer installs its own `torch.compile` wrapper around the same code
object. `_install_callable_compile_fallback` only serializes the first call of
one wrapper; a compiled call on layer A can therefore run while layer B is
still tracing that shared code object. Under free-threaded Python dynamo's
per-code-object cache is not safe against that, and the racing call can use
the wrong specialization and return finite but completely wrong tensors
(relative error 1-6) with nothing raised.

Skip the per-layer compile when the GIL is disabled. `has_gil_disabled()` is
read at `optimize()` time, so a free-threaded build whose torch import
re-enables the GIL still compiles. The model-level forward compile
(`model.optimize()` -> unbound `forward`) is a single wrapper and still runs,
so the compiled `generate()` path measured in #5102 is kept.

Tests cover both branches: with the GIL disabled no wrapper is installed and
the eager `VPTQLinear.dequantize` stays reachable; with the GIL enabled the
compile still runs.

The `test_vptq_cuda_*` failures in this environment are pre-existing: the JIT
extension cannot build because `CUDA_HOME` is unset.
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