-
Notifications
You must be signed in to change notification settings - Fork 747
Arm backend: Add decomposition of div.Tensor_mode #13940
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
7 commits
Select commit
Hold shift + click to select a range
44d7528
Arm backend: Add support for div.Tensor_mode op
wwwind b70b856
Arm backend: Add support for div.Tensor_mode op
wwwind 337ecaf
Merge branch 'main' into div
wwwind 66580b6
Merge branch 'main' into div
zingo a4e3e36
Merge branch 'main' into div
wwwind bebd8aa
Arm backend: fix test for U55
wwwind 0d3d423
Arm backend: fix lint error
wwwind 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,84 @@ | ||
| # Copyright 2025 Arm Limited and/or its affiliates. | ||
| # | ||
| # This source code is licensed under the BSD-style license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
|
|
||
| # pyre-unsafe | ||
|
|
||
| import torch | ||
| from executorch.exir.dialects._ops import ops as exir_ops | ||
| from executorch.exir.pass_base import ExportPass | ||
|
|
||
| edge_div_mode_ops = (exir_ops.edge.aten.div.Tensor_mode,) | ||
| aten_div_mode_ops = (torch.ops.aten.div.Tensor_mode,) | ||
|
|
||
| edge_unary = { | ||
| "div": exir_ops.edge.aten.div.Tensor, | ||
| "floor": exir_ops.edge.aten.floor.default, | ||
| "ceil": exir_ops.edge.aten.ceil.default, | ||
| "full": exir_ops.edge.aten.full.default, | ||
| "lt": exir_ops.edge.aten.lt.Tensor, | ||
| "where": exir_ops.edge.aten.where.self, | ||
| } | ||
|
|
||
| aten_unary = { | ||
| "div": torch.ops.aten.div.Tensor, | ||
| "floor": torch.ops.aten.floor.default, | ||
| "ceil": torch.ops.aten.ceil.default, | ||
| "full": torch.ops.aten.full.default, | ||
| "lt": torch.ops.aten.lt.Tensor, | ||
| "where": torch.ops.aten.where.self, | ||
| } | ||
|
|
||
|
|
||
| def _get_opset(op): | ||
| if op in edge_div_mode_ops: | ||
| return edge_unary | ||
| if op in aten_div_mode_ops: | ||
| return aten_unary | ||
| raise RuntimeError(f"div.Tensor_mode not supported for op {op}") | ||
|
|
||
|
|
||
| class DecomposeDivTensorModePass(ExportPass): | ||
| """ | ||
| Rewrites aten.div.Tensor_mode into | ||
|
|
||
| rounding_mode=None -> div(a, b) | ||
| rounding_mode='floor' -> floor(div(a, b)) | ||
| rounding_mode='trunc' -> where(div(a,b) < 0, ceil(div(a,b)), floor(div(a,b))) | ||
| """ | ||
|
|
||
| def call_operator(self, op, args, kwargs, meta): | ||
| if op not in (edge_div_mode_ops + aten_div_mode_ops): | ||
| return super().call_operator(op, args, kwargs, meta) | ||
|
|
||
| opset = _get_opset(op) | ||
|
|
||
| a, b = args[0], args[1] | ||
| rounding_mode = kwargs.get("rounding_mode", None) | ||
| if rounding_mode is None and len(args) > 2: | ||
| rounding_mode = args[2] | ||
|
|
||
| q = super().call_operator(opset["div"], (a, b), {}, meta) | ||
|
|
||
| if rounding_mode is None: | ||
| return q | ||
|
|
||
| if rounding_mode == "floor": | ||
| return super().call_operator(opset["floor"], (q,), {}, meta) | ||
|
|
||
| if rounding_mode == "trunc": | ||
| zero = super().call_operator( | ||
| opset["full"], | ||
| args=((1,) * len(meta["val"].size()), 0.0), | ||
| kwargs={"dtype": torch.float32}, | ||
| meta=meta, | ||
| ) | ||
| lt0 = self.call_operator(opset["lt"], (q, zero), {}, meta) | ||
| ceilq = self.call_operator(opset["ceil"], (q,), {}, meta) | ||
| floorq = self.call_operator(opset["floor"], (q,), {}, meta) | ||
| return self.call_operator(opset["where"], (lt0, ceilq, floorq), {}, meta) | ||
|
|
||
| raise RuntimeError( | ||
| f"Unsupported rounding_mode for div.Tensor_mode: {rounding_mode!r}" | ||
| ) |
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,151 @@ | ||
| # Copyright 2025 Arm Limited and/or its affiliates. | ||
| # 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 Tuple | ||
|
|
||
| import pytest | ||
| import torch | ||
|
|
||
| from executorch.backends.arm.test import common | ||
| from executorch.backends.arm.test.tester.test_pipeline import ( | ||
| EthosU55PipelineINT, | ||
| EthosU85PipelineINT, | ||
| TosaPipelineFP, | ||
| TosaPipelineINT, | ||
| VgfPipeline, | ||
| ) | ||
|
|
||
| input_tt = Tuple[torch.Tensor, torch.Tensor] | ||
|
|
||
|
|
||
| def make_float_div_inputs(B: int = 4, T: int = 64) -> input_tt: | ||
| x = torch.randn(B, T) | ||
| # guard against zero in denominator | ||
| y = torch.randn(B, T).abs() + 1e-3 | ||
| return x, y | ||
|
|
||
|
|
||
| class DivTensorModeFloat(torch.nn.Module): | ||
| """ | ||
| torch.div(x, y, rounding_mode=mode) with | ||
| mode from {None, "floor", "trunc"}. | ||
| """ | ||
|
|
||
| aten_ops = ["aten.div.Tensor_mode"] | ||
| aten_ops_int = ["aten.mul.Tensor", "aten.reciprocal.default"] | ||
|
|
||
| def __init__(self, mode=None): | ||
| super().__init__() | ||
| assert mode in (None, "floor", "trunc") | ||
| self.mode = mode | ||
|
|
||
| def forward(self, x: torch.Tensor, y: torch.Tensor) -> torch.Tensor: | ||
| return torch.div(x, y, rounding_mode=self.mode) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("mode", [None, "floor", "trunc"]) | ||
| def test_div_tensor_mode_tosa_FP(mode): | ||
|
|
||
| model = DivTensorModeFloat(mode) | ||
| inputs = make_float_div_inputs() | ||
|
|
||
| pipeline = TosaPipelineFP[input_tt]( | ||
| model, | ||
| inputs, | ||
| aten_op=model.aten_ops, | ||
| exir_op=[], | ||
| use_to_edge_transform_and_lower=True, | ||
| ) | ||
| pipeline.pop_stage("check_count.exir") | ||
| pipeline.run() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("mode", [None, "floor", "trunc"]) | ||
| def test_div_tensor_mode_tosa_INT(mode): | ||
|
|
||
| model = DivTensorModeFloat(mode) | ||
| inputs = make_float_div_inputs() | ||
|
|
||
| pipeline = TosaPipelineINT[input_tt]( | ||
| model, | ||
| inputs, | ||
| aten_op=model.aten_ops_int, | ||
| exir_op=[], | ||
| use_to_edge_transform_and_lower=True, | ||
| ) | ||
| pipeline.pop_stage("check_count.exir") | ||
| pipeline.run() | ||
|
|
||
|
|
||
| @common.XfailIfNoCorstone300 | ||
| @pytest.mark.parametrize("mode", [None, "floor"]) | ||
| def test_div_tensor_mode_u55_INT(mode): | ||
|
|
||
| model = DivTensorModeFloat(mode) | ||
| inputs = make_float_div_inputs() | ||
|
|
||
| pipeline = EthosU55PipelineINT[input_tt]( | ||
| model, | ||
| inputs, | ||
| aten_ops=model.aten_ops_int, | ||
| exir_ops=[], | ||
| use_to_edge_transform_and_lower=True, | ||
| run_on_fvp=True, | ||
| ) | ||
| pipeline.run() | ||
|
|
||
|
|
||
| @common.XfailIfNoCorstone320 | ||
| @pytest.mark.parametrize("mode", [None, "floor", "trunc"]) | ||
| def test_div_tensor_mode_u85_INT(mode): | ||
|
|
||
| model = DivTensorModeFloat(mode) | ||
| inputs = make_float_div_inputs() | ||
|
|
||
| pipeline = EthosU85PipelineINT[input_tt]( | ||
| model, | ||
| inputs, | ||
| aten_ops=model.aten_ops_int, | ||
| exir_ops=[], | ||
| use_to_edge_transform_and_lower=True, | ||
| run_on_fvp=True, | ||
| ) | ||
| pipeline.run() | ||
|
|
||
|
|
||
| @common.SkipIfNoModelConverter | ||
| @pytest.mark.parametrize("mode", [None, "floor", "trunc"]) | ||
| def test_div_tensor_mode_vgf_INT(mode): | ||
|
|
||
| model = DivTensorModeFloat(mode) | ||
| inputs = make_float_div_inputs() | ||
|
|
||
| pipeline = VgfPipeline[input_tt]( | ||
| model, | ||
| inputs, | ||
| aten_op=model.aten_ops_int, | ||
| exir_op=[], | ||
| tosa_version="TOSA-1.0+INT", | ||
| use_to_edge_transform_and_lower=True, | ||
| ) | ||
| pipeline.pop_stage("check_count.exir") | ||
| pipeline.run() | ||
|
|
||
|
|
||
| @common.SkipIfNoModelConverter | ||
| @pytest.mark.parametrize("mode", [None, "floor", "trunc"]) | ||
| def test_div_tensor_mode_vgf_FP(mode): | ||
|
|
||
| model = DivTensorModeFloat(mode) | ||
| inputs = make_float_div_inputs() | ||
|
|
||
| pipeline = VgfPipeline[input_tt]( | ||
| model, | ||
| inputs, | ||
| aten_op=model.aten_ops, | ||
| exir_op=[], | ||
| tosa_version="TOSA-1.0+FP", | ||
| use_to_edge_transform_and_lower=True, | ||
| ) | ||
| pipeline.run() | ||
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.
Maybe add @common.XfailIfNoCorstone300
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.
oh, yes, missed yhis