Repository navigation
Conversation
[ghstack-poisoned]
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/124319
Note: Links to docs will display an error until the docs builds have been completed. ✅ You can merge normally! (1 Unrelated Failure)As of commit da2ffc2 with merge base b412b75 ( FLAKY - The following job failed but was 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 chauhang d4l3k [ghstack-poisoned]
| with torch.profiler.record_function("FSDP::post_backward_reshard"): | ||
| if not self.reduce_grads: | ||
| self.reshard() | ||
| if self.reshard_after_backward: |
There was a problem hiding this comment.
This is the main expected use case: do not reduce gradients, and do not reshard parameters.
There was a problem hiding this comment.
is the use case mainly for pipeline parallel?
There was a problem hiding this comment.
This is useful for any kind of gradient accumulation, where pipeline parallelism is one example.
| unsharded_grads.append(fsdp_param.unsharded_grad_data) | ||
| fsdp_param.unsharded_param.grad = None | ||
| self.reshard() | ||
| if self.reshard_after_backward: |
There was a problem hiding this comment.
However, it is also easy for us to support the other case: do reduce gradients, but do not reshard parameters.
If people want to play around with different collective schedules during gradient accumulation, then this provides an extra lever.
This PR adds a `set_reshard_after_backward` method to allow disabling resharding after backward. `reshard_after_backward=False` can be used with `reshard_after_forward=False` to implement "ZeRO-1", where there is only all-gather on the first microbatch forward and reduce-scatter on the last microbatch backward.
```
for microbatch_idx, microbatch in dataloader:
is_last_microbatch = microbatch_idx == num_microbatches - 1
model.set_requires_gradient_sync(is_last_microbatch)
model.set_reshard_after_backward(is_last_microbatch)
model.set_is_last_backward(is_last_microbatch)
microbatch_fwd_bwd(model, microbatch, microbatch_idx)
```
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]
|
@pytorchbot merge |
|
The merge job was canceled. If you believe this is a mistake, then you can re trigger it through pytorch-bot. |
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 |
Merge failedReason: 1 mandatory check(s) failed. The first few are: Dig deeper by viewing the failures on hud |
pull / linux-jammy-py3.8-gcc11-pch / build failure looks unrelated |
|
@pytorchbot merge -i |
Merge startedYour change will be merged while ignoring the following 2 checks: pull / linux-focal-py3.11-clang10 / build, pull / linux-jammy-py3.8-gcc11-pch / build Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
|
The merge job was canceled. If you believe this is a mistake, then you can re trigger it through pytorch-bot. |
|
@pytorchbot merge -i |
Merge startedYour change will be merged while ignoring the following 1 checks: pull / linux-focal-py3.11-clang10 / test (default, 2, 3, linux.2xlarge) Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
#### 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: #120256 Approved by: https://github.com/weifengpy ghstack dependencies: #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
This PR adds a `set_reshard_after_backward` method to allow disabling resharding after backward. `reshard_after_backward=False` can be used with `reshard_after_forward=False` to implement "ZeRO-1", where there is only all-gather on the first microbatch forward and reduce-scatter on the last microbatch backward.
```
for microbatch_idx, microbatch in dataloader:
is_last_microbatch = microbatch_idx == num_microbatches - 1
model.set_requires_gradient_sync(is_last_microbatch)
model.set_reshard_after_backward(is_last_microbatch)
model.set_is_last_backward(is_last_microbatch)
microbatch_fwd_bwd(model, microbatch, microbatch_idx)
```
Pull Request resolved: pytorch#124319
Approved by: https://github.com/weifengpy
#### 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: #120256 Approved by: https://github.com/weifengpy ghstack dependencies: #124319
Since `DTensor` uses `swap_tensors` path automatically now, we can remove this check for the global flag. Pull Request resolved: pytorch#124513 Approved by: https://github.com/weifengpy ghstack dependencies: pytorch#124319, pytorch#120256
This PR adds a `set_reshard_after_backward` method to allow disabling resharding after backward. `reshard_after_backward=False` can be used with `reshard_after_forward=False` to implement "ZeRO-1", where there is only all-gather on the first microbatch forward and reduce-scatter on the last microbatch backward.
```
for microbatch_idx, microbatch in dataloader:
is_last_microbatch = microbatch_idx == num_microbatches - 1
model.set_requires_gradient_sync(is_last_microbatch)
model.set_reshard_after_backward(is_last_microbatch)
model.set_is_last_backward(is_last_microbatch)
microbatch_fwd_bwd(model, microbatch, microbatch_idx)
```
Pull Request resolved: pytorch#124319
Approved by: https://github.com/weifengpy
#### 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: pytorch#124513 Approved by: https://github.com/weifengpy ghstack dependencies: pytorch#124319, pytorch#120256
Stack from ghstack (oldest at bottom):
set_reshard_after_backward#124319This PR adds a
set_reshard_after_backwardmethod to allow disabling resharding after backward.reshard_after_backward=Falsecan be used withreshard_after_forward=Falseto implement "ZeRO-1", where there is only all-gather on the first microbatch forward and reduce-scatter on the last microbatch backward.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