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】support paddle elementwise_floordiv #13059

Merged
merged 2 commits into from
Oct 20, 2022
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
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 @@ -131,6 +131,12 @@ static const std::vector<std::string> models{
std::string("elementwise_mul4"),
std::string("elementwise_pow4"),
std::string("elementwise_sub4"),
std::string("elementwise_floordiv_int32_1"),
std::string("elementwise_floordiv_int32_2"),
std::string("elementwise_floordiv_int32_3"),
std::string("elementwise_floordiv_int64_1"),
std::string("elementwise_floordiv_int64_2"),
std::string("elementwise_floordiv_int64_3"),
ceciliapeng2011 marked this conversation as resolved.
Show resolved Hide resolved
std::string("embedding_0"),
std::string("embedding_sparse"),
std::string("embedding_none_weight"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,34 @@ def elementwise_pow(name : str, x, y, axis, in_dtype):

return outs[0]


def elementwise_floordiv(name : str, x, y, axis, 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)
if paddle.__version__ == "1.8":
out = paddle.fluid.layers.nn.elementwise_floordiv(node_x, node_y, axis=axis)
else:
if axis != -1:
pass
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)
Expand Down Expand Up @@ -221,5 +249,29 @@ 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
ceciliapeng2011 marked this conversation as resolved.
Show resolved Hide resolved
floordiv_support_dtype = ['int64', 'int32']
data_x = np.array([-4, 0, -8])

data_y = np.array([3, 5, 3])
axis = -1
for dtype in floordiv_support_dtype:
elementwise_floordiv("elementwise_floordiv_" + dtype + "_1",
data_x.astype(dtype), data_y.astype(dtype), axis, dtype)

data_x = np.random.randint(-10, 10, [2, 5, 3, 4])
data_y = np.random.randint(1, 5, [3, 4])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @taixiurong could add the test case with negative value, seems Paddle's implementation is aligned with Torch.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @taixiurong could add the test case with negative value, seems Paddle's implementation is aligned with Torch.

  1. yes, i review the paddle code , floor_div to call std::trunc(a/b), https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/kernels/funcs/elementwise_functor.h#L573
    but the paddle give a response:

image

PaddlePaddle/Paddle#46379
2. in this test case , i use a = -2, b = 1, is ok. a = -4, b = 3, z = floor_div(a/b), z = -1。 so want to modiy this logic in openvino?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ceciliapeng2011 @meiyang-intel what's your opinion ?

Copy link
Contributor Author

@taixiurong taixiurong Sep 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@OpenVINO-dev-contest hi paddle fix this bug,
doc: https://github.com/PaddlePaddle/Paddle/pull/46419/files
code: https://github.com/PaddlePaddle/Paddle/pull/45051/files
i fix the code in openvino, tese code like:
image

the tese result:
image

do want to push new code ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

of course, you can try it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you test float to check if OpenVINO and Paddle can have the same result too? I am afraid they won't.

OpenVINO divide op has an attribute "pythondiv", but works with integer only. That's why the integer tests pass.


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jane-intel Do you have comments about the algorithm discrepancy of openvino divide and paddle floor_divide? Thanks.

Copy link
Contributor Author

@taixiurong taixiurong Sep 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for dtype in floordiv_support_dtype:
elementwise_floordiv("elementwise_floordiv_" + dtype + "_2",
data_x.astype(dtype), data_y.astype(dtype), axis, dtype)

data_y = np.random.randint(1, 5, [5, 3, 4])
for dtype in floordiv_support_dtype:
elementwise_floordiv("elementwise_floordiv_" + dtype + "_3",
data_x.astype(dtype), data_y.astype(dtype), axis, dtype)


if __name__ == "__main__":
main()
15 changes: 15 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,21 @@ 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");
auto axis = -1;
if (node_context.has_attribute("axis")) {
axis = node_context.get_attribute<int>("axis");
}
return node_context.default_single_output_mapping(
{std::make_shared<default_opset::Divide>(x,
y,
false,
ov::op::AutoBroadcastSpec(ov::op::AutoBroadcastType::PDPD, axis))},
{"Out"});
}

NamedOutputs elementwise_mod(const NodeContext& node_context) {
return elementwise_ops<default_opset::FloorMod>(node_context);
}
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 @@ -26,6 +26,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 @@ -126,6 +127,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_mod", op::elementwise_mod},
Expand Down