From 52fba2e9f91fe244d9c79978d88f66ddda1ea10d Mon Sep 17 00:00:00 2001 From: Gaoyuyang Date: Fri, 17 Feb 2023 07:30:03 +0000 Subject: [PATCH 1/3] add one_hot_v2 for paddle frontend --- python/tvm/relay/frontend/paddlepaddle.py | 16 ++++++++++++++++ .../python/frontend/paddlepaddle/test_forward.py | 11 +++++++++++ 2 files changed, 27 insertions(+) mode change 100644 => 100755 python/tvm/relay/frontend/paddlepaddle.py mode change 100644 => 100755 tests/python/frontend/paddlepaddle/test_forward.py diff --git a/python/tvm/relay/frontend/paddlepaddle.py b/python/tvm/relay/frontend/paddlepaddle.py old mode 100644 new mode 100755 index 4927a362522e..6a76704f2b91 --- a/python/tvm/relay/frontend/paddlepaddle.py +++ b/python/tvm/relay/frontend/paddlepaddle.py @@ -1141,6 +1141,21 @@ def convert_mv(g, op, block): g.add_node(op.output("Out")[0], out) +def convert_one_hot_v2(g, op, block): + """Operator converter for one_hot_v2.""" + + x = g.get_node(op.input("X")[0]) + depth = op.attr("depth") + dtype = op.attr("dtype") + dtype = _convert_dtype_value(dtype) + ndim = len(infer_shape(x)) + on_value = _op.const(1) + off_value = _op.const(0) + axis = ndim + out = _op.one_hot(x, on_value, off_value, depth, axis,dtype) + g.add_node(op.output("Out")[0], out) + + def convert_padding(g, op, block): """Operator converter for padding.""" @@ -2136,6 +2151,7 @@ def convert_unsqueeze(g, op, block): "mv": convert_mv, "nearest_interp_v2": convert_interpolate, "not_equal": convert_elementwise_op, + "one_hot_v2": convert_one_hot_v2, "pad1d": convert_padding, "pad2d": convert_padding, "pad3d": convert_padding, diff --git a/tests/python/frontend/paddlepaddle/test_forward.py b/tests/python/frontend/paddlepaddle/test_forward.py old mode 100644 new mode 100755 index de6ea1dcf1da..2651c78ae2f1 --- a/tests/python/frontend/paddlepaddle/test_forward.py +++ b/tests/python/frontend/paddlepaddle/test_forward.py @@ -1723,5 +1723,16 @@ def topk6(inputs): verify_model(topk6, input_data=input_data) +@tvm.testing.uses_gpu +def test_forward_one_hot_v2(): + @paddle.jit.to_static + def one_hot_v2_1(inputs): + return nn.functional.one_hot(inputs, num_classes=4) + + + input_data = paddle.to_tensor([1, 1, 3, 0], dtype=paddle.int32) + verify_model(one_hot_v2_1, input_data=input_data) + + if __name__ == "__main__": tvm.testing.main() From 43988f6a4d638328de1a544d04c515d1898bbac0 Mon Sep 17 00:00:00 2001 From: Gaoyuyang Date: Fri, 17 Feb 2023 07:59:57 +0000 Subject: [PATCH 2/3] format py code --- python/tvm/relay/frontend/paddlepaddle.py | 6 +++--- tests/python/frontend/paddlepaddle/test_forward.py | 1 - 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/python/tvm/relay/frontend/paddlepaddle.py b/python/tvm/relay/frontend/paddlepaddle.py index 6a76704f2b91..eb5d501d1da3 100755 --- a/python/tvm/relay/frontend/paddlepaddle.py +++ b/python/tvm/relay/frontend/paddlepaddle.py @@ -1143,7 +1143,7 @@ def convert_mv(g, op, block): def convert_one_hot_v2(g, op, block): """Operator converter for one_hot_v2.""" - + x = g.get_node(op.input("X")[0]) depth = op.attr("depth") dtype = op.attr("dtype") @@ -1152,7 +1152,7 @@ def convert_one_hot_v2(g, op, block): on_value = _op.const(1) off_value = _op.const(0) axis = ndim - out = _op.one_hot(x, on_value, off_value, depth, axis,dtype) + out = _op.one_hot(x, on_value, off_value, depth, axis, dtype) g.add_node(op.output("Out")[0], out) @@ -2151,7 +2151,7 @@ def convert_unsqueeze(g, op, block): "mv": convert_mv, "nearest_interp_v2": convert_interpolate, "not_equal": convert_elementwise_op, - "one_hot_v2": convert_one_hot_v2, + "one_hot_v2": convert_one_hot_v2, "pad1d": convert_padding, "pad2d": convert_padding, "pad3d": convert_padding, diff --git a/tests/python/frontend/paddlepaddle/test_forward.py b/tests/python/frontend/paddlepaddle/test_forward.py index 2651c78ae2f1..69f6aaa88bef 100755 --- a/tests/python/frontend/paddlepaddle/test_forward.py +++ b/tests/python/frontend/paddlepaddle/test_forward.py @@ -1729,7 +1729,6 @@ def test_forward_one_hot_v2(): def one_hot_v2_1(inputs): return nn.functional.one_hot(inputs, num_classes=4) - input_data = paddle.to_tensor([1, 1, 3, 0], dtype=paddle.int32) verify_model(one_hot_v2_1, input_data=input_data) From 8b1596ad5960fe018af4b3f69100ba5a9b99278a Mon Sep 17 00:00:00 2001 From: Gaoyuyang Date: Fri, 17 Feb 2023 09:48:26 +0000 Subject: [PATCH 3/3] Add norm for paddlepaddle frontend --- python/tvm/relay/frontend/paddlepaddle.py | 12 ++++++++++++ .../python/frontend/paddlepaddle/test_forward.py | 16 ++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/python/tvm/relay/frontend/paddlepaddle.py b/python/tvm/relay/frontend/paddlepaddle.py index eb5d501d1da3..0842cd55dae2 100755 --- a/python/tvm/relay/frontend/paddlepaddle.py +++ b/python/tvm/relay/frontend/paddlepaddle.py @@ -1141,6 +1141,17 @@ def convert_mv(g, op, block): g.add_node(op.output("Out")[0], out) +def convert_norm(g, op, block): + """Operator converter for norm.""" + + x = g.get_node(op.input("X")[0]) + axis = op.attr("axis") + axis_l = [axis] + epsilon = op.attr("epsilon") + out = _op.nn.l2_normalize(x, epsilon, axis_l) + g.add_node(op.output("Out")[0], out) + + def convert_one_hot_v2(g, op, block): """Operator converter for one_hot_v2.""" @@ -2150,6 +2161,7 @@ def convert_unsqueeze(g, op, block): "mul": convert_mul, "mv": convert_mv, "nearest_interp_v2": convert_interpolate, + "norm": convert_norm, "not_equal": convert_elementwise_op, "one_hot_v2": convert_one_hot_v2, "pad1d": convert_padding, diff --git a/tests/python/frontend/paddlepaddle/test_forward.py b/tests/python/frontend/paddlepaddle/test_forward.py index 69f6aaa88bef..cd2c0be7ef36 100755 --- a/tests/python/frontend/paddlepaddle/test_forward.py +++ b/tests/python/frontend/paddlepaddle/test_forward.py @@ -1733,5 +1733,21 @@ def one_hot_v2_1(inputs): verify_model(one_hot_v2_1, input_data=input_data) +@tvm.testing.uses_gpu +def test_forward_norm(): + @paddle.jit.to_static + def norm_1(inputs): + return paddle.fluid.layers.l2_normalize(inputs, -1, 1e-12) + + def norm_2(inputs): + return paddle.fluid.layers.l2_normalize(inputs, 1, 1e-12) + + input_data = paddle.to_tensor( + [[[1, 2], [3, 1], [4, 5]], [[3, 1], [3, 5], [2, 4]]], dtype=paddle.float32 + ) + verify_model(norm_1, input_data=input_data) + verify_model(norm_2, input_data=input_data) + + if __name__ == "__main__": tvm.testing.main()