Repository navigation
Conversation
bitsandbytes-foundation#2034) MatMul4Bit.forward stored the packed weight as a plain ctx attribute (ctx.tensors = (None, B)) instead of through save_for_backward. In the ordinary case this is harmless -- the weight is a stable Parameter. It breaks for a caller that re-materialises the weight (weight offloading, layer streaming) into a recycled device buffer under gradient checkpointing: torch.utils.checkpoint discards and recomputes *saved* tensors, so the recompute is expected to hand backward a fresh copy of the weight. A raw ctx attribute is invisible to that mechanism -- the reference taken in the original forward survives into backward and, by then, points at a buffer refilled with a different layer, producing silently wrong gradients (forward stays bit-exact). bf16 through the same harness is unaffected because MmBackward0 already uses save_for_backward. Switch to ctx.save_for_backward(B) in forward and read it back from ctx.saved_tensors in backward. The QuantState stays on ctx.state since it is not a tensor. No behavioural change in the ordinary path.
|
save_for_backward(B) addresses the packed-weight lifetime issue in #2034. I hit the same class of lifetime issue in streamed 4-bit training. I wonder whether the same issue also applies to ctx.state = quant_state, since QuantState contains tensor-valued absmax / nested quantization state used by backward. Worth adding one regression that independently recycles B and the quant-state tensors. |
…for_backward backward dequantizes with ctx.state, so absmax (and for nested quantization offset, state2.absmax and state2.code) were still read through a plain ctx attribute and kept pointing at the original forward's buffers under a checkpoint recompute. Save them with the packed weight and rebuild the QuantState from ctx.saved_tensors in backward; ctx.state is kept for its non-tensor metadata only. Add a regression test that re-materialises the weight, the quant-state tensors, or both into a two-slot ring under checkpoint(use_reentrant=False) and compares the input gradient with private buffers.
|
@pjordanandrsn yes, it does. |
What
MatMul4Bit.forwardkeeps the packed 4-bit weight and itsQuantStateas plainctxattributes (ctx.tensors = (None, B),ctx.state = quant_state), andbackwarddequantizes from them. This PR saves the weight and the quant-statetensors
dequantize_4bitreads (absmax, plusoffset,state2.absmaxandstate2.codefor nested quantization) throughctx.save_for_backward, andrebuilds a
QuantStatefromctx.saved_tensorsinbackward.ctx.stateisstill kept, but only for its non-tensor metadata (shape, blocksize, quant_type,
dtype).
Fixes #2034.
Why
In the ordinary case the
ctx-attribute approach is harmless — the weight is astable
Parameter, so holding a reference to it is exactly as correct as savingit, and costs nothing.
It stops being correct for a caller that re-materialises the weight — weight
offloading, layer streaming, anything that writes a layer's bytes into a recycled
device buffer just before use.
torch.utils.checkpointdiscards and recomputessaved tensors, so under checkpointing such a caller expects the recompute to
hand
backwardfresh copies. A plainctxattribute is invisible to thatmechanism: the reference taken during the original forward survives into
backwardand, by then, points at a buffer that has been refilled with adifferent layer. The result is silently wrong gradients — the forward stays
bit-exact and the loss curve looks healthy. The quant-state tensors have the same
problem as the weight when a caller streams them too (pointed out by
@pjordanandrsn below).
bf16 through the same buffer-recycling harness is unaffected, because it goes
through
MmBackward0, which already usessave_for_backward. This change bringsMatMul4Bitin line with that.No behavioural change in the ordinary training path (stable
Parameterweight andquant state, nothing mutated between forward and backward).
Testing
New
test_matmul_4bit_checkpoint_recycled_buffersintests/test_autograd.py:four 4-bit layers, each re-materialised into a two-slot ring of buffers right
before use and run under
checkpoint(use_reentrant=False). It compares the inputgradient with the same stack using private buffers, recycling the weight, the
quant-state tensors, or both, with and without
compress_statistics.Max |input grad − reference| on an RTX 4090 (torch 2.5.1+cu121, nf4, reference
max ≈ 1.42):
(
compress_statistics=Truegives the same picture.)pytest tests/test_autograd.py -k matmul_4bitwith this branch'sautograd/_functions.pyon top of the 0.50.2 wheel (identicalMatMul4Bittomain): 1132 passed, 416 xfailed, cpu + cuda.