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

- op : any
args : (Tensor x, int64_t[] axis={}, bool keepdim=false)
python_api:
name : [paddle.any, paddle.Tensor.any]
args_alias:
use_default_mapping : True
output : Tensor(out)
infer_meta :
func : ReduceInferMeta
Expand Down
82 changes: 82 additions & 0 deletions python/paddle/_paddle_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,88 @@ def isnan(
)

# liuyi
add_doc_and_signature(
"any",
"""
Computes the ``logical or`` of tensor elements over the given dimension, and return the result.

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

Args:
x (Tensor): An N-D Tensor, the input data type should be 'bool', 'float32', 'float64', 'int32', 'int64', 'complex64', 'complex128'.
alias: ``input``.
axis (int|list|tuple|None, optional): The dimensions along which the ``logical or`` is compute. If
:attr:`None`, and all elements of :attr:`x` and return a
Tensor with a single element, otherwise must be in the
range :math:`[-rank(x), rank(x))`. If :math:`axis[i] < 0`,
the dimension to reduce is :math:`rank + axis[i]`.
alias: ``dim``.
keepdim (bool, optional): Whether to reserve the reduced dimension in the
output Tensor. The result Tensor will have one fewer dimension
than the :attr:`x` unless :attr:`keepdim` is true, default
value is False.
name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
out (Tensor|None, optional): The output tensor. Default: None.

Returns:
Tensor: Results the ``logical or`` on the specified axis of input Tensor `x`, it's data type is bool.

Examples:
.. code-block:: python

>>> import paddle
>>> # type: ignore

>>> x = paddle.to_tensor([[1, 0], [1, 1]], dtype='int32')
>>> x = paddle.assign(x)
>>> x
Tensor(shape=[2, 2], dtype=int32, place=Place(cpu), stop_gradient=True,
[[1, 0],
[1, 1]])
>>> x = paddle.cast(x, 'bool')
>>> # x is a bool Tensor with following elements:
>>> # [[True, False]
>>> # [True, True]]

>>> # out1 should be True
>>> out1 = paddle.any(x)
>>> out1
Tensor(shape=[], dtype=bool, place=Place(cpu), stop_gradient=True,
True)

>>> # out2 should be [True, True]
>>> out2 = paddle.any(x, axis=0)
>>> out2
Tensor(shape=[2], dtype=bool, place=Place(cpu), stop_gradient=True,
[True, True])

>>> # keepdim=False, out3 should be [True, True], out.shape should be (2,)
>>> out3 = paddle.any(x, axis=-1)
>>> out3
Tensor(shape=[2], dtype=bool, place=Place(cpu), stop_gradient=True,
[True, True])

>>> # keepdim=True, result should be [[True], [True]], out.shape should be (2,1)
>>> out4 = paddle.any(x, axis=1, keepdim=True)
>>> out4
Tensor(shape=[2, 1], dtype=bool, place=Place(cpu), stop_gradient=True,
[[True],
[True]])

""",
"""
def any(
x: Tensor,
axis: int | Sequence[int] | None = None,
keepdim: bool = False,
name: str | None = None,
*,
out: Tensor | None = None
) -> Tensor
""",
)

# shenwei

Expand Down
7 changes: 7 additions & 0 deletions python/paddle/tensor/manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4536,6 +4536,7 @@ def scatter_nd(
return scatter_nd_add(zeros(shape, updates.dtype), index, updates, name)


@ParamAliasDecorator({"x": ["input"], "axis": ["dim"]})
def chunk(
x: Tensor, chunks: int, axis: int | Tensor = 0, name: str | None = None
) -> list[Tensor]:
Expand All @@ -4555,12 +4556,18 @@ def chunk(
:alt: legend of reshape API
:align: center

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

Args:
x (Tensor): A N-D Tensor. The data type is bool, float16, float32, float64, int32 or int64.
alias: ``input``.
chunks(int): The number of tensor to be split along the certain axis.
axis (int|Tensor, optional): The axis along which to split, it can be a integer or a ``0-D Tensor``
with shape [] and data type ``int32`` or ``int64``.
If :math::`axis < 0`, the axis to split along is :math:`rank(x) + axis`. Default is 0.
alias: ``dim``.
name (str|None, optional): The default value is None. Normally there is no need for user to set this property.
For more information, please refer to :ref:`api_guide_Name` .
Returns:
Expand Down
104 changes: 1 addition & 103 deletions python/paddle/tensor/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
all,
amax,
amin,
any,
isfinite,
isinf,
isnan,
Expand Down Expand Up @@ -4952,109 +4953,6 @@ def increment(x: Tensor, value: float = 1.0, name: str | None = None) -> Tensor:
return x


def any(
x: Tensor,
axis: int | Sequence[int] | None = None,
keepdim: bool = False,
name: str | None = None,
) -> Tensor:
"""
Computes the ``logical or`` of tensor elements over the given dimension, and return the result.

Args:
x (Tensor): An N-D Tensor, the input data type should be 'bool', 'float32', 'float64', 'int32', 'int64', 'complex64', 'complex128'.
axis (int|list|tuple|None, optional): The dimensions along which the ``logical or`` is compute. If
:attr:`None`, and all elements of :attr:`x` and return a
Tensor with a single element, otherwise must be in the
range :math:`[-rank(x), rank(x))`. If :math:`axis[i] < 0`,
the dimension to reduce is :math:`rank + axis[i]`.
keepdim (bool, optional): Whether to reserve the reduced dimension in the
output Tensor. The result Tensor will have one fewer dimension
than the :attr:`x` unless :attr:`keepdim` is true, default
value is False.
name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.

Returns:
Tensor: Results the ``logical or`` on the specified axis of input Tensor `x`, it's data type is bool.

Examples:
.. code-block:: python

>>> import paddle

>>> x = paddle.to_tensor([[1, 0], [1, 1]], dtype='int32')
>>> x = paddle.assign(x)
>>> x
Tensor(shape=[2, 2], dtype=int32, place=Place(cpu), stop_gradient=True,
[[1, 0],
[1, 1]])
>>> x = paddle.cast(x, 'bool')
>>> # x is a bool Tensor with following elements:
>>> # [[True, False]
>>> # [True, True]]

>>> # out1 should be True
>>> out1 = paddle.any(x)
>>> out1
Tensor(shape=[], dtype=bool, place=Place(cpu), stop_gradient=True,
True)

>>> # out2 should be [True, True]
>>> out2 = paddle.any(x, axis=0)
>>> out2
Tensor(shape=[2], dtype=bool, place=Place(cpu), stop_gradient=True,
[True, True])

>>> # keepdim=False, out3 should be [True, True], out.shape should be (2,)
>>> out3 = paddle.any(x, axis=-1)
>>> out3
Tensor(shape=[2], dtype=bool, place=Place(cpu), stop_gradient=True,
[True, True])

>>> # keepdim=True, result should be [[True], [True]], out.shape should be (2,1)
>>> out4 = paddle.any(x, axis=1, keepdim=True)
>>> out4
Tensor(shape=[2, 1], dtype=bool, place=Place(cpu), stop_gradient=True,
[[True],
[True]])

"""
if in_dynamic_or_pir_mode():
return _C_ops.any(x, axis, keepdim)
else:
reduce_all, axis = _get_reduce_axis(axis, x)
attrs = {
'dim': axis,
'keep_dim': keepdim,
'reduce_all': reduce_all,
}
check_variable_and_dtype(
x,
'x',
[
'bool',
'float32',
'float64',
'int32',
'int64',
'complex64',
'complex128',
],
'any',
)
check_type(axis, 'axis', (int, list, tuple, type(None)), 'any')

helper = LayerHelper('any', **locals())
out = helper.create_variable_for_type_inference(dtype=paddle.bool)
helper.append_op(
type='reduce_any',
inputs={'X': x},
outputs={'Out': out},
attrs=attrs,
)
return out


def broadcast_shapes(*shapes: Sequence[int]) -> list[int]:
"""
The function returns the shape of doing operation with broadcasting on tensors of shape list.
Expand Down
19 changes: 16 additions & 3 deletions python/paddle/tensor/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
__all__ = []


@ParamAliasDecorator({"x": ["input"], "axis": ["dim"]})
def argsort(
x: Tensor,
axis: int = -1,
Expand All @@ -57,12 +58,18 @@ def argsort(
"""
Sorts the input along the given axis, and returns the corresponding index tensor for the sorted output values. The default sort algorithm is ascending, if you want the sort algorithm to be descending, you must set the :attr:`descending` as True.

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

Args:
x (Tensor): An input N-D Tensor with type bfloat16, float16, float32, float64, int16,
int32, int64, uint8.
alias: ``input``.
axis (int, optional): Axis to compute indices along. The effective range
is [-R, R), where R is Rank(x). when axis<0, it works the same way
as axis+R. Default is -1.
alias: ``dim``.
descending (bool, optional) : Descending is a flag, if set to true,
algorithm will sort by descending order, else sort by
ascending order. Default is false.
Expand Down Expand Up @@ -456,15 +463,21 @@ def index_select(


@overload
def nonzero(x: Tensor, as_tuple: Literal[False] = ...) -> Tensor: ...
def nonzero(
x: Tensor, as_tuple: Literal[False] = ..., *, out: Tensor | None = None
) -> Tensor: ...


@overload
def nonzero(x: Tensor, as_tuple: Literal[True] = ...) -> tuple[Tensor, ...]: ...
def nonzero(
x: Tensor, as_tuple: Literal[True] = ..., *, out: Tensor | None = None
) -> tuple[Tensor, ...]: ...


@overload
def nonzero(x: Tensor, as_tuple: bool = ...) -> Tensor | tuple[Tensor, ...]: ...
def nonzero(
x: Tensor, as_tuple: bool = ..., *, out: Tensor | None = None
) -> Tensor | tuple[Tensor, ...]: ...


@param_one_alias(['x', 'input'])
Expand Down
Loading
Loading