Skip to content

[FSDP2] Added CPU offloading - #120256

Closed
awgu wants to merge 19 commits into
gh/awgu/517/basefrom
gh/awgu/517/head
Closed

awgu wants to merge 19 commits into
gh/awgu/517/basefrom
gh/awgu/517/head

Conversation

@awgu

@awgu awgu commented Feb 20, 2024 •

Copy link
Copy Markdown
Collaborator

Stack from ghstack (oldest at bottom):

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
Screenshot 2024-02-21 at 10 36 29 AM
Screenshot 2024-02-21 at 10 39 12 AM

Backward
Screenshot 2024-02-21 at 10 37 47 AM
Screenshot 2024-02-21 at 10 38 44 AM

Overall CPU (CPU optimizer step dominates)
Screenshot 2024-02-21 at 10 39 47 AM

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

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Feb 20, 2024 •

Copy link
Copy Markdown

🔗 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 (image):

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]
@awgu
awgu marked this pull request as ready for review February 21, 2024 15:45
@awgu

awgu commented Feb 21, 2024

Copy link
Copy Markdown
Collaborator Author

cc: @msaroufim

With this PR, per-parameter FSDP (fully_shard) has preliminary support for CPU offloading.

  • We do not have meta-device init yet. One approach is to replicate existing FSDP's approach to meta-device init, where the unsharded module is materialized on GPU and immediately sharded, requiring either reset_parameters() or a user-defined function, but I want to defer this since we can have a cleaner way once we land some DTensor infra. See [FSDP2] Eager-Mode Execution Tracker #120003.
  • For now, the model can be initialized on CPU. We need to test/flesh out the checkpoint load flow, but this is orthogonal to CPU offloading. We can get some help from @fegin @wz337 @LucasLLC there.
  • We shard the model with FSDP. For a transformer model, this effectively means:
    for module in model.modules():
      if isinstance(module, TransformerBlock):
        fully_shard(module, offload_policy=OffloadPolicy("cpu"))
    fully_shard(model, offload_policy=OffloadPolicy("cpu"))
    
  • Each TransformerBlock forms one parameter group, and the root module (model) forms a final group (of the embedding and output projection). Each group's parameters are communicated together via all-gather (and similarly its gradients for reduce-scatter). If CPU offloading, then the group's parameters are copied H2D at the same point, and its gradients are copied D2H at the same point. If you want smaller groups, you can consider applying fully_shard at a finer granularity, e.g. on the Attention and Feedforward within the TransfomerBlock.
  • During initialization, the current implementation will move the group's parameters to GPU, shard, and offload them back to CPU. This should not increase peak memory since those parameters would be moved to GPU together anyway for forward/backward computation.
  • FSDP will implicitly 'prefetch'. If CPU offloading, then the next group's parameters are copied H2D during the previous group's forward. In backward, the gradient D2H copy will be async as long as we are not accumulating gradients (aka microbatching). The PR description shows some toy traces to exemplify the overlap.

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.)

@weifengpy

Copy link
Copy Markdown
Contributor

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

@Skylion007 Skylion007 Feb 22, 2024 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have some episilon constant here for 1e6?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, to clarify, what would the epsilon be for?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some constant so it can be changed easily in the future if we need to for precision reasons (instead of a magic number).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the 1e6 is for converting to MB rather than being an epsilon in the numerical stability sense.

@awgu

awgu commented Feb 22, 2024

Copy link
Copy Markdown
Collaborator Author

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?

@weifengpy

Copy link
Copy Markdown
Contributor

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
for backward, reduce-scatter hook -> D2H hook

@awgu

awgu commented Feb 22, 2024 •

Copy link
Copy Markdown
Collaborator Author

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 for backward, reduce-scatter hook -> D2H hook

To summarize, CPU offloading for FSDP looks like four parts, all at the tensor level (as opposed to module level):

  • Post-sharded-parameter init: offload the sharded parameter's data (padded) to CPU pinned memory and initialize a CPU sharded gradient in pinned memory
  • Pre-all-gather: copy the sharded parameter from H2D
  • Post-reduce-scatter: copy the sharded gradient from D2H, optionally as non-blocking if not accumulating gradients, and record a CUDA event
  • Finalize backward (end of backward): block CPU on the recorded CUDA event

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.

@awgu

awgu commented Feb 22, 2024

Copy link
Copy Markdown
Collaborator Author

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 Dict[str, ...] that maps from fully-qualified name to policy.

offload_policy = {
  "feed_forward.w1": OffloadPolicy("cpu"),
  "feed_forward.w2": OffloadPolicy("cpu"),
  "feed_forward.w3": OffloadPolicy("cpu"), 
}

A few issues:

  • The FQNs need to be prefixed from the module passed to fully_shard(module), which may change with different "wrapping" policy.
  • This approach might make sense for CPU offloading because it is purely a tensor-level construct. However, for something like mixed precision, we confound module-level constructs like casting the forward inputs/outputs to a dtype with tensor-level constructs like the parameter/reduce dtypes. It is less clear how this Dict[str, ...] approach would work then. cc: @wanchaol

#### 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
![Screenshot 2024-02-21 at 10 36 29 AM](https://github.com/pytorch/pytorch/assets/31054793/00ed95db-3a55-49bb-ac98-9b9162feaacd)
![Screenshot 2024-02-21 at 10 39 12 AM](https://github.com/pytorch/pytorch/assets/31054793/10e29854-1907-4001-b3dc-aab6c3bf153c)

Backward
![Screenshot 2024-02-21 at 10 37 47 AM](https://github.com/pytorch/pytorch/assets/31054793/7039cb2e-eb78-4f53-b83f-67bae61ebddd)
![Screenshot 2024-02-21 at 10 38 44 AM](https://github.com/pytorch/pytorch/assets/31054793/e34615d6-6b6b-4995-aef1-9c7563034799)

Overall CPU (CPU optimizer step dominates)
![Screenshot 2024-02-21 at 10 39 47 AM](https://github.com/pytorch/pytorch/assets/31054793/7a2a929a-3a40-4b35-891b-016cf57e8079)


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]
@msaroufim

Copy link
Copy Markdown
Collaborator

. 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 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.

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

@msaroufim

msaroufim commented Mar 4, 2024 •

Copy link
Copy Markdown
Collaborator

@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 opt.step() to unblock for now

#### 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
![Screenshot 2024-02-21 at 10 36 29 AM](https://github.com/pytorch/pytorch/assets/31054793/00ed95db-3a55-49bb-ac98-9b9162feaacd)
![Screenshot 2024-02-21 at 10 39 12 AM](https://github.com/pytorch/pytorch/assets/31054793/10e29854-1907-4001-b3dc-aab6c3bf153c)

Backward
![Screenshot 2024-02-21 at 10 37 47 AM](https://github.com/pytorch/pytorch/assets/31054793/7039cb2e-eb78-4f53-b83f-67bae61ebddd)
![Screenshot 2024-02-21 at 10 38 44 AM](https://github.com/pytorch/pytorch/assets/31054793/e34615d6-6b6b-4995-aef1-9c7563034799)

Overall CPU (CPU optimizer step dominates)
![Screenshot 2024-02-21 at 10 39 47 AM](https://github.com/pytorch/pytorch/assets/31054793/7a2a929a-3a40-4b35-891b-016cf57e8079)


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]
awgu pushed a commit that referenced this pull request Mar 7, 2024
ghstack-source-id: 02805e2
Pull Request resolved: #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
![Screenshot 2024-02-21 at 10 36 29 AM](https://github.com/pytorch/pytorch/assets/31054793/00ed95db-3a55-49bb-ac98-9b9162feaacd)
![Screenshot 2024-02-21 at 10 39 12 AM](https://github.com/pytorch/pytorch/assets/31054793/10e29854-1907-4001-b3dc-aab6c3bf153c)

Backward
![Screenshot 2024-02-21 at 10 37 47 AM](https://github.com/pytorch/pytorch/assets/31054793/7039cb2e-eb78-4f53-b83f-67bae61ebddd)
![Screenshot 2024-02-21 at 10 38 44 AM](https://github.com/pytorch/pytorch/assets/31054793/e34615d6-6b6b-4995-aef1-9c7563034799)

Overall CPU (CPU optimizer step dominates)
![Screenshot 2024-02-21 at 10 39 47 AM](https://github.com/pytorch/pytorch/assets/31054793/7a2a929a-3a40-4b35-891b-016cf57e8079)


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]
awgu pushed a commit that referenced this pull request Mar 8, 2024
ghstack-source-id: 43a070d
Pull Request resolved: #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
![Screenshot 2024-02-21 at 10 36 29 AM](https://github.com/pytorch/pytorch/assets/31054793/00ed95db-3a55-49bb-ac98-9b9162feaacd)
![Screenshot 2024-02-21 at 10 39 12 AM](https://github.com/pytorch/pytorch/assets/31054793/10e29854-1907-4001-b3dc-aab6c3bf153c)

Backward
![Screenshot 2024-02-21 at 10 37 47 AM](https://github.com/pytorch/pytorch/assets/31054793/7039cb2e-eb78-4f53-b83f-67bae61ebddd)
![Screenshot 2024-02-21 at 10 38 44 AM](https://github.com/pytorch/pytorch/assets/31054793/e34615d6-6b6b-4995-aef1-9c7563034799)

Overall CPU (CPU optimizer step dominates)
![Screenshot 2024-02-21 at 10 39 47 AM](https://github.com/pytorch/pytorch/assets/31054793/7a2a929a-3a40-4b35-891b-016cf57e8079)


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
![Screenshot 2024-02-21 at 10 36 29 AM](https://github.com/pytorch/pytorch/assets/31054793/00ed95db-3a55-49bb-ac98-9b9162feaacd)
![Screenshot 2024-02-21 at 10 39 12 AM](https://github.com/pytorch/pytorch/assets/31054793/10e29854-1907-4001-b3dc-aab6c3bf153c)

Backward
![Screenshot 2024-02-21 at 10 37 47 AM](https://github.com/pytorch/pytorch/assets/31054793/7039cb2e-eb78-4f53-b83f-67bae61ebddd)
![Screenshot 2024-02-21 at 10 38 44 AM](https://github.com/pytorch/pytorch/assets/31054793/e34615d6-6b6b-4995-aef1-9c7563034799)

Overall CPU (CPU optimizer step dominates)
![Screenshot 2024-02-21 at 10 39 47 AM](https://github.com/pytorch/pytorch/assets/31054793/7a2a929a-3a40-4b35-891b-016cf57e8079)


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
![Screenshot 2024-02-21 at 10 36 29 AM](https://github.com/pytorch/pytorch/assets/31054793/00ed95db-3a55-49bb-ac98-9b9162feaacd)
![Screenshot 2024-02-21 at 10 39 12 AM](https://github.com/pytorch/pytorch/assets/31054793/10e29854-1907-4001-b3dc-aab6c3bf153c)

Backward
![Screenshot 2024-02-21 at 10 37 47 AM](https://github.com/pytorch/pytorch/assets/31054793/7039cb2e-eb78-4f53-b83f-67bae61ebddd)
![Screenshot 2024-02-21 at 10 38 44 AM](https://github.com/pytorch/pytorch/assets/31054793/e34615d6-6b6b-4995-aef1-9c7563034799)

Overall CPU (CPU optimizer step dominates)
![Screenshot 2024-02-21 at 10 39 47 AM](https://github.com/pytorch/pytorch/assets/31054793/7a2a929a-3a40-4b35-891b-016cf57e8079)


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]
awgu pushed a commit that referenced this pull request Apr 15, 2024
ghstack-source-id: 603691c
Pull Request resolved: #120256
@awgu
awgu requested a review from weifengpy April 16, 2024 15:02

@weifengpy weifengpy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
![Screenshot 2024-02-21 at 10 36 29 AM](https://github.com/pytorch/pytorch/assets/31054793/00ed95db-3a55-49bb-ac98-9b9162feaacd)
![Screenshot 2024-02-21 at 10 39 12 AM](https://github.com/pytorch/pytorch/assets/31054793/10e29854-1907-4001-b3dc-aab6c3bf153c)

Backward
![Screenshot 2024-02-21 at 10 37 47 AM](https://github.com/pytorch/pytorch/assets/31054793/7039cb2e-eb78-4f53-b83f-67bae61ebddd)
![Screenshot 2024-02-21 at 10 38 44 AM](https://github.com/pytorch/pytorch/assets/31054793/e34615d6-6b6b-4995-aef1-9c7563034799)

Overall CPU (CPU optimizer step dominates)
![Screenshot 2024-02-21 at 10 39 47 AM](https://github.com/pytorch/pytorch/assets/31054793/7a2a929a-3a40-4b35-891b-016cf57e8079)


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]
awgu pushed a commit that referenced this pull request Apr 19, 2024
ghstack-source-id: 12d386a
Pull Request resolved: #120256
@awgu awgu added the ciflow/trunk Trigger trunk jobs on your pull request label Apr 19, 2024
@awgu

awgu commented Apr 19, 2024

Copy link
Copy Markdown
Collaborator Author

Follow-ups for performance:

  • Land DTensor support for fused Adam(W) so that we can leverage the new CPU fused Adam(W) kernels

@awgu

awgu commented Apr 20, 2024

Copy link
Copy Markdown
Collaborator Author

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your 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

Advanced Debugging
Check the merge workflow status
here

pytorchmergebot pushed a commit that referenced this pull request Apr 20, 2024
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
sanketpurandare pushed a commit to sanketpurandare/pytorch that referenced this pull request Apr 22, 2024
#### 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
![Screenshot 2024-02-21 at 10 36 29 AM](https://github.com/pytorch/pytorch/assets/31054793/00ed95db-3a55-49bb-ac98-9b9162feaacd)
![Screenshot 2024-02-21 at 10 39 12 AM](https://github.com/pytorch/pytorch/assets/31054793/10e29854-1907-4001-b3dc-aab6c3bf153c)

Backward
![Screenshot 2024-02-21 at 10 37 47 AM](https://github.com/pytorch/pytorch/assets/31054793/7039cb2e-eb78-4f53-b83f-67bae61ebddd)
![Screenshot 2024-02-21 at 10 38 44 AM](https://github.com/pytorch/pytorch/assets/31054793/e34615d6-6b6b-4995-aef1-9c7563034799)

Overall CPU (CPU optimizer step dominates)
![Screenshot 2024-02-21 at 10 39 47 AM](https://github.com/pytorch/pytorch/assets/31054793/7a2a929a-3a40-4b35-891b-016cf57e8079)

Pull Request resolved: pytorch#120256
Approved by: https://github.com/weifengpy
ghstack dependencies: pytorch#124319
pytorch-bot Bot pushed a commit that referenced this pull request Apr 22, 2024
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
petrex pushed a commit to petrex/pytorch that referenced this pull request May 3, 2024
#### 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
![Screenshot 2024-02-21 at 10 36 29 AM](https://github.com/pytorch/pytorch/assets/31054793/00ed95db-3a55-49bb-ac98-9b9162feaacd)
![Screenshot 2024-02-21 at 10 39 12 AM](https://github.com/pytorch/pytorch/assets/31054793/10e29854-1907-4001-b3dc-aab6c3bf153c)

Backward
![Screenshot 2024-02-21 at 10 37 47 AM](https://github.com/pytorch/pytorch/assets/31054793/7039cb2e-eb78-4f53-b83f-67bae61ebddd)
![Screenshot 2024-02-21 at 10 38 44 AM](https://github.com/pytorch/pytorch/assets/31054793/e34615d6-6b6b-4995-aef1-9c7563034799)

Overall CPU (CPU optimizer step dominates)
![Screenshot 2024-02-21 at 10 39 47 AM](https://github.com/pytorch/pytorch/assets/31054793/7a2a929a-3a40-4b35-891b-016cf57e8079)

Pull Request resolved: pytorch#120256
Approved by: https://github.com/weifengpy
ghstack dependencies: pytorch#124319
pytorch-bot Bot pushed a commit that referenced this pull request May 3, 2024
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
@github-actions
github-actions Bot deleted the gh/awgu/517/head branch June 2, 2024 01:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk Trigger trunk jobs on your pull request Merged oncall: distributed Add this issue/PR to distributed oncall triage queue release notes: distributed (fsdp2) release notes category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants