-
Notifications
You must be signed in to change notification settings - Fork 149
Track generated functions for torch compile #1094
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
Merged
+113
−17
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,18 @@ | ||
| import copy | ||
| from typing import Any | ||
|
|
||
| from pytensor.graph.basic import Variable | ||
| from pytensor.link.basic import JITLinker | ||
| from pytensor.link.utils import unique_name_generator | ||
|
|
||
|
|
||
| class PytorchLinker(JITLinker): | ||
| """A `Linker` that compiles NumPy-based operations using torch.compile.""" | ||
|
|
||
| def __init__(self, *args, **kwargs): | ||
| super().__init__(*args, **kwargs) | ||
| self.gen_functors = [] | ||
|
|
||
| def input_filter(self, inp: Any) -> Any: | ||
| from pytensor.link.pytorch.dispatch import pytorch_typify | ||
|
|
||
|
|
@@ -18,14 +24,68 @@ def output_filter(self, var: Variable, out: Any) -> Any: | |
| def fgraph_convert(self, fgraph, input_storage, storage_map, **kwargs): | ||
| from pytensor.link.pytorch.dispatch import pytorch_funcify | ||
|
|
||
| # We want to have globally unique names | ||
| # across the entire pytensor graph, not | ||
| # just the subgraph | ||
| generator = unique_name_generator(["torch_linker"]) | ||
|
|
||
| # Ensure that torch is aware of the generated | ||
| # code so we can compile without graph breaks | ||
| def conversion_func_register(*args, **kwargs): | ||
| functor = pytorch_funcify(*args, **kwargs) | ||
| name = kwargs["unique_name"](functor) | ||
| self.gen_functors.append((f"_{name}", functor)) | ||
| return functor | ||
|
|
||
| built_kwargs = { | ||
| "unique_name": generator, | ||
| "conversion_func": conversion_func_register, | ||
| **kwargs, | ||
| } | ||
| return pytorch_funcify( | ||
| fgraph, input_storage=input_storage, storage_map=storage_map, **kwargs | ||
| fgraph, input_storage=input_storage, storage_map=storage_map, **built_kwargs | ||
| ) | ||
|
|
||
| def jit_compile(self, fn): | ||
| import torch | ||
|
|
||
| return torch.compile(fn) | ||
| class wrapper: | ||
| """ | ||
| Pytorch would fail compiling our method when trying | ||
| to resolve some of the methods returned from dispatch | ||
| calls. We want to be careful to not leak the methods, | ||
| so this class just holds them and provisions the expected | ||
| location accordingly | ||
|
|
||
| https://discuss.pytorch.org/t/closures-are-being-gcd-and-causing-failures-to-compile/213319 | ||
| """ | ||
|
|
||
| def __init__(self, fn, gen_functors): | ||
| self.fn = torch.compile(fn) | ||
| self.gen_functors = copy.copy(gen_functors) | ||
|
|
||
| def __call__(self, *args, **kwargs): | ||
| import pytensor.link.utils | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note, there is no way this is threadsafe. |
||
|
|
||
| # set attrs | ||
| for n, fn in self.gen_functors: | ||
| setattr(pytensor.link.utils, n[1:], fn) | ||
|
|
||
| res = self.fn(*args, **kwargs) | ||
|
|
||
| # unset attrs | ||
| for n, _ in self.gen_functors: | ||
| if getattr(pytensor.link.utils, n[1:], False): | ||
| delattr(pytensor.link.utils, n[1:]) | ||
|
|
||
| return res | ||
|
|
||
| def __del__(self): | ||
| del self.gen_functors | ||
|
|
||
| res = wrapper(fn, self.gen_functors) | ||
| self.gen_functors = [] | ||
| return res | ||
|
|
||
| def create_thunk_inputs(self, storage_map): | ||
| thunk_inputs = [] | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.