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

[nn-module] Use standard dict for _parameters, _modules and _buffers #129164

Closed
wants to merge 2 commits into from

Conversation

anijain2305
Copy link
Contributor

@anijain2305 anijain2305 commented Jun 20, 2024

Stack from ghstack (oldest at bottom):

TorchDynamo guard mechanism guards on the key order on the dictionaries if the user iterates over the dictionary. For standard dict, we can write a fast C++ implementation by using PyDict_Next. But with OrderedDict, we have to rely on keys Python API to get the key ordering. This makes guard evaluation slow.

With Dynamo inlining into inbuilt nn modules, I am seeing many guards over the OrderedDict on _modules, _parameters. From reading the code, I don't see any reason to not use standard dicts. I think OrderedDict was preferred over dict because of the ordering, but dicts are now ordered. With this PR, I am observing ~20% reduction in guard overhead of a HF model.

Functionality impact

  • The only difference between dict and OrdedeDict is move_to_end method for OrderedDict (link). But the changes here are internal to nn module, and we do not use move_to_end for _parameters, _modules and _buffers. We use move_to_end for hooks but this PR keeps the OrderedDict for hooks untouched (we should still followup with hooks but in a separate PR).

Perf impact

  • I dont anticipate any perf impact. dict is completely implemented in C. OrderedDict is Python wrapper over dict with only few method overridden (link).

Typing impact

  • I dont anticipate any. For all the user visible methods for nn.Module, we don't expose the underlying _modules etc. We have iterators like named_parameters which return an Iterator of Parameter. So, no typing changes required.

Copy link

pytorch-bot bot commented Jun 20, 2024

🔗 Helpful Links

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

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 ae8e301 with merge base f2f4dde (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.

anijain2305 added a commit that referenced this pull request Jun 20, 2024
ghstack-source-id: 410b138b7d88377f36a116fdb914b04445a19805
Pull Request resolved: #129164
Copy link
Collaborator

@albanD albanD left a comment

Choose a reason for hiding this comment

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

Why do we need this change?
Any perf or typing impact?

anijain2305 added a commit that referenced this pull request Jun 20, 2024
ghstack-source-id: 62efbe2258cefd704b8a7eb7b9c82b292994013e
Pull Request resolved: #129164
@anijain2305
Copy link
Contributor Author

@albanD I modified the description to answer the questions to best of my knowledge.

@anijain2305 anijain2305 requested a review from albanD June 21, 2024 04:13
@anijain2305
Copy link
Contributor Author

@albanD A gentle ping. Let me know if you have some experiments in mind to check perf impact. Or any other suggestions to move on this PR.

Copy link
Contributor

@mikaylagawarecki mikaylagawarecki left a comment

Choose a reason for hiding this comment

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

From discussion with @albanD, we are probably ok with this one

The only slight concern might be BC for an nn.Module that was torch.save-ed, but I think that should not be an issue

@anijain2305 anijain2305 added the topic: not user facing topic category label Jun 26, 2024
@anijain2305
Copy link
Contributor Author

@pytorchbot merge

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label Jun 26, 2024
@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

@huydhn
Copy link
Contributor

huydhn commented Jun 28, 2024

@pytorchbot revert -m 'Sorry for reverting your change but it seems to break some internal dper3 tests' -c ghfirst

From the imported diff D59091876, the error is AttributeError: 'dict' object has no attribute '_python_modules', so I guess they expect OrderedDict somehow?

@pytorchmergebot
Copy link
Collaborator

@pytorchbot successfully started a revert job. Check the current status here.
Questions? Feedback? Please reach out to the PyTorch DevX Team

pytorchmergebot added a commit that referenced this pull request Jun 28, 2024
…buffers (#129164)"

This reverts commit f2840bb.

Reverted #129164 on behalf of https://github.com/huydhn due to Sorry for reverting your change but it seems to break some internal dper3 tests ([comment](#129164 (comment)))
@pytorchmergebot
Copy link
Collaborator

@anijain2305 your PR has been successfully reverted.

@anijain2305
Copy link
Contributor Author

It was a silly impl in internal - fixed in https://www.internalfb.com/diff/D59166026

@anijain2305
Copy link
Contributor Author

@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

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 Reverted topic: not user facing topic category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants