From 4667bba43f9d4f528c59fd214ca8f14033262e2c Mon Sep 17 00:00:00 2001 From: hong <43953930+phlrain@users.noreply.github.com> Date: Sat, 12 Oct 2019 11:02:16 +0800 Subject: [PATCH] Cherry pick floor div mod (#20538) * fix elementwise floordiv mod; test=develop * restore nn.py; test=release/1.6 * change floordiv, mod doc; test=release/1.6 --- paddle/fluid/API.spec | 4 +- .../elementwise/elementwise_floordiv_op.cc | 16 ++++++ .../elementwise/elementwise_mod_op.cc | 16 ++++++ python/paddle/fluid/layers/nn.py | 57 +++++++++++++++++-- 4 files changed, 87 insertions(+), 6 deletions(-) diff --git a/paddle/fluid/API.spec b/paddle/fluid/API.spec index 53f74bc0da..7394f4c209 100644 --- a/paddle/fluid/API.spec +++ b/paddle/fluid/API.spec @@ -251,8 +251,8 @@ paddle.fluid.layers.elementwise_mul (ArgSpec(args=['x', 'y', 'axis', 'act', 'nam paddle.fluid.layers.elementwise_max (ArgSpec(args=['x', 'y', 'axis', 'act', 'name'], varargs=None, keywords=None, defaults=(-1, None, None)), ('document', '27e1e1604433f85cd7c4c320cb7b9a5f')) paddle.fluid.layers.elementwise_min (ArgSpec(args=['x', 'y', 'axis', 'act', 'name'], varargs=None, keywords=None, defaults=(-1, None, None)), ('document', 'cdbfe2042da18b0e2a2fd79fde944dc2')) paddle.fluid.layers.elementwise_pow (ArgSpec(args=['x', 'y', 'axis', 'act', 'name'], varargs=None, keywords=None, defaults=(-1, None, None)), ('document', '6fc5d7492830d60c7fa61b3bc8f0d7e7')) -paddle.fluid.layers.elementwise_mod (ArgSpec(args=['x', 'y', 'axis', 'act', 'name'], varargs=None, keywords=None, defaults=(-1, None, None)), ('document', '4101ee1f9280f00dce54054ccc434890')) -paddle.fluid.layers.elementwise_floordiv (ArgSpec(args=['x', 'y', 'axis', 'act', 'name'], varargs=None, keywords=None, defaults=(-1, None, None)), ('document', '67e6101c31314d4082621e8e443cfb68')) +paddle.fluid.layers.elementwise_mod (ArgSpec(args=['x', 'y', 'axis', 'act', 'name'], varargs=None, keywords=None, defaults=(-1, None, None)), ('document', '7bc28b6e147067a220f8e00b9055f62e')) +paddle.fluid.layers.elementwise_floordiv (ArgSpec(args=['x', 'y', 'axis', 'act', 'name'], varargs=None, keywords=None, defaults=(-1, None, None)), ('document', '0035541f94bddb5ad472f9788384ca6a')) paddle.fluid.layers.uniform_random_batch_size_like (ArgSpec(args=['input', 'shape', 'dtype', 'input_dim_idx', 'output_dim_idx', 'min', 'max', 'seed'], varargs=None, keywords=None, defaults=('float32', 0, 0, -1.0, 1.0, 0)), ('document', 'cfa120e583cd4a5bfa120c8a26f98a28')) paddle.fluid.layers.gaussian_random (ArgSpec(args=['shape', 'mean', 'std', 'seed', 'dtype'], varargs=None, keywords=None, defaults=(0.0, 1.0, 0, 'float32')), ('document', 'dd4ddb66c78a2564e5d1e0e345d8286f')) paddle.fluid.layers.sampling_id (ArgSpec(args=['x', 'min', 'max', 'seed', 'dtype'], varargs=None, keywords=None, defaults=(0.0, 1.0, 0, 'float32')), ('document', '9ac9bdc45be94494d8543b8cec5c26e0')) 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 d0da63e527..54d9934ee7 100755 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -13826,10 +13826,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())) @@ -13841,6 +13891,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_( @@ -13858,10 +13910,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