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

- op : isfinite
args : (Tensor x)
python_api:
name : [paddle.isfinite, paddle.Tensor.isfinite]
args_alias:
use_default_mapping : True
output : Tensor(out)
infer_meta :
func : IsfiniteInferMeta
Expand All @@ -2959,6 +2963,10 @@

- op : isinf
args : (Tensor x)
python_api:
name : [paddle.isinf, paddle.Tensor.isinf]
args_alias:
use_default_mapping : True
output : Tensor(out)
infer_meta :
func : IsfiniteInferMeta
Expand All @@ -2970,6 +2978,10 @@

- op : isnan
args : (Tensor x)
python_api:
name : [paddle.isnan, paddle.Tensor.isnan]
args_alias:
use_default_mapping : True
output : Tensor(out)
infer_meta :
func : IsfiniteInferMeta
Expand Down
103 changes: 102 additions & 1 deletion python/paddle/_paddle_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ def amax(
) -> Tensor
""",
)

add_doc_and_signature(
"all",
"""
Expand Down Expand Up @@ -354,7 +355,6 @@ def amax(
.. code-block:: python
>>> # type: ignore
>>> import paddle

>>> # x is a bool Tensor with following elements:
>>> # [[True, False]
>>> # [True, True]]
Expand Down Expand Up @@ -402,6 +402,107 @@ def all(
)

# zhengsheng
add_doc_and_signature(
"isfinite",
"""
Return whether every element of input tensor is finite number or not.

.. note::
Alias Support: The parameter name ``input`` can be used as an alias for ``x``.
For example, ``isfinite(input=tensor_x)`` is equivalent to ``isfinite(x=tensor_x)``.

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

Returns:
`Tensor`, the bool result which shows every element of `x` whether it is finite number or not.
>>> x = paddle.to_tensor([float('-inf'), -2, 3.6, float('inf'), 0, float('-nan'), float('nan')])
>>> out = paddle.isfinite(x)
>>> out
Tensor(shape=[7], dtype=bool, place=Place(cpu), stop_gradient=True,
[False, True , True , False, True , False, False])
""",
"""
def isfinite(
x: Tensor,
name: str | None = None,
) -> Tensor
""",
)

add_doc_and_signature(
"isinf",
"""
Return whether every element of input tensor is `+/-INF` or not.

.. note::
Alias Support: The parameter name ``input`` can be used as an alias for ``x``.
For example, ``isinf(input=tensor_x)`` is equivalent to ``isinf(x=tensor_x)``.

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

Returns:
`Tensor`, the bool result which shows every element of `x` whether it is `+/-INF` or not.

Examples:
.. code-block:: python
>>> # type: ignore
>>> import paddle

>>> x = paddle.to_tensor([float('-inf'), -2, 3.6, float('inf'), 0, float('-nan'), float('nan')])
>>> out = paddle.isinf(x)
>>> out
Tensor(shape=[7], dtype=bool, place=Place(cpu), stop_gradient=True,
[True , False, False, True , False, False, False])
""",
"""
def isinf(
x: Tensor,
name: str | None = None,
) -> Tensor
""",
)

add_doc_and_signature(
"isnan",
"""
Return whether every element of input tensor is `NaN` or not.

.. note::
Alias Support: The parameter name ``input`` can be used as an alias for ``x``.
For example, ``isnan(input=tensor_x)`` is equivalent to ``isnan(x=tensor_x)``.

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

Returns:
`Tensor`, the bool result which shows every element of `x` whether it is `NaN` or not.

Examples:
.. code-block:: python
>>> # type: ignore
>>> import paddle

>>> x = paddle.to_tensor([float('-inf'), -2, 3.6, float('inf'), 0, float('-nan'), float('nan')])
>>> out = paddle.isnan(x)
>>> out
Tensor(shape=[7], dtype=bool, place=Place(cpu), stop_gradient=True,
[False, False, False, False, False, True , True ])
""",
"""
def isnan(
x: Tensor,
name: str | None = None,
) -> Tensor
""",
)

# liuyi

Expand Down
149 changes: 3 additions & 146 deletions python/paddle/tensor/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
all,
amax,
amin,
isfinite,
isinf,
isnan,
)
from paddle.base.libpaddle import DataType
from paddle.common_ops_import import VarDesc, dygraph_utils
Expand Down Expand Up @@ -4654,152 +4657,6 @@ def cumprod_(
return _C_ops.cumprod_(x, dim, False, False)


def isfinite(x: Tensor, name: str | None = None) -> Tensor:
"""

Return whether every element of input tensor is finite number or not.

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

Returns:
`Tensor`, the bool result which shows every element of `x` whether it is finite number or not.

Examples:
.. code-block:: python

>>> import paddle

>>> x = paddle.to_tensor([float('-inf'), -2, 3.6, float('inf'), 0, float('-nan'), float('nan')])
>>> out = paddle.isfinite(x)
>>> out
Tensor(shape=[7], dtype=bool, place=Place(cpu), stop_gradient=True,
[False, True , True , False, True , False, False])
"""
if in_dynamic_or_pir_mode():
return _C_ops.isfinite(x)
else:
helper = LayerHelper("isfinite_v2", **locals())
check_variable_and_dtype(
x,
'x',
[
'float16',
'float32',
'float64',
'int32',
'int64',
'uint16',
'complex64',
'complex128',
],
'isfinite',
)
out = helper.create_variable_for_type_inference('bool')
helper.append_op(
type="isfinite_v2", inputs={"X": x}, outputs={"Out": out}
)
return out


def isinf(x: Tensor, name: str | None = None) -> Tensor:
"""

Return whether every element of input tensor is `+/-INF` or not.

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

Returns:
`Tensor`, the bool result which shows every element of `x` whether it is `+/-INF` or not.

Examples:
.. code-block:: python

>>> import paddle

>>> x = paddle.to_tensor([float('-inf'), -2, 3.6, float('inf'), 0, float('-nan'), float('nan')])
>>> out = paddle.isinf(x)
>>> out
Tensor(shape=[7], dtype=bool, place=Place(cpu), stop_gradient=True,
[True , False, False, True , False, False, False])
"""
if in_dynamic_or_pir_mode():
return _C_ops.isinf(x)
else:
helper = LayerHelper("isinf_v2", **locals())
check_variable_and_dtype(
x,
'x',
[
'float16',
'float32',
'float64',
'int8',
'int16',
'int32',
'int64',
'uint8',
'uint16',
'complex64',
'complex128',
],
'isinf',
)
out = helper.create_variable_for_type_inference(dtype='bool')
helper.append_op(type="isinf_v2", inputs={"X": x}, outputs={"Out": out})
return out


def isnan(x: Tensor, name: str | None = None) -> Tensor:
"""

Return whether every element of input tensor is `NaN` or not.

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

Returns:
`Tensor`, the bool result which shows every element of `x` whether it is `NaN` or not.

Examples:
.. code-block:: python

>>> import paddle

>>> x = paddle.to_tensor([float('-inf'), -2, 3.6, float('inf'), 0, float('-nan'), float('nan')])
>>> out = paddle.isnan(x)
>>> out
Tensor(shape=[7], dtype=bool, place=Place(cpu), stop_gradient=True,
[False, False, False, False, False, True , True ])
"""
if in_dynamic_or_pir_mode():
return _C_ops.isnan(x)
else:
helper = LayerHelper("isnan_v2", **locals())
check_variable_and_dtype(
x,
'x',
[
'float16',
'float32',
'float64',
'int32',
'int64',
'uint16',
'complex64',
'complex128',
],
'isnan',
)
out = helper.create_variable_for_type_inference(dtype='bool')
helper.append_op(type="isnan_v2", inputs={"X": x}, outputs={"Out": out})
return out


@param_two_alias(["x", "input"], ["axis", "dim"])
def prod(
x: Tensor,
Expand Down
Loading