Commit 966611d
committed
fix(grad-checkpoint): use nn.utils.checkpoint — mx.checkpoint silently dropped param grads
The decoder's grad_checkpoint used mx.checkpoint(layer), which only checkpoints
w.r.t. the call's array inputs and does NOT thread the module's trainable
parameters through the recompute. Under nn.value_and_grad this SILENTLY DROPPED
gradients for many parameters — verified the ENTIRE final MoE layer's expert +
shared_expert weights, plus conv_bias and rope_inv_freq, got |grad|==0 with
checkpoint vs |grad|>0 without (loss identical, so the bug was invisible to a
loss-only check). It also made the additive attention mask a differentiable
checkpoint input, tripping 'scaled_dot_product_attention does not support VJP
w.r.t. mask' on CUDA — the real reason grad-checkpointed Path-B training was
blocked on gb10.
nn.utils.checkpoint(module) checkpoints w.r.t. the module's trainable parameters
AND inputs (threads params via module.trainable_parameters()), giving
bitwise-correct gradients: on the real HybridTinyLM, grad_checkpoint ON now
matches OFF with dloss=0 and grad max_rel=0.0 (was max_rel=1.0 / dropped grads).
Adds test_grad_checkpoint_gradients_match_non_checkpoint (RULE #1 regression:
ON==OFF grads) and updates the decoder-checkpoint spy test to assert the
nn.utils.checkpoint contract. This is a CORRECTNESS fix independent of memory —
every prior --grad-checkpoint run trained with wrong gradients.1 parent 0031bcc commit 966611d
2 files changed
Lines changed: 108 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2255 | 2255 | | |
2256 | 2256 | | |
2257 | 2257 | | |
| 2258 | + | |
| 2259 | + | |
| 2260 | + | |
| 2261 | + | |
| 2262 | + | |
| 2263 | + | |
| 2264 | + | |
| 2265 | + | |
| 2266 | + | |
| 2267 | + | |
| 2268 | + | |
| 2269 | + | |
2258 | 2270 | | |
2259 | 2271 | | |
2260 | 2272 | | |
| 2273 | + | |
2261 | 2274 | | |
2262 | | - | |
| 2275 | + | |
2263 | 2276 | | |
2264 | 2277 | | |
2265 | 2278 | | |
2266 | | - | |
| 2279 | + | |
2267 | 2280 | | |
2268 | 2281 | | |
2269 | 2282 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
426 | 426 | | |
427 | 427 | | |
428 | 428 | | |
429 | | - | |
430 | | - | |
| 429 | + | |
431 | 430 | | |
432 | | - | |
433 | | - | |
| 431 | + | |
434 | 432 | | |
435 | | - | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
436 | 441 | | |
437 | | - | |
| 442 | + | |
438 | 443 | | |
439 | 444 | | |
440 | 445 | | |
| |||
480 | 485 | | |
481 | 486 | | |
482 | 487 | | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
483 | 570 | | |
484 | 571 | | |
485 | 572 | | |
| |||
0 commit comments