Commit c1e509b
Add gradient_accumulation_no_sync flag to AutoUnit (#1057)
Summary:
Pull Request resolved: #1057
### Context
TorchTNT's AutoUnit.train_step calls set_requires_gradient_sync(False) on FSDP2 modules during gradient accumulation micro-batches. This tells FSDP2 to skip the gradient reduce-scatter in backward, keeping full unsharded gradients in memory on each rank. For large models this is catastrophic:
- 26B model on 16 GPUs: sharded gradients = 3.25 GB/rank, unsharded = ~52 GB/rank. Observed memory jump from 38% → 86% GPU utilization — a 48% increase from gradient accumulation alone.
- Small models (2B-4B): only 5-8% increase, because the absolute gradient size is small relative to 80 GB GPU memory.
The FSDP2 API already supports the memory-efficient alternative: simply don't disable gradient sync. Gradients reduce-scatter every micro-batch and accumulate in .grad in sharded form. This is exactly what torchtitan does. The only tradeoff is N× more reduce-scatter communication (N = gradient_accumulation_steps).
Goal: Add a single flag to AutoUnit that controls whether no_sync is used during gradient accumulation, enabling users to trade communication for memory.
### Approach
Add gradient_accumulation_no_sync: bool = True to AutoUnit.__init__. When False, skip both:
- FSDP2: set_requires_gradient_sync(False) call
- DDP/FSDP1: module.no_sync() context manager
Gradients then reduce-scatter (FSDP2) or all-reduce (DDP) on every micro-batch and accumulate in sharded .grad. The math is identical —
reduce is associative over micro-batches.
No changes needed in HuggingfaceSFTUnit or MediaAutoUnit — the flag flows through **kwargs.
Reviewed By: galrotem
Differential Revision: D99706122
fbshipit-source-id: 321a130aaaacfa98af9551f9d4579273823e7c231 parent 861ca2c commit c1e509b
2 files changed
Lines changed: 30 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
781 | 781 | | |
782 | 782 | | |
783 | 783 | | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
784 | 805 | | |
785 | 806 | | |
786 | 807 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
474 | 474 | | |
475 | 475 | | |
476 | 476 | | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
477 | 482 | | |
478 | 483 | | |
479 | 484 | | |
| |||
515 | 520 | | |
516 | 521 | | |
517 | 522 | | |
| 523 | + | |
518 | 524 | | |
519 | 525 | | |
520 | 526 | | |
| |||
585 | 591 | | |
586 | 592 | | |
587 | 593 | | |
| 594 | + | |
588 | 595 | | |
589 | 596 | | |
590 | 597 | | |
| |||
697 | 704 | | |
698 | 705 | | |
699 | 706 | | |
| 707 | + | |
700 | 708 | | |
701 | 709 | | |
702 | 710 | | |
703 | 711 | | |
704 | | - | |
| 712 | + | |
705 | 713 | | |
706 | 714 | | |
707 | 715 | | |
| |||
0 commit comments