Skip to content

[Trace Python Dispatcher] Support FuncTorchInterpreter #144444

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

Closed
wants to merge 9 commits into from

Conversation

yanboliang
Copy link
Contributor

@yanboliang yanboliang commented Jan 9, 2025

[ghstack-poisoned]
Copy link

pytorch-bot bot commented Jan 9, 2025

🔗 Helpful Links

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

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

❌ 1 New Failure, 2 Unrelated Failures

As of commit 19d87e8 with merge base d44c390 (image):

NEW FAILURE - The following job has failed:

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.

[ghstack-poisoned]
yanboliang added a commit that referenced this pull request Jan 9, 2025
ghstack-source-id: 8564d38
Pull Request resolved: #144444
@yanboliang yanboliang added the ciflow/trunk Trigger trunk jobs on your pull request label Jan 9, 2025
[ghstack-poisoned]
[ghstack-poisoned]
yanboliang added a commit that referenced this pull request Jan 10, 2025
ghstack-source-id: d521384
Pull Request resolved: #144444

x = torch.tensor([1, 2, 3, 4])
y = torch.tensor([10, 20, 30, 40])
self.assertEqual(fn(x, y), torch.tensor([11, 24, 39, 56]))
Copy link
Contributor

@zou3519 zou3519 Jan 13, 2025

Choose a reason for hiding this comment

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

Can you test that a second call to fn does not recompile? I'm suspicious of the ID_MATCH (you should just be able to guard on the level, key, and metadata of the Interpreter)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately it would recompile, because the interpreter's ID is changed if we call the compiled function again. Do you think we should add a new C++ guards for Interpreter like the DispatchKeySet I did in last PR?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, otherwise we are going to recompile every time a function that needs retrieve_current_functorch_interpreter gets called. Every time vmap gets called a fresh interpreter is being created

Copy link
Contributor

Choose a reason for hiding this comment

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

Is there an easy way to add a Pythons guard on the .level, .key, and .batch_size, and .randomness of the INterpreter?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Upon reviewing this again, I found my previous assessment to be incorrect. The recompilation is caused by a different reason, not the guard on the interpreter. This is because we always generate the interpreter within the compiled region, and there are no use cases where an interpreter is passed into the compiled region that would require guarding. A thorough search of the codebase confirmed this.

As such, it seems that only the sourceless builder is used in this case. We could either remove the builder when source is not None or leave it as is until we encounter a scenario where recompilation due to ID_MATCH becomes a genuine issue. Because we can't find a proper unit test either.

Comment on lines +696 to +698
elif isinstance(
value, (enum.Enum, torch.DispatchKey, torch._C._functorch.TransformType)
):
Copy link
Contributor

Choose a reason for hiding this comment

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

Are we sure that DispatchKey and TransformType actually behave like Python enums? (I don't care if they don't, this is close enough, but I'm just curious)

Copy link
Contributor

@zou3519 zou3519 Jan 13, 2025

Choose a reason for hiding this comment

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

If they do then I want some easy way for a developer to add support for more of these C++ enums. Maybe shove them into a list somewhere near EnumVariable

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think so, as DispatchKey and TransformType are pure python classes extending from enum.Enum, even though they are bound to their corresponding C++ types.

Copy link
Contributor

Choose a reason for hiding this comment

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

no action required here, might be nice to have an easy(ier) way to add more of these in the future

Copy link
Contributor

@zou3519 zou3519 left a comment

Choose a reason for hiding this comment

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

Looks fine, I want to check the recompilation and guarding

[ghstack-poisoned]
yanboliang added a commit that referenced this pull request Jan 14, 2025
ghstack-source-id: d4ab056
Pull Request resolved: #144444
[ghstack-poisoned]
[ghstack-poisoned]
yanboliang added a commit that referenced this pull request Jan 15, 2025
ghstack-source-id: 3460c02
Pull Request resolved: #144444
@yanboliang yanboliang requested a review from zou3519 January 15, 2025 19:19
Copy link
Contributor

@zou3519 zou3519 left a comment

Choose a reason for hiding this comment

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

cool I buy it

Copy link
Contributor

@zou3519 zou3519 left a comment

Choose a reason for hiding this comment

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

cool I buy it

[ghstack-poisoned]
yanboliang added a commit that referenced this pull request Jan 16, 2025
ghstack-source-id: 9e05fea
Pull Request resolved: #144444
[ghstack-poisoned]
yanboliang added a commit that referenced this pull request Jan 16, 2025
ghstack-source-id: 2ad930d
Pull Request resolved: #144444
@yanboliang
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

@pytorchmergebot
Copy link
Collaborator

Merge failed

Reason: 1 mandatory check(s) failed. The first few are:

Dig deeper by viewing the failures on hud

Details for Dev Infra team Raised by workflow job

Failing merge rule: Core Maintainers

@yanboliang
Copy link
Contributor Author

@pytorchbot merge -f "irrelevant failure"

@pytorchmergebot
Copy link
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

@yanboliang yanboliang deleted the gh/yanboliang/52/head branch January 17, 2025 02:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants