Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a bug in nnvm to relay converter. #2756

Merged
merged 2 commits into from
Mar 14, 2019
Merged

Conversation

lixiaoquan
Copy link
Contributor

line 455 checks whether a child is expr.TupleWrapper, so _split() should
return TupleWrapper instead of TupleWrapper.tuple_value

455 if isinstance(child, expr.TupleWrapper):
456 children.append(child[i[1]])

  line 455 checks whether a child is expr.TupleWrapper, so _split() should
  return TupleWrapper instead of TupleWrapper.tuple_value

  455             if isinstance(child, expr.TupleWrapper):
  456                 children.append(child[i[1]])
@kazum
Copy link
Contributor

kazum commented Mar 9, 2019

@lixiaoquan, I did the change in #2734 and it looks wrong. Thanks for the catch.

To pass the CI error, I think we also need a fix to handle TupleWrapper in outputs of the graph.

@jroesch
Copy link
Member

jroesch commented Mar 11, 2019

Can we add a regression test?

These kind of regressions are usually due to lacking tests, and it would be good to guard against this error in the future.

@lixiaoquan
Copy link
Contributor Author

@kazum I refine it according to your suggestion. But there is another CI error which I can't reproduce locally. Could you check my patch? Thanks.

@@ -480,6 +480,7 @@ def to_relay(graph, shape_dict, dtype_dict, params):
"nnvm.to_relay: unsupported operator: {0}".format(op_name))

outputs = [relay_map[nid] for nid in output_ids]
outputs = [x if not isinstance(x, expr.TupleWrapper) else x.astuple() for x in outputs]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is not enough. Here is an example we also need to consider.

import nnvm   
from nnvm.to_relay import to_relay

x = nnvm.sym.Variable("x")
y = nnvm.sym.split(x, indices_or_sections=2)
z = y[1]
graph = nnvm.graph.create(z)
print(graph.ir())
# Graph(%x) {
#   %1 = split(%x, indices_or_sections='2')
#   ret %1.1
# }

func, _ = to_relay(graph, {}, {}, {})
print(func)
# fn (%x: ) {
#   %0 = split(%x, indices_or_sections=int64(2))
#   %0  <= should be %0.1
# }  

In the case of TupleWrapper, I think we should return x[index] instead of x.astuple() where index can be taken from json.loads(graph.json())['heads'].

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those cases are also handled now

@@ -31,6 +31,23 @@ def check_model(sym, shapes, dtypes, params):
relay_out = relay_rts.evaluate(relay_model)(*list(inputs.values()))
np.testing.assert_allclose(nnvm_out.asnumpy(), relay_out.asnumpy())


def test_split_concatenate():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this test should be in tests/python/frontend/nnvm_to_relay/test_forward.py.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modified

@kazum
Copy link
Contributor

kazum commented Mar 11, 2019

@lixiaoquan I added some comments. I think the current CI error is not related to your change. I'll look into it.

Copy link
Contributor

@kazum kazum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added final comments. I'll approve after they are addressed.

@@ -479,7 +480,14 @@ def to_relay(graph, shape_dict, dtype_dict, params):
raise Exception(
"nnvm.to_relay: unsupported operator: {0}".format(op_name))

outputs = [relay_map[nid] for nid in output_ids]
outputs = []
for i in output_ids:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for i in output_ids:
for nid, idx, _ in gidx.output_entries:

Then, we can remove heads and output_ids, and replace i[0] and i[1] with nid and gid for better readability.

verify_nnvm_to_relay(splited, params, data_shape=shape)
verify_nnvm_to_relay(concatenated, params, data_shape=shape)


if __name__ == '__main__':
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add test_forward_split_concatenate() here so that the test will be executed when we run the this script directly.

@lixiaoquan lixiaoquan force-pushed the to_relay branch 2 times, most recently from 64e1c8b to 7955a82 Compare March 13, 2019 02:36
Add a regression test guarding on original bug.
Copy link
Contributor

@kazum kazum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, thanks!

@tqchen tqchen merged commit 7182201 into apache:master Mar 14, 2019
@tqchen
Copy link
Member

tqchen commented Mar 14, 2019

Thanks @kazum @lixiaoquan @jroesch , this is now merged

wweic pushed a commit to wweic/tvm that referenced this pull request Mar 20, 2019
wweic pushed a commit to neo-ai/tvm that referenced this pull request Mar 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants