-
Notifications
You must be signed in to change notification settings - Fork 685
Aoti support multi method #14715
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
Merged
Aoti support multi method #14715
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 |
---|---|---|
@@ -0,0 +1,115 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
# pyre-strict | ||
|
||
from typing import Iterable | ||
|
||
import torch | ||
from executorch.exir.dialects._ops import ops | ||
from executorch.exir.pass_base import ExportPass, PassResult | ||
from torch import fx | ||
|
||
|
||
_SLICE_COPY_TARGETS = ( | ||
torch.ops.aten.slice_copy.Tensor, | ||
ops.edge.aten.slice_copy.Tensor, | ||
) | ||
|
||
_SLICE_TARGETS = { | ||
torch.ops.aten.slice_copy.Tensor: torch.ops.aten.slice.Tensor, | ||
ops.edge.aten.slice_copy.Tensor: ops.edge.aten.slice.Tensor, | ||
} | ||
|
||
|
||
class ReplaceSliceCopyWithSlicePass(ExportPass): | ||
"""Replace non-mutated ``slice_copy`` results with ``slice`` views.""" | ||
|
||
def call(self, graph_module: fx.GraphModule) -> PassResult: | ||
graph_changed = False | ||
|
||
for node in graph_module.graph.nodes: | ||
if node.op != "call_function" or node.target not in _SLICE_COPY_TARGETS: | ||
continue | ||
|
||
if self._has_blocking_user(node, node.users.keys()): | ||
continue | ||
|
||
node.target = _SLICE_TARGETS[node.target] | ||
graph_changed = True | ||
|
||
if graph_changed: | ||
graph_module.graph.lint() | ||
graph_module.recompile() | ||
|
||
return PassResult(graph_module, graph_changed) | ||
|
||
def _has_blocking_user(self, node: fx.Node, users: Iterable[fx.Node]) -> bool: | ||
for user in users: | ||
if self._is_mutating_user(node, user) or self._is_view_user(node, user): | ||
return True | ||
return False | ||
|
||
def _is_mutating_user(self, node: fx.Node, user: fx.Node) -> bool: | ||
if user.op == "call_method": | ||
# Treat in-place tensor methods conservatively as mutations only when the | ||
# method name ends with ``_`` which is the PyTorch convention for mutation. | ||
return isinstance(user.target, str) and user.target.endswith("_") | ||
|
||
if user.op != "call_function": | ||
return False | ||
|
||
target = user.target | ||
if not hasattr(target, "_schema"): | ||
return False | ||
|
||
schema = target._schema # pyre-ignore[16] | ||
# Positional arguments | ||
for index, arg in enumerate(user.args): | ||
if arg is node and self._argument_mutates(schema, index): | ||
return True | ||
|
||
# Keyword arguments | ||
for name, arg in user.kwargs.items(): | ||
if arg is node and self._argument_mutates(schema, name): | ||
return True | ||
|
||
return False | ||
|
||
def _is_view_user(self, node: fx.Node, user: fx.Node) -> bool: | ||
if user.op == "call_method": | ||
# Treat tensor methods conservatively and assume they may be view-producing. | ||
return True | ||
|
||
if user.op != "call_function": | ||
return False | ||
|
||
target = user.target | ||
if getattr(target, "is_view", False): | ||
for arg in user.args: | ||
if arg is node: | ||
return True | ||
for arg in user.kwargs.values(): | ||
if arg is node: | ||
return True | ||
|
||
return False | ||
|
||
def _argument_mutates( | ||
self, schema: torch._C.FunctionSchema, key | ||
) -> bool: # pyre-ignore[11] | ||
arguments = schema.arguments | ||
if isinstance(key, int): | ||
if key >= len(arguments): | ||
return False | ||
argument = arguments[key] | ||
else: | ||
argument = next((arg for arg in arguments if arg.name == key), None) | ||
if argument is None: | ||
return False | ||
|
||
alias_info = argument.alias_info | ||
return bool(alias_info and alias_info.is_write) |
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.
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.
Seems pretty hacky to run the force functionalization pass and then come through and undo it (but only for slice). Wont you in practice have to do this for all view ops?
Does AOTI lowering typically happen on functionalized IR?
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.
Inductor's reinplace pass reverts most of the functionalization. I don't think it handles
slice_copy
though, since it comes from this pass: https://github.com/pytorch/executorch/blob/main/exir/passes/replace_broken_ops_with_function_ops_pass.py#L13The other option we can do is to optionally run this pass in
to_edge()
.