-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[TOPI][Hexagon] Implement Argmax Slice Op #11847
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
2c21040
[TOPI][Hexagon] Implement Argmax Slice Op
97f2503
run through black
arangasa b93ab71
Merge branch 'main' into slice_op_argmax
arangasa b9cc6eb
Address initial review comments
5cd8226
Fix variable names in tests
arangasa d03f891
Fix lint issue
arangasa 574e642
Merge branch 'main' into slice_op_argmax
arangasa 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| """ Hexagon slice argmax compute and schedule""" | ||
|
|
||
| from tvm import tir | ||
| from tvm import topi | ||
| from ..utils import get_layout_transform_fn | ||
|
|
||
|
|
||
| def argmax_compute(in_tensor, axis): | ||
| out_tensor = topi.argmax(in_tensor, axis) | ||
| return out_tensor | ||
|
|
||
|
|
||
| def argmax_stir_schedule_nhwc(func, in_layout, out_layout): | ||
| """Schedule for nhwc argmax""" | ||
| sch = tir.Schedule(func, debug_mask="all") | ||
| sch.transform_layout("A_red_temp", "A", in_layout) | ||
| sch.transform_layout("A_red", "A_red", out_layout) | ||
| return sch | ||
|
|
||
|
|
||
| def argmax_schedule(argmax_func, in_layout_str, out_layout_str): | ||
| """Schedule for argmax: top level function""" | ||
| if (in_layout_str == "nhwc-8h2w32c2w-2d") and (out_layout_str == "nhw-32h16w-2d"): | ||
| fp16_layout_transform = get_layout_transform_fn(in_layout_str) | ||
| int32_layout_transform = get_layout_transform_fn(out_layout_str) | ||
| tir_s = argmax_stir_schedule_nhwc( | ||
| argmax_func, fp16_layout_transform, int32_layout_transform | ||
| ) | ||
| return tir_s | ||
| raise RuntimeError(f"Unexpected input_layout, output_layout '{in_layout_str, out_layout_str}'") |
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
116 changes: 116 additions & 0 deletions
116
tests/python/contrib/test_hexagon/topi/test_argmax_slice.py
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,116 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| """ Tests for Hexagon slice argmax op """ | ||
| import numpy as np | ||
|
|
||
| import tvm | ||
| import tvm.testing | ||
| from tvm import te | ||
| import tvm.topi.hexagon.slice_ops as sl | ||
| import tvm.contrib.hexagon | ||
| from ..infrastructure import allocate_hexagon_array, transform_numpy | ||
|
|
||
|
|
||
| class TestArgMaxSlice: | ||
| """Argmax Slice Op Tests""" | ||
|
|
||
| ( | ||
| input_shape, | ||
| input_layout, | ||
| output_layout, | ||
| in_axis, | ||
| in_axis_sep, | ||
| out_axis_sep, | ||
| ) = tvm.testing.parameters( | ||
| ((1, 64, 64, 32), "nhwc-8h2w32c2w-2d", "nhw-32h16w-2d", [3], [4], [3]), | ||
| ((3, 32, 16, 32), "nhwc-8h2w32c2w-2d", "nhw-32h16w-2d", [3], [4], [3]), | ||
| ((1, 32, 32, 64), "nhwc-8h2w32c2w-2d", "nhw-32h16w-2d", [3], [4], [3]), | ||
| ) | ||
| dtype = tvm.testing.parameter("float16") | ||
| working_scope = tvm.testing.parameter("global.vtcm") | ||
|
|
||
| @tvm.testing.fixture | ||
| def input_np(self, input_shape, dtype): | ||
| return np.random.uniform(size=input_shape).astype(dtype) | ||
|
|
||
| @tvm.testing.fixture | ||
| def transformed_input_np(self, input_np, input_layout): | ||
| return transform_numpy(input_np, "nhwc", input_layout) | ||
|
|
||
| @tvm.testing.fixture | ||
| def expected_output_np(self, input_np, in_axis): | ||
| ref_np = np.argmax(input_np, *in_axis).astype("int32") | ||
| return ref_np | ||
|
|
||
| @tvm.testing.fixture | ||
| def transformed_expected_output_np(self, expected_output_np, output_layout): | ||
| return transform_numpy(expected_output_np, "nhw", output_layout) | ||
|
|
||
| @tvm.testing.requires_hexagon | ||
| def test_argmax_slice( | ||
| self, | ||
| input_shape, | ||
| dtype, | ||
| input_layout, | ||
| output_layout, | ||
| in_axis, | ||
| transformed_input_np, | ||
| transformed_expected_output_np, | ||
| in_axis_sep, | ||
| out_axis_sep, | ||
| hexagon_session, | ||
| working_scope, | ||
| ): | ||
| """Top level testing function for argmax""" | ||
| target_hexagon = tvm.target.hexagon("v69") | ||
| target = tvm.target.Target(target_hexagon, host=target_hexagon) | ||
| argmax_input = te.placeholder(input_shape, name="A", dtype=dtype) | ||
| output = sl.argmax.argmax_compute(argmax_input, in_axis) | ||
| argmax_func = te.create_prim_func([argmax_input, output]) | ||
| tir_s = sl.argmax_schedule(argmax_func, input_layout, output_layout) | ||
| input_data = allocate_hexagon_array( | ||
| hexagon_session.device, | ||
| data=transformed_input_np, | ||
| axis_separators=in_axis_sep, | ||
| mem_scope=working_scope, | ||
| ) | ||
| output_data = allocate_hexagon_array( | ||
| hexagon_session.device, | ||
| tensor_shape=transformed_expected_output_np.shape, | ||
| dtype=transformed_expected_output_np.dtype, | ||
| axis_separators=out_axis_sep, | ||
| mem_scope=working_scope, | ||
| ) | ||
| with tvm.transform.PassContext(opt_level=3, config={"tir.disable_assert": True}): | ||
| tir_irm = tvm.lower(tir_s.mod, [argmax_input, output], name="argmax") | ||
| runtime_module = tvm.build( | ||
| tir_irm, [argmax_input, output], target=target, name="argmax" | ||
| ) | ||
| mod = hexagon_session.load_module(runtime_module) | ||
|
|
||
| mod(input_data, output_data) | ||
| output_np = output_data.numpy() | ||
| tvm.testing.assert_allclose( | ||
| output_np, | ||
| transformed_expected_output_np, | ||
| 1e-3, | ||
| 1e-3, | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| tvm.testing.main() | ||
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.
add this at the end of the test file:
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.
Hi @mehrdadh , Thank you for your time and inputs. Addressed the comments.