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

【PaddlePaddle Hackathon 3】No.102:为 OpenVINO 实现 Paddle 算子 elementwise_floordiv 转换 #12186

Closed
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
6 changes: 6 additions & 0 deletions src/core/tests/frontend/paddle/op_fuzzy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,26 @@ static const std::vector<std::string> models{
std::string("dropout_upscale_in_train"),
std::string("elementwise_add1"),
std::string("elementwise_div1"),
std::string("elementwise_floordiv1_int32"),
std::string("elementwise_floordiv1_int64"),
std::string("elementwise_max1"),
std::string("elementwise_min1"),
std::string("elementwise_mul1"),
std::string("elementwise_pow1"),
std::string("elementwise_sub1"),
std::string("elementwise_add2"),
std::string("elementwise_div2"),
std::string("elementwise_floordiv2_int32"),
std::string("elementwise_floordiv2_int64"),
std::string("elementwise_max2"),
std::string("elementwise_min2"),
std::string("elementwise_mul2"),
std::string("elementwise_pow2"),
std::string("elementwise_sub2"),
std::string("elementwise_add3"),
std::string("elementwise_div3"),
std::string("elementwise_floordiv3_int32"),
std::string("elementwise_floordiv3_int64"),
std::string("elementwise_max3"),
std::string("elementwise_min3"),
std::string("elementwise_mul3"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Copyright (C) 2018-2022 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

#
# elementwise_floordiv paddle model generator
#
import numpy as np
import sys
from save_model import saveModel

def elementwise_floordiv(name : str, x, y, in_dtype):
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)
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 main():
for in_dtype in ['int64', 'int32']:
data_x = np.array([2, 3, 4]).astype(in_dtype)
data_y = np.array([1, 5, 2]).astype(in_dtype)
elementwise_floordiv(f"elementwise_floordiv1_{in_dtype}", data_x, data_y, in_dtype)

# data_y's shape is the continuous subsequence of data_x's shape
data_x = np.random.randint(1, 5, size=[2, 3, 4, 5]).astype(in_dtype)
data_y = np.random.randint(-10, -1, size=[2, 3, 4, 5]).astype(in_dtype)
elementwise_floordiv(f"elementwise_floordiv2_{in_dtype}", data_x, data_y, in_dtype)

data_y = np.random.randint(1, 5, size=[5]).astype(in_dtype)
elementwise_floordiv(f"elementwise_floordiv3_{in_dtype}", data_x, data_y, in_dtype)

if __name__ == "__main__":
main()

10 changes: 10 additions & 0 deletions src/frontends/paddle/src/op/elementwise_ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ NamedOutputs elementwise_greater_equal(const NodeContext& node_context) {
return elementwise_ops<default_opset::GreaterEqual>(node_context);
}

NamedOutputs elementwise_floordiv(const NodeContext& node_context) {
auto x = node_context.get_input("X");
auto y = node_context.get_input("Y");

const auto axis = node_context.get_attribute<int>("axis", -1);
auto autob = ov::op::AutoBroadcastSpec(ov::op::AutoBroadcastType::PDPD, axis);

return node_context.default_single_output_mapping({std::make_shared<default_opset::Floor>(std::make_shared<default_opset::Divide>(x, y, false, autob))}, {"Out"});
}

} // namespace op
} // namespace paddle
} // namespace frontend
Expand Down
2 changes: 2 additions & 0 deletions src/frontends/paddle/src/op_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -122,6 +123,7 @@ std::map<std::string, CreatorFunction> 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},
Expand Down