Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
a8c4e87
[HEXAGON] Initial clip operator for Hexagon
jcoplin-quic Jun 2, 2022
0e4fddc
Changes to utils and infra for pylint
jcoplin-quic Jun 2, 2022
1824408
Merge branch 'apache:main' into main
jcoplin-quic Jun 2, 2022
9fa3668
Remove unused import
jcoplin-quic Jun 2, 2022
adf2e47
Use tvm.testing.main()
jcoplin-quic Jun 2, 2022
7b34983
Address pylint error
jcoplin-quic Jun 2, 2022
ff1279f
Fix incorrect function call
jcoplin-quic Jun 3, 2022
b38b0e9
Changes to calls to transform_numpy
jcoplin-quic Jun 3, 2022
8f5cee4
Merge branch 'apache:main' into main
jcoplin-quic Jun 6, 2022
6c1f18d
Merge branch 'main' into jcoplin-quic/clip
jcoplin-quic Jun 6, 2022
a280957
Merge branch 'apache:main' into main
jcoplin-quic Jun 7, 2022
67c9c27
Merge branch 'main' of https://github.com/jcoplin-quic/tvm into main
jcoplin-quic Jun 7, 2022
dead6e8
Merge branch 'apache:main' into main
jcoplin-quic Jun 9, 2022
b01b2e7
Merge branch 'main' of https://github.com/jcoplin-quic/tvm into main
jcoplin-quic Jun 9, 2022
4221a86
Merge branch 'apache:main' into main
jcoplin-quic Jun 14, 2022
db87055
Merge branch 'main' of https://github.com/jcoplin-quic/tvm into main
jcoplin-quic Jun 14, 2022
984042f
Merge branch 'main' of https://github.com/apache/tvm into apache-main
jcoplin-quic Jun 16, 2022
36fc891
Merge branch 'apache-main' into main
jcoplin-quic Jun 16, 2022
8ac841a
Add newline at end of file
jcoplin-quic Jun 16, 2022
93a5828
Merge branch 'apache:main' into main
jcoplin-quic Jun 20, 2022
165169b
Merge branch 'main' into jcoplin-quic/clip
jcoplin-quic Jun 20, 2022
5657b4e
Add requires_hexagon and rename under topi
jcoplin-quic Jun 21, 2022
d0d173a
Merge branch 'main' of https://github.com/apache/tvm into apache-main
jcoplin-quic Jun 21, 2022
5c35e6b
Merge branch 'apache-main' into main
jcoplin-quic Jun 21, 2022
8faa207
Merge branch 'main' into jcoplin-quic/clip
jcoplin-quic Jun 21, 2022
25d89df
Whitespace fix and reduce input size
jcoplin-quic Jun 22, 2022
ed9a992
Merge branch 'apache:main' into main
jcoplin-quic Jun 22, 2022
c2f7099
Merge branch 'main' into jcoplin-quic/clip
jcoplin-quic Jun 22, 2022
f03351f
Remove te tensor arguments
jcoplin-quic Jun 23, 2022
6f96567
Merge branch 'apache:main' into main
jcoplin-quic Jun 23, 2022
2928571
Merge branch 'main' into jcoplin-quic/clip
jcoplin-quic Jun 23, 2022
635d2a3
Merge branch 'main' of https://github.com/apache/tvm into apache-main
jcoplin-quic Jun 27, 2022
a9757a2
Merge branch 'apache-main' into main
jcoplin-quic Jun 27, 2022
495c08a
Merge branch 'main' into jcoplin-quic/clip
jcoplin-quic Jun 27, 2022
9adaa7d
Correct call to tvm.build
jcoplin-quic Jun 27, 2022
15cbf67
Run black formatting
jcoplin-quic Jun 27, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions python/tvm/topi/hexagon/slice_ops/__init__.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
from .avg_pool2d import avg_pool2d_compute, avg_pool2d_STIR_schedule
from .add_subtract_multiply import *
from .softmax_slice import *
from .clip import *
66 changes: 66 additions & 0 deletions python/tvm/topi/hexagon/slice_ops/clip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# 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.

# pylint: disable=invalid-name

"""
Clip the elements in `A` between `A_min` and `A_max`.
"""

from tvm import te, tir, topi
from ..utils import get_layout_transform_fn


def clip_compute(A, A_min, A_max):
"""
Use topi clip implementation
"""
return topi.clip(A, A_min, A_max)


def clip_schedule(outs, ins, output_layout: str, input_layout: str):
"""
Hexagon clip schedule
"""
A = ins
M = outs

func = te.create_prim_func([A, M])

s = tir.Schedule(func)

block = s.get_block("compute")

input_transformed_layout = get_layout_transform_fn(input_layout)
s.transform_layout(block, buffer=("read", 0), index_map=input_transformed_layout)

output_transformed_layout = get_layout_transform_fn(output_layout)
s.transform_layout(block, buffer=("write", 0), index_map=output_transformed_layout)

n, h, w, c = s.get_loops(block)

ho, hi = s.split(h, [None, 8])
wo, wi = s.split(w, [None, 4])
co, ci = s.split(c, [None, 32])
wio, wii = s.split(wi, [None, 2])

s.reorder(n, ho, wo, co, hi, wio, ci, wii)

fused = s.fuse(ci, wii)
s.vectorize(fused)

return s
128 changes: 128 additions & 0 deletions tests/python/contrib/test_hexagon/topi/test_clip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# 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.

# pylint: disable=invalid-name

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, transform_numpy

input_layout = tvm.testing.parameter(
"nhwc-8h2w32c2w-2d",
)


@tvm.testing.fixture
def input_np(input_shape, dtype):
return np.random.random(input_shape).astype(dtype)


@tvm.testing.fixture
def transformed_expected_output_np(expected_output_np, output_layout):
return transform_numpy(expected_output_np, "nhwc", output_layout)


@tvm.testing.fixture
def transformed_input_np(input_np, input_layout):
return transform_numpy(input_np, "nhwc", input_layout)


class TestClipSlice:
input_shape, output_shape, A_min, A_max, output_layout, dtype = tvm.testing.parameters(
([1, 8, 4, 32], [1, 8, 4, 32], 0.1, 0.5, "nhwc-8h2w32c2w-2d", "float16")
)

@tvm.testing.fixture
def expected_output_np(self, input_np, A_min, A_max):
ref_np = np.clip(input_np, A_min, A_max)
return ref_np

@tvm.testing.requires_hexagon
def test_clip_slice(
self,
input_shape,
output_shape,
input_np,
input_layout,
output_layout,
dtype,
A_min,
A_max,
transformed_input_np,
transformed_expected_output_np,
hexagon_session,
):
# establish target and input placeholder
target_hexagon = tvm.target.hexagon("v69")
A = te.placeholder(input_shape, name="A", dtype=dtype)

# get the compute function and schedule
M = sl.clip_compute(A, A_min, A_max)

# Assume layout is nhwc-8h2w32c2w-2d
tir_schedule = sl.clip_schedule(M, A, output_layout, input_layout)

# build the function
with tvm.transform.PassContext(opt_level=3):
func = tvm.build(
tir_schedule.mod,
target=tvm.target.Target(target_hexagon, host=target_hexagon),
name="clip",
)

# allocate input and output nd arrays
axis_separators = [4]
input_arr = allocate_hexagon_array(
hexagon_session.device,
data=transformed_input_np,
dtype=dtype,
axis_separators=axis_separators,
mem_scope="global.vtcm",
)

output_arr = allocate_hexagon_array(
hexagon_session.device,
transformed_expected_output_np.shape,
dtype=dtype,
axis_separators=axis_separators,
mem_scope="global.vtcm",
)

# execute
mod = hexagon_session.load_module(func)
mod(input_arr, output_arr)

# convert output nd array to numpy array
output_np = output_arr.numpy()
b, h, w, c = output_shape
reshaped_output_np = np.reshape(output_np, [b, h // 8, w // 4, c // 32, 8, 2, 32, 2])

# test results
np.testing.assert_allclose(
reshaped_output_np, transformed_expected_output_np, rtol=1e-3, atol=1e-3
)


if __name__ == "__main__":
tvm.testing.main()