-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
[Hexagon] Softmax slice op initial version #11559
Merged
Merged
Changes from all commits
Commits
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,76 @@ | ||
# 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 softmax compute and schedule""" | ||
|
||
import typing | ||
|
||
from tvm import te, tir, topi | ||
from ..utils import get_layout_transform_fn | ||
|
||
|
||
def softmax_compute(in_tensor): | ||
""" | ||
Compute for slice softmax op for hexagon. | ||
This op makes the following assumptions: | ||
1. This op is written for a sliced softmax operation. | ||
2. The input is assumed to be in NC layout. | ||
""" | ||
return topi.nn.softmax(in_tensor, axis=1) | ||
|
||
|
||
def softmax_stir_schedule( | ||
out: te.Tensor, inp: te.Tensor, out_layout: typing.Callable, in_layout: typing.Callable | ||
): | ||
""" | ||
STIR schedule definition for the compute of softmax | ||
""" | ||
|
||
in_layout = get_layout_transform_fn(in_layout) | ||
out_layout = get_layout_transform_fn(out_layout) | ||
|
||
func = te.create_prim_func([inp, out]) | ||
sch = tir.Schedule(func, debug_mask="all") | ||
|
||
max_tensor = sch.get_block("T_softmax_maxelem") | ||
exp_tensor = sch.get_block("T_softmax_exp") | ||
sum_tensor = sch.get_block("T_softmax_expsum") | ||
out_tensor = sch.get_block("T_softmax_norm") | ||
|
||
sch.transform_layout(max_tensor, inp.name, in_layout) | ||
sch.transform_layout(out_tensor, out.name, out_layout) | ||
|
||
_, c_inner = sch.get_loops(max_tensor) | ||
_, c_inner_i = sch.split(c_inner, [None, 64]) | ||
rf_max = sch.rfactor(c_inner_i, 0) | ||
_, _, max_inner = sch.get_loops(rf_max) | ||
sch.vectorize(max_inner) | ||
|
||
_, loopi = sch.get_loops(exp_tensor) | ||
_, loopi_i = sch.split(loopi, [None, 512]) | ||
sch.vectorize(loopi_i) | ||
|
||
_, c_sum_inner = sch.get_loops(sum_tensor) | ||
_, c_sum_inner_i = sch.split(c_sum_inner, [None, 64]) | ||
rf_sum = sch.rfactor(c_sum_inner_i, 0) | ||
_, _, sum_inner = sch.get_loops(rf_sum) | ||
sch.vectorize(sum_inner) | ||
|
||
_, c_out_inner = sch.get_loops(out_tensor) | ||
_, c_out_inner_i = sch.split(c_out_inner, [None, 512]) | ||
sch.vectorize(c_out_inner_i) | ||
|
||
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
140 changes: 140 additions & 0 deletions
140
tests/python/contrib/test_hexagon/test_softmax_slice.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,140 @@ | ||
# 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 pytest | ||
import numpy as np | ||
from tvm import te, topi | ||
|
||
import tvm.testing | ||
from tvm.topi import testing | ||
from tvm.contrib.hexagon.build import HexagonLauncher | ||
|
||
import tvm.topi.hexagon.slice_ops as sl | ||
from .infrastructure import allocate_hexagon_array | ||
|
||
|
||
def transform_numpy(arr_np, layout): | ||
|
||
if layout in ["nc-512c-2d"]: | ||
N, C = arr_np.shape | ||
return arr_np.reshape([N, C // 512, 512]) | ||
raise RuntimeError(f"Unexpected layout '{layout}'") | ||
|
||
|
||
@tvm.testing.fixture | ||
def input_np(input_shape, dtype): | ||
return (np.random.uniform(size=input_shape)).astype(dtype) | ||
|
||
|
||
@tvm.testing.fixture | ||
def transformed_expected_output_np(expected_output_np, output_layout): | ||
return transform_numpy(expected_output_np, output_layout) | ||
|
||
|
||
@tvm.testing.fixture | ||
def transformed_input_np(input_np, input_layout): | ||
return transform_numpy(input_np, input_layout) | ||
|
||
|
||
class Basesoftmax2d: | ||
|
||
input_shape, input_layout, output_layout, axis_sep = tvm.testing.parameters( | ||
((1, 1024), "nc-512c-2d", "nc-512c-2d", [2]) | ||
) | ||
dtype = tvm.testing.parameter("float32") | ||
working_scope = tvm.testing.parameter("global.vtcm") | ||
|
||
|
||
class TestSoftmax2d(Basesoftmax2d): | ||
@tvm.testing.fixture | ||
def expected_output_np(self, input_np): | ||
if len(input_np.shape) == 2: | ||
ref_np_2d = tvm.topi.testing.softmax_python(input_np) | ||
return ref_np_2d | ||
raise RuntimeError(f"Unexpected input shape '{input_np.shape}'") | ||
|
||
@tvm.testing.requires_hexagon | ||
def test_softmax_f32( | ||
self, | ||
dtype, | ||
input_layout, | ||
output_layout, | ||
input_shape, | ||
input_np, | ||
transformed_input_np, | ||
transformed_expected_output_np, | ||
expected_output_np, | ||
working_scope, | ||
axis_sep, | ||
hexagon_session, | ||
): | ||
|
||
target_hexagon = tvm.target.hexagon( | ||
"v69", | ||
llvm_options="--disable-loop-unrolling-pass", | ||
) | ||
A = te.placeholder(input_shape, name="A", dtype=dtype) | ||
|
||
O = sl.softmax_compute(A) | ||
|
||
if input_layout == "nc-512c-2d": | ||
tir_s = sl.softmax_stir_schedule(O, A, output_layout, input_layout) | ||
sch = tir_s.mod | ||
else: | ||
raise RuntimeError(f"Unexpected input layout '{input_layout}'") | ||
|
||
with tvm.transform.PassContext( | ||
opt_level=3, | ||
config={ | ||
"tir.LoopPartition": {"partition_const_loop": True}, | ||
}, | ||
): | ||
|
||
func = tvm.build( | ||
sch, | ||
[A, O], | ||
tvm.target.Target(target_hexagon, host=target_hexagon), | ||
name="softmax_slice", | ||
) | ||
|
||
input_arr = allocate_hexagon_array( | ||
hexagon_session.device, | ||
data=transformed_input_np, | ||
axis_separators=axis_sep, | ||
mem_scope=working_scope, | ||
) | ||
|
||
output_arr = allocate_hexagon_array( | ||
hexagon_session.device, | ||
tensor_shape=transformed_expected_output_np.shape, | ||
dtype=transformed_expected_output_np.dtype, | ||
axis_separators=axis_sep, | ||
mem_scope=working_scope, | ||
) | ||
|
||
mod = hexagon_session.load_module(func) | ||
mod(input_arr, output_arr) | ||
|
||
n, c = input_np.shape | ||
output_np = output_arr.numpy().reshape(1, c // 512, 512) | ||
|
||
np.testing.assert_allclose(output_np, transformed_expected_output_np, rtol=1e-4, atol=1e-4) | ||
|
||
|
||
if __name__ == "__main__": | ||
|
||
sys.exit(pytest.main(sys.argv)) | ||
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. we can do this in a follow up:
|
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.
Follow up PR: move this to under test_hexagon/topi