From d934cd60e15b2c58dcf5f5aa5361fda4f058597b Mon Sep 17 00:00:00 2001 From: taixiurong Date: Thu, 15 Sep 2022 04:05:44 +0000 Subject: [PATCH] support paddle elementwise_floordiv --- src/core/tests/frontend/paddle/op_fuzzy.cpp | 12 ++++ .../gen_scripts/generate_elementwise_ops.py | 60 +++++++++++++++++++ .../paddle/src/op/elementwise_ops.cpp | 14 +++++ src/frontends/paddle/src/op_table.cpp | 2 + 4 files changed, 88 insertions(+) diff --git a/src/core/tests/frontend/paddle/op_fuzzy.cpp b/src/core/tests/frontend/paddle/op_fuzzy.cpp index b8a226e2cc21a9..572892355e34ff 100644 --- a/src/core/tests/frontend/paddle/op_fuzzy.cpp +++ b/src/core/tests/frontend/paddle/op_fuzzy.cpp @@ -126,6 +126,18 @@ static const std::vector models{ std::string("elementwise_mul4"), std::string("elementwise_pow4"), std::string("elementwise_sub4"), + std::string("elementwise_floordiv_for_paddle1.8_int32_1"), + std::string("elementwise_floordiv_for_paddle1.8_int64_1"), + std::string("elementwise_floordiv_for_paddle1.8_int32_2"), + std::string("elementwise_floordiv_for_paddle1.8_int64_2"), + std::string("elementwise_floordiv_for_paddle1.8_int32_3"), + std::string("elementwise_floordiv_for_paddle1.8_int64_3"), + std::string("elementwise_floordiv_for_paddle1.8_int32_4"), + std::string("elementwise_floordiv_for_paddle1.8_int64_4"), + std::string("elementwise_floordiv_for_paddle2.x_int32_1"), + std::string("elementwise_floordiv_for_paddle2.x_int64_1"), + std::string("elementwise_floordiv_for_paddle2.x_int32_2"), + std::string("elementwise_floordiv_for_paddle2.x_int64_2"), std::string("embedding_0"), std::string("embedding_sparse"), std::string("embedding_none_weight"), diff --git a/src/core/tests/frontend/paddle/test_models/gen_scripts/generate_elementwise_ops.py b/src/core/tests/frontend/paddle/test_models/gen_scripts/generate_elementwise_ops.py index bbe45e21e82711..2b4bf5186f2b7f 100644 --- a/src/core/tests/frontend/paddle/test_models/gen_scripts/generate_elementwise_ops.py +++ b/src/core/tests/frontend/paddle/test_models/gen_scripts/generate_elementwise_ops.py @@ -162,6 +162,32 @@ def elementwise_pow(name : str, x, y, axis, in_dtype): return outs[0] + +def elementwise_floordiv(name : str, x, y, axis, in_dtype, paddle_ver="1.8"): + import paddle + paddle.enable_static() + + with paddle.static.program_guard(paddle.static.Program(), paddle.static.Program()): + node_x = paddle.static.data(name = 'x', shape = x.shape, dtype = in_dtype) + node_y = paddle.static.data(name = 'y', shape = y.shape, dtype = in_dtype) + if paddle_ver == "1.8": + out = paddle.fluid.layers.nn.elementwise_floordiv(node_x, node_y, axis=axis) + else: + out = paddle.floor_divide(node_x, node_y) + + cpu = paddle.static.cpu_places(1) + exe = paddle.static.Executor(cpu[0]) + + # startup program will call initializer to initialize the parameters. + exe.run(paddle.static.default_startup_program()) + outs = exe.run( + feed={'x': x, 'y': y}, + fetch_list=[out]) + saveModel(name, exe, feedkeys=['x', 'y'], fetchlist=[out], inputs=[x, y], outputs=[outs[0]], target_dir=sys.argv[1]) + + return outs[0] + + def elementwise_ops(name : str, data_x, data_y, axis, in_dtype): elementwise_add("elementwise_add" + name, data_x, data_y, axis, in_dtype) elementwise_sub("elementwise_sub" + name, data_x, data_y, axis, in_dtype) @@ -193,5 +219,39 @@ def main(): axis = 0 elementwise_ops("4", data_x, data_y, axis, in_dtype) + # test for elementwise_floordiv, support int and int64 + # paddle1.8 support axis = [0, x_last_dims] + # paddle2.x only support axis = -1 + floordiv_support_dtype = ['int64', 'int32'] + data_x = np.array([-2, 0, 4]) + data_y = np.array([1, 5, 2]) + axis = -1 + for dtype in floordiv_support_dtype: + elementwise_floordiv("elementwise_floordiv_for_paddle1.8_" + dtype + "_1", + data_x.astype(dtype), data_y.astype(dtype), axis, dtype) + elementwise_floordiv("elementwise_floordiv_for_paddle2.x_" + dtype + "_1", + data_x.astype(dtype), data_y.astype(dtype), axis, dtype, paddle_ver="2.x") + + data_x = np.random.randint(1, 10, [2, 5, 3, 4]) + data_y = np.random.randint(1, 5, [3, 4]) + for dtype in floordiv_support_dtype: + elementwise_floordiv("elementwise_floordiv_for_paddle1.8_" + dtype + "_2", + data_x.astype(dtype), data_y.astype(dtype), axis, dtype) + elementwise_floordiv("elementwise_floordiv_for_paddle2.x_" + dtype + "_2", + data_x.astype(dtype), data_y.astype(dtype), axis, dtype, paddle_ver="2.x") + + data_y = np.random.randint(1, 5, [5]) + axis = 1 + for dtype in floordiv_support_dtype: + elementwise_floordiv("elementwise_floordiv_for_paddle1.8_" + dtype + "_3", + data_x.astype(dtype), data_y.astype(dtype), axis, dtype) + + data_y = np.random.randint(1, 5, [2, 5, 3]) + axis = 0 + for dtype in floordiv_support_dtype: + elementwise_floordiv("elementwise_floordiv_for_paddle1.8_" + dtype + "_4", + data_x.astype(dtype), data_y.astype(dtype), axis, dtype) + + if __name__ == "__main__": main() diff --git a/src/frontends/paddle/src/op/elementwise_ops.cpp b/src/frontends/paddle/src/op/elementwise_ops.cpp index b833f1a19d4a1f..27da223d189809 100644 --- a/src/frontends/paddle/src/op/elementwise_ops.cpp +++ b/src/frontends/paddle/src/op/elementwise_ops.cpp @@ -46,6 +46,20 @@ NamedOutputs elementwise_greater_equal(const NodeContext& node_context) { return elementwise_ops(node_context); } +NamedOutputs elementwise_floordiv(const NodeContext& node_context) { + auto x = node_context.get_input("X"); + auto y = node_context.get_input("Y"); + auto axis = -1; + if (node_context.has_attribute("axis")) { + axis = node_context.get_attribute("axis"); + } + return node_context.default_single_output_mapping( + {std::make_shared(x, + y, + ov::op::AutoBroadcastSpec(ov::op::AutoBroadcastType::PDPD, axis))}, + {"Out"}); +} + } // namespace op } // namespace paddle } // namespace frontend diff --git a/src/frontends/paddle/src/op_table.cpp b/src/frontends/paddle/src/op_table.cpp index 97603c1ddcf91c..fedf235f1b5d6f 100644 --- a/src/frontends/paddle/src/op_table.cpp +++ b/src/frontends/paddle/src/op_table.cpp @@ -25,6 +25,7 @@ OP_CONVERTER(dropout); OP_CONVERTER(elementwise_add); OP_CONVERTER(elementwise_div); OP_CONVERTER(elementwise_equal); +OP_CONVERTER(elementwise_floordiv); OP_CONVERTER(elementwise_greater_equal); OP_CONVERTER(elementwise_max); OP_CONVERTER(elementwise_min); @@ -123,6 +124,7 @@ std::map get_supported_ops() { {"dropout", op::dropout}, {"elementwise_add", op::elementwise_add}, {"elementwise_div", op::elementwise_div}, + {"elementwise_floordiv", op::elementwise_floordiv}, {"elementwise_max", op::elementwise_max}, {"elementwise_min", op::elementwise_min}, {"elementwise_mul", op::elementwise_mul},