
    ёiC                      % S SK Jr  S SKrS SKrS SKrS SKJrJrJr  S SK	r
S SKJr  S SKrS SKJr  S SKJrJrJr  S SKJr  S SKJrJrJrJrJrJrJrJrJr  S S	KJ r   S
SK!J"r"J#r#J$r$J%r%  S
SK&J'r'J(r(  S
SK)J*r*J+r+J,r,J-r-J.r.J/r/J0r0  SSK1J2r2J3r3J4r4  \(       a$  S SK5J6r6J7r7  S SK8J9r9  S SKJ:r:  S SK;J<r<J=r=J>r>J?r?J@r@JArA  S SKJBrB  S SKJCrC  / rD   S         SS jjrESS jrF\ SS j5       rG          SS jrH          SS jrI S       SS jjrJSSS  jjrK S           SS! jjrL   S         SS" jjrM\-SS# j5       rN\-SS$ j5       rO\-   S           SS% jj5       rP    S             SS& jjrQ    S             SS' jjrR    S             SS( jjrS\-SS) j5       rT\" S*/S/S+.5        SSS,.         SS- jjj5       rU S     SS. jjrV S       SS/ jjrWSS S/S4         SS0 jjrX\" S/S1/S2/S3.5       S         SS4 jj5       rYSS5 jrZ\  S         SS6 jj5       r[\" S*/S/S+.5        SSS,.         SS7 jjj5       r\SSS8 jjr]SSS9 jjr^SSS: jjr_SSS; jjr`SSS< jjra\C" 1 S=kS>S?S@SA9  S         SSB jj5       rb\  S         SSC jj5       rc S       SSD jjrd S       SSE jjre S       SSF jjrf\" SS/SS/5       S       SSG jj5       rg\  S       SSH jj5       rh\" SS/SS/5           S             SSI jj5       ri\       S                 SSK jj5       rj\       S                 SSL jj5       rj\       S                 SSM jj5       rj\       S                 SSN jj5       rj\       S                 SSO jj5       rj\       S                 SSP jj5       rj\       S                 SSQ jj5       rj\       S                 SSR jj5       rj\       S                 SSS jj5       rj       SSU jrj\" SS/SS/5       S       SSV jj5       rk\  S       SSW jj5       rl S         SSX jjrm   S           SSY jjrn\   S           SSZ jj5       ro\ S         SS[ jj5       roSS\ jro\R                  " \n5      \olq        \" SS/5      SSS] jj5       rr   S             SS^ jjrs  S           SS_ jjrt\  S           SS` jj5       ru\   S             SSa jj5       ru\ SSb j5       ru\R                  " \t5      \ulp           S             SSc jjrv    S               SSd jjrw\   S             SSe jj5       rx\    S               SSf jj5       rxSSg jrx\R                  " \v5      \xlq         S         SSh jjry S         SSi jjrz\" S/S/S+.5       S         SSj jj5       r{\" S/Sk/Sl.5       S       SSm jj5       r|\" Sn5            SSo j5       r}\" S/Sp/Sq.5       S       SSr jj5       r~\" 5       SSSs jj5       r\" 5       SSSt jj5       r S         SSu jjr\  S         SSv jj5       r\ SSSw jj5       r\SSSx jj5       r\SJSy.SSz jj5       rSSy.S{ jr\SSS| jj5       r\SJSy.SS} jj5       rSSy.S~ jr\SSS jj5       r\SJSy.SS jj5       rSSy.S jrSSS jjr S             SS jjr  S         SS jjrSSS jjrSSS jjrSS jrSS jr\" SS/SS/5        SSS.           SS jjj5       r S         SS jjr S       GS S jjr\  S       GS S jj5       r\GSS j5       r\GSS j5       rS r          GSS jr        GSS jr          GSS jr\" S/S/S.5       GSSS,.           GSS jjj5       rSTS.             GSS jjr   GS	               GS
S jjr\    GS	             GSS jj5       r          GSS jr\" 5         GSSS,.               GSS jjj5       r\" 5       \   GS             GSS jj5       5       r\" S/S/S/S.5       S         GSS jj5       r\-  S           GSS jj5       r\-\" 5        S       GSS jj5       5       r\-SGSS jj5       r\-\" SS/5       S           GSS jj5       5       r\N\O\P\R\S\TS.rS\S'   \GRI                  5        H%  u  rr\" \,GRP                  Rt                  \\5        M'                 GSS jr S         GSS jjr\  S         GSS jj5       r    S             GSS jjr S           GSS jjr S               GSS jjrSGSS jjrg(      )annotationsN)TYPE_CHECKINGAnyLiteral)overload)_C_ops)	index_put
index_put_roll)fill_constant)	ParamAliasDecoratorVariableArgsDecoratorexpand_decoratorindex_add_decoratorparam_one_aliasparam_two_aliasreshape_decoratortensor_split_decoratorview_decorator)inplace_apis_in_dygraph_only   )check_dtype
check_typecheck_variable_and_dtypeconvert_dtype)Variabledefault_main_program)LayerHelperconvert_np_dtype_to_dtype_coredygraph_onlyin_dynamic_modein_dynamic_or_pir_modein_pir_mode   )_complex_to_real_dtype_real_to_complex_dtypezeros)CallableSequence)Number)Tensor)	DTypeLike
NestedListNestedSequenceNumeric	ShapeLikeTensorOrTensors)	expand_as)ForbidKeywordsDecoratorinputaxisc                   [        5       (       a  [        U [        5      (       d   S5       eSSKJnJn  U(       a  UOUnU" XS9n[        R                  " [        R                  " U  Vs/ s H  n[        UR                  U   5      PM     sn5      5      n	Xy4$ [        5       (       a  [        U S[        [        R                  R                  4S5        [        U [        5      (       aj  [        U 5       HZ  u  p[        US[!        U
5      -   S-   [        R                  R                  S5        UR#                  5       (       a  MQ  [%        S	5      e   O U R#                  5       (       d  [%        S	5      e[        R&                  R)                  XU5      $ [        U S[        [*        4S5        [        U [        5      (       a5  [        U 5       H&  u  p[        US[!        U
5      -   S-   [*        S5        M(     [-        S0 [/        5       D6nUR1                  UR3                  5       S
9nUR1                  SS
9nUR5                  SSU 0U/U/S.XS.S9  X4$ s  snf )a	  
This function concatenates or stacks all tensors in the input DenseTensorArray
along the axis mentioned and returns that as the output.

For Example:

.. code-block:: text

    Case 1:

        Given:

            input.data = {[[0.6, 0.1, 0.3],
                           [0.5, 0.3, 0.2]],
                          [[1.3],
                           [1.8]],
                          [[2.3, 2.1],
                           [2.5, 2.4]]}

            axis = 1, use_stack = False

        Then:

            output.data = [[0.6, 0.1, 0.3, 1.3, 2.3, 2.1],
                           [0.5, 0.3, 0.2, 1.8, 2.5, 2.4]]

            output_index.data = [3, 1, 2]

    Case 2:

        Given:

            input.data = {[[0.6, 0.1],
                           [0.5, 0.3]],
                          [[0.3, 1.3],
                           [0.2, 1.8]],
                          [[2.3, 2.1],
                           [2.5, 2.4]]}

            axis = 1, use_stack = True

        Then:

            output.data = [[[0.6, 0.1]
                            [0.3, 1.3]
                            [2.3, 2.1],
                           [[0.5, 0.3]
                            [0.2, 1.8]
                            [2.5, 2.4]]]

            output_index.data = [2, 2, 2]

Args:
    input(Tensor|list[Tensor]): A TensorArray variable.
    axis(int, optional): The axis along which the tensors in attr::`input` will be
        concatenated or stacked.
    use_stack(bool, optional): Act as concat_op or stack_op. For stack mode, all
        tensors in the tensor array must have the same shape.
    name(str|None, optional): A name for this layer(optional). If set None, the layer
                   will be named automatically.

Returns:
    Tensor: The concatenated or stacked tensor variable.
    Tensor: A 1-D tensor variable with int32 data type. The data in this \
        tensor contains all input including tensors' sizes along the axis.

Examples:
    .. code-block:: python

        >>> import numpy
        >>> import paddle
        >>> x0 = paddle.assign(numpy.random.rand(2, 2).astype("float32"))
        >>> x1 = paddle.assign(numpy.random.rand(2, 2).astype("float32"))
        >>> i = paddle.full(shape=[1], dtype="int64", fill_value=0)
        >>> array = paddle.tensor.array.create_array(dtype='float32')
        >>> paddle.tensor.array.array_write(x0, i, array)
        >>> paddle.tensor.array.array_write(x1, i + 1, array)
        >>> output, output_index = paddle.tensor.manipulation.tensor_array_to_tensor(input=array)
z2The 'input' in tensor_array_to_tensor must be listr   )concatstackr6   r5   tensor_array_to_tensorinput[]z%input should be tensor array variabledtypeint32XOutOutIndexr6   	use_stacktypeinputsoutputsattrs)r;   )r"   
isinstancelistpaddler8   r9   	to_tensornparrayintshaper$   r   pirValue	enumeratestris_dense_tensor_array_type	TypeError_pir_opsarray_to_tensorr   r   locals"create_variable_for_type_inferenceinput_dtype	append_op)r5   r6   rF   namer8   r9   opresxsizesiinput_xhelperout	out_indexs                  Z/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/tensor/manipulation.pyr;   r;   O   s"   j %&& 	
@	
& 	)UV"  u*Mu!3qwwt}+=u*M!NOz	6::##$$		
 eT""'.
s1v%+JJ$$,	 99;;#$KLL / 3355 GHH..uIFF5'D(#35MNeT""'.
s1v%+,	 / BB77$$& 8 
 ==G=L	)< E	{;8	 	 	
 ~Y +Ns   )"Irc   c                   [        U[        R                  R                  [        R                  45      (       d  [        U5      n[        5       (       a  [        R                  " X5      $ [        U S/ SQS5        [        US/ SQS5        [        S0 [        5       D6nUR                  XR                  S9nUR                  SSU /0SU/0U R                   UR                   S	.S
9  U$ )a  

Take in the Tensor :attr:`x` with :attr:`x.dtype` and cast it
to the output with :attr:`dtype`. It's meaningless if the output dtype
equals the input dtype, but it's fine if you do so.

The following picture shows an example where a tensor of type float64 is cast to a tensor of type uint8.

.. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/cast.png
    :width: 800
    :alt: legend of reshape API
    :align: center

Args:
    x (Tensor): An input N-D Tensor with data type bool, float16,
        float32, float64, int32, int64, uint8.
    dtype (paddle.dtype|np.dtype|str): Data type of the output:
        bool, float16, float32, float64, int8, int32, int64, uint8.

Returns:
    Tensor, A Tensor with the same shape as input's.

Examples:
    .. code-block:: python

        >>> import paddle

        >>> x = paddle.to_tensor([2, 3, 4], 'float64')
        >>> y = paddle.cast(x, 'uint8')
rc   )boolfloat16float32float64int16r@   int64uint8uint16float8_e4m3fnfloat8_e5m2castr?   )rl   rm   rn   ro   int8rp   r@   rq   rr   rs   rt   ru   r?   stop_gradientrA   rC   )in_dtype	out_dtyperG   )rv   )rL   r    VarDescVarTypeDataTyper   r#   r   rv   r   r   r   r\   r]   ry   r_   r?   )rc   r?   rg   rh   s       rj   rv   rv      s    > edll22DMMBCC*51{{1$$  !	
$ 	 #	
( 0vx077 8 
 	!:SEN wwSYY?	 	 	
 
    c                    [        5       (       aZ  [        U[        R                  R                  [        R
                  45      (       d  [        U5      n[        R                  " X5      $ g)z
Inplace version of ``cast`` API, the output Tensor will be inplaced with input ``x``.
Please refer to :ref:`api_paddle_cast`.
N)	r"   rL   r    r|   r}   r~   r   r   cast_)rc   r?   s     rj   r   r   2  sK     %$,,"6"6!FGG.u5E||A%% r   c                   [        U[        [        45      (       a  [        U5      n[        U5      S:X  a  [	        S5      e[        [        U5      5       HY  nX   S:  a(  [        SX   [        U R                  5      -   5      X'   M3  [        [        U R                  5      S-
  X   5      X'   M[     O[	        S[        U5       35      e[        5       (       Ga  SnSnSn[        [        U5      5       Vs/ s H  nSPM     nn[        U[        [        45      (       aM  U V	s/ s H?  n	[        U	[        R                  R                  5      (       a  U	R                  S5      OU	PMA     nn	Oi[        U[        R                  R                  5      (       a@  UR                  S5      n
[        U
5      n[        [        U5      5       Vs/ s H  nSPM     nn[        U[        [        45      (       aM  U V	s/ s H?  n	[        U	[        R                  R                  5      (       a  U	R                  S5      OU	PMA     nn	Oi[        U[        R                  R                  5      (       a@  UR                  S5      n
[        U
5      n[        [        U5      5       Vs/ s H  nSPM     nn[         R"                  " XX#U/ 5      $ [%        5       (       Ga  [        U[        [        [&        R(                  R*                  45      (       d  [	        S	5      e[        U[        [        [&        R(                  R*                  45      (       d  [	        S
5      e[        [        U5      5       Vs/ s H  nSPM     nn[        U[&        R(                  R*                  5      (       a,  SUl        [        [        U5      5       Vs/ s H  nSPM     nnO[        U[        [        45      (       a  [&        R.                  R1                  U5      (       ab  [3        U5       H4  u  pK[        U[&        R(                  R*                  5      (       d  M0  SX'   M6     [&        R.                  R5                  U5      n[        U[&        R(                  R*                  5      (       a,  SUl        [        [        U5      5       Vs/ s H  nSPM     nnO[        U[        [        45      (       a  [&        R.                  R1                  U5      (       ab  [3        U5       H4  u  pK[        U[&        R(                  R*                  5      (       d  M0  SX'   M6     [&        R.                  R5                  U5      n[         R"                  " XX#U/ 5      $ [        U[        [        [6        45      (       d  [	        S5      e[        U[        [        [6        45      (       d  [	        S5      e[9        S0 [;        5       D6nSU 0nSU0n[        [        U5      5       Vs/ s H  nSPM     nn[        U[6        5      (       a0  SUl        X-S'   [        [        U5      5       Vs/ s H  nSPM     nnO[        U[        [        45      (       a  / US'   [&        R.                  R1                  U5      (       az  [&        R.                  R=                  U5      US'   [3        U5       HH  u  pK[        U[6        5      (       a  US   R?                  S5        SX'   M4  US   R?                  U5        MJ     OX%S'   [        U[6        5      (       a0  SUl        X=S'   [        [        U5      5       Vs/ s H  nSPM     nnO[        U[        [        45      (       a  / US'   [&        R.                  R1                  U5      (       az  [&        R.                  R=                  U5      US'   [3        U5       HH  u  pK[        U[6        5      (       a  US   R?                  S5        SX'   M4  US   R?                  U5        MJ     OX5S'   XS'   URA                  URC                  S5      S9nURE                  SXSU0S9  U$ s  snf s  sn	f s  snf s  sn	f s  snf s  snf s  snf s  snf s  snf s  snf s  snf )a  
This operator produces a slice of ``input`` along multiple axes. Similar to numpy:
https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html
Slice uses ``axes``, ``starts`` and ``ends`` attributes to specify the start and
end dimension for each axis in the list of axes and Slice uses this information
to slice the input data tensor. If a negative value is passed to
``starts`` or ``ends`` such as :math:`-i`,  it represents the reverse position of the
axis :math:`i-1` (here 0 is the initial position).
If the value passed to ``starts`` or ``ends`` is greater than n
(the number of elements in this dimension), it represents n.
For slicing to the end of a dimension with unknown size, it is recommended
to pass in INT_MAX. The size of ``axes`` must be equal to ``starts`` and ``ends``.
Following examples will explain how slice works:

.. code-block:: text

    Case1:
        Given:
            data = [ [1, 2, 3, 4], [5, 6, 7, 8], ]
            axes = [0, 1]
            starts = [1, 0]
            ends = [2, 3]
        Then:
            result = [ [5, 6, 7], ]

    Case2:
        Given:
            data = [ [1, 2, 3, 4], [5, 6, 7, 8], ]
            axes = [0, 1]
            starts = [0, 1]
            ends = [-1, 1000]       # -1 denotes the reverse 0th position of dimension 0.
        Then:
            result = [ [2, 3, 4], ] # result = data[0:1, 1:4]

The following figure illustrates the first case -- a 2D tensor of shape [2, 4] is transformed into a 2D tensor of shape [1, 3] through a slicing operation.

.. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/slice.png
    :width: 500
    :alt: legend of slice API
    :align: center

Args:
    input (Tensor): A ``Tensor`` . The data type is ``float16``, ``float32``, ``float64``, ``int32`` or ``int64``.
    axes (list|tuple): The data type is ``int32`` . Axes that `starts` and `ends` apply to .
    starts (list|tuple|Tensor): The data type is ``int32`` . If ``starts`` is a list or tuple, each element of
            it should be integer or 0-D int Tensor with shape []. If ``starts`` is an Tensor, it should be an 1-D Tensor.
            It represents starting indices of corresponding axis in ``axes``.
    ends (list|tuple|Tensor): The data type is ``int32`` . If ``ends`` is a list or tuple, each element of
            it should be integer or 0-D int Tensor with shape []. If ``ends`` is an Tensor, it should be an 1-D Tensor .
            It represents ending indices of corresponding axis in ``axes``.

Returns:
    Tensor, A ``Tensor``. The data type is same as ``input``.

Examples:
    .. code-block:: python

        >>> import paddle

        >>> input = paddle.rand(shape=[4, 5, 6], dtype='float32')
        >>> # example 1:
        >>> # attr starts is a list which doesn't contain tensor.
        >>> axes = [0, 1, 2]
        >>> starts = [-3, 0, 2]
        >>> ends = [3, 2, 4]
        >>> sliced_1 = paddle.slice(input, axes=axes, starts=starts, ends=ends)
        >>> # sliced_1 is input[1:3, 0:2, 2:4].

        >>> # example 2:
        >>> # attr starts is a list which contain tensor.
        >>> minus_3 = paddle.full([1], -3, "int32")
        >>> sliced_2 = paddle.slice(input, axes=axes, starts=[minus_3, 0, 2], ends=ends)
        >>> # sliced_2 is input[1:3, 0:2, 2:4].
r   z-Input axes should not be an empty list/tuple.r%   z8Input axes must be a python list or tuple, but received  NFz4Input starts must be an Value, python list or tuple.z2Input ends must be an Value, python list or tuple.Tz7Input starts must be an Variable, python list or tuple.z5Input ends must be an Variable, python list or tuple.sliceInputaxesStartsTensorstartsStartsTensorList
EndsTensorendsEndsTensorListinfer_flagsr5   r>   rC   rH   rI   rK   rJ   )r   )#rL   rM   tuplelen
ValueErrorrangemaxrS   minrH   r"   r    eagerr,   itemnumpyr   r   r$   rN   rT   rU   ry   utils_contain_varrV   get_int_tensor_listr   r   r\   _convert_to_tensor_listappendr]   r^   r_   )r5   r   r   r   re   rK   starts_tensorends_tensorr   r   tensor_tdimrg   rI   rh   s                  rj   r   r   >  s   ` $u&&Dzt9>LMMs4y!Aw{a3u{{+;!;<c%++.2DG<	 " FtDzlS
 	
 "'D	"23"2Qq"23ftUm,, #"D !+41B1B C C		!M"  F 

 1 122||E*H(^F',SY'78'7!2'7K8dT5M** ! D !+41B1B C C		!M   D djj//00zz%(H>D',SY'78'7!2'7K8||E{BGG	&4

0@0@"ABBF  $ufjj.>.> ?@@D  #(D	"23"2Qq"23ffjj..//#'F ',SY'78'7!2'7K8Ku..||((00'/FA!#vzz'7'788)+ 0  99&A dFJJ,,--!%D',SY'78'7!2'7K8KtUm,,||((..'oFA!#vzz'7'788)+ . ||77=||E{BGG&4"9::I  $uh 788G  115!"'D	"23"2Qq"23 fh''#'F %+>"',SY'78'7!2'7K8Ku.. E(O||((00LL88@ )* (/FA!#x00h..r2)+h..s3 0 #)h dH%%!%D#'< ',SY'78'7!2'7K8KtUm,,E&M||((..+1<<+O+O,'( (oFA!#x00f,,R0)+f,,S1 . !%f  +m77$$W- 8 
 	ucl 	 	
 
q 4 9 9 4 9 9, 4 9( 9sD   6a#Aaa4Aaa$#a)7a.-a3=a8a=br   c           	     r   [        U[        R                  5      (       aU  UR                  S:X  a.  UR                  [        R
                  [        R                  4;   d   S5       eUR                  5       nU R                  S:  d   S5       eUS:  d   S5       eU R                  nU R                  S:X  a  SnSUs=::  a  U:  d4  O  US:  a  X-   OUnUS:  d  XT:  a  [        SU*  SUS-
   SU S	35      eUnU R                  U   nU* Us=::  a  U::  d  O   S
U*  SU SU S	35       eUS:  a  X&-   nX&U-
  ::  d   SU SU SU S35       e[        U R                  5      nX7U'   U R                  nX(U   -  n	U	[        R                  R                  U R                  5      -  n	[        R                  " XXS9$ )ag  
Returns a narrowed slice of input along a single axis.

This operator selects the index range [start, start + length) on dimension dim and keeps all
the dimensions unchanged.

Args:
    input (Tensor): Input tensor.
    dim (int): Dimension to narrow. Supports negative indexing.
    start (int|Tensor): Start index on ``dim``. Can be a Python int or a 0-D
        int Tensor (int32 or int64). Negative values are supported.
    length (int): Number of elements to select from ``start``. Must be
        non-negative.

Returns:
    Tensor: A tensor that is a narrowed view of ``input``.

Examples:
    .. code-block:: python

        >>> import paddle

        >>> x = paddle.to_tensor([[1, 2, 3, 4],[5, 6, 7, 8]], dtype='int64')
        >>> y1 = paddle.narrow(x, dim=1, start=1, length=2)
        >>> print(y1)
        Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
        [[2, 3],
         [6, 7]])

        >>> y2 = paddle.narrow(x, dim=-1, start=-3, length=3)
        >>> print(y2)
        Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
        [[2, 3, 4],
         [6, 7, 8]])

        >>> s = paddle.to_tensor(0, dtype='int64')
        >>> y3 = paddle.narrow(x, dim=1, start=s, length=2)
        >>> print(y3)
        Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
        [[1, 2],
         [5, 6]])
r   z'start must be an 0-dim integral Tensor.z-narrow() cannot be applied to a 0-dim tensor.z&narrow(): length must be non-negative.r%   z4Dimension out of range (expected to be in range of [, z], but got )z0start out of range (expected to be in range of [zstart (z) + length (z) exceeds dimension size ().)rS   strideoffset)rL   rN   r,   ndimr?   r@   rq   r   
IndexErrorrS   rM   stridesr    size_of_dtype
as_strided)
r5   r   startlengthrank_dim
dim_length	new_shaper   r   s
             rj   narrowr     s   b %''zzQ5;;LLLL3
 $
 	5 5	5 
 

::>JJJ>Q;@@@;::DzzQOtO 1Wsz#!8t|FugRPTWXPXzYdehdiijk  S!J;%-:- 
:J;-r*U`af`gghi- qy"'' 
%VH,FzlRTU' U[[!IcN]]FC[ F
fkk''44Fv r   c                   [        5       (       a  [        R                  " X5      $ [        U S/ SQS5        [	        US[
        [        4S5        [        U[        5      (       a  [        U5      n[        U5      [        U R                  5      :w  a.  [        S[        U R                  5       S[        U5       S35      e[        U5       HH  u  p4U[        U R                  5      :  d  M   [        SU S	X    S
[        U R                  5       S35      e   [        S0 [        5       D6nUR                  U R                  5      nUR                  U R                  5      nUR!                  SSU /0U/U/S.SU0S9  U$ )aE  
Permute the data dimensions of `input` according to `perm`.

The `i`-th dimension  of the returned tensor will correspond to the
perm[i]-th dimension of `input`.

The image illustrates the second example of the transpose operation.

.. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/transpose.png
    :width: 500
    :alt: legend of transpose API

Args:
    x (Tensor): The input Tensor. It is a N-D Tensor of data types bool, float32, float64, int32.
    perm (list|tuple): Permute the input according to the data of perm.
    name (str|None, optional): The name of this layer. It is optional.

Returns:
    Tensor, A transposed n-D Tensor, with data type being bool, float32, float64, int32, int64.

For Example:

    .. code-block:: text

     x = [[[ 1  2  3  4] [ 5  6  7  8] [ 9 10 11 12]]
         [[13 14 15 16] [17 18 19 20] [21 22 23 24]]]
     shape(x) =  [2,3,4]

     # Example 1
     perm0 = [1,0,2]
     y_perm0 = [[[ 1  2  3  4] [13 14 15 16]]
               [[ 5  6  7  8]  [17 18 19 20]]
               [[ 9 10 11 12]  [21 22 23 24]]]
     shape(y_perm0) = [3,2,4]

     # Example 2
     perm1 = [2,1,0]
     y_perm1 = [[[ 1 13] [ 5 17] [ 9 21]]
               [[ 2 14] [ 6 18] [10 22]]
               [[ 3 15]  [ 7 19]  [11 23]]
               [[ 4 16]  [ 8 20]  [12 24]]]
     shape(y_perm1) = [4,3,2]

Examples:

    .. code-block:: pycon

        >>> import paddle

        >>> x = paddle.randn([2, 3, 4])
        >>> x_transposed = paddle.transpose(x, perm=[1, 0, 2])
        >>> print(x_transposed.shape)
        paddle.Size([3, 2, 4])

rc   	rl   rm   rn   ro   r@   rq   rs   	complex64
complex128	transposepermzInput(perm) is the permutation of dimensions of Input(x), its length should be equal to dimensions of Input(x), but received dimension of Input(x) is z, the length of Input(perm) is .zJEach element in Input(perm) should be less than Input(x)'s dimension, but z-th element in Input(perm) is z$ which exceeds Input(x)'s dimension 
transpose2rA   rC   XShaper6   rG   )r   )r#   r   r   r   r   rM   r   rL   r   rS   r   rV   r   r\   r]   r?   r_   )rc   r   r`   idxr   rg   rh   x_shapes           rj   r   r   v  sx   t (( 
 	
  	4$<dE"":Dt9AGG$99<QWW G003D	{!=  "$HCc!''l" %=di[ I!!$QWWa1  ( 5FH577@;;AGGD!: EgY74.	 	 	
 
r   c                   U R                   * Us=::  a  U R                   :  d(  O  [        SU R                    SU R                    S35      eUb4  US:  d  X R                  U   :  a  [        SU R                  U    S35      e[        5       (       a1  Uc  U R                  U   nUS:X  a  / $ [        R
                  " XU5      $ [        S0 [        5       D6nUc0  Ub  U R                  U   S::  a  [        S5      eU R                  U   n/ n[        U5       H-  nUR                  UR                  U R                  5      5        M/     UR                  SSU /0S	U0XS
.S9  U$ )a  
This layer unstacks input Tensor :code:`x` into several Tensors along :code:`axis`.

If :code:`axis` < 0, it would be replaced with :code:`axis+rank(x)`.
If :code:`num` is None, it would be inferred from :code:`x.shape[axis]`,
and if :code:`x.shape[axis]` <= 0 or is unknown, :code:`ValueError` is
raised.

Args:
    x (Tensor): Input Tensor. It is a N-D Tensors of data types float32, float64, int32, int64, complex64, complex128.
    axis (int, optional): The axis along which the input is unstacked.
    num (int|None, optional): The number of output variables.

Returns:
    list(Tensor), The unstacked Tensors list. The list elements are N-D Tensors of data types float32, float64, int32, int64, complex64, complex128.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> x = paddle.ones(name='x', shape=[2, 3, 5], dtype='float32')  # create a tensor with shape=[2, 3, 5]
        >>> y = paddle.unstack(x, axis=1)  # unstack with second axis, which results 3 tensors with shape=[2, 5]

z`axis` must be in the range [-r   r   r   z`num` must be in the range [0, unstackzunknown unstack numberrA   Y)r6   numrG   )r   )r   r   rS   r#   r   r   r   r\   r   r   r]   r?   r_   )rc   r6   r   rg   outs_s         rj   r   r     sO   2 VVGt$aff$9!&&AFF81MNN
C!GsWWT]'::1774=/KLL;''$-C!8I~~as++3&(3;|qwwt}1 !9::ggdmsAKKAA!''JK  	!:$K,	 	 	
 r   c           
     F   [        5       (       a  [        R                  " XX#U5      $ [        U SSS/S5        Sn[	        U40 [        5       D6nUS:  d  X2:  a  [        SU SU S35      eUR                  U R                  S	9nUR                  US
U /0SU0UUUUS.SS9  U$ )a	  
Reset the values of `input` according to the shard it belongs to.
Every value in `input` must be a non-negative integer, and
the parameter `index_num` represents the integer above the maximum
value of `input`. Thus, all values in `input` must be in the range
[0, index_num) and each value can be regarded as the offset to the beginning
of the range. The range is further split into multiple shards. Specifically,
we first compute the `shard_size` according to the following formula,
which represents the number of integers each shard can hold. So for the
i'th shard, it can hold values in the range [i*shard_size, (i+1)*shard_size).
::

    shard_size = (index_num + nshards - 1) // nshards

For each value `v` in `input`, we reset it to a new value according to the
following formula:
::

    v = v - shard_id * shard_size if shard_id * shard_size <= v < (shard_id + 1) * shard_size else ignore_value

That is, the value `v` is set to the new offset within the range represented by the shard `shard_id`
if it in the range. Otherwise, we reset it to be `ignore_value`.

As shown below, a ``[2, 1]`` 2D tensor is updated with the ``shard_index`` operation. Given ``index_num = 20``, ``nshards = 2``, and ``shard_id = 0``, the shard size is ``shard_size = (20 + 2 - 1) // 2 = 10``.
For each label element: if its value is in [0, 10), it's adjusted to its offset; e.g., 1 becomes 1 - 0 * 10 = 1. Otherwise, it's set to the default ignore_value of -1, like 16 becoming -1.

.. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/shard_index.png
   :width: 500
   :alt: Illustration of Case 2
   :align: center

Args:
    input (Tensor): Input tensor with data type int64 or int32. It's last dimension must be 1.
    index_num (int): An integer represents the integer above the maximum value of `input`.
    nshards (int): The number of shards.
    shard_id (int): The index of the current shard.
    ignore_value (int, optional): An integer value out of sharded index range. The default value is -1.

Returns:
    Tensor.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> label = paddle.to_tensor([[16], [1]], "int64")
        >>> shard_label = paddle.shard_index(input=label,
        ...                                  index_num=20,
        ...                                  nshards=2,
        ...                                  shard_id=0)
        >>> print(shard_label.numpy())
        [[-1]
         [ 1]]


r5   rq   r@   shard_indexr   zThe shard_id(z) should be in [0, r   r>   rA   rC   )	index_numnshardsshard_idignore_valueT)rH   rI   rJ   rK   ry   )
r#   r   r   r   r   r\   r   r]   r?   r_   )r5   r   r   r   r   op_typerg   rh   s           rj   r   r     s    ~ !!g
 	
 UGgw-?OG-FH-F!|x*H:%8	C
 	
 
3
3%++
3
FC
eW~" (	
    Jr   c           
     h
   [        S0 [        5       D6n[        U S/ SQS5        [        US[        [
        [        [        S5      [        R                  R                  4S5        [        US[        [
        [        [        S5      [        R                  R                  4S5        Uc  S/[        U R                  5      -  nUc  U R                  n[        5       (       a  [        R                  " XU5      $ S nS	 n[!        5       (       Ga  [#        U[        R                  R                  5      (       a  S
Ul        O[        R&                  R)                  U5      (       aw  / nU Hl  n[#        U[        R                  R                  5      (       a  S
Ul        UR+                  U5        MF  U" U5        [-        S/SUS
S9n	UR+                  U	5        Mn     UnOU H  n
U" U
5        M     [#        U[        R                  R                  5      (       a  S
Ul        O[        R&                  R)                  U5      (       aw  / nU Hl  n[#        U[        R                  R                  5      (       a  S
Ul        UR+                  U5        MF  U" U5        [-        S/SUS
S9n	UR+                  U	5        Mn     UnOU H  nU" U5        M     [        R                  " XU5      $ UR/                  U R0                  5      nSU 0n0 n[#        U[        5      (       a(  S
Ul        X.S'   S/[        U R                  5      -  US'   O[        R&                  R)                  U5      (       a  / n/ nU H  n[#        U[        5      (       a+  S
Ul        UR+                  U5        UR+                  S5        MC  U" U5        UR/                  S5      n	[-        S/SUS
U	S9  UR+                  U	5        UR+                  U5        M     X~S'   UUS'   OU H  n
U" U
5        M     X/S'   [#        U[        5      (       a  S
Ul        XS'   O[        R&                  R)                  U5      (       a  / n/ nU H  n[#        U[        5      (       a+  S
Ul        UR+                  U5        UR+                  S5        MC  U" U5        UR/                  S5      n	[-        S/SUS
U	S9  UR+                  U	5        UR+                  U5        M     XS'   UUS'   OU H  nU" U5        M     XS'   UR3                  SUSU0[        U5      S:X  a  SOUS9  U$ )a  
Crop input into output, as specified by offsets and shape.

.. code-block:: text

    * Case 1 (input is a 2-D Tensor):
        Input:
            X.shape = [3, 5]
            X.data = [[0, 1, 2, 0, 0],
                      [0, 3, 4, 0, 0],
                      [0, 0, 0, 0, 0]]
        Parameters:
            shape = [2, 2]
            offsets = [0, 1]
        Output:
            Out.shape = [2, 2]
            Out.data = [[1, 2],
                        [3, 4]]
    * Case 2 (input is a 3-D Tensor):
        Input:
            X.shape = [2, 3, 4]
            X.data =  [[[0, 1, 2, 3],
                        [0, 5, 6, 7],
                        [0, 0, 0, 0]],
                       [[0, 3, 4, 5],
                        [0, 6, 7, 8],
                        [0, 0, 0, 0]]]
        Parameters:
            shape = [2, 2, -1]
            offsets = [0, 0, 1]
        Output:
            Out.shape = [2, 2, 3]
            Out.data  = [[[1, 2, 3],
                          [5, 6, 7]],
                         [[3, 4, 5],
                          [6, 7, 8]]]

The image below demonstrates the  Case 2 that a 3D tensor with shape [2,3,4] is cropped into a 3D tensor with shape [2,2,3]

.. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/crop.png
   :width: 500
   :alt: Illustration of Case 2
   :align: center

Parameters:
    x (Tensor): 1-D to 6-D Tensor, the data type is float32, float64, int32 or int64.
    shape (list|tuple|Tensor, optional): The output shape is specified
        by `shape`. Its data type is int32. If a list/tuple, it's length must be
        the same as the dimension size of `x`. If a Tensor, it should be a 1-D Tensor.
        When it is a list, each element can be an integer or a Tensor of shape: [1].
        If Variable contained, it is suitable for the case that the shape may
        be changed each iteration.
    offsets (list|tuple|Tensor, optional): Specifies the cropping
        offsets at each dimension. Its data type is int32. If a list/tuple, it's length
        must be the same as the dimension size of `x`. If a Tensor, it should be a 1-D
        Tensor. When it is a list, each element can be an integer or a Tensor of shape: [1].
        If Variable contained, it is suitable for the case that the offsets may be changed
        each iteration. Default: None, the offsets are 0 at each dimension.
    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 cropped Tensor has same data type with `x`.

Examples:

    .. code-block:: python

        >>> import paddle
        >>> x = paddle.to_tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
        >>> # x.shape = [3, 3]
        >>> # x = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

        >>> # shape can be a 1-D Tensor or list or tuple.
        >>> shape = paddle.to_tensor([2, 2], dtype='int32')
        >>> # shape = [2, 2]
        >>> # shape = (2, 2)
        >>> out = paddle.crop(x, shape)
        >>> # out.shape = [2, 2]
        >>> # out = [[1,2], [4,5]]

        >>> # offsets can be a 1-D Tensor or list or tuple.
        >>> offsets = paddle.to_tensor([0, 1], dtype='int32')
        >>> # offsets = [1, 0]
        >>> # offsets = (1, 1)
        >>> out = paddle.crop(x, shape, offsets)
        >>> # out.shape = [2, 2]
        >>> # if offsets = [0, 0], out = [[1,2], [4,5]]
        >>> # if offsets = [0, 1], out = [[2,3], [5,6]]
        >>> # if offsets = [1, 0], out = [[4,5], [7,8]]
        >>> # if offsets = [1, 1], out = [[5,6], [8,9]]

crop_tensorrc   rn   ro   r@   rq   rS   Noffsetsr   c                    [        U [        5      (       d  [        S[        U 5       S35      eU S:  a  [	        SU  S35      eg )NzFAttr(shape)'s dtype of Op(crop_tensor) should be int32, but received: r   r   zdWhen the element in Attr(shape) of Op(crop_tensor) is negative, only -1 is supported, but received: rL   rR   rY   rH   r   )	shape_vals    rj   _attr_shape_checkcrop.<locals>._attr_shape_check  sj    )S))XY]^gYhXiijk  r>v  xA  wB  BC  D  r   c                    [        U [        5      (       d  [        S[        U 5       S35      eU S:  a  [	        SU  S35      eg )NzHAttr(offsets)'s dtype of Op(crop_tensor) should be int32, but received: r   r   zSAttr(offsets) of Op(crop_tensor) should be greater or equal to zero, but received: r   )
offset_vals    rj   _attr_offsets_check!crop.<locals>._attr_offsets_check  s[    *c**Z[_`j[kZllmn  >efpeqqrs  r   Tr%   r@   )	force_cpurA   Offsetsr   r   rh   OffsetsTensorShapeShapeTensorrC   rG   )r   )r   r\   r   r   rM   r   r   rH   rN   rT   rU   r   rS   r"   r   cropr$   rL   ry   r   r   r   r   r]   r?   r_   )rc   rS   r   r`   rg   r   r   new_offsets_tensorr   temp_outr   new_shape_tensordim_sizerh   rI   rK   offsets_attr
shape_attrs                     rj   r   r   u  s   F 3&(3F	38- 	uhT
FJJ,<,<=	 	uhT
FJJ,<,<=	 #AGG$}{{1W-- }}gvzz//00$(G!\\&&w//!#c6::#3#344(,C%&--c2',,aS'3$OH&--h7  )G!#F+ " eVZZ--.."&E\\&&u--!!h

(8(899-1H*$++H5%h/,Wh$ H %++H5 " %E!!(+ "{{1W--

3
3AGG
<C1XFE'8$$ $#y4#agg,.i		"	"7	+	+C#x(($(!"))#.##B'#C(!DDWMqc7C4XN"))(3##C(  #5'iF' "i%"""w		"	"5	)	)
H(H--)-& ''1!!!$!(+!DDWMC(d !''1!!(+  !1}#gHh' g
%jAod5	   Jr   c                    [        U[        [        45      (       d  [        S[	        U5       S35      e[
        R                  " X5      $ )a
  
**Notes**:
    **This API is ONLY available in Dygraph mode**

This function fill the Tensor with value inplace.

Args:
    x (Tensor): ``x`` is the Tensor we want to filled data inplace
    value (int|float): ``value`` is the value to be filled in x

Returns:
    x(Tensor), Tensor x filled with value inplace

Examples:
    .. code-block:: python

        >>> import paddle

        >>> tensor = paddle.to_tensor([0, 1, 2, 3, 4])

        >>> tensor.fill_(0)
        >>> print(tensor.tolist())
        [0, 0, 0, 0, 0]

z8The type of 'value'  must be int or float, but received r   )rL   floatrR   rY   rH   r   fill_)rc   values     rj   r   r   l  sD    6 eeS\**FtE{mSTU
 	
 <<!!r   c                0    [         R                  " U S5      $ )a  
**Notes**:
    **This API is ONLY available in Dygraph mode**

This function fill the Tensor with zero inplace.

Args:
    x (Tensor): ``x`` is the Tensor we want to filled with zero inplace

Returns:
    x (Tensor), Tensor x filled with zero inplace

Examples:
    .. code-block:: python

        >>> import paddle

        >>> tensor = paddle.to_tensor([0, 1, 2, 3, 4])

        >>> tensor.zero_()
        >>> print(tensor.tolist())
        [0, 0, 0, 0, 0]

g        )r   r   rc   s    rj   zero_r     s    4 <<3r   c                    [        5       (       aH  [        U R                  5      S:X  a  [        R                  " XX#5      $ [        R                  " XUS5      $ g)a  
Note:
    This API is ONLY available in Dygraph mode.

This function fill the value into the x Tensor's diagonal inplace.

Args:
    x(Tensor): ``x`` is the original Tensor
    value(int|float): ``value`` is the value to filled in x
    offset(int,optional): the offset to the main diagonal. Default: 0 (main diagonal).
    wrap(bool,optional): the diagonal 'wrapped' after N columns for tall matrices.
    name(str|None,optional): Name for the operation (optional, default is None)

Returns:
    Tensor, Tensor with diagonal filled with value.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> x = paddle.ones((4, 3)) * 2
        >>> x.fill_diagonal_(1.0)
        >>> print(x.tolist())
        [[1.0, 2.0, 2.0], [2.0, 1.0, 2.0], [2.0, 2.0, 1.0], [2.0, 2.0, 2.0]]
r   TN)r"   r   rS   r   fill_diagonal_)rc   r   r   wrapr`   s        rj   r   r     sK    B qww<1((6@@$$Qvt<< r   c           	        U R                   nU[        U5      :  a  U[        U5      * :  d   S5       eU[        U5      :  a  U[        U5      * :  d   S5       e[        U5      S:  d   S5       eU[        U5      -  nU[        U5      -  n/ n[        [        U5      5       H$  nX:w  d  M
  X:w  d  M  UR                  Xh   5        M&     [	        Xc   Xc   U-   Xd   Xd   U-
  5      n	UR                  U	5        [        U5      [        UR                   5      :X  d
   SU 35       e[        UR                   5      S:X  a  UR                  SS/5      n[        5       (       a7  U(       a  [        R                  " XX#U5      $ [        R                  " XX#U5      $ [        U S/ S	QS
5        [        US/ S	QS
5        [        S0 [        5       D6n
U
R                  U R                  5      nU
R!                  SU US.SU0UUUS.S9  U$ )Nz9dim1 should between [-rank,rank) in fill_diagonal_tensor_z9dim2 should between [-rank,rank) in fill_diagonal_tensor_r   z0Tensor dims should >= 2 in fill_diagonal_tensor_zthe y shape should be r%   r   rA   )rm   rn   ro   rs   rr   rw   rp   r@   rq   rl   r   r   z/paddle.tensor.manipulation.fill_diagonal_tensorr   fill_diagonal_tensor)rA   r   rC   )r   dim1dim2rG   r   )rS   r   r   r   r   r   reshaper#   r   fill_diagonal_tensor_r   r   r   r\   r]   r?   r_   )rc   yr   r   r   inplaceinshape	predshapere   diaglenrg   rh   s               rj   _fill_diagonal_tensor_implr     s    ggG#g,4CL=#8 C8 #g,4CL=#8 C8 w<1PPPCLDCLDI3w< 9WZ( ! 	G WuQWW~- 
 ,- 177|qIIq"g//fDII..qV4HH  >#	
& 	! >#	
& @vx@77@' CL  	 	
 
r   c           	         [        XX#USS9$ )a  
Note:
    This API is ONLY available in Dygraph mode.

This function fill the source Tensor y into the x Tensor's diagonal inplace.

Args:
    x (Tensor): ``x`` is the original Tensor
    y (Tensor): ``y`` is the Tensor to filled in x
    dim1 (int,optional): first dimension with respect to which to fill diagonal. Default: 0.
    dim2 (int,optional): second dimension with respect to which to fill diagonal. Default: 1.
    offset (int,optional): the offset to the main diagonal. Default: 0 (main diagonal).
    name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.

Returns:
    Tensor, Tensor with diagonal filled with y.

Examples:
    .. code-block:: python

        >>> import paddle

        >>> x = paddle.ones((4, 3)) * 2
        >>> y = paddle.ones((3,))
        >>> x.fill_diagonal_tensor_(y)
        >>> print(x.tolist())
        [[1.0, 2.0, 2.0], [2.0, 1.0, 2.0], [2.0, 2.0, 1.0], [2.0, 2.0, 2.0]]

Tr   r   r   r   r   rc   r   r   r   r   r`   s         rj   r   r   4  s    J &	VT4 r   c           	         [        XX#USS9$ )a  
This function fill the source Tensor y into the x Tensor's diagonal.

Args:
    x (Tensor): ``x`` is the original Tensor
    y (Tensor): ``y`` is the Tensor to filled in x
    dim1 (int,optional): first dimension with respect to which to fill diagonal. Default: 0.
    dim2 (int,optional): second dimension with respect to which to fill diagonal. Default: 1.
    offset (int,optional): the offset to the main diagonal. Default: 0 (main diagonal).
    name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.

Returns:
    Tensor, Tensor with diagonal filled with y.

Examples:
    .. code-block:: python

        >>> import paddle

        >>> x = paddle.ones((4, 3)) * 2
        >>> y = paddle.ones((3,))
        >>> nx = x.fill_diagonal_tensor(y)
        >>> print(nx.tolist())
        [[1.0, 2.0, 2.0], [2.0, 1.0, 2.0], [2.0, 2.0, 1.0], [2.0, 2.0, 2.0]]

Fr   r   r  s         rj   r   r   ^  s    D &	VT5 r   c                @    U R                  S5      R                  5       $ )a2  
Note:
    This API is ONLY available in Dygraph mode.

This function translate the paddle.Tensor to python list.

Args:
    x (Tensor): ``x`` is the Tensor we want to translate to list.

Returns:
    list, A list that contain the same value of current Tensor.


Examples:
    .. code-block:: python

        >>> import paddle

        >>> t = paddle.to_tensor([0,1,2,3,4])
        >>> expectlist = t.tolist()
        >>> print(expectlist)
        [0, 1, 2, 3, 4]

        >>> expectlist = paddle.tolist(t)
        >>> print(expectlist)
        [0, 1, 2, 3, 4]

F)r   tolistr   s    rj   r  r    s    > 775>  ""r   tensors)rc   r6   rh   c                  U n[        5       (       a;  [        U[        5      (       a  UR                  S5      n[        R
                  " XAUS9$ [        5       (       Ga;  S nU" 5       n[        US[        [        [        R                  R                  4S5        [        U[        R                  R                  5      (       de  [        U5       HU  u  pp[        U S[        U5      -   S-   / SQS5        U(       a  M-  U R                   US   R                   :w  d  ML  [#        S	5      e   O\[        U[        R                  R                  5      (       a0  UR%                  5       (       a  [        R&                  " XAS
5      u  p8U$ U/n[        R
                  " XA5      $ [        US[        [        [        4S5        [        U[        5      (       d\  [        U5       HL  u  pp[        U S[        U5      -   S-   / SQS5        U R                   US   R                   :w  d  MC  [#        S	5      e   OU/n[        US[(        [        4S5        [        U[        5      (       a  [+        UR                   SSS/SS5        [-        S0 [/        5       D6n	U	R1                  U	R3                  5       S9nUS   R4                  R7                  5       [8        R:                  R<                  R>                  :X  aS  [A        U5      S:X  d   S[A        U5       S35       eU	R1                  SS9n
U	RC                  SSUS   0U/U
/S.US
S.S9  U$ SU0n0 n[        U[        5      (       a  SUl"        XS'   OXS'   U	RC                  SUSU/0US9  U$ )a  

Concatenates the input along the axis. It doesn't support 0-D Tensor because it requires a certain axis, and 0-D Tensor
doesn't have any axis.

The image illustrates a typical case of the concat operation.
Two three-dimensional tensors with shapes [2, 3, 4] are concatenated along different axes, resulting in tensors of different shapes.
The effects of concatenation along various dimensions are clearly visible.

.. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/concat.png
    :width: 500
    :alt: legend of concat API
    :align: center

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

Args:
    x (list|tuple): ``x`` is a Tensor list or Tensor tuple which is with data type bool, float16, bfloat16,
        float32, float64, int8, int16, int32, int64, uint8, uint16, complex64, complex128. All the Tensors in ``x`` must have same data type.
        alias: ``tensors``.
    axis (int|Tensor, optional): Specify the axis to operate on the input Tensors.
        Tt should be integer or 0-D int Tensor with shape []. The effective range is [-R, R), where R is Rank(x). When ``axis < 0``,
        it works the same way as ``axis+R``. Default is 0.
        alias: ``dim``.
    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. If set, the result will be stored in this Tensor. Default is None.

Returns:
    Tensor, A Tensor with the same data type as ``x``.

Examples:
    .. code-block:: python

        >>> import paddle

        >>> x1 = paddle.to_tensor([[1, 2, 3],
        ...                        [4, 5, 6]])
        >>> x2 = paddle.to_tensor([[11, 12, 13],
        ...                        [14, 15, 16]])
        >>> x3 = paddle.to_tensor([[21, 22],
        ...                        [23, 24]])
        >>> zero = paddle.full(shape=[1], dtype='int32', fill_value=0)
        >>> # When the axis is negative, the real axis is (axis + Rank(x))
        >>> # As follow, axis is -1, Rank(x) is 2, the real axis is 1
        >>> out1 = paddle.concat(x=[x1, x2, x3], axis=-1)
        >>> out2 = paddle.concat(x=[x1, x2], axis=0)
        >>> out3 = paddle.concat(x=[x1, x2], axis=zero)
        >>> print(out1)
        Tensor(shape=[2, 8], dtype=int64, place=Place(cpu), stop_gradient=True,
        [[1 , 2 , 3 , 11, 12, 13, 21, 22],
         [4 , 5 , 6 , 14, 15, 16, 23, 24]])
        >>> print(out2)
        Tensor(shape=[4, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
        [[1 , 2 , 3 ],
         [4 , 5 , 6 ],
         [11, 12, 13],
         [14, 15, 16]])
        >>> print(out3)
        Tensor(shape=[4, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
        [[1 , 2 , 3 ],
         [4 , 5 , 6 ],
         [11, 12, 13],
         [14, 15, 16]])
r   r  c                     [         R                  " 5       n U R                  n[         R                  R                  [         R                  R
                  /nX;   $ N)r    _get_amp_attrs
_amp_levelAmpLevelO1O2)	amp_attrs	amp_levelapply_amp_level_lists      rj   is_in_amp_modeconcat.<locals>.is_in_amp_mode  sH    ++-I!,,I    $  44r   r5   r8   r<   r=   )rl   rm   bfloat16rn   ro   rp   r@   rq   rw   unit8rs   r   r   :All the Tensors in the input must have the same data type.Fr6   r@   rq   zBThe data type of axis must be int32 or int64 when axis is a Tensorr>   r%   ztIf the elements of 'input' in concat are Variable(DenseTensorArray), number of the elements must be 1, but received r   r;   rA   rB   rE   rG   T
AxisTensorrC   )r8   )#r"   rL   r   r   r   r8   r$   r   rM   r   rN   rT   rU   rV   r   rW   r?   rY   rX   r[   rR   r   r   r\   r]   r^   descrH   r    r|   r}   DENSE_TENSOR_ARRAYr   r_   ry   )rc   r6   r`   rh   r5   r  	is_in_ampidr   rg   ri   rI   rK   s                rj   r8   r8     sG   T EdH%%99Q<D}}Uc22		5 #$	5'D%1A1A#BHM%!1!122"5)(s2w&, %( "	qww%(..'@#T - *4 ufjj..//0022++E?FCJGE}}U))5'D%#:HE%**"5)(s2w&, %( 77eAhnn,#T - *4 GE4#x(;dH%%

'"T 2277$$& 8 
 8==4<<#7#7#J#JJ
 u:? BBEe*QP? AAAPI-U1X!$I;?#%8	  * 
 5\FE$))%)"'+|$ $f	   
r   c                F   [        U 5      n[        5       (       a  [        R                  " U 5      $ [	        U S[
        [        4S5        US:  a  [        S5      e[        U 5       HL  u  p4[        US[        U5      -   S-   / SQS5        UR                  U S   R                  :w  d  MC  [        S	5      e   / n/ nSnU[        U 5      :  a  X   n[        [        UR                  5      5      n	Sn
U
[        U	5      :  a  [        U5      U
::  a%  UR                  X   5        UR                  U5        OXXj   X   :g  =(       a    Xj   S:g  =(       a    X   S:g  nU(       a  XZ   n[        S
U SU SU
 35      eXj   X   ::  a
  X   Xj'   XuU
'   U
S-  n
U
[        U	5      :  a  M  US-  nU[        U 5      :  a  M  [        S0 [!        5       D6nSn
/ nX:  a8  UR                  UR#                  UR%                  5       S95        U
S-  n
X:  a  M8  SU 0nUR'                  SUSU00 S9  U$ )a>	  
Broadcast a list of tensors following broadcast semantics

Note:
    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

The following figure illustrates the process of broadcasting three tensors to the same dimensions.
The dimensions of the three tensors are [4, 1, 3], [2, 3], and [4, 2, 1], respectively. During broadcasting,
alignment starts from the last dimension, and for each dimension, either the sizes of the two tensors in that dimension are equal,
or one of the tensors has a dimension of 1, or one of the tensors lacks that dimension. In the figure below, in the last dimension,
Tensor3 has a size of 1, while Tensor1 and Tensor2 have sizes of 3; thus, this dimension is expanded to 3 for all tensors.
In the second-to-last dimension, Tensor1 has a size of 2, and Tensor2 and Tensor3 both have sizes of 2; hence, this dimension is expanded to 2 for all tensors.
In the third-to-last dimension, Tensor2 lacks this dimension, while Tensor1 and Tensor3 have sizes of 4; consequently,
this dimension is expanded to 4 for all tensors. Ultimately, all tensors are expanded to [4, 2, 3].

.. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/broadcast.png
   :width: 800
   :alt: Illustration of BroadCast
   :align: center

Args:
    input (list|tuple): ``input`` is a Tensor list or Tensor tuple which is with data type bool,
        float16, float32, float64, int32, int64, complex64, complex128. All the Tensors in ``input`` must have same data type.
        Currently we only support tensors with rank no greater than 5.
    name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.

Returns:
    list(Tensor), The list of broadcasted tensors following the same order as ``input``.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> x1 = paddle.rand([1, 2, 3, 4]).astype('float32')
        >>> x2 = paddle.rand([1, 2, 1, 4]).astype('float32')
        >>> x3 = paddle.rand([1, 1, 3, 1]).astype('float32')
        >>> out1, out2, out3 = paddle.broadcast_tensors(input=[x1, x2, x3])
        >>> # out1, out2, out3: tensors broadcasted from x1, x2, x3 with shape [1,2,3,4]
r5   broadcast_tensorsr%   z8At least 1 tensor is needed to perform broadcast_tensorsr<   r=   r   r   r  zIInput tensors to broadcast_tensors does not follow bcast semanticsTensor z conflicts with Tensor z in reversed dimension r>   rA   rC   rG   )r  )r   r#   r   r  r   rM   r   rY   rV   r   rW   r?   reversedrS   r   r   r\   r]   r^   r_   )r5   r`   
num_inputsr  rc    output_shape_r_last_tensor_indexoutput_shape_rjtensorrS   re   invalid
last_indexrg   rh   rI   s                   rj   r  r  w  sr   Z UJ''..5'D%=2EF>J 
 u%EB$3r7"S(
 $  ww%(..(P % &. ,.( #e*nXF&,,/0EAc%j.~&!+"))%(34;;A> ')UX5 **-2*!HM 
 %E%H
'&&0\1HKbcdbeg  &(EH4,1H)>?;Q' c%j.( FA3 #e*n6 =FH=nJJ99 ,,. : 
 FA n u$CL	 	 	
 
r   c                   [        U[        5      (       a  U/n[        5       (       a  [        R                  " X5      $ [        S
0 [        5       D6n[        U S[        S5        UR                  S5      n[        US/ SQS5        [        US[        [        4S5        Uc  UR                  U5      nOUR                  X$SS9nUR                  SSU 0SU0SU0S	9  U$ )aI  
Reverse the order of a n-D tensor along given axis in axis.

The image below illustrates how ``flip`` works.

.. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/flip.png
    :width: 500
    :alt: legend of flip API
    :align: center

Args:
    x (Tensor): A Tensor with shape :math:`[N_1, N_2,..., N_k]` . The data type of the input Tensor x
        should be float32, float64, int32, int64, bool.
    axis (list|tuple|int): The axis(axes) to flip on. Negative indices for indexing from the end are accepted.
    name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.

Returns:
    Tensor, Tensor or DenseTensor calculated by flip layer. The data type is same with input x.

Examples:
    .. code-block:: python

        >>> # doctest: +SKIP("This has diff in xdoctest env")
        >>> import paddle

        >>> image_shape=(3, 2, 2)
        >>> img = paddle.arange(image_shape[0] * image_shape[1] * image_shape[2]).reshape(image_shape)
        >>> tmp = paddle.flip(img, [0,1])
        >>> print(tmp)
        Tensor(shape=[3, 2, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
        [[[10, 11],
          [8 , 9 ]],
         [[6 , 7 ],
          [4 , 5 ]],
         [[2 , 3 ],
          [0 , 1 ]]])

        >>> out = paddle.flip(tmp,-1)
        >>> print(out)
        Tensor(shape=[3, 2, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
        [[[11, 10],
          [9 , 8 ]],
         [[7 , 6 ],
          [5 , 4 ]],
         [[3 , 2 ],
          [1 , 0 ]]])
fliprA   rc   rm   rn   ro   r@   rq   rl   r6   F)r`   r?   persistablerC   rG   )r'  )rL   rR   r#   r   r'  r   r\   r   r   r^   r   rM   r   r]   create_variabler_   )rc   r6   r`   rg   r?   rh   s         rj   r'  r'    s    d $v{{1##0vx01cHv.""3'G		
 	4$7<;;EBC((E ) C 	8CL4.	 	 	
 
r   c                   [        S0 [        5       D6n[        U S[        [        R
                  R                  4S5        UR                  S5      n[        US/ SQS5        [        US[        [        4S5        [        U R                  5      n[        U5      nUS:w  a  [        SU 35      eUS:  a  [        SU 35      eUS	   US
   :w  a  [        US	   US
   -
  5      U:w  d  [        SUS	    SUS
    35      eUS	   U:  a
  US	   U* :  d  [        SUS	    35      eUS
   U:  a
  US
   U* :  d  [        SUS
    35      eUS-  nUS	:X  a  U $ US:X  a  [        [        XS	   5      US
   5      $ [        [!        S	U5      5      nXS
      XS	      sXS	   '   XS
   '   US
:X  a  [#        [        XS
   5      U5      $ [        [#        X5      US
   5      $ )a]  
Rotate a n-D tensor by 90 degrees. The rotation direction and times are specified by axes and the absolute value of k. Rotation direction is from axes[0] towards axes[1] if k > 0, and from axes[1] towards axes[0] for k < 0.

Args:
    x (Tensor): The input Tensor. The data type of the input Tensor x
        should be float16, float32, float64, int32, int64, bool. float16 is only supported on gpu.
    k (int, optional): Direction and number of times to rotate, default value: 1.
    axes (list|tuple, optional): Axes to rotate, dimension must be 2. default value: [0, 1].
    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:
    Tensor, Tensor or DenseTensor calculated by rot90 layer. The data type is same with input x.

Examples:
    .. code-block:: python

        >>> import paddle

        >>> data = paddle.arange(4)
        >>> data = paddle.reshape(data, (2, 2))
        >>> print(data.numpy())
        [[0 1]
         [2 3]]

        >>> y = paddle.rot90(data, 1, [0, 1])
        >>> print(y.numpy())
        [[1 3]
         [0 2]]

        >>> y= paddle.rot90(data, -1, [0, 1])
        >>> print(y.numpy())
        [[2 0]
         [3 1]]

        >>> data2 = paddle.arange(8)
        >>> data2 = paddle.reshape(data2, (2,2,2))
        >>> print(data2.numpy())
        [[[0 1]
          [2 3]]
         [[4 5]
          [6 7]]]

        >>> y = paddle.rot90(data2, 1, [1, 2])
        >>> print(y.numpy())
        [[[1 3]
          [0 2]]
         [[5 7]
          [4 6]]]
rot90rA   rc   r(  r   r   z2expected total rotation axes == 2, but got axes = z/expected total dims >= 2, but got total dims = r   r%   z8expected rotation axes to be different, but got axis0 = z, and axis1 = z%Rotation axis0 out of range, axis0 = z%Rotation axis1 out of range, axis1 =    )r,  )r   r\   r   r   rN   rT   rU   r^   r   rM   r   r   rS   r   absr'  r   r   )	rc   kr   r`   rg   r?   input_total_dimstotal_rot_dims	axes_lists	            rj   r,  r,  N  s*   l -FH-Fq#&**"2"23W=s#EC	 tVdE]G4177|YN@@PQ
 	
 !=>N=OP
 	
 GtAw3tAwa'8#9=M#MFtAwi~^bcd^e]fg
 	
 G&&477G6G+G@a	JKKG&&477G6G+G@a	JKKFAAvAvDG$d1g..U1./0Iq'q' -YAw7+ 	Avaa)955 Ia+T!W55r   	start_dimend_dim)rc   
start_axis	stop_axisc                x   [        U [        [        R                  R                  45      (       d  [        S5      e[        U R                  5      nUS:X  aM  [        U[        5      (       a  US;  a  [        S5      e[        U[        5      (       a  US;  a  [        S5      eO[        U[        5      (       a  XS-
  :  d  X* :  a  [        S5      e[        U[        5      (       a  X$S-
  :  d  X$* :  a  [        S5      eUS:  a  X-   nUS:  a  X$-   nX:  a  [        S	5      e[        5       (       a  [        R                  " XU5      $ [        U S
/ SQS5        [        S0 [        5       D6nUR                  U R                   5      nUR                  U R                   5      nUR#                  SSU 0XgS.XS.S9  U$ )aB	  
Flattens a contiguous range of axes in a tensor according to start_axis and stop_axis.

Note:
    The output Tensor will share data with origin Tensor and doesn't have a Tensor copy in ``dygraph`` mode.
    If you want to use the Tensor copy version, please use `Tensor.clone` like ``flatten_clone_x = x.flatten().clone()``.

For Example:

.. code-block:: text

    Case 1:

      Given
        X.shape = (3, 100, 100, 4)

      and
        start_axis = 1
        end_axis = 2

      We get:
        Out.shape = (3, 100 * 100, 4)

    Case 2:

      Given
        X.shape = (3, 100, 100, 4)

      and
        start_axis = 0
        stop_axis = -1

      We get:
        Out.shape = (3 * 100 * 100 * 4)

.. note::
    Alias Support: The parameter name ``input`` can be used as an alias for ``x``, the parameter name ``start_dim`` can be used as an alias for ``start_axis`` , and the parameter name ``end_dim`` can be used as an alias for ``stop_axis``.
    For example, ``flatten(input=tensor_x, start_dim=0, end_dim=-1)`` is equivalent to ``flatten(x=tensor_x, start_axis=0, stop_axis=-1)``.

Args:
    x (Tensor): A tensor of number of dimensions >= axis. A tensor with data type float16, float32,
                  float64, int8, int32, int64, uint8.
        alias: ``input``.
    start_axis (int): the start axis to flatten
        alias: ``start_dim``.
    stop_axis (int): the stop axis to flatten
        alias: ``end_dim``.
    name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.

Returns:
    Tensor, A tensor with the contents of the input tensor, whose input axes are flattened by indicated :attr:`start_axis` and :attr:`end_axis`, and data type is the same as input :attr:`x`.

Examples:

    .. code-block:: pycon

        >>> import paddle

        >>> image_shape = (2, 3, 4, 4)

        >>> x = paddle.arange(end=image_shape[0] * image_shape[1] * image_shape[2] * image_shape[3])
        >>> img = paddle.reshape(x, image_shape)

        >>> out = paddle.flatten(img, start_axis=1, stop_axis=2)
        >>> print(out.shape)
        paddle.Size([2, 12, 4])

        >>> # out shares data with img in dygraph mode
        >>> img[0, 0, 0, 0] = -1
        >>> print(out[0, 0, 0])
        Tensor(shape=[], dtype=int64, place=Place(cpu), stop_gradient=True,
        -1)
The input x should be a Tensorr   )r   r   zYThe start_axis should be int, and should be 0 or -1 when the input tensor is a 0-D-TensorzXThe stop_axis should be int, and should be 0 or -1 when the input tensor is a 0-D-Tensorr%   @The start_axis should be a int, and in range [-rank(x), rank(x))?The stop_axis should be a int, and in range [-rank(x), rank(x))-The stop_axis should be larger than stat_axisrc   )	rm   rn   ro   rw   rp   r@   rq   rr   rs   flattenflatten_contiguous_rangerA   r   )r5  r6  rG   r<  )rL   r   rN   rT   rU   r   r   rS   rR   r#   r   r<  r   r   r\   r]   r?   r_   )rc   r5  r6  r`   x_dimrg   rh   r   s           rj   r<  r<    s   ^ q8VZZ%5%56779::LEz:s++
'0Ik  9c**y/Gj  0H J,,QY&F"R  Is++AI%6!Q  >#+Jq=!)I!LMM~~aY77 
 	
  3&(377@;;AGGD+83!+D	 	 	
 
r   c                    [        U 5      $ )a  
Return a contiguous flattened tensor. A copy is made only if needed.
Note:
    The output Tensor will share data with origin Tensor and doesn't have a Tensor copy in ``dygraph`` mode.
    If you want to use the Tensor copy version, please use `Tensor.clone` like ``ravel_clone_x = x.ravel().clone()``.
    For example:

    .. code-block:: text
        Case 1:
          Given
            X.shape = (3, 100, 100, 4)

          We get:
            Out.shape = (3 * 100 * 100 * 4)
Args:
    x (Tensor): A tensor of number of dimensions >= axis. A tensor with data type float16, float32,
                  float64, int8, int32, int64, uint8.

Returns:
    Tensor: A tensor with the contents of the input tensor, whose input axes are flattened by indicated :attr:`start_axis` and :attr:`stop_axis`, and data type is the same as input :attr:`x`.

Examples:

    .. code-block:: pycon

        >>> import paddle

        >>> image_shape = (2, 3, 4, 4)

        >>> x = paddle.arange(end=image_shape[0] * image_shape[1] * image_shape[2] * image_shape[3])
        >>> img = paddle.reshape(x, image_shape)

        >>> out = paddle.ravel(img)
        >>> print(out.shape)
        paddle.Size([96])
r>  r5   s    rj   ravelrB  I  s    J 5>r   c                   [        U [        5      (       d  [        S5      e[        U R                  5      n[        U[
        5      (       a  XS-
  :  d  X* :  a  [        S5      e[        U[
        5      (       a  X$S-
  :  d  X$* :  a  [        S5      eUS:  a  X-   nUS:  a  X$-   nX:  a  [        S5      e[        5       (       a  [        R                  " XU5      $ g)z
Inplace version of ``flatten`` API, the output Tensor will be inplaced with input ``x``.
Please refer to :ref:`api_paddle_flatten`.
r8  r%   r9  r:  r   r;  N)	rL   r   r   r   rS   rR   r"   r   flatten_)rc   r5  r6  r`   r?  s        rj   rD  rD  q  s     q(##9::LE
C(("N
 	
 	3''	!vM
 	
 A~'
1}%	HIIqi88 r   c          
        Uc  SOUn[        5       (       a  [        R                  " XUS9$ [        U [        5      (       d  [        U [
        5      (       d  [        U [        5      (       a@  U R                  R                  5       [        R                  R                  R                  :X  d>  [        U [        R                  R                  5      (       a  U R!                  5       (       a  U /n O&[#        SR%                  SSS[        U 5      5      5      e['        5       (       al  U S   R!                  5       (       a>  [)        U 5      S:X  d   S[)        U 5       S	35       e[        R*                  " XS
5      u  p4U$ [        R                  " X5      $ [-        S0 [/        5       D6nUR1                  U S   R2                  5      nU S   R                  R                  5       [        R                  R                  R                  :X  al  [)        U 5      S:X  d   S[)        U 5       S	35       eUR1                  SS9nU  H  n[5        US/ SQS5        M     UR7                  SSU S   0U/U/S.US
S.S9  U$ UR7                  SSU 0SU0SU0S9  U$ )al  
Stacks all the input tensors ``x`` along ``axis`` dimension.
All tensors must be of the same shape and same dtype.

For example, given N tensors of shape [A, B], if ``axis == 0``, the shape of stacked
tensor is [N, A, B]; if ``axis == 1``, the shape of stacked
tensor is [A, N, B], etc.

It also supports the operation with zero-size tensors which contain 0 in their shape.
See the examples below.

.. code-block:: text

    Case 1:

      Input:
        x[0].shape = [1, 2]
        x[0].data = [ [1.0 , 2.0 ] ]
        x[1].shape = [1, 2]
        x[1].data = [ [3.0 , 4.0 ] ]
        x[2].shape = [1, 2]
        x[2].data = [ [5.0 , 6.0 ] ]

      Attrs:
        axis = 0

      Output:
        Out.dims = [3, 1, 2]
        Out.data =[ [ [1.0, 2.0] ],
                    [ [3.0, 4.0] ],
                    [ [5.0, 6.0] ] ]


    Case 2:

      Input:
        x[0].shape = [1, 2]
        x[0].data = [ [1.0 , 2.0 ] ]
        x[1].shape = [1, 2]
        x[1].data = [ [3.0 , 4.0 ] ]
        x[2].shape = [1, 2]
        x[2].data = [ [5.0 , 6.0 ] ]


      Attrs:
        axis = 1 or axis = -2  # If axis = -2, axis = axis+ndim(x[0])+1 = -2+2+1 = 1.

      Output:
        Out.shape = [1, 3, 2]
        Out.data =[ [ [1.0, 2.0]
                      [3.0, 4.0]
                      [5.0, 6.0] ] ]


    Case 3:

        Input:
            x[0].shape = [0, 1, 2]
            x[0].data = []
            x[1].shape = [0, 1, 2]
            x[1].data = []

        Attrs:
            axis = 0

        Output:
            Out.shape = [2, 0, 1, 2]
            Out.data = []


    Case 4:

        Input:
            x[0].shape = [0, 1, 2]
            x[0].data = []
            x[1].shape = [0, 1, 2]
            x[1].data = []

        Attrs:
            axis = 1

        Output:
            Out.shape = [0, 2, 1, 2]
            Out.data = []

The image below demonstrates the Case 1: three 2-dimensional tensors with shape [1, 2] are stacked in the dimension of axis=0 to form a 3-dimensional tensor with shape [3, 1, 2] .

.. figure:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/stack/stack-0.png
   :width: 1000
   :alt: Legend 1
   :align: center

Args:
    x (list[Tensor]|tuple[Tensor]): Input ``x`` can be a ``list`` or ``tuple`` of tensors, the Tensors in ``x``
                                 must be of the same shape and dtype. Supported data types: float32, float64, int32, int64. Alias: ``tensors``.
    axis (int, optional): The axis along which all inputs are stacked. ``axis`` range is ``[-(R+1), R+1)``,
                          where ``R`` is the number of dimensions of the first input tensor ``x[0]``.
                          If ``axis < 0``, ``axis = axis+R+1``. The default value of axis is 0. Alias: ``dim``.
    name (str, 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. If set, the output will be written to this tensor.

Returns:
    Tensor, The stacked tensor with same data type as input.

Examples:
    .. code-block:: pycon

        >>> import paddle

        >>> x1 = paddle.to_tensor([[1.0, 2.0]])
        >>> x2 = paddle.to_tensor([[3.0, 4.0]])
        >>> x3 = paddle.to_tensor([[5.0, 6.0]])

        >>> out = paddle.stack([x1, x2, x3], axis=0)
        >>> print(out.shape)
        paddle.Size([3, 1, 2])
        >>> print(out)
        Tensor(shape=[3, 1, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[[1., 2.]],
         [[3., 4.]],
         [[5., 6.]]])

        >>> out = paddle.stack([x1, x2, x3], axis=-2)
        >>> print(out.shape)
        paddle.Size([1, 3, 2])
        >>> print(out)
        Tensor(shape=[1, 3, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[[1., 2.],
          [3., 4.],
          [5., 6.]]])

        >>> # zero-size tensors
        >>> x1 = paddle.ones([0, 1, 2])
        >>> x2 = paddle.ones([0, 1, 2])

        >>> out = paddle.stack([x1, x2], axis=0)
        >>> print(out.shape)
        paddle.Size([2, 0, 1, 2])
        >>> print(out)
        Tensor(shape=[2, 0, 1, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[],
         []])

        >>> out = paddle.stack([x1, x2], axis=1)
        >>> print(out.shape)
        paddle.Size([0, 2, 1, 2])
        >>> print(out)
        Tensor(shape=[0, 2, 1, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
        [])
r   r  z2The type of '{}' in {} must be {}, but received {}rc   r9   z*list[Tensor], tuple[Tensor] or TensorArrayr%   zoIf the elements of 'x' in stack are Variable(DenseTensorArray), number of the elements must be 1, but received r   Tr@   r>   rm   rn   ro   r@   rq   rs   r;   rA   rB   rE   rG   r   r6   )r9   )r"   r   r9   rL   rM   r   r   r  rH   r    r|   r}   r  rN   rT   rU   rX   rY   formatr$   r   r[   r   r\   r]   r?   r   r_   )rc   r6   r`   rh   r   rg   ri   re   s           rj   r9   r9     sc   | 1$D||A--az!U';'; q(##!5!5!H!HHq&****++0L0L0N0NADKK@G	  }}Q4**,,q6Q; BBEa&L; ++AT:FCJ<<((-FH-F

3
3AaDJJ
?Ctyy~~4<<//BBB1v{ 	
>>A!fXQH	
{ ==G=L	A$   	)1; E	{;d3	 	 	
 J 	8#J4.	 	 	
 Jr   c                    [         R                  " U 6 n[        U[        5      (       d  U/nU(       a)  US   R                  S:X  a  [         R
                  " USUS9$ [         R
                  " USUS9$ )a+  
Stacks all the input tensors ``x`` along horizontal axis.
All tensors must be of the same dtype.

The image below illustrates how ``hstack`` works.

.. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/hstack.png
    :width: 500
    :alt: legend of hstack API
    :align: center

Args:
    x (list[Tensor]|tuple[Tensor]): Input ``x`` can be a ``list`` or ``tuple`` of tensors, the Tensors in ``x`` must be of the same
        shape and dtype. Supported data types: ``float16``, ``float32``, ``float64``, ``int8``, ``int32``, ``int64`` or ``bfloat16``.
    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 stacked tensor with same data type as input.

Examples:
    .. code-block:: python

        >>> import paddle

        >>> # hstack with 0-D tensors
        >>> x1 = paddle.to_tensor(1.0)
        >>> x2 = paddle.to_tensor(2.0)
        >>> out = paddle.hstack((x1, x2))
        >>> print(out)
        Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
        [1., 2.])

        >>> # hstack with 1-D tensors
        >>> x1 = paddle.to_tensor([1.0, 2.0])
        >>> x2 = paddle.to_tensor([3.0, 4.0, 5.0])
        >>> out = paddle.hstack((x1, x2))
        >>> print(out)
        Tensor(shape=[5], dtype=float32, place=Place(cpu), stop_gradient=True,
        [1., 2., 3., 4., 5.])

        >>> # hstack mix with 0-D & 1-D tensors
        >>> x1 = paddle.to_tensor(1.0)
        >>> x2 = paddle.to_tensor([3.0, 4.0, 5.0])
        >>> out = paddle.hstack((x1, x2))
        >>> print(out)
        Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
        [1., 3., 4., 5.])

        >>> # hstack with 2-D tensors
        >>> x1 = paddle.to_tensor([[1.0, 2.0]])
        >>> x2 = paddle.to_tensor([[3.0, 4.0, 5.0]])
        >>> out = paddle.hstack((x1, x2))
        >>> print(out)
        Tensor(shape=[1, 5], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[1., 2., 3., 4., 5.]])

r   r%   r6   r`   )rN   
atleast_1drL   rM   r   r8   rc   r`   arrayss      rj   hstackrM  	  sa    t "Ffd##&)..A%}}V!$77}}V!$77r   c                    [         R                  " U 6 n[        U[        5      (       d  U/n[         R                  " USUS9$ )a  
Stacks all the input tensors ``x`` along vertical axis.
All tensors must be of the same dtype.

The image below illustrates how ``vstack`` works.

.. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/vstack.png
    :width: 500
    :alt: legend of vstack API
    :align: center

Args:
    x (list[Tensor]|tuple[Tensor]): Input ``x`` can be a ``list`` or ``tuple`` of tensors, the Tensors in ``x`` must be of the same
        shape and dtype. Supported data types: ``float16``, ``float32``, ``float64``, ``int8``, ``int32``, ``int64`` or ``bfloat16``.
    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 stacked tensor with same data type as input.

Examples:
    .. code-block:: python

        >>> import paddle

        >>> # vstack with 0-D tensors
        >>> x1 = paddle.to_tensor(1.0)
        >>> x2 = paddle.to_tensor(2.0)
        >>> out = paddle.vstack((x1, x2))
        >>> print(out)
        Tensor(shape=[2, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[1.],
         [2.]])

        >>> # vstack with 1-D tensors
        >>> x1 = paddle.to_tensor([1.0, 2.0, 3.0])
        >>> x2 = paddle.to_tensor([3.0, 4.0, 5.0])
        >>> out = paddle.vstack((x1, x2))
        >>> print(out)
        Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[1., 2., 3.],
         [3., 4., 5.]])

        >>> # vstack mix with 1-D & 2-D tensors
        >>> x1 = paddle.to_tensor([1.0, 2.0, 3.0])
        >>> x2 = paddle.to_tensor([[3.0, 4.0, 5.0]])
        >>> out = paddle.vstack((x1, x2))
        >>> print(out)
        Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[1., 2., 3.],
         [3., 4., 5.]])

        >>> # vstack with 2-D tensors
        >>> x1 = paddle.to_tensor([[1.0, 2.0, 3.0]])
        >>> x2 = paddle.to_tensor([[3.0, 4.0, 5.0]])
        >>> out = paddle.vstack((x1, x2))
        >>> print(out)
        Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[1., 2., 3.],
         [3., 4., 5.]])

r   rI  )rN   
atleast_2drL   rM   r8   rK  s      rj   vstackrP  	  s<    | "Ffd##==ad33r   c                    [         R                  " U 6 n[        U[        5      (       d  U/n[         R                  " USUS9$ )a|  
Stacks all the input tensors ``x`` along depth axis.
All tensors must be of the same dtype.

Args:
    x (list[Tensor]|tuple[Tensor]): Input ``x`` can be a ``list`` or ``tuple`` of tensors, the Tensors in ``x`` must be of the same
        shape and dtype. Supported data types: ``float16``, ``float32``, ``float64``, ``int8``, ``int32``, ``int64`` or ``bfloat16``.
    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 stacked tensor with same data type as input.

Examples:
    .. code-block:: python

        >>> import paddle

        >>> # dstack with 0-D tensors
        >>> x1 = paddle.to_tensor(1.0)
        >>> x2 = paddle.to_tensor(2.0)
        >>> out = paddle.dstack((x1, x2))
        >>> print(out)
        Tensor(shape=[1, 1, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[[1., 2.]]])

        >>> # dstack with 1-D tensors
        >>> x1 = paddle.to_tensor([1.0, 2.0, 3.0])
        >>> x2 = paddle.to_tensor([3.0, 4.0, 5.0])
        >>> out = paddle.dstack((x1, x2))
        >>> print(out)
        Tensor(shape=[1, 3, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[[1., 3.],
          [2., 4.],
          [3., 5.]]])

        >>> # dstack with 3-D tensors
        >>> x1 = paddle.to_tensor([[[1.0, 2.0], [3.0, 4.0]]])
        >>> x2 = paddle.to_tensor([[[3.0, 4.0], [5.0, 6.0]]])
        >>> out = paddle.dstack((x1, x2))
        >>> print(out)
        Tensor(shape=[1, 2, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[[1., 2., 3., 4.],
          [3., 4., 5., 6.]]])

r   rI  )rN   
atleast_3drL   rM   r8   rK  s      rj   dstackrS  
  s<    \ "Ffd##==ad33r   c                    / nU  HV  nUR                   S:  a2  UR                  UR                  UR                  5       S45      5        ME  UR                  U5        MX     [        R
                  " USUS9$ )a-  
Stacks all the input tensors ``x`` along horizontal axis. Each tensor in ``x`` will be first reshaped into ``(tensor.numel(), 1)``
if ``tensor.ndim < 2`` before being stacked.
All tensors must be of the same dtype.

Args:
    x (list[Tensor]|tuple[Tensor]): Input ``x`` can be a ``list`` or ``tuple`` of tensors, the Tensors in ``x`` must be of the same
        shape and dtype. Supported data types: ``float16``, ``float32``, ``float64``, ``int32``, ``int64`` or ``bfloat16``.
    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 stacked tensor with same data type as input.

Examples:
    .. code-block:: python

        >>> import paddle

        >>> # column_stack with 0-D tensors
        >>> x1 = paddle.to_tensor(1.0)
        >>> x2 = paddle.to_tensor(2.0)
        >>> out = paddle.column_stack((x1, x2))
        >>> print(out)
        Tensor(shape=[1, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[1., 2.]])

        >>> # column_stack mix with 1-D & 2-D tensors
        >>> x1 = paddle.to_tensor([[1.0], [2.0], [3.0]])
        >>> x2 = paddle.to_tensor([3.0, 4.0, 5.0])
        >>> out = paddle.column_stack((x1, x2))
        >>> print(out)
        Tensor(shape=[3, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[1., 3.],
         [2., 4.],
         [3., 5.]])

        >>> # column_stack with 3-D tensors
        >>> x1 = paddle.to_tensor([[[1.0, 2.0], [3.0, 4.0]]])
        >>> x2 = paddle.to_tensor([[[3.0, 4.0], [5.0, 6.0]]])
        >>> out = paddle.column_stack((x1, x2))
        >>> print(out)
        Tensor(shape=[1, 4, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[[1., 2.],
          [3., 4.],
          [3., 4.],
          [5., 6.]]])

r   r%   rI  )r   r   r   numelrN   r8   )rc   r`   rL  r#  s       rj   column_stackrV  B
  s`    b F;;?MM&..&,,.!)<=>MM&!	  ==ad33r   c                *    [         R                  " XS9$ )a  
Alias of `paddle.vstack()`.
Stacks all the input tensors ``x`` along vertical axis.
All tensors must be of the same dtype.

Args:
    x (list[Tensor]|tuple[Tensor]): Input ``x`` can be a ``list`` or ``tuple`` of tensors, the Tensors in ``x`` must be of the same
        shape and dtype. Supported data types: ``float16``, ``float32``, ``float64``, ``int8``, ``int32``, ``int64`` or ``bfloat16``.
    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 stacked tensor with same data type as input.

Examples:
    .. code-block:: python

        >>> import paddle

        >>> # row_stack with 0-D tensors
        >>> x1 = paddle.to_tensor(1.0)
        >>> x2 = paddle.to_tensor(2.0)
        >>> out = paddle.row_stack((x1, x2))
        >>> print(out)
        Tensor(shape=[2, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[1.],
         [2.]])

        >>> # row_stack with 1-D tensors
        >>> x1 = paddle.to_tensor([1.0, 2.0, 3.0])
        >>> x2 = paddle.to_tensor([3.0, 4.0, 5.0])
        >>> out = paddle.row_stack((x1, x2))
        >>> print(out)
        Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[1., 2., 3.],
         [3., 4., 5.]])

        >>> # row_stack mix with 1-D & 2-D tensors
        >>> x1 = paddle.to_tensor([1.0, 2.0, 3.0])
        >>> x2 = paddle.to_tensor([[3.0, 4.0, 5.0]])
        >>> out = paddle.row_stack((x1, x2))
        >>> print(out)
        Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[1., 2., 3.],
         [3., 4., 5.]])

        >>> # row_stack with 2-D tensors
        >>> x1 = paddle.to_tensor([[1.0, 2.0, 3.0]])
        >>> x2 = paddle.to_tensor([[3.0, 4.0, 5.0]])
        >>> out = paddle.row_stack((x1, x2))
        >>> print(out)
        Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[1., 2., 3.],
         [3., 4., 5.]])

r`   )rN   rP  )rc   r`   s     rj   	row_stackrY  ~
  s    p ==&&r   >   r   r#  split_size_or_sectionszpaddle.splitzpaddle.compat.splitztorch.split)illegal_keys	func_namecorrect_name
url_suffixc                ^  ^ U nUn[        5       (       GaX  [        U[        5      (       a  UR                  S5      nU[	        UR
                  5      -   S:  d   S5       eUS:  a  U[	        UR
                  5      -   OUn[        U[        [        45      (       ad  [        R                  R                  U5      (       a?  [        U5       H0  u  pg[        U[        5      (       d  M  X   R                  5       X'   M2     O-[        U[        5      (       d  [        S[        U5       S35      e[        U[        5      (       a  [        R                   " XAU5      $ [        R"                  " XAU5      $ [%        5       (       Ga  [        U[        R&                  R(                  5      (       a  SUl        [        U[        5      (       aC  [	        UR
                  5      U-   S:  d   S5       eUS:  a  [	        UR
                  5      U-   OUnUR
                  n[        U[        [        [        45      (       d  [        S[        U5       S35      e[        U[        5      (       a\  US:  d   S5       e[        U[        5      (       a#  X   S:  a  X   U-  S:X  d   SU SX    S	35       e[        R                   " XAU5      $ [        U[        5      (       a   X   S:  a  [	        U5      X   ::  d   S
5       e[        R                  R                  U5      (       a  [        R                  R-                  U5      n[        R"                  " XAU5      $ [/        US/ SQS5        [1        US[        [        [        4S5        [1        US[        [        4S5        [        U[        5      (       a  [3        UR4                  SSS/S5        [7        S0 [9        5       D6mUR
                  nSU0n	S[        U[        5      (       a  UOS0n
U4S jn[        U[        5      (       a  SUl        XYS'   O=[	        UR
                  5      U-   S:  d   S5       eUS:  a  [	        U5      U-   OUnXZS'   [        U[        5      (       aH  US:  d   S5       e[        U[        5      (       a#  X   S:  a  X   U-  S:X  d   SU SX    S	35       eUnO[        U[        5      (       a   X   S:  a  [	        U5      X   ::  d   S
5       e[	        U5      nU Vs/ s H  n[        U[        5      (       a  SOUPM     snU
S'   [        R                  R                  U5      (       a  U" U5      U	S'   [;        U5       Vs/ s H   nTR=                  TR?                  5       S9PM"     nnTRA                  SU	SU0U
S9  U$ s  snf s  snf )a{	  
Split the input tensor into multiple sub-Tensors.

Args:
    x (Tensor): A N-D Tensor. The data type is bool, bfloat16, float16, float32, float64, uint8, int8, int32 or int64.
    num_or_sections (int|list|tuple): If ``num_or_sections`` is an int, then ``num_or_sections``
        indicates the number of equal sized sub-Tensors that the ``x`` will be divided into.
        If ``num_or_sections`` is a list or tuple, the length of it indicates the number of
        sub-Tensors and the elements in it indicate the sizes of sub-Tensors'  dimension orderly.
        The length of the list must not  be larger than the ``x`` 's size of specified ``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.
    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:
    list(Tensor), The list of segmented Tensors.

Examples:
    .. code-block:: pycon

        >>> import paddle

        >>> # x is a Tensor of shape [3, 9, 5]
        >>> x = paddle.rand([3, 9, 5])

        >>> out0, out1, out2 = paddle.split(x, num_or_sections=3, axis=1)
        >>> print(out0.shape)
        paddle.Size([3, 3, 5])
        >>> print(out1.shape)
        paddle.Size([3, 3, 5])
        >>> print(out2.shape)
        paddle.Size([3, 3, 5])

        >>> out0, out1, out2 = paddle.split(x, num_or_sections=[2, 3, 4], axis=1)
        >>> print(out0.shape)
        paddle.Size([3, 2, 5])
        >>> print(out1.shape)
        paddle.Size([3, 3, 5])
        >>> print(out2.shape)
        paddle.Size([3, 4, 5])

        >>> out0, out1, out2 = paddle.split(x, num_or_sections=[2, 3, -1], axis=1)
        >>> print(out0.shape)
        paddle.Size([3, 2, 5])
        >>> print(out1.shape)
        paddle.Size([3, 3, 5])
        >>> print(out2.shape)
        paddle.Size([3, 4, 5])

        >>> # axis is negative, the real axis is (rank(x) + axis)=1
        >>> out0, out1, out2 = paddle.split(x, num_or_sections=3, axis=-2)
        >>> print(out0.shape)
        paddle.Size([3, 3, 5])
        >>> print(out1.shape)
        paddle.Size([3, 3, 5])
        >>> print(out2.shape)
        paddle.Size([3, 3, 5])
r   z(rank(x) + axis) must >= 0zcThe type of 'num_or_sections' in split must be int, list or tuple in imperative mode, but received r   Tznum_or_sections must be than 0.zbThe input's size along the split dimension must be evenly divisible by Attr(num_or_sections). But z is not evenly divisible by z. z<len(num_or_sections) must not be more than input.shape[dim].r5   )
rl   r  rm   rs   rn   ro   r@   rq   rr   rw   splitnum_or_sectionsr   r@   rq   rA   r   c           	     V  > / nSn[        U 5       H  u  p4[        U[        5      (       a  SUl        UR	                  U5        M4  [        U[
        5      (       d   eUS:X  a  US:X  d   SU S35       eUnTR                  S5      n[        S/SUSUS9  UR	                  U5        M     U$ )Nr   TzSOnly one value of 'num_or_section' in split can be -1. But received num_or_section[z] is also -1.r@   r%   r   )rV   rL   r   ry   r   rR   r]   r   )one_listtensor_listunk_dim_idxr   r   r   rg   s         rj   _get_SectionsTensorList&split.<locals>._get_SectionsTensorList[  s    KK!*8!4h11-1H*&&x0%h44442~*b0 BBEmU0 '*%HH H "Wh$H  &&x0% "5& r   r  r6   r   sectionsSectionsTensorListr>   rC   rG   )r`  )!r"   rL   r   r   r   rS   rM   r   rN   r   r   rV   rR   rY   rH   r   split_with_numr`  r$   rT   rU   ry   r   r   r   r   r?   r   r\   r   r]   r^   r_   )rc   ra  r6   r`   r5   r   indexr   input_shaperI   rK   rf  r   elere   r   rg   s                   @rj   r`  r`  
  s   P E
Cc8$$((1+CS%%*H,HH**-'sS%%soe}55||((99#,_#=KE!$111@1G1L1L1N. $> OS11 12!5 
 os++((EE<<<<	c6::++,, $Cc3u{{#c)Q.L0LL..1Ag3u{{#c)3Ckk/Cu+=>> 12!5  os++"Q&I(II&#s##(81(<"'/9Q> *++GHXGYY[]>
 ((EE#s##(81(<?+{/?? R? ||((99"(,,"B"B## <<<< 	! 	
" 	.sE0BG	
 	3X8c8$$		57G*<gF11kkuj#&F&F?A
	0 c8$$ $C#&< u{{#c)Q.L0LL..1Ag3{#c)3C&Mos++"Q&I(II&#s##(81(<"'/9Q> *++GHXGYY[]>
 "C#s##(81(<?+{/?? R? o&C +!*C !h//S8*!E* ||((99/F#0+, 3Z	
   55((* 6   	 	 
 	%e 	 	
 %!
s   #V%&'V*c                T   U R                   S::  d  U R                   U::  a  [        SU SU R                    35      eU R                  U   nS nS n[        U[        5      (       a	  U" XX5      $ [        U[
        [        45      (       a	  U" XX5      $ [        S[        U5       35      e)aC  
Split the input tensor into multiple sub-Tensors along ``axis``, allowing not being of equal size.

In the following figure, the shape of Tenser x is [6], and after paddle.tensor_split(x, num_or_indices=4) transformation, we get four sub-Tensors out0, out1, out2, and out3 :

.. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/tensor_split/tensor_split-1_en.png

since the length of x in axis = 0 direction 6 is not divisible by num_or_indices = 4,
the size of the first int(6 % 4) part after splitting will be int(6 / 4) + 1
and the size of the remaining parts will be int(6 / 4).

.. note::
    Alias Support: The parameter name ``input`` can be used as an alias for ``x``, ``indices_or_sections`` can be used as an alias for ``num_or_indices``, and ``dim`` can be used as an alias for ``axis``.
    For example, ``tensor_split(input=tensor_x, indices=[2,4], dim=1, ...)`` is equivalent to ``tensor_split(x=tensor_x, num_or_indices=[2,4], axis=1, ...)``.

Args:
    x (Tensor): A Tensor whose dimension must be greater than 0. The data type is bool, bfloat16, float16, float32, float64, uint8, int32 or int64.
        alias: ``input``
    num_or_indices (int|list|tuple): If ``num_or_indices`` is an int ``n``, ``x`` is split into ``n`` sections along ``axis``.
        If ``x`` is divisible by ``n``, each section will be ``x.shape[axis] / n``. If ``x`` is not divisible by ``n``, the first
        ``int(x.shape[axis] % n)`` sections will have size ``int(x.shape[axis] / n) + 1``, and the rest will be ``int(x.shape[axis] / n).
        If ``num_or_indices`` is a list or tuple of integer indices, ``x`` is split along ``axis`` at each of the indices. For instance,
        ``num_or_indices=[2, 4]`` with ``axis=0`` would split ``x`` into ``x[:2]``, ``x[2:4]`` and ``x[4:]`` along axis 0.
        alias: ``indices`` or ``sections``
    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:
    list[Tensor], The list of segmented Tensors.

Examples:
    .. code-block:: pycon
        :name: tensor-split-example-1

        >>> import paddle

        >>> # evenly split
        >>> # x is a Tensor of shape [8]
        >>> x = paddle.rand([8])
        >>> out0, out1 = paddle.tensor_split(x, num_or_indices=2)
        >>> print(out0.shape)
        paddle.Size([4])
        >>> print(out1.shape)
        paddle.Size([4])


    .. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/tensor_split/tensor_split-2.png

    .. code-block:: pycon
        :name: tensor-split-example-2

        >>> import paddle

        >>> # not evenly split
        >>> # x is a Tensor of shape [8]
        >>> x = paddle.rand([8])
        >>> out0, out1, out2 = paddle.tensor_split(x, num_or_indices=3)
        >>> print(out0.shape)
        paddle.Size([3])
        >>> print(out1.shape)
        paddle.Size([3])
        >>> print(out2.shape)
        paddle.Size([2])

    .. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/tensor_split/tensor_split-3_en.png

    .. code-block:: pycon
        :name: tensor-split-example-3

        >>> import paddle

        >>> # split with indices
        >>> # x is a Tensor of shape [8]
        >>> x = paddle.rand([8])
        >>> out0, out1, out2 = paddle.tensor_split(x, num_or_indices=[2, 3])
        >>> print(out0.shape)
        paddle.Size([2])
        >>> print(out1.shape)
        paddle.Size([1])
        >>> print(out2.shape)
        paddle.Size([5])

    .. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/tensor_split/tensor_split-4.png

    .. code-block:: pycon
        :name: tensor-split-example-4

        >>> import paddle

        >>> # split along axis
        >>> # x is a Tensor of shape [7, 8]
        >>> x = paddle.rand([7, 8])
        >>> out0, out1 = paddle.tensor_split(x, num_or_indices=2, axis=1)
        >>> print(out0.shape)
        paddle.Size([7, 4])
        >>> print(out1.shape)
        paddle.Size([7, 4])

    .. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/tensor_split/tensor_split-5.png

    .. code-block:: pycon
        :name: tensor-split-example-5

        >>> import paddle

        >>> # split along axis with indices
        >>> # x is a Tensor of shape [7, 8]
        >>> x = paddle.rand([7, 8])
        >>> out0, out1, out2 = paddle.tensor_split(x, num_or_indices=[2, 3], axis=1)
        >>> print(out0.shape)
        paddle.Size([7, 2])
        >>> print(out1.shape)
        paddle.Size([7, 1])
        >>> print(out2.shape)
        paddle.Size([7, 5])

    .. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/tensor_split/tensor_split-6.png

r   zEThe input tensor's dimension must be greater than 0 or axis which is z
, but got c                    / nSnSn/ [        U5      QUP HT  nUnUS:  a  UOX-   nUS:  a  UOX-   n	[        X5      n	[        R                  " X/U/U	/S9n
UR	                  U
5        UnMV     U$ )Nr   )r   r   r   )rM   r   rN   r   r   )rc   total_nindicesr6   splitsr   r   r   starts_index
ends_index	sub_arrays              rj   _tensor_split_indices+tensor_split.<locals>._tensor_split_indices'  s    ,T'],G,CD%+q[6g6FL!%J\6J~ZLI MM)$F - r   c                z    US::  a  [        S5      e[        X5      u  pEUS-   /U-  U/X%-
  -  -   n[        XU5      $ )Nr   z%num_or_indices must be larger than 0.r%   )r   divmodr`  )rc   rp  rh  r6   basemodra  s          rj   _tensor_split_sections,tensor_split.<locals>._tensor_split_sections<  sL    q=DEE7-	!8*s*dVx~-FFQ..r   zAThe num_or_indices should be int, list or tuple of ints, but got )r   r   rS   rL   rR   rM   r   rH   )rc   num_or_indicesr6   r`   rp  rv  r|  s          rj   tensor_splitr    s    B 	vv{affnSTXSYYcdedjdjckl
 	
 ggdmG*/ .#&&%a.GG	NT5M	2	2$QFF OPTUcPdOef
 	
r   c                    U R                   S:  a  [        SU R                    35      eU R                   S:  a  [        XSUS9$ [        XSUS9$ )a  

``hsplit`` Full name Horizontal Split, splits the input Tensor into multiple sub-Tensors along the horizontal axis, in the following two cases:

1. When the dimension of x is equal to 1, it is equivalent to ``paddle.tensor_split`` with ``axis=0``;

    .. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/hsplit/hsplit-1.png

2. when the dimension of x is greater than 1, it is equivalent to ``paddle.tensor_split`` with ``axis=1``.

    .. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/hsplit/hsplit-2.png


Args:
    x (Tensor): A Tensor whose dimension must be greater than 0. The data type is bool, bfloat16, float16, float32, float64, uint8, int32 or int64.
    num_or_indices (int|list|tuple): If ``num_or_indices`` is an int ``n``, ``x`` is split into ``n`` sections.
        If ``num_or_indices`` is a list or tuple of integer indices, ``x`` is split at each of the indices.
    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:
    list[Tensor], The list of segmented Tensors.

Examples:
    .. code-block:: pycon

        >>> import paddle

        >>> # x is a Tensor of shape [8]
        >>> x = paddle.rand([8])
        >>> out0, out1 = paddle.hsplit(x, num_or_indices=2)
        >>> print(out0.shape)
        paddle.Size([4])
        >>> print(out1.shape)
        paddle.Size([4])

        >>> # x is a Tensor of shape [7, 8]
        >>> x = paddle.rand([7, 8])
        >>> out0, out1 = paddle.hsplit(x, num_or_indices=2)
        >>> print(out0.shape)
        paddle.Size([7, 4])
        >>> print(out1.shape)
        paddle.Size([7, 4])

        >>> out0, out1, out2 = paddle.hsplit(x, num_or_indices=[1, 4])
        >>> print(out0.shape)
        paddle.Size([7, 1])
        >>> print(out1.shape)
        paddle.Size([7, 3])
        >>> print(out2.shape)
        paddle.Size([7, 4])

r%   z=The input tensor's dimension must be greater than 0, but got rI  r   r   r   r  rc   r~  r`   s      rj   hsplitr  P  sV    n 	vvzKAFF8T
 	
 	vvzAADAAAADAAr   c                h    U R                   S:  a  [        SU R                    35      e[        XSUS9$ )a  

``dsplit`` Full name Depth Split, splits the input Tensor into multiple sub-Tensors along the depth axis, which is equivalent to ``paddle.tensor_split`` with ``axis=2``.

Note:
    Make sure that the number of Tensor dimensions transformed using ``paddle.dsplit`` must be no less than 3.

In the following figure, Tenser ``x`` has shape [4, 4, 4], and after ``paddle.dsplit(x, num_or_indices=2)`` transformation, we get ``out0`` and ``out1`` sub-Tensors whose shapes are both [4, 4, 2] :

.. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/dsplit/dsplit.png


Args:
    x (Tensor): A Tensor whose dimension must be greater than 2. The data type is bool, bfloat16, float16, float32, float64, uint8, int32 or int64.
    num_or_indices (int|list|tuple): If ``num_or_indices`` is an int ``n``, ``x`` is split into ``n`` sections.
        If ``num_or_indices`` is a list or tuple of integer indices, ``x`` is split at each of the indices.
    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:
    list[Tensor], The list of segmented Tensors.

Examples:
    .. code-block:: pycon

        >>> import paddle

        >>> # x is a Tensor of shape [7, 6, 8]
        >>> x = paddle.rand([7, 6, 8])
        >>> out0, out1 = paddle.dsplit(x, num_or_indices=2)
        >>> print(out0.shape)
        paddle.Size([7, 6, 4])
        >>> print(out1.shape)
        paddle.Size([7, 6, 4])

        >>> out0, out1, out2 = paddle.dsplit(x, num_or_indices=[1, 4])
        >>> print(out0.shape)
        paddle.Size([7, 6, 1])
        >>> print(out1.shape)
        paddle.Size([7, 6, 3])
        >>> print(out2.shape)
        paddle.Size([7, 6, 4])

   z=The input tensor's dimension must be greater than 2, but got r   rI  r  r  s      rj   dsplitr    s<    \ 	vvzKAFF8T
 	
 ==r   c                h    U R                   S:  a  [        SU R                    35      e[        XSUS9$ )a6  

``vsplit`` Full name Vertical Split, splits the input Tensor into multiple sub-Tensors along the vertical axis, which is equivalent to ``paddle.tensor_split`` with ``axis=0``.

1. When the number of Tensor dimensions is equal to 2:

.. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/vsplit/vsplit-1.png

2. When the number of Tensor dimensions is greater than 2:

.. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/vsplit/vsplit-2.png


Note:
    Make sure that the number of Tensor dimensions transformed using ``paddle.vsplit`` must be not less than 2.

Args:
    x (Tensor): A Tensor whose dimension must be greater than 1. The data type is bool, bfloat16, float16, float32, float64, uint8, int32 or int64.
    num_or_indices (int|list|tuple): If ``num_or_indices`` is an int ``n``, ``x`` is split into ``n`` sections.
        If ``num_or_indices`` is a list or tuple of integer indices, ``x`` is split at each of the indices.
    name (str, 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:
    list[Tensor], The list of segmented Tensors.

Examples:
    .. code-block:: pycon

        >>> import paddle

        >>> # x is a Tensor of shape [8, 6, 7]
        >>> x = paddle.rand([8, 6, 7])
        >>> out0, out1 = paddle.vsplit(x, num_or_indices=2)
        >>> print(out0.shape)
        paddle.Size([4, 6, 7])
        >>> print(out1.shape)
        paddle.Size([4, 6, 7])

        >>> out0, out1, out2 = paddle.vsplit(x, num_or_indices=[1, 4])
        >>> print(out0.shape)
        paddle.Size([1, 6, 7])
        >>> print(out1.shape)
        paddle.Size([3, 6, 7])
        >>> print(out2.shape)
        paddle.Size([4, 6, 7])

r   z=The input tensor's dimension must be greater than 1, but got r   rI  r  r  s      rj   vsplitr    s<    f 	vvzKAFF8T
 	
 ==r   c                   Uc  / nOq[        U[        5      (       a  U/nOX[        U[        5      (       a  [        U5      nO7[        U[        R
                  [        45      (       a  UR                  S:X  a  U $ U nUn[        5       (       a  [        R                  " X45      $ [        5       (       a  [        U[        5      (       a  U/n[        U[        R                  R                  5      (       a  SUl        O][        U[        [        45      (       aB  [        R                  R!                  U5      (       a  [        R                  R#                  USS9n[        R                  " X45      $ [%        S0 ['        5       D6n[)        US/ SQS5        [+        US[        [        [        [        4S5        0 n[        U[        5      (       a  SUl        XFS	'   Of[        U[        [        45      (       aK  [        R                  R!                  U5      (       a#  [        R                  R-                  U5      US	'   OXFS	'   UR/                  UR0                  S
9nUR/                  UR0                  S
9nUR3                  SSU0UXxS.S9  U$ )a
  
Squeeze the dimension(s) of size 1 of input tensor x's shape.

Note that the output Tensor will share data with origin Tensor and doesn't have a
Tensor copy in ``dygraph`` mode. If you want to use the Tensor copy version,
please use `Tensor.clone` like ``squeeze_clone_x = x.squeeze().clone()``.

If axis is provided, it will remove the dimension(s) by given axis that of size 1.
If the dimension of given axis is not of size 1, the dimension remain unchanged.
If axis is not provided, all dims equal of size 1 will be removed.

.. code-block:: text

    Case1:

      Input:
        x.shape = [1, 3, 1, 5]  # If axis is not provided, all dims equal of size 1 will be removed.
        axis = None
      Output:
        out.shape = [3, 5]

    Case2:

      Input:
        x.shape = [1, 3, 1, 5]  # If axis is provided, it will remove the dimension(s) by given axis that of size 1.
        axis = 0
      Output:
        out.shape = [3, 1, 5]

    Case4:

      Input:
        x.shape = [1, 3, 1, 5]  # If the dimension of one given axis (3) is not of size 1, the dimension remain unchanged.
        axis = [0, 2, 3]
      Output:
        out.shape = [3, 5]

    Case4:

      Input:
        x.shape = [1, 3, 1, 5]  # If axis is negative, axis = axis + ndim (number of dimensions in x).
        axis = [-2]
      Output:
        out.shape = [1, 3, 5]

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

Args:
    x (Tensor): The input Tensor. Supported data type: float32, float64, bool, int8, int32, int64.
        alias: ``input``.
    axis (int|list|tuple, optional): An integer or list/tuple of integers, indicating the dimensions to be squeezed. Default is None.
                      The range of axis is :math:`[-ndim(x), ndim(x))`.
                      If axis is negative, :math:`axis = axis + ndim(x)`.
                      If axis is None, all the dimensions of x of size 1 will be removed.
        alias: ``dim``.
    name (str|None, optional): Please refer to :ref:`api_guide_Name`, Default None.

Returns:
    Tensor, Squeezed Tensor with the same data type as input Tensor.

Examples:
    .. code-block:: pycon

        >>> import paddle

        >>> x = paddle.rand([5, 1, 10])
        >>> output = paddle.squeeze(x, axis=1)

        >>> print(x.shape)
        paddle.Size([5, 1, 10])
        >>> print(output.shape)
        paddle.Size([5, 10])

        >>> # output shares data with x in dygraph mode
        >>> x[0, 0, 0] = 10.0
        >>> print(output[0, 0])
        Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
        10.)

r   Trq   default_dtypesqueezer5   )
rm   rs   rn   ro   rl   rw   r@   rq   r   r   	axis/axesr   r>   squeeze2rA   r   r   )r  )rL   rR   r   rM   rN   r,   r   sizer"   r   r  r$   rT   rU   ry   r   r   r   r   r\   r   r   r   r]   r?   r_   )	rc   r6   r`   r5   r   rg   rK   rh   r   s	            rj   r  r     s   l |	D#		v	D%	 	 Dz	D6==(3	4	499>HED~~e**	dC  6DdFJJ,,--!%DtUm,,||((..||77 8  ~~e**3&(3  	
$ 	4sD%&BINdH%%!%D &MtUm,,||((.. & D DT Jf $f77ekk7J;;%++;N<3	 	 	
 
r   c                    Uc  / nO9[        U[        5      (       a  U/nO [        U[        5      (       a  [        U5      nU nUn[	        5       (       a  [
        R                  " X45      $ g)z
Inplace version of ``squeeze`` API, the output Tensor will be inplaced with input ``x``.
Please refer to :ref:`api_paddle_tensor_squeeze`.
N)rL   rR   r   rM   r"   r   squeeze_)rc   r6   r`   r5   r   s        rj   r  r    s_     |	D#		v	D%	 	 DzEDu++ r   c                F   Uc  / nOU/nUn[        U[        R                  R                  [        R
                  R                  R                  45      (       d  [        U5      n[        5       (       Ga  [        R                  " U R                  5      S:X  a  U/ :X  a!  [        R                  " / U R                  S9/nOU R                  5       /nUS:X  d  U[        R                  :X  a  [        R                  nO[        R                   nU(       a$  UR#                  [        R                  " / US95        U(       a$  UR#                  [        R                  " / US95        [%        U5      S:X  a  US   $ ['        U5      $ [)        5       (       am  [*        R,                  " XX#U5      u  pnU	/nU(       a  UR#                  U
5        U(       a  UR#                  U5        [%        U5      S:X  a  US   $ ['        U5      $ [/        U S/ SQS5        [1        US[2        S5        [1        US	[2        S5        [5        US
SS/S5        [%        U5      S:w  a  [1        US   S[6        S5        [9        S0 [;        5       D6nUUUUS.nUR=                  U R                  SS9n	UR=                  USS9n
UR=                  USS9nXUS.nU	/nU(       a  UR#                  U
5        U(       a  UR#                  U5        UR?                  SSU 0UUS9  [%        U5      S:X  a  US   $ ['        U5      $ )a2  
Eliminates all but the first element from every consecutive group of equivalent elements.

Note:
    This function is different from :ref:`api_paddle_unique` in the sense that this function
    only eliminates consecutive duplicate values. This semantics is similar to :ref:`api_paddle_unique` in C++.

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

Args:
    x(Tensor): the input tensor, it's data type should be float32, float64, int32, int64.
        alias: ``input``.
    return_inverse(bool, optional): If True, also return the indices for where elements in
        the original input ended up in the returned unique consecutive tensor. Default is False.
    return_counts(bool, optional): If True, also return the counts for each unique consecutive element.
        Default is False.
    axis(int, optional): The axis to apply unique consecutive. If None, the input will be flattened.
        Default is None.
    dtype(str|paddle.dtype|np.dtype, optional):The data type `inverse` tensor: int32 or int64.
        Default: int64.
    name(str|None, optional): Name for the operation. For more information, please refer to
        :ref:`api_guide_Name`. Default is None.

Returns:
    - out (Tensor), the unique consecutive tensor for x.
    - inverse (Tensor), the element of the input tensor corresponds to
        the index of the elements in the unique consecutive tensor for x.
        inverse is provided only if return_inverse is True.
    - counts (Tensor), the counts of the every unique consecutive element in the input tensor.
        counts is provided only if return_counts is True.

Examples:
    .. code-block:: python

        >>> import paddle

        >>> x = paddle.to_tensor([1, 1, 2, 2, 3, 1, 1, 2])
        >>> output = paddle.unique_consecutive(x) #
        >>> print(output)
        Tensor(shape=[5], dtype=int64, place=Place(cpu), stop_gradient=True,
        [1, 2, 3, 1, 2])

        >>> _, inverse, counts = paddle.unique_consecutive(x, return_inverse=True, return_counts=True)
        >>> print(inverse)
        Tensor(shape=[8], dtype=int64, place=Place(cpu), stop_gradient=True,
        [0, 0, 1, 1, 2, 3, 3, 4])
        >>> print(counts)
        Tensor(shape=[5], dtype=int64, place=Place(cpu), stop_gradient=True,
         [2, 2, 1, 2, 1])

        >>> x = paddle.to_tensor([[2, 1, 3], [3, 0, 1], [2, 1, 3], [2, 1, 3]])
        >>> output = paddle.unique_consecutive(x, axis=0) #
        >>> print(output)
        Tensor(shape=[3, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
        [[2, 1, 3],
         [3, 0, 1],
         [2, 1, 3]])

        >>> x = paddle.to_tensor([[2, 1, 3], [3, 0, 1], [2, 1, 3], [2, 1, 3]])
        >>> output = paddle.unique_consecutive(x, axis=0) #
        >>> print(output)
        Tensor(shape=[3, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
        [[2, 1, 3],
         [3, 0, 1],
         [2, 1, 3]])
r   r>   r@   r%   r5   r   unique_consecutivereturn_inversereturn_countsr?   rq   r6   )r?   r  r  r6   Trx   )rC   IndexCountsrA   r   )r  ) rL   r    r|   r}   rN   rT   r~   r   r"   mathprodrS   rO   r?   cloner@   rq   r   r   r   r#   r   r  r   r   rl   r   rR   r   r\   r]   r_   )rc   r  r  r6   r?   r`   
attr_dtyper   return_dtyperh   inversecountsrg   rK   rJ   s                  rj   r  r    s   \ |vJT\\))6::??+C+CD  06
99QWW"rz((177;<	{5FLL#8%||%||F,,R|DEF,,R|DE4yA~Aw;%88}J 
f uKK KKt9>7NT{ 4 		
 	>#3T;OP=/49MNE7Wg$68LMt9>tAw-AB>VX>,*	
 77'' 8 
 ;;D < 
 ::D ; 
 6BuKK KK%8	 	 	
 t9>7NT{r   .c                    g r	  r   rc   return_indexr  r  r6   r?   sortedr`   s           rj   uniquer  S  s     -0r   c                    g r	  r   r  s           rj   r  r  `       %(r   c                    g r	  r   r  s           rj   r  r  m  r  r   c                    g r	  r   r  s           rj   r  r  z  r  r   c                    g r	  r   r  s           rj   r  r          r   c                    g r	  r   r  s           rj   r  r    r  r   c                    g r	  r   r  s           rj   r  r    r  r   c                    g r	  r   r  s           rj   r  r    s     r   c                    g r	  r   r  s           rj   r  r    s     #&r   Tc           	     n   Uc  / nOU/n[        U5      n[        5       (       Ga  [        R                  " U R                  5      S:X  a  U R                  5       /n	US:X  d  U[        R                  :X  a  [        R                  n
O[        R                  n
U(       a$  U	R                  [        R                  " / U
S95        U(       a$  U	R                  [        R                  " / U
S95        U(       a$  U	R                  [        R                  " / U
S95        [        U	5      S:X  a  U	S   $ [        U	5      $ [        R                  " XX#XH5      u  ppU/n	U(       a  U	R                  U5        U(       a  U	R                  U5        U(       a  U	R                  U5        [        U	5      S:X  a  U	S   $ [        U	5      $ [        5       (       a  [        R                  " U UUUUUS5      u  ppU/n	U(       a  U	R                  U5        U(       a  U	R                  U5        U(       a  U	R                  U5        [        U	5      S:X  a  U	S   $ [        U	5      $ [!        U S/ SQS5        [#        US	[$        S5        [#        US
[$        S5        [#        US[$        S5        ['        USSS/S5        [        U5      S:w  a  [#        US   S[(        S5        [+        S0 [-        5       D6nUUUUUSS.nUR/                  U R0                  SS9nUR/                  USS9nUR/                  USS9nUR/                  USS9nUUUUS.nU/n	U(       a  U	R                  U5        U(       a  U	R                  U5        U(       a  U	R                  U5        UR3                  SSU 0UUS9  [        U	5      S:X  a  U	S   $ [        U	5      $ )a	  
Returns the unique elements of `x` in ascending order.

Args:
    x(Tensor): The input tensor, it's data type should be float32, float64, int32, int64.
    return_index(bool, optional): If True, also return the indices of the input tensor that
        result in the unique Tensor.
    return_inverse(bool, optional): If True, also return the indices for where elements in
        the original input ended up in the returned unique tensor.
    return_counts(bool, optional): If True, also return the counts for each unique element.
    axis(int, optional): The axis to apply unique. If None, the input will be flattened.
        Default: None.
    dtype(str|paddle.dtype|np.dtype, optional): The date type of `indices` or `inverse` tensor: int32 or int64.
        Default: int64.
    sorted(bool, optional): Does not affect the return result, same as PyTorch.
    name(str|None, optional): Name for the operation. For more information, please refer to
        :ref:`api_guide_Name`. Default: None.

Returns:
    tuple (out, indices, inverse, counts). `out` is the unique tensor for `x`. `indices` is \
        provided only if `return_index` is True. `inverse` is provided only if `return_inverse` \
        is True. `counts` is provided only if `return_counts` is True.

Examples:
    .. code-block:: python

        >>> import paddle

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

        >>> _, indices, inverse, counts = paddle.unique(x, return_index=True, return_inverse=True, return_counts=True)
        >>> print(indices)
        Tensor(shape=[4], dtype=int64, place=Place(cpu), stop_gradient=True,
        [3, 0, 1, 4])
        >>> print(inverse)
        Tensor(shape=[6], dtype=int64, place=Place(cpu), stop_gradient=True,
        [1, 2, 2, 0, 3, 2])
        >>> print(counts)
        Tensor(shape=[4], dtype=int64, place=Place(cpu), stop_gradient=True,
        [1, 1, 3, 1])

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

        >>> unique = paddle.unique(x, axis=0)
        >>> print(unique)
        Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
        [[2, 1, 3],
         [3, 0, 1]])
r   r@   r>   r%   Tr5   )rm   rs   rn   ro   r@   rq   r  r  r  r  r?   rq   r6   )r?   r  r  r  r6   	is_sortedrx   )rC   Indicesr  r  rA   r   )r  )r   r"   r  r  rS   r  rN   r@   rq   r   rO   r   r   r   r  r$   r   r   rl   r   rR   r   r\   r]   r?   r_   )rc   r  r  r  r6   r?   r  r`   r  r   r  rh   rq  r  r  rg   rK   rJ   s                     rj   r  r    sk   F |v+E2J99QWW"GGI;D5FLL#8%||%||F,,R|DEF,,R|DEF,,R|DE4yA~Aw;(.^D)
%g uKK KK KKt9>7NT{	(.)
%g uKK KK KKt9>7NT{ I		
 	<x@>#3T8D=/4BE7Wg$6At9>tAwX622(,*
 77'' 8 
 ;;D < 
 ;;D < 
 ::D ; 
 	
 uKK KK KK3(% 	 	
 t9>7NT{r   c                |   U nUn[        5       (       a  [        U[        5      (       a  U/nOy[        U[        5      (       a  UR	                  5       nOS[        U[
        [        45      (       a8  U Vs/ s H+  n[        U[        5      (       a  UR                  S5      OUPM-     nn[        R                  " X45      $ [        5       (       a  [        U[        5      (       a  U/n[        U[        R                  R                  5      (       a  SUl        O][        U[
        [        45      (       aB  [        R                  R!                  U5      (       a  [        R                  R#                  USS9n[        R                  " X45      $ [%        US[        [
        [        [        4S5        ['        US/ SQS5        [)        S0 [+        5       D6nS
U0n0 n[        U[        5      (       a  U/n[        U[        5      (       a  SUl        XGS'   Of[        U[
        [        45      (       aK  [        R                  R!                  U5      (       a#  [        R                  R-                  U5      US'   OXHS'   UR/                  UR0                  S9n	UR/                  UR0                  S9n
UR3                  S	UUXS.S9  U	$ s  snf )a	  
Insert single-dimensional entries to the shape of input Tensor ``x``. Takes one
required argument axis, a dimension or list of dimensions that will be inserted.
Dimension indices in axis are as seen in the output tensor.

Note that the output Tensor will share data with origin Tensor and doesn't have a
Tensor copy in ``dygraph`` mode. If you want to use the Tensor copy version,
please use `Tensor.clone` like ``unsqueeze_clone_x = x.unsqueeze(-1).clone()``.

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

Args:
    x (Tensor): The input Tensor to be unsqueezed. Supported data type: bfloat16, float16, float32, float64, bool, int8, int32, int64.
        alias: ``input``.
    axis (int|list|tuple|Tensor): Indicates the dimensions to be inserted. The data type is ``int32`` .
                                If ``axis`` is a list or tuple, each element of it should be integer or 0-D Tensor with shape [].
                                If ``axis`` is a Tensor, it should be an 1-D Tensor .
                                If ``axis`` is negative, ``axis = axis + ndim(x) + 1``.
        alias: ``dim``.
    name (str|None, optional): Name for this layer. Please refer to :ref:`api_guide_Name`, Default None.

Returns:
    Tensor, Unsqueezed Tensor with the same data type as input Tensor.

Examples:
    .. code-block:: pycon

        >>> import paddle

        >>> x = paddle.rand([5, 10])
        >>> print(x.shape)
        paddle.Size([5, 10])

        >>> out1 = paddle.unsqueeze(x, axis=0)
        >>> print(out1.shape)
        paddle.Size([1, 5, 10])

        >>> out2 = paddle.unsqueeze(x, axis=[0, 2])
        >>> print(out2.shape)
        paddle.Size([1, 5, 1, 10])

        >>> axis = paddle.to_tensor([0, 1, 2])
        >>> out3 = paddle.unsqueeze(x, axis=axis)
        >>> print(out3.shape)
        paddle.Size([1, 1, 1, 5, 10])

        >>> # out1, out2, out3 share data with x in dygraph mode
        >>> x[0, 0] = 10.0
        >>> print(out1[0, 0, 0])
        Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
        10.)
        >>> print(out2[0, 0, 0, 0])
        Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
        10.)
        >>> print(out3[0, 0, 0, 0, 0])
        Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
        10.)

r   Trq   r  r  	unsqueezer5   )rs   rm   rs   rn   ro   rl   rw   rp   r@   rq   r   r   
unsqueeze2rA   
AxesTensorAxesTensorListr   r>   r   r   )r  )r"   rL   rR   r   r  rM   r   r   r   r  r$   rN   rT   rU   ry   r   r   r   r   r   r   r\   r   r]   r?   r_   )rc   r6   r`   r5   r   r   rg   rI   rK   rh   r   s              rj   r  r    s\   F EDdC  6Dh'';;=DtUm,, ! D !+4 : :		!D    ,,	dC  6DdFJJ,,--!%DtUm,,||((..||77 8  ,,4sD%&BKP  #	
& 6VX6udC  6DdH%%!%D#'< tUm,,||((..+1<<+O+O,'( !%f77ekk7J;;%++;N3	 	 	
 
s   22J9c                d   U nUn[        U[        5      (       a  U/nOy[        U[        5      (       a  UR                  5       nOS[        U[        [
        45      (       a8  U Vs/ s H+  n[        U[        5      (       a  UR                  S5      OUPM-     nn[        R                  " X45      $ s  snf )z
Inplace version of ``unsqueeze`` API, the output Tensor will be inplaced with input ``x``.
Please refer to :ref:`api_paddle_tensor_unsqueeze`.
r   )	rL   rR   r   r  rM   r   r   r   
unsqueeze_)rc   r6   r`   r5   r   r   s         rj   r  r    s     ED$v	D(	#	#{{}	D4-	(	( 
 'tX66DIIaLD@ 	 
 U))	
s   #2B-c                d    [         R                  " XUSS9nUb  [         R                  " XC5        U$ )zWrapper for take_along_axisF	broadcast)rN   take_along_axisassign)r5   r   rk  rh   rb   s        rj   _take_along_axis_wrapperr  $  s.     
 
 se
DC
cJr   c                   Uc  Sn[        5       (       a  [        R                  " XU5      nO[        U S/ SQS5        [        USSS/S5        [	        U[
        5      (       a  [        USSS/S5        [        S0 [        5       D6nUR                  S5      nUR                  U5      n[	        U[
        5      (       d  UR                  SXS	.US
S.SU0S9  OUR                  SXUS.SS
0SU0S9  UnUb  [        R                  " XT5        U$ )zWrapper for original gatherr   rc   )rl   rm   rn   ro   rp   r@   rq   rr   rs   r   r   gatherrk  r@   rq   r6   rA   r  F)r6   	overwriterC   r   )rA   r  Axisr  )r  )r#   r   r  r   rL   r   r   r\   r^   r]   r_   rN   r  )	rc   rk  r6   r`   rh   rb   rg   r?   outputs	            rj   _gather_wrapperr  1  s'    |mmAd+  !	
$ 	!'71CXNdH%%$T6GW3ExP22""3'::5A$))/#%8	   ="E*	   
cJr   c                    g r	  r   )rc   rk  r6   r`   rh   s        rj   r  r  n       r   c                    g r	  r   )r5   r   rk  rh   s       rj   r  r  x  s     r   c                     [        U 5      nU[        U5      -   S:  a  [        SU S[        U5       S35      eSnUS:  a  U[        U S   [        5      -  nOUSU;   -  nU(       a  [	        U 0 UD6$ [        U 0 UD6$ )ax
  
This function has two functionalities, depending on the parameters passed:

1. ``gather(Tensor input, int dim, Tensor index, Tensor out = None)``:
    PyTorch compatible gather, calls a non-broadcast `paddle.take_along_axis`.
    Check out :ref:`api_paddle_take_along_axis` and also `[torch has more parameters] torch.scatter <https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/guides/model_convert/convert_from_pytorch/api_difference/torch/torch.gather.html>`_
    Note that ``sparse_grad`` param of PyTorch is currently not supported by Paddle, therefore do not pass this param (behavior is equivalent to ``sparse_grad = False``).
    Also, dim allows for Tensor input, the same as PyTorch. However, when the first 3 params are all of Tensor types, there will be ambiguity between these two functionalities.
    Currently, original gather pass is more actively selected. Try avoiding using Tensor dim as input therefore.

2. ``gather(Tensor x, Tensor index, int axis, str name = None, Tensor out = None)``:
    The original paddle.gather, see the following docs.

Output is obtained by gathering entries of ``axis``
of ``x`` indexed by ``index`` and concatenate them together.

.. code-block:: text


            Given:

            x = [[1, 2],
                 [3, 4],
                 [5, 6]]

            index = [1, 2]
            axis=[0]

            Then:

            out = [[3, 4],
                   [5, 6]]

Args:
    x (Tensor): The source input tensor with rank>=1. Supported data type is
        int32, int64, float32, float64, complex64, complex128 and uint8 (only for CPU),
        float16 (only for GPU).
    index (Tensor): The index input tensor with rank=0 or rank=1. Data type is int32 or int64.
    axis (Tensor|int|None, optional): The axis of input to be gathered, it's can be int or a Tensor with data type is int32 or int64. The default value is None, if None, the ``axis`` is 0.
    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:
    output (Tensor), If the index is a 1-D tensor, the output is a tensor with the same shape as ``x``. If the index is a 0-D tensor, the output will reduce the dimension where the axis pointing.

Examples:

    .. code-block:: python

        >>> import paddle

        >>> input = paddle.to_tensor([[1,2],[3,4],[5,6]])
        >>> index = paddle.to_tensor([0,1])
        >>> output = paddle.gather(input, index, axis=0)
        >>> print(output)
        Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
        [[1, 2],
         [3, 4]])
r   (Too few arguments in the function call: r   z. Expect one of: 
 - (Tensor input, int dim, Tensor index, *, Tensor out = None)
 - (Tensor x, Tensor index, int axis, str name = None, Tensor out = None)Fr%   r   )r   rY   rL   rR   r  r  )argskwargslen_argsis_take_along_axiss       rj   r  r    s    x 4yH#f+!6xj3v;- PX X
 	
 1}ja#66evo-'888///r   c                (   [        U[        5      (       d  [        S[        U5       S35      eU[	        U R
                  * U R
                  5      ;  a'  [        SU R
                  *  SU R
                   S35      e[        5       (       a  [        R                  " X5      $ [        U[        R                  5      (       a  [        R                  " U5      nU R                  nUS:  a  UO[        U5      U-   nX#   n[        S0 [!        5       D6n[#        U S[$        S5        UR'                  5       n[)        US/ S	QS5        [	        U5       Vs/ s H   nUR+                  UR'                  5       S
9PM"     nnUR-                  SSU 0SU0SU0S9  U$ s  snf )a  

Removes a tensor dimension, then split the input tensor into multiple sub-Tensors.

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

Args:
    input (Tensor): The input variable which is an N-D Tensor, data type being bool, float16, float32, float64, int32, int64, complex64 or complex128.
    axis (int, optional): A 0-D Tensor with shape [] and type is ``int32|int64``. The dimension along which to unbind.
        If :math:`axis < 0`, the dimension to unbind along is :math:`rank(input) + axis`. Default is 0.
        alias: ``dim``.
Returns:
    list(Tensor), The list of segmented Tensor variables.

Examples:
    .. code-block:: python

        >>> import paddle

        >>> # input is a Tensor which shape is [3, 4, 5]
        >>> input = paddle.rand([3, 4, 5])

        >>> [x0, x1, x2] = paddle.unbind(input, axis=0)
        >>> # x0.shape [4, 5]
        >>> # x1.shape [4, 5]
        >>> # x2.shape [4, 5]

        >>> [x0, x1, x2, x3] = paddle.unbind(input, axis=1)
        >>> # x0.shape [3, 5]
        >>> # x1.shape [3, 5]
        >>> # x2.shape [3, 5]
        >>> # x3.shape [3, 5]
z.The type of 'axis'  must be int, but received r   zThe axis must in range(r   r   r   unbindr5   	rl   rm   rs   rn   ro   r@   rq   r   r   r>   rA   rC   r6   rG   )r  )rL   rR   rY   rH   r   r   r   r#   r   r  rP   genericasscalarrS   r   r   r\   r   r   r^   r   r]   r_   )	r5   r6   rl  axis_r   rg   r?   re   r   s	            rj   r  r    s   J dS""<T$ZLJ
 	
 5%**ejj11%uzzk]"UZZLC
 	
 }}U))dBJJ'';;t$Dkk	s;'7$'> 225'Hx8""$
 	
( 3Z	
   55((* 6   	 	 
 	<DM4.	 	 	
 
s   'Fc                   Uc  UnUc  [        S5      eOUb  [        S5      eUc  Sn[        U R                  5      [        UR                  5      :w  a  [        S5      e[	        X5      n[        U[        R                  [        R                  R                  45      (       a  [        UR                  5      [        UR                  5      :w  a  [        S5      e[        [        U R                  5      5       H  nXv:w  a   U R                  U   UR                  U   :  d"  UR                  U   UR                  U   :  d  MJ  [        SU SUR                   SU R                   S	U S
UR                   3
5      e   On[        R                  " U5      R                  U R                  5      nSnUR                   H  n	X-  nM	     US:X  a   [        R                  " X2R                  5      nU R                  U   n
X*:  R!                  5       (       d  [        SU SU
 35      e[#        UR                  5      S;  a!  [        S[#        UR                  5       35      e[$        R&                  " XX6US5      $ )zWrapper for inplace version of put_along_axis
This API is not directly available for users. One can only call this API via torch.Tensor.scatter_ or torch.scatter_
a  'paddle.Tensor.scatter_' expect one of the following input pattern: 
 - (int dim, Tensor index, Tensor src (alias value), *, str reduce)
 - (Tensor index, Tensor updates, bool overwrite, str name = None)
However, the input pattern does not match, please check.R`value` is useless when `src` is specified. Be careful for conflicting parameters.r  z<`index` and `input` must have the same number of dimensions!z:`index` and `src` must have the same number of dimensions!!Size does not match at dimension  expected index  to be smaller than self  apart from dimension z! and to be smaller size than src r%   z7one of element of index is out of bounds for dimension  with size r@   rq   zDThe data type of index should be one of ['int32', 'int64'], but got T)rY   r   rS   r   non_negative_axisrL   rN   r,   rT   rU   r   RuntimeErrorrO   astyper?   broadcast_toallr   r   put_along_axis_)r5   r   rk  srcreducer   r6   re   elementsr   axis_max_sizes              rj   _put_along_axis_inplace_wrapperr  .  sy    {;K   
	`
 	
 ~
5;;3u{{++J
 	
 U(D#vzz'7'7899u{{s399~-L  s5;;'(A	ekk!nu{{1~=%++C		!C #7s:J5;;-Wpqvq|q|p}  ~T  UY  TZ  Z{  |  |E  |E  {F  G 	 ) s#**5;;799COH q=%%c;;7CKK%M!&&((EdV;WdVef
 	
 U[[!);;RS`afalalSmRno
 	
 !!%64HHr   c                0    [         R                  " XX#5      $ )z"Wrapper for inplace origin scatter)r   scatter_rc   rk  updatesr  r`   s        rj   _scatter_inplace_wrapperr  o  s     ??1W88r   c                    g r	  r   r  s        rj   r  r  z  r  r   c                    g r	  r   )r5   r   rk  r  r  r   s         rj   r  r         r   c                     [        U 5      nU[        U5      -   S:  a  [        SU S[        U5       S35      eSnUS:  a  [        U S   [        5      nOSU;   nU(       a  [	        U 0 UD6$ [        U 0 UD6$ )z
Inplace version of ``scatter`` API, the output Tensor will be inplaced with input.
Please refer to :ref:`api_paddle_tensor_scatter`.
r   r  r   z. Expect one of: 
 - (int dim, Tensor index, Tensor src, *, str reduce, Tensor out = None)
 - (Tensor index, Tensor updates, bool overwrite, str name = None)Fr%   r   )r   rY   rL   rR   r  r  r  r  r  is_put_along_axiss       rj   r  r    s     4yH#f+!6xj3v;- PQ Q
 	

 1}&tAw4!VO.???'888r   c                \   [        5       (       a  [        R                  " XX#5      nOk[        U S/ SQS5        [	        US[
        S5        [        S0 [        5       D6nUR                  U R                  5      nUR                  SXUS.SU0SU0S9  UnUb  [        R                  " Xe5        U$ )	zWrapper for original scatter
This API is not directly available for users. One can only call this API via torch.Tensor.scatter or torch.scatter
r?   )rn   ro   rm   r@   rq   rs   scatterr  )rA   IdsUpdatesrC   r   )r  )r#   r   r  r   r   rl   r   r\   r]   r?   r_   rN   r  )	rc   rk  r  r  r`   rh   rb   rg   r  s	            rj   _scatter_wrapperr    s     nnQw: I		
 	9k4;3&(3::177CW=	*FO	 	 	
 
cJr   c           	         Uc  UnUc  [        S5      eOUb  [        S5      eUc  Sn[        R                  " XX1USS9nUb  [        R                  " Xu5        U$ )zA PyTorch Compatible wrapper for put_along_axis
This API is not directly available for users. One can only call this API via torch.Tensor.scatter or torch.scatter
a'  'paddle.scatter' expect one of the following input pattern: 
 - (Tensor input, int dim, Tensor index, Tensor src (alias value), *, str reduce, Tensor out = None)
 - (Tensor x, Tensor index, Tensor updates, bool overwrite, str name = None)
However, the input pattern does not match, please check.r  r  Fr  )rY   rN   put_along_axisr  )r5   r   rk  r  r  rh   r   rb   s           rj   _put_along_axis_wrapperr    s{     {;K   
	`
 	
 ~


c%
PC
cJr   c                    g r	  r   )rc   rk  r  r  r`   rh   s         rj   r  r    r  r   c                    g r	  r   )r5   r   rk  r  r  rh   r   s          rj   r  r    s     r   c                     [        U 5      nU[        U5      -   S:  a  [        SU S[        U5       S35      eSnUS:  a  [        U S   [        5      nOSU;   nU(       a  [	        U 0 UD6$ [        U 0 UD6$ )a  

This function has two functionalities, depending on the parameters passed:

1. ``scatter(Tensor input, int dim, Tensor index, Tensor src (alias value), *, str reduce = None, Tensor out = None)``:
    PyTorch compatible scatter, calls a non-broadcast `paddle.put_along_axis`.
    Check out :ref:`api_paddle_put_along_axis` and also `[torch has more parameters] torch.scatter <https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/guides/model_convert/convert_from_pytorch/api_difference/torch/torch.scatter.html>`_

2. ``scatter(Tensor x, Tensor index, Tensor updates, bool overwrite, str name = None)``:
    The original paddle.scatter, see the following docs.


**Scatter Layer**
Output is obtained by updating the input on selected indices based on updates.

As shown in the figure, when ``overwrite`` is set to ``True``, the output for the same index is updated in overwrite mode, where ``x[index[i]]`` is directly replaced with ``update[i]`` sequentially; When ``overwrite`` is set to ``False``, the output for the same index is updated in accumulation mode. In this mode, ``x[index[i]]`` is first initialized with elements set to 0. Then, ``update[i]`` is sequentially added to ``x[index[i]]`` to produce the output.

.. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/scatter.png
    :alt: Legend - scatter behavior display

.. code-block:: pycon
    :name: scatter-example-1

    >>> import paddle
    >>> # input:
    >>> x = paddle.to_tensor([[1, 1], [2, 2], [3, 3]], dtype='float32')
    >>> index = paddle.to_tensor([2, 1, 0, 1], dtype='int64')
    >>> # shape of updates should be the same as x
    >>> # shape of updates with dim > 1 should be the same as input
    >>> updates = paddle.to_tensor([[1, 1], [2, 2], [3, 3], [4, 4]], dtype='float32')
    >>> overwrite = False
    >>> # calculation:
    >>> if not overwrite:
    ...     for i in range(len(index)):
    ...         x[index[i]] = paddle.zeros([2])
    >>> for i in range(len(index)):
    ...     if overwrite:
    ...         x[index[i]] = updates[i]
    ...     else:
    ...         x[index[i]] += updates[i]
    >>> # output:
    >>> out = paddle.to_tensor([[3, 3], [6, 6], [1, 1]])
    >>> print(out.shape)
    paddle.Size([3, 2])

**NOTICE**: The order in which updates are applied is nondeterministic,
so the output will be nondeterministic if index contains duplicates.

Args:
    x (Tensor): The input N-D Tensor with ndim>=1. Data type can be float32, float64.
    index (Tensor): The index is a 1-D or 0-D Tensor. Data type can be int32, int64. The length of index cannot exceed updates's length, and the value in index cannot exceed input's length.
    updates (Tensor): Update input with updates parameter based on index. When the index is a 1-D tensor, the updates shape should be the same as input, and dim value with dim > 1 should be the same as input. When the index is a 0-D tensor, the updates should be a (N-1)-D tensor, the ith dim of the updates should be equal with the (i+1)th dim of the input.
    overwrite (bool, optional): The mode that updating the output when there are same indices.If True, use the overwrite mode to update the output of the same index,if False, use the accumulate mode to update the output of the same index. Default value is True.
    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:
    Tensor, The output is a Tensor with the same shape as x.

Examples:
    .. code-block:: python
        :name: scatter-example-2

        >>> import paddle

        >>> x = paddle.to_tensor([[1, 1], [2, 2], [3, 3]], dtype='float32')
        >>> index = paddle.to_tensor([2, 1, 0, 1], dtype='int64')
        >>> updates = paddle.to_tensor([[1, 1], [2, 2], [3, 3], [4, 4]], dtype='float32')

        >>> output1 = paddle.scatter(x, index, updates, overwrite=False)
        >>> print(output1)
        Tensor(shape=[3, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[3., 3.],
         [6., 6.],
         [1., 1.]])

        >>> output2 = paddle.scatter(x, index, updates, overwrite=True)
        >>> # CPU device:
        >>> # [[3., 3.],
        >>> #  [4., 4.],
        >>> #  [1., 1.]]
        >>> # GPU device maybe have two results because of the repeated numbers in index
        >>> # result 1:
        >>> # [[3., 3.],
        >>> #  [4., 4.],
        >>> #  [1., 1.]]
        >>> # result 2:
        >>> # [[3., 3.],
        >>> #  [2., 2.],
        >>> #  [1., 1.]]
r   r  r   z. Expect one of: 
 - (Tensor input, int dim, Tensor index, Tensor src, *, str reduce, Tensor out = None)
 - (Tensor x, Tensor index, Tensor updates, bool overwrite, str name = None)Fr%   r   )r   rY   rL   rR   r  r  r  s       rj   r  r    s    v 4yH#f+!6xj3v;- P[ [
 	

 1}&tAw4!VO&777000r   c                   U R                   UR                   :w  a7  [        S[        U R                   5       S[        UR                   5       35      e[        5       (       a#  UR                  S:X  a  U R                  5       U-   $ [        5       (       a  [        R                  " XU5      $ [        S
0 [        5       D6nUR                  SS9nUR                  U5      nUR                  SXUS.SU0S	9  U$ )aq  

Output is obtained by applying sparse addition to a single value
or slice in a Tensor.

:attr:`x` is a Tensor with ndim :math:`R`
and :attr:`index` is a Tensor with ndim :math:`K` . Thus, :attr:`index`
has shape :math:`[i_0, i_1, ..., i_{K-2}, Q]` where :math:`Q \leq R` . :attr:`updates`
is a Tensor with ndim :math:`K - 1 + R - Q` and its
shape is :math:`index.shape[:-1] + x.shape[index.shape[-1]:]` .

According to the :math:`[i_0, i_1, ..., i_{K-2}]` of :attr:`index` ,
add the corresponding :attr:`updates` slice to the :attr:`x` slice
which is obtained by the last one dimension of :attr:`index` .

.. code-block:: text

    Given:

    * Case 1:
        x = [0, 1, 2, 3, 4, 5]
        index = [[1], [2], [3], [1]]
        updates = [9, 10, 11, 12]

      we get:

        output = [0, 22, 12, 14, 4, 5]

    * Case 2:
        x = [[65, 17], [-14, -25]]
        index = [[], []]
        updates = [[[-1, -2], [1, 2]],
                   [[3, 4], [-3, -4]]]
        x.shape = (2, 2)
        index.shape = (2, 0)
        updates.shape = (2, 2, 2)

      we get:

        output = [[67, 19], [-16, -27]]

Args:
    x (Tensor): The x input. Its dtype should be int32, int64, float32, float64.
    index (Tensor): The index input with ndim > 1 and index.shape[-1] <= x.ndim.
                      Its dtype should be int32 or int64 as it is used as indexes.
    updates (Tensor): The updated value of scatter_nd_add op, and it must have the same dtype
                        as x. It must have the shape index.shape[:-1] + x.shape[index.shape[-1]:].
    name (str|None, optional): The output tensor name. If set None, the layer will be named automatically.

Returns:
    output (Tensor), The output is a tensor with the same shape and dtype as x.

Examples:

    .. code-block:: pycon

        >>> import paddle

        >>> x = paddle.rand(shape=[3, 5, 9, 10], dtype='float32')
        >>> updates = paddle.rand(shape=[3, 9, 10], dtype='float32')
        >>> index = paddle.to_tensor([[1, 1], [0, 1], [1, 3]], dtype='int64')

        >>> output = paddle.scatter_nd_add(x, index, updates)
        >>> print(output.shape)
        paddle.Size([3, 5, 9, 10])
z3x and updates must have same data type but x.dtype=z, updates.dtype=r   scatter_nd_addrc   input_param_name)rA   r  r  rC   rH   rI   rJ   )r  )r?   rY   r   r"   r  r  r#   r   r  r   r\   r^   r]   r_   )rc   rk  r  r`   rg   r?   r  s          rj   r  r  w  s   J 	ww'--A-PQPWPWBXAYYijwx  yF  yF  kG  jH  I
 	
 ::?779w&&$$Qw77::""C"8::5A!w?FO 	 	

 r   c                B    [        [        X!R                  5      XU5      $ )a  
**Scatter_nd Layer**

Output is obtained by scattering the :attr:`updates` in a new tensor according
to :attr:`index` . This op is similar to :code:`scatter_nd_add`, except the
tensor of :attr:`shape` is zero-initialized. Correspondingly, :code:`scatter_nd(index, updates, shape)`
is equal to :code:`scatter_nd_add(paddle.zeros(shape, updates.dtype), index, updates)` .
If :attr:`index` has repeated elements, then the corresponding updates are accumulated.
Because of the numerical approximation issues, the different order of repeated elements
in :attr:`index` may cause different results. The specific calculation method can be
seen :code:`scatter_nd_add` . This op is the inverse of the :code:`gather_nd` op.

Args:
    index (Tensor): The index input with ndim >= 1 and index.shape[-1] <= len(shape).
                      Its dtype should be int32 or int64 as it is used as indexes.
    updates (Tensor): The updated value of scatter_nd op. Its dtype should be float32, float64.
                        It must have the shape index.shape[:-1] + shape[index.shape[-1]:]
    shape(tuple|list|Tensor): Shape of output tensor.
    name (str|None, optional): The output Tensor name. If set None, the layer will be named automatically.

Returns:
    output (Tensor), The output is a tensor with the same type as :attr:`updates` .

Examples:

    .. code-block:: python

        >>> import paddle

        >>> index = paddle.to_tensor([[1, 1],
        ...                           [0, 1],
        ...                           [1, 3]], dtype="int64")
        >>> updates = paddle.rand(shape=[3, 9, 10], dtype='float32')
        >>> shape = [3, 5, 9, 10]

        >>> output = paddle.scatter_nd(index, updates, shape)
)r  r(   r?   )rk  r  rS   r`   s       rj   
scatter_ndr    s    P %}}5utLLr   c                *   [        US[        S5        US::  dS  [        U[        5      (       a]  US:  aW  U R                  U   S:w  aD  XR                  U   :  a2  U R                  U   S:w  a  [	        SU SU R                  U    S35      e[        XX#S9$ )	am	  
Split the input tensor into multiple sub-Tensors.

Here are some examples to explain it.

    - 1. Given a 3-D tensor x with a shape [3, 3, 3], if we split the first dimension into three equal parts, it will output a list containing three 3-D tensors with a shape of [1, 3, 3].
    - 2. Given a 3-D tensor x with a shape [3, 3, 3], if we split the second dimension into three equal parts, it will output a list containing three 3-D tensors with a shape of [3, 1, 3].
    - 3. Given a 3-D tensor x with a shape [3, 3, 3], if we split the third dimension into three equal parts, it will output a list containing three 3-D tensors with a shape of [3, 3, 1].

The following figure illustrates the first example.

.. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/chunk.png
    :width: 800
    :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:
    list(Tensor), The list of segmented Tensors.

Examples:
    .. code-block:: python

        >>> import paddle

        >>> x = paddle.rand([3, 9, 5])

        >>> out0, out1, out2 = paddle.chunk(x, chunks=3, axis=1)
        >>> # out0.shape [3, 3, 5]
        >>> # out1.shape [3, 3, 5]
        >>> # out2.shape [3, 3, 5]


        >>> # axis is negative, the real axis is (rank(x) + axis) which real
        >>> # value is 1.
        >>> out0, out1, out2 = paddle.chunk(x, chunks=3, axis=-2)
        >>> # out0.shape [3, 3, 5]
        >>> # out1.shape [3, 3, 5]
        >>> # out2.shape [3, 3, 5]
chunkschunkr   r   zThe value of 'chunks' must be greater than 0 and less than or equal to the size of the dimension specified by 'axis', but received chunks=z and x.shape[axis]=r   )ra  r6   r`   )r   rR   rL   rS   r   r`  )rc   r  r6   r`   s       rj   r  r    s    r vx#0{4AIWWT]aFWWT]$:GGDMR Y  Z`  Ya  at  uv  u|  u|  }A  uB  tC  CD  E
 	
 AAr   dims)rc   repeat_timesc                   S n[        5       (       af  [        U[        R                  R                  5      (       a'  UR
                  S:X  d   S5       eUR                  5       n[        R                  " X5      $ [        5       (       a|  U" X5        [        U[        [        45      (       aC  [        R                  R                  U5      (       a  [        R                  R                  U5      n[        R                  " X5      $ U" X5        S n[!        S0 [#        5       D6nSU /0n0 n[        U[$        5      (       a  SUl        XS'   S	/US
'   Ol[        U[        [        45      (       aQ  U" U5      US
'   [        R                  R                  U5      (       a"  [        R                  R)                  U5      US'   UR+                  SS9nUR-                  U5      n	UR/                  SUSU	0US9  U	$ )aT  

Construct a new Tensor by repeating ``x`` the number of times given by ``repeat_times``.
After tiling, the value of the i'th dimension of the output is equal to ``x.shape[i]*repeat_times[i]``.

Both the number of dimensions of ``x`` and the number of elements in ``repeat_times`` should be less than or equal to 6.

.. note::
    Alias Support: The parameter name ``input`` can be used as an alias for ``x``, and ``dims`` can be used as an alias for ``repeat_times``.
    For example, ``tile(input=x, dims=repeat_times)`` is equivalent to ``tile(x=x, repeat_times=repeat_times)``.
Args:
    x (Tensor): The input tensor, its data type should be bool, float16, float32, float64, int32, int64, complex64 or complex128.
        alias: ``input``.
    repeat_times (list|tuple|Tensor): The number of repeating times. If repeat_times is a list or tuple, all its elements
        should be integers or 1-D Tensors with the data type int32. If repeat_times is a Tensor, it should be an 1-D Tensor with the data type int32.
        alias: ``dims``.
    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. The data type is the same as ``x``. The size of the i-th dimension is equal to ``x[i] * repeat_times[i]``.

Examples:
    .. code-block:: python

        >>> import paddle

        >>> data = paddle.to_tensor([1, 2, 3], dtype='int32')
        >>> out = paddle.tile(data, repeat_times=[2, 1])
        >>> print(out)
        Tensor(shape=[2, 3], dtype=int32, place=Place(cpu), stop_gradient=True,
        [[1, 2, 3],
         [1, 2, 3]])

        >>> out = paddle.tile(data, repeat_times=(2, 2))
        >>> print(out)
        Tensor(shape=[2, 6], dtype=int32, place=Place(cpu), stop_gradient=True,
        [[1, 2, 3, 1, 2, 3],
         [1, 2, 3, 1, 2, 3]])

        >>> repeat_times = paddle.to_tensor([1, 2], dtype='int32')
        >>> out = paddle.tile(data, repeat_times=repeat_times)
        >>> print(out)
        Tensor(shape=[1, 6], dtype=int32, place=Place(cpu), stop_gradient=True,
        [[1, 2, 3, 1, 2, 3]])
c                   [        US[        [        [        [        R
                  R                  4S5        [        U[        [        R
                  R                  45      (       a!  [        UR                  5      S:X  d   S5       eOU H  n[        U[        [        R
                  R                  45      (       a2  [        R                  " S UR                  S5      nUS:X  d   S5       eMd  [        [        R                  [        R                  4n[        X$5      (       a  M   S5       e   [!        U S/ SQS5        [#        U R$                  5      S	:X  a  U R&                  (       d  [)        S
5      eg g )Nr  tiler%   z-repeat_times must be a Tensor with ndim == 1.c                
    X-  $ r	  r   )rc   r   s     rj   <lambda>+tile.<locals>.check_input.<locals>.<lambda>  s    !%r   zEElements in repeat_times must be Tensor with one element or integers.rc   r  rl   zWhen the date type is bool for the input 'x' of tile op, you must set its stop_gradient to be True by some_var.stop_gradient == True supporting some_var is the input.)r   rM   r   r   rN   rT   rU   rL   r   rS   	functoolsr  rR   rP   r@   rq   r   r   r?   ry   r   )rc   r  elemrU  
type_tuples        rj   check_inputtile.<locals>.check_inputx  sA   5(FJJ$4$45		
 lXvzz/?/?$@AA|))*a/ ?/ %dXvzz/?/?$@AA%,,-?QOE A: _: #&rxx!:J%d77 _7 % 	!
 	
  !V+AOOS  5D+r   r%   z6Only support ndim == 1 while repeat_times is a Tensor.c                    / n[        U 5       HK  u  p#[        U[        5      (       a  UR                  S5        M-  UR                  U5        US:  a  MF   S5       e   U$ )Nr   r   z7All elements in repeat_times must be positive for tile.rV   rL   r   r   )list_repeat_timesattrs_repeat_timesr   timess       rj   get_attr_repeat_times#tile.<locals>.get_attr_repeat_times  sa    !#'(9:
eX..&--b1&--e4 19 Q9 ; &%r   r  rA   TRepeatTimesr   r  repeat_times_tensorrc   r  rC   rG   r  )r"   rL   r    r   r,   r   r  r   r  r$   rM   r   rN   r   r   r   r   r\   r   ry   r   r^   r]   r_   )
rc   r  r`   r  r  rg   rI   rK   r?   rh   s
             rj   r  r  D  s   h-^ lDJJ$5$566$$) H) (..0L{{1++	A$lT5M22||((66%||??M{{1++A$
	& 0vx0slH--)-L&$0=!%'DE.!tUm44$9,$GE.!||((66LL88F ,- ""C"877>E 	 	
 
r   repeatsc                    [        XS9$ )a  
Repeat elements of a tensor along specified dimensions.

Args:
    input (Tensor): The input tensor to be repeated.
    *repeats (int|list|tuple|Tensor): The number of times to repeat along each dimension.
        Can be a single integer (applies to the first dimension only), or multiple integers (one per dimension).

Returns:
    Tensor: The repeated tensor with expanded dimensions.

Note:
    When using a single integer, it only repeats along the first dimension.
    The total number of repeat values must match the number of dimensions in the tensor when using multiple values.

Examples:
    .. code-block:: python

        >>> import paddle

        >>> # Example 1: 1D tensor - single repeat
        >>> x = paddle.to_tensor([1, 2, 3])
        >>> out = x.repeat(2)
        >>> print(out)
        Tensor(shape=[6], dtype=int64, place=Place(cpu), stop_gradient=True,
        [1, 2, 3, 1, 2, 3])

        >>> # Example 2: 2D tensor - single repeat value
        >>> x = paddle.to_tensor([[1, 2], [3, 4]])
        >>> out = x.repeat(2)
        >>> print(out)
        Tensor(shape=[2, 4], dtype=int64, place=Place(gpu:0), stop_gradient=True,
        [[1, 2, 1, 2],
        [3, 4, 3, 4]])

        >>> # Example 3: 2D tensor - multiple repeats
        >>> x = paddle.to_tensor([[1, 2], [3, 4]])
        >>> out = x.repeat([2, 3])
        >>> print(out)
        Tensor(shape=[4, 6], dtype=int64, place=Place(gpu:0), stop_gradient=True,
        [[1, 2, 1, 2, 1, 2],
        [3, 4, 3, 4, 3, 4],
        [1, 2, 1, 2, 1, 2],
        [3, 4, 3, 4, 3, 4]])

        >>> # Example 4: 3D tensor - mixed repeats
        >>> x = paddle.to_tensor([[[1, 2], [3, 4]]])
        >>> out = x.repeat([2, 1, 3])
        >>> print(out)
        Tensor(shape=[2, 2, 6], dtype=int64, place=Place(gpu:0), stop_gradient=True,
        [[[1, 2, 1, 2, 1, 2],
        [3, 4, 3, 4, 3, 4]],
        [[1, 2, 1, 2, 1, 2],
        [3, 4, 3, 4, 3, 4]]])
)r  r  )r5   r  s     rj   repeatr     s    x ,,r   r  )rc   rS   c                    [        XU5      $ )a  

Broadcast the input tensor to a given shape.

Both the number of dimensions of ``x`` and the number of elements in ``shape`` should be less than or equal to 6. The dimension to broadcast to must have a value 0.

The following figure shows the process of broadcasting a one-dimensional tensor of shape [3] to a two-dimensional tensor of shape [2,3] based on the shape specified by 'shape'.

.. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/broadcast_to.png
    :width: 500
    :alt: broadcast_to API
    :align: center

.. note::
    Alias Support: The parameter name ``input`` can be used as an alias for ``x``, and ``size`` can be used as an alias for ``shape``.
    For example, ``broadcast_to(input=tensor_x, size=[2, 3], ...)`` is equivalent to ``broadcast_to(x=tensor_x, shape=[2, 3], ...)``.

Args:
    x (Tensor): The input tensor, its data type is bool, float16, float32, float64, int32, int64, uint8 or uint16.
        alias: ``input``.
    shape (list|tuple|Tensor): The result shape after broadcasting. The data type is int32. If shape is a list or tuple, all its elements
        should be integers or 0-D or 1-D Tensors with the data type int32. If shape is a Tensor, it should be an 1-D Tensor with the data type int32.
        The value -1 in shape means keeping the corresponding dimension unchanged.
        alias: ``size``.
    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 Tensor with the given shape. The data type is the same as ``x``.

Examples:
    .. code-block:: python

        >>> import paddle

        >>> data = paddle.to_tensor([1, 2, 3], dtype='int32')
        >>> out = paddle.broadcast_to(data, shape=[2, 3])
        >>> print(out)
        Tensor(shape=[2, 3], dtype=int32, place=Place(cpu), stop_gradient=True,
        [[1, 2, 3],
         [1, 2, 3]])
expand)rc   rS   r`   s      rj   r  r    s    \ !D!!r   c                l   [        U[        [        45      (       a  [        U5      S:X  a  U $ [	        5       (       a  [
        R                  " X5      $ [        5       (       a  [        U R                  5      S:X  a  U R                  (       d  [        S5      e[        U[        R                  R                  5      (       a  SUl
        Oj[        U[        [        45      (       aD  [        R                  R!                  U5      (       a  [        R                  R#                  U5      nO[%        S5      e[
        R                  " X5      $ [        U[&        5      (       a!  [        UR(                  5      S:X  d   S5       eOwU Hq  n[        U[&        5      (       a  UR+                  5       S:X  d   S5       eM5  [,        [.        R0                  [.        R2                  4n[        X45      (       a  Ml   S5       e   [5        U S	/ S
QS5        [7        US[        [        [&        4S5        [        U R                  5      S:X  a  U R                  (       d  [        S5      eSU /0n0 n[9        S0 [;        5       D6nS n[        U[&        5      (       a  SUl
        XS'   Ol[        U[        [        45      (       aQ  U" U5      US'   [        R                  R!                  U5      (       a"  [        R                  R=                  U5      US'   UR?                  S	S9n	URA                  U	5      n
URC                  SUSU
0US9  U
$ )a	  

Expand the input tensor to a given shape.

Both the number of dimensions of ``x`` and the number of elements in ``shape`` should be less than or equal to 6. And the number of dimensions of ``x`` should be less than the number of elements in ``shape``. The dimension to expand must have a value 0.

The image illustrates a typical case of the expand operation.
The Original Tensor is a 1D tensor with shape ``[3]`` and values [1, 2, 3]. Using the ``paddle.expand`` method with the parameter ``shape = [2, 3]``, it is broadcasted and expanded into a 2D tensor with shape ``[2, 3]``

.. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/expand.png
    :width: 500
    :alt: legend of expand API
    :align: center

.. note::
    Alias Support: The parameter name ``input`` can be used as an alias for ``x`` and ``size`` can be used as an alias for ``shape``.
    ``shape`` can be a variable number of arguments.
    For example:
        ``paddle.expand(tensor_x, shape=[3, 4], name=None)``
        ``tensor_x.expand([3, 4]) -> paddle.expand(tensor_x, [3, 4])``
        ``tensor_x.expand(3, 4) -> paddle.expand(tensor_x, 3, 4)``
        ``tensor_x.expand(size=[3, 4]) -> paddle.expand(tensor_x, size=[3, 4])``

Args:
    x (Tensor): The input Tensor, its data type is bool, float16, float32, float64, int32, int64, uint8, uint16, complex64 or complex128.
        alias: ``input``
    shape (list|tuple|Tensor|variable number of arguments): The result shape after expanding. The data type is int32. If shape is a list or tuple, all its elements
        should be integers or 0-D or 1-D Tensors with the data type int32. If shape is a Tensor, it should be an 1-D Tensor with the data type int32.
        The value -1 in shape means keeping the corresponding dimension unchanged.
        ``shape`` can be a variable number of arguments.
        alias: ``size``.
    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:
    N-D Tensor, A Tensor with the given shape. The data type is the same as ``x``.

Examples:
    .. code-block:: python

        >>> import paddle

        >>> data = paddle.to_tensor([1, 2, 3], dtype='int32')
        >>> out = paddle.expand(data, shape=[2, 3])
        >>> print(out)
        Tensor(shape=[2, 3], dtype=int32, place=Place(cpu), stop_gradient=True,
        [[1, 2, 3],
         [1, 2, 3]])
r   rl   zWhen the data type of input 'x' for expand is bool, you must set its stop_gradient to be False by some_var.stop_gradient = True, supporting some_var as the input.Tz-Shape only supports Value, or list, or tuple.r%   zshape must be a 1-D Tensor.z>Elements in shape must be Tensor with one element or integers.rc   )
rl   rm   rn   ro   r@   rq   rr   rs   r   r   r#  rS   rA   c                    / n[        U 5       HS  u  p#[        U[        5      (       a  UR                  S5        M-  UR                  U5        US:  a  MF  US:X  a  MN   S5       e   U$ )Nr   r   z7All elements in shape of expand must be positive or -1.r  )list_expand_shapeattrs_expand_shaper   rS   s       rj   get_attr_expand_shape%expand.<locals>.get_attr_expand_shape  sh    !#'(9:
eX..&--b1&--e4 19 Q3 ; &%r   r   expand_shapes_tensorr  	expand_v2rC   rG   r"  )"rL   rM   r   r   r"   r   r#  r$   r   r?   ry   r   rN   rT   rU   r   r   r   rY   r   rS   rU  rR   rP   r@   rq   r   r   r   r\   r   r^   r]   r_   )rc   rS   r`   r  r  rI   rK   rg   r)  r?   rh   s              rj   r#  r#  L  s   d %$''CJ!O}}Q&&	!V+AOO)  eVZZ--.."&Ee}--||((//88?KLL}}Q&&eX&&u{{#q(G*GG(dH--::<1, X, #&rxx!:J%d77 X7  	! 	
" 	5'D%#:HE!V+AOO)  s22
	& eX&&"&E#7Oe}--259E'N||((//LL88? -. ""C"877>VeS\ 	 	
 
r   c                  ^  U 4S jn[        5       (       Ga  [        U[        [        45      (       a  / nU H^  n[        U[        R
                  R                  5      (       a!  UR                  UR                  5       5        MM  UR                  U5        M`     UT R                  :X  a  T nU$ [        R                  " T U5      n U$ [        U[        R
                  R                  5      (       a   SUl        [        R                  " T U5      nU$ [        S[        U5       S35      e[        5       (       Ga"  [!        T S/ SQS5        [#        US[        [        [$        R&                  R(                  4S5        [        U[        [        45      (       ae  [$        R*                  R-                  U5      (       a   [$        R*                  R/                  U5      nOU" U5      n[        R                  " T U5      nU$ [        U[$        R&                  R(                  5      (       a   SUl        [        R                  " T U5      nU$ [        S	[        U5       S35      e[!        T S/ S
QS5        [#        US[        [        [0        4S5        ST 0n0 n[        U[0        5      (       a  SUl        XS'   Ol[        U[        [        45      (       aQ  U" U5      US'   [$        R*                  R-                  U5      (       a"  [$        R*                  R3                  U5      US'   [5        S0 [7        5       D6n	U	R9                  T R:                  S9nU	R9                  T R:                  S9n
U	R=                  SUUXjS.S9  U$ )aV  
Changes the shape of ``x`` without changing its data.

Note that the output Tensor will share data with origin Tensor and doesn't
have a Tensor copy in ``dygraph`` mode.
If you want to use the Tensor copy version, please use `Tensor.clone` like
``reshape_clone_x = x.reshape([-1]).clone()``.

Some tricks exist when specifying the target shape.

    - 1. -1 means the value of this dimension is inferred from the total element number of x and remaining dimensions. Thus one and only one dimension can be set -1.

    - 2. 0 means the actual dimension value is going to be copied from the corresponding dimension of x. The index of 0s in shape can not exceed the dimension of x.

Here are some examples to explain it.

    - 1. Given a 3-D tensor x with a shape [2, 4, 6], and the target shape is [6, 8], the reshape operator will transform x into a 2-D tensor with shape [6, 8] and leaving x's data unchanged.

    - 2. Given a 3-D tensor x with a shape [2, 4, 6], and the target shape specified is [2, 3, -1, 2], the reshape operator will transform x into a 4-D tensor with shape [2, 3, 4, 2] and leaving x's data unchanged. In this case, one dimension of the target shape is set to -1, the value of this dimension is inferred from the total element number of x and remaining dimensions.

    - 3. Given a 3-D tensor x with a shape [2, 4, 6], and the target shape is [-1, 0, 3, 2], the reshape operator will transform x into a 4-D tensor with shape [2, 4, 3, 2] and leaving x's data unchanged. In this case, besides -1, 0 means the actual dimension value is going to be copied from the corresponding dimension of x.

The following figure illustrates the first example -- a 3D tensor of shape [2, 4, 6] is transformed into a 2D tensor of shape [6, 8], during which the order and values of the elements in the tensor remain unchanged. The elements in the two subdiagrams correspond to each other, clearly demonstrating how the reshape API works.

.. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/reshape.png
    :width: 800
    :alt: legend of reshape API
    :align: center

Args:
    x (Tensor): An N-D Tensor. The data type is ``float16``, ``float32``, ``float64``, ``int16``, ``int32``, ``int64``, ``int8``, ``uint8``, ``complex64``, ``complex128``, ``bfloat16`` or ``bool``.
    shape (list|tuple|Tensor): Define the target shape. At most one dimension of the target shape can be -1.
                    The data type is ``int32`` . If ``shape`` is a list or tuple, each element of it should be integer or Tensor with shape [].
                    If ``shape`` is a Tensor, it should be an 1-D Tensor .
    name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.

Returns:
    Tensor, A reshaped Tensor with the same data type as ``x``.

Examples:
    .. code-block:: pycon

        >>> import paddle

        >>> x = paddle.rand([2, 4, 6], dtype="float32")
        >>> positive_four = paddle.full([1], 4, "int32")

        >>> out = paddle.reshape(x, [-1, 0, 3, 2])
        >>> print(out.shape)
        paddle.Size([2, 4, 3, 2])

        >>> out = paddle.reshape(x, shape=[positive_four, 12])
        >>> print(out.shape)
        paddle.Size([4, 12])

        >>> shape_tensor = paddle.to_tensor([8, 6], dtype=paddle.int32)
        >>> out = paddle.reshape(x, shape=shape_tensor)
        >>> print(out.shape)
        paddle.Size([8, 6])
        >>> # out shares data with x in dygraph mode
        >>> x[0, 0, 0] = 10.0
        >>> print(out[0, 0])
        Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
        10.)

c                  > Sn/ n[        U 5       H  u  p4[        U[        [        R                  R
                  45      (       a  UR                  S5        MG  UR                  U5        US:X  a  US:X  d   SU S35       eUnMs  US:X  ac  [        R                  " TR                  5      (       a<  U[        TR                  5      :  d!   SU S[        TR                  5       S35       eM  M  US:  a  M   SU S	U< S35       e   U$ )
Nr   zMOnly one dimension value of 'shape' in reshape can be -1. But received shape[aO  ] is also -1.

	# N = x.shape()[2]		# N is an int. (NOT recommend under @to_static)
	N = paddle.shape(x)[2]		# N is a Tensor. (Recommend)
	z = paddle.reshape([N, -1, 4])	# z.shape is [-1, -1, 4]

    If your target shape in Reshape represents dynamic shape, please turn it into a Tensor under @to_static. See above example for details.r   z`The index of 0 in `shape` must be less than the input tensor X's dimensions. But received shape[z] = 0, X's dimensions = r   zqEach dimension value of 'shape' in reshape must not be negative except one unknown dimension. But received shape[z] = )rV   rL   r   rN   rT   rU   r   r  r  rS   r   )
list_shapere  attrs_shapedim_idxr   rc   s        rj   get_attr_shapereshape.<locals>.get_attr_shape#  s)   !*:!6G(Xvzz/?/?$@AA""2&""8,r>&", 	55<I >hh	, #*K]yy))&QWW5 229:RSVWXW^W^S_R``ac5 * $a< ..5Yd8,aI<3 "7< r   Tz>shape must be an instance of `list`, `tuple` `Variable`, got '.'rc   )rt   rm   rn   ro   rw   rr   rp   r@   rq   rl   rs   r   r   r   rS   zHshape must be an instance of `list`, `tuple` `Value(in pir mode)`, got ')rm   rn   ro   rp   r@   rq   rl   rs   rw   rr   r   r   rA   r   r   reshape2r>   r   r   )r5  )r"   rL   rM   r   r    r   r,   r   r   rS   r   r   ry   r   rH   r$   r   r   rN   rT   rU   r   r   r   r   r   r   r\   r]   r?   r_   )rc   rS   r`   r2  r   rm  rh   rI   rK   rg   r   s   `          rj   r   r     s	   J!F edE]++Ic4::#4#455$$SXXZ0$$S)	 
 AGG# 
 nnQ	2 
 tzz0011"&E..E*C 
	 eR) 
 
  %	
( 	5'D%1A1A#BINedE]++||((//"LL<<UC	*51	..I.C 
 vzz//00"&E..E*C 
 eR)  	! #	
& 	5'D%#:IFqeX&&"&E#7Oe}--+E2E'N||((//(.(L(L)}% 468477agg7F;;!'';J3	 	 	
 
r   c                J   U R                   UR                   :X  d!   SU R                    SUR                    35       eUR                   [        R                  :X  d   e[        R                  " U [        S9n[        R
                  " [        R                  " USS9U5      n[        R                  " UR                  5       S-
  SS9n[        5       (       ae  UR                  5       S:w  aQ  US   UR                  5       ::  d:   S	US   R                  5        S
UR                  5       R                  5        35       eUR                  5       U   R                  UR                  5      n[        R                  " UR!                  [        5      5      n[        R"                  " XU5      $ )a
  
Copies elements from `value` into `x` tensor at positions where the `mask` is True.

Elements from source are copied into `x` starting at position 0 of `value` and continuing in order one-by-one for
each occurrence of `mask` being True. The shape of `mask` must be broadcastable with the shape of the underlying tensor.
The `value` should have at least as many elements as the number of ones in `mask`.

The image illustrates a typical case of the masked_scatter operation.

  1. Tensor  ``value``: Contains the data to be filled into the target tensor. Only the parts where the mask is True will take values from the value tensor, while the rest will be ignored;
  2. Tensor  ``mask``: Specifies which positions should extract values from the value tensor and update the target tensor. True indicates the corresponding position needs to be updated;
  3. Tensor  ``origin``: The input tensor, where only the parts satisfying the mask will be replaced, and the rest remains unchanged;

Result: After the ``masked_scatter`` operation, the parts of the ``origin`` tensor where the ``mask`` is ``True`` are updated with the corresponding values from the ``value`` tensor, while the parts where the ``mask`` is ``False`` remain unchanged, forming the final updated tensor.

.. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/masked_scatter.png
    :width: 500
    :alt: legend of masked_scatter API
    :align: center

Args:
    x (Tensor): An N-D Tensor. The data type is ``float16``, ``float32``, ``float64``, ``int32``,
        ``int64`` or ``bfloat16``.
    mask (Tensor): The boolean tensor indicate the position to be filled.
        The data type of mask must be bool.
    value (Tensor): The value used to fill the target tensor.
        Supported data types are same as x.
    name (str|None, optional): Name for the operation (optional, default is None). For more information,
        please refer to :ref:`api_guide_Name`.

Returns:
    Tensor, A reshaped Tensor with the same data type as ``x``.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> paddle.seed(2048)
        >>> x = paddle.randn([2, 2])
        >>> print(x)
        Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
            [[-1.24725831,  0.03843464],
            [-0.31660911,  0.04793844]])

        >>> mask = paddle.to_tensor([[True, True], [False, False]])
        >>> value = paddle.to_tensor([1, 2, 3, 4, 5,], dtype="float32")

        >>> out = paddle.masked_scatter(x, mask, value)
        >>> print(out)
        Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
            [[1,  2],
            [-0.31660911,  0.04793844]])

9x and value must have the same dtype, but got x dtype is , value dtype is r>   rR   r%   r   r   r   @mask true nums must be <= value size, but got mask true nums is , value size is )r?   rN   rl   
zeros_likerR   addrv   clipcumsumr"   rU  r   r<  r   rS   logical_notr  where)rc   maskr   r`   zeros_like_xmask_prefixs         rj   masked_scatterrE    su   t 77ekk! 
CAGG9L]^c^i^i]jk! ::$$$$$Qc2L::fkk$e4lCD++dkkma/Q7K[..0A52%++-/ 	
N{[]OcOcOeNffvw|  xC  xC  xE  xJ  xJ  xL  wM  N	
/ MMOK(00<Edkk$/0D<<''r   c                   U R                   UR                   :X  d!   SU R                    SUR                    35       eUR                   [        R                  :X  d   e[        R                  " U [        S9n[        R
                  " [        R                  " USS9U5      n[        R                  " UR                  5       S-
  SS9nUS   UR                  5       ::  d:   S	US   R                  5        S
UR                  5       R                  5        35       eUR                  5       U   R                  UR                  5      n[        R                  " UR                  [        5      5      n[        R                   " XU5      nU$ )z
Inplace version of ``masked_scatter`` API, the output Tensor will be inplaced with input ``x``.
Please refer to :ref:`api_paddle_masked_scatter`.
r7  r8  r>   rR   r%   r   r9  r   r:  r;  )r?   rN   rl   r<  rR   r=  rv   r>  r?  rU  r   r<  r   rS   r@  r  where_)rc   rB  r   r`   rC  rD  rh   s          rj   masked_scatter_rH    sW    77ekk! 
CAGG9L]^c^i^i]jk! ::$$$$$Qc2L::fkk$e4lCD++dkkma/Q7Kr?ekkm+ 
J;WY?K_K_KaJbbrsxs~s~  tA  tF  tF  tH  sI  	J+ MMOK(00<Edkk$/0D
--
'CJr   c                   [        5       (       a  [        R                  R                  n[	        U[
        [        45      (       a_  U Vs/ s H&  n[	        XC5      (       a  UR                  S5      OUPM(     nnXR                  :X  a  U nU$ [        R                  " X5      n U$ [	        X5      (       a  SUl        [        R                  " X5      nU$ [        S[        U5       S35      egs  snf )z
Inplace version of ``reshape`` API, the output Tensor will be inplaced with input ``x``.
Please refer to :ref:`api_paddle_tensor_reshape`.
r   TzAshape must be an instance of `list`, `tuple` or `Variable`, got 'r4  N)r"   r    r   r,   rL   rM   r   r   rS   r   reshape_ry   r   rH   )rc   rS   r`   tmp_tensor_typer   rh   s         rj   rJ  rJ    s     **++edE]++ "!D !+4 A A		!tK!    
 ooa/ 
 //"&E//!+C 
 eR)  s   	-C,c                    g r	  r   rI   r`   s     rj   rJ  rJ  2      BEr   rX  c                    g r	  r   r`   rI   s     rj   rJ  rJ  6      ILr   c                `   [        U5      S:X  a=  [        US   [        [        45      (       a  [	        S US    5       5      (       a  US   n/ nU H  n[        U[
        R                  [
        R                  R                  R                  [
        R                  R                  R                  R                  45      (       d  [
        R                  " U5      nOUnUR                  5       S:X  a  UR                  S5      nOUnUR!                  U5        M     [        U5      S:X  a  US   $ U$ )a  
Convert inputs to tensors and return the view with at least 1-dimension. Scalar inputs are converted,
one or high-dimensional inputs are preserved.

Args:
    inputs (list[Tensor]): One or more tensors. The data type is ``float16``, ``float32``, ``float64``, ``int16``, ``int32``, ``int64``, ``int8``, ``uint8``, ``complex64``, ``complex128``, ``bfloat16`` or ``bool``.
    name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.

Returns:
    One Tensor, if there is only one input.
    List of Tensors, if there are more than one inputs.

Examples:
    .. code-block:: python

        >>> import paddle

        >>> # one input
        >>> x = paddle.to_tensor(123, dtype='int32')
        >>> out = paddle.atleast_1d(x)
        >>> print(out)
        Tensor(shape=[1], dtype=int32, place=Place(cpu), stop_gradient=True,
        [123])

        >>> # more than one inputs
        >>> x = paddle.to_tensor(123, dtype='int32')
        >>> y = paddle.to_tensor([1.23], dtype='float32')
        >>> out = paddle.atleast_1d(x, y)
        >>> print(out)
        [Tensor(shape=[1], dtype=int32, place=Place(cpu), stop_gradient=True,
        [123]), Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
        [1.23000002])]

        >>> # more than 1-D input
        >>> x = paddle.to_tensor(123, dtype='int32')
        >>> y = paddle.to_tensor([[1.23]], dtype='float32')
        >>> out = paddle.atleast_1d(x, y)
        >>> print(out)
        [Tensor(shape=[1], dtype=int32, place=Place(cpu), stop_gradient=True,
        [123]), Tensor(shape=[1, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[1.23000002]])]
r%   r   c              3     #    U  Hp  n[        U[        R                  [        R                  R                  R
                  [        R                  R                  R                  R                  45      v   Mr     g 7fr	  	rL   rN   r,   rz  	frameworkr   	libpaddlerT   rU   .0r   s     rj   	<genexpr>atleast_1d.<locals>.<genexpr>f  `      

 " MMKK))22KK))--33  "   A8A:)r%   )r   rL   rM   r   anyrN   r,   rz  rU  r   rV  rT   rU   rO   r   r   r   r`   rI   rh   r5   r#  results         rj   rJ  rJ  :  s   V 6{aJvay4-@@ 

 q	

 

 

 AYF
C%%..%%))//
 
 %%e,FF::<1^^D)FF

6# & 3x1}1v
r   c                    g r	  r   rM  s     rj   rO  rO    rN  r   c                    g r	  r   rP  s     rj   rO  rO    rQ  r   c                   [        U5      S:X  a=  [        US   [        [        45      (       a  [	        S US    5       5      (       a  US   n/ nU H  n[        U[
        R                  [
        R                  R                  R                  [
        R                  R                  R                  R                  45      (       d  [
        R                  " U5      nOUnUR                  5       S:X  a  UR                  S5      nO,UR                  5       S:X  a  [
        R                   " USS9nOUnUR#                  U5        M     [        U5      S:X  a  US   $ U$ )a  
Convert inputs to tensors and return the view with at least 2-dimension. Two or high-dimensional inputs are preserved.

The following diagram illustrates the behavior of atleast_2d on different dimensional inputs for the following cases:

    1. A 0-dim tensor input.
    2. A 0-dim tensor and a 1-dim tensor input.
    3. A 0-dim tensor and a 3-dim tensor input.

.. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/atleast_2d.png
    :width: 600
    :alt: legend of atleast_2d API
    :align: center

In each case, the function returns the tensors (or a list of tensors) in views with at least 2 dimensions.

Args:
    inputs (Tensor|list(Tensor)): One or more tensors. The data type is ``float16``, ``float32``, ``float64``, ``int16``, ``int32``, ``int64``, ``int8``, ``uint8``, ``complex64``, ``complex128``, ``bfloat16`` or ``bool``.
    name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.

Returns:
    One Tensor, if there is only one input.
    List of Tensors, if there are more than one inputs.

Examples:
    .. code-block:: python

        >>> import paddle

        >>> # one input
        >>> x = paddle.to_tensor(123, dtype='int32')
        >>> out = paddle.atleast_2d(x)
        >>> print(out)
        Tensor(shape=[1, 1], dtype=int32, place=Place(cpu), stop_gradient=True,
        [[123]])

        >>> # more than one inputs
        >>> x = paddle.to_tensor(123, dtype='int32')
        >>> y = paddle.to_tensor([1.23], dtype='float32')
        >>> out = paddle.atleast_2d(x, y)
        >>> print(out)
        [Tensor(shape=[1, 1], dtype=int32, place=Place(cpu), stop_gradient=True,
        [[123]]), Tensor(shape=[1, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[1.23000002]])]

        >>> # more than 2-D input
        >>> x = paddle.to_tensor(123, dtype='int32')
        >>> y = paddle.to_tensor([[[1.23]]], dtype='float32')
        >>> out = paddle.atleast_2d(x, y)
        >>> print(out)
        [Tensor(shape=[1, 1], dtype=int32, place=Place(cpu), stop_gradient=True,
        [[123]]), Tensor(shape=[1, 1, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[[1.23000002]]])]
r%   r   c              3     #    U  Hp  n[        U[        R                  [        R                  R                  R
                  [        R                  R                  R                  R                  45      v   Mr     g 7fr	  rT  rW  s     rj   rY  atleast_2d.<locals>.<genexpr>  r[  r\  )r%   r%   r:   r   rL   rM   r   r]  rN   r,   rz  rU  r   rV  rT   rU   rO   r   r   r  r   r^  s         rj   rO  rO    s&   n 6{aJvay4-@@ 

 q	

 

 

 AYF
C%%..%%))//
 
 %%e,FF::<1^^F+FZZ\Q%%f15FF

6' * 3x1}1v
r   c                    g r	  r   rM  s     rj   rR  rR    rN  r   c                    g r	  r   rP  s     rj   rR  rR    rQ  r   c                   [        U5      S:X  a=  [        US   [        [        45      (       a  [	        S US    5       5      (       a  US   n/ nU GH  n[        U[
        R                  [
        R                  R                  R                  [
        R                  R                  R                  R                  45      (       d  [
        R                  " U5      nOUnUR                  5       S:X  a  UR                  S5      nOXUR                  5       S:X  a  [
        R                   " USS/S9nO,UR                  5       S:X  a  [
        R                   " USS9nOUnUR#                  U5        GM     [        U5      S:X  a  US   $ U$ )a  
Convert inputs to tensors and return the view with at least 3-dimension. Three or high-dimensional inputs are preserved.

Args:
    inputs (Tensor|list(Tensor)): One or more tensors. The data type is ``float16``, ``float32``, ``float64``, ``int16``, ``int32``, ``int64``, ``int8``, ``uint8``, ``complex64``, ``complex128``, ``bfloat16`` or ``bool``.
    name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.

Returns:
    One Tensor, if there is only one input.
    List of Tensors, if there are more than one inputs.

Examples:
    .. code-block:: python

        >>> import paddle

        >>> # one input
        >>> x = paddle.to_tensor(123, dtype='int32')
        >>> out = paddle.atleast_3d(x)
        >>> print(out)
        Tensor(shape=[1, 1, 1], dtype=int32, place=Place(cpu), stop_gradient=True,
        [[[123]]])

        >>> # more than one inputs
        >>> x = paddle.to_tensor(123, dtype='int32')
        >>> y = paddle.to_tensor([1.23], dtype='float32')
        >>> out = paddle.atleast_3d(x, y)
        >>> print(out)
        [Tensor(shape=[1, 1, 1], dtype=int32, place=Place(cpu), stop_gradient=True,
        [[[123]]]), Tensor(shape=[1, 1, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[[1.23000002]]])]

        >>> # more than 3-D input
        >>> x = paddle.to_tensor(123, dtype='int32')
        >>> y = paddle.to_tensor([[[[1.23]]]], dtype='float32')
        >>> out = paddle.atleast_3d(x, y)
        >>> print(out)
        [Tensor(shape=[1, 1, 1], dtype=int32, place=Place(cpu), stop_gradient=True,
        [[[123]]]), Tensor(shape=[1, 1, 1, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[[[1.23000002]]]])]
r%   r   c              3     #    U  Hp  n[        U[        R                  [        R                  R                  R
                  [        R                  R                  R                  R                  45      v   Mr     g 7fr	  rT  rW  s     rj   rY  atleast_3d.<locals>.<genexpr>)  r[  r\  )r%   r%   r%   r   r:   re  r^  s         rj   rR  rR    sJ   T 6{aJvay4-@@ 

 q	

 

 

 AYF
C%%..%%))//
 
 %%e,FF::<1^^I.FZZ\Q%%fAq6:FZZ\Q%%f15FF

6+ . 3x1}1v
r   c                V   [        5       (       a0  [        UR                  SSS/S5        [        R                  " X5      $ [        U S/ SQS5        [        USSS/S5        [        S
0 [        5       D6nUR                  5       nUR                  U5      nUR                  SXS.SU0S	9  U$ )a  

This function is actually a high-dimensional extension of :code:`gather`
and supports for simultaneous indexing by multiple axes. :attr:`index` is a
K-dimensional integer tensor, which is regarded as a (K-1)-dimensional
tensor of :attr:`index` into :attr:`input`, where each element defines
a slice of params:

.. math::

    output[(i_0, ..., i_{K-2})] = input[index[(i_0, ..., i_{K-2})]]

Obviously, :code:`index.shape[-1] <= input.rank` . And, the output tensor has
shape :code:`index.shape[:-1] + input.shape[index.shape[-1]:]` .

.. code-block:: text

        Given:
            x =  [[[ 0,  1,  2,  3],
                   [ 4,  5,  6,  7],
                   [ 8,  9, 10, 11]],
                  [[12, 13, 14, 15],
                   [16, 17, 18, 19],
                   [20, 21, 22, 23]]]
            x.shape = (2, 3, 4)

        * Case 1:
            index = [[1]]

            gather_nd(x, index)
                     = [x[1, :, :]]
                     = [[12, 13, 14, 15],
                        [16, 17, 18, 19],
                        [20, 21, 22, 23]]

        * Case 2:
            index = [[0,2]]

            gather_nd(x, index)
                     = [x[0, 2, :]]
                     = [8, 9, 10, 11]

        * Case 3:
            index = [[1, 2, 3]]

            gather_nd(x, index)
                     = [x[1, 2, 3]]
                     = [23]

Args:
    x (Tensor): The input Tensor which it's data type should be bool, float16, float32, float64, int32, int64.
    index (Tensor): The index input with rank > 1, index.shape[-1] <= input.rank.
                    Its dtype should be 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:
    output (Tensor), A tensor with the shape index.shape[:-1] + input.shape[index.shape[-1]:]

Examples:

    .. code-block:: python

        >>> import paddle

        >>> x = paddle.to_tensor([[[1, 2], [3, 4], [5, 6]],
        ...                       [[7, 8], [9, 10], [11, 12]]])
        >>> index = paddle.to_tensor([[0, 1]])

        >>> output = paddle.gather_nd(x, index)
        >>> print(output)
        Tensor(shape=[1, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
        [[3, 4]])

rk  r@   rq   	gather_ndrc   )rl   rm   rs   rn   ro   rp   r@   rq   r  rC   r  )rl  )r#   r   r?   r   rl  r   r   r\   r^   r]   r_   )rc   rk  r`   rg   r?   r  s         rj   rl  rl  T  s    V EKK7G*<kJ)) 	 	
 	!7Wg.	
 5FH5""$::5A+FO 	 	

 r   c                  ^ [        5       (       a  [        R                  " XX#U5      $ [        5       (       a3  S nU" U5      nU" U5      nU" U5      n[        R                  " XX#U5      $ [	        S0 [        5       D6m[        U S/ SQS5        [        US[        [        4S5        [        US[        [        [        4S5        [        US[        [        [        4S5        [        US[        [        [        4S5        S	 nU" US5        U" US5        U" US5        U" US5        U4S
 jnSU 0n	SU0n
[        [        U5      5       Vs/ s H  nSPM     nn[        U[        5      (       a  SUl        X)S'   O[        U[        [        45      (       a  / U
S'   [        R                   R#                  U5      (       ac  U" U5      U	S'   [%        U5       HH  u  p[        U[        5      (       a  U
S   R'                  S5        SX'   M4  U
S   R'                  U5        MJ     OX*S'   [        U[        5      (       a  SUl        X9S'   O[        U[        [        45      (       a  / U
S'   [        R                   R#                  U5      (       ac  U" U5      U	S'   [%        U5       HH  u  p[        U[        5      (       a  U
S   R'                  S5        SX'   M4  U
S   R'                  U5        MJ     OX:S'   [        U[        5      (       a  SUl        XIS'   O[        U[        [        45      (       a  / U
S'   [        R                   R#                  U5      (       ac  U" U5      U	S'   [%        U5       HH  u  p[        U[        5      (       a  U
S   R'                  S5        SX'   M4  U
S   R'                  U5        MJ     OXJS'   XS'   TR)                  TR+                  S5      S9nTR-                  SU	U
SU0S9  U$ s  snf )a  
This operator produces a slice of ``x`` along multiple axes. Similar to numpy:
https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html
Slice uses ``axes``, ``starts`` and ``ends`` attributes to specify the start and
end dimension for each axis in the list of axes and Slice uses this information
to slice the input data tensor. If a negative value is passed to
``starts`` or ``ends`` such as :math:`-i`,  it represents the reverse position of the
axis :math:`i-1` th(here 0 is the initial position). The ``strides`` represents steps of
slicing and if the ``strides`` is negative, slice operation is in the opposite direction.
If the value passed to ``starts`` or ``ends`` is greater than n
(the number of elements in this dimension), it represents n.
For slicing to the end of a dimension with unknown size, it is recommended
to pass in INT_MAX. The size of ``axes`` must be equal to ``starts`` , ``ends`` and ``strides``.
Following examples will explain how strided_slice works:

.. code-block:: text

    Case1:
        Given:
            data = [ [1, 2, 3, 4], [5, 6, 7, 8], ]
            axes = [0, 1]
            starts = [1, 0]
            ends = [2, 3]
            strides = [1, 1]
        Then:
            result = [ [5, 6, 7], ]

    Case2:
        Given:
            data = [ [1, 2, 3, 4], [5, 6, 7, 8], ]
            axes = [0, 1]
            starts = [0, 1]
            ends = [2, 0]
            strides = [1, -1]
        Then:
            result = [ [8, 7, 6], ]
    Case3:
        Given:
            data = [ [1, 2, 3, 4], [5, 6, 7, 8], ]
            axes = [0, 1]
            starts = [0, 1]
            ends = [-1, 1000]
            strides = [1, 3]
        Then:
            result = [ [2], ]

Args:
    x (Tensor): An N-D ``Tensor``. The data type is ``bool``, ``float16``, ``float32``, ``float64``, ``int32`` or ``int64``.
    axes (list|tuple): The data type is ``int32`` . Axes that `starts` and `ends` apply to.
                        It's optional. If it is not provides, it will be treated as :math:`[0,1,...,len(starts)-1]`.
    starts (list|tuple|Tensor): The data type is ``int32`` . If ``starts`` is a list or tuple, the elements of it should be
        integers or Tensors with shape []. If ``starts`` is an Tensor, it should be an 1-D Tensor.
        It represents starting indices of corresponding axis in ``axes``.
    ends (list|tuple|Tensor): The data type is ``int32`` . If ``ends`` is a list or tuple, the elements of it should be
        integers or Tensors with shape []. If ``ends`` is an Tensor, it should be an 1-D Tensor.
        It represents ending indices of corresponding axis in ``axes``.
    strides (list|tuple|Tensor): The data type is ``int32`` . If ``strides`` is a list or tuple, the elements of it should be
        integers or Tensors with shape []. If ``strides`` is an Tensor, it should be an 1-D Tensor.
        It represents slice step of corresponding axis in ``axes``.
    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:
    Tensor, A ``Tensor`` with the same dimension as ``x``. The data type is same as ``x``.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> x = paddle.zeros(shape=[3,4,5,6], dtype="float32")
        >>> # example 1:
        >>> # attr starts is a list which doesn't contain Tensor.
        >>> axes = [1, 2, 3]
        >>> starts = [-3, 0, 2]
        >>> ends = [3, 2, 4]
        >>> strides_1 = [1, 1, 1]
        >>> strides_2 = [1, 1, 2]
        >>> sliced_1 = paddle.strided_slice(x, axes=axes, starts=starts, ends=ends, strides=strides_1)
        >>> # sliced_1 is x[:, 1:3:1, 0:2:1, 2:4:1].
        >>> # example 2:
        >>> # attr starts is a list which contain tensor Tensor.
        >>> minus_3 = paddle.full(shape=[1], fill_value=-3, dtype='int32')
        >>> sliced_2 = paddle.strided_slice(x, axes=axes, starts=[minus_3, 0, 2], ends=ends, strides=strides_2)
        >>> # sliced_2 is x[:, 1:3:1, 0:2:1, 2:4:2].
c                &   [        U [        R                  R                  5      (       a	  SU l        U $ [        U [
        [        45      (       aC  [        R                  R                  U 5      (       a  [        R                  R                  U 5      n U $ )NT)
rL   rN   rT   rU   ry   rM   r   r   r   r   rA  s    rj   r   .strided_slice.<locals>._convert_to_tensor_list!  si    %!1!122&*# L ED%=11<<,,U33"LL<<UCELr   strided_slicerc   )rl   rm   rs   rn   ro   r@   rq   r   r   r   r   c                   [        U [        5      (       a  [        U R                  USS/S5        g [	        U 5       HI  u  p#US-   [        U5      -   S-   n[        U[        5      (       d  M0  [        UR                  US/S5        MK     g )Nr@   rq   rp  [r=   )rL   r   r   r?   rV   rW   )
list_input
input_namere   varvar_names        rj   check_list_elements_dtype0strided_slice.<locals>.check_list_elements_dtypeD  s}    *h//$$g&#	 (
3FA)C/#a&83>H!#x00#IIx'O 4r   c           	     
  > / nU  Hy  n[        U[        5      (       a  SUl        UR                  U5        M2  [        U[        5      (       d   eTR                  S5      n[        S/SUSUS9  UR                  U5        M{     U$ )NTr@   r%   r   )rL   r   ry   r   rR   r]   r   )old_listnew_list_tensorr   r   rg   s       rj   get_new_list_tensor*strided_slice.<locals>.get_new_list_tensorY  s     Oc8,,(,C%#**3/%c3////%HH H "WcTx $**84   #"r   r   r%   Tr   r   r   r   r   StridesTensorStridesTensorListr   r>   rC   r   )rp  )r"   r   rp  r$   r   r\   r   r   rM   r   r   r   r   rL   ry   rN   r   r   rV   r   r]   r^   r_   )rc   r   r   r   r   r`   r   rw  r|  rI   rK   re   r   r   rh   rg   s                  @rj   rp  rp    s   z ##AV7CC		 )0&t,)'2##AV7CC99  	
 	4$@68dE8%<oN4$x!8/J7IeX'>P	  	"$/!&(3!$/!'95	#" 1"'D	"23"2Qq"23fh''#'F %+>"u.. E(O||((00-@-H)*'/FA!#x00h..r2)+h..s3 0 #)h dH%%!%D#'< tUm,,E&M||((..+>t+D'('oFA!#x00f,,R0)+f,,S1 . !%f gx(($(G!&-?#$//!E)||((11.A'.J*+'0FA!#x00i(//3)+i(//4 1 $+i *m77$$S) 8 
 	 CL	 	 	
 
} 4s   O2c                  ^ Sm/ SQn[        U SUT5        [        USUT5        [        US[        [        [        [
        [        R                  R                  4T5        U4S jn/ n/ n[        R                  " [        U5      [        R                  5      (       aF  US:  d   ST-   S	U S
3-   5       e[        U R                  U-
  U R                  5      n[        U5      nO[        U[
        5      (       a  U" U5      nU(       a6  [        R                  " [        US   5      [        R                  5      (       a  UnOSUS   n[!        U5      S:  a  US   n[        U[
        5      (       a  U" U5      n[        U[
        5      (       a  U" U5      n[	        U5      [	        U5      pv[!        U5      [!        U5      pX:  a  UR#                  XxS 5        OX:  a  UR#                  XiS 5        [	        U R$                  5      [	        UR$                  5      p[        R&                  " U R                  [(        S9n[        R&                  " UR                  [(        S9nSn[        [!        U5      5       H  nXo   X   nnU
U   UU   nnUS:X  a/  SUU'   UR+                  UUR,                  S9R/                  U5      nOUUS:X  a/  SU
U'   U R+                  UU R,                  S9R/                  U
5      n O UU:X  d   ST-   SU SU SU SU S
3	-   5       eSUU'   SUU'   XU   -  nM     / n/ n/ nSnSn[        U R                  5       H9  nX   (       a  M  UR1                  U5        UR1                  X   5        UX   -  nM;     UR#                  U5        UR#                  U5        [        UR                  5       H9  nX   (       a  M  UR1                  U5        UR1                  X   5        UX   -  nM;     U R3                  US9R/                  UU/5      n UR3                  US9R/                  UU/5      nU R5                  U5      R/                  U5      nU$ )a  
This function computes a contraction, which sum the product of elements from two tensors along the given axes.

Args:
    x (Tensor): The left tensor for contraction with data type ``float16`` or ``float32`` or ``float64``.
    y (Tensor): The right tensor for contraction with the same data type as ``x``.
    axes (int|tuple|list|Tensor, optional):  The axes to contract for ``x`` and ``y``, defaulted to integer ``2``.

        1. It could be a non-negative integer ``n``,
           in which the function will sum over the last ``n`` axes of ``x`` and the first ``n`` axes of ``y`` in order.

        2. It could be a 1-d tuple or list with data type ``int``, in which ``x`` and ``y`` will be contracted along the same given axes.
           For example, ``axes`` =[0, 1] applies contraction along the first two axes for ``x`` and the first two axes for ``y``.

        3. It could be a tuple or list containing one or two 1-d tuple|list|Tensor with data type ``int``.
           When containing one tuple|list|Tensor, the data in tuple|list|Tensor specified the same axes for ``x`` and ``y`` to contract.
           When containing two tuple|list|Tensor, the first will be applied to ``x`` and the second to ``y``.
           When containing more than two tuple|list|Tensor, only the first two axis sequences will be used while the others will be ignored.

        4. It could be a tensor, in which the ``axes`` tensor will be translated to a python list
           and applied the same rules described above to determine the contraction axes.
           Note that the ``axes`` with Tensor type is ONLY available in Dygraph mode.
    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` .

Return:
    Output (Tensor), The contraction result with the same data type as ``x`` and ``y``.
    In general, :math:`output.ndim = x.ndim + y.ndim - 2 \times n_{axes}`, where :math:`n_{axes}` denotes the number of axes to be contracted.

NOTES:
    1. This function supports tensor broadcast,
       the size in the corresponding dimensions of ``x`` and ``y`` should be equal, or applies to the broadcast rules.
    2. This function also supports axes expansion,
       when the two given axis sequences for ``x`` and ``y`` are of different lengths,
       the shorter sequence will expand the same axes as the longer one at the end.
       For example, if ``axes`` =[[0, 1, 2, 3], [1, 0]],
       the axis sequence for ``x`` is [0, 1, 2, 3],
       while the corresponding axis sequences for ``y`` will be expanded from [1, 0] to [1, 0, 2, 3].

Examples:
    .. code-block:: python

        >>> import paddle
        >>> from typing import Literal

        >>> data_type: Literal["float64"] = 'float64'

        >>> # For two 2-d tensor x and y, the case axes=0 is equivalent to outer product.
        >>> # Note that tensordot supports empty axis sequence, so all the axes=0, axes=[], axes=[[]], and axes=[[],[]] are equivalent cases.
        >>> x = paddle.arange(4, dtype=data_type).reshape([2, 2])
        >>> y = paddle.arange(4, dtype=data_type).reshape([2, 2])
        >>> z = paddle.tensordot(x, y, axes=0)
        >>> print(z)
        Tensor(shape=[2, 2, 2, 2], dtype=float64, place=Place(cpu), stop_gradient=True,
         [[[[0., 0.],
            [0., 0.]],
           [[0., 1.],
            [2., 3.]]],
          [[[0., 2.],
            [4., 6.]],
           [[0., 3.],
            [6., 9.]]]])

        >>> # For two 1-d tensor x and y, the case axes=1 is equivalent to inner product.
        >>> x = paddle.arange(10, dtype=data_type)
        >>> y = paddle.arange(10, dtype=data_type)
        >>> z1 = paddle.tensordot(x, y, axes=1)
        >>> z2 = paddle.dot(x, y)
        >>> print(z1)
        Tensor(shape=[], dtype=float64, place=Place(cpu), stop_gradient=True,
        285.)
        >>> print(z2)
        Tensor(shape=[], dtype=float64, place=Place(cpu), stop_gradient=True,
        285.)


        >>> # For two 2-d tensor x and y, the case axes=1 is equivalent to matrix multiplication.
        >>> x = paddle.arange(6, dtype=data_type).reshape([2, 3])
        >>> y = paddle.arange(12, dtype=data_type).reshape([3, 4])
        >>> z1 = paddle.tensordot(x, y, axes=1)
        >>> z2 = paddle.matmul(x, y)
        >>> print(z1)
        Tensor(shape=[2, 4], dtype=float64, place=Place(cpu), stop_gradient=True,
        [[20., 23., 26., 29.],
         [56., 68., 80., 92.]])
        >>> print(z2)
        Tensor(shape=[2, 4], dtype=float64, place=Place(cpu), stop_gradient=True,
        [[20., 23., 26., 29.],
         [56., 68., 80., 92.]])

        >>> # When axes is a 1-d int list, x and y will be contracted along the same given axes.
        >>> # Note that axes=[1, 2] is equivalent to axes=[[1, 2]], axes=[[1, 2], []], axes=[[1, 2], [1]], and axes=[[1, 2], [1, 2]].
        >>> x = paddle.arange(24, dtype=data_type).reshape([2, 3, 4])
        >>> y = paddle.arange(36, dtype=data_type).reshape([3, 3, 4])
        >>> z = paddle.tensordot(x, y, axes=[1, 2])
        >>> print(z)
        Tensor(shape=[2, 3], dtype=float64, place=Place(cpu), stop_gradient=True,
        [[506. , 1298., 2090.],
         [1298., 3818., 6338.]])

        >>> # When axes is a list containing two 1-d int list, the first will be applied to x and the second to y.
        >>> x = paddle.arange(60, dtype=data_type).reshape([3, 4, 5])
        >>> y = paddle.arange(24, dtype=data_type).reshape([4, 3, 2])
        >>> z = paddle.tensordot(x, y, axes=([1, 0], [0, 1]))
        >>> print(z)
        Tensor(shape=[5, 2], dtype=float64, place=Place(cpu), stop_gradient=True,
        [[4400., 4730.],
         [4532., 4874.],
         [4664., 5018.],
         [4796., 5162.],
         [4928., 5306.]])

        >>> # Thanks to the support of axes expansion, axes=[[0, 1, 3, 4], [1, 0, 3, 4]] can be abbreviated as axes= [[0, 1, 3, 4], [1, 0]].
        >>> x = paddle.arange(720, dtype=data_type).reshape([2, 3, 4, 5, 6])
        >>> y = paddle.arange(720, dtype=data_type).reshape([3, 2, 4, 5, 6])
        >>> z = paddle.tensordot(x, y, axes=[[0, 1, 3, 4], [1, 0]])
        >>> print(z)
        Tensor(shape=[4, 4], dtype=float64, place=Place(cpu), stop_gradient=True,
        [[23217330., 24915630., 26613930., 28312230.],
         [24915630., 26775930., 28636230., 30496530.],
         [26613930., 28636230., 30658530., 32680830.],
         [28312230., 30496530., 32680830., 34865130.]])
	tensordot)rm   rn   ro   rc   r   r   c                Z   > [        5       (       a  [        U 5      $ [        ST-   S-   5      e)Nz!The 'axes' with type 'Tensor' in zm is not available in static graph mode, please convert its type to int|Tuple|List, or use dynamic graph mode.)r"   r  rY   )ru  r   s    rj   _var_to_listtensordot.<locals>._var_to_list7  s:    #;/TT
 	
r   r   zThe 'axes' in z+ should not be negative, but received axes=r   r%   Nr>   z(The dimensional size for 'x' and 'y' in z+ should match each other, but 'x' has size z in dim z while 'y' has size T)r   )r   r   rR   r   rM   r   rN   rT   rU   rP   
issubdtyperH   integerr   r   rL   r   extendrS   r(   rl   sumr?   r   r   r   matmul)rc   r   r   r`   r^   r  axes_xaxes_y
len_axes_x
len_axes_yshape_xshape_yneed_contracted_dim_xneed_contracted_dim_ycontraction_sizere   dim_xdim_ysxsyperm_xperm_y	shape_outnot_contraction_size_xnot_contraction_size_yrh   r   s                             @rj   r  r    s   B G3KQ[':Q[':fsE46::3C3CDg
 FF	}}T$Z,,qy 	
;D6CD	
y
 qvv}aff-tdH%%%Dr}}T$q']BJJ??F!WF4y1}a&(++%f-&(++%f-&\4<F [#f+
f[)*		 f[)*AGG}d177mWHHaffT:HHaffT:3v;y&)uB7GENe177+33G<A1WGENe177+33G<A8 :?t8E7Rfgifjjrsxryyz{|8 (,e$'+e$EN*%  ( FFI166]$''MM!WZ("gj0"	 
 MM&
MM&166]$''MM!WZ("gj0"	  	
 ((	!12	A 	
 ((	12	A ((1+

i
(CJr   c                   [        5       (       a  [        R                  " U 5      $ [        U SSS/S5        Sn[	        U40 [        5       D6nSU 0nUR                  [        U R                  5      S9nSU0n0 nUR                  X$XvS9  U$ )	a
  Transform a real tensor to a complex tensor.

The data type of the input tensor is 'float32' or 'float64', and the data
type of the returned tensor is 'complex64' or 'complex128', respectively.

The shape of the input tensor is ``(* ,2)``, (``*`` means arbitrary shape), i.e.
the size of the last axis should be 2, which represent the real and imag part
of a complex number. The shape of the returned tensor is ``(*,)``.

The image below demonstrates the case that a real 3D-tensor with shape [2, 3, 2] is transformed into a complex 2D-tensor with shape [2, 3].

.. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/as_complex.png
   :width: 500
   :alt: Illustration of as_complex
   :align: center

Args:
    x (Tensor): The input tensor. Data type is 'float32' or 'float64'.
    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 output. Data type is 'complex64' or 'complex128', with the same precision as the input.

Examples:
    .. code-block:: pycon

        >>> import paddle
        >>> x = paddle.arange(12, dtype=paddle.float32).reshape([2, 3, 2])
        >>> y = paddle.as_complex(x)
        >>> print(y)
        Tensor(shape=[2, 3], dtype=complex64, place=Place(cpu), stop_gradient=True,
        [[(0.00000000+1.00000000j) , (2.00000000+3.00000000j)  , (4.00000000+5.00000000j)  ],
         [(6.00000000+7.00000000j) , (8.00000000+9.00000000j)  , (10.00000000+11.00000000j)]])
rc   rn   ro   
as_complexrA   r>   rC   r   )
r#   r   r  r   r   r\   r]   r'   r?   r_   )rc   r`   r   rg   rI   rh   rJ   rK   s           rj   r  r    s    F   ## C)Y)?NW11q77(1 8 
 #,u 	 	
 
r   c                   [        5       (       a  [        R                  " U 5      $ [        U SSS/S5        Sn[	        U40 [        5       D6nSU 0nUR                  [        U R                  5      S9nSU0nUR                  X$US9  U$ )	a  Transform a complex tensor to a real tensor.

The data type of the input tensor is 'complex64' or 'complex128', and the data
type of the returned tensor is 'float32' or 'float64', respectively.

When the shape of the input tensor is ``(*, )``, (``*`` means arbitrary shape),
the shape of the output tensor is ``(*, 2)``, i.e. the shape of the output is
the shape of the input appended by an extra ``2``.

Args:
    x (Tensor): The input tensor. Data type is 'complex64' or '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 output. Data type is 'float32' or 'float64', with the same precision as the input.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> x = paddle.arange(12, dtype=paddle.float32).reshape([2, 3, 2])
        >>> y = paddle.as_complex(x)
        >>> z = paddle.as_real(y)
        >>> print(z)
        Tensor(shape=[2, 3, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[[0. , 1. ],
         [2. , 3. ],
         [4. , 5. ]],
        [[6. , 7. ],
         [8. , 9. ],
         [10., 11.]]])
rc   r   r   as_realrA   r>   rC   r  )
r#   r   r  r   r   r\   r]   r&   r?   r_   )rc   r`   r   rg   rI   rh   rJ   s          rj   r  r    s    B ~~a   C+|)DiPW11q77(1 8 
 #,ggF
r   c                    [        U S9$ )a  Return a complex tensor that is a view of the input real tensor .

The data type of the input tensor is 'float32' or 'float64', and the data
type of the returned tensor is 'complex64' or 'complex128', respectively.

The shape of the input tensor is ``(* ,2)``, (``*`` means arbitrary shape), i.e.
the size of the last axis should be 2, which represent the real and imag part
of a complex number. The shape of the returned tensor is ``(*,)``.

The complex tensor is a view of the input real tensor, meaning that it shares the same memory with real tensor.

The image below demonstrates the case that a real 3D-tensor with shape [2, 3, 2] is transformed into a complex 2D-tensor with shape [2, 3].

.. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/as_complex.png
   :width: 500
   :alt: Illustration of as_complex
   :align: center

Args:
    input (Tensor): The input tensor. Data type is 'float32' or 'float64'.

Returns:
    Tensor, The output. Data type is 'complex64' or 'complex128', sharing the same memory with input.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> x = paddle.arange(12, dtype=paddle.float32).reshape([2, 3, 2])
        >>> y = paddle.as_complex(x)
        >>> print(y)
        Tensor(shape=[2, 3], dtype=complex64, place=Place(cpu), stop_gradient=True,
        [[1j      , (2+3j)  , (4+5j)  ],
         [(6+7j)  , (8+9j)  , (10+11j)]])
r   )r  rA  s    rj   view_as_complexr    s    J r   c                    [        U S9$ )a  Return a real tensor that is a view of the input complex tensor.

The data type of the input tensor is 'complex64' or 'complex128', and the data
type of the returned tensor is 'float32' or 'float64', respectively.

When the shape of the input tensor is ``(*, )``, (``*`` means arbitrary shape),
the shape of the output tensor is ``(*, 2)``, i.e. the shape of the output is
the shape of the input appended by an extra ``2``.

The real tensor is a view of the input complex tensor, meaning that it shares the same memory with complex tensor.

Args:
    input (Tensor): The input tensor. Data type is 'complex64' or 'complex128'.

Returns:
    Tensor, The output. Data type is 'float32' or 'float64', sharing the same memory with input.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> x = paddle.arange(12, dtype=paddle.float32).reshape([2, 3, 2])
        >>> y = paddle.as_complex(x)
        >>> z = paddle.as_real(y)
        >>> print(z)
        Tensor(shape=[2, 3, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[[0. , 1. ],
         [2. , 3. ],
         [4. , 5. ]],
        [[6. , 7. ],
         [8. , 9. ],
         [10., 11.]]])
r   )r  rA  s    rj   view_as_realr  #  s    D Ur   )output_sizec          
         [        U[        [        R                  R                  45      (       a)  UR
                  (       d  [        R                  " US/5      nUc(  U R                  S:w  a  [        R                  " U 5      n Sn[        5       (       ao  [        U[        [        R                  R                  45      (       a   [        R                  " XX$b  U5      $ S5      $ [        R                  " XX$b  U5      $ S5      $ [        S0 [        5       D6n[        U S/ SQS5        UR!                  U R"                  5      nUR%                  SU [        U[        5      (       a  UOSS	.S
U0U[        U[&        5      (       a  UOSUb  UOSS.S9  U$ )a
  

Returns a new tensor which repeats the ``x`` tensor along dimension ``axis`` using
the entries in ``repeats`` which is a int or a Tensor.

The image illustrates a typical case of the repeat_interleave operation.
Given a tensor ``[[1, 2, 3], [4, 5, 6]]``, with the repeat counts ``repeats = [3, 2, 1]`` and parameter ``axis = 1``, it means that the elements in the 1st column are repeated 3 times, the 2nd column is repeated 2 times, and the 3rd column is repeated 1 time.

The final output is a 2D tensor: ``[[1, 1, 1, 2, 2, 3], [4, 4, 4, 5, 5, 6]]``.

.. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/repeat_interleave.png
    :width: 500
    :alt: legend of repeat_interleave API
    :align: center

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

Args:
    x (Tensor): The input Tensor to be operated. The data of ``x`` can be one of float32, float64, int32, int64.
        alias: ``input``.
    repeats (Tensor|int): The number of repetitions for each element. repeats is broadcasted to fit the shape of the given axis.
    axis (int|None, optional): The dimension in which we manipulate. Default: None, the output tensor is flatten.
        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`.
    output_size (int, optional): Total output size for the given axis (e.g. sum of repeats). If given, it will avoid stream synchronization needed to calculate output shape of the tensor.

Returns:
    Tensor, A Tensor with same data type as ``x``.

Examples:
    .. code-block:: python

        >>> import paddle

        >>> x = paddle.to_tensor([[1, 2, 3], [4, 5, 6]])
        >>> repeats = paddle.to_tensor([3, 2, 1], dtype='int32')

        >>> out = paddle.repeat_interleave(x, repeats, 1)
        >>> print(out)
        Tensor(shape=[2, 6], dtype=int64, place=Place(cpu), stop_gradient=True,
        [[1, 1, 1, 2, 2, 3],
         [4, 4, 4, 5, 5, 6]])

        >>> out = paddle.repeat_interleave(x, 2, 0)
        >>> print(out)
        Tensor(shape=[4, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
        [[1, 2, 3],
         [1, 2, 3],
         [4, 5, 6],
         [4, 5, 6]])

        >>> out = paddle.repeat_interleave(x, 2, None)
        >>> print(out)
        Tensor(shape=[12], dtype=int64, place=Place(cpu), stop_gradient=True,
        [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6])
r%   Nr   r   repeat_interleaverc   r   z,paddle.tensor.manipulation.repeat_interleave)rA   RepeatsTensorrC   )r   Repeatsr  rG   )r  )rL   r   rN   rT   rU   rS   r   r   r<  r#   r   #repeat_interleave_with_tensor_indexr  r   r\   r   r]   r?   r_   rR   )rc   r  r6   r`   r  rg   rh   s          rj   r  r  H  st   J 'Hfjj&6&6788..1#.|66Q;q!Ag&***:*:;<<==D1H+ NP  ''-Dk
 	
JL
 	
 99F	06	 
3
3AGG
<C
 (27H(E(EW4
 ",Wc":":w*5*A;r
   Jr   c                &   [        U[        5      (       a  U/OUn[        U[        5      (       a  U/OUn[        U[        5      (       a  [        U5      n[        U[        5      (       a  [        U5      n[	        U5      [	        U5      :X  d   S5       e[	        U5      [	        [        U5      5      :w  a  [        S5      e[	        U5      [	        [        U5      5      :w  a  [        S5      e[	        U R                  5      n[        [        U5      5      n[        [        U5      5      n[        [        U5      5      n	[        [        XE5      5       H  u  p[        US   [        5      (       d   S5       eUS   S:  a%  US   U* :  d   SU SU S35       eXJ==   U-  ss'   OUS   U:  d   SU SU S35       e[        US	   [        5      (       d   S5       eUS	   S:  a%  US	   U* :  d   SU SU S35       eXZ==   U-  ss'   OUS	   U:  d   SU SU S35       eXJ   XuU
   '   UR                  XJ   5        U	R                  XZ   5        M     [        [	        U5      5       H  n
X   XyU
   '   M     [        5       (       a  [        R                  " X5      nU$ [        U S
/ SQS5        [!        S0 [#        5       D6nUR%                  U R&                  5      nUR%                  U R&                  5      nUR)                  SSU /0U/U/S.SU0S9  U$ )a  
Move the axis of tensor from ``source`` position to ``destination`` position.

Other axis that have not been moved remain their original order.

Args:
    x (Tensor): The input Tensor. It is a N-D Tensor of data types bool, int32, int64, float32, float64, complex64, complex128.
    source(int|tuple|list): ``source`` position of axis that will be moved. Each element must be unique and integer.
    destination(int|tuple|list): ``destination`` position of axis that has been moved. Each element must be unique and integer.
    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:
    Tensor, A new tensor whose axis have been moved.

Examples:
    .. code-block:: pycon

        >>> import paddle

        >>> x = paddle.ones([3, 2, 4])
        >>> outshape = paddle.moveaxis(x, [0, 1], [1, 2]).shape
        >>> print(outshape)
        paddle.Size([4, 3, 2])

        >>> x = paddle.ones([2, 3])
        >>> outshape = paddle.moveaxis(x, 0, 1).shape  # equivalent to paddle.t(x)
        >>> print(outshape)
        paddle.Size([3, 2])
z5'source' must have the same number with 'destination'z(Each element of 'source' must be unique!z-Each element of 'destination' must be unique!r   z)Each element of 'source' must be integer.z#'source' must be in the range of [-r   r   r%   rc   )rl   rm   rn   ro   r@   rq   r   r   moveaxisr   rA   r   r6   rG   )r  )rL   rR   r   rM   r   setr   rS   r   rV   zipremover#   r   r   r   r   r\   r]   r?   r_   )rc   sourcedestinationr`   r  dstr   r   src_dimsdst_dimsre   r6   rh   rg   r   s                  rj   r  r    s(   H !--6(6C%k377;-[C&%  6l+u%%;s8s3x ? 3x3s3x= CDD
3x3s3x= HIIqww<D dDE$K HE$K HS]+$q'3'' 	
7	
' 7Q;7te# 5dV2dV1E# FdNF7T> 5dV2dV1E> $q'3'' 	
7	
' 7Q;7te# 5dV2dV1E# FdNF7T> 5dV2dV1E> vV9 ,< 3x=!$Ka[ " q'
 	 	
  468477@;;AGGD!: EgY74.	 	 	
 
r   c                   [         R                  " U5      (       a!  [        R                  " / X R                  5      nUR                  S:w  a  [        R
                  " US5      n[        5       (       a  [        R                  " XU5      nU$ [        U S/ SQS5        [        USS/S5        [        S
0 [        5       D6nUR                  U R                  S9nUR                  SU UUS.SU0S	9  U$ )a  
Fills elements of self tensor with value where mask is True. The shape of mask must be broadcastable with the shape of the underlying tensor.

The following figure shows an example: consider a 3x3 matrix `x`,where all elements have a value of 1, and a matrix `mask` of the same size, and `value` is 3.

.. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/masked_fill.png
   :width: 700
   :align: center

Args:
    x (Tensor) : The Destination Tensor. Supported data types are float,
        double, int, int64_t,float16 and bfloat16.
    mask (Tensor): The boolean tensor indicate the position to be filled.
        The data type of mask must be bool.
    value (Scalar or 0-D Tensor): The value used to fill the target tensor.
        Supported data types are float, double, int, int64_t,float16 and bfloat16.
    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:
    Tensor, same dimension and dtype with x.

Examples:
    .. code-block:: python

        >>> # doctest: +REQUIRES(env:GPU)
        >>> import paddle
        >>> x = paddle.ones((3, 3), dtype="float32")
        >>> mask = paddle.to_tensor([[True, True, False]])
        >>> print(mask)
        Tensor(shape=[1, 3], dtype=bool, place=Place(gpu:0), stop_gradient=True,
               [[True , True , False]])
        >>> out = paddle.masked_fill(x, mask, 2)
        >>> print(out)
        Tensor(shape=[3, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
               [[2., 2., 1.],
                [2., 2., 1.],
                [2., 2., 1.]])
rl   rc   )rl   rm   r  rn   ro   rp   r@   rq   rw   r  r   r   masked_fillrB  r>   )rc   rB  r   rh   r  )r  )rP   isscalarrN   fullr?   rv   r#   r   r  r   r   r\   r]   r_   )rc   rB  r   r`   rh   rg   s         rj   r  r  1  s    V 
{{5Bww/zzV{{4(  %0
  #	
& 	!H		
 7fh777agg7F
 CL 	 	
 
r   c                    [         R                  " U5      (       a!  [        R                  " / X R                  5      nUR                  S:w  a  [        R
                  " US5      n[        R                  " XU5      n U $ )z
Inplace version of ``masked_fill`` API, the output Tensor will be inplaced with input ``x``.
Please refer to :ref:`api_paddle_masked_fill`.
rl   )rP   r  rN   r  r?   rv   r   masked_fill_)rc   rB  r   r`   s       rj   r  r    sZ     
{{5Bww/zzV{{4(AU+AHr   c                    g r	  r   arrr6   s     rj   r  r    s    69r   c                    g r	  r   r  s     rj   r  r    s    <?r   c                    [        U R                  5      nUS:  a  X:  d   SU SU S35       e U$ X* :  d   SU SU S35       eX-  nU$ )Nr   z"'axis'  must be in the range of [-r   r   )r   rS   )r  r6   r   s      rj   r  r    sq    syy>Dqy{P@baPP{ K u} 	
0ba@	
} 	Kr   c                    US:X  a  USS U SS /nO%X1S-
  :X  a  U SU UUS /nOU SU X#US-    XS-   S /n[         R                  " U5      $ )aJ  
Find the broadcast shape for indices when `arr` has a dynamic shape.

Args:
    arr_shape (Tensor): Shape tensor of arr.
    arr_shape_dim (int): Dimensions of arr.
    indices_shape (Tensor): Shape tensor of indices.
    axis (int): The axis to put 1d slices along.

Returns:
    Tensor: The shape tensor for later broadcasting
r   Nr%   )rN   r8   )	arr_shapearr_shape_dimindices_shaper6   
new_shapess        rj   infer_dynamic_broadcast_shaper    s     qy abM	

 
"	"et

 et*Qhj!


 ==$$r   c                   [        U R                  5      n[        UR                  5      U   X2'   [        U5      n[        [	        U R                  5      5       H%  nU R                  U   UR                  U   :  d  M%    g    U$ r	  )rM   rS   r   r   r   )r  rq  r6   broadcast_shape_listbroadcast_shapere   s         rj   infer_broadcast_shaper    so      		?!%gmm!4T!:01O3syy>"99Q<'--** # r   c           
         [        XX1SSSS9$ )aq  
Scatter the values of the source tensor to the target tensor according to the given indices, and perform a add operation along the designated axis.

Args:
    input (Tensor) : The Input Tensor. Supported data types are bfloat16, float16, float32, float64,
        int32, int64, uint8.
    dim (int) : The axis to scatter 1d slices along.
    index (Tensor) : Indices to scatter along each 1d slice of input. This must match the dimension of input,
         Supported data type are int32 and int64.
    src (Tensor) : The value element(s) to scatter. The data types should be same as input.

Returns:
    Tensor, The indexed element, same dtype with input

Examples:
    .. code-block:: python

        >>> import paddle

        >>> x = paddle.to_tensor([[10, 20, 30], [40, 50, 60]])
        >>> indices = paddle.zeros((2,3)).astype("int32")
        >>> values = paddle.to_tensor([[1, 2, 3],[4, 5, 6]]).astype(x.dtype)
        >>> result = paddle.scatter_add(x, 0, indices, values)
        >>> print(result)
        Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
        [[15, 27, 39],
         [40, 50, 60]])

r=  TFinclude_selfr  r  r5   r   rk  r  s       rj   scatter_addr    s    H cDE r   r  c                  [        U R                  5      [        UR                  5      :w  a  [        S5      e[        X5      nU(       Ga  [	        U R                  5      n[        [        U R                  5      5       HW  nUR                  U   S:X  d  U R                  U   S:X  a  SXV'   M/  [        U R                  U   UR                  U   5      XV'   MY     [	        UR                  5      U   XR'   [        U5      n[        R                  " X5      n[	        U5      n[	        U R                  5      U   XR'   [        U5      n[        R                  " X5      n O[        [        U R                  5      5       Hj  nXb:w  d  M
  U R                  U   S:w  d  M  U R                  U   UR                  U   :  d  MA  [        SU SUR                   SU R                   SU 35      e   [        5       (       a  [        R                  " XX$S9$ [        U S	/ S
QS5        [        USSS/S5        [        S0 [!        5       D6nUR#                  5       n	UR%                  U	5      n
UR'                  SXS.SU0SU
0S9  U
$ )a{  
Take values from the input array by given indices matrix along the designated axis.

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

Args:
    arr (Tensor) : The input Tensor. Supported data types are bfloat16, float16, float32, float64,
        int32, int64, uint8.
        alias: ``input``.
    indices (Tensor) : Indices to take along each 1d slice of arr. This must match the dimension of arr,
        and need to broadcast against arr. Supported data type are int32 and int64.
    axis (int) : The axis to take 1d slices along.
        alias: ``dim``.
    broadcast (bool, optional): whether the indices broadcast.
    out (Tensor, optional): The output Tensor. If set, the output will be written to this Tensor.

Returns:
    Tensor, The indexed element, same dtype with arr.

Examples:
    .. code-block:: python

        >>> import paddle

        >>> x = paddle.to_tensor([[1, 2, 3], [4, 5, 6], [7,8,9]])
        >>> index = paddle.to_tensor([[0]])
        >>> axis = 0
        >>> result = paddle.take_along_axis(x, index, axis)
        >>> print(result)
        Tensor(shape=[1, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
        [[1, 2, 3]])
<`indices` and `arr` must have the same number of dimensions!r   r   r  r  r  r  r  rc   rm   rn   ro   r@   rq   rr   rs   r  rk  r@   rq   )r   r  r  Resultr   )r  )r   rS   r   r  rM   r   r   r   rN   r  r  r#   r   r  r   r   r\   r^   r]   r_   )r  rq  r6   r  rh   r  re   r  rg   r?   r_  s              rj   r  r    sZ   V 399~W]]++J
 	
 S'D#CIIs399~&A}}Q1$		!(9*+$'*-ciilGMM!<L*M$'	 '
 &*'--%8%>" 45%%g?#O4%)#))_T%:" 45!!#7s399~&A	IIaLB&IIaL7==#33"7s:J7==/Yrsvs|s|r}  ~T  UY  TZ  [  ' %%cDBB  	
 	!Ww02C	
 ;&(;""$::5A" 34.v&	 	 	
 r   )r  c          
     :    US:X  a  SnUS:X  a  Sn[        XX1XESS9$ )aC  
Scatter the values of the source tensor to the target tensor according to the given indices, and perform a reduction operation along the designated axis.

Args:
    input (Tensor) : The Input Tensor. Supported data types are bfloat16, float16, float32, float64,
        int32, int64, uint8.
    dim (int) : The axis to scatter 1d slices along.
    index (Tensor) : Indices to scatter along each 1d slice of input. This must match the dimension of input,
         Supported data type are int32 and int64.
    src (Tensor) : The value element(s) to scatter. The data types should be same as input.
    reduce (str): The reduce operation, support 'sum', 'prod', 'mean', 'amin', 'amax'.
    include_self (bool, optional): whether to reduce with the elements of input, default is 'True'.

Returns:
    Tensor, The indexed element, same dtype with input

Examples:
    .. code-block:: python

        >>> import paddle

        >>> x = paddle.to_tensor([[10, 20, 30], [40, 50, 60]])
        >>> indices = paddle.zeros((2,3)).astype("int32")
        >>> values = paddle.to_tensor([[1, 2, 3],[4, 5, 6]]).astype(x.dtype)
        >>> result = paddle.scatter_reduce(x, 0, indices, values, "sum", include_self=True)
        >>> print(result)
        Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
        [[15, 27, 39],
         [40, 50, 60]])

        >>> result = paddle.scatter_reduce(x, 0, indices, values, "prod", include_self=True)
        >>> print(result)
        Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
        [[40 , 200, 540],
         [40 , 50 , 60 ]])

        >>> result = paddle.scatter_reduce(x, 0, indices, values, "mean", include_self=True)
        >>> print(result)
        Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
        [[5 , 9 , 13],
         [40, 50, 60]])

r  r=  r  multiplyFr  r  )r5   r   rk  r  r  r  s         rj   scatter_reducer  {  s4    j c r   c                L	   S n[        U R                  5      [        UR                  5      :w  a  [        S5      e[        X5      nU(       Ga  U" U R                  5      (       d  U" UR                  5      (       a  [        R                  " U 5      n[        R                  " U5      n	[        XR                  X5      n
[        U[        R                  [        R                  R                  [        45      (       d  [        R                  " U5      OUn[        R                  " X5      n[        R                  " X*5      nGO[        XU5      n
[        U[        R                  [        R                  R                  [        45      (       d  [        R                  " U5      OUnU
(       a.  [        R                  " X5      n[        R                  " X*5      nGO[        R                  " X!R                  5      nGO[        U[        R                  [        R                  R                  45      (       a  [        UR                  5      [        UR                  5      :w  a  [        S5      e[        [        U R                  5      5       H  nX:w  a   U R                  U   UR                  U   :  d"  UR                  U   UR                  U   :  d  MJ  [!        SU SUR                   SU R                   SU SUR                   3
5      e   On[        R                  " U5      R#                  U R$                  5      nS	nUR                   H  nX-  nM	     US	:X  a   [        R                  " X!R                  5      nU R                  U   n['        5       (       a(  X:  R)                  5       (       d  [!        S
U SU 35      e[+        5       (       aR  [-        UR$                  5      S;  a!  [/        S[-        UR$                  5       35      e[0        R2                  " XX#XE5      $ [5        U S/ SQS5        [5        USSS/S5        [7        US[8        S5        [;        S0 [=        5       D6nUR?                  5       nURA                  U5      nURC                  SXUS.UUUS.SU0S9  U$ )a  
Put values into the destination array by given indices matrix along the designated axis.

Args:
    arr (Tensor) : The Destination Tensor. Supported data types are bfloat16, float16, float32, float64,
        int32, int64, uint8.
    indices (Tensor) : Indices to put along each 1d slice of arr. This must match the dimension of arr,
        and need to broadcast against arr if broadcast is 'True'. Supported data type are int32 and int64.
    values (scalar|Tensor) : The value element(s) to put. The data types should be same as arr.
    axis (int) : The axis to put 1d slices along.
    reduce (str, optional): The reduce operation, default is 'assign', support 'add', 'assign', 'mul', 'multiply', 'mean', 'amin' and 'amax'.
    include_self (bool, optional): whether to reduce with the elements of arr, default is 'True'.
    broadcast (bool, optional): whether to broadcast indices, default is 'True'.

Returns:
    Tensor, The indexed element, same dtype with arr

Examples:
    .. code-block:: python

        >>> import paddle

        >>> x = paddle.to_tensor([[10, 30, 20], [60, 40, 50]])
        >>> index = paddle.to_tensor([[0]])
        >>> value = 99
        >>> axis = 0
        >>> result = paddle.put_along_axis(x, index, value, axis)
        >>> print(result)
        Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
        [[99, 99, 99],
         [60, 40, 50]])

        >>> index = paddle.zeros((2,2)).astype("int32")
        >>> value=paddle.to_tensor([[1,2],[3,4]]).astype(x.dtype)
        >>> result = paddle.put_along_axis(x, index, value, 0, "add", True, False)
        >>> print(result)
        Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
        [[14, 36, 20],
         [60, 40, 50]])

        >>> result = paddle.put_along_axis(x, index, value, 0, "mul", True, False)
        >>> print(result)
        Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
        [[30 , 240, 20 ],
         [60 , 40 , 50 ]])

        >>> result = paddle.put_along_axis(x, index, value, 0, "mean", True, False)
        >>> print(result)
        Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
        [[4 , 12, 20],
         [60, 40, 50]])

        >>> result = paddle.put_along_axis(x, index, value, 0, "amin", True, False)
        >>> print(result)
        Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
        [[1 , 2 , 20],
         [60, 40, 50]])

        >>> result = paddle.put_along_axis(x, index, value, 0, "amax", True, False)
        >>> print(result)
        Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
        [[10, 30, 20],
         [60, 40, 50]])

        >>> result = paddle.put_along_axis(x, index, value, 0, "add", False, False)
        >>> print(result)
        Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
        [[4 , 6 , 20],
         [60, 40, 50]])

c                &    [        S U  5       5      $ )Nc              3  *   #    U  H	  oS :  v   M     g7fr   Nr   )rX  rS   s     rj   rY  <put_along_axis.<locals>.has_dynamic_shape.<locals>.<genexpr>  s     1&19&s   )r]  )shapess    rj   has_dynamic_shape)put_along_axis.<locals>.has_dynamic_shape  s    1&111r   r  ?`indices` and `values` must have the same number of dimensions!r  r  r  r  $ and to be smaller size than values r%   9one of element of indices is out of bounds for dimension r  r  zFThe data type of indices should be one of ['int32', 'int64'], but got rc   r  r  rk  r@   rq   r  )r   r  rU   )r  ReduceInclude_selfr  r   r  )"r   rS   r   r  rN   r  r   rL   r,   rT   rU   r   rO   r  r  r   r  r  r?   r"   r  r#   r   rY   r   r  r   r   rl   r   r\   r^   r]   r_   )r  rq  valuesr6   r  r  r  r  r  r  r  re   r  r   r  rg   r?   r_  s                     rj   r  r    s6   f2 399~W]]++J
 	
 S'DSYY''+<W]]+K+KS)I"LL1M;88]O
 "V]]FJJ,<,<hG    (   ))'CG((AF3C$GO "V]]FJJ,<,<hG    (    --gG,,VE,,V]]Cfv}}fjj.>.>?@@7==!S%66 U  3syy>*I#))A,q1A"A]]1%Q7&;A3>Nw}}o]vwz  xA  xA  wB  BX  Y]  X^  ^B  CI  CO  CO  BP  Q 	 + %%f-44SYY?FH|| $1},,V]]C		$g&=%B%B%D%DKD6Q\]j\kl  '/AAXYfgngtgtYuXvw  $$&
 	
 	! 	
 	!Ww02B	
 	<7GH::""$::5A! VD  ,
 v& 	 		
 r   c                8   [        U R                  5      [        UR                  5      :w  a  [        S5      e[        X5      nU(       a  [	        XU5      n[        U[        R                  5      (       d  [        R                  " U5      OUnU(       a  [        R                  " X5      n[        R                  " X!R                  5      nGO[        U[        R                  [        R                  R                  45      (       a  [        UR                  5      [        UR                  5      :w  a  [        S5      e[        [        U R                  5      5       H  nX:w  a   U R                  U   UR                  U   :  d"  UR                  U   UR                  U   :  d  MJ  [        SU SUR                   SU R                   SU SUR                   3
5      e   On[        R                  " U5      R                  U R                  5      nSn	UR                   H  n
X-  n	M	     U	S:X  a   [        R                  " X!R                  5      nU R                  U   nX:  R!                  5       (       d  [        S	U S
U 35      e["        R$                  " XX#XE5      $ )z
Inplace version of ``put_along_axis`` API, the output Tensor will be inplaced with input ``arr``.
Please refer to :ref:`api_paddle_put_along_axis`.
r  r  r  r  r  r  r  r%   r  r  )r   rS   r   r  r  rL   rN   r,   rO   r  rT   rU   r   r  r  r?   r  r   r  )r  rq  r  r6   r  r  r  r  re   r  r   r  s               rj   r  r  v  s^     399~W]]++J
 	
 S'D/dC ffmm44 V$ 	
 ))'CG$$V]];fv}}fjj.>.>?@@7==!S%66 U  3syy>*I#))A,q1A"A]]1%Q7&;A3>Nw}}o]vwz  xA  xA  wB  BX  Y]  X^  ^B  CI  CO  CO  BP  Q 	 + %%f-44SYY?FH|| $1},,V]]C		$',,..KD6Q\]j\kl  !!fF r   c           
         [        XX1SSSS9$ )z
Inplace version of ``scatter_add`` API, the output Tensor will be inplaced with input ``input``.
Please refer to :ref:`api_paddle_scatter_add`.
r=  TFr  )r  r  s       rj   scatter_add_r    s     cDE r   c               X   [        5       (       a"  US:w  a  X4-  OUn[        R                  " XXrUS9$ [        S0 [	        5       D6n[        U S/ SQS5        [        USSS	/S5        [        US
/ SQS5        UR                  U R                  5      nUR                  SU UUS.SU0SU0S9  U$ )a  
Adds the elements of the input tensor with value tensor by selecting the indices in the order given in index.

Args:
    x (Tensor) : The Destination Tensor. Supported data types are int32, int64, float16, float32, float64.
        alias: ``input``.
    index (Tensor): The 1-D Tensor containing the indices to index.
        The data type of ``index`` must be int32 or int64.
    axis (int): The dimension in which we index.
        alias: ``dim``.
    value (Tensor): The tensor used to add the elements along the target axis.
        alias: ``source``.
    alpha (Number, optional): Scaling factor for value. Default: 1.
    out (Tensor, optional): The output tensor. Default: None.
    name(str|None, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.

Returns:
    Tensor, same dimension and dtype with x.

Examples:
    .. code-block:: python

        >>> # doctest: +REQUIRES(env:GPU)
        >>> import paddle
        >>> paddle.device.set_device('gpu')

        >>> input_tensor = paddle.to_tensor(paddle.ones((3, 3)), dtype="float32")
        >>> index = paddle.to_tensor([0, 2], dtype="int32")
        >>> value = paddle.to_tensor([[1, 1, 1], [1, 1, 1]], dtype="float32")
        >>> outplace_res = paddle.index_add(input_tensor, index, 0, value)
        >>> print(outplace_res)
        Tensor(shape=[3, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
        [[2., 2., 2.],
         [1., 1., 1.],
         [2., 2., 2.]])
r%   r  	index_addrc   rF  z$paddle.tensor.manipulation.index_addrk  r@   rq   	add_value)rA   r  AddValuerC   r6   rG   )r  )	r#   r   r  r   r\   r   r]   r?   r_   )	rc   rk  r6   r   alphar`   rh   scaled_valuerg   s	            rj   r  r    s    ^ (-
u},#FF11F	E.	 	'.	 E.	 
3
3AGG
<C


 tn  	 Jr   c                H    US:w  a  X4-  OUn[         R                  " XXb5      $ )z
Inplace version of ``index_add`` API, the output Tensor will be inplaced with input ``x``.
Please refer to :ref:`api_paddle_index_add`.
r%   )r   
index_add_)rc   rk  r6   r   r  r`   r  s          rj   r  r    s&     %*QJ5=ELQ|::r   rd   )rc   r6   rS   c                B   [        X5      n[        U[        [        45      (       aA  [        U R                  SU 5      [        U5      -   [        U R                  US-   S 5      -   nO[        U[
        [        R                  R                  45      (       a`  [        R                  " [        R                  " U 5      SU [        R                  " US5      [        R                  " U 5      US-   S /5      nO[        S[        U5       35      eU R                  U5      n U $ )a!	  
Expand a certain dimension of the input x Tensor into a desired shape.

The figure below shows the shape of a [2, 6] Tensor after applying ``unflatten(X, axis=1, shape=(2, 3))``, with data ranging from 0 to 11 in sequence.

.. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/unflatten.png
   :width: 500
   :alt: Illustration of unflatten
   :align: center

.. note::
    Alias Support: The parameter name ``input`` can be used as an alias for ``x``.
    Alias Support: The parameter name ``dim`` can be used as an alias for ``axis``.
    Alias Support: The parameter name ``sizes`` can be used as an alias for ``shape``.

Args:
    x (Tensor) : An N-D Tensor. The data type is float16, float32, float64, int16, int32, int64, bool, uint16.
        Alias: ``input``.
    axis (int): :attr:`axis` to be unflattened, specified as an index into `x.shape`.
        Alias: ``dim``.
    shape (list|tuple|Tensor): Unflatten :attr:`shape` on the specified :attr:`axis`. At most one dimension of the target :attr:`shape` can be -1.
        If the input :attr:`shape` does not contain -1 , the product of all elements in ``shape`` should be equal to ``x.shape[axis]``.
        The data type is `int` . If :attr:`shape` is a list or tuple, the elements of it should be integers or Tensors with shape [].
        If :attr:`shape` is an Tensor, it should be an 1-D Tensor.
        Alias: ``sizes``.
    name(str|None, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.

Returns:
    Tensor, return the unflatten tensor of :attr:`x`.

Examples:
    .. code-block:: python

        >>> import paddle

        >>> x = paddle.randn(shape=[4, 6, 8])
        >>> shape = [2, 3]
        >>> axis = 1
        >>> res = paddle.unflatten(x, axis, shape)
        >>> print(res.shape)
        paddle.Size([4, 2, 3, 8])

        >>> x = paddle.randn(shape=[4, 6, 8])
        >>> shape = (-1, 2)
        >>> axis = -1
        >>> res = paddle.unflatten(x, axis, shape)
        >>> print(res.shape)
        paddle.Size([4, 6, 4, 2])

        >>> x = paddle.randn(shape=[4, 6, 8])
        >>> shape = paddle.to_tensor([2, 2])
        >>> axis = 0
        >>> res = paddle.unflatten(x, axis, shape)
        >>> print(res.shape)
        paddle.Size([2, 2, 6, 8])
Nr%   rq   zIThe data type of x should be one of ['List', 'Tuple', 'Tensor'], but got )r  rL   rM   r   rS   r   rN   rT   rU   r8   rv   rY   rH   r   )rc   r6   rS   r`   r   s        rj   	unflattenr  +  s    | Q%D%$''$ 4;.aggdQhj6I1JJ 	 
EHfjj&6&67	8	8MMQ&E7+Qq
+
	 WX\]bXcWde
 	
 	
		)AHr   c                0    [         R                  " XX#5      $ )ag  
View x with specified shape, stride and offset.

Note that the output Tensor will share data with origin Tensor and doesn't
have a Tensor copy in ``dygraph`` mode.

The following image illustrates an example: transforming an input Tensor with shape [2,4,6] into a Tensor with ``shape [8,6]`` and ``stride [6,1]``.


.. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/as_strided.png
     :alt: Legend

Args:
    x (Tensor): An N-D Tensor. The data type is ``float32``, ``float64``, ``int32``, ``int64`` or ``bool``
    shape (list|tuple): Define the target shape. Each element of it should be integer.
    stride (list|tuple): Define the target stride. Each element of it should be integer.
    offset (int, optional): Define the target Tensor's offset from x's holder. Default: 0.
    name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.

Returns:
    Tensor, A as_strided Tensor with the same data type as ``x``.

Examples:
    .. code-block:: pycon

        >>> import paddle
        >>> paddle.base.set_flags({"FLAGS_use_stride_kernel": True})

        >>> x = paddle.rand([2, 4, 6], dtype="float32")

        >>> out = paddle.as_strided(x, [8, 6], [6, 1])
        >>> print(out.shape)
        paddle.Size([8, 6])
        >>> # the stride is [6, 1].
)r   r   )rc   rS   r   r   r`   s        rj   r   r     s    V Qv66r   c                <   [        U[        [        45      (       a  [        R                  " X5      $ [        U[
        R                  R                  [
        R                  45      (       d  [        U5      nU R                  U:X  a  U $ [        R                  " X5      $ )a	  
View x with specified shape or dtype.

Note that the output Tensor will share data with origin Tensor and doesn't
have a Tensor copy in ``dygraph`` mode.

.. note::
    Alias Support: The parameter name ``size`` and ``dtype`` can be used as an alias for ``shape_or_dtype``.
    ``shape_or_dtype`` can be a variable number of arguments.
    For example:
        ``tensor_x.view(dtype=paddle.float32)``
        ``tensor_x.view(size=[-1, 1, 3])``
        ``tensor_x.view(-1, 1, 3)``

Args:
    x (Tensor): An N-D Tensor. The data type is ``float32``, ``float64``, ``int32``, ``int64`` or ``bool``
    shape_or_dtype (list|tuple|np.dtype|str|VarType|variable number of arguments): Define the target shape or dtype. If list or tuple, shape_or_dtype represents shape, each element of it should be integer. If np.dtype or str or VarType, shape_or_dtype represents dtype, it can be bool, float16, float32, float64, int8, int32, int64, uint8.
        ``shape_or_dtype`` can be a variable number of arguments.
        alias: ``size`` or ``dtype``.
    name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.

Returns:
    Tensor, A viewed Tensor with the same data as ``x``.

Examples:
    .. code-block:: pycon

        >>> import paddle
        >>> paddle.base.set_flags({"FLAGS_use_stride_kernel": True})

        >>> x = paddle.rand([2, 4, 6], dtype="float32")

        >>> out = paddle.view(x, [8, 6])
        >>> print(out.shape)
        paddle.Size([8, 6])

        >>> import paddle
        >>> paddle.base.set_flags({"FLAGS_use_stride_kernel": True})

        >>> x = paddle.rand([2, 4, 6], dtype="float32")

        >>> out = paddle.view(x, "uint8")
        >>> print(out.shape)
        paddle.Size([2, 4, 24])

        >>> import paddle
        >>> paddle.base.set_flags({"FLAGS_use_stride_kernel": True})

        >>> x = paddle.rand([2, 4, 6], dtype="float32")

        >>> out = paddle.view(x, [8, -1])
        >>> print(out.shape)
        paddle.Size([8, 6])

        >>> import paddle
        >>> paddle.base.set_flags({"FLAGS_use_stride_kernel": True})

        >>> x = paddle.rand([2, 4, 6], dtype="float32")

        >>> out = paddle.view(x, paddle.uint8)
        >>> print(out.shape)
        paddle.Size([2, 4, 24])

)rL   rM   r   r   
view_shaper    r|   r}   r~   r   r?   
view_dtype)rc   shape_or_dtyper`   s      rj   viewr    sz    N .4-00  33T\\114==A
 
 8GN77n$H  33r   c                B    [         R                  " XR                  5      $ )aS  
View x with other's shape.

Note that the output Tensor will share data with origin Tensor and doesn't
have a Tensor copy in ``dygraph`` mode.

The following figure shows a view_as operation - a three-dimensional tensor with a shape of [2, 4, 6]
is transformed into a two-dimensional tensor with a shape of [8, 6] through the view_as operation.
We can clearly see the corresponding relationship between the elements before and after the transformation.

.. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/view_as.png
    :width: 800
    :alt: legend of view_as API
    :align: center

Args:
    x (Tensor): An N-D Tensor. The data type is ``float32``, ``float64``, ``int32``, ``int64`` or ``bool``
    other (Tensor): The result tensor has the same size as other.
    name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.

Returns:
    Tensor, A viewed Tensor with the same shape as ``other``.

Examples:
    .. code-block:: pycon

        >>> import paddle
        >>> paddle.base.set_flags({"FLAGS_use_stride_kernel": True})

        >>> x = paddle.rand([2, 4, 6], dtype="float32")
        >>> y = paddle.rand([8, 6], dtype="float32")

        >>> out = paddle.view_as(x, y)
        >>> print(out.shape)
        paddle.Size([8, 6])
)r   r  rS   )rc   otherr`   s      rj   view_asr     s    L Q,,r   	dimensionc                0    [         R                  " XX#5      $ )a]  
View x with specified shape, stride and offset, which contains all slices of size from x in the dimension axis.

Note that the output Tensor will share data with origin Tensor and doesn't
have a Tensor copy in ``dygraph`` mode.

Args:
    x (Tensor): An N-D Tensor. The data type is ``float32``, ``float64``, ``int32``, ``int64`` or ``bool``
    axis (int): The axis along which the input is unfolded. Alias: ``dimension``.
    size (int): The size of each slice that is unfolded.
    step (int): The step between each slice.
    name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.

Returns:
    Tensor, A unfold Tensor with the same data type as ``x``.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> paddle.base.set_flags({"FLAGS_use_stride_kernel": True})

        >>> x = paddle.arange(9, dtype="float64")

        >>> out = paddle.unfold(x, 0, 2, 4)
        >>> print(out)
        Tensor(shape=[2, 2], dtype=float64, place=Place(cpu), stop_gradient=True,
        [[0., 1.],
         [4., 5.]])
)r   tensor_unfold)rc   r6   r  stepr`   s        rj   unfoldr  )  s    F 44r   )r   r   r   r   r   r  zdict[str, Callable[..., Any]]	__METHODSc                   [        U[        [        R                  R                  45      (       d  [        S5      e[        U[        5      (       d  [        R                  " X0R                  S9nO$[        UR                  5      S:  a  [        S5      e[        U R                  5      n[        U[        5      (       a  X%S-
  :  d  X%* :  a  [        S5      eUS:  a  X%-   n[        [        [        U R                  5      5      5      nX&S'   SXb'   U(       ag  [        5       (       a  UR                  S:X  a  U $ [        R                  " X5        [        R                   " X4U5        [        R                  " X5        U $ [        5       (       a   UR                  S:X  a  U R#                  5       $ [        R$                  " X5      n[        R&                  " Xq4U5      n[        R$                  " Xv5      nU$ )Nzindex must be Tensorr>   r   z"value must be scalar or 0-D tensorr%   z8The axis should be int, and in range [-rank(x), rank(x)))rL   r   rN   rT   rU   r   rO   r?   r   rS   rR   rM   r   r"   r  
transpose_r
   r  r   r	   )rc   rk  r6   r   r   r?  r   rh   s           rj   _index_fill_implr  \  s~    eh

(8(89::/00eX&&  gg6u{{aABBLEtS!!tai'7D6MF
 	
 ax|c!''l#$DGDJqH!"!Xu-!"q779q'sHe4s)
r   c                    [        XX#S5      $ )a  
Fill the elements of the input tensor with value by the specific axis and index.

As shown below, a ``[3, 3]`` 2D tensor is updated via the index_fill operation. With ``axis=0``, ``index=[0, 2]`` and ``value=-1``, the 1st and 3rd row elements become ``-1``. The resulting tensor, still [3, 3], has updated values.

.. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/index_fill.png
   :width: 500
   :alt: Illustration of Case 2
   :align: center

Args:
    x (Tensor) : The Destination Tensor. Supported data types are int32, int64, float16, float32, float64.
    index (Tensor): The 1-D Tensor containing the indices to index.
        The data type of ``index`` must be int32 or int64.
    axis (int): The dimension along which to index.
    value (int|float): The tensor used to fill with.
    name(str|None, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.

Returns:
    Tensor, same dimension and dtype with x.


Examples:
    .. code-block:: python

        >>> import paddle
        >>> input_tensor = paddle.to_tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype='int64')
        >>> index = paddle.to_tensor([0, 2], dtype="int32")
        >>> value = -1
        >>> res = paddle.index_fill(input_tensor, index, 0, value)
        >>> print(input_tensor)
        Tensor(shape=[3, 3], dtype=int64, place=Place(gpu:0), stop_gradient=True,
               [[1, 2, 3],
                [4, 5, 6],
                [7, 8, 9]])
        >>> print(res)
        Tensor(shape=[3, 3], dtype=int64, place=Place(gpu:0), stop_gradient=True,
               [[-1, -1, -1],
                [ 4,  5,  6],
                [-1, -1, -1]])

Fr  rc   rk  r6   r   r`   s        rj   
index_fillr    s    Z Ad599r   c                    [        XX#S5      $ )z
Inplace version of ``index_fill`` API, the output Tensor will be inplaced with input ``x``.
Please refer to :ref:`api_paddle_index_fill`.
Tr  r  s        rj   index_fill_r    s     Ad488r   c                    [        XX#XE5      $ )aB  
Embed the values of Tensor ``y`` into Tensor ``x`` along the diagonal elements
of Tensor ``x``, with respect to ``axis1`` and ``axis2``.

This function returns a tensor with fresh storage.

The argument ``offset`` controls which diagonal to consider:

- If ``offset`` = 0, it is the main diagonal.
- If ``offset`` > 0, it is above the main diagonal.
- If ``offset`` < 0, it is below the main diagonal.

Note:
    ``y`` should have the same shape as :ref:`paddle.diagonal <api_paddle_diagonal>`.

The image below demonstrates the example: A 2D tensor with a shape of [2, 3] is ``diagonal_scatter`` along its main diagonal (``offset = 0``)  within ``axis1 = 0`` and ``axis2 = 1`` using a 1D tensor filled with ones.

.. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/diagonal_scatter.png
   :width: 500
   :alt: legend of diagonal_scatter API

Args:
    x (Tensor): ``x`` is the original Tensor. Must be at least 2-dimensional.
    y (Tensor): ``y`` is the Tensor to embed into ``x``
    offset (int, optional): which diagonal to consider. Default: 0 (main diagonal).
    axis1 (int, optional): first axis with respect to which to take diagonal. Default: 0.
    axis2 (int, optional): second axis with respect to which to take diagonal. Default: 1.
    name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.

Returns:
    Tensor, Tensor with diagonal embedded with ``y``.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> x = paddle.arange(6.0).reshape((2, 3))
        >>> y = paddle.ones((2,))
        >>> out = x.diagonal_scatter(y)
        >>> print(out)
        Tensor(shape=[2, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
               [[1., 1., 2.],
                [3., 1., 5.]])

r   )rc   r   r   axis1axis2r`   s         rj   diagonal_scatterr    s    j  fUAAr   c           
     p   U R                   nUR                   n[        U[        5      (       d  [        U5      nUS:  a  X5U   -  nUS:  a  U[        U5      -  nXR	 [        U5      [        U5      :w  a&  [	        S[        U5      -   S-   [        U5      -   5      e[        [        U5      5       H3  nXW   Xg   :w  d  M  [	        S[        U5      -   S-   [        U5      -   5      e   SSKJn  U/n	US-   /n
S/nU/n/ nU/nSU 0nUU	U
UUUS.nU R                  nUUS	'   UR                  U5      nXS
'   [        5       (       a  [        R                  " U UU	U
UUUU5      $ [        S0 [        5       D6nUR!                  U R                  S9nU" 5       R#                  5       nUR%                  SUSU0USS0S9  U$ )a  
Embeds the values of the values tensor into x at the given index of axis.

Args:
    x (Tensor) : The Destination Tensor. Supported data types are `bool`, `float16`, `float32`, `float64`, `uint8`, `int8`, `int16`, `int32`, `int64`, `bfloat16`, `complex64`, `complex128`.
    values (Tensor) : The tensor to embed into x. Supported data types are `bool`, `float16`, `float32`, `float64`, `uint8`, `int8`, `int16`, `int32`, `int64`, `bfloat16`, `complex64`, `complex128`.
    axis (int) : the dimension to insert the slice into.
    index (int) : the index to select with.
    name (str|None, optional): Name for the operation (optional, default is None).

Returns:
    Tensor, same dtype and shape with x

Examples:
    .. code-block:: python

        >>> import paddle

        >>> x = paddle.zeros((2,3,4)).astype("float32")
        >>> values = paddle.ones((2,4)).astype("float32")
        >>> res = paddle.select_scatter(x,values,1,1)
        >>> print(res)
        Tensor(shape=[2, 3, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
               [[[0., 0., 0., 0.],
                 [1., 1., 1., 1.],
                 [0., 0., 0., 0.]],
                [[0., 0., 0., 0.],
                 [1., 1., 1., 1.],
                 [0., 0., 0., 0.]]])

r   zEexpected values to have a size equal to the slice of x. value size = z slice size = r   )r   r%   r   )r   r   r   stepsdecrease_axes	none_axesr?   ValueTensorr>   	set_valuerC   rH   rI   rJ   rK   inplace_map)select_scatter)rS   rL   rM   r   r  rW   r   base.frameworkr   r?   r  r#   r   set_value_with_tensorr   r\   r]   current_blockr_   )rc   r  r6   rk  r`   r   value_shapere   r   r   r   r  r   r  r  rI   rK   r?   rg   r  	cur_blocks                        rj   r  r    s   D ggG,,Kgt$$w-qyaxG
7|s;''S+ 'l
 	
 3w< :'Wk"#"# g,  ! 6WFAI;DCE6DIFMq\F&E GGEE'N]]5!F"=++	
 		
 :::::I(*88:	FO %( 	 	
 r   c           
     t   / n/ nU R                   n	UR                  U	5      n[        5       (       a  [        R                  " U UUUUUUU5      $ UUUUUUU	S.n
U US.n[        S0 [        5       D6nUR                  U R                   S9n[        5       R                  5       nUR                  SUSU0U
SS0S9  U$ )	a	  
Embeds the `value` tensor into `x` along multiple axes. Returns a new tensor instead of a view.
The size of `axes` must be equal to `starts` , `ends` and `strides`.

Args:
    x (Tensor) : The input Tensor. Supported data types are `bool`, `float16`, `float32`, `float64`, `uint8`, `int8`, `int16`, `int32`, `int64`, `bfloat16`, `complex64`, `complex128`.
    value (Tensor) : The tensor to embed into x. Supported data types are `bool`, `float16`, `float32`, `float64`, `uint8`, `int8`, `int16`, `int32`, `int64`, `bfloat16`, `complex64`, `complex128`.
    axes (list|tuple) : the dimensions to insert the value.
    starts (list|tuple) : the start indices of where to insert.
    ends (list|tuple) : the stop indices of where to insert.
    strides (list|tuple) : the steps for each insert.
    name (str|None, optional): Name for the operation (optional, default is None).

Returns:
    Tensor, same dtype and shape with x

Examples:
    .. code-block:: python

        >>> import paddle

        >>> x = paddle.zeros((3, 9))
        >>> value = paddle.ones((3, 2))
        >>> res = paddle.slice_scatter(x, value, axes=[1], starts=[2], ends=[6], strides=[2])
        >>> print(res)
        Tensor(shape=[3, 9], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[0., 0., 1., 0., 1., 0., 0., 0., 0.],
         [0., 0., 1., 0., 1., 0., 0., 0., 0.],
         [0., 0., 1., 0., 1., 0., 0., 0., 0.]])

        >>> # broadcast `value` got the same result
        >>> x = paddle.zeros((3, 9))
        >>> value = paddle.ones((3, 1))
        >>> res = paddle.slice_scatter(x, value, axes=[1], starts=[2], ends=[6], strides=[2])
        >>> print(res)
        Tensor(shape=[3, 9], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[0., 0., 1., 0., 1., 0., 0., 0., 0.],
         [0., 0., 1., 0., 1., 0., 0., 0., 0.],
         [0., 0., 1., 0., 1., 0., 0., 0., 0.]])

        >>> # broadcast `value` along multiple axes
        >>> x = paddle.zeros((3, 3, 5))
        >>> value = paddle.ones((1, 3, 1))
        >>> res = paddle.slice_scatter(x, value, axes=[0, 2], starts=[1, 0], ends=[3, 4], strides=[1, 2])
        >>> print(res)
        Tensor(shape=[3, 3, 5], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[[0., 0., 0., 0., 0.],
          [0., 0., 0., 0., 0.],
          [0., 0., 0., 0., 0.]],
         [[1., 0., 1., 0., 0.],
          [1., 0., 1., 0., 0.],
          [1., 0., 1., 0., 0.]],
         [[1., 0., 1., 0., 0.],
          [1., 0., 1., 0., 0.],
          [1., 0., 1., 0., 0.]]])

)r   r   r   r  r  r  r?   )r   r  r>   r  rC   r   r  )slice_scatter)r?   r  r#   r   r  r   r\   r]   r   r  r_   )rc   r   r   r   r   r   r`   r  r  r?   rK   rI   rg   r  r  s                  rj   r  r  d   s    D IMGGELLE++	
 		
 *"
  

 99:::I(*88:	FO %( 	 	
 r   c                    S nS nU  Vs/ s H
  oC" U5      PM     nn[        U5       VVs/ s H!  u  pd[        R                  " U" XVU5      SS9PM#     nnn[        R                  " USS9$ s  snf s  snnf )aP  
Create a block diagonal matrix from provided tensors.

Args:
    inputs (list|tuple): ``inputs`` is a Tensor list or Tensor tuple, one or more tensors with 0, 1, or 2 dimensions. The data type: ``bool``, ``float16``, ``float32``, ``float64``, ``uint8``, ``int8``, ``int16``, ``int32``, ``int64``, ``bfloat16``, ``complex64``, ``complex128``.
    name (str|None, optional): Name for the operation (optional, default is None).

Returns:
    Tensor, A ``Tensor``. The data type is same as ``inputs``.

Examples:
    .. code-block:: python

        >>> import paddle

        >>> A = paddle.to_tensor([[4], [3], [2]])
        >>> B = paddle.to_tensor([7, 6, 5])
        >>> C = paddle.to_tensor(1)
        >>> D = paddle.to_tensor([[5, 4, 3], [2, 1, 0]])
        >>> E = paddle.to_tensor([[8, 7], [7, 8]])
        >>> out = paddle.block_diag([A, B, C, D, E])
        >>> print(out)
        Tensor(shape=[9, 10], dtype=int64, place=Place(gpu:0), stop_gradient=True,
            [[4, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [3, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [2, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 7, 6, 5, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 5, 4, 3, 0, 0],
            [0, 0, 0, 0, 0, 2, 1, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 8, 7],
            [0, 0, 0, 0, 0, 0, 0, 0, 7, 8]])
c           	         [        U 5       VVs/ s HG  u  p4X1:X  a  UO:[        R                  " UR                  S   UR                  S   /UR                  S9PMI     snn$ s  snnf )Nr   r%   r>   )rV   rN   r(   rS   r?   )arysre   ar   arys        rj   to_col_block block_diag.<locals>.to_col_block   sg     &dO
 , 8 \\399Q<"<AGGLM ,
 	
 
s   AA!c                    U R                   S:X  a  U R                  SS9R                  SS9$ U R                   S:X  a  U R                  SS9$ U R                   S:X  a  U $ [        SU R                    35      e)Nr   r:   r%   r   zYFor 'block_diag', the dimension of each elements in 'inputs' must be 0, 1, or 2, but got )r   r  r   )r!  s    rj   to_2dblock_diag.<locals>.to_2d!  sv    88q===a=(222::88q===a=((88q=Jgxxj
 	
r   r   r:   r%   )rV   rN   r8   )rI   r`   r"  r%  r!  r  r   matrixs           rj   
block_diagr(     s~    F


 #))&3E#J&D) "$'HC 	l4c2;'   ==a(( *s
   A,(A1)r%   FN)
r5   zTensor | list[Tensor]r6   rR   rF   rl   r`   
str | Nonereturntuple[Tensor, Tensor])rc   r,   r?   r-   r*  r,   )
r5   r,   r   Sequence[int | Tensor]r   Sequence[int | Tensor] | Tensorr   r-  r*  r,   )
r5   r,   r   rR   r   int | Tensorr   rR   r*  r,   r	  )rc   r,   r   Sequence[int]r`   r)  r*  r,   r  )rc   r,   r6   rR   r   
int | Noner*  list[Tensor])r   )r5   r,   r   rR   r   rR   r   rR   r   rR   r*  r,   )NNN)
rc   r,   rS   zShapeLike | Noner   zSequence[int] | Tensor | Noner`   r)  r*  r,   )rc   r,   r   r   r*  r,   )rc   r,   r*  r,   )r   FN)rc   r,   r   r   r   rR   r   rl   r`   r)  r*  r,   )r   r   r%   F)rc   r,   r   r,   r   rR   r   rR   r   rR   r   rl   r*  r,   )r   r   r%   N)rc   r,   r   r,   r   rR   r   rR   r   rR   r`   r)  r*  r,   )rc   r,   r*  z!NestedList[int | float | complex])
rc   Sequence[Tensor]r6   r.  r`   r)  rh   Tensor | Noner*  r,   )r5   r2  r`   r)  r*  r1  )rc   r,   r6   zSequence[int] | intr`   r)  r*  r,   )
rc   r,   r/  rR   r   r/  r`   r)  r*  r,   )r   r   N)
rc   r,   r5  rR   r6  rR   r`   r)  r*  r,   )r5   r,   r*  r,   )
rc   r2  r6   rR   r`   r)  rh   r3  r*  r,   )rc   r2  r`   r)  r*  r,   )
rc   r,   ra  int | Sequence[int]r6   r.  r`   r)  r*  r1  )
rc   r,   r~  r4  r6   r.  r`   r)  r*  r1  )rc   r,   r~  r4  r`   r)  r*  r1  )NN)rc   r,   r6   zint | Sequence[int] | Noner`   r)  r*  r,   )FFNrq   N)rc   r,   r  rl   r  rl   r6   r0  r?   r-   r`   r)  r*  tuple[Tensor, Tensor, Tensor]).......)rc   r,   r  Literal[True]r  r6  r  r6  r6   r0  r?   r-   r  rl   r`   r)  r*  z%tuple[Tensor, Tensor, Tensor, Tensor])rc   r,   r  Literal[False]r  r6  r  r6  r6   r0  r?   r-   r  rl   r`   r)  r*  r5  )rc   r,   r  r6  r  r7  r  r6  r6   r0  r?   r-   r  rl   r`   r)  r*  r5  )rc   r,   r  r6  r  r6  r  r7  r6   r0  r?   r-   r  rl   r`   r)  r*  r5  )rc   r,   r  r7  r  r7  r  r6  r6   r0  r?   r-   r  rl   r`   r)  r*  r+  )rc   r,   r  r7  r  r6  r  r7  r6   r0  r?   r-   r  rl   r`   r)  r*  r+  )rc   r,   r  r6  r  r7  r  r7  r6   r0  r?   r-   r  rl   r`   r)  r*  r+  )rc   r,   r  r7  r  r7  r  r7  r6   r0  r?   r-   r  rl   r`   r)  r*  r,   )FFF....)rc   r,   r  rl   r  rl   r  rl   r6   r0  r?   r-   r  rl   r`   r)  r*  zTensor | tuple[Tensor, ...])FFFNrq   TN)rc   r,   r6   z%int | Sequence[Tensor | int] | Tensorr`   r)  r*  r,   )rc   r,   r6   int | Sequence[int] | Tensorr`   r)  r*  r,   )
r5   r,   r   rR   rk  r,   rh   r3  r*  r,   )rc   r,   rk  r,   r6   zTensor | int | Noner`   r)  rh   r3  r*  r,   )r  r   r  r   r*  r,   )r   )r5   r,   r6   rR   r*  r1  )r5   r,   r   rR   rk  r,   r  r3  r  r)  r   r3  r*  r,   )TN)rc   r,   rk  r,   r  r,   r  rl   r`   r)  r*  r,   )TNN)rc   r,   rk  r,   r  r,   r  rl   r`   r)  rh   r3  r*  r,   )NNNN)r5   r,   r   rR   rk  r,   r  r3  r  r)  rh   r3  r   r3  r*  r,   )
rc   r,   rk  r,   r  r,   r`   r)  r*  r,   )
rk  r,   r  r,   rS   r1   r`   r)  r*  r,   )
rc   r,   r  rR   r6   r.  r`   r)  r*  r1  )rc   r,   r  zTensorOrTensors | Sequence[int]r`   r)  r*  r,   )r5   r,   r  r8  r*  r,   )rc   r,   rS   r1   r`   r)  r*  r,   )
rc   r,   rB  r,   r   r,   r`   r)  r*  r,   ).)rI   r,   r`   r)  r*  r,   )rI   r,   r`   r)  r*  r1  )rc   r,   rk  r,   r`   r)  r*  r,   )rc   r,   r   r,  r   r-  r   r-  r   r-  r`   r)  r*  r,   )r   N)
rc   r,   r   r,   r   z"int | NestedSequence[int] | Tensorr`   r)  r*  r,   )rc   r,   r`   r)  r*  r,   )rc   r,   r  r.  r6   r0  r`   r)  r  r0  r*  r,   )
rc   r,   r  r4  r  r4  r`   r)  r*  r,   )rB  r,   r   r0   r`   r)  r*  r,   )r  r,   r6   rR   r*  rR   )r  r,   r6   r,   r*  r,   )
r  r,   r  rR   r  r,   r6   rR   r*  r,   )r  r,   rq  r,   r6   r.  r*  ztuple[int] | None)
r5   r,   r   rR   rk  r,   r  r,   r*  r,   )T)r  r,   rq  r,   r6   rR   r  rl   rh   r3  r*  r,   )r5   r,   r   rR   rk  r,   r  r,   r  z.Literal['sum', 'prod', 'mean', 'amin', 'amax']r  rl   r*  r,   )r  TT)r  r,   rq  r,   r  float | Tensorr6   rR   r  CLiteral['assign', 'add', 'mul', 'multiply', 'mean', 'amin', 'amax']r  rl   r  rl   r*  r,   )r  r,   rq  r,   r  r9  r6   rR   r  r:  r  rl   r  rl   )r%   N)rc   r,   rk  r,   r6   rR   r   r,   r  r+   r`   r)  rh   r3  r*  r,   )rc   r,   rk  r,   r6   rR   r   r,   r  rR   r`   r)  r*  r,   )
rc   r,   r6   rR   rS   r1   r`   r)  r*  r,   )rc   r,   rS   r/  r   r/  r   rR   r`   r)  r*  r,   )rc   r,   r  zSequence[int] | DTypeLiker`   r)  r*  r,   )rc   r,   r  r,   r`   r)  r*  r,   )rc   r,   r6   rR   r  rR   r  rR   r`   r)  r*  r,   )rc   r,   rk  r,   r6   rR   r   r,   r   rl   r*  r,   )
rc   r,   rk  r,   r6   rR   r   r   r`   r)  )rc   r,   r   r,   r   rR   r
  rR   r  rR   r`   r)  r*  r,   )rc   r,   r  r,   r6   rR   rk  rR   r`   r)  r*  r,   )rc   r,   r   r,   r   r/  r   r/  r   r/  r   r/  r`   r)  r*  r,   )rI   r2  r`   r)  r*  r,   )
__future__r   r  inspectr  typingr   r   r   r   rP   typing_extensionsr   rN   r   paddle._C_opsr	   r
   r   paddle.tensorr   paddle.utils.decorator_utilsr   r   r   r   r   r   r   r   r   paddle.utils.inplace_utilsr   base.data_feederr   r   r   r   r  r   r   rU  r   r   r    r!   r"   r#   r$   creationr&   r'   r(   collections.abcr)   r*   numbersr+   r,   paddle._typingr-   r.   r/   r0   r1   r2   r3   r4   __all__r;   rv   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r8   r  r'  r,  r<  rB  rD  r9   rM  rP  rS  rV  rY  r`  r  r  r  r  r  r  r  r  r  r  r  r  r  	signature__signature__r  r  r  r  r  r  r  r  r  r  r  r   r  r#  r   rE  rH  rJ  rJ  rO  rR  rl  rp  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r  r  r  __annotations__itemsr`   funcsetattrr   r  r  r  r  r  r  r(  r   r   rj   <module>rO     su   #    . .  &   5 5 '
 
 
 D  <   L K2  $ @

 	I I
I I 	I
 IXTn & &\\
 \ ,\ *	\
 \~VV	V V 	V
 Vt 8<hh"h*4hhV5z YYY Y 	Y
 Y Y| #-1	ttt +t 	t
 tn " "B    8  #=#=#= #= 	#=
 #= #= #=R ___ _ 	_
 _ _ _J ''' ' 	'
 ' ' 'Z $$$ $ 	$
 $ $ $N # #B I;89 L
 LL
L L
 
L L :L` 15BB#-BBL >BOO(O0:OOf !QDe6e6e6!.e6?Ie6e6P )K=	{K MQMMM/2M?IMMM`%P LP#9#9#9/2#9?I#9#9 #9L I;89 h
 hh
h h
 
h h :hVA8HB4J24j94x8'v <&	 	]](] ] 	]
 ]]@  	m
m
'm
 m
 	m

 m
 m
b HL>B>B2>B:D>B>BD HL2>2>22>:D2>2>l HL7>7>27>:D7>7>t #w&%1KOVV/V>HVV 2Vr KO,,/,>H,, ,( #w&%1 !``` ` 	`
 ` ` #` 2`F 
 #&$'#&	0	0	0 "	0 !		0
 	0 	0 	0 	0 +	0 
	0 
 $'$'#&	(	( 	( "	( !		(
 	( 	( 	( 	( #	( 
	( 
 #&%(#&	(	(	( #	( !		(
 	( 	( 	( 	( #	( 
	( 
 #&$'$'	(	(	( "	( "		(
 	( 	( 	( 	( #	( 
	( 
 $'%(#&	 	  	  #	  !		 
 	  	  	  	  	  
	  
 $'$'$'	 	  	  "	  "		 
 	  	  	  	  	  
	  
 #&%($'	 	 	  #	  "		 
 	  	  	  	  	  
	  
 $'%($'		 	 #	 "		
 	 	 	 	 	 
	 
  	&	&	& 	& 		&
 	& 	& 	& 	& !	& 
	& 	
	vr #w&%1 II
/I I 	I 2IX FJ**1*9C** *2 	

	
 
 
	

 
  !%::: : 	:
 
: :z 
 !%  	
 
  
 

 		  
	
  
N0b ((9  &%!U "Ux >I>I	>I >I 
	>I
 >I >I >IJ 999 9 	9
 9 9 

   	
   
 

 	  
	
    
 9 90 &&'?@        	 
   
   N 	  
	
  
  B 

   	
  
  
 

 	  
	
  
   
k1\  ))*:;  CGXXX'-X5?XXx JN(M(M"(M+4(M<F(M(MV G9ug67GKCBCBCB".CB:DCBCB 8CBL G9vh?@ TT1T T 	T ATn y!;-;-);- ;- ";-| G9x89 -"-"-" -" 	-" :-"` N Nb N Nd @DI(I(I($*I(2<I(I(X ?C$*2< 0  8 
 E 
 E 
36 L 
 L " Pf 
 E 
 E 
36 L 
 L " ^B 
 E 
 E 
36 L 
 L " Slid jj
 j ,j *	j
 -j j j` 01	fff -f 	f
 fR2j-`%P"J #w&%1 	j #jjj j 	j j j 2jb 	xxx %x 	x
 xx 9=YY#Y+5YYx 8<#+5 " 
 9 
 9 
 ? 
 ?
#%#%&)#%:@#%HK#%#%L	 (4&&	& & 
	&
 &R gY89
 	f f	ff f 	f 
f f :f` ;;	; ; 
	;
 ;; ; ;J 	z	zz z 	z
z z z zz  	9	99 9 	9
9 9 9 9x	  
	
    R RRR R 	R
 R R 
R R Rj  ;;; ; 	;
 ; ; ;  ;  G9ug	JK?CPPP!*P2<PP LPf 
 *7*7*7 *7 	*7
 *7 *7 *7Z  N4N4-N4 N4 	N4  N4b %- %-P &+&'CG!5!5!5 #!5+.!56@!5!5 ( !5L $20,	(  //#JD$DJJtT* $&&&$'&06&AE&&T KO-:-:-:$'-:05-:=G-:` JN999$'9059=G9 9 5B5B5B 5B 	5B
 5B 5B 5Br JNiii%(i14i<Fiif mmm m 	m
 m m m m`?) ?)r   