-
Notifications
You must be signed in to change notification settings - Fork 26.3k
Initial Guard Env, duplicate tensor guards #90370
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
Conversation
wip wip Initial guard env Working [ghstack-poisoned]
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/90370
Note: Links to docs will display an error until the docs builds have been completed. ❗ 1 Active SEVsThere are 1 currently active SEVs. If your PR is affected, please view them below: ❌ 2 FailuresAs of commit 937d912: The following jobs have failed:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
wip wip Initial guard env Working cc mlazos soumith yanboliang penguinwu anijain2305 EikanWang jgong5 Guobing-Chen chunyuan-w XiaobingSuper zhuhaozhe blzheng Xia-Weiwen wenzhe-nrv jiayisunx desertfire [ghstack-poisoned]
wip wip Initial guard env Working cc mlazos soumith yanboliang penguinwu anijain2305 EikanWang jgong5 Guobing-Chen chunyuan-w XiaobingSuper zhuhaozhe blzheng Xia-Weiwen wenzhe-nrv jiayisunx desertfire [ghstack-poisoned]
Adds the guard env defined here: https://docs.google.com/document/d/1VbiXkIhKy744Q7Lx2e8UUBvP_5RH_WU0FEc3VadVX4U/edit#heading=h.bk4qqlofhsq cc mlazos soumith yanboliang penguinwu anijain2305 EikanWang jgong5 Guobing-Chen chunyuan-w XiaobingSuper zhuhaozhe blzheng Xia-Weiwen wenzhe-nrv jiayisunx desertfire [ghstack-poisoned]
Adds the guard env defined here: https://docs.google.com/document/d/1VbiXkIhKy744Q7Lx2e8UUBvP_5RH_WU0FEc3VadVX4U/edit#heading=h.bk4qqlofhsq cc mlazos soumith yanboliang penguinwu anijain2305 EikanWang jgong5 Guobing-Chen chunyuan-w XiaobingSuper zhuhaozhe blzheng Xia-Weiwen wenzhe-nrv jiayisunx desertfire [ghstack-poisoned]
Adds the guard env defined here: https://docs.google.com/document/d/1VbiXkIhKy744Q7Lx2e8UUBvP_5RH_WU0FEc3VadVX4U/edit#heading=h.bk4qqlofhsq cc mlazos soumith yanboliang penguinwu anijain2305 EikanWang jgong5 Guobing-Chen chunyuan-w XiaobingSuper zhuhaozhe blzheng Xia-Weiwen wenzhe-nrv jiayisunx desertfire [ghstack-poisoned]
| ) -> Optional[GuardedCode]: | ||
| output: Optional[OutputGraph] = None | ||
|
|
||
| # from .utils import print_once; print_once(code.co_filename) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to reviewer note: There are no meaningful changes here, it is just tabbed over.
Adds the guard env defined here: https://docs.google.com/document/d/1VbiXkIhKy744Q7Lx2e8UUBvP_5RH_WU0FEc3VadVX4U/edit#heading=h.bk4qqlofhsq cc mlazos soumith yanboliang penguinwu anijain2305 EikanWang jgong5 Guobing-Chen chunyuan-w XiaobingSuper zhuhaozhe blzheng Xia-Weiwen wenzhe-nrv jiayisunx desertfire [ghstack-poisoned]
Adds the guard env defined here: https://docs.google.com/document/d/1VbiXkIhKy744Q7Lx2e8UUBvP_5RH_WU0FEc3VadVX4U/edit#heading=h.bk4qqlofhsq cc mlazos soumith yanboliang penguinwu anijain2305 EikanWang jgong5 Guobing-Chen chunyuan-w XiaobingSuper zhuhaozhe blzheng Xia-Weiwen wenzhe-nrv jiayisunx desertfire [ghstack-poisoned]
Adds the guard env defined here: https://docs.google.com/document/d/1VbiXkIhKy744Q7Lx2e8UUBvP_5RH_WU0FEc3VadVX4U/edit#heading=h.bk4qqlofhsq cc mlazos soumith yanboliang penguinwu anijain2305 EikanWang jgong5 Guobing-Chen chunyuan-w XiaobingSuper zhuhaozhe blzheng Xia-Weiwen wenzhe-nrv jiayisunx desertfire [ghstack-poisoned]
Adds the guard env defined here: https://docs.google.com/document/d/1VbiXkIhKy744Q7Lx2e8UUBvP_5RH_WU0FEc3VadVX4U/edit#heading=h.bk4qqlofhsq cc mlazos soumith yanboliang penguinwu anijain2305 EikanWang jgong5 Guobing-Chen chunyuan-w XiaobingSuper zhuhaozhe blzheng Xia-Weiwen wenzhe-nrv jiayisunx desertfire [ghstack-poisoned]
Adds the guard env defined here: https://docs.google.com/document/d/1VbiXkIhKy744Q7Lx2e8UUBvP_5RH_WU0FEc3VadVX4U/edit#heading=h.bk4qqlofhsq cc mlazos soumith yanboliang penguinwu anijain2305 EikanWang jgong5 Guobing-Chen chunyuan-w XiaobingSuper zhuhaozhe blzheng Xia-Weiwen wenzhe-nrv jiayisunx desertfire [ghstack-poisoned]
Adds the guard env defined here: https://docs.google.com/document/d/1VbiXkIhKy744Q7Lx2e8UUBvP_5RH_WU0FEc3VadVX4U/edit#heading=h.bk4qqlofhsq cc mlazos soumith yanboliang penguinwu anijain2305 EikanWang jgong5 Guobing-Chen chunyuan-w XiaobingSuper zhuhaozhe blzheng Xia-Weiwen wenzhe-nrv jiayisunx desertfire [ghstack-poisoned]
| @dataclasses.dataclass | ||
| class DuplicateInputs(GuardEnvExpr): | ||
| arg_a: str | ||
| arg_b: str |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think, directionally, my biggest discomfort with introducing a new GuardEnvExpr concept here that is distinct from Dynamo guards, is that Dynamo may very well WANT to generate guards like this (e.g., it is processing a conditional a is b) and it seems weird to have two possible ways to generate a guard (regular Guard, or GuardEnvExpr)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, it would be good to consolidate how we manage guards into a single place. That doesn't mean all guards need to be sympy guards (or dynamo guards)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is due to a layering issue - aot_autograd is below dynamo, so this needs to be in another place. We could violate layering and reuse the definitions in guards.py, or we could lower those down to a shared place, or we could do something like this. This felt the more reasonable, especially given the fact that we have another guard system in shape_env already that follows a similar pattern.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is also different from dynamo guards as all dynamo guards handle a single object at a time, the entire guards static_fn pattern is predicated on passing a Guard object which represent a single arg (see the self.get calls in guards.py). We could break that assumption and make multi-target guards, and rewrite that file, or we could carve out a special one-off guard that takes multiple tensors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fwiw, I understand the concern, and we could do something like:
- throw out DuplicateInputs
- Make the mapping tensor:source instead of tensor:name
- define a new Guard type that is multi-source / or a Source type that is multi-guard (we need to model the fact that this is a 2 object guard somewhere)
- Call Guards.DUPLICATE_INPUTS with either the 2 object guard, or with 2 guards for the 2 sources
Would you prefer that? Does that feel cleaner to you both? it does not to me.
torch/_dynamo/convert_frame.py
Outdated
| instructions[:] = output.output_instructions | ||
| code_options.update(output.code_options) | ||
| guard_env = GuardEnv() | ||
| with guarding(guard_env): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This with can go around the function call instead of the def ... part. The env doesn't matter for a function def. This will make this file's diff easier to read.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done! I added a comment about why it is where it is too.
| self._guards.clear() | ||
| self._tensor_to_names.clear() | ||
|
|
||
| CURRENT_GUARD_ENV = None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How are we passing the shape env to each part of the stack today? Is it a different global variable somewhere? I'd suggest some sort of tracing context class (and if needed just one global pointer to that) that holds pointers to these sorts of things and also allows us to guarantee they are set.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ShapeEnv is accessible from all SymInts, so all the points when we may want to register guards (e.g. an equality test on symint) can just directly add the guard to the SymInt. So ShapeEnv didn't really need a context class (although maybe it should.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We were talking about global vs passed around and Ed suggested this dynamic scoping to be a medium between the two. I don't have strong opinions on either way, but the plumbing story right now to backends is not amazing (as you recall from the kwargs/pass fake_mode around PRs we tried and failed prior).
There's a few ways we could take this:
- Leave as is, its a one off for now, until another thing with a similar lifecycle and pass-through-backends story needs to be developed
- Match the fake_mode pattern more closely, and ride guard_env down on the inputs (I dislike it, but its worth mentioning for consistency)
- Make a tracing context we pass around, move the shape_env and guard_env there
Adds the guard env defined here: https://docs.google.com/document/d/1VbiXkIhKy744Q7Lx2e8UUBvP_5RH_WU0FEc3VadVX4U/edit#heading=h.bk4qqlofhsq cc mlazos soumith yanboliang penguinwu anijain2305 EikanWang jgong5 Guobing-Chen chunyuan-w XiaobingSuper zhuhaozhe blzheng Xia-Weiwen wenzhe-nrv jiayisunx desertfire [ghstack-poisoned]
Stack from ghstack (oldest at bottom):
Adds the guard env defined here: https://docs.google.com/document/d/1VbiXkIhKy744Q7Lx2e8UUBvP_5RH_WU0FEc3VadVX4U/edit#heading=h.bk4qqlofhsq
cc @mlazos @soumith @yanboliang @penguinwu @anijain2305 @EikanWang @jgong5 @Guobing-Chen @chunyuan-w @XiaobingSuper @zhuhaozhe @blzheng @Xia-Weiwen @wenzhe-nrv @jiayisunx @desertfire