-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
[TOPI] [Hexagon] Batch flatten slice op initial version #11522
Merged
kparzysz-quic
merged 12 commits into
apache:main
from
abhikran-quic:batch_flatten_slice
Jun 30, 2022
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0c301de
[TOPI] [Hexagon] Batch flatten slice op initial version
abhikran-quic 7108e52
Fix lint errors
abhikran-quic fdb9dbc
Fix more lint errors
abhikran-quic 6839726
Fix lint warnings
abhikran-quic f4495bf
Fix review comments
abhikran-quic d7eed47
Merge branch 'main' into batch_flatten_slice
abhikran-quic 89479ba
Update tests to use util functions
abhikran-quic 61bd89a
Merge branch 'main' into batch_flatten_slice
abhikran-quic c4db7e3
Update __init__.py
abhikran-quic 0514097
Fix review comments
abhikran-quic 9fba46d
Merge branch 'main' into batch_flatten_slice
abhikran-quic e805feb
Merge branch 'main' into batch_flatten_slice
abhikran-quic 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 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 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,77 @@ | ||
# 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 batch flatten compute and schedule""" | ||
from tvm import te, tir, topi | ||
from ..utils import get_layout_transform_fn | ||
|
||
|
||
def batch_flatten_compute(inp: te.Tensor) -> te.Tensor: | ||
"""Compute for slice batch flatten op for hexagon. | ||
This op makes the following assumptions: | ||
1. This op is written for a sliced batch flatten operation. | ||
2. The input is assumed to be in NHWC layout. | ||
|
||
Parameters | ||
---------- | ||
Input : te.Tensor | ||
Input activations padded for inner dimension size | ||
Returns | ||
------- | ||
Output : te.Tensor | ||
Output of applying batch flatten operation on input | ||
""" | ||
return topi.nn.flatten(inp) | ||
|
||
|
||
def batch_flatten_stir_schedule( | ||
out: te.Tensor, | ||
inp: te.Tensor, | ||
out_layout: str, | ||
in_layout: str, | ||
) -> tir.Schedule: | ||
"""STIR schedule definition for the compute of batch flatten compute. | ||
Parameters | ||
---------- | ||
outputs : te.Tensor | ||
The output tensor as returned by a call to batch_flatten_compute | ||
input : te.Tensor | ||
Input tensor to batch_flatten | ||
out_layout: typing.Callable | ||
The transformation function definition for the expected output layout | ||
in_layout: typing.Callable | ||
The transformation function definition for the input layout | ||
Returns | ||
------- | ||
sch : tvm.tir.Schedule | ||
The STIR schedule for slice batch flatten compute | ||
""" | ||
|
||
batch_flatten_func = te.create_prim_func([inp, out]) | ||
sch = tir.Schedule(batch_flatten_func, debug_mask="all") | ||
compute = sch.get_block("compute") | ||
|
||
sch.transform_layout(compute, inp.name, get_layout_transform_fn(in_layout)) | ||
abhikran-quic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
sch.transform_layout(compute, out.name, get_layout_transform_fn(out_layout)) | ||
i, j = sch.get_loops(compute) | ||
jout, channel = sch.split(j, [None, inp.shape[3]]) | ||
height, width = sch.split(jout, [inp.shape[1], inp.shape[2]]) | ||
channelo, channeli = sch.split(channel, [None, 1024]) | ||
channelio, channelii = sch.split(channeli, [None, 64]) | ||
sch.reorder(i, height, width, channelo, channelio, channelii) | ||
sch.vectorize(channelii) | ||
return sch |
This file contains 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 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
101 changes: 101 additions & 0 deletions
101
tests/python/contrib/test_hexagon/topi/test_batch_flatten.py
This file contains 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,101 @@ | ||
# 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. | ||
|
||
import numpy as np | ||
import pytest | ||
|
||
import tvm | ||
import tvm.testing | ||
import tvm.topi.hexagon.slice_ops as sl | ||
from tvm import te, topi | ||
from tvm.contrib.hexagon.build import HexagonLauncher | ||
from tvm.topi import testing | ||
|
||
from ..infrastructure import allocate_hexagon_array, transform_numpy | ||
|
||
|
||
class BaseTestBatchFlatten: | ||
input_shape = tvm.testing.parameter( | ||
(1, 1, 1, 2048), | ||
(1, 2, 4, 2048), | ||
(1, 8, 8, 1024), | ||
(2, 4, 8, 1024), | ||
(2, 3, 5, 2048), | ||
) | ||
input_layout, input_axis_sep = tvm.testing.parameters(("nhwc-1024c-2d", [4])) | ||
output_layout, output_axis_sep = tvm.testing.parameters(("nc-1024-2d", [2])) | ||
data_type = tvm.testing.parameter("float16") | ||
|
||
|
||
class TestBatchFlatten(BaseTestBatchFlatten): | ||
@tvm.testing.fixture | ||
def output_shape(self, input_shape): | ||
return input_shape[0], input_shape[1] * input_shape[2] * input_shape[3] | ||
|
||
@tvm.testing.requires_hexagon | ||
def test_batch_flatten( | ||
self, | ||
data_type, | ||
input_shape, | ||
input_layout, | ||
input_axis_sep, | ||
output_shape, | ||
output_layout, | ||
output_axis_sep, | ||
hexagon_session, | ||
): | ||
target_hexagon = tvm.target.hexagon("v69") | ||
target = tvm.target.Target(target_hexagon, host=target_hexagon) | ||
A = te.placeholder(input_shape, name="A", dtype=data_type) | ||
D = sl.batch_flatten_compute(A) | ||
tir_s = sl.batch_flatten_stir_schedule( | ||
D, | ||
A, | ||
output_layout, | ||
input_layout, | ||
) | ||
func_name = "batch_flatten" | ||
with tvm.transform.PassContext(opt_level=3): | ||
runtime_module = tvm.build(tir_s.mod, target=target, name=func_name) | ||
|
||
mod = hexagon_session.load_module(runtime_module) | ||
|
||
a_numpy = (np.random.uniform(-1, 1, input_shape)).astype(data_type) | ||
ref = np.reshape(a_numpy, output_shape) | ||
|
||
input_np_transformed = transform_numpy(a_numpy, "nhwc", input_layout) | ||
ref_np_transformed = transform_numpy(ref, "nhwc", output_layout) | ||
|
||
a_tvm = allocate_hexagon_array( | ||
hexagon_session.device, | ||
data=input_np_transformed, | ||
axis_separators=input_axis_sep, | ||
mem_scope="global.vtcm", | ||
) | ||
output = allocate_hexagon_array( | ||
hexagon_session.device, | ||
ref_np_transformed.shape, | ||
data_type, | ||
axis_separators=output_axis_sep, | ||
mem_scope="global.vtcm", | ||
) | ||
mod(a_tvm, output) | ||
np.testing.assert_allclose(output.numpy(), ref_np_transformed, atol=1e-07, rtol=0) | ||
|
||
|
||
if __name__ == "__main__": | ||
tvm.testing.main(pytest.main(sys.argv)) |
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.
I'm a bit suspicious about assuming that there's a block named "compute", as I don't see any promises in the documentation about the name and what it represents. But making assumptions like this seems somewhat idiomatic within TVM, so IMHO it's okay enough.
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.
I completely agree with you and don't like it either. But, there doesn't seem to be a way to specify a different block name for topi defined ops. Please correct me if I'm wrong.
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.
Since I'm reusing
batch_flatten
compute intopi
, the block namecompute
comes up. Sharing the schedule belowThere 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.
👍