Skip to content

Filter non input symexprs from codecache guards - #128052

Closed
oulgen wants to merge 1 commit into
mainfrom
export-D58200476
Closed

oulgen wants to merge 1 commit into
mainfrom
export-D58200476

Conversation

@oulgen

@oulgen oulgen commented Jun 5, 2024 •

Copy link
Copy Markdown
Contributor

Summary: Dynamo lifts all symexprs that appear in the inputs to top level which means that we do not need to look at guards that contain symexprs that do not appear in the inputs. Prune them.

Test Plan: added two new tests

Differential Revision: D58200476

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @peterbell10 @ipiszy @yf225 @chenyang78 @kadeng @muchulee8 @ColinPeppler @amjames @desertfire @chauhang

@pytorch-bot

pytorch-bot Bot commented Jun 5, 2024 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ You can merge normally! (1 Unrelated Failure)

As of commit 605eee3 with merge base e8670f6 (image):

UNSTABLE - The following job failed but was likely due to flakiness present on trunk and has been marked as unstable:

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

@facebook-github-bot

Copy link
Copy Markdown
Contributor

This pull request was exported from Phabricator. Differential Revision: D58200476

@oulgen
oulgen requested review from bdhirsh, ezyang and masnesral June 5, 2024 18:38
@oulgen oulgen added the ciflow/trunk Trigger trunk jobs on your pull request label Jun 5, 2024
oulgen added a commit that referenced this pull request Jun 5, 2024
Summary:

Dynamo lifts all symexprs that appear in the inputs to top level which means that we do not need to look at guards that contain symexprs that do not appear in the inputs. Prune them.

Test Plan: added two new tests

Differential Revision: D58200476
@oulgen
oulgen force-pushed the export-D58200476 branch from c33ba1b to 4d1b3a8 Compare June 5, 2024 18:39
@facebook-github-bot

Copy link
Copy Markdown
Contributor

This pull request was exported from Phabricator. Differential Revision: D58200476

@@ -904,6 +905,7 @@ def _save_graph(
shape_env = FxGraphCache._get_shape_env()
assert shape_env is not None
symints = FxGraphCache._filter_backed_symints(example_inputs)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

So, what's the plan for unbacked symints allocated in forwards that end up as inputs to the backwards graph?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hmm.. good point. Do you have any thought about this? Do we need to lift them in dynamo?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

On #126251 @masnesral claims that unbacked symints cant affect guards

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Curiosity question: can runtime asserts be associated with the backward graph today? Or do they only show up in forward (if they show up in backward, do include them directly inside the backward graph as fx nodes?)

for g in self.guards:
if all(str(s) in symints for s in g.expr.free_symbols):
guards.append(g)
self.guards = guards

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Hmmm it's not clear to me why this is correct. Say we have this code:

@torch.compile
def f(x, val):
    if val > 5:
        return x.sin()
    else:
        return x.cos()

x = torch.randn(2)
out1 = f(x, 6)
out2 = f(x, 4)

We need to dynamo to fail a guard for s0 > 5 on the second invocation of f. But... if I understand correctly, won't this change in-place mutate the ShapeEnv and delete that guard (while inductor is compiling), before dynamo has a chance to grab all relevant guards from the ShapeEnv and codegen them?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@bdhirsh and i looked at this example with dynamic=True and it appears to not fit the cache at all and ends up in a fresh compile

Comment thread torch/_inductor/codecache.py Outdated
assert shape_env is not None

symints = FxGraphCache._filter_backed_symints(example_inputs)
shape_env.drop_non_input_symint_guards(symints)

@masnesral masnesral Jun 5, 2024 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@ezyang can you verify its safe to manipulate the existing shape env? My high-level understanding from @bdhirsh is that dynamo will still evaluate these guards after inductor is finished doing its thing. If it is safe to remove this category of guards, why don't we remove them unconditionally before we start inducting rather than putting the manipulation in the cache path?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No, you definitely cannot start dropping guards from the shape env lol

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed this on a previous version of the PR

facebook-github-bot pushed a commit that referenced this pull request Jun 5, 2024
Summary:

Dynamo lifts all symexprs that appear in the inputs to top level which means that we do not need to look at guards that contain symexprs that do not appear in the inputs. Prune them.

Test Plan: added two new tests

Differential Revision: D58200476
@facebook-github-bot

Copy link
Copy Markdown
Contributor

This pull request was exported from Phabricator. Differential Revision: D58200476

)

self.guards: List[ShapeGuard] = []
self.input_guards: List[ShapeGuard] = []

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

i ended up separating these because symbolic shape recording pass validation was failing otherwise

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This seems like a dorky way to do this. One superficial complaint is that ShapeEnv is not Inductor specific, and yet we have named this thing "input guards", what the heck is an input guard?

For me, an intuitive code organization is that I want some way to call produce_guards with an explicit guards list, rather than using the guards that is accumulated directly on the ShapeEnv. That seems like a very modest change and doesn't require adding an extra field on ShapeEnv.

You need to make sure to not run translation validation on this produce_guards call either, as it will fail (and for good reason). TV should only run when you don't explicitly pass in guards to produce.

@facebook-github-bot

Copy link
Copy Markdown
Contributor

This pull request was exported from Phabricator. Differential Revision: D58200476

@oulgen
oulgen force-pushed the export-D58200476 branch from 19d9ab6 to 39ca6f7 Compare June 6, 2024 00:50
oulgen added a commit that referenced this pull request Jun 6, 2024
Summary:

Dynamo lifts all symexprs that appear in the inputs to top level which means that we do not need to look at guards that contain symexprs that do not appear in the inputs. Prune them.

Test Plan: added two new tests

Differential Revision: D58200476
@facebook-github-bot

Copy link
Copy Markdown
Contributor

This pull request was exported from Phabricator. Differential Revision: D58200476

@masnesral masnesral left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks correct based on discussions, but I still don't understand everything about guards so I'll defer to @ezyang about the overall soundness.

One thing that makes me nervous is that we're using a different set of guards for the cache than elsewhere. So if we've made a mistake and these guards are too lax, it's going to be very confusing and hard to debug. It would seem better, for example, if we could distinguish those guards added by inductor -- then use them directly in the cache rather than filtering from this single list based on criteria that I'm not sure are codified. But I presume that distinguishing the inductor-added guards would be a large refactor?

@facebook-github-bot

Copy link
Copy Markdown
Contributor

This pull request was exported from Phabricator. Differential Revision: D58200476

Summary:

Dynamo lifts all symexprs that appear in the inputs to top level which means that we do not need to look at guards that contain symexprs that do not appear in the inputs. Prune them.

Test Plan: added two new tests

Reviewed By: masnesral

Differential Revision: D58200476
@oulgen
oulgen force-pushed the export-D58200476 branch from 26d82c8 to 605eee3 Compare June 6, 2024 20:08
@facebook-github-bot

Copy link
Copy Markdown
Contributor

This pull request was exported from Phabricator. Differential Revision: D58200476

@facebook-github-bot

Copy link
Copy Markdown
Contributor

@pytorchbot merge -f 'Landed internally'

(Initiating merge automatically since Phabricator Diff has merged, using force because this PR might not pass merge_rules.json but landed internally)

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged immediately since you used the force (-f) flag, bypassing any CI checks (ETA: 1-5 minutes). Please use -f as last resort and instead consider -i/--ignore-current to continue the merge ignoring current failures. This will allow currently pending tests to finish and report signal before the merge.

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

masnesral added a commit that referenced this pull request Jun 7, 2024
Summary: We've already enabled for timm and huggingface, but we had failures saving cache entries for moco. It looks like #128052 has fixed that issue, so we can enable for torchbench.

[ghstack-poisoned]
masnesral added a commit that referenced this pull request Jun 7, 2024
Summary: We've already enabled for timm and huggingface, but we had failures saving cache entries for moco. It looks like #128052 has fixed that issue, so we can enable for torchbench.

ghstack-source-id: 1417089
Pull Request resolved: #128239
pytorchmergebot pushed a commit that referenced this pull request Jun 11, 2024
Summary: We've already enabled for timm and huggingface, but we had failures saving cache entries for moco. It looks like #128052 has fixed that issue, so we can enable for torchbench.

Pull Request resolved: #128239
Approved by: https://github.com/oulgen
facebook-github-bot pushed a commit to pytorch/benchmark that referenced this pull request Jun 11, 2024
Summary:
We've already enabled for timm and huggingface, but we had failures saving cache entries for moco. It looks like pytorch/pytorch#128052 has fixed that issue, so we can enable for torchbench.

X-link: pytorch/pytorch#128239
Approved by: https://github.com/oulgen

Reviewed By: clee2000

Differential Revision: D58394057

Pulled By: masnesral

fbshipit-source-id: 58a226babdcd04a3bb14118e7a555558c56bc9ef
masnesral added a commit that referenced this pull request Jun 12, 2024
Summary: We've already enabled for timm and huggingface, but we had failures saving cache entries for moco. It looks like #128052 has fixed that issue, so we can enable for torchbench.

ghstack-source-id: 4c4e9c9
Pull Request resolved: #128239
masnesral added a commit that referenced this pull request Jun 12, 2024
…ench"

Summary: We've already enabled for timm and huggingface, but we had failures saving cache entries for moco. It looks like #128052 has fixed that issue, so we can enable for torchbench.

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx chenyang78 kadeng chauhang

[ghstack-poisoned]
masnesral added a commit that referenced this pull request Jun 12, 2024
Summary: We've already enabled for timm and huggingface, but we had failures saving cache entries for moco. It looks like #128052 has fixed that issue, so we can enable for torchbench.

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx chenyang78 kadeng chauhang

[ghstack-poisoned]
pytorchmergebot pushed a commit that referenced this pull request Jun 12, 2024
Summary: We've already enabled for timm and huggingface, but we had failures saving cache entries for moco. It looks like #128052 has fixed that issue, so we can enable for torchbench.

Pull Request resolved: #128239
Approved by: https://github.com/oulgen
facebook-github-bot pushed a commit to pytorch/benchmark that referenced this pull request Jun 13, 2024
Summary:
We've already enabled for timm and huggingface, but we had failures saving cache entries for moco. It looks like pytorch/pytorch#128052 has fixed that issue, so we can enable for torchbench.

X-link: pytorch/pytorch#128239
Approved by: https://github.com/oulgen

Reviewed By: clee2000

Differential Revision: D58501160

Pulled By: masnesral

fbshipit-source-id: b7ceeecbf632671059a07e17851d4301c79945ec
TharinduRusira pushed a commit to TharinduRusira/pytorch that referenced this pull request Jun 14, 2024
Summary: Dynamo lifts all symexprs that appear in the inputs to top level which means that we do not need to look at guards that contain symexprs that do not appear in the inputs. Prune them.

Test Plan: added two new tests

Differential Revision: D58200476

Pull Request resolved: pytorch#128052
Approved by: https://github.com/ezyang, https://github.com/masnesral
TharinduRusira pushed a commit to TharinduRusira/pytorch that referenced this pull request Jun 14, 2024
Summary: We've already enabled for timm and huggingface, but we had failures saving cache entries for moco. It looks like pytorch#128052 has fixed that issue, so we can enable for torchbench.

Pull Request resolved: pytorch#128239
Approved by: https://github.com/oulgen
TharinduRusira pushed a commit to TharinduRusira/pytorch that referenced this pull request Jun 14, 2024
Summary: We've already enabled for timm and huggingface, but we had failures saving cache entries for moco. It looks like pytorch#128052 has fixed that issue, so we can enable for torchbench.

Pull Request resolved: pytorch#128239
Approved by: https://github.com/oulgen
ignaciobartol pushed a commit to ignaciobartol/pytorch that referenced this pull request Jun 14, 2024
Summary: We've already enabled for timm and huggingface, but we had failures saving cache entries for moco. It looks like pytorch#128052 has fixed that issue, so we can enable for torchbench.

Pull Request resolved: pytorch#128239
Approved by: https://github.com/oulgen
@github-actions
github-actions Bot deleted the export-D58200476 branch July 8, 2024 01:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/inductor ciflow/trunk Trigger trunk jobs on your pull request fb-exported Merged module: inductor release notes: fx release notes category suppress-bc-linter Suppresses the failures of API backward-compatibility linter (Lint/bc_linter)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants