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
10 changes: 10 additions & 0 deletions paddle/phi/ops/yaml/python_api_info.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@
args_alias :
use_default_mapping : True

- op : maximum
name : [paddle.maximum,paddle.Tensor.maximum]
args_alias :
use_default_mapping : True

- op : minimum
name : [paddle.minimum,paddle.Tensor.minimum]
args_alias :
use_default_mapping : True

- op : greater_than
name : [paddle.greater_than, paddle.Tensor.greater_than]
args_alias :
Expand Down
138 changes: 138 additions & 0 deletions python/paddle/_paddle_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,144 @@ def floor(
""",
)
# hehongyu
add_doc_and_signature(
"maximum",
"""
Compare two tensors and returns a new tensor containing the element-wise maxima. The equation is:

.. math::
out = max(x, y)

Note:
``paddle.maximum`` supports broadcasting. If you want know more about broadcasting, please refer to `Introduction to Tensor`_ .

.. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor

Args:
x (Tensor): the input tensor, it's data type should be bfloat16, float16, float32, float64, int32, int64.
y (Tensor): the input tensor, it's data type should be bfloat16, float16, float32, float64, int32, int64.
name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
out(Tensor, optional): The output tensor.

Returns:
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.

Examples:

.. code-block:: python

>>> import paddle

>>> x = paddle.to_tensor([[1, 2], [7, 8]])
>>> y = paddle.to_tensor([[3, 4], [5, 6]])
>>> res = paddle.maximum(x, y)
>>> print(res)
Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
[[3, 4],
[7, 8]])

>>> x = paddle.to_tensor([[1, 2, 3], [1, 2, 3]])
>>> y = paddle.to_tensor([3, 0, 4])
>>> res = paddle.maximum(x, y)
>>> print(res)
Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
[[3, 2, 4],
[3, 2, 4]])

>>> x = paddle.to_tensor([2, 3, 5], dtype='float32')
>>> y = paddle.to_tensor([1, float("nan"), float("nan")], dtype='float32')
>>> res = paddle.maximum(x, y)
>>> print(res)
Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
[2. , nan, nan])

>>> x = paddle.to_tensor([5, 3, float("inf")], dtype='float32')
>>> y = paddle.to_tensor([1, -float("inf"), 5], dtype='float32')
>>> res = paddle.maximum(x, y)
>>> print(res)
Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
[5. , 3. , inf.])
""",
"""
def maximum(
x: Tensor,
y: Tensor,
name: str | None = None,
*,
out: Tensor | None = None,
) -> Tensor
""",
)

add_doc_and_signature(
"minimum",
"""
Compare two tensors and return a new tensor containing the element-wise minima. The equation is:

.. math::
out = min(x, y)

Note:
``paddle.minimum`` supports broadcasting. If you want know more about broadcasting, please refer to `Introduction to Tensor`_ .

.. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor

Args:
x (Tensor): the input tensor, it's data type should be bfloat16, float16, float32, float64, int32, int64.
y (Tensor): the input tensor, it's data type should be bfloat16, float16, float32, float64, int32, int64.
name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
out(Tensor, optional): The output tensor.

Returns:
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.

Examples:

.. code-block:: python

>>> import paddle

>>> x = paddle.to_tensor([[1, 2], [7, 8]])
>>> y = paddle.to_tensor([[3, 4], [5, 6]])
>>> res = paddle.minimum(x, y)
>>> print(res)
Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
[[1, 2],
[5, 6]])

>>> x = paddle.to_tensor([[[1, 2, 3], [1, 2, 3]]])
>>> y = paddle.to_tensor([3, 0, 4])
>>> res = paddle.minimum(x, y)
>>> print(res)
Tensor(shape=[1, 2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
[[[1, 0, 3],
[1, 0, 3]]])

>>> x = paddle.to_tensor([2, 3, 5], dtype='float32')
>>> y = paddle.to_tensor([1, float("nan"), float("nan")], dtype='float32')
>>> res = paddle.minimum(x, y)
>>> print(res)
Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
[1. , nan, nan])

>>> x = paddle.to_tensor([5, 3, float("inf")], dtype='float64')
>>> y = paddle.to_tensor([1, -float("inf"), 5], dtype='float64')
>>> res = paddle.minimum(x, y)
>>> print(res)
Tensor(shape=[3], dtype=float64, place=Place(cpu), stop_gradient=True,
[ 1. , -inf., 5. ])
""",
"""
def minimum(
x: Tensor,
y: Tensor,
name: str | None = None,
*,
out: Tensor | None = None,
) -> Tensor
""",
)

add_doc_and_signature(
"sqrt",
"""
Expand Down
134 changes: 6 additions & 128 deletions python/paddle/tensor/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
isnan,
log,
logsumexp,
maximum,
minimum,
sign,
sin,
)
Expand Down Expand Up @@ -852,10 +854,10 @@ def logaddexp(x: Tensor, y: Tensor, name: str | None = None) -> Tensor:
[-0.30685282, -0.68673831, -0.87307199])
"""
log_1p = paddle.log1p(paddle.exp(-paddle.abs(x - y)))
maximum = paddle.maximum(x, y)
if maximum.dtype == paddle.int32 or maximum.dtype == paddle.int64:
maximum = maximum.astype(log_1p.dtype)
return log_1p + maximum
_maximum = paddle.maximum(x, y)
if _maximum.dtype == paddle.int32 or _maximum.dtype == paddle.int64:
_maximum = _maximum.astype(log_1p.dtype)
return log_1p + _maximum
Comment on lines +857 to +860
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

解决命名冲突



def subtract(x: Tensor, y: Tensor, name: str | None = None) -> Tensor:
Expand Down Expand Up @@ -1392,130 +1394,6 @@ def _divide_with_axis(x, y, axis=-1, name=None):
return _elementwise_op(LayerHelper(op_type, **locals()))


def maximum(x: Tensor, y: Tensor, name: str | None = None) -> Tensor:
"""
Compare two tensors and returns a new tensor containing the element-wise maxima. The equation is:

.. math::
out = max(x, y)

Note:
``paddle.maximum`` supports broadcasting. If you want know more about broadcasting, please refer to `Introduction to Tensor`_ .

.. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor

Args:
x (Tensor): the input tensor, it's data type should be bfloat16, float16, float32, float64, int32, int64.
y (Tensor): the input tensor, it's data type should be bfloat16, float16, float32, float64, int32, int64.
name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.

Returns:
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.

Examples:

.. code-block:: python

>>> import paddle

>>> x = paddle.to_tensor([[1, 2], [7, 8]])
>>> y = paddle.to_tensor([[3, 4], [5, 6]])
>>> res = paddle.maximum(x, y)
>>> print(res)
Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
[[3, 4],
[7, 8]])

>>> x = paddle.to_tensor([[1, 2, 3], [1, 2, 3]])
>>> y = paddle.to_tensor([3, 0, 4])
>>> res = paddle.maximum(x, y)
>>> print(res)
Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
[[3, 2, 4],
[3, 2, 4]])

>>> x = paddle.to_tensor([2, 3, 5], dtype='float32')
>>> y = paddle.to_tensor([1, float("nan"), float("nan")], dtype='float32')
>>> res = paddle.maximum(x, y)
>>> print(res)
Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
[2. , nan, nan])

>>> x = paddle.to_tensor([5, 3, float("inf")], dtype='float32')
>>> y = paddle.to_tensor([1, -float("inf"), 5], dtype='float32')
>>> res = paddle.maximum(x, y)
>>> print(res)
Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
[5. , 3. , inf.])
"""
if in_dynamic_or_pir_mode():
return _C_ops.maximum(x, y)
else:
return _elementwise_op(LayerHelper('elementwise_max', **locals()))


def minimum(x: Tensor, y: Tensor, name: str | None = None) -> Tensor:
"""
Compare two tensors and return a new tensor containing the element-wise minima. The equation is:

.. math::
out = min(x, y)

Note:
``paddle.minimum`` supports broadcasting. If you want know more about broadcasting, please refer to `Introduction to Tensor`_ .

.. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor

Args:
x (Tensor): the input tensor, it's data type should be bfloat16, float16, float32, float64, int32, int64.
y (Tensor): the input tensor, it's data type should be bfloat16, float16, float32, float64, int32, int64.
name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.

Returns:
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.

Examples:

.. code-block:: python

>>> import paddle

>>> x = paddle.to_tensor([[1, 2], [7, 8]])
>>> y = paddle.to_tensor([[3, 4], [5, 6]])
>>> res = paddle.minimum(x, y)
>>> print(res)
Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
[[1, 2],
[5, 6]])

>>> x = paddle.to_tensor([[[1, 2, 3], [1, 2, 3]]])
>>> y = paddle.to_tensor([3, 0, 4])
>>> res = paddle.minimum(x, y)
>>> print(res)
Tensor(shape=[1, 2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
[[[1, 0, 3],
[1, 0, 3]]])

>>> x = paddle.to_tensor([2, 3, 5], dtype='float32')
>>> y = paddle.to_tensor([1, float("nan"), float("nan")], dtype='float32')
>>> res = paddle.minimum(x, y)
>>> print(res)
Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
[1. , nan, nan])

>>> x = paddle.to_tensor([5, 3, float("inf")], dtype='float64')
>>> y = paddle.to_tensor([1, -float("inf"), 5], dtype='float64')
>>> res = paddle.minimum(x, y)
>>> print(res)
Tensor(shape=[3], dtype=float64, place=Place(cpu), stop_gradient=True,
[ 1. , -inf., 5. ])
"""
if in_dynamic_or_pir_mode():
return _C_ops.minimum(x, y)
else:
return _elementwise_op(LayerHelper('elementwise_min', **locals()))


def fmax(x: Tensor, y: Tensor, name: str | None = None) -> Tensor:
"""
Compares the elements at the corresponding positions of the two tensors and returns a new tensor containing the maximum value of the element.
Expand Down
1 change: 0 additions & 1 deletion test/deprecated/legacy_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,6 @@ else()
endif()

set_tests_properties(test_argsort_op_deprecated PROPERTIES TIMEOUT 120)
set_tests_properties(test_sgd_op_deprecated PROPERTIES TIMEOUT 250)
set_tests_properties(test_generator_dataloader_deprecated PROPERTIES TIMEOUT
120)
set_tests_properties(test_decoupled_py_reader_deprecated PROPERTIES TIMEOUT 120)
Expand Down
Loading