Skip to content

Commit

Permalink
Fix prelu bug in onnx frontend. (apache#7208)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwfromm authored and Tushar Dey committed Jan 20, 2021
1 parent 7fa9b24 commit 5a7a9c8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
12 changes: 5 additions & 7 deletions python/tvm/relay/frontend/onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -822,13 +822,11 @@ class Prelu(OnnxOpConverter):
@classmethod
def _impl_v1(cls, inputs, attr, params):
assert len(inputs) == 2, "Prelu need 2 inputs, {} given".format(len(inputs))
input_channels = infer_shape(inputs[0])[1]
alpha_shape = infer_shape(inputs[1])
if len(alpha_shape) != 1:
alpha = _op.reshape(inputs[1], (-1,))
else:
alpha = inputs[1]
return _op.nn.prelu(inputs[0], _op.broadcast_to(alpha, [input_channels]))
input_shape = _op.shape_of(inputs[0])
alpha = _op.broadcast_to_like(inputs[1], inputs[0])
alpha = _op.reshape(alpha, [-1])
output = _op.nn.prelu(_op.reshape(inputs[0], [-1]), alpha, axis=0)
return _op.reshape(output, input_shape)


class Reciprocal(OnnxOpConverter):
Expand Down
5 changes: 4 additions & 1 deletion tests/python/frontend/onnx/test_forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -2037,12 +2037,15 @@ def verify_prelu(x_shape, a_shape):

model = helper.make_model(graph, producer_name="prelu_test")

verify_with_ort(model, [x_shape, a_shape], list(x_shape))
verify_with_ort(
model, [x_shape, a_shape], list(x_shape), use_vm=True, convert_to_static=True
)

verify_prelu([3, 4, 5, 6], [1, 4, 1, 1])
verify_prelu([1, 8, 5, 6], [1, 8, 1, 1])
verify_prelu([2, 12, 16, 16], [1, 12, 1, 1])
verify_prelu([2, 12, 16, 16], [1]) # Test alpha broadcasting.
verify_prelu([3, 1], [3, 1]) # Test non NCHW workload.


@tvm.testing.uses_gpu
Expand Down

0 comments on commit 5a7a9c8

Please sign in to comment.