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

[ONNX] Wrap 'If' if it has multiple outputs #8385

Merged
merged 3 commits into from
Jul 7, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion python/tvm/relay/frontend/onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -2846,7 +2846,10 @@ def _impl_v1(cls, inputs, attr, params):
graph_scope._nodes.update({var.name_hint: var})

# Now we can construct the relay if statement and return.
return _expr.If(cond, then_expr, else_expr)
ret = _expr.If(cond, then_expr, else_expr)
if len(then_branch.output) > 1:
ret = _expr.TupleWrapper(ret, len(then_branch.output))
return ret


class NonMaxSuppression(OnnxOpConverter):
Expand Down
56 changes: 36 additions & 20 deletions tests/python/frontend/onnx/test_forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -4065,29 +4065,43 @@ def test_loop():
verify_tensor_loop()


def verify_if(cond_array):
def verify_if(cond_array, num_outputs):
# Given a bool scalar input cond.
# return constant tensor x if cond is True, otherwise return constant tensor y.
then_out = onnx.helper.make_tensor_value_info("then_out", onnx.TensorProto.FLOAT, [5])
else_out = onnx.helper.make_tensor_value_info("else_out", onnx.TensorProto.FLOAT, [5])

x = np.array([1, 2, 3, 4, 5]).astype(np.float32)
y = np.array([5, 4, 3, 2, 1]).astype(np.float32)
def append_constant_nodes(nodes, outputs, expected, name):
outputs.append(onnx.helper.make_tensor_value_info(name, onnx.TensorProto.FLOAT, [5]))

then_const_node = onnx.helper.make_node(
"Constant", inputs=[], outputs=["then_out"], value=numpy_helper.from_array(x)
)
expected.append(np.random.randn(5).astype("float32"))

else_const_node = onnx.helper.make_node(
"Constant", inputs=[], outputs=["else_out"], value=numpy_helper.from_array(y)
)
nodes.append(onnx.helper.make_node(
"Constant", inputs=[], outputs=[name], value=numpy_helper.from_array(expected[-1])
))

if_outputs = []
graph_outputs = []

then_nodes, then_outs, then_expected = [], [], []
else_nodes, else_outs, else_expected = [], [], []

for i in range(num_outputs):
append_constant_nodes(then_nodes, then_outs, then_expected, "then_out{}".format(i))
append_constant_nodes(else_nodes, else_outs, else_expected, "else_out{}".format(i))

then_body = onnx.helper.make_graph([then_const_node], "then_body", [], [then_out])
if_outputs.append("res{}".format(i))
graph_outputs.append(
onnx.helper.make_tensor_value_info("res{}".format(i), onnx.TensorProto.FLOAT, [5]),
)

else_body = onnx.helper.make_graph([else_const_node], "else_body", [], [else_out])
then_body = onnx.helper.make_graph(then_nodes, "then_body", [], then_outs)
else_body = onnx.helper.make_graph(else_nodes, "else_body", [], else_outs)

if_node = onnx.helper.make_node(
"If", inputs=["cond"], outputs=["res"], then_branch=then_body, else_branch=else_body
"If",
inputs=["cond"],
outputs=if_outputs,
then_branch=then_body,
else_branch=else_body
)

if_graph = onnx.helper.make_graph(
Expand All @@ -4096,31 +4110,33 @@ def verify_if(cond_array):
inputs=[
onnx.helper.make_tensor_value_info("cond", onnx.TensorProto.BOOL, []),
],
outputs=[
onnx.helper.make_tensor_value_info("res", onnx.TensorProto.FLOAT, [5]),
],
outputs=graph_outputs,
)

if_model = onnx.helper.make_model(if_graph)
if cond_array:
cond = np.array([1]).astype("bool")
else:
cond = np.array(1).astype("bool")
correct_out = x if cond else y
correct_out = then_expected if cond else else_expected

# TODO(jwfromm): Onnxruntime 1.0.0 is buggy with If statements. Replace this with
# verify_with_ort once we update versions.
for target, dev in tvm.testing.enabled_targets():
tvm_out = get_tvm_output_with_vm(if_model, [cond], target, dev, freeze_params=True)
if not isinstance(tvm_out, list):
tvm_out = [tvm_out]
for i in range(len(tvm_out)):
tvm.testing.assert_allclose(correct_out[i], tvm_out[i], rtol=1e-05, atol=1e-05)


@tvm.testing.uses_gpu
def test_if():
# Confirm that if works with cond as an array or scalar.
verify_if(cond_array=False)
verify_if(cond_array=True)
verify_if(cond_array=False, num_outputs=1)
verify_if(cond_array=False, num_outputs=2)
verify_if(cond_array=True, num_outputs=1)
verify_if(cond_array=True, num_outputs=2)


@tvm.testing.uses_gpu
Expand Down