forked from apache/tvm
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[OP] Experimental assign op (apache#389)
- Loading branch information
Showing
6 changed files
with
196 additions
and
5 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
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,54 @@ | ||
/*! | ||
* Copyright (c) 2018 by Contributors | ||
* \file state_op.cc | ||
* \brief Experimental operators | ||
* Currently we only support assign | ||
*/ | ||
#include <nnvm/op.h> | ||
#include <nnvm/node.h> | ||
#include <nnvm/op_attr_types.h> | ||
#include <nnvm/compiler/op_attr_types.h> | ||
#include <nnvm/top/tensor.h> | ||
#include <topi/elemwise.h> | ||
#include "../op_common.h" | ||
#include "../elemwise_op_common.h" | ||
|
||
namespace nnvm { | ||
namespace top { | ||
|
||
using namespace tvm; | ||
using namespace nnvm::compiler; | ||
|
||
NNVM_REGISTER_OP(_assign) | ||
.describe(R"doc(Assign rhs to the lhs. | ||
lhs must be a Variable. | ||
This is an experimental operator. | ||
)doc" NNVM_ADD_FILELINE) | ||
.set_num_inputs(2) | ||
.set_num_outputs(1) | ||
.set_attr<FMutateInputs>( | ||
"FMutateInputs", [](const NodeAttrs& attrs) { | ||
return std::vector<uint32_t>{0}; | ||
}) | ||
.set_attr<FTVMCompute>( | ||
"FTVMCompute", [](const NodeAttrs& attrs, | ||
const Array<Tensor>& inputs, | ||
const Array<Tensor>& out_info) { | ||
// This implementation is needed for the special | ||
// logic handling assign in the compiler | ||
// It simply copies the result of rhs the output | ||
// The later decoration in compiler will change | ||
// the memory assignment of assign to tie | ||
// the lhs to the output. | ||
return Array<Tensor>{ topi::identity(inputs[1]) }; | ||
}) | ||
.set_attr<FInferShape>("FInferShape", SameShape) | ||
.set_attr<FInplaceOption>( | ||
"FInplaceOption", [](const NodeAttrs& attrs) { | ||
return std::vector<std::pair<int, int> >{{1, 0}}; | ||
}); | ||
|
||
} // namespace top | ||
} // namespace nnvm |
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,41 @@ | ||
import numpy as np | ||
|
||
import tvm | ||
from tvm.contrib import graph_runtime | ||
|
||
import nnvm.symbol as sym | ||
import nnvm.compiler | ||
from nnvm.testing.config import ctx_list | ||
|
||
|
||
def test_update(): | ||
w = sym.Variable("w") | ||
w2 = sym.Variable("w2") | ||
w = sym._assign(w, w + 1) | ||
w2 = sym._assign(w2, w + 1) | ||
|
||
dshape = (5, 3, 18, 18) | ||
shape_dict = {"w": dshape, "w2":dshape} | ||
dtype = "float32" | ||
|
||
def check(target, ctx): | ||
graph, lib, _ = nnvm.compiler.build(w2, target, shape_dict) | ||
|
||
m = graph_runtime.create(graph, lib, ctx) | ||
|
||
data = tvm.nd.array(np.random.uniform(size=dshape).astype(dtype)) | ||
m.set_input("w", data) | ||
m.run() | ||
out = m.get_input("w2", tvm.nd.empty(dshape, dtype)) | ||
np.testing.assert_allclose(out.asnumpy(), data.asnumpy() + 2, rtol=1e-5) | ||
|
||
m.run() | ||
out = m.get_input("w2", tvm.nd.empty(dshape, dtype)) | ||
np.testing.assert_allclose(out.asnumpy(), data.asnumpy() + 3, rtol=1e-5) | ||
|
||
for target, ctx in ctx_list(): | ||
check(target, ctx) | ||
|
||
|
||
if __name__ == "__main__": | ||
test_update() |