Skip to content

Conversation

Copy link

pytorch-bot bot commented Jan 31, 2024

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/118755

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit fe502d8 with merge base 278a0e1 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.


def get_future():
future = torch.futures.Future()
future: torch.futures.Future = torch.futures.Future()
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

For some reason mypy wanted this type annotation.

]
world_size = dist.get_world_size(process_group)
olist = [None for _ in range(world_size)]
dist.all_gather_object(olist, named_module_states, group=process_group)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

all_gather_object fills in the olist destructively. Another approach could be to initialize olist with some dummy object of the expected type.

Copy link
Contributor

Choose a reason for hiding this comment

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

would it be a good BE item (maybe for myself), to allow [] ?

olist = []
dist.all_gather_object(olist

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This seems reasonable to me. all_gather_object can destructively modify the olist as it already does today and append world_size many elements.

This sounds like a good BE task!

dist.reduce_scatter_tensor = orig_reduce_scatter


@no_type_check
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

These patching methods complain about assigning to a method like FSDPParamGroup.unshard = new_unshard. Since we have two of these assignments (to set to new and to restore to old) per patch context, I preferred to just ignore type checking, as it is not too valuable here.

awgu pushed a commit to awgu/pytorch that referenced this pull request Feb 8, 2024
ghstack-source-id: 5430730
Pull Request resolved: pytorch#118755
@awgu awgu added the ciflow/trunk Trigger trunk jobs on your pull request label Feb 13, 2024
@awgu
Copy link
Collaborator Author

awgu commented Feb 13, 2024

@pytorchbot merge

@pytorchmergebot
Copy link
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 Feb 13, 2024
The `groupby` logic to check if all all-gather inputs have the same dtype is not so readable. Let us use `all` instead.

Pull Request resolved: #119825
Approved by: https://github.com/Skylion007
ghstack dependencies: #119550, #118136, #118223, #118755
pytorchmergebot pushed a commit that referenced this pull request Feb 14, 2024
This PR adds a way to do gradient accumulation without collectives (i.e. reduce-scatter for FSDP and reduce-scatter/all-reduce for HSDP, though HSDP is not yet implemented). Since the `no_sync()` context manager has received some feedback, we simply define a method on the module to set whether the module requires gradient synchronization or not, where this method can recurse or not.
```
# Before with `no_sync()`:
with fsdp_model.no_sync() if not is_last_microbatch else contextlib.nullcontext():
  # Forward/backward

# After with a setter:
fsdp_model.set_requires_gradient_sync(not is_last_microbatch)
# Forward/backward
```
Having the method be able to recurse or not also gives some flexibility. For example, some large modules can still reduce-scatter, while some smaller modules can avoid it to save communication bandwidth:
```
fsdp_modules_to_reduce_scatter: Set[nn.Module] = ...
for module in fsdp_model.modules():
  if isinstance(module, FSDP) and module not in fsdp_modules_to_reduce_scatter:
    module.set_requires_gradient_sync(not is_last_microbatch)
# Forward/backward
```

(Separately, we may expose a helper for `return [module for model.modules() if isinstance(module, FSDP)]`.)

---

To show the spirit of this API choice, I also included `set_requires_all_reduce` that would give us the ability to only reduce-scatter but not all-reduce for HSDP (originally from the MiCS paper). If we want to flexibly support heterogeneous sharding where FSDP is applied to some modules and HSDP to others in the same model, then having a module-level method that has the option to not recurse makes sense to me.

Pull Request resolved: #118298
Approved by: https://github.com/wconstab, https://github.com/wanchaol
ghstack dependencies: #119550, #118136, #118223, #118755, #119825
@github-actions github-actions bot deleted the gh/awgu/504/head branch March 15, 2024 01:53
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 topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants