-
Notifications
You must be signed in to change notification settings - Fork 607
Support rsqrt in XNNPACK backend #7992
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ | |
op_prelu, | ||
op_quantize_per_tensor, | ||
op_relu, | ||
op_rsqrt, | ||
op_sdpa, | ||
op_sigmoid, | ||
op_skip_ops, | ||
|
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,52 @@ | ||
# 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. | ||
|
||
from typing import Dict | ||
|
||
import torch | ||
from executorch.backends.xnnpack.operators.node_visitor import ( | ||
NodeVisitor, | ||
register_node_visitor, | ||
) | ||
from executorch.backends.xnnpack.serialization.xnnpack_graph_schema import ( | ||
XNNGraph, | ||
XNNReciprocalSquareRoot, | ||
XNode, | ||
) | ||
from executorch.backends.xnnpack.utils.utils import get_input_node | ||
|
||
|
||
@register_node_visitor | ||
class ReciprocalSquareRootVisitor(NodeVisitor): | ||
target = "aten.rsqrt.default" | ||
|
||
def __init__(self, *args) -> None: | ||
super().__init__(*args) | ||
|
||
def define_node( | ||
self, | ||
node: torch.fx.Node, | ||
xnn_graph: XNNGraph, | ||
vals_to_ids: Dict[torch.fx.Node, int], | ||
debug_handle: int, | ||
) -> None: | ||
self.define_nodes_tensor_inputs_outputs(node, xnn_graph, vals_to_ids) | ||
|
||
# input | ||
input_id = vals_to_ids[get_input_node(node, 0)] | ||
|
||
# output | ||
output_id = vals_to_ids[node] | ||
|
||
ser_node = XNode( | ||
xnode_union=XNNReciprocalSquareRoot( | ||
input_id=input_id, | ||
output_id=output_id, | ||
flags=0, | ||
), | ||
debug_handle=debug_handle, | ||
) | ||
xnn_graph.xnodes.append(ser_node) |
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
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
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,42 @@ | ||
# 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. | ||
|
||
import unittest | ||
|
||
import torch | ||
from executorch.backends.xnnpack.test.tester import Tester | ||
|
||
|
||
class TestRsqrt(unittest.TestCase): | ||
class Rsqrt(torch.nn.Module): | ||
def __init__(self): | ||
super().__init__() | ||
|
||
def forward(self, x): | ||
x = torch.abs(x) | ||
z = torch.rsqrt(x) | ||
return z | ||
|
||
def _test_rsqrt(self, inputs): | ||
( | ||
Tester(self.Rsqrt(), inputs) | ||
.export() | ||
.check_count({"torch.ops.aten.rsqrt.default": 1}) | ||
.to_edge_transform_and_lower() | ||
.check_count({"torch.ops.higher_order.executorch_call_delegate": 1}) | ||
.check_not(["executorch_exir_dialects_edge__ops_aten_rsqrt_default"]) | ||
.to_executorch() | ||
.serialize() | ||
.run_method_and_compare_outputs() | ||
) | ||
|
||
def test_fp16_rsqrt(self): | ||
inputs = (torch.randn(20).to(torch.float16),) | ||
self._test_rsqrt(inputs) | ||
|
||
def test_fp32_rsqrt(self): | ||
inputs = (torch.randn(20),) | ||
self._test_rsqrt(inputs) |
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.
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.
Nit: Do you want to improve this test by adding dynamic_shape support like this?
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.
it's a pointwise operator, why does it care about the shape? Everything is the same as sqrt including the test
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.
Yeah, just checking boxes, i.e. adding dynamic shape test for every operator.
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.
out of scope for this change! :)