You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
torch.compile fullgraph breaks on CPU: matmul_4bit mutates quant_state.dtype #1917
matmul_4bit() mutates quant_state.dtype in-place on CPU (_functions.py:387):
ifA.device.type=="cpu":
quant_state.dtype=A.dtype
Under torch.compile(fullgraph=True) with activation checkpointing, Dynamo flags this as:
torch._dynamo.exc.Unsupported: HigherOrderOperator: Mutating a variable
not in the current scope (SideEffects)
This only affects CPU — on CUDA the branch is not taken, so GPU compilation works fine.
How discovered
The regression test added in #1916 (test_linear4bit_torch_compile_activation_checkpointing) exercises fullgraph=True + torch.utils.checkpoint. It passes on all GPU backends but fails on CPU with torch 2.10.0 due to this pre-existing mutation. The test now skips on CPU to keep #1916 focused on the __getattr__ → @property fix.
Suggested fix
Refactor matmul_4bit() to avoid mutating quant_state.dtype in-place. For example, pass the input dtype as a parameter to the downstream functions instead of writing it onto the shared QuantState object. This would make matmul_4bit safe for Dynamo tracing on CPU.
Reproducer
importtorchimportbitsandbytesasbnbclassCheckpointedNet(torch.nn.Module):
def__init__(self):
super().__init__()
self.layers=torch.nn.ModuleList([
bnb.nn.Linear4bit(256, 256, bias=False, compute_dtype=torch.bfloat16, quant_type="nf4")
for_inrange(4)
])
defforward(self, x):
forlayerinself.layers:
x=torch.utils.checkpoint.checkpoint(layer, x, use_reentrant=False)
returnxnet=CheckpointedNet().to("cpu")
compiled=torch.compile(net, fullgraph=True, backend="inductor")
x=torch.randn(16, 256, dtype=torch.bfloat16, requires_grad=True)
compiled(x).sum().backward() # raises Unsupported: Mutating a variable not in the current scope
Environment
torch 2.10.0
CPU only (CUDA is unaffected)
Observed on linux-x64, linux-aarch64, linux-x64-icelake, macOS
Activity
Skip activation checkpointing compile test on CPU (#1917)
Remove dead quant_state.dtype mutation in matmul_4bit CPU path (#1917)
Merge pull request #1916 from bitsandbytes-foundation/fix/issue-1904