Repository navigation
fix(vptq): skip the per-layer compile when the GIL is disabled (#5228) - #5244
Merged
Merged
Conversation
`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.
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.
Fixes #5228.
Problem
VPTQLinear.optimize()compiles each module's bounddequantize, so every VPTQ layer installs its owntorch.compilewrapper around the same code object._install_callable_compile_fallbackserializes 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:
has_gil_disabled()is evaluated atoptimize()time, so a free-threaded build whose torch import re-enables the GIL still compiles.model.optimize()-> unboundforward) is a single wrapper and still runs, keeping the compiledgenerate()path from [PERF] #5006 (fix of #4984) incomplete: QQQTorchLinear and VPTQLinear stay off the static-cache compile with a stale/unmeasured reason though they trace as 1 graph/0 breaks; 2.6-4.5x slower generate on ROCm AUTO and CUDA bf16 QQQ #5102.optimizedis still set, and the eager class method stays reachable, soforwardis unaffected.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:optimize()installs no wrapper andVPTQLinear.dequantizeremains the eager method.format/check.shpasses. Thetest_vptq_cuda_*failures on this host are pre-existing (JIT extension cannot build,CUDA_HOMEunset); all 232 non-CUDA VPTQ tests pass.