Skip to content

Commit

Permalink
[Relax] [ONNX] Add support for HardSwish (#17088)
Browse files Browse the repository at this point in the history
add hardswish support to onnx frontend
  • Loading branch information
mshr-h authored Jun 14, 2024
1 parent d3011ab commit 4ecae58
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
17 changes: 17 additions & 0 deletions python/tvm/relax/frontend/onnx/onnx_frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1918,6 +1918,22 @@ def _impl_v1(cls, bb, inputs, attr, params):
) + relax.op.nn.relu(inputs[0])


class HardSwish(OnnxOpConverter):
"""Converts an onnx HardSwish node into an equivalent Relax expression."""

@classmethod
def _impl_v14(cls, bb, inputs, attr, params):
x = inputs[0]
dtype = x.struct_info.dtype
return relax.op.multiply(
x,
relax.op.divide(
relax.op.clip(relax.op.add(x, relax.const(3, dtype)), 0, 6),
relax.expr.const(6, dtype),
),
)


def _get_convert_map():
return {
"MatMul": MatMul,
Expand Down Expand Up @@ -1998,6 +2014,7 @@ def _get_convert_map():
"Reciprocal": Reciprocal,
"OneHot": OneHot,
"Elu": Elu,
"HardSwish": HardSwish,
}


Expand Down
4 changes: 4 additions & 0 deletions tests/python/relax/test_frontend_onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,10 @@ def test_elu():
verify_unary("Elu", [32, 32])


def test_hardswish():
verify_unary("HardSwish", [32, 32])


def test_conv():
def _verify_conv(input_shape, weight_shape, output_shape):
bias_shape = [output_shape[1]]
Expand Down

0 comments on commit 4ecae58

Please sign in to comment.