forked from apache/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.
Add High-level Op Support (apache#5)
* high-level-op support * format * format * follow relay convention * format * fix
- Loading branch information
1 parent
304048c
commit 7425128
Showing
18 changed files
with
730 additions
and
107 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# 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 | ||
"""Neural network related operators.""" | ||
from .nn import * |
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("relax.op.nn", __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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# 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. | ||
|
||
from . import _make | ||
from ...expr import Expr | ||
|
||
|
||
def dense(lhs: Expr, rhs: Expr) -> Expr: | ||
return _make.dense(lhs, rhs) | ||
|
||
|
||
def conv2d( | ||
lhs: Expr, rhs: Expr, kernel_size, stride=(1, 1), padding=[0, 0], dilation=[1, 1] | ||
) -> Expr: | ||
return _make.conv2d(lhs, rhs, kernel_size, stride, padding, dilation) | ||
|
||
|
||
def relu(data: Expr) -> Expr: | ||
return _make.relu(data) | ||
|
||
|
||
def softmax(data: Expr) -> Expr: | ||
return _make.softmax(data) | ||
|
||
|
||
def flatten(data: Expr) -> Expr: | ||
return _make.flatten(data) | ||
|
||
|
||
def max_pool2d(data: Expr, kernel_size, stride=None, padding=(0, 0), dilation=(1, 1)) -> Expr: | ||
if stride is None: | ||
stride = kernel_size | ||
return _make.max_pool2d(data, kernel_size, stride, padding, dilation) | ||
|
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,50 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
#include "convolution.h" | ||
|
||
#include "../tensor/binary.h" | ||
namespace tvm { | ||
namespace relax { | ||
|
||
TVM_REGISTER_NODE_TYPE(Conv2dAttrs); | ||
|
||
RELAY_REGISTER_OP("relax.nn.conv2d") | ||
.set_num_inputs(2) | ||
.add_argument("e1", "Expr", "The input expression") | ||
.add_argument("e2", "Expr", "The input expression") | ||
.set_attrs_type<Conv2dAttrs>() | ||
.set_attr<FInferShape>("FInferShape", InferShapeConv2d) | ||
.set_attr<FInferType>("FInferType", InferTypeBinaryBroadcast); | ||
|
||
Expr MakeConv2d(Expr expr1, Expr expr2, Array<PrimExpr> kernel_size, Array<PrimExpr> stride, | ||
Array<PrimExpr> padding, Array<PrimExpr> dilation) { | ||
static const Op& op = Op::Get("relax.nn.conv2d"); | ||
auto attrs = make_object<Conv2dAttrs>(); | ||
attrs->kernel_size = kernel_size; | ||
attrs->stride = stride; | ||
attrs->padding = padding; | ||
attrs->dilation = dilation; | ||
return Call(op, {expr1, expr2}, Attrs(attrs), {}); | ||
} | ||
|
||
TVM_REGISTER_GLOBAL("relax.op.nn.conv2d").set_body_typed(MakeConv2d); | ||
|
||
} // namespace relax | ||
} // 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
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. | ||
*/ | ||
|
||
#ifndef TVM_RELAX_OP_NN_CONVOLUTION_H_ | ||
#define TVM_RELAX_OP_NN_CONVOLUTION_H_ | ||
|
||
#include <tvm/relax/expr.h> | ||
#include <tvm/relax/type.h> | ||
|
||
#include "../op_common.h" | ||
namespace tvm { | ||
namespace relax { | ||
|
||
Optional<Expr> InferShapeConv2d(const Call& call, DiagnosticContext diag_ctx) { | ||
if (call->args.size() != 2) { | ||
diag_ctx.EmitFatal(Diagnostic::Error(call->span) << "Conv2d op should have 2 arguments"); | ||
} | ||
Expr shape0 = call->args[0]->shape(); | ||
Expr shape1 = call->args[1]->shape(); | ||
auto* s0 = shape0.as<ShapeExprNode>(); | ||
auto* s1 = shape1.as<ShapeExprNode>(); | ||
auto* attrs = call->attrs.as<Conv2dAttrs>(); | ||
if (s0 && s1) { | ||
std::vector<PrimExpr> output_shape; | ||
size_t ndim0 = s0->values.size(); | ||
size_t ndim1 = s1->values.size(); | ||
if (ndim0 != 4 || ndim1 != 4) { | ||
LOG(INFO) << ndim0; | ||
LOG(INFO) << ndim1; | ||
diag_ctx.EmitFatal(Diagnostic::Error(call->span) | ||
<< "The 2 arguments of Conv2d must be 4D Tensors"); | ||
} | ||
// N | ||
output_shape.push_back(s0->values[0]); | ||
// C | ||
output_shape.push_back(s1->values[0]); | ||
// H | ||
output_shape.push_back((s0->values[2] + 2 * attrs->padding[0] - | ||
attrs->dilation[0] * (attrs->kernel_size[0] - 1) - 1) / | ||
attrs->stride[0] + | ||
1); | ||
// W | ||
output_shape.push_back((s0->values[3] + 2 * attrs->padding[1] - | ||
attrs->dilation[1] * (attrs->kernel_size[1] - 1) - 1) / | ||
attrs->stride[1] + | ||
1); | ||
return ShapeExpr(Array<PrimExpr>{output_shape.begin(), output_shape.end()}); | ||
} else { | ||
return NullOpt; | ||
} | ||
} | ||
|
||
} // namespace relax | ||
} // namespace tvm | ||
#endif |
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,56 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
#include "nn.h" | ||
|
||
namespace tvm { | ||
namespace relax { | ||
|
||
RELAY_REGISTER_OP("relax.nn.dense") | ||
.set_num_inputs(2) | ||
.add_argument("e1", "Expr", "The input expression") | ||
.add_argument("e2", "Expr", "The input expression") | ||
.set_attr<FInferShape>("FInferShape", InferShapeDense) | ||
.set_attr<FInferType>("FInferType", InferTypeDense); | ||
|
||
Expr MakeDense(Expr expr1, Expr expr2) { | ||
static const Op& op = Op::Get("relax.nn.dense"); | ||
return Call(op, {expr1, expr2}, {}, {}); | ||
} | ||
|
||
TVM_REGISTER_GLOBAL("relax.op.nn.dense").set_body_typed(MakeDense); | ||
|
||
RELAX_REGISTER_UNARY_OP("nn.softmax"); | ||
|
||
RELAX_REGISTER_UNARY_OP("nn.relu"); | ||
|
||
RELAY_REGISTER_OP("relax.nn.flatten") | ||
.set_num_inputs(1) | ||
.add_argument("data", "Tensor", "The input tensor") | ||
.set_attr<FInferShape>("FInferShape", InferShapeFlatten) | ||
.set_attr<FInferType>("FInferType", InferTypeFlatten); | ||
|
||
Expr MakeFlatten(Expr data) { | ||
static const Op& op = Op::Get("relax.nn.flatten"); | ||
return Call(op, {data}, {}, {}); | ||
} | ||
TVM_REGISTER_GLOBAL("relax.op.nn.flatten").set_body_typed(MakeFlatten); | ||
|
||
} // namespace relax | ||
} // namespace tvm |
Oops, something went wrong.