Commit 78485d5
Reduce GPU OOM in layer gradient computation by offloading tensors to CPU
Summary:
LayerGradientXActivation is having a number of jobs with OOM erros. These errors are due to the way we drain GPU memory during the forward and backward passes to obtain layer evaluations, copies of evaluations, and layer gradients.
The issue with the current way of getting evalutaions and gradients is the following:
* After getting the evaluations (activations) from the forward pass, we gather all the activation tensors across multiple devices into [one device](https://www.internalfb.com/code/fbsource/[0579e5aab76b1f89fc82d27913cbb3a6e0160b5f]/fbcode/pytorch/captum/captum/_utils/common.py?lines=785) meaning that one device would hold its own layer activations + copy of all the activations across all devices. This is the peak of the memory utilization. This is in addition to storing the model graph and the original layer activations in memory.
* After the backward pass on the `saved_layer` (still on GPU), we are also doing a similar operation on the gradients - collecting gradients across all devices into the first device. At this point though, we don't have as much memory utilization since backward pass is already completed. So it is safer to collect all gradients. We can then offload to cpu afterwards.
What we want to do now is to offload the layer activations to cpu during these peak gpu utilization. Before we run the expensive torch.cat, we offload all these tensors to cpu first. That way, when we run torch.cat, this is actually done on cpu freeing up gpu memory.
Additionally when we get to the gradients, after gathering all tensors together, we also offload these to cpu.
With this simple change, we would significantly improve gpu utilization. We add a flag that will include an efficient path to the implementation.
Adds a `memory_efficient` mode to `LayerGradientXActivation` and an `offload_to_cpu` parameter to `compute_layer_gradients_and_eval` to reduce peak GPU memory usage during multi-layer attribution.
Differential Revision: D949153671 parent f48fa42 commit 78485d5
File tree
2 files changed
+58
-24
lines changed- captum
- _utils
- attr/_core/layer
2 files changed
+58
-24
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
555 | 555 | | |
556 | 556 | | |
557 | 557 | | |
| 558 | + | |
558 | 559 | | |
559 | 560 | | |
560 | 561 | | |
| |||
574 | 575 | | |
575 | 576 | | |
576 | 577 | | |
| 578 | + | |
577 | 579 | | |
578 | 580 | | |
579 | 581 | | |
| |||
593 | 595 | | |
594 | 596 | | |
595 | 597 | | |
| 598 | + | |
596 | 599 | | |
597 | 600 | | |
598 | 601 | | |
| |||
612 | 615 | | |
613 | 616 | | |
614 | 617 | | |
| 618 | + | |
615 | 619 | | |
616 | 620 | | |
617 | 621 | | |
| |||
669 | 673 | | |
670 | 674 | | |
671 | 675 | | |
| 676 | + | |
672 | 677 | | |
673 | 678 | | |
674 | 679 | | |
| |||
693 | 698 | | |
694 | 699 | | |
695 | 700 | | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
696 | 724 | | |
697 | 725 | | |
698 | | - | |
699 | | - | |
700 | | - | |
701 | | - | |
702 | | - | |
703 | | - | |
704 | | - | |
705 | | - | |
| 726 | + | |
706 | 727 | | |
707 | 728 | | |
708 | 729 | | |
709 | 730 | | |
710 | 731 | | |
711 | | - | |
712 | | - | |
713 | | - | |
714 | | - | |
715 | | - | |
| 732 | + | |
716 | 733 | | |
717 | 734 | | |
718 | 735 | | |
719 | 736 | | |
720 | 737 | | |
721 | | - | |
722 | | - | |
723 | | - | |
724 | | - | |
| 738 | + | |
725 | 739 | | |
726 | 740 | | |
727 | 741 | | |
| |||
750 | 764 | | |
751 | 765 | | |
752 | 766 | | |
753 | | - | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
754 | 774 | | |
755 | 775 | | |
756 | 776 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| 31 | + | |
31 | 32 | | |
32 | 33 | | |
33 | 34 | | |
| |||
62 | 63 | | |
63 | 64 | | |
64 | 65 | | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
65 | 73 | | |
66 | 74 | | |
67 | 75 | | |
68 | 76 | | |
| 77 | + | |
69 | 78 | | |
70 | 79 | | |
71 | 80 | | |
| |||
181 | 190 | | |
182 | 191 | | |
183 | 192 | | |
| 193 | + | |
184 | 194 | | |
185 | 195 | | |
186 | 196 | | |
| |||
191 | 201 | | |
192 | 202 | | |
193 | 203 | | |
194 | | - | |
195 | | - | |
196 | | - | |
197 | | - | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
198 | 213 | | |
199 | | - | |
200 | | - | |
| 214 | + | |
201 | 215 | | |
202 | 216 | | |
203 | 217 | | |
| |||
0 commit comments