-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Relay][Dyn] Dynamic TopK Op (#6008)
* add dynamic topk op * add topk to dynamic_to_static pass * fix TF test * fix pylint
- Loading branch information
Matthew Brookhart
authored
Jul 10, 2020
1 parent
ba04c6a
commit 474d472
Showing
11 changed files
with
353 additions
and
59 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
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,71 @@ | ||
# 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. | ||
"Definition of classic algorithms" | ||
# pylint: disable=invalid-name,unused-argument | ||
from __future__ import absolute_import | ||
|
||
from tvm.te.hybrid import script | ||
from tvm.runtime import convert | ||
|
||
from .. import strategy | ||
from .. import op as _reg | ||
from ..op import OpPattern, register_pattern | ||
from ..op import register_strategy | ||
|
||
# topk | ||
register_strategy("dyn.topk", strategy.topk_strategy) | ||
register_pattern("dyn.topk", OpPattern.OPAQUE) | ||
|
||
@script | ||
def _topk_shape_func_input_data(data, k, axis): | ||
ndim = len(data.shape) | ||
val_out = output_tensor((ndim,), "int64") | ||
indices_out = output_tensor((ndim,), "int64") | ||
|
||
for i in const_range(ndim): | ||
if i != axis: | ||
val_out[i] = int64(data.shape[i]) | ||
indices_out[i] = int64(data.shape[i]) | ||
else: | ||
if k[0] < 1: | ||
val_out[i] = int64(data.shape[i]) | ||
indices_out[i] = int64(data.shape[i]) | ||
else: | ||
val_out[i] = int64(k[0]) | ||
indices_out[i] = int64(k[0]) | ||
return val_out, indices_out | ||
|
||
@_reg.register_shape_func("dyn.topk", True) | ||
def topk_shape_func(attrs, inputs, _): | ||
""" | ||
Shape func for topk. | ||
""" | ||
axis = attrs.axis | ||
if axis < 0: | ||
axis += len(inputs[0].shape) | ||
val_out, indices_out = \ | ||
_topk_shape_func_input_data(inputs[0], inputs[1], convert(axis)) | ||
|
||
ret_type = attrs.ret_type | ||
if ret_type == "both": | ||
ret = [val_out, indices_out] | ||
elif ret_type == "values": | ||
ret = [val_out] | ||
else: | ||
ret = [indices_out] | ||
|
||
return ret |
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
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,107 @@ | ||
/* | ||
* 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 topk.cc | ||
* \brief TopK operators | ||
*/ | ||
#include <tvm/relay/attrs/algorithm.h> | ||
#include <tvm/relay/op.h> | ||
#include <tvm/tir/op.h> | ||
|
||
namespace tvm { | ||
namespace relay { | ||
namespace dyn { | ||
|
||
bool TopKRel(const Array<Type>& types, int num_inputs, const Attrs& attrs, | ||
const TypeReporter& reporter) { | ||
// `types` contains: [data, k, result] | ||
const TopKAttrs* param = attrs.as<TopKAttrs>(); | ||
CHECK_EQ(types.size(), 3); | ||
const auto* data = types[0].as<TensorTypeNode>(); | ||
const auto* k = types[1].as<TensorTypeNode>(); | ||
if (data == nullptr) { | ||
CHECK(types[0].as<IncompleteTypeNode>()) | ||
<< "tile: expect input type to be TensorType but get " << types[0]; | ||
return false; | ||
} | ||
if (k == nullptr) { | ||
CHECK(types[1].as<IncompleteTypeNode>()) | ||
<< "tile: expect input type to be TensorType but get " << types[1]; | ||
return false; | ||
} | ||
CHECK(k->shape.size() <= 1) << "Parameter k must be a Scalar or a Tensor of shape (1, )"; | ||
if (k->shape.size() == 1) { | ||
const IntImmNode* k_shape = k->shape[0].as<IntImmNode>(); | ||
CHECK(k_shape) << "Parameter k must have static shape"; | ||
CHECK_EQ(k_shape->value, 1) << "Parameter k must be a Scalar or a Tensor of shape (1, )"; | ||
} | ||
int ndim = data->shape.size(); | ||
int axis = param->axis; | ||
if (axis < 0) { | ||
axis += ndim; | ||
} | ||
CHECK(axis >= 0 && axis < ndim); | ||
Array<IndexExpr> out_shape; | ||
for (int i = 0; i < ndim; ++i) { | ||
if (i != axis) { | ||
out_shape.push_back(data->shape[i]); | ||
} else { | ||
out_shape.push_back(Any()); | ||
} | ||
} | ||
auto values_ty = TensorType(out_shape, data->dtype); | ||
auto indices_ty = TensorType(out_shape, param->dtype); | ||
if (param->ret_type == "both") { | ||
reporter->Assign(types[2], TupleType({values_ty, indices_ty})); | ||
} else if (param->ret_type == "values") { | ||
reporter->Assign(types[2], values_ty); | ||
} else if (param->ret_type == "indices") { | ||
reporter->Assign(types[2], indices_ty); | ||
} else { | ||
LOG(FATAL) << "Unsupported ret type: " << param->ret_type; | ||
} | ||
return true; | ||
} | ||
|
||
Expr MakeTopK(Expr data, Expr k, int axis, String ret_type, bool is_ascend, DataType dtype) { | ||
auto attrs = make_object<TopKAttrs>(); | ||
attrs->axis = axis; | ||
attrs->ret_type = ret_type; | ||
attrs->is_ascend = is_ascend; | ||
attrs->dtype = dtype; | ||
static const Op& op = Op::Get("dyn.topk"); | ||
return Call(op, {data, k}, Attrs(attrs), {}); | ||
} | ||
|
||
TVM_REGISTER_GLOBAL("relay.op.dyn._make.topk").set_body_typed(MakeTopK); | ||
|
||
RELAY_REGISTER_OP("dyn.topk") | ||
.describe(R"doc(Get the top k elements in an input tensor along the given axis. | ||
)doc" TVM_ADD_FILELINE) | ||
.set_num_inputs(2) | ||
.set_attrs_type<TopKAttrs>() | ||
.add_argument("data", "Tensor", "Input data.") | ||
.add_argument("k", "Tensor", "Number of top elements.") | ||
.set_support_level(6) | ||
.add_type_rel("DynTopK", TopKRel); | ||
|
||
} // namespace dyn | ||
} // namespace relay | ||
} // namespace tvm |
Oops, something went wrong.