add paddle elementwise mod op (#12370)

Co-authored-by: cecilia peng <cecilia.peng@intel.com>
This commit is contained in:
Asthestarsfalll 2022-10-10 16:03:09 +08:00 committed by GitHub
parent 372fe475c9
commit 94ef65f0bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 0 deletions

View File

@ -103,6 +103,7 @@ 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"),
@ -110,6 +111,7 @@ static const std::vector<std::string> models{
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"),
@ -117,6 +119,7 @@ static const std::vector<std::string> models{
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"),
@ -124,6 +127,7 @@ static const std::vector<std::string> models{
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"),
@ -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"),

View File

@ -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()
@ -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)
@ -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

View File

@ -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

View File

@ -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);
@ -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},