Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prepend argument to nn.Module hooks #87370

Closed
wants to merge 13 commits into from

Conversation

mrshenli
Copy link
Contributor

@mrshenli mrshenli commented Oct 20, 2022

@pytorch-bot
Copy link

pytorch-bot bot commented Oct 20, 2022

🔗 Helpful Links

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

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

✅ No Failures, 3 Pending

As of commit 510d0aa:
💚 Looks good so far! There are no failures yet. 💚

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

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Oct 20, 2022

CLA Signed

The committers listed above are authorized under a signed CLA.

mrshenli added a commit that referenced this pull request Oct 20, 2022
ghstack-source-id: e01b3bba5a6994c14f83d5d867637025a4b63a89
Pull Request resolved: #87370
mrshenli added a commit that referenced this pull request Oct 20, 2022
ghstack-source-id: 64c9492f5bb1fe14fcab903973681f862d8914ce
Pull Request resolved: #87370
@mrshenli mrshenli changed the title Add prepend to nn.Module hooks Add prepend argument to nn.Module hooks Oct 20, 2022
mrshenli added a commit that referenced this pull request Oct 20, 2022
ghstack-source-id: 6beebd38d8a924d3f4be00719170a100b9cfe700
Pull Request resolved: #87370
@albanD albanD added the module: bc-breaking Related to a BC-breaking change label Oct 20, 2022
@pytorch-bot pytorch-bot bot added the topic: bc breaking topic category label Oct 20, 2022
@albanD
Copy link
Collaborator

albanD commented Oct 20, 2022

What is the exact use case for which you want this?
This is not very reliable in general as the next preprend hook you add will make your not the first one anymore.

cc @soulitzer related to hook ordering questions we were having.

cc ezyang gchanan

[ghstack-poisoned]
mrshenli added a commit that referenced this pull request Oct 20, 2022
ghstack-source-id: 7fb74675c5964713a58e15847dc9104365e2f770
Pull Request resolved: #87370
@mrshenli
Copy link
Contributor Author

What is the exact use case for which you want this?

We are working on converting some distributed features to hook-based solutions, so that we won't mess up FQNs as module wrapper did. To maintain the similar behavior as today DDP/FSDP, we plan to split their forward into top and bottom halves and install those as pre and post forward hooks. And the pre hook needs to be called before any existing hooks on the local module.

This is not very reliable in general as the next preprend hook you add will make your not the first one anymore.

This should be OK for now, as we expect DDP logic is added after local model preparations.

cc ezyang gchanan

[ghstack-poisoned]
mrshenli added a commit that referenced this pull request Oct 20, 2022
ghstack-source-id: 3ccbf150f92874aed50ff18166aba2b9e99dddf6
Pull Request resolved: #87370
cc ezyang gchanan

[ghstack-poisoned]
mrshenli added a commit that referenced this pull request Oct 20, 2022
ghstack-source-id: 96ad4992d503b23fce8f33902a9778e33cc97f4e
Pull Request resolved: #87370
@albanD
Copy link
Collaborator

albanD commented Oct 20, 2022

This should be OK for now, as we expect DDP logic is added after local model preparations.

Sure, but I'm not sure how to convey that to the end user of this flag in the doc :p (btw docs need updating).
Like we don't have a strict guarantee on the order of the backward hooks right now (which we want to figure out with @soulitzer btw). So I'm not sure what we're going to say here...

@soulitzer
Copy link
Contributor

For hooks in general it feels like provide guarantees about ordering is something we'd want to do as long as it doesn't complicate the implementation, so the idea sounds OK.

For this case in particular though, communicating to the user about what "prepend" does can be tricky because hooks registered globally will still execute before those prepend, and I'm not sure we even document that anywhere currently?

@mrshenli
Copy link
Contributor Author

mrshenli commented Oct 21, 2022

Hey @albanD @soulitzer, thanks a lot for the comments!

For this case in particular though, communicating to the user about what "prepend" does can be tricky because hooks registered globally will still execute before those prepend,

Would I be correct that global hooks are mainly for debugging/profiling purposes. Or have we seen cases where program relies on global hooks to produce correct results?

and I'm not sure we even document that anywhere currently?

Any suggestions on which of the following might be better?

  1. Add an underscore (i.e., _prepend) to make it private.
  2. Add arg docstrings to make it official and fully explain the ordering of hooks

Edit: chatted with @albanD offline, since private args will still render in the doc, it's not really private. So let's avoid that.

cc ezyang gchanan

[ghstack-poisoned]
mrshenli added a commit that referenced this pull request Oct 21, 2022
ghstack-source-id: c2647f464f7fc189aa7baeea553f5875249a5767
Pull Request resolved: #87370
cc ezyang gchanan

[ghstack-poisoned]
mrshenli added a commit that referenced this pull request Oct 21, 2022
ghstack-source-id: a882c6765f387e19de0be34928d43d3443558d57
Pull Request resolved: #87370
@mrshenli mrshenli added ciflow/trunk Trigger trunk jobs on your pull request ciflow/periodic Trigger jobs ran periodically on master (periodic.yml) on the PR labels Oct 21, 2022
@mrshenli
Copy link
Contributor Author

Screen Shot 2022-10-21 at 7 23 13 PM

Screen Shot 2022-10-21 at 7 24 23 PM

Screen Shot 2022-10-21 at 7 24 47 PM

Screen Shot 2022-10-21 at 7 25 27 PM

cc ezyang gchanan

[ghstack-poisoned]
mrshenli added a commit that referenced this pull request Oct 23, 2022
ghstack-source-id: 1b2f9416254cbaa5075f255fadc35248cf1cc22d
Pull Request resolved: #87370
cc ezyang gchanan

[ghstack-poisoned]
mrshenli added a commit that referenced this pull request Oct 23, 2022
ghstack-source-id: 7f288d76ab80f6f0e8bf10063e2febd0fc9c79bd
Pull Request resolved: #87370
@soulitzer
Copy link
Contributor

Thanks for the updated documentation, looks great.

Add arg docstrings to make it official and fully explain the ordering of hooks

To explain the ordering of hooks fully, we may also want to think about the interaction of full_backward_pre_hooks with hooks registered to module outputs (but that might be out of the scope of this PR)

  • FWIW the interaction seems to be: pre_hooks registered to the grad_fn of module outputs execute BEFORE prepended full_backward_pre_hooks, and post_hooks registered will execute AFTER. Not sure if it is problematic/relevant to your use case.

We might want to have some documentation regarding module hooks in general now, but that can be done in a follow up.

cc ezyang gchanan

[ghstack-poisoned]
@github-actions
Copy link

This PR needs a label

If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

For more information, see https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

cc ezyang gchanan

[ghstack-poisoned]
@mrshenli mrshenli added the release notes: nn release notes category label Oct 25, 2022
mrshenli added a commit to mrshenli/pytorch that referenced this pull request Oct 25, 2022
ghstack-source-id: 2c2800e1fa2ce7cec45590c2c63318ff167f6009
Pull Request resolved: pytorch#87370
@mrshenli
Copy link
Contributor Author

@pytorchbot merge -g

@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks on your PR pass since you used the green (-g) flag (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

sgrigory pushed a commit to sgrigory/pytorch that referenced this pull request Oct 28, 2022
kulinseth pushed a commit to kulinseth/pytorch that referenced this pull request Nov 5, 2022
kulinseth pushed a commit to kulinseth/pytorch that referenced this pull request Dec 10, 2022
@facebook-github-bot facebook-github-bot deleted the gh/mrshenli/335/head branch June 8, 2023 18:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ciflow/periodic Trigger jobs ran periodically on master (periodic.yml) on the PR ciflow/trunk Trigger trunk jobs on your pull request Merged module: bc-breaking Related to a BC-breaking change release notes: nn release notes category topic: bc breaking topic category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants