Skip to content

Commit 118a4d2

Browse files
committed
update
1 parent 813fb53 commit 118a4d2

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

python/paddle/tensor/math.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -767,27 +767,28 @@ def add(
767767
[3., 8., 6.])
768768
"""
769769
if in_dynamic_or_pir_mode():
770-
if alpha != 1:
771-
y = y * alpha
772-
773-
result = _C_ops.add(x, y, out=out)
774-
return result
770+
scaled_y = y * alpha if alpha != 1 else y
771+
return _C_ops.add(x, scaled_y, out=out)
775772
else:
776773
helper = LayerHelper('elementwise_add', **locals())
774+
scaled_y = (
775+
helper.create_variable_for_type_inference(y.dtype)
776+
if alpha != 1
777+
else y
778+
)
777779

778780
if alpha != 1:
779-
y = helper.create_variable_for_type_inference(y.dtype)
780781
helper.append_op(
781782
type='scale',
782783
inputs={'X': [y]},
783-
outputs={'Out': [y]},
784+
outputs={'Out': [scaled_y]},
784785
attrs={'scale': alpha, 'bias': 0.0},
785786
)
786787

787788
output = helper.create_variable_for_type_inference(x.dtype)
788789
helper.append_op(
789790
type='elementwise_add',
790-
inputs={'X': x, 'Y': y},
791+
inputs={'X': x, 'Y': scaled_y},
791792
outputs={'Out': output},
792793
attrs={'axis': -1},
793794
)
@@ -814,9 +815,8 @@ def add_(
814815
f"The shape of broadcast output {out_shape} is different from that of inplace tensor {x.shape} in the Inplace operation."
815816
)
816817

817-
if alpha != 1:
818-
y = y * alpha
819-
return _C_ops.add_(x, y)
818+
scaled_y = y * alpha if alpha != 1 else y
819+
return _C_ops.add_(x, scaled_y)
820820

821821

822822
def logaddexp(x: Tensor, y: Tensor, name: str | None = None) -> Tensor:

test/legacy_test/test_elementwise_add_op.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ def init_axis(self):
647647

648648
class TestAddApi(unittest.TestCase):
649649
def _executed_api(self, x, y, name=None):
650-
return paddle.add(x, y, name)
650+
return paddle.add(x, y, name=name)
651651

652652
def test_name(self):
653653
with (
@@ -697,7 +697,7 @@ def init_data(self):
697697
self.y_numpy = np.random.rand(0, 3, 4).astype('float32')
698698

699699
def _executed_api(self, x, y, name=None):
700-
return paddle.add(x, y, name)
700+
return paddle.add(x, y, name=name)
701701

702702
def test_declarative(self):
703703
self.init_data()

0 commit comments

Comments
 (0)