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

[Relay][Frontend][Onnx] Add softplus operator conversion to Onnx. #7089

Merged
merged 1 commit into from
Dec 11, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 12 additions & 0 deletions python/tvm/relay/frontend/onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -2095,6 +2095,17 @@ def _impl_v11(cls, inputs, attr, params):
return result


class Softplus(OnnxOpConverter):
"""Operator converter for Softplus."""

@classmethod
def _impl_v1(cls, inputs, attr, params):
data = inputs[0]
data_dtype = infer_type(data).checked_type.dtype
data = _op.exp(data) + _expr.const(1, dtype=data_dtype)
return _op.log(data)


class Loop(OnnxOpConverter):
"""Operator converter for Loop"""

Expand Down Expand Up @@ -2371,6 +2382,7 @@ def _get_convert_map(opset):
"Sum": Sum.get_converter(opset),
"Mean": Mean.get_converter(opset),
"Clip": Clip.get_converter(opset),
"Softplus": Softplus.get_converter(opset),
# softmax default axis is different in onnx
"Softmax": Softmax.get_converter(opset),
"LogSoftmax": AttrCvt("log_softmax", {"axis": ("axis", 1)}),
Expand Down
29 changes: 29 additions & 0 deletions tests/python/frontend/onnx/test_forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -3983,6 +3983,34 @@ def verify_maxunpool(data, indices, kernel_shape, strides, output_shape=None, pa
verify_maxunpool(xT, xI, [2, 2], strides=[2, 2], pads=pads)


@tvm.testing.uses_gpu
def test_softplus():
def verify_softplus(indata):
node = helper.make_node(
"Softplus",
inputs=["X"],
outputs=["Y"],
)

graph = helper.make_graph(
[node],
"softplus_test",
inputs=[helper.make_tensor_value_info("X", TensorProto.FLOAT, list(indata.shape))],
outputs=[helper.make_tensor_value_info("Y", TensorProto.FLOAT, list(indata.shape))],
)

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

verify_with_ort_with_inputs(model, [indata], dtype="float32", use_vm=True, opset=11)

# Simple case with all signs.
input_data = np.array([[-1, 0, 1]], dtype=np.float32)
verify_softplus(input_data)
# More fancy case.
input_data = np.random.randn(1, 32, 32, 3).astype("float32")
verify_softplus(input_data)


if __name__ == "__main__":
test_flatten()
test_reshape()
Expand Down Expand Up @@ -4061,3 +4089,4 @@ def verify_maxunpool(data, indices, kernel_shape, strides, output_shape=None, pa
test_loop()
test_size()
test_maxunpool()
test_softplus()