-
Notifications
You must be signed in to change notification settings - Fork 607
Add pass to convert special case of mean.dim to averagepool2d #4900
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
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,52 @@ | ||
# Copyright 2024 Arm Limited and/or its 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 Any, cast, Dict, Tuple | ||
|
||
import torch.fx | ||
|
||
from executorch.exir.dialects._ops import ops as exir_ops | ||
from executorch.exir.pass_base import ExportPass, NodeMetadata, ProxyValue | ||
|
||
Argument = Any | ||
|
||
|
||
class ConvertMeanDimToAveragePool(ExportPass): | ||
""" | ||
Replace a mean operation with dim = [-1, -2] and keep_dim = True with an average pool operation. | ||
""" | ||
|
||
def call_operator( | ||
self, | ||
op: torch.fx.node.Target, | ||
args: Tuple[Argument, ...], | ||
kwargs: Dict[str, Argument], | ||
meta: NodeMetadata, | ||
) -> ProxyValue: | ||
if op != exir_ops.edge.aten.mean.dim: | ||
return super().call_operator(op, args, kwargs, meta) | ||
|
||
input_value = cast(ProxyValue, args[0]) | ||
dim = cast(list, args[1]) | ||
keep_dim = cast(bool, args[2]) if len(args) > 2 else False | ||
|
||
# averagepool2d gets converted to a mean operation with dim = [-1, -2] and keep_dim = True | ||
# so check the dim argument for this case | ||
if dim == [-1, -2] and keep_dim is True: | ||
# Given the shape format of input is (N, C, H, W) | ||
kernel_size = [ | ||
input_value.to_tensor().size()[2], | ||
input_value.to_tensor().size()[3], | ||
] | ||
stride = [1, 1] | ||
return super().call_operator( | ||
exir_ops.edge.aten.avg_pool2d.default, | ||
(input_value, kernel_size, stride), | ||
{}, | ||
meta, | ||
) | ||
else: | ||
return super().call_operator(op, args, kwargs, meta) |
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,75 @@ | ||
# Copyright 2024 Arm Limited and/or its 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.arm.passes.meandim_to_averagepool_pass import ( | ||
ConvertMeanDimToAveragePool, | ||
) | ||
|
||
from executorch.backends.arm.test import common | ||
from executorch.backends.arm.test.tester.arm_tester import ArmTester | ||
|
||
from executorch.backends.xnnpack.test.tester.tester import RunPasses | ||
|
||
|
||
class MeanDim(torch.nn.Module): | ||
def forward(self, x): | ||
return torch.mean(x, dim=[-1, -2], keepdim=True) | ||
|
||
def get_inputs(self): | ||
return (torch.rand(1, 1280, 7, 7),) | ||
|
||
|
||
class MeanDim2(torch.nn.Module): | ||
def forward(self, x): | ||
return torch.mean(x, dim=1) | ||
|
||
def get_inputs(self): | ||
return (torch.rand(1, 1280, 7, 7),) | ||
|
||
|
||
class TestMeandimToAveragePool2dPass(unittest.TestCase): | ||
""" | ||
Tests the MeanDimToAveragePool2dPass which converts mean.dim to average_pool2d | ||
for the special case where dim is [-1, -2] and keepdim is True. | ||
""" | ||
|
||
def test_tosa_BI_meandim_to_averagepool(self): | ||
module = MeanDim() | ||
test_pass_stage = RunPasses([ConvertMeanDimToAveragePool]) | ||
( | ||
ArmTester( | ||
module, | ||
example_inputs=module.get_inputs(), | ||
compile_spec=common.get_tosa_compile_spec(), | ||
) | ||
.quantize() | ||
.export() | ||
.to_edge() | ||
.check(["executorch_exir_dialects_edge__ops_aten_mean_dim"]) | ||
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. ok I see you are doing here, thanks. |
||
.run_passes(test_pass_stage) | ||
.check(["executorch_exir_dialects_edge__ops_aten_avg_pool2d_default"]) | ||
) | ||
|
||
def test_tosa_BI_meandim_no_modification(self): | ||
module = MeanDim2() | ||
test_pass_stage = RunPasses([ConvertMeanDimToAveragePool]) | ||
( | ||
ArmTester( | ||
module, | ||
example_inputs=module.get_inputs(), | ||
compile_spec=common.get_tosa_compile_spec(), | ||
) | ||
.quantize() | ||
.export() | ||
.to_edge() | ||
.check(["executorch_exir_dialects_edge__ops_aten_mean_dim"]) | ||
.run_passes(test_pass_stage) | ||
.check(["executorch_exir_dialects_edge__ops_aten_mean_dim"]) | ||
.check_not(["executorch_exir_dialects_edge__ops_aten_avg_pool2d_default"]) | ||
) |
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.