Skip to content

Commit

Permalink
Add missing check when deciding conv op and injective op are in the s…
Browse files Browse the repository at this point in the history
…ame group (apache#1622)
  • Loading branch information
masahi authored and tqchen committed Aug 20, 2018
1 parent 026c262 commit d6d97e8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions nnvm/src/compiler/graph_fuse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ nnvm::Graph GraphFindFusibleGroups(nnvm::Graph g) {
bool parent_out_ewise = false;
bool parent_injective = false;
for (const auto& e : inode.inputs) {
if (fuse_vec[e.node_id] != FuseRule::kFuseToMaster) continue;
TOpPattern pt = pattern_vec[e.node_id];
if (pt == kOutEWiseFusable) {
parent_out_ewise = true;
Expand Down
34 changes: 34 additions & 0 deletions nnvm/tests/python/compiler/test_op_fusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,39 @@ def test_injective_conv2d():
np.testing.assert_allclose(out.asnumpy(), c_np, rtol=1e-5)


def test_concatenate_conv2d():
ch = 3
size = 8
data = sym.Variable(name="data")
concat = sym.concatenate(data, data, axis=1)
conv = sym.conv2d(data=concat, kernel_size=(1,1), channels=ch*2, use_bias=False, name="conv")
net = sym.elemwise_add(concat, conv)

dtype="float32"
dshape = (1, ch, size, size)
kshape = (ch*2, ch*2, 1, 1)
oshape = (1, ch*2, size, size)
shape_dict = {"data": dshape}

for target, ctx in ctx_list():
graph, lib, _ = nnvm.compiler.build(net, target, shape_dict)
# data, conv weight, conv op, concat
assert graph.index.num_nodes == 4

data = tvm.nd.array(np.random.uniform(size=dshape).astype(dtype))
kernel = tvm.nd.array(np.random.uniform(size=kshape).astype(dtype))
m = graph_runtime.create(graph, lib, ctx)
m.run(data=data, conv_weight=kernel)
# get output
out = m.get_output(0, tvm.nd.empty(oshape, dtype))

concat = np.concatenate((data.asnumpy(), data.asnumpy()), axis=1)
conv = topi.testing.conv2d_nchw_python(
concat, kernel.asnumpy(), (1,1), 'SAME')
ref = concat + conv
np.testing.assert_allclose(out.asnumpy(), ref, rtol=1e-5)


def build_and_run(sym, params, data, out_shape, target, ctx, opt_level=2):
with nnvm.compiler.build_config(opt_level=opt_level):
graph, lib, params = nnvm.compiler.build(sym, target, shape={"data":data.shape}, params=params)
Expand Down Expand Up @@ -157,3 +190,4 @@ def get_sym(out_channel):
test_conv_ewise_injective()
test_fuse_conv2d_elu()
test_injective_conv2d()
test_concatenate_conv2d()

0 comments on commit d6d97e8

Please sign in to comment.