Skip to content
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
31 changes: 5 additions & 26 deletions python/paddle/fluid/tests/unittests/test_elementwise_add_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ def test_name(self):
y_1 = paddle.add(x, y, name='add_res')
self.assertEqual(('add_res' in y_1.name), True)

def test_alpha(self):
def test_declarative(self):
with fluid.program_guard(fluid.Program()):

def gen_data():
Expand All @@ -408,33 +408,12 @@ def gen_data():

x = fluid.data(name="x", shape=[3], dtype='float32')
y = fluid.data(name="y", shape=[3], dtype='float32')
z = paddle.add(x, y, alpha=10)
z = paddle.add(x, y)

place = fluid.CPUPlace()
exe = fluid.Executor(place)
z_value = exe.run(feed=gen_data(), fetch_list=[z.name])
z_expected = np.array([12., 53., 24.])
self.assertEqual((z_value == z_expected).all(), True)

def test_alpha_gpu(self):
if not fluid.core.is_compiled_with_cuda():
return
with fluid.program_guard(fluid.Program()):

def gen_data():
return {
"x": np.array([2, 3, 4]).astype('float32'),
"y": np.array([1, 5, 2]).astype('float32')
}

x = fluid.data(name="x", shape=[3], dtype='float32')
y = fluid.data(name="y", shape=[3], dtype='float32')
z = paddle.add(x, y, alpha=-0.5)

place = fluid.CUDAPlace(0)
exe = fluid.Executor(place)
z_value = exe.run(feed=gen_data(), fetch_list=[z.name])
z_expected = np.array([1.5, 0.5, 3.])
z_expected = np.array([3., 8., 6.])
self.assertEqual((z_value == z_expected).all(), True)

def test_dygraph(self):
Expand All @@ -443,9 +422,9 @@ def test_dygraph(self):
np_y = np.array([1, 5, 2]).astype('float64')
x = fluid.dygraph.to_variable(np_x)
y = fluid.dygraph.to_variable(np_y)
z = paddle.add(x, y, alpha=-0.5)
z = paddle.add(x, y)
np_z = z.numpy()
z_expected = np.array([1.5, 0.5, 3.])
z_expected = np.array([3., 8., 6.])
self.assertEqual((np_z == z_expected).all(), True)


Expand Down
147 changes: 17 additions & 130 deletions python/paddle/tensor/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,134 +236,30 @@ def _elementwise_op(helper):
return helper.append_activation(out)


def add(x, y, alpha=1, name=None):
def add(x, y, name=None):
"""
Examples:

.. code-block:: python

import paddle
import paddle.fluid as fluid
import numpy as np

def gen_data():
return {
"x": np.array([2, 3, 4]).astype('float32'),
"y": np.array([1, 5, 2]).astype('float32')
}

x = fluid.data(name="x", shape=[3], dtype='float32')
y = fluid.data(name="y", shape=[3], dtype='float32')
z1 = paddle.add(x, y)
z2 = paddle.add(x, y, alpha=10)
# z = x + y

place = fluid.CPUPlace()
exe = fluid.Executor(place)
z_value = exe.run(feed=gen_data(),
fetch_list=[z1.name, z2.name])

print(z_value[0]) # [3., 8., 6.]
print(z_value[1]) # [12. 53. 24.]


.. code-block:: python

import paddle
import paddle.fluid as fluid
import numpy as np

def gen_data():
return {
"x": np.ones((2, 3, 4, 5)).astype('float32'),
"y": np.zeros((4, 5)).astype('float32')
}

x = fluid.data(name="x", shape=[2, 3, 4, 5], dtype='float32')
y = fluid.data(name="y", shape=[4, 5], dtype='float32')
z = paddle.add(x, y, name='z')
# z = x + y

place = fluid.CPUPlace()
exe = fluid.Executor(place)

z_value = exe.run(feed=gen_data(),
fetch_list=[z.name])

print(z_value[0])
print(z_value[0].shape) # z.shape=[2,3,4,5]


.. code-block:: python

import paddle
import paddle.fluid as fluid
import numpy as np

def gen_data():
return {
"x": np.random.randint(1, 5, size=[2, 3, 4, 5]).astype('float32'),
"y": np.random.randint(1, 5, size=[5]).astype('float32')
}

x = fluid.data(name="x", shape=[2,3,4,5], dtype='float32')
y = fluid.data(name="y", shape=[5], dtype='float32')
paddle.disable_static()
np_x = np.array([2, 3, 4]).astype('float64')
np_y = np.array([1, 5, 2]).astype('float64')
x = paddle.to_variable(np_x)
y = paddle.to_variable(np_y)
z = paddle.add(x, y)
# z = x / y

place = fluid.CPUPlace()
exe = fluid.Executor(place)

z_value = exe.run(feed=gen_data(),
fetch_list=[z.name])
print(z_value[0])
print(z_value[0].shape) # z.shape=[2,3,4,5]


.. code-block:: python

import paddle
import paddle.fluid as fluid
import numpy as np

x = fluid.data(name="x", shape=[3], dtype="float32")
y = fluid.data(name='y', shape=[3], dtype='float32')
z = paddle.add(x, y)

place = fluid.CPUPlace()
exe = fluid.Executor(place)
data1 = np.array([2, 3, 4], dtype='float32')
data2 = np.array([1, 5, 2], dtype='float32')
z_value = exe.run(feed={'x': data1,
'y': data2},
fetch_list=[z])
print(z_value[0]) # [3. 8. 6.]


.. code-block:: python

import paddle
import paddle.fluid as fluid
import numpy as np

with fluid.dygraph.guard():
np_x = np.array([2, 3, 4]).astype('float64')
np_y = np.array([1, 5, 2]).astype('float64')
x = fluid.dygraph.to_variable(np_x)
y = fluid.dygraph.to_variable(np_y)
z = paddle.add(x, y, alpha=-0.5)
np_z = z.numpy()
print(np_z) # [1.5, 0.5, 3. ]
np_z = z.numpy()
print(np_z) # [3., 8., 6. ]

"""
op_type = 'elementwise_add'
axis = -1
act = None
if alpha != 1:
y = scale(y, scale=alpha)
if in_dygraph_mode():
return _elementwise_op_in_dygraph(
x, y, axis=axis, act=act, op_name=op_type)
x, y, axis=axis, op_name=op_type)

return _elementwise_op(LayerHelper(op_type, **locals()))

Expand Down Expand Up @@ -520,30 +416,21 @@ def multiply(x, y, axis=-1, name=None):
]:
proto_dict = {'add': 'elementwise_add', 'div': 'elementwise_div', 'multiply': 'elementwise_mul'}
op_proto = OpProtoHolder.instance().get_op_proto(proto_dict[func.__name__])
if func.__name__ in ['add']:
alias_main = ':alias_main: paddle.%(func)s' % {'func': func.__name__}
alias = ':alias: paddle.%(func)s, paddle.tensor.%(func)s, paddle.tensor.math.%(func)s' % {'func': func.__name__}

additional_args_lines = [
"alpha (int|float, optional): The alpha factor of the input. Default is 1. If alpha is not 1, the equation becomes Out = X + alpha * Y.",
"name (string, optional): Name of the output. \
Default is None. It's used to print debug info for developers. Details: \
:ref:`api_guide_Name` "
]
else:
additional_args_lines = [
"name (string, optional): Name of the output. \
Default is None. It's used to print debug info for developers. Details: \
:ref:`api_guide_Name` "
]

func.__doc__ = alias_main + """\n""" + alias + """\n""" + _generate_doc_string_(
additional_args_lines = [
"name (string, optional): Name of the output. \
Default is None. It's used to print debug info for developers. Details: \
:ref:`api_guide_Name` "
]

func.__doc__ = _generate_doc_string_(
op_proto,
additional_args_lines=additional_args_lines,
skip_attrs_set={"x_data_format", "y_data_format", "axis",
"use_quantizer", "mkldnn_data_type", "Scale_x", "Scale_y", "Scale_out"
}) + """\n""" + str(func.__doc__)


def sum(input, dim=None, dtype=None, keep_dim=False, name=None):
"""
:alias_main: paddle.sum
Expand Down