forked from neo-ai/tvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Relay][Dynamic] Add Dynamic Resize Op (apache#6198)
* WIP * optionally remove output shape inference from topi * fix resize * add resize to dynamic_to_static pass add resize to dynamic_to_static pass * fix clang-format * fix bad rebase * add argument to dynamic resize doc string * fix i386 test * fix lint
- Loading branch information
Matthew Brookhart
authored and
Trevor Morris
committed
Sep 2, 2020
1 parent
d94eeb2
commit 69bb0dd
Showing
12 changed files
with
410 additions
and
27 deletions.
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 | ||
- **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) | ||
.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.