Skip to content

Conversation

@kit1980
Copy link
Contributor

@kit1980 kit1980 commented May 24, 2022

Generator comprehensions with any/all are less verbose and potentially help to save memory/CPU : https://eklitzke.org/generator-comprehensions-and-using-any-and-all-in-python

To make JIT work with this change, I added code to convert GeneratorExp to ListComp. So the whole PR is basically NoOp for JIT, but potentially memory and speed improvement for eager mode.

@facebook-github-bot
Copy link
Contributor

facebook-github-bot commented May 24, 2022

🔗 Helpful links

✅ 1 Base Failures

As of commit 0fc1580 (more details on the Dr. CI page):

Expand to see more

None of the CI failures appear to be your fault 💚



🚧 1 fixed upstream failure:

These were probably caused by upstream breakages that were already fixed.

Please rebase on the viable/strict branch (expand for instructions)

If your commit is older than viable/strict, run these commands:

git fetch https://github.com/pytorch/pytorch viable/strict
git rebase FETCH_HEAD

This comment was automatically generated by Dr. CI (expand for details).

Please report bugs/suggestions to the (internal) Dr. CI Users group.

Click here to manually regenerate this comment.

@kit1980 kit1980 added the better-engineering Relatively self-contained tasks for better engineering contributors label May 24, 2022
@kit1980 kit1980 requested a review from a team May 24, 2022 00:47
@kit1980 kit1980 marked this pull request as ready for review May 24, 2022 00:47
@kit1980 kit1980 requested a review from albanD as a code owner May 24, 2022 00:47
@kit1980 kit1980 removed the request for review from a team May 24, 2022 04:27
@kit1980 kit1980 closed this May 24, 2022
@kit1980 kit1980 deleted the sdym/generator-optim branch May 24, 2022 18:15
@kit1980 kit1980 restored the sdym/generator-optim branch May 24, 2022 21:54
@kit1980 kit1980 reopened this May 24, 2022
@kit1980 kit1980 marked this pull request as draft May 24, 2022 22:30
@kit1980 kit1980 force-pushed the sdym/generator-optim branch 2 times, most recently from 95b4b07 to 483b655 Compare May 24, 2022 23:34
@kit1980 kit1980 requested a review from malfet May 25, 2022 02:57
@kit1980 kit1980 marked this pull request as ready for review May 25, 2022 02:58
@facebook-github-bot
Copy link
Contributor

@kit1980 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

Copy link
Contributor

@malfet malfet left a comment

Choose a reason for hiding this comment

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

Looks good to me, but please explain why test_scriptable is getting removed

with parametrize.cached():
traced_model = torch.jit.trace_module(model, {'forward': x})

def test_scriptable(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, why are you deleting this test?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As I mentioned in the PR description, the test was bad and had a TODO to actually implement and just tested that UnsupportedNodeError is thrown, and with GeneratorExp support a different error would be thrown.

Copy link
Collaborator

Choose a reason for hiding this comment

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

This test was here on purpose to make sure that trying to script this will raise a nice error message and not crash in an obscure way. I think we want to keep it and make sure the new error message is good.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@albanD any suggestions how to best reconcile this? It was just an accident that GeneratorExp was not supported and the test produced somewhat related-looking UnsupportedNodeError.

Copy link
Collaborator

Choose a reason for hiding this comment

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

In that case, I guess we can add a check if it it scripting and if so, raise a nice error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In that case, I guess we can add a check if it it scripting and if so, raise a nice error?

Hmm, add a check where exactly?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure how torchscript fails here so hard to say.
If register_parametrization does fail, then in that function.
If it fails at runtime, I guess in

# Unpack the originals for the first parametrization
which is called at runtime. But script might fail before that?

Or within torchscript if that's possible?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@albanD I've updated the PR, please take another look.

@kit1980 kit1980 requested a review from lezcano as a code owner June 7, 2022 00:40
@kit1980 kit1980 force-pushed the sdym/generator-optim branch from 6d373da to f83a466 Compare June 7, 2022 00:43
@facebook-github-bot
Copy link
Contributor

@kit1980 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@kit1980 kit1980 force-pushed the sdym/generator-optim branch from f83a466 to c48be80 Compare June 7, 2022 00:47
@facebook-github-bot
Copy link
Contributor

@kit1980 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@kit1980 kit1980 force-pushed the sdym/generator-optim branch from c48be80 to 2ede2a6 Compare June 7, 2022 00:56
@facebook-github-bot
Copy link
Contributor

@kit1980 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@kit1980 kit1980 force-pushed the sdym/generator-optim branch from 2ede2a6 to 32cd2bf Compare June 7, 2022 02:37
@facebook-github-bot
Copy link
Contributor

@kit1980 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@kit1980 kit1980 force-pushed the sdym/generator-optim branch from 32cd2bf to 4dec7e2 Compare June 7, 2022 03:45
@facebook-github-bot
Copy link
Contributor

@kit1980 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@kit1980 kit1980 force-pushed the sdym/generator-optim branch from 4dec7e2 to efce3b9 Compare June 7, 2022 04:21
@kit1980 kit1980 force-pushed the sdym/generator-optim branch from fcbdb34 to bfd9ce5 Compare June 19, 2022 04:43
@facebook-github-bot
Copy link
Contributor

@kit1980 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@kit1980 kit1980 added ciflow/trunk Trigger trunk jobs on your pull request ciflow/periodic Trigger jobs ran periodically on master (periodic.yml) on the PR labels Jun 19, 2022
@kit1980 kit1980 force-pushed the sdym/generator-optim branch from bfd9ce5 to d8b6304 Compare June 23, 2022 01:17
@facebook-github-bot
Copy link
Contributor

@kit1980 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@kit1980 kit1980 marked this pull request as ready for review June 23, 2022 04:05
@kit1980
Copy link
Contributor Author

kit1980 commented Jun 23, 2022

@pytorchbot rebase

@pytorchmergebot
Copy link
Collaborator

@pytorchbot successfully started a rebase job. Check the current status here

@pytorchmergebot
Copy link
Collaborator

Successfully rebased sdym/generator-optim onto refs/remotes/origin/master, please pull locally before adding more changes (for example, via git checkout sdym/generator-optim && git pull --rebase)

@facebook-github-bot
Copy link
Contributor

@kit1980 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

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.

SGTM

@kit1980
Copy link
Contributor Author

kit1980 commented Jun 24, 2022

@pytorchbot merge

@pytorchmergebot
Copy link
Collaborator

@pytorchbot successfully started a merge job. Check the current status here

@github-actions
Copy link
Contributor

Hey @kit1980.
You've committed this PR, but it does not have both a 'release notes: ...' and 'topics: ...' label. Please add one of each to the PR. The 'release notes: ...' label should represent the part of PyTorch that this PR changes (fx, autograd, distributed, etc) and the 'topics: ...' label should represent the kind of PR it is (not user facing, new feature, bug fix, perf improvement, etc). The list of valid labels can be found here for the 'release notes: ...' and here for the 'topics: ...'.
For changes that are 'topic: not user facing' there is no need for a release notes label.

facebook-github-bot pushed a commit that referenced this pull request Jun 27, 2022
Summary:
Generator comprehensions with any/all are less verbose and potentially help to save memory/CPU : https://eklitzke.org/generator-comprehensions-and-using-any-and-all-in-python

To make JIT work with this change, I added code to convert GeneratorExp to ListComp. So the whole PR is basically NoOp for JIT, but potentially memory and speed improvement for eager mode.

Also I removed a test from test/jit/test_parametrization.py. The test was bad and had a TODO to actually implement and just tested that UnsupportedNodeError is thrown, and with GeneratorExp support a different error would be thrown.

Pull Request resolved: #78142
Approved by: https://github.com/malfet, https://github.com/albanD

Test Plan: contbuild & OSS CI, see https://hud.pytorch.org/commit/pytorch/pytorch/de7219e8a7f3b2f8e20162f3b5af338df2e110c1

Reviewed By: malfet, albanD

Differential Revision: D36653959

Pulled By: kit1980

fbshipit-source-id: 26b68aa46229f59e21019ec1fd1cfeff81657e58
@pytorch-bot pytorch-bot bot added the release notes: jit release notes category label Apr 22, 2023
pytorchmergebot pushed a commit that referenced this pull request Apr 25, 2023
Apparently #78142 made torch.JIT allow for simple generator expressions which allows us to enable rules that replace unnecessary list comprehensions with generators in any/all. This was originally part of #99280 but I split it off into this PR so that it can be easily reverted should anything break.

Pull Request resolved: #99890
Approved by: https://github.com/justinchuby, https://github.com/kit1980, https://github.com/malfet
@github-actions github-actions bot deleted the sdym/generator-optim branch February 17, 2024 01:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

better-engineering Relatively self-contained tasks for better engineering contributors ciflow/periodic Trigger jobs ran periodically on master (periodic.yml) on the PR ciflow/trunk Trigger trunk jobs on your pull request cla signed Merged release notes: jit release notes category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants