2222import paddle
2323from paddle import _C_ops
2424from paddle .common_ops_import import VarDesc , Variable
25- from paddle .utils .decorator_utils import ParamAliasDecorator
25+ from paddle .utils .decorator_utils import ParamAliasDecorator , param_one_alias
2626from paddle .utils .inplace_utils import inplace_apis_in_dygraph_only
2727
2828from ..base .data_feeder import check_dtype , check_variable_and_dtype
@@ -467,7 +467,8 @@ def nonzero(x: Tensor, as_tuple: Literal[True] = ...) -> tuple[Tensor, ...]: ...
467467def nonzero (x : Tensor , as_tuple : bool = ...) -> Tensor | tuple [Tensor , ...]: ...
468468
469469
470- def nonzero (x : Tensor , as_tuple = False ):
470+ @param_one_alias (['x' , 'input' ])
471+ def nonzero (x : Tensor , as_tuple = False , * , out : Tensor | None = None ):
471472 """
472473 Return a tensor containing the indices of all non-zero elements of the `input`
473474 tensor. If as_tuple is True, return a tuple of 1-D tensors, one for each dimension
@@ -477,9 +478,15 @@ def nonzero(x: Tensor, as_tuple=False):
477478 number of all non-zero elements in the `input` tensor. If as_tuple is True, we can get
478479 a 1-D tensor tuple of length `n`, and the shape of each 1-D tensor is [z, 1].
479480
481+ .. note::
482+ Alias Support: The parameter name ``input`` can be used as an alias for ``x``.
483+ For example, ``nonzero(input=tensor_x)`` is equivalent to ``nonzero(x=tensor_x)``.
484+
480485 Args:
481486 x (Tensor): The input tensor variable.
487+ alias: ``input``.
482488 as_tuple (bool, optional): Return type, Tensor or tuple of Tensor.
489+ out (Tensor|None, optional): The output tensor. Default: None.
483490
484491 Returns:
485492 Tensor or tuple of Tensor, The data type is int64.
@@ -504,14 +511,10 @@ def nonzero(x: Tensor, as_tuple=False):
504511 >>> out_z1_tuple = paddle.nonzero(x1, as_tuple=True)
505512 >>> for out in out_z1_tuple:
506513 ... print(out)
507- Tensor(shape=[3, 1], dtype=int64, place=Place(cpu), stop_gradient=True,
508- [[0],
509- [1],
510- [2]])
511- Tensor(shape=[3, 1], dtype=int64, place=Place(cpu), stop_gradient=True,
512- [[0],
513- [1],
514- [2]])
514+ Tensor(shape=[3], dtype=int64, place=Place(cpu), stop_gradient=True,
515+ [0, 1, 2])
516+ Tensor(shape=[3], dtype=int64, place=Place(cpu), stop_gradient=True,
517+ [0, 1, 2])
515518
516519 >>> out_z2 = paddle.nonzero(x2)
517520 >>> print(out_z2)
@@ -522,13 +525,12 @@ def nonzero(x: Tensor, as_tuple=False):
522525 >>> out_z2_tuple = paddle.nonzero(x2, as_tuple=True)
523526 >>> for out in out_z2_tuple:
524527 ... print(out)
525- Tensor(shape=[2, 1], dtype=int64, place=Place(cpu), stop_gradient=True,
526- [[1],
527- [3]])
528+ Tensor(shape=[2], dtype=int64, place=Place(cpu), stop_gradient=True,
529+ [1, 3])
528530
529531 """
530532 if in_dynamic_or_pir_mode ():
531- outs = _C_ops .nonzero (x )
533+ outs = _C_ops .nonzero (x , out = out )
532534 else :
533535 check_variable_and_dtype (
534536 x ,
0 commit comments