-
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] Add schedule and test for conv2d_transpose_nchw #11175
Merged
csullivan
merged 8 commits into
apache:main
from
farshidsp:farshidsp/hexagon/add_schedule
May 3, 2022
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
441d485
Add test for registered scheduales - depthwise_conv2d
farshidsp 59dd7e5
added more test to depthwise_conv2
farshidsp ad15633
adding new line at the end of the file
farshidsp ab8b529
reformatted the file
farshidsp e5b5670
resolve comments
farshidsp e8892c7
Merge branch 'main' into farshidsp/hexagon/add_schedule
farshidsp 2cde8db
add schedule and tests for conv2d_transpose_nchw
farshidsp 1f9f97e
registering conv2d_transpose strategy and clean up test
farshidsp 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
177 changes: 177 additions & 0 deletions
177
tests/python/contrib/test_hexagon/topi/test_conv2d_transpose.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,177 @@ | ||
# 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. | ||
"""Test code for transposed convolution.""" | ||
import numpy as np | ||
import tvm | ||
import tvm.testing | ||
from tvm import te | ||
from tvm import topi | ||
import tvm.topi.testing | ||
from tvm.contrib.pickle_memoize import memoize | ||
from tvm.topi.utils import get_const_tuple | ||
from ..conftest import requires_hexagon_toolchain | ||
|
||
|
||
# TODO Should add kernal to tvm.testing.fixture | ||
|
||
random_seed = tvm.testing.parameter(0) | ||
|
||
|
||
@tvm.testing.fixture | ||
def shift_shape(batch): | ||
return batch | ||
|
||
|
||
@tvm.testing.fixture | ||
def shift_shape(in_channel): | ||
return in_channel | ||
|
||
|
||
@tvm.testing.fixture | ||
def shift_shape(in_size): | ||
return in_size | ||
|
||
|
||
@tvm.testing.fixture | ||
def shift_shape(num_filter): | ||
return num_filter | ||
|
||
|
||
@tvm.testing.fixture | ||
def shift_shape(stride): | ||
return stride | ||
|
||
|
||
@tvm.testing.fixture | ||
def shift_shape(padding): | ||
return padding | ||
|
||
|
||
@tvm.testing.fixture | ||
def shift_shape(output_padding): | ||
return output_padding | ||
|
||
|
||
class BaseConv2DTransposeTests: | ||
@requires_hexagon_toolchain | ||
def test_conv2d( | ||
self, | ||
hexagon_session, | ||
batch, | ||
in_channel, | ||
in_size, | ||
num_filter, | ||
stride, | ||
padding, | ||
output_padding, | ||
random_seed, | ||
): | ||
|
||
target_hexagon = tvm.target.hexagon("v68") | ||
|
||
in_height, in_width = in_size | ||
kernel_height, kernel_width = (1, 1) | ||
stride_height, stride_width = stride | ||
pad_top, pad_left, pad_bottom, pad_right = padding | ||
|
||
A = te.placeholder((batch, in_channel, in_height, in_width), name="A") | ||
W = te.placeholder((in_channel, num_filter, kernel_height, kernel_width), name="W") | ||
|
||
a_shape = get_const_tuple(A.shape) | ||
w_shape = get_const_tuple(W.shape) | ||
dtype = A.dtype | ||
|
||
def get_ref_data(): | ||
|
||
np.random.seed(random_seed) | ||
a_np = np.random.uniform(size=a_shape).astype(dtype) | ||
w_np = np.random.uniform(size=w_shape).astype(dtype) | ||
b_np = tvm.topi.testing.conv2d_transpose_nchw_python( | ||
a_np, w_np, stride, padding, output_padding | ||
) | ||
c_np = np.maximum(b_np, 0) | ||
return a_np, w_np, b_np, c_np | ||
|
||
a_np, w_np, b_np, c_np = get_ref_data() | ||
|
||
fcompute_args = ( | ||
A, | ||
W, | ||
[stride_height, stride_width], | ||
[pad_top, pad_left, pad_bottom, pad_right], | ||
A.dtype, | ||
output_padding, | ||
) | ||
|
||
with tvm.target.Target(target_hexagon): | ||
fcompute = topi.nn.conv2d_transpose_nchw | ||
fschedule = topi.hexagon.schedule_conv2d_transpose_nchw | ||
B = fcompute(*fcompute_args) | ||
C = topi.nn.relu(B) | ||
s1 = fschedule([B]) | ||
s2 = fschedule([C]) | ||
|
||
dev = hexagon_session.device | ||
|
||
a = tvm.nd.array(a_np, dev) | ||
w = tvm.nd.array(w_np, dev) | ||
b = tvm.nd.array(np.zeros(get_const_tuple(B.shape), dtype=B.dtype), dev) | ||
c = tvm.nd.array(np.zeros(get_const_tuple(C.shape), dtype=C.dtype), dev) | ||
|
||
func1 = tvm.build(s1, [A, W, B], tvm.target.Target(target_hexagon, host=target_hexagon)) | ||
func2 = tvm.build(s2, [A, W, C], tvm.target.Target(target_hexagon, host=target_hexagon)) | ||
|
||
mod1 = hexagon_session.load_module(func1) | ||
mod2 = hexagon_session.load_module(func2) | ||
|
||
mod1(a, w, b) | ||
mod2(a, w, c) | ||
tvm.testing.assert_allclose(b.numpy(), b_np, rtol=1e-5) | ||
tvm.testing.assert_allclose(c.numpy(), c_np, rtol=1e-5) | ||
|
||
|
||
class TestConv2DTranspose_1(BaseConv2DTransposeTests): | ||
|
||
batch = tvm.testing.parameter(1) | ||
in_channel = tvm.testing.parameter(3, 8) | ||
in_size = tvm.testing.parameter((224, 224)) | ||
num_filter = tvm.testing.parameter(1, 8) | ||
stride = tvm.testing.parameter((1, 1)) | ||
padding = tvm.testing.parameter((0, 0, 0, 0)) | ||
output_padding = tvm.testing.parameter((0, 0)) | ||
|
||
|
||
class TestConv2DTranspose_2(BaseConv2DTransposeTests): | ||
|
||
batch = tvm.testing.parameter(1) | ||
in_channel = tvm.testing.parameter(512) | ||
in_size = tvm.testing.parameter((8, 1)) | ||
num_filter = tvm.testing.parameter(128) | ||
stride = tvm.testing.parameter((31, 1)) | ||
padding = tvm.testing.parameter((0, 0, 0, 0)) | ||
output_padding = tvm.testing.parameter((0, 0)) | ||
|
||
|
||
class TestConv2DTranspose_3(BaseConv2DTransposeTests): | ||
|
||
batch = tvm.testing.parameter(1) | ||
in_channel = tvm.testing.parameter(32) | ||
in_size = tvm.testing.parameter((8192, 1)) | ||
num_filter = tvm.testing.parameter(1) | ||
stride = tvm.testing.parameter((1, 1)) | ||
padding = tvm.testing.parameter((0, 0, 0, 0)) | ||
output_padding = tvm.testing.parameter((0, 0)) |
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.
can you combine these three classes using this patter:
batch, in_channel, ... = tvm.testing.parameters((1, (3, 8), ...)
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.
also rename the class to
TestConv2DTranspose
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.
Thanks for the suggestion. Have addressed both in the next commit.