Skip to content

Commit 955e32c

Browse files
authored
[API compatibility] argsort, chunk, any (#74735)
* [API compatibility] argsort, chunk, any * delete, type ignore and fix TestChunkOpError * delete old ir test * add out test
1 parent 627693b commit 955e32c

File tree

9 files changed

+507
-230
lines changed

9 files changed

+507
-230
lines changed

paddle/phi/ops/yaml/ops.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,10 @@
282282

283283
- op : any
284284
args : (Tensor x, int64_t[] axis={}, bool keepdim=false)
285+
python_api:
286+
name : [paddle.any, paddle.Tensor.any]
287+
args_alias:
288+
use_default_mapping : True
285289
output : Tensor(out)
286290
infer_meta :
287291
func : ReduceInferMeta

python/paddle/_paddle_docs.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,88 @@ def isnan(
505505
)
506506

507507
# liuyi
508+
add_doc_and_signature(
509+
"any",
510+
"""
511+
Computes the ``logical or`` of tensor elements over the given dimension, and return the result.
512+
513+
.. note::
514+
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``.
515+
For example, ``any(input=tensor_x, dim=1)`` is equivalent to ``any(x=tensor_x, axis=1)``.
516+
517+
Args:
518+
x (Tensor): An N-D Tensor, the input data type should be 'bool', 'float32', 'float64', 'int32', 'int64', 'complex64', 'complex128'.
519+
alias: ``input``.
520+
axis (int|list|tuple|None, optional): The dimensions along which the ``logical or`` is compute. If
521+
:attr:`None`, and all elements of :attr:`x` and return a
522+
Tensor with a single element, otherwise must be in the
523+
range :math:`[-rank(x), rank(x))`. If :math:`axis[i] < 0`,
524+
the dimension to reduce is :math:`rank + axis[i]`.
525+
alias: ``dim``.
526+
keepdim (bool, optional): Whether to reserve the reduced dimension in the
527+
output Tensor. The result Tensor will have one fewer dimension
528+
than the :attr:`x` unless :attr:`keepdim` is true, default
529+
value is False.
530+
name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
531+
out (Tensor|None, optional): The output tensor. Default: None.
532+
533+
Returns:
534+
Tensor: Results the ``logical or`` on the specified axis of input Tensor `x`, it's data type is bool.
535+
536+
Examples:
537+
.. code-block:: python
538+
539+
>>> import paddle
540+
>>> # type: ignore
541+
542+
>>> x = paddle.to_tensor([[1, 0], [1, 1]], dtype='int32')
543+
>>> x = paddle.assign(x)
544+
>>> x
545+
Tensor(shape=[2, 2], dtype=int32, place=Place(cpu), stop_gradient=True,
546+
[[1, 0],
547+
[1, 1]])
548+
>>> x = paddle.cast(x, 'bool')
549+
>>> # x is a bool Tensor with following elements:
550+
>>> # [[True, False]
551+
>>> # [True, True]]
552+
553+
>>> # out1 should be True
554+
>>> out1 = paddle.any(x)
555+
>>> out1
556+
Tensor(shape=[], dtype=bool, place=Place(cpu), stop_gradient=True,
557+
True)
558+
559+
>>> # out2 should be [True, True]
560+
>>> out2 = paddle.any(x, axis=0)
561+
>>> out2
562+
Tensor(shape=[2], dtype=bool, place=Place(cpu), stop_gradient=True,
563+
[True, True])
564+
565+
>>> # keepdim=False, out3 should be [True, True], out.shape should be (2,)
566+
>>> out3 = paddle.any(x, axis=-1)
567+
>>> out3
568+
Tensor(shape=[2], dtype=bool, place=Place(cpu), stop_gradient=True,
569+
[True, True])
570+
571+
>>> # keepdim=True, result should be [[True], [True]], out.shape should be (2,1)
572+
>>> out4 = paddle.any(x, axis=1, keepdim=True)
573+
>>> out4
574+
Tensor(shape=[2, 1], dtype=bool, place=Place(cpu), stop_gradient=True,
575+
[[True],
576+
[True]])
577+
578+
""",
579+
"""
580+
def any(
581+
x: Tensor,
582+
axis: int | Sequence[int] | None = None,
583+
keepdim: bool = False,
584+
name: str | None = None,
585+
*,
586+
out: Tensor | None = None
587+
) -> Tensor
588+
""",
589+
)
508590

509591
# shenwei
510592

python/paddle/tensor/manipulation.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4543,6 +4543,7 @@ def scatter_nd(
45434543
return scatter_nd_add(zeros(shape, updates.dtype), index, updates, name)
45444544

45454545

4546+
@ParamAliasDecorator({"x": ["input"], "axis": ["dim"]})
45464547
def chunk(
45474548
x: Tensor, chunks: int, axis: int | Tensor = 0, name: str | None = None
45484549
) -> list[Tensor]:
@@ -4562,12 +4563,18 @@ def chunk(
45624563
:alt: legend of reshape API
45634564
:align: center
45644565
4566+
.. note::
4567+
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``.
4568+
For example, ``chunk(input=tensor_x, dim=1)`` is equivalent to ``chunk(x=tensor_x, axis=1)``.
4569+
45654570
Args:
45664571
x (Tensor): A N-D Tensor. The data type is bool, float16, float32, float64, int32 or int64.
4572+
alias: ``input``.
45674573
chunks(int): The number of tensor to be split along the certain axis.
45684574
axis (int|Tensor, optional): The axis along which to split, it can be a integer or a ``0-D Tensor``
45694575
with shape [] and data type ``int32`` or ``int64``.
45704576
If :math::`axis < 0`, the axis to split along is :math:`rank(x) + axis`. Default is 0.
4577+
alias: ``dim``.
45714578
name (str|None, optional): The default value is None. Normally there is no need for user to set this property.
45724579
For more information, please refer to :ref:`api_guide_Name` .
45734580
Returns:

python/paddle/tensor/math.py

Lines changed: 1 addition & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
all,
2727
amax,
2828
amin,
29+
any,
2930
isfinite,
3031
isinf,
3132
isnan,
@@ -4977,109 +4978,6 @@ def increment(x: Tensor, value: float = 1.0, name: str | None = None) -> Tensor:
49774978
return x
49784979

49794980

4980-
def any(
4981-
x: Tensor,
4982-
axis: int | Sequence[int] | None = None,
4983-
keepdim: bool = False,
4984-
name: str | None = None,
4985-
) -> Tensor:
4986-
"""
4987-
Computes the ``logical or`` of tensor elements over the given dimension, and return the result.
4988-
4989-
Args:
4990-
x (Tensor): An N-D Tensor, the input data type should be 'bool', 'float32', 'float64', 'int32', 'int64', 'complex64', 'complex128'.
4991-
axis (int|list|tuple|None, optional): The dimensions along which the ``logical or`` is compute. If
4992-
:attr:`None`, and all elements of :attr:`x` and return a
4993-
Tensor with a single element, otherwise must be in the
4994-
range :math:`[-rank(x), rank(x))`. If :math:`axis[i] < 0`,
4995-
the dimension to reduce is :math:`rank + axis[i]`.
4996-
keepdim (bool, optional): Whether to reserve the reduced dimension in the
4997-
output Tensor. The result Tensor will have one fewer dimension
4998-
than the :attr:`x` unless :attr:`keepdim` is true, default
4999-
value is False.
5000-
name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
5001-
5002-
Returns:
5003-
Tensor: Results the ``logical or`` on the specified axis of input Tensor `x`, it's data type is bool.
5004-
5005-
Examples:
5006-
.. code-block:: python
5007-
5008-
>>> import paddle
5009-
5010-
>>> x = paddle.to_tensor([[1, 0], [1, 1]], dtype='int32')
5011-
>>> x = paddle.assign(x)
5012-
>>> x
5013-
Tensor(shape=[2, 2], dtype=int32, place=Place(cpu), stop_gradient=True,
5014-
[[1, 0],
5015-
[1, 1]])
5016-
>>> x = paddle.cast(x, 'bool')
5017-
>>> # x is a bool Tensor with following elements:
5018-
>>> # [[True, False]
5019-
>>> # [True, True]]
5020-
5021-
>>> # out1 should be True
5022-
>>> out1 = paddle.any(x)
5023-
>>> out1
5024-
Tensor(shape=[], dtype=bool, place=Place(cpu), stop_gradient=True,
5025-
True)
5026-
5027-
>>> # out2 should be [True, True]
5028-
>>> out2 = paddle.any(x, axis=0)
5029-
>>> out2
5030-
Tensor(shape=[2], dtype=bool, place=Place(cpu), stop_gradient=True,
5031-
[True, True])
5032-
5033-
>>> # keepdim=False, out3 should be [True, True], out.shape should be (2,)
5034-
>>> out3 = paddle.any(x, axis=-1)
5035-
>>> out3
5036-
Tensor(shape=[2], dtype=bool, place=Place(cpu), stop_gradient=True,
5037-
[True, True])
5038-
5039-
>>> # keepdim=True, result should be [[True], [True]], out.shape should be (2,1)
5040-
>>> out4 = paddle.any(x, axis=1, keepdim=True)
5041-
>>> out4
5042-
Tensor(shape=[2, 1], dtype=bool, place=Place(cpu), stop_gradient=True,
5043-
[[True],
5044-
[True]])
5045-
5046-
"""
5047-
if in_dynamic_or_pir_mode():
5048-
return _C_ops.any(x, axis, keepdim)
5049-
else:
5050-
reduce_all, axis = _get_reduce_axis(axis, x)
5051-
attrs = {
5052-
'dim': axis,
5053-
'keep_dim': keepdim,
5054-
'reduce_all': reduce_all,
5055-
}
5056-
check_variable_and_dtype(
5057-
x,
5058-
'x',
5059-
[
5060-
'bool',
5061-
'float32',
5062-
'float64',
5063-
'int32',
5064-
'int64',
5065-
'complex64',
5066-
'complex128',
5067-
],
5068-
'any',
5069-
)
5070-
check_type(axis, 'axis', (int, list, tuple, type(None)), 'any')
5071-
5072-
helper = LayerHelper('any', **locals())
5073-
out = helper.create_variable_for_type_inference(dtype=paddle.bool)
5074-
helper.append_op(
5075-
type='reduce_any',
5076-
inputs={'X': x},
5077-
outputs={'Out': out},
5078-
attrs=attrs,
5079-
)
5080-
return out
5081-
5082-
50834981
def broadcast_shapes(*shapes: Sequence[int]) -> list[int]:
50844982
"""
50854983
The function returns the shape of doing operation with broadcasting on tensors of shape list.

python/paddle/tensor/search.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
__all__ = []
4848

4949

50+
@ParamAliasDecorator({"x": ["input"], "axis": ["dim"]})
5051
def argsort(
5152
x: Tensor,
5253
axis: int = -1,
@@ -57,12 +58,18 @@ def argsort(
5758
"""
5859
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.
5960
61+
.. note::
62+
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``.
63+
For example, ``argsort(input=tensor_x, dim=1)`` is equivalent to ``(x=tensor_x, axis=1)``.
64+
6065
Args:
6166
x (Tensor): An input N-D Tensor with type bfloat16, float16, float32, float64, int16,
6267
int32, int64, uint8.
68+
alias: ``input``.
6369
axis (int, optional): Axis to compute indices along. The effective range
6470
is [-R, R), where R is Rank(x). when axis<0, it works the same way
6571
as axis+R. Default is -1.
72+
alias: ``dim``.
6673
descending (bool, optional) : Descending is a flag, if set to true,
6774
algorithm will sort by descending order, else sort by
6875
ascending order. Default is false.
@@ -456,15 +463,21 @@ def index_select(
456463

457464

458465
@overload
459-
def nonzero(x: Tensor, as_tuple: Literal[False] = ...) -> Tensor: ...
466+
def nonzero(
467+
x: Tensor, as_tuple: Literal[False] = ..., *, out: Tensor | None = None
468+
) -> Tensor: ...
460469

461470

462471
@overload
463-
def nonzero(x: Tensor, as_tuple: Literal[True] = ...) -> tuple[Tensor, ...]: ...
472+
def nonzero(
473+
x: Tensor, as_tuple: Literal[True] = ..., *, out: Tensor | None = None
474+
) -> tuple[Tensor, ...]: ...
464475

465476

466477
@overload
467-
def nonzero(x: Tensor, as_tuple: bool = ...) -> Tensor | tuple[Tensor, ...]: ...
478+
def nonzero(
479+
x: Tensor, as_tuple: bool = ..., *, out: Tensor | None = None
480+
) -> Tensor | tuple[Tensor, ...]: ...
468481

469482

470483
@param_one_alias(['x', 'input'])

0 commit comments

Comments
 (0)