Skip to content

Commit 532736a

Browse files
committed
update
1 parent 54b4fc1 commit 532736a

File tree

2 files changed

+69
-99
lines changed

2 files changed

+69
-99
lines changed

python/paddle/tensor/math.py

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -706,23 +706,22 @@ def _elementwise_op(helper):
706706
@ParamAliasDecorator({"x": ["input"], "y": ["other"]})
707707
def add(
708708
x: Tensor,
709-
y: Tensor | Number,
710-
name: str | None = None,
711-
*,
712-
alpha: Number | None = 1,
709+
y: Tensor,
710+
alpha: Number = 1,
713711
out: Tensor | None = None,
712+
name: str | None = None,
714713
) -> Tensor:
715714
"""
716715
Elementwise Add Operator.
717-
Add two tensors element-wise or add a scalar to tensor.
716+
Add two tensors element-wise to tensor.
718717
The equation is:
719718
720719
.. math::
721720
722721
Out=X+Y
723722
724723
$X$ the tensor of any dimension.
725-
$Y$ the tensor or scalar whose dimensions must be less than or equal to the dimensions of $X$.
724+
$Y$ the tensor whose dimensions must be less than or equal to the dimensions of $X$.
726725
727726
This operator is used in the following cases:
728727
@@ -744,11 +743,11 @@ def add(
744743
Args:
745744
x (Tensor): Tensor of any dimensions. Its dtype should be bool, bfloat16, float16, float32, float64,
746745
int8, int16, int32, int64, uint8, complex64, complex128.
747-
y (Tensor|Number): Tensor or scalar of any dimensions. Its dtype should be bool, bfloat16, float16, float32, float64,
746+
y (Tensor): Tensor of any dimensions. Its dtype should be bool, bfloat16, float16, float32, float64,
748747
int8, int16, int32, int64, uint8, complex64, complex128.
749-
name (str|None, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
750748
alpha (Number, optional): Scaling factor for Y. Default: 1.
751749
out (Tensor, optional): The output tensor. Default: None.
750+
name (str|None, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
752751
753752
Returns:
754753
N-D Tensor. A location into which the result is stored. It's dimension equals with x.
@@ -766,29 +765,6 @@ def add(
766765
Tensor(shape=[3], dtype=float64, place=Place(cpu), stop_gradient=True,
767766
[3., 8., 6.])
768767
"""
769-
770-
if not isinstance(y, paddle.Tensor):
771-
if in_dynamic_or_pir_mode():
772-
y = paddle.to_tensor(y)
773-
else:
774-
if isinstance(y, bool):
775-
dtype = paddle.int64
776-
elif isinstance(y, int):
777-
dtype = (
778-
paddle.int64
779-
if x.dtype in (paddle.int32, paddle.int64)
780-
else x.dtype
781-
)
782-
elif isinstance(y, float):
783-
dtype = (
784-
paddle.float32
785-
if x.dtype in (paddle.float16, paddle.float32)
786-
else x.dtype
787-
)
788-
else:
789-
dtype = x.dtype
790-
y = paddle.full(shape=[1], fill_value=y, dtype=dtype)
791-
792768
if in_dynamic_or_pir_mode():
793769
if alpha != 1:
794770
y = y * alpha
@@ -820,20 +796,13 @@ def add(
820796
@ParamAliasDecorator({"x": ["input"], "y": ["other"]})
821797
@inplace_apis_in_dygraph_only
822798
def add_(
823-
x: Tensor,
824-
y: Tensor | Number,
825-
name: str | None = None,
826-
*,
827-
alpha: Number | None = 1,
799+
x: Tensor, y: Tensor, alpha: Number = 1, name: str | None = None
828800
) -> Tensor:
829801
"""
830802
Inplace version of ``add`` API, the output Tensor will be inplaced with input ``x``.
831803
Please refer to :ref:`api_paddle_add`.
832804
"""
833805

834-
if not isinstance(y, paddle.Tensor):
835-
y = paddle.to_tensor(y)
836-
837806
out_shape = broadcast_shape(x.shape, y.shape)
838807
if out_shape != x.shape:
839808
raise ValueError(

test/legacy_test/test_add_op.py

Lines changed: 61 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -106,66 +106,67 @@ def test_param_alias_input_other(self):
106106
np.testing.assert_array_equal(out2.numpy(), expected)
107107
np.testing.assert_array_equal(x_clone.numpy(), expected)
108108

109-
def test_scalar_addition(self):
110-
"""test scalar addition"""
111-
x = paddle.to_tensor(self.x_np)
112-
113-
out1 = paddle.add(x, self.scalar)
114-
expected1 = self.x_np + self.scalar
115-
np.testing.assert_array_equal(out1.numpy(), expected1)
116-
117-
out2 = x.add(self.scalar)
118-
np.testing.assert_array_equal(out2.numpy(), expected1)
119-
120-
out3 = paddle.add(x, self.scalar, alpha=2)
121-
expected3 = self.x_np + self.scalar * 2
122-
np.testing.assert_array_equal(out3.numpy(), expected3)
123-
124-
def test_scalar_addition_inplace(self):
125-
"""test inplace scalar addition"""
126-
x = paddle.to_tensor(self.x_np)
127-
x_clone = x.clone()
128-
129-
x_clone.add_(self.scalar)
130-
expected = self.x_np + self.scalar
131-
np.testing.assert_array_equal(x_clone.numpy(), expected)
132-
133-
x_clone2 = x.clone()
134-
x_clone2.add_(self.scalar, alpha=2)
135-
expected2 = self.x_np + self.scalar * 2
136-
np.testing.assert_array_equal(x_clone2.numpy(), expected2)
137-
138-
def test_different_dtype_scalar(self):
139-
"""test different dtype scalar addition"""
140-
x = paddle.to_tensor(self.x_np)
141-
142-
out1 = x.add(2)
143-
expected1 = self.x_np + 2
144-
np.testing.assert_array_equal(out1.numpy(), expected1)
145-
146-
out2 = x.add(2.5)
147-
expected2 = self.x_np + 2.5
148-
np.testing.assert_array_equal(out2.numpy(), expected2)
149-
150-
def test_scalar_addition_static_graph(self):
151-
"""test static graph scalar addition"""
152-
paddle.enable_static()
153-
with paddle.static.program_guard(paddle.static.Program()):
154-
x = paddle.static.data(name='x', shape=[-1, 2], dtype='float32')
155-
out1 = paddle.add(x, self.scalar)
156-
out2 = paddle.add(x, self.scalar, alpha=2)
157-
158-
exe = paddle.static.Executor(self.place)
159-
res = exe.run(
160-
feed={'x': self.x_np.reshape(1, 2)},
161-
fetch_list=[out1, out2],
162-
)
163-
164-
expected1 = self.x_np + self.scalar
165-
expected2 = self.x_np + self.scalar * 2
166-
np.testing.assert_array_equal(res[0].flatten(), expected1)
167-
np.testing.assert_array_equal(res[1].flatten(), expected2)
168-
paddle.disable_static()
109+
# Note: y does not support scalars separately, but will support them uniformly in the future.
110+
# def test_scalar_addition(self):
111+
# """test scalar addition"""
112+
# x = paddle.to_tensor(self.x_np)
113+
114+
# out1 = paddle.add(x, self.scalar)
115+
# expected1 = self.x_np + self.scalar
116+
# np.testing.assert_array_equal(out1.numpy(), expected1)
117+
118+
# out2 = x.add(self.scalar)
119+
# np.testing.assert_array_equal(out2.numpy(), expected1)
120+
121+
# out3 = paddle.add(x, self.scalar, alpha=2)
122+
# expected3 = self.x_np + self.scalar * 2
123+
# np.testing.assert_array_equal(out3.numpy(), expected3)
124+
125+
# def test_scalar_addition_inplace(self):
126+
# """test inplace scalar addition"""
127+
# x = paddle.to_tensor(self.x_np)
128+
# x_clone = x.clone()
129+
130+
# x_clone.add_(self.scalar)
131+
# expected = self.x_np + self.scalar
132+
# np.testing.assert_array_equal(x_clone.numpy(), expected)
133+
134+
# x_clone2 = x.clone()
135+
# x_clone2.add_(self.scalar, alpha=2)
136+
# expected2 = self.x_np + self.scalar * 2
137+
# np.testing.assert_array_equal(x_clone2.numpy(), expected2)
138+
139+
# def test_different_dtype_scalar(self):
140+
# """test different dtype scalar addition"""
141+
# x = paddle.to_tensor(self.x_np)
142+
143+
# out1 = x.add(2)
144+
# expected1 = self.x_np + 2
145+
# np.testing.assert_array_equal(out1.numpy(), expected1)
146+
147+
# out2 = x.add(2.5)
148+
# expected2 = self.x_np + 2.5
149+
# np.testing.assert_array_equal(out2.numpy(), expected2)
150+
151+
# def test_scalar_addition_static_graph(self):
152+
# """test static graph scalar addition"""
153+
# paddle.enable_static()
154+
# with paddle.static.program_guard(paddle.static.Program()):
155+
# x = paddle.static.data(name='x', shape=[-1, 2], dtype='float32')
156+
# out1 = paddle.add(x, self.scalar)
157+
# out2 = paddle.add(x, self.scalar, alpha=2)
158+
159+
# exe = paddle.static.Executor(self.place)
160+
# res = exe.run(
161+
# feed={'x': self.x_np.reshape(1, 2)},
162+
# fetch_list=[out1, out2],
163+
# )
164+
165+
# expected1 = self.x_np + self.scalar
166+
# expected2 = self.x_np + self.scalar * 2
167+
# np.testing.assert_array_equal(res[0].flatten(), expected1)
168+
# np.testing.assert_array_equal(res[1].flatten(), expected2)
169+
# paddle.disable_static()
169170

170171

171172
class TestAddOut(unittest.TestCase):

0 commit comments

Comments
 (0)