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】Add Paddle elementwise_mod operator #12370

Merged
merged 6 commits into from
Oct 10, 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 @@ -103,27 +103,31 @@ static const std::vector<std::string> models{
std::string("elementwise_div1"),
std::string("elementwise_max1"),
std::string("elementwise_min1"),
std::string("elementwise_mod1"),
std::string("elementwise_mul1"),
std::string("elementwise_pow1"),
std::string("elementwise_sub1"),
std::string("elementwise_add2"),
std::string("elementwise_div2"),
std::string("elementwise_max2"),
std::string("elementwise_min2"),
std::string("elementwise_mod2"),
std::string("elementwise_mul2"),
std::string("elementwise_pow2"),
std::string("elementwise_sub2"),
std::string("elementwise_add3"),
std::string("elementwise_div3"),
std::string("elementwise_max3"),
std::string("elementwise_min3"),
std::string("elementwise_mod3"),
std::string("elementwise_mul3"),
std::string("elementwise_pow3"),
std::string("elementwise_sub3"),
std::string("elementwise_add4"),
std::string("elementwise_div4"),
std::string("elementwise_max4"),
std::string("elementwise_min4"),
std::string("elementwise_mod4"),
std::string("elementwise_mul4"),
std::string("elementwise_pow4"),
std::string("elementwise_sub4"),
Expand Down Expand Up @@ -155,6 +159,8 @@ static const std::vector<std::string> models{
std::string("fill_constant_shape_tensor_list"),
std::string("flatten_contiguous_range_test1"),
std::string("floor_float32"),
std::string("floor_mod1"),
std::string("floor_mod2"),
std::string("gather_multi_dimension"),
std::string("gather_one_dimension"),
std::string("gather_one_dimension2"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,31 @@ def elementwise_div(name : str, x, y, axis, in_dtype):
return outs[0]


def elementwise_mod(name : str, x, y, axis, in_dtype, is_api=False):
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 is_api:
out = paddle.floor_mod(node_x, node_y)
else:
out = paddle.fluid.layers.elementwise_mod(node_x, node_y, axis=axis)

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_mul(name : str, x, y, axis, in_dtype):
import paddle
paddle.enable_static()
Expand Down Expand Up @@ -166,6 +191,7 @@ 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)
elementwise_div("elementwise_div" + name, data_x, data_y, axis, in_dtype)
elementwise_mod("elementwise_mod" + name, data_x, data_y, axis, in_dtype)
elementwise_mul("elementwise_mul" + name, data_x, data_y, axis, in_dtype)
elementwise_min("elementwise_min" + name, data_x, data_y, axis, in_dtype)
elementwise_max("elementwise_max" + name, data_x, data_y, axis, in_dtype)
Expand All @@ -179,11 +205,13 @@ def main():
data_y = np.array([1, 5, 2]).astype(in_dtype)
axis = -1
elementwise_ops("1", data_x, data_y, axis, in_dtype)
elementwise_mod('floor_mod1', data_x, data_y, -1, in_dtype, True)

# data_y's shape is the continuous subsequence of data_x's shape
data_x = np.random.rand(2, 5, 3, 4).astype(np.float32)
data_y = (0.1 + np.random.rand(3, 4).astype(np.float32)) / 1.1
elementwise_ops("2", data_x, data_y, axis, in_dtype)
elementwise_mod('floor_mod2', data_x, data_y, -1, in_dtype, True)

data_y = (0.1 + np.random.rand(5).astype(np.float32)) / 1.1
axis = 1
Expand Down
4 changes: 4 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,10 @@ NamedOutputs elementwise_greater_equal(const NodeContext& node_context) {
return elementwise_ops<default_opset::GreaterEqual>(node_context);
}

NamedOutputs elementwise_mod(const NodeContext& node_context) {
return elementwise_ops<default_opset::FloorMod>(node_context);
}

} // 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 @@ -29,6 +29,7 @@ OP_CONVERTER(elementwise_equal);
OP_CONVERTER(elementwise_greater_equal);
OP_CONVERTER(elementwise_max);
OP_CONVERTER(elementwise_min);
OP_CONVERTER(elementwise_mod);
OP_CONVERTER(elementwise_mul);
OP_CONVERTER(elementwise_pow);
OP_CONVERTER(elementwise_sub);
Expand Down Expand Up @@ -127,6 +128,7 @@ std::map<std::string, CreatorFunction> get_supported_ops() {
{"elementwise_div", op::elementwise_div},
{"elementwise_max", op::elementwise_max},
{"elementwise_min", op::elementwise_min},
{"elementwise_mod", op::elementwise_mod},
{"elementwise_mul", op::elementwise_mul},
{"elementwise_pow", op::elementwise_pow},
{"elementwise_sub", op::elementwise_sub},
Expand Down