Commit 657c4bc
mem: re-dequantize frozen expert weights in backward instead of saving them
Route every expert projection through _FrozenLinearRecomputeBackward, a
small autograd Function whose forward computes dequantize + F.linear
directly and whose backward re-dequantizes the frozen weight to form
grad_output @ W:
- Bit-exact by construction, in and out of grad mode, on every device:
recomputation changes what is saved, never what is computed. (An earlier
iteration routed through bnb.matmul_4bit instead; it was dropped because
the fused gemm_4bit kernel it dispatches to for small token batches
differs from dequantize+linear by accumulation order.)
- The dequantized [out, in] expert weight never enters autograd's
saved-tensor storage, so training activation memory stays independent of
the number of experts held between forward and backward — no custom
kernel needed.
- Cost: one extra dequantize per projection in backward; subsumed by full
gradient checkpointing when that is enabled.
Tests: test_experts4bit_forward_is_bit_exact_dequantize_linear pins forward
== plain dequantize+linear at rtol=0/atol=0 (grad and no_grad);
test_experts4bit_backward_saves_no_dequantized_weight uses
saved_tensors_hooks to assert nothing weight-shaped (either orientation) is
saved while a plain dequantize+linear control does save it, and that
gradients match the control exactly.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>1 parent 68cc784 commit 657c4bc
2 files changed
Lines changed: 134 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
16 | 45 | | |
17 | 46 | | |
18 | 47 | | |
| |||
33 | 62 | | |
34 | 63 | | |
35 | 64 | | |
36 | | - | |
37 | | - | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
38 | 69 | | |
39 | 70 | | |
40 | 71 | | |
| |||
217 | 248 | | |
218 | 249 | | |
219 | 250 | | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
220 | 261 | | |
221 | 262 | | |
222 | 263 | | |
| |||
238 | 279 | | |
239 | 280 | | |
240 | 281 | | |
241 | | - | |
242 | | - | |
| 282 | + | |
| 283 | + | |
243 | 284 | | |
244 | | - | |
245 | 285 | | |
246 | 286 | | |
247 | 287 | | |
248 | 288 | | |
249 | 289 | | |
250 | 290 | | |
251 | | - | |
252 | | - | |
| 291 | + | |
| 292 | + | |
253 | 293 | | |
254 | | - | |
255 | 294 | | |
256 | 295 | | |
257 | 296 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
347 | 347 | | |
348 | 348 | | |
349 | 349 | | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
0 commit comments