-
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
[Relay][Dynamic] Add Dynamic Resize Op #6198
Merged
kevinthesun
merged 9 commits into
apache:master
from
mbrookhart:mbrookhart/dynamic_resize
Aug 8, 2020
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4a96b6f
WIP
3fd05f1
optionally remove output shape inference from topi
7950c8a
fix resize
98f47b8
add resize to dynamic_to_static pass
dd3c9d4
fix clang-format
d3535a7
fix bad rebase
03dea6e
add argument to dynamic resize doc string
31b5a5d
fix i386 test
1cea338
fix lint
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,5 @@ | |
from . import _algorithm | ||
from . import _transform | ||
from . import _tensor | ||
|
||
from .import image |
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,20 @@ | ||
# 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=wildcard-import, redefined-builtin, invalid-name | ||
"""The Relay namespace containing dynamic image ops.""" | ||
|
||
from . import _image |
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. | ||
#pylint: disable=invalid-name, unused-argument | ||
"""Backend compiler related feature registration""" | ||
from __future__ import absolute_import | ||
|
||
import tvm.topi | ||
from tvm.runtime import convert | ||
from tvm.te.hybrid import script | ||
from tvm.topi.util import nchw_pack_layout, nchw_xc_layout | ||
from ... import op as reg | ||
|
||
|
||
# resize | ||
@reg.register_compute("dyn.image.resize") | ||
def compute_resize(attrs, inputs, out_type): | ||
layout = attrs.layout | ||
method = attrs.method | ||
coord_trans = attrs.coordinate_transformation_mode | ||
out_dtype = attrs.out_dtype | ||
return [ | ||
tvm.topi.image.resize(inputs[0], inputs[1], layout, method, coord_trans, out_dtype, | ||
out_type.shape) | ||
] | ||
|
||
|
||
reg.register_injective_schedule("dyn.image.resize") | ||
|
||
|
||
@script | ||
def _NCHW_resize_shape_func(dshape, size, ndim): | ||
out = output_tensor((ndim, ), "int64") | ||
for i in const_range(ndim): | ||
out[i] = int64(dshape[i]) | ||
out[2] = int64(size[0]) | ||
out[3] = int64(size[1]) | ||
return out | ||
|
||
|
||
@script | ||
def _NHWC_resize_shape_func(dshape, size, ndim): | ||
out = output_tensor((ndim, ), "int64") | ||
for i in const_range(ndim): | ||
out[i] = int64(dshape[i]) | ||
out[1] = int64(size[0]) | ||
out[2] = int64(size[1]) | ||
return out | ||
|
||
|
||
@reg.register_shape_func("dyn.image.resize", True) | ||
def resize_shape_func(attrs, inputs, _): | ||
""" | ||
Shape function for dyn.image.resize op. | ||
""" | ||
layout = attrs.layout | ||
if layout == 'NHWC': | ||
out = [_NHWC_resize_shape_func(inputs[0].shape, inputs[1], convert(len(inputs[0].shape)))] | ||
elif (layout == 'NCHW') or nchw_pack_layout(layout) or nchw_xc_layout(layout): | ||
out = [_NCHW_resize_shape_func(inputs[0].shape, inputs[1], convert(len(inputs[0].shape)))] | ||
else: | ||
raise ValueError("Resize Unsupported Layout", layout) | ||
return out |
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,20 @@ | ||
# 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. | ||
"""Constructor APIs""" | ||
import tvm._ffi | ||
|
||
tvm._ffi._init_api("relay.op.dyn.image._make", __name__) |
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
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,109 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/*! | ||
* \file resize.cc | ||
* \brief Image resize operators | ||
*/ | ||
#include <tvm/relay/attrs/image.h> | ||
#include <tvm/relay/op.h> | ||
#include <tvm/tir/data_layout.h> | ||
|
||
#include "../../op_common.h" | ||
|
||
namespace tvm { | ||
namespace relay { | ||
namespace dyn { | ||
|
||
TVM_REGISTER_NODE_TYPE(ResizeAttrs); | ||
|
||
bool ResizeRel(const Array<Type>& types, int num_inputs, const Attrs& attrs, | ||
const TypeReporter& reporter) { | ||
// {data, size, out} | ||
CHECK_EQ(types.size(), 3); | ||
const auto* data = types[0].as<TensorTypeNode>(); | ||
if (data == nullptr) return false; | ||
|
||
static const Layout kNCHW("NCHW"); | ||
|
||
const ResizeAttrs* param = attrs.as<ResizeAttrs>(); | ||
CHECK(param != nullptr); | ||
const Layout in_layout(param->layout); | ||
auto layout_converter = tir::BijectiveLayout(in_layout, kNCHW); | ||
CHECK(layout_converter.defined()) | ||
<< "Resize only support input layouts that are convertible from NCHW." | ||
<< " But got " << in_layout; | ||
|
||
auto oshape = layout_converter.ForwardShape(data->shape); | ||
oshape.Set(2, Any()); | ||
oshape.Set(3, Any()); | ||
|
||
DataType out_dtype = param->out_dtype; | ||
if (out_dtype.bits() == 0) { | ||
out_dtype = data->dtype; | ||
} | ||
|
||
// assign output type | ||
reporter->Assign(types[2], TensorType(layout_converter.BackwardShape(oshape), out_dtype)); | ||
return true; | ||
} | ||
|
||
// Positional relay function to create image operator | ||
// used by frontend FFI. | ||
Expr MakeResize(Expr data, Expr size, String layout, String method, | ||
String coordinate_transformation_mode, DataType out_dtype) { | ||
auto attrs = make_object<ResizeAttrs>(); | ||
attrs->layout = std::move(layout); | ||
attrs->method = std::move(method); | ||
attrs->coordinate_transformation_mode = coordinate_transformation_mode; | ||
attrs->out_dtype = out_dtype; | ||
static const Op& op = Op::Get("dyn.image.resize"); | ||
return Call(op, {data, size}, Attrs(attrs), {}); | ||
} | ||
|
||
TVM_REGISTER_GLOBAL("relay.op.dyn.image._make.resize").set_body_typed(MakeResize); | ||
|
||
RELAY_REGISTER_OP("dyn.image.resize") | ||
.describe(R"code(Perform resize to input array with nearest neighbour or bilinear interpolation. | ||
|
||
- **data**: data is 4D array of shape | ||
(batch_size, channels, in_height, in_width) for NCHW | ||
(batch_size, in_height, in_width, channels) for NHWC | ||
|
||
mbrookhart marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- **size**: data is 2D array of shape (2,) with values | ||
(new_height, new_width) | ||
|
||
- **out**: Output is 4D array of shape | ||
for layout NCHW | ||
(batch_size, channels, size[0], size[1]) | ||
|
||
for layout NHWC | ||
(batch_size, size[0], size[1], channels) | ||
)code" TVM_ADD_FILELINE) | ||
.set_attrs_type<ResizeAttrs>() | ||
.set_num_inputs(2) | ||
.add_argument("data", "Tensor", "The input tensor.") | ||
.add_argument("size", "Tensor", "The output size tensor.") | ||
.set_support_level(5) | ||
.add_type_rel("DynResize", ResizeRel) | ||
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. Did you decide to leave out the FInferCorrectLayout pass? 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. No, it isn't present in the static version either. |
||
.set_attr<TOpPattern>("TOpPattern", kInjective); | ||
|
||
} // namespace dyn | ||
} // namespace relay | ||
} // namespace tvm |
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
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
Oops, something went wrong.
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.
Remove extra white space
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 ran Yapf on it, which inserted this line. Looking up Pep8 to be sure, we need two lines between top level functions in a module: https://www.python.org/dev/peps/pep-0008/#blank-lines