From efa10937bd4c9c5c7950160a44b831c3c9a39cff Mon Sep 17 00:00:00 2001 From: zhongpu <2013000149@qq.com> Date: Wed, 16 Oct 2019 20:06:23 +0800 Subject: [PATCH] fix elementwise_floordiv_op and elementwise_mod_op (#20534) * fix elementwise_floordiv_op and elementwise_mod_op, test=develop * fix API.spec, test=develop * fix API.spec, test=develop --- .../elementwise/elementwise_floordiv_op.cc | 16 ++++++ .../elementwise/elementwise_mod_op.cc | 16 ++++++ python/paddle/fluid/layers/nn.py | 57 +++++++++++++++++-- 3 files changed, 85 insertions(+), 4 deletions(-) diff --git a/paddle/fluid/operators/elementwise/elementwise_floordiv_op.cc b/paddle/fluid/operators/elementwise/elementwise_floordiv_op.cc index 66c56da417..5a398fa50f 100644 --- a/paddle/fluid/operators/elementwise/elementwise_floordiv_op.cc +++ b/paddle/fluid/operators/elementwise/elementwise_floordiv_op.cc @@ -22,6 +22,22 @@ class ElementwiseFloorDivOpMaker : public ElementwiseOpMaker { protected: std::string GetName() const override { return "FloorDiv"; } std::string GetEquation() const override { return "Out = X // Y"; } + + void AddInputX() override { + AddInput("X", + "(Variable), Tensor or LoDTensor of any dimensions. Its dtype " + "should be int32, int64."); + } + + void AddInputY() override { + AddInput("Y", + "(Variable), Tensor or LoDTensor of any dimensions. Its dtype " + "should be int32, int64."); + } + + std::string GetOpFuntionality() const override { + return "Floor divide two tensors element-wise"; + } }; } // namespace operators } // namespace paddle diff --git a/paddle/fluid/operators/elementwise/elementwise_mod_op.cc b/paddle/fluid/operators/elementwise/elementwise_mod_op.cc index 451c7816b9..af80666b95 100644 --- a/paddle/fluid/operators/elementwise/elementwise_mod_op.cc +++ b/paddle/fluid/operators/elementwise/elementwise_mod_op.cc @@ -22,6 +22,22 @@ class ElementwiseModOpMaker : public ElementwiseOpMaker { protected: std::string GetName() const override { return "Mod"; } std::string GetEquation() const override { return "Out = X \\\\% Y"; } + + void AddInputX() override { + AddInput("X", + "(Variable), Tensor or LoDTensor of any dimensions. Its dtype " + "should be int32, int64."); + } + + void AddInputY() override { + AddInput("Y", + "(Variable), Tensor or LoDTensor of any dimensions. Its dtype " + "should be int32, int64."); + } + + std::string GetOpFuntionality() const override { + return "Mod two tensors element-wise"; + } }; } // namespace operators } // namespace paddle diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index 3322519fcd..fd9af1a015 100644 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -14320,10 +14320,60 @@ Examples: def elementwise_mod(x, y, axis=-1, act=None, name=None): + """ +Examples: + + .. code-block:: python + + import paddle.fluid as fluid + import numpy as np + + def gen_data(): + return { + "x": np.array([10, 15, 8]).astype('int32'), + "y": np.array([3, 6, 5]).astype('int32') + } + + x = fluid.data(name="x", shape=[3], dtype='int32') + y = fluid.data(name="y", shape=[3], dtype='int32') + z = fluid.layers.elementwise_mod(x, y) + + place = fluid.CPUPlace() + exe = fluid.Executor(place) + z_value = exe.run(feed=gen_data(), + fetch_list=[z.name]) + + print(z_value) #[1, 3, 3] + """ return _elementwise_op(LayerHelper('elementwise_mod', **locals())) def elementwise_floordiv(x, y, axis=-1, act=None, name=None): + """ +Examples: + + .. code-block:: python + + import paddle.fluid as fluid + import numpy as np + + def gen_data(): + return { + "x": np.array([10, 15, 8]).astype('int32'), + "y": np.array([3, 7, 5]).astype('int32') + } + + x = fluid.data(name="x", shape=[3], dtype='int32') + y = fluid.data(name="y", shape=[3], dtype='int32') + z = fluid.layers.elementwise_floordiv(x, y) + + place = fluid.CPUPlace() + exe = fluid.Executor(place) + z_value = exe.run(feed=gen_data(), + fetch_list=[z.name]) + + print(z_value) #[3, 2, 1] + """ return _elementwise_op(LayerHelper('elementwise_floordiv', **locals())) @@ -14335,6 +14385,8 @@ for func in [ elementwise_max, elementwise_pow, elementwise_min, + elementwise_mod, + elementwise_floordiv, ]: op_proto = OpProtoHolder.instance().get_op_proto(func.__name__) func.__doc__ = _generate_doc_string_( @@ -14352,10 +14404,7 @@ for func in [ skip_attrs_set={"x_data_format", "y_data_format", "axis" }) + """\n""" + str(func.__doc__) -for func in [ - elementwise_mod, - elementwise_floordiv, -]: +for func in []: op_proto = OpProtoHolder.instance().get_op_proto(func.__name__) func.__doc__ = _generate_doc_string_( op_proto, -- GitLab