Repository navigation
[FSDP2] Added CPU offloading - #120256
[FSDP2] Added CPU offloading#120256awgu wants to merge 19 commits into
Conversation
[ghstack-poisoned]
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/120256
Note: Links to docs will display an error until the docs builds have been completed. ✅ You can merge normally! (2 Unrelated Failures)As of commit 62d698e with merge base b412b75 ( FLAKY - The following jobs failed but were likely due to flakiness present on trunk:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
cc mrshenli pritamdamania87 zhaojuanmao satgera rohan-varma gqchen aazzolini osalpekar jiayisuse H-Huang kwen2501 penguinwu fegin XilunWu wanchaol fduwjj wz337 tianyu-l wconstab yf225 [ghstack-poisoned]
|
cc: @msaroufim With this PR, per-parameter FSDP (
One caveat I was thinking about is that FSDP assumes distributed is available and will try to initialize a process group if not passed one. For single-GPU setups, perhaps this assumption is a hindrance. As such, I am not sure whether it makes sense to couple single-GPU CPU offloading with FSDP's CPU offloading. (Though, having a fragmented story would also be a downside.) |
|
I am testing this PR on FSDP + llama-7B |
| expected_mem_mb += 2 * model_sharded_numel * 4 / 1e6 | ||
| if not use_cpu_offload: | ||
| # 2x sharded parameters/gradients | ||
| expected_mem_mb += 2 * model_sharded_numel * 4 / 1e6 |
There was a problem hiding this comment.
Should we have some episilon constant here for 1e6?
There was a problem hiding this comment.
Sorry, to clarify, what would the epsilon be for?
There was a problem hiding this comment.
Just some constant so it can be changed easily in the future if we need to for precision reasons (instead of a magic number).
There was a problem hiding this comment.
I think the 1e6 is for converting to MB rather than being an epsilon in the numerical stability sense.
|
I am thinking a bit on the API design. Should we expose a way for users to apply these kinds of policies (e.g. mixed precision, CPU offloading) at tensor level? If so, how? |
is there some chance to decouple cpu offloading as indepedent pre/post-forward/backward hooks? so offloading can work for non-distributed and distributed ? for forward, H2D hook -> all-gather hook |
To summarize, CPU offloading for FSDP looks like four parts, all at the tensor level (as opposed to module level):
The CUDA event part is only necessary because FSDP uses a separate reduce-scatter stream. I am not sure how useful it is to factor out these parts to be usable by single-GPU (non-FSDP) because I think the majority of the heavy lifting is done not by these additional four parts but rather by FSDP's module-level forward/backward hook logic to call the pre-all-gather H2D copy and post-reduce-scatter D2H copy at a good time. |
|
If we want to allow users to offload just some of the parameters in a parameter group, then we could follow the tensor parallel API's approach by requiring a A few issues:
|
#### Overview This PR adds CPU offloading via the `offload_policy: OffloadPolicy` argument. - We incur one H2D copy for each parameter before all-gather. - We incur one D2H copy for each gradient after reduce-scatter. - We run optimizer on CPU. #### Example (Mixed Precision and CPU Offloading) This example uses a small 125M numel model, which is not too representative. We can try to run with a larger model like Llama-7B. However, since the current optimizer step is already too slow, we may want to patch a faster CPU optimizer. Forward   Backward   Overall CPU (CPU optimizer step dominates)  cc mrshenli pritamdamania87 zhaojuanmao satgera rohan-varma gqchen aazzolini osalpekar jiayisuse H-Huang kwen2501 penguinwu fegin XilunWu wanchaol fduwjj wz337 tianyu-l wconstab yf225 [ghstack-poisoned]
Thank you for the educational comments (as usual), although I do worry that offloading is still most useful at 1 GPU. IIRC @wanchaol had been working on single GPU distributed that might be relevant for this work |
|
@awgu so last week I did try to see if torch.compile vectorizes and uses OMP pragmas when compiling ADAM on cpu and it indeed does https://github.com/msaroufim/tinyoptimizer/blob/master/compiled_output_code/graph1.py#L31-L37 I was checking to see if I could codegen the kernel with torch.compile and rip it out so we could also use the code in eager, I won't do that since the code is kinda hard to read but at least to unblock I'm wondering whether you can just torch.compile on cpu the |
#### Overview This PR adds CPU offloading via the `offload_policy: OffloadPolicy` argument. - We incur one H2D copy for each parameter before all-gather. - We incur one D2H copy for each gradient after reduce-scatter. - We run optimizer on CPU. #### Example (Mixed Precision and CPU Offloading) This example uses a small 125M numel model, which is not too representative. We can try to run with a larger model like Llama-7B. However, since the current optimizer step is already too slow, we may want to patch a faster CPU optimizer. Forward   Backward   Overall CPU (CPU optimizer step dominates)  cc mrshenli pritamdamania87 zhaojuanmao satgera rohan-varma gqchen aazzolini osalpekar jiayisuse H-Huang kwen2501 penguinwu fegin XilunWu wanchaol fduwjj wz337 tianyu-l wconstab yf225 [ghstack-poisoned]
#### Overview This PR adds CPU offloading via the `offload_policy: OffloadPolicy` argument. - We incur one H2D copy for each parameter before all-gather. - We incur one D2H copy for each gradient after reduce-scatter. - We run optimizer on CPU. #### Example (Mixed Precision and CPU Offloading) This example uses a small 125M numel model, which is not too representative. We can try to run with a larger model like Llama-7B. However, since the current optimizer step is already too slow, we may want to patch a faster CPU optimizer. Forward   Backward   Overall CPU (CPU optimizer step dominates)  cc mrshenli pritamdamania87 zhaojuanmao satgera rohan-varma gqchen aazzolini osalpekar jiayisuse H-Huang kwen2501 penguinwu fegin XilunWu wanchaol fduwjj wz337 tianyu-l wconstab yf225 [ghstack-poisoned]
#### Overview This PR adds CPU offloading via the `offload_policy: OffloadPolicy` argument. - We incur one H2D copy for each parameter before all-gather. - We incur one D2H copy for each gradient after reduce-scatter. - We run optimizer on CPU. #### Example (Mixed Precision and CPU Offloading) This example uses a small 125M numel model, which is not too representative. We can try to run with a larger model like Llama-7B. However, since the current optimizer step is already too slow, we may want to patch a faster CPU optimizer. Forward   Backward   Overall CPU (CPU optimizer step dominates)  cc mrshenli pritamdamania87 zhaojuanmao satgera rohan-varma gqchen aazzolini osalpekar jiayisuse H-Huang kwen2501 penguinwu fegin XilunWu wanchaol fduwjj wz337 tianyu-l wconstab yf225 [ghstack-poisoned]
#### Overview This PR adds CPU offloading via the `offload_policy: OffloadPolicy` argument. - We incur one H2D copy for each parameter before all-gather. - We incur one D2H copy for each gradient after reduce-scatter. - We run optimizer on CPU. #### Example (Mixed Precision and CPU Offloading) This example uses a small 125M numel model, which is not too representative. We can try to run with a larger model like Llama-7B. However, since the current optimizer step is already too slow, we may want to patch a faster CPU optimizer. Forward   Backward   Overall CPU (CPU optimizer step dominates)  cc mrshenli pritamdamania87 zhaojuanmao satgera rohan-varma gqchen aazzolini osalpekar jiayisuse H-Huang kwen2501 penguinwu fegin XilunWu wanchaol fduwjj wz337 tianyu-l wconstab yf225 [ghstack-poisoned]
#### Overview This PR adds CPU offloading via the `offload_policy: OffloadPolicy` argument. - We incur one H2D copy for each parameter before all-gather. - We incur one D2H copy for each gradient after reduce-scatter. - We run optimizer on CPU. #### Example (Mixed Precision and CPU Offloading) This example uses a small 125M numel model, which is not too representative. We can try to run with a larger model like Llama-7B. However, since the current optimizer step is already too slow, we may want to patch a faster CPU optimizer. Forward   Backward   Overall CPU (CPU optimizer step dominates)  cc mrshenli pritamdamania87 zhaojuanmao satgera rohan-varma gqchen aazzolini osalpekar jiayisuse H-Huang kwen2501 penguinwu fegin XilunWu wanchaol fduwjj wz337 tianyu-l wconstab yf225 [ghstack-poisoned]
weifengpy
left a comment
There was a problem hiding this comment.
thanks, CPUOffloadPolicy(pin_memory=True) (kwargs) helps
#### Overview This PR adds CPU offloading via the `offload_policy: OffloadPolicy` argument. - We incur one H2D copy for each parameter before all-gather. - We incur one D2H copy for each gradient after reduce-scatter. - We run optimizer on CPU. #### Example (Mixed Precision and CPU Offloading) This example uses a small 125M numel model, which is not too representative. We can try to run with a larger model like Llama-7B. However, since the current optimizer step is already too slow, we may want to patch a faster CPU optimizer. Forward   Backward   Overall CPU (CPU optimizer step dominates)  cc mrshenli pritamdamania87 zhaojuanmao satgera rohan-varma gqchen aazzolini osalpekar jiayisuse H-Huang kwen2501 penguinwu fegin XilunWu wanchaol fduwjj wz337 tianyu-l wconstab yf225 [ghstack-poisoned]
|
Follow-ups for performance:
|
|
@pytorchbot merge |
Merge startedYour change will be merged once all checks pass (ETA 0-4 Hours). Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
Since `DTensor` uses `swap_tensors` path automatically now, we can remove this check for the global flag. Pull Request resolved: #124513 Approved by: https://github.com/weifengpy ghstack dependencies: #124319, #120256
#### Overview This PR adds CPU offloading via the `offload_policy: OffloadPolicy` argument. - We incur one H2D copy for each parameter before all-gather. - We incur one D2H copy for each gradient after reduce-scatter. - We run optimizer on CPU. #### Example (Mixed Precision and CPU Offloading) This example uses a small 125M numel model, which is not too representative. We can try to run with a larger model like Llama-7B. However, since the current optimizer step is already too slow, we may want to patch a faster CPU optimizer. Forward   Backward   Overall CPU (CPU optimizer step dominates)  Pull Request resolved: pytorch#120256 Approved by: https://github.com/weifengpy ghstack dependencies: pytorch#124319
Since `DTensor` uses `swap_tensors` path automatically now, we can remove this check for the global flag. Pull Request resolved: #124513 Approved by: https://github.com/weifengpy ghstack dependencies: #124319, #120256
#### Overview This PR adds CPU offloading via the `offload_policy: OffloadPolicy` argument. - We incur one H2D copy for each parameter before all-gather. - We incur one D2H copy for each gradient after reduce-scatter. - We run optimizer on CPU. #### Example (Mixed Precision and CPU Offloading) This example uses a small 125M numel model, which is not too representative. We can try to run with a larger model like Llama-7B. However, since the current optimizer step is already too slow, we may want to patch a faster CPU optimizer. Forward   Backward   Overall CPU (CPU optimizer step dominates)  Pull Request resolved: pytorch#120256 Approved by: https://github.com/weifengpy ghstack dependencies: pytorch#124319
Since `DTensor` uses `swap_tensors` path automatically now, we can remove this check for the global flag. Pull Request resolved: #124513 Approved by: https://github.com/weifengpy ghstack dependencies: #124319, #120256
Stack from ghstack (oldest at bottom):
set_reshard_after_backward#124319Overview
This PR adds CPU offloading via the
offload_policy: OffloadPolicyargument.Example (Mixed Precision and CPU Offloading)
This example uses a small 125M numel model, which is not too representative. We can try to run with a larger model like Llama-7B. However, since the current optimizer step is already too slow, we may want to patch a faster CPU optimizer.
Forward


Backward


Overall CPU (CPU optimizer step dominates)

cc @mrshenli @pritamdamania87 @zhaojuanmao @satgera @rohan-varma @gqchen @aazzolini @osalpekar @jiayisuse @H-Huang @kwen2501 @penguinwu @fegin @XilunWu @wanchaol @fduwjj @wz337 @tianyu-l @wconstab @yf225 @chauhang @d4l3k