Skip to content

Commit 69d6153

Browse files
aquagullaquagull
andauthored
[API Compatibility] add out parameter for maximum minimum (#74683)
* update * fix * update * update * fix * fix * fix docs * restore sqrt * fix * fix * fix * revert * update * update * update * add test * fix * code-style * fix --------- Co-authored-by: aquagull <[email protected]>
1 parent 0df3edc commit 69d6153

18 files changed

+334
-1857
lines changed

paddle/phi/ops/yaml/python_api_info.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@
88
args_alias :
99
use_default_mapping : True
1010

11+
- op : maximum
12+
name : [paddle.maximum,paddle.Tensor.maximum]
13+
args_alias :
14+
use_default_mapping : True
15+
16+
- op : minimum
17+
name : [paddle.minimum,paddle.Tensor.minimum]
18+
args_alias :
19+
use_default_mapping : True
20+
1121
- op : greater_than
1222
name : [paddle.greater_than, paddle.Tensor.greater_than]
1323
args_alias :

python/paddle/_paddle_docs.py

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,6 +1130,144 @@ def floor(
11301130
""",
11311131
)
11321132
# hehongyu
1133+
add_doc_and_signature(
1134+
"maximum",
1135+
"""
1136+
Compare two tensors and returns a new tensor containing the element-wise maxima. The equation is:
1137+
1138+
.. math::
1139+
out = max(x, y)
1140+
1141+
Note:
1142+
``paddle.maximum`` supports broadcasting. If you want know more about broadcasting, please refer to `Introduction to Tensor`_ .
1143+
1144+
.. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor
1145+
1146+
Args:
1147+
x (Tensor): the input tensor, it's data type should be bfloat16, float16, float32, float64, int32, int64.
1148+
y (Tensor): the input tensor, it's data type should be bfloat16, float16, float32, float64, int32, int64.
1149+
name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
1150+
out(Tensor, optional): The output tensor.
1151+
1152+
Returns:
1153+
N-D Tensor. A location into which the result is stored. If x, y have different shapes and are "broadcastable", the resulting tensor shape is the shape of x and y after broadcasting. If x, y have the same shape, its shape is the same as x and y.
1154+
1155+
Examples:
1156+
1157+
.. code-block:: python
1158+
1159+
>>> import paddle
1160+
1161+
>>> x = paddle.to_tensor([[1, 2], [7, 8]])
1162+
>>> y = paddle.to_tensor([[3, 4], [5, 6]])
1163+
>>> res = paddle.maximum(x, y)
1164+
>>> print(res)
1165+
Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
1166+
[[3, 4],
1167+
[7, 8]])
1168+
1169+
>>> x = paddle.to_tensor([[1, 2, 3], [1, 2, 3]])
1170+
>>> y = paddle.to_tensor([3, 0, 4])
1171+
>>> res = paddle.maximum(x, y)
1172+
>>> print(res)
1173+
Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
1174+
[[3, 2, 4],
1175+
[3, 2, 4]])
1176+
1177+
>>> x = paddle.to_tensor([2, 3, 5], dtype='float32')
1178+
>>> y = paddle.to_tensor([1, float("nan"), float("nan")], dtype='float32')
1179+
>>> res = paddle.maximum(x, y)
1180+
>>> print(res)
1181+
Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
1182+
[2. , nan, nan])
1183+
1184+
>>> x = paddle.to_tensor([5, 3, float("inf")], dtype='float32')
1185+
>>> y = paddle.to_tensor([1, -float("inf"), 5], dtype='float32')
1186+
>>> res = paddle.maximum(x, y)
1187+
>>> print(res)
1188+
Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
1189+
[5. , 3. , inf.])
1190+
""",
1191+
"""
1192+
def maximum(
1193+
x: Tensor,
1194+
y: Tensor,
1195+
name: str | None = None,
1196+
*,
1197+
out: Tensor | None = None,
1198+
) -> Tensor
1199+
""",
1200+
)
1201+
1202+
add_doc_and_signature(
1203+
"minimum",
1204+
"""
1205+
Compare two tensors and return a new tensor containing the element-wise minima. The equation is:
1206+
1207+
.. math::
1208+
out = min(x, y)
1209+
1210+
Note:
1211+
``paddle.minimum`` supports broadcasting. If you want know more about broadcasting, please refer to `Introduction to Tensor`_ .
1212+
1213+
.. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor
1214+
1215+
Args:
1216+
x (Tensor): the input tensor, it's data type should be bfloat16, float16, float32, float64, int32, int64.
1217+
y (Tensor): the input tensor, it's data type should be bfloat16, float16, float32, float64, int32, int64.
1218+
name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
1219+
out(Tensor, optional): The output tensor.
1220+
1221+
Returns:
1222+
Tensor. If x, y have different shapes and are "broadcastable", the resulting tensor shape is the shape of x and y after broadcasting. If x, y have the same shape, its shape is the same as x and y.
1223+
1224+
Examples:
1225+
1226+
.. code-block:: python
1227+
1228+
>>> import paddle
1229+
1230+
>>> x = paddle.to_tensor([[1, 2], [7, 8]])
1231+
>>> y = paddle.to_tensor([[3, 4], [5, 6]])
1232+
>>> res = paddle.minimum(x, y)
1233+
>>> print(res)
1234+
Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
1235+
[[1, 2],
1236+
[5, 6]])
1237+
1238+
>>> x = paddle.to_tensor([[[1, 2, 3], [1, 2, 3]]])
1239+
>>> y = paddle.to_tensor([3, 0, 4])
1240+
>>> res = paddle.minimum(x, y)
1241+
>>> print(res)
1242+
Tensor(shape=[1, 2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
1243+
[[[1, 0, 3],
1244+
[1, 0, 3]]])
1245+
1246+
>>> x = paddle.to_tensor([2, 3, 5], dtype='float32')
1247+
>>> y = paddle.to_tensor([1, float("nan"), float("nan")], dtype='float32')
1248+
>>> res = paddle.minimum(x, y)
1249+
>>> print(res)
1250+
Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
1251+
[1. , nan, nan])
1252+
1253+
>>> x = paddle.to_tensor([5, 3, float("inf")], dtype='float64')
1254+
>>> y = paddle.to_tensor([1, -float("inf"), 5], dtype='float64')
1255+
>>> res = paddle.minimum(x, y)
1256+
>>> print(res)
1257+
Tensor(shape=[3], dtype=float64, place=Place(cpu), stop_gradient=True,
1258+
[ 1. , -inf., 5. ])
1259+
""",
1260+
"""
1261+
def minimum(
1262+
x: Tensor,
1263+
y: Tensor,
1264+
name: str | None = None,
1265+
*,
1266+
out: Tensor | None = None,
1267+
) -> Tensor
1268+
""",
1269+
)
1270+
11331271
add_doc_and_signature(
11341272
"sqrt",
11351273
"""

python/paddle/tensor/math.py

Lines changed: 6 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
isnan,
3333
log,
3434
logsumexp,
35+
maximum,
36+
minimum,
3537
sign,
3638
sin,
3739
)
@@ -852,10 +854,10 @@ def logaddexp(x: Tensor, y: Tensor, name: str | None = None) -> Tensor:
852854
[-0.30685282, -0.68673831, -0.87307199])
853855
"""
854856
log_1p = paddle.log1p(paddle.exp(-paddle.abs(x - y)))
855-
maximum = paddle.maximum(x, y)
856-
if maximum.dtype == paddle.int32 or maximum.dtype == paddle.int64:
857-
maximum = maximum.astype(log_1p.dtype)
858-
return log_1p + maximum
857+
_maximum = paddle.maximum(x, y)
858+
if _maximum.dtype == paddle.int32 or _maximum.dtype == paddle.int64:
859+
_maximum = _maximum.astype(log_1p.dtype)
860+
return log_1p + _maximum
859861

860862

861863
def subtract(x: Tensor, y: Tensor, name: str | None = None) -> Tensor:
@@ -1448,130 +1450,6 @@ def _divide_with_axis(x, y, axis=-1, name=None):
14481450
return _elementwise_op(LayerHelper(op_type, **locals()))
14491451

14501452

1451-
def maximum(x: Tensor, y: Tensor, name: str | None = None) -> Tensor:
1452-
"""
1453-
Compare two tensors and returns a new tensor containing the element-wise maxima. The equation is:
1454-
1455-
.. math::
1456-
out = max(x, y)
1457-
1458-
Note:
1459-
``paddle.maximum`` supports broadcasting. If you want know more about broadcasting, please refer to `Introduction to Tensor`_ .
1460-
1461-
.. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor
1462-
1463-
Args:
1464-
x (Tensor): the input tensor, it's data type should be bfloat16, float16, float32, float64, int32, int64.
1465-
y (Tensor): the input tensor, it's data type should be bfloat16, float16, float32, float64, int32, int64.
1466-
name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
1467-
1468-
Returns:
1469-
N-D Tensor. A location into which the result is stored. If x, y have different shapes and are "broadcastable", the resulting tensor shape is the shape of x and y after broadcasting. If x, y have the same shape, its shape is the same as x and y.
1470-
1471-
Examples:
1472-
1473-
.. code-block:: python
1474-
1475-
>>> import paddle
1476-
1477-
>>> x = paddle.to_tensor([[1, 2], [7, 8]])
1478-
>>> y = paddle.to_tensor([[3, 4], [5, 6]])
1479-
>>> res = paddle.maximum(x, y)
1480-
>>> print(res)
1481-
Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
1482-
[[3, 4],
1483-
[7, 8]])
1484-
1485-
>>> x = paddle.to_tensor([[1, 2, 3], [1, 2, 3]])
1486-
>>> y = paddle.to_tensor([3, 0, 4])
1487-
>>> res = paddle.maximum(x, y)
1488-
>>> print(res)
1489-
Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
1490-
[[3, 2, 4],
1491-
[3, 2, 4]])
1492-
1493-
>>> x = paddle.to_tensor([2, 3, 5], dtype='float32')
1494-
>>> y = paddle.to_tensor([1, float("nan"), float("nan")], dtype='float32')
1495-
>>> res = paddle.maximum(x, y)
1496-
>>> print(res)
1497-
Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
1498-
[2. , nan, nan])
1499-
1500-
>>> x = paddle.to_tensor([5, 3, float("inf")], dtype='float32')
1501-
>>> y = paddle.to_tensor([1, -float("inf"), 5], dtype='float32')
1502-
>>> res = paddle.maximum(x, y)
1503-
>>> print(res)
1504-
Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
1505-
[5. , 3. , inf.])
1506-
"""
1507-
if in_dynamic_or_pir_mode():
1508-
return _C_ops.maximum(x, y)
1509-
else:
1510-
return _elementwise_op(LayerHelper('elementwise_max', **locals()))
1511-
1512-
1513-
def minimum(x: Tensor, y: Tensor, name: str | None = None) -> Tensor:
1514-
"""
1515-
Compare two tensors and return a new tensor containing the element-wise minima. The equation is:
1516-
1517-
.. math::
1518-
out = min(x, y)
1519-
1520-
Note:
1521-
``paddle.minimum`` supports broadcasting. If you want know more about broadcasting, please refer to `Introduction to Tensor`_ .
1522-
1523-
.. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor
1524-
1525-
Args:
1526-
x (Tensor): the input tensor, it's data type should be bfloat16, float16, float32, float64, int32, int64.
1527-
y (Tensor): the input tensor, it's data type should be bfloat16, float16, float32, float64, int32, int64.
1528-
name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
1529-
1530-
Returns:
1531-
Tensor. If x, y have different shapes and are "broadcastable", the resulting tensor shape is the shape of x and y after broadcasting. If x, y have the same shape, its shape is the same as x and y.
1532-
1533-
Examples:
1534-
1535-
.. code-block:: python
1536-
1537-
>>> import paddle
1538-
1539-
>>> x = paddle.to_tensor([[1, 2], [7, 8]])
1540-
>>> y = paddle.to_tensor([[3, 4], [5, 6]])
1541-
>>> res = paddle.minimum(x, y)
1542-
>>> print(res)
1543-
Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
1544-
[[1, 2],
1545-
[5, 6]])
1546-
1547-
>>> x = paddle.to_tensor([[[1, 2, 3], [1, 2, 3]]])
1548-
>>> y = paddle.to_tensor([3, 0, 4])
1549-
>>> res = paddle.minimum(x, y)
1550-
>>> print(res)
1551-
Tensor(shape=[1, 2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
1552-
[[[1, 0, 3],
1553-
[1, 0, 3]]])
1554-
1555-
>>> x = paddle.to_tensor([2, 3, 5], dtype='float32')
1556-
>>> y = paddle.to_tensor([1, float("nan"), float("nan")], dtype='float32')
1557-
>>> res = paddle.minimum(x, y)
1558-
>>> print(res)
1559-
Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
1560-
[1. , nan, nan])
1561-
1562-
>>> x = paddle.to_tensor([5, 3, float("inf")], dtype='float64')
1563-
>>> y = paddle.to_tensor([1, -float("inf"), 5], dtype='float64')
1564-
>>> res = paddle.minimum(x, y)
1565-
>>> print(res)
1566-
Tensor(shape=[3], dtype=float64, place=Place(cpu), stop_gradient=True,
1567-
[ 1. , -inf., 5. ])
1568-
"""
1569-
if in_dynamic_or_pir_mode():
1570-
return _C_ops.minimum(x, y)
1571-
else:
1572-
return _elementwise_op(LayerHelper('elementwise_min', **locals()))
1573-
1574-
15751453
def fmax(x: Tensor, y: Tensor, name: str | None = None) -> Tensor:
15761454
"""
15771455
Compares the elements at the corresponding positions of the two tensors and returns a new tensor containing the maximum value of the element.

test/deprecated/legacy_test/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,6 @@ else()
596596
endif()
597597

598598
set_tests_properties(test_argsort_op_deprecated PROPERTIES TIMEOUT 120)
599-
set_tests_properties(test_sgd_op_deprecated PROPERTIES TIMEOUT 250)
600599
set_tests_properties(test_generator_dataloader_deprecated PROPERTIES TIMEOUT
601600
120)
602601
set_tests_properties(test_decoupled_py_reader_deprecated PROPERTIES TIMEOUT 120)

0 commit comments

Comments
 (0)