
    ёig             	      V   % 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Jr  S SKJr  S SKJrJr  S SKJrJrJrJr  S S	KJr  S S
KJr  S SKJrJr  S SK J!r!  SSK"J#r#J$r$J%r%  SSK&J'r'J(r(J)r)J*r*  SSK+J,r,  SSK-J.r.J/r/  \(       aR  S SK0J1r1  S SK
J2r2  S SKJ3r3  S SK4J5r5J6r6J7r7J8r8J9r9J:r:J;r;  S SK<J=r=  \S   r>S\?S'   \S   r@S\?S'   \S   rAS\?S'   \S   rBS\?S'   / rC    SD             SES" jjrD\      SF               SGS$ jj5       rE\        SH                   SIS% jj5       rESJS& jrE         SK                     SLS' jjrF\R                  " \F5      \ElH               SM                 SNS( jjrI  SO           SPS) jjrJ\" S!S#/5            SQ               SRS+ jj5       rK   SS         STS, jjrL    SU           SVS- jjrM    SW           SXS. jjrN  SY           SZS/ jjrO   S[         S\S0 jjrP   S[         S\S1 jjrQ\" S!S#/05           S]               S^S2 jj5       rR\" S3S4S S5S69  S_         S`S8 jj5       rS\" S*S9/5       Sa         SbS: jj5       rT  SO         ScS; jjrU   Sd         SeS< jjrV Sf         SgS= jjrW\" S#/S>/S?/S@/S7/SA/SB.5          SD               ShSC jj5       rXg)i    )annotationsN)TYPE_CHECKINGAnyLiteral)overload)_C_opspir)LayerHelper)Variabledefault_main_program)corein_dynamic_modein_dynamic_or_pir_modein_pir_mode)full)
deprecated)ParamAliasDecoratorparam_one_alias)NotSupportedTensorArgumentError   )check_dtype
check_typecheck_variable_and_dtype)clipconcatsqrtsum)zeros)squeeze	unsqueeze)Sequence)	TypeAlias)Tensor)DataLayout1DVariantDataLayout2DDataLayout3DDataLayoutND	ShapeLikeSize2Size4)Group)linearareanearestbilinearbicubic	trilinearr"   _InterpolateMode)upscale_in_traindownscale_in_infer_DropoutMode)r   constantreflect	replicatecircular_PaddingTensorMode)validsameVALIDSAME_PaddingSizeMode   xc           
     Z   [        S0 [        5       D6n[        U S/ SQS5        [        U R                  5      S:X  d   S5       e[        U[        5      (       a  X/nOA[        U[        [        45      (       a  [        U5      S:X  d  [        SS5      e[        U5      n[        U[        5      (       a  X"/nOA[        U[        [        45      (       a  [        U5      S:X  d  [        S	S
5      e[        U5      n[        U[        5      (       a  XD/nOA[        U[        [        45      (       a  [        U5      S:X  d  [        SS5      e[        U5      n[        U[        5      (       a  U/S-  nOb[        U[        [        45      (       a;  [        U5      n[        U5      S:X  a  US-  nO'[        U5      S:X  a  O[        S5      e[        SS5      e[        5       (       a  [        R                  " XX#U5      $ UR                  U R                  S9nUR!                  SSU 0SU0UUUUS.S9  U$ )a  

Return a col buffer of sliding local blocks of input x, also known
as im2col for batched 2D image tensors. For each block under the convolution filter,
all element will be rearranged as a column. While the convolution filter sliding over
the input feature map, a series of such columns will be formed.

For each input :math:`x` with shape [N, C, H, W], the output shape [N, Cout, Lout]
can be calculated as following.

.. math::

    dkernel[0] &= dilations[0] \times (kernel\_sizes[0] - 1) + 1

    dkernel[1] &= dilations[1] \times (kernel\_sizes[1] - 1) + 1

    hout &= \frac{H + paddings[0] + paddings[2] - dkernel[0]}{strides[0]} + 1

    wout &= \frac{W + paddings[1] + paddings[3] - dkernel[1]}{strides[1]} + 1

    Cout &= C \times kernel\_sizes[0] \times kernel\_sizes[1]

    Lout &= hout \times wout


Parameters:
    x(Tensor):              4-D Tensor, input tensor of format [N, C, H, W],
                              data type can be float32 or float64
    kernel_sizes(int|list|tuple):   The size of convolution kernel, should be [k_h, k_w]
                              or an integer k treated as [k, k].
    strides(int|list|tuple, optional):        The strides, should be [stride_h, stride_w]
                              or an integer stride treated as [stride, stride].
                              For default, strides will be [1, 1].
    paddings(int|list|tuple, optional):       The paddings of each dimension, should be
                              [padding_top, padding_left, padding_bottom, padding_right]
                              or [padding_h, padding_w] or an integer padding.
                              If [padding_h, padding_w] was given, it will expanded to
                              [padding_h, padding_w, padding_h, padding_w]. If an integer
                              padding was given, [padding, padding, padding, padding] will
                              be used. For default, paddings will be [0, 0, 0, 0]
    dilations(int|list|tuple, optional):      the dilations of convolution kernel, should be
                              [dilation_h, dilation_w], or an integer dilation treated as
                              [dilation, dilation]. For default, it will be [1, 1].
    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:
    Tensor, The tensor corresponding to the sliding local blocks.
    The output shape is [N, Cout, Lout] as described above.
    Cout is the  total number of values within each block,
    and Lout is the total number of such blocks.
    The data type of output is the same as the input :math:`x`

Examples:

    .. code-block:: python

        >>> import paddle
        >>> import paddle.nn.functional as F

        >>> x = paddle.randn((100,3,224,224))
        >>> y = F.unfold(x, [3, 3], 1, 1, 1)
unfoldrA   uint16float16float32float64   z*input should be the format of [N, C, H, W]   Hkernel_sizes should either be an integer or a list/tuple of two integerskernel_sizesCstrides should either be an integer or a list/tuple of two integersstridesEdilations should either be an integer or a list/tuple of two integers	dilationszGpaddings should either be an integer or a list/tuple of 2 or 4 integersz^Unexpected type of paddings, it should be either an integer or a list/tuple of 2 or 4 integerspaddingsdtypeXY)rL   rN   rQ   rP   typeinputsoutputsattrs)rC   )r
   localsr   lenshape
isinstanceintlisttupler   
ValueErrorr   r   rC   "create_variable_for_type_inferencerS   	append_op)rA   rL   rN   rQ   rP   namehelperouts           [/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/nn/functional/common.pyrC   rC   R   s#   T .VX.F	3;X qww<1JJJ,$$$3 |dE]33\9Ja9O1Z  L)'3$7T5M22G8I1U  w-)S!!*	9tUm44#i.A:M1W  O	(C  :>	HtUm	,	,>x=A!|H]aY  .!
 	
 }}QgKK

3
3!''
3
BC
Qxc
( "	
	  
 J    inputc                    g N )rj   sizescale_factormodealign_cornersrecompute_scale_factor	antialiass          rh   interpolatert      s     ri   c	                    g rl   rm   )	rA   rn   ro   rp   rq   
align_modedata_formatrr   re   s	            rh   rt   rt      s     ri   c                 F  ^ [        U 5      nS	U4S jjnST;   a  U" STR                  S5      5        [        U 5      S:  aV  [        U S   5      [        LaA  SS/n[	        [        US-
  [        U5      5      5       H  nU" XE   XS-      5        M     U SS n [        U 0 TD6$ )
aJ)  

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

1. ``interpolate(input, size, scale_factor, mode, align_corners, recompute_scale_factor, antialias)``:
    PyTorch compatible interpolate.

2. ``interpolate(x, size, scale_factor, mode, align_corners, align_mode, data_format, recompute_scale_factor, name)``
    The original PaddlePaddle implementation of interpolate, see the following docs.


This API resizes a batch of images.

The input must be a 3-D Tensor of the shape (num_batches, channels, in_w)
or (num_batches, in_w, channels), or 4-D (num_batches, channels, in_h, in_w) or
(num_batches, in_h, in_w, channels), or a 5-D Tensor of the shape
(num_batches, channels, in_d, in_h, in_w) or (num_batches, in_d, in_h, in_w, channels),
Where in_w is width of the input tensor, in_h is the height of the input tensor,
in_d is the depth of the input tensor.
and the resizing only applies on the three dimensions(depth, height and width).

Supporting resample methods:

- 'linear' : Linear interpolation
- 'bilinear' : Bilinear interpolation
- 'trilinear' : Trilinear interpolation
- 'nearest' : Nearest neighbor interpolation
- 'bicubic' : Bicubic interpolation
- 'area': Area interpolation

Linear interpolation is the method of using a line connecting two known quantities
to determine the value of an unknown quantity between the two known quantities.

Nearest neighbor interpolation is to perform nearest neighbor interpolation
in both the 3rd dimension(in height direction) and the 4th dimension(in width
direction) on input tensor.

Bilinear interpolation is an extension of linear interpolation for
interpolating functions of two variables (e.g. H-direction and
W-direction in this op) on a rectilinear 2D grid. The key idea is
to perform linear interpolation first in one direction, and then
again in the other direction.

Trilinear interpolation is an extension of linear interpolation for
interpolating functions of three variables (e.g. D-direction,
H-direction and W-direction in this op) on a rectilinear 3D grid.
The linear interpolation is performed on three directions.
align_corners and align_mode are optional parameters,the calculation method
of interpolation can be selected by them.

Bicubic interpolation is an extension of cubic interpolation for interpolating
data points on a two-dimensional regular grid. The interpolated surface is
smoother than corresponding surfaces obtained by bilinear interpolation or
nearest-neighbor interpolation.

Area interpolation is to perform area interpolation
in both the 3rd dimension(in height direction) , the 4th dimension(in width
direction) and the 5th dimension(in depth direction) on input tensor. Set to
area will directly call `paddle.nn.functional.adaptive_avg_pool1d` or
`paddle.nn.functional.adaptive_avg_pool2d` or `paddle.nn.functional.adaptive_avg_pool3d`.

Example:

.. code-block:: text

    # For scale_factor:
        if align_corners = True && out_size > 1 :
          scale_factor = (in_size-1.0)/(out_size-1.0)
        else:
          scale_factor = float(in_size/out_size)

    # Linear interpolation:
        if:
            align_corners = False , align_mode = 0
            input : (N,C,W_in)
            output: (N,C,W_out) where:
            W_out = (W_{in}+0.5) * scale_{factor} - 0.5
        else:
            input : (N,C,W_in)
            output: (N,C,W_out) where:
            W_out = W_{in} * scale_{factor}

    # Nearest neighbor interpolation:

          align_corners = False
          input : (N,C,H_in,W_in)
          output: (N,C,H_out,W_out) where:
          H_out = floor (H_{in} * scale_{factor})
          W_out = floor (W_{in} * scale_{factor})

    # Bilinear interpolation:
      if:
          align_corners = False , align_mode = 0
          input : (N,C,H_in,W_in)
          output: (N,C,H_out,W_out) where:
          H_out = (H_{in}+0.5) * scale_{factor} - 0.5
          W_out = (W_{in}+0.5) * scale_{factor} - 0.5
      else:
          input : (N,C,H_in,W_in)
          output: (N,C,H_out,W_out) where:
          H_out = H_{in} * scale_{factor}
          W_out = W_{in} * scale_{factor}

    # Bicubic interpolation:
      if:
          align_corners = False
          input : (N,C,H_in,W_in)
          output: (N,C,H_out,W_out) where:
          H_out = (H_{in}+0.5) * scale_{factor} - 0.5
          W_out = (W_{in}+0.5) * scale_{factor} - 0.5
      else:
          input : (N,C,H_in,W_in)
          output: (N,C,H_out,W_out) where:
          H_out = H_{in} * scale_{factor}
          W_out = W_{in} * scale_{factor}

    # Trilinear interpolation:
      if:
          align_corners = False , align_mode = 0
          input : (N,C,D_in,H_in,W_in)
          output: (N,C,D_out,H_out,W_out) where:
          D_out = (D_{in}+0.5) * scale_{factor} - 0.5
          H_out = (H_{in}+0.5) * scale_{factor} - 0.5
          W_out = (W_{in}+0.5) * scale_{factor} - 0.5
      else:
          input : (N,C,D_in,H_in,W_in)
          output: (N,C,D_out,H_out,W_out) where:
          D_out = D_{in} * scale_{factor}
          H_out = H_{in} * scale_{factor}
          W_out = W_{in} * scale_{factor}

For details of linear interpolation, please refer to Wikipedia:
https://en.wikipedia.org/wiki/Linear_interpolation.

For details of nearest neighbor interpolation, please refer to Wikipedia:
https://en.wikipedia.org/wiki/Nearest-neighbor_interpolation.

For details of bilinear interpolation, please refer to Wikipedia:
https://en.wikipedia.org/wiki/Bilinear_interpolation.

For details of trilinear interpolation, please refer to Wikipedia:
https://en.wikipedia.org/wiki/Trilinear_interpolation.

For details of bicubic interpolation, please refer to Wikipedia:
https://en.wikipedia.org/wiki/Bicubic_interpolation

Parameters:
    x (Tensor): 3-D, 4-D or 5-D Tensor, its data type is float32, float64, or uint8, its data format is
         specified by :attr:`data_format`. If :attr:`data_format` is not provided, the data format will
         be presumed according to its dimension. See details in :attr:`data_format`.
        alias: ``input``.
    size (list|tuple|Tensor|None): Output shape of image resize
         layer, the shape is (out_w, ) when input is a 3-D Tensor, the shape is (out_h, out_w)
         when input is a 4-D Tensor and is (out_d, out_h, out_w) when input is a 5-D Tensor.
         Default: None. If a list/tuple, each element can be an integer or a Tensor of shape: [1] or [].
         If a Tensor, its dimensions size should be a 1.
    scale_factor (float|Tensor|list|tuple|None): The multiplier for the input height or width. At
         least one of :attr:`size` or :attr:`scale_factor` must be set.
         And :attr:`size` has a higher priority than :attr:`scale_factor`.Has to match input size if it is either
         a list or a tuple or a Tensor. If a list/tuple, each element can be an integer or a Tensor of shape: [1] or [].
         Default: None.
    mode (str): The resample method. It supports 'linear', 'area', 'nearest', 'bilinear',
                   'bicubic' and 'trilinear' currently. Default: 'nearest'
    align_corners(bool) :  An optional bool, If True, the centers of the 4 corner pixels of the
                           input and output tensors are aligned, preserving the values at the
                           corner pixels.This only has an effect when 'linear', 'bilinear', 'bicubic' or 'trilinear'.
                           Default: False
    antialias(bool) : Flag to apply anti-aliasing. Default: False. Using anti-alias option together with align_corners=False,
                      interpolation result would match Pillow result for downsampling operation.
                      Supported modes: 'bilinear', 'bicubic'.
    align_mode(int)  :  An optional for linear/bilinear/trilinear interpolation. Refer to the formula in the example above,
                        it can be '0' for src_idx = scale_factor*(dst_index+0.5)-0.5 , can be '1' for
                        src_idx = scale_factor*dst_index.
    data_format (str, optional): Specify the data format of the input, and the data format of
         the output will be consistent with that of the input. An optional string from:`"NCW"`,
         `"NWC"`,  `"NCHW"`, `"NHWC"`, `"NCDHW"`, `"NDHWC"`. The default value is None.
         When :attr:`data_format` is not specified, it will be automatically inferred from the
         input dimension of :attr:`x`. When :attr:`x` is a 3-D Tensor, :attr:`data_format` will be
         set to `"NCW"`; When :attr:`x` is a 4-D Tensor, :attr:`data_format` will be set to
         `"NCHW"`; When :attr:`x` is a 5-D Tensor, :attr:`data_format` will be set to `"NCDHW"`.
         When it is `"NCHW"`, the data should be stored in the order of:
         `[batch_size, input_channels, input_height, input_width]`. When it is `"NCDHW"`, the
         data should be stored in the order of: `[batch_size, input_channels, input_depth, input_height, input_width]`.
    recompute_scale_factor (bool, optional):  Whether to recompute the scaling factor for interpolation calculation.
         When set to `True`, the `scale_factor` parameter must be provided, and the function will use it along with
         the input tensor shape to calculate the output tensor shape, then recalculate the scaling factor based on
         the output and input tensor shapes. This parameter is particularly useful when `scale_factor` is a floating-point
         value. When set to `False`, either `size` or `scale_factor` will be used directly for interpolation without
         recalculation. Default: None.
    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:
    A 3-D, 4-D or 5-D Tensor, with the same data format of the input :attr:`x`.


Examples:
    .. code-block:: pycon

        >>> import paddle
        >>> import paddle.nn.functional as F

        >>> input_data = paddle.randn(shape=(2, 3, 6, 10)).astype(paddle.float32)
        >>> output_1 = F.interpolate(x=input_data, size=[12, 12])
        >>> print(output_1.shape)
        paddle.Size([2, 3, 12, 12])
        >>> # given scale
        >>> output_2 = F.interpolate(x=input_data, scale_factor=[2, 1])
        >>> print(output_2.shape)
        paddle.Size([2, 3, 12, 10])
        >>> # bilinear interp
        >>> output_3 = F.interpolate(x=input_data, scale_factor=[2, 1], mode="bilinear")
        >>> print(output_2.shape)
        paddle.Size([2, 3, 12, 10])
c                :   > U T;   a  [        SU  S35      eUTU '   g )Nz"got multiple values for argument '')	TypeError)keyvaluekwargss     rh   safe_set_param#interpolate.<locals>.safe_set_param  s(    &=@QGHHsri   rj   rA         rr   rs   N)r|   strr}   r   )r\   poprW   r_   rangemin_interpolate_wrapper)argsr~   len_argsr   
param_keysidxs    `    rh   rt   rt     s    p 4yH
 &sFJJw/0
4yA~$tAw-s2.<
X\3z?;<C:?DqM: =BQx000ri   c
                2"  ^) Uc?  [        U R                  5      n
U
S:X  a  SnO!U
S:X  a  SnOU
S:X  a  SnO[        SU
 S35      eUR                  5       nUR                  5       nUR	                  5       n/ S	QnX;  a  [        S
5      eUS;   a$  [        U R                  5      S:w  a  [        S5      eUS;   a=  [        U R                  5      S:w  a$  [        U R                  5      S:w  a  [        S5      eUS;   a$  [        U R                  5      S:w  a  [        S5      eUS:X  a$  [        U R                  5      S:w  a  [        S5      eUc  Uc  [        S5      e[        U[        [        45      (       a'  [        U5      U R                  S-
  :w  a  [        S5      e[        U[        [        R                  R                  45      (       ae  UR                  S5      nUR                  S:w  a  [        SUR                   S35      eUR                  S   U R                  S-
  :w  a  [        S5      e[        U[        5      (       d  [        S5      e[        U[        5      (       d  [        S5      eUS:w  a  US:w  a  [        S5      eUS:w  a  US:X  a  [        S5      eU(       a  US;  a  [        S 5      eUS!:X  Ga'  [        U[        [        [        [        R                  R                  45      (       a  [        U5      S:X  a  [        S"5      eUc  [        S#5      e[        U R                  5      S:X  a)  [        R                   R"                  R%                  X5      $ [        U R                  5      S:X  a)  [        R                   R"                  R'                  X5      $ [        U R                  5      S:X  a)  [        R                   R"                  R)                  X5      $ [+        U S$340 [-        5       D6n[        U R                  5      S:X  a  US%;  a  [        S&U-   S'-   5      e[        U R                  5      S:X  a  US(;  a  [        S&U-   S)-   5      e[        U R                  5      S:X  a  US*;  a  [        S&U-   S+-   5      eS, nUS:X  d  US:X  d  US:X  a  SnUS-:X  d  US.:X  d  US/:X  a  S-nUS:X  a  S0nS1U 0nS2S2S2UUUWS3.nUnUnUb  Ub  [        S45      eUGb  U(       a  [        S55      e[        U[        [        R                  R                  45      (       a  [/        5       (       d  S6Ul        UUS7'   GO[/        5       (       az  [        U[        5      (       a  [        UR3                  S05      5      nO[        U5      n[5        U5       H0  u  nn[        U[        5      (       d  M  UR7                  5       UU'   M2     U" U5      (       d  [        S85      eS0n[5        U5       HF  u  nn
[        U
[        [        R                  R                  45      (       a  S6nM9  U
S:  a  MA   S95       e   U(       Ga	  / n/ nU H  n[        U[        [        R                  R                  45      (       a+  S6Ul        UR9                  U5        UR9                  S25        M]  [        U[:        5      (       d   e[=        5       (       a"  [        R>                  RA                  S/SUS6S:9nO3URC                  S5      n[        R>                  RA                  S/SUS6US;9  UR9                  U5        UR9                  U5        M     UUS<'   [        U R                  5      S:X  aK  [        U5      S:w  a  [        S=5      eU(       a	  WS   US>'   O![        [E        [:        U5      5      nUS   US>'   [        U R                  5      S:X  a[  [        U5      S:w  a  [        S?5      eU(       a  WS   US@'   US   US>'   O)[        [E        [:        U5      5      nUS   US@'   US   US>'   [        U R                  5      S:X  al  [        U5      S:w  a  [        SA5      eU(       a  WS   USB'   US   US@'   US   US>'   GO[        [E        [:        U5      5      nUS   USB'   US   US@'   US   US>'   GO_UGb[  U R                  nUS:X  a  US   m)OUS/:X  a  US   m)OUS:X  a'  [G        U R                  S   U R                  S   5      m)OUS-:X  a'  [G        U R                  S   U R                  S   5      m)OxUS:X  a5  [G        U R                  S   U R                  S   U R                  S   5      m)O=US.:X  a5  [G        U R                  S   U R                  S   U R                  S   5      m)OSm)U)4SC jnU(       Ga  [/        5       (       aJ  [        U[        5      (       a5  UR                  / :X  a  [I        U5      nO[        UR3                  5       5      n[        U R                  5      S-
  n[        U[H        [:        [2        RJ                  45      (       a  [I        U5      /U-  nOt[        U[        [        45      (       aN  [        U5      U:w  a%  [        SDU SE[        U R                  5       SF35      e[        [E        [H        U5      5      nO[        SG5      e/ n[M        U5       HT  nU R                  US-      n[;        [2        RN                  " [I        U5      UU   -  5      5      n UR9                  U 5        MV     [        U R                  5      S:X  a	  US   US>'   O[[        U R                  5      S:X  a  US   US@'   US   US>'   O1[        U R                  5      S:X  a  US   USB'   US   US@'   US   US>'   S nGORS0n![/        5       (       a  S6n!U!(       aJ  [        U[        5      (       a5  UR                  / :X  a  [I        U5      nO[        UR3                  5       5      n[        U[        [        R                  R                  45      (       a  S6Ul        UUSH'   GO[        U[H        [:        [2        RJ                  45      (       a  US::  a  [        SI5      e/ n[M        [        U R                  5      S-
  5       H  nUR9                  U5        M     U!(       a*  U RP                  S:X  a  [        [E        UU5      5      USJ'   GO
[        [E        [H        U5      5      USJ'   O[        U[        [        45      (       a  [        U5      [        U R                  5      S-
  :w  a;  [        SD[        U R                  5      S-
   SE[        U R                  5       SF35      eU H  n"U"S::  d  M  [        SI5      e   U!(       a)  U RP                  S:X  a  [        [E        UU5      5      USJ'   O([        [E        [H        U5      5      USJ'   O[        SG5      e[S        5       (       Ga  / n#URU                  5        H(  u  n$n%U#R9                  U$5        U#R9                  U%5        M*     [        U#5      n&U(       ad  [V        RX                  " U S7U;   a  US7   OS S<U;   a  US<   OS SHU;   a  USH   OS USK   USB   US@   US>   SJU;   a  USJ   O/ USL   USM   USN   5      n'U'$ USO:X  ad  [V        RZ                  " U S7U;   a  US7   OS S<U;   a  US<   OS SHU;   a  USH   OS USK   USB   US@   US>   SJU;   a  USJ   O/ USL   USM   USN   5      n'U'$ USP:X  ad  [V        R\                  " U S7U;   a  US7   OS S<U;   a  US<   OS SHU;   a  USH   OS USK   USB   US@   US>   SJU;   a  USJ   O/ USL   USM   USN   5      n'U'$ USQ:X  ad  [V        R^                  " U S7U;   a  US7   OS S<U;   a  US<   OS SHU;   a  USH   OS USK   USB   US@   US>   SJU;   a  USJ   O/ USL   USM   USN   5      n'U'$ USR:X  ad  [V        R`                  " U S7U;   a  US7   OS S<U;   a  US<   OS SHU;   a  USH   OS USK   USB   US@   US>   SJU;   a  USJ   O/ USL   USM   USN   5      n'U'$ USS:X  ab  [V        Rb                  " U S7U;   a  US7   OS S<U;   a  US<   OS SHU;   a  USH   OS USK   USB   US@   US>   SJU;   a  USJ   O/ USL   USM   USN   5      n'W'$ URe                  STSU9n(URC                  U(5      n'URg                  U S$3USVU'0USW9  U'$ )XNr   NCWrI   NCHWr   NCDHWz`The dimension of the input tensor should only be 3-D, 4-D or 5-D, but the received dimension is .)LINEARBILINEAR	TRILINEARNEARESTBICUBICAREAzxThe 'resample' of image_resize can only be 'area', 'linear', 'bilinear', 'trilinear',  'bicubic' or 'nearest' currently.)r   z!'linear' only support 3-D tensor.)r   z*'NEAREST' only support 4-D  or 5-D tensor.)r   r   z1'bilinear' and 'bicubic' only support 4-D tensor.r   z#'trilinear'only support 5-D tensor.z.One of size and scale_factor must not be None.rJ   z7The x and size should satisfy rank(x) - 2 == len(size).int32r@   z7If size is a tensor, it's rank must be 1, but received r   z;The x and size should satisfy rank(x) - 2 == size.shape[0].z)Attr align_corners should be a bool valuez%Attr antialias should be a bool valuezalign_mode can only be 0 or 1r   zjalign_corners option can only be set with the interpolating modes: linear | bilinear | bicubic | trilinearzBAnti-alias option is only supported for bilinear and bicubic modesr   zoutput size can not be emptyz(output size can not be None in AREA mode
_interp_v2)r   NWCz)Got wrong value for param `data_format`: z: received but only `NCW` or `NWC` supported for 3-D input.r   NHWCz< received but only `NCHW` or `NHWC` supported for 4-D input.r   NDHWCz> received but only `NCDHW` or `NDHWC` supported for 5-D input.c                .    [        U [        [        45      $ rl   r^   r`   ra   datas    rh   _is_list_or_tuple_0_interpolate_wrapper.<locals>._is_list_or_tuple_k      $u..ri   r   r   r   FrT   )out_dout_hout_winterp_methodrq   rv   data_layoutz3Only one of size or scale_factor should be defined.z?recompute_scale_factor is not meaningful with an explicit size.TOutSizez+size should be a list or tuple or Variable.z>Each dimension size given in out_shape must be greater than 0.)	force_cpu)r   rg   
SizeTensorz,size length should be 2 for input 3-D tensorr   z-size length should be 2 for input 4-D tensor.r   z-size length should be 3 for input 5-D tensor.r   c                   > [        [        U 5      5      S::  a  U $ [        R                  " [	        U T-  5      T-  5      $ )N
   )r\   r   numpyrG   r_   )r}   max_dims    rh   _scale_to_float32/_interpolate_wrapper.<locals>._scale_to_float32  s5    3u:"$==UW_!5!?@@ri   zscale_shape length should be z for input z
-D tensor.z@Attr(scale)'s type should be float, int, list, tuple, or Tensor.Scalez(Attr(scale) should be greater than zero.scaler   r   rq   rv   r,   r/   r1   r.   r0   rA   input_param_nameOutrV   )4r\   r]   rb   upperlowerr^   ra   r`   ndimr   paddler	   Valuecastboolr{   nn
functionaladaptive_avg_pool1dadaptive_avg_pool2dadaptive_avg_pool3dr
   r[   r   stop_gradientr   	enumerateitemappendr_   r   tensorfill_constantrc   mapmaxfloatndarrayr   floorrn   r   itemsr   interp_antialiaslinear_interpbilinear_interptrilinear_interpnearest_interpbicubic_interpinput_dtyperd   )*rA   rn   ro   rp   rq   rv   rw   rr   rs   re   dim_sizeresampleresample_typeresample_methodsrf   r   r   rX   rZ   	out_shaper   idimcontain_vardim_idxnew_size_tensor	size_listtemp_outx_shaper   
scale_list
input_sizeoutput_sizedynamic_moder}   	attr_listkvdy_attrrg   rS   r   s*                                            @rh   r   r     s    qww<q=K] K]!Krs{r||}~  ##%Kzz|HJJLM '1
 	

 :#agg,!"3<==;3qww<1#4QWW9JEFF**s177|q/@LMM;3qww<1#4>??|,IJJ$&&CI!,CE
 	
 $6::#3#3455yy!99>I$))TUV  ::a=AFFQJ&M  mT**CDDi&&?@@Q:?899h)3x
 	
 X%<<P
 	
 6dT5(FJJ4D4DEFF4yA~ !?@@<GHHqww<199'';;ADD\Q99'';;ADD\Q99'';;ADDM?*5BBF
177|q[>7JK
 	

 
QWW	{2BB7LM
 	

 
QWW	{2DD7NO
 	
/ fw 6+:Nfw 6+:N91XF&& "E IE!2NOO!Q 
 y8VZZ-=-=">??#%%&*I# )F9  i22 $Y__U%; <I $YI'	2FAs!#x00'*xxz	! 3 'y11 MNNK%.y%9!h6::3C3C(DEE"&K!| T|	 &: "$	$C!#&**2B2B'CDD,0)'..s3!((,)#s3333&=='-}}'B'B!"WcT (C (H
 !' I I$+!" %
 #MM77!"WcTx 8  (..x8!((-+ %, (7|$177|q y>Q&$F  %.q\E'N $Si%8 9I%.q\E'N177|q y>Q&$G  %.q\E'N%.q\E'N $Si%8 9I%.q\E'N%.q\E'N177|q y>Q&$G  %.q\E'N%.q\E'N%.q\E'N $Si%8 9I%.q\E'N%.q\E'N%.q\E'N		 ''%ajGE!ajGF"!''!*aggaj1GF"!''!*aggaj1GG#!''!*aggaj!''!*=GG#!''!*aggaj!''!*=GG	A "  Zx%@%@;;"$!%LE /Eagg,"C%%emm!<==#El^c1
ED%=11u:$$7u =!!$QWWj:  "#eU"34
V  I3ZWWQU^
!KKj 1JqM AB   -   177|q !*1gQWW"!*1g!*1gQWW"!*1g!*1g!*1gE L  #
5( ; ;;;"$!%LE /E%(FJJ,<,<!=>>&*#"'wEE3#>??A:$%OPP
s177|a/0A%%e, 1AFFaK%)#.?*L%ME'N%)#eZ*@%AE'NED%=11u:QWW!11$7AGGq8H7I J!!$QWWj:  #Ez(F  #
  AFFaK%)#.?*G%HE'N%)#eU*;%<E'NV  	KKMDAqQQ " 	"))%.&%8y!d(4(>|$D#*f#4w$m$ggg")U"2go&o&l#Cr 
W h&&&%.&%8y!d(4(>|$D#*f#4w$m$ggg")U"2go&o&l#CT 
y j(((%.&%8y!d(4(>|$D#*f#4w$m$ggg")U"2go&o&l#Cv 
[ k)))%.&%8y!d(4(>|$D#*f#4w$m$ggg")U"2go&o&l#CX 
= i'''%.&%8y!d(4(>|$D#*f#4w$m$ggg")U"2go&o&l#C: 
 i'''%.&%8y!d(4(>|$D#*f#4w$m$ggg")U"2go&o&l#C 
 4E

3
3E
:C
j)	   Jri   c           	         [        XX#XEU5      $ )a$  

This API resizes a batch of images.

The input must be a 3-D Tensor of the shape (num_batches, channels, in_w)
or (num_batches, in_w, channels), or 4-D (num_batches, channels, in_h, in_w) or
(num_batches, in_h, in_w, channels), or a 5-D Tensor of the shape
(num_batches, channels, in_d, in_h, in_w) or (num_batches, in_d, in_h, in_w, channels),
Where in_w is width of the input tensor, in_h is the height of the input tensor,
in_d is the depth of the input tensor.
and the resizing only applies on the three dimensions(depth, height and width).

Supporting resample methods:
- 'linear' : Linear interpolation
- 'bilinear' : Bilinear interpolation
- 'trilinear' : Trilinear interpolation
- 'nearest' : Nearest neighbor interpolation
- 'bicubic' : Bicubic interpolation
- 'area': Area interpolation

Linear interpolation is the method of using a line connecting two known quantities
to determine the value of an unknown quantity between the two known quantities.

Nearest neighbor interpolation is to perform nearest neighbor interpolation
in both the 3rd dimension(in height direction) and the 4th dimension(in width
direction) on input tensor.
Bilinear interpolation is an extension of linear interpolation for
interpolating functions of two variables (e.g. H-direction and
W-direction in this op) on a rectilinear 2D grid. The key idea is
to perform linear interpolation first in one direction, and then
again in the other direction.

Bicubic interpolation is an extension of cubic interpolation for interpolating
data points on a two-dimensional regular grid. The interpolated surface is
smoother than corresponding surfaces obtained by bilinear interpolation or
nearest-neighbor interpolation.

Trilinear interpolation is an extension of linear interpolation for
interpolating functions of three variables (e.g. D-direction,
H-direction and W-direction in this op) on a rectilinear 3D grid.

The linear interpolation is performed on three directions.
align_corners and align_mode are optional parameters,the calculation method
of interpolation can be selected by them.

Area interpolation is to perform area interpolation
in both the 3rd dimension(in height direction) , the 4th dimension(in width
direction) and the 5th dimension(in depth direction) on input tensor. Set to
area will directly call `paddle.nn.functional.adaptive_avg_pool1d` or
`paddle.nn.functional.adaptive_avg_pool2d` or `paddle.nn.functional.adaptive_avg_pool3d`.

Example:
    .. code-block:: text

        For scale_factor:
            if align_corners = True && out_size > 1 :
            scale_factor = (in_size-1.0)/(out_size-1.0)
            else:
            scale_factor = float(in_size/out_size)
        Linear interpolation:
            if:
                align_corners = False , align_mode = 0
                input : (N,C,W_in)
                output: (N,C,W_out) where:
                W_out = (W_{in}+0.5) * scale_{factor} - 0.5
            else:
                input : (N,C,W_in)
                output: (N,C,W_out) where:
                W_out = W_{in} * scale_{factor}
        Nearest neighbor interpolation:
        if:
            align_corners = False
            input : (N,C,H_in,W_in)
            output: (N,C,H_out,W_out) where:
            H_out = floor (H_{in} * scale_{factor})
            W_out = floor (W_{in} * scale_{factor})
        else:
            align_corners = True
            input : (N,C,H_in,W_in)
            output: (N,C,H_out,W_out) where:
            H_out = round(H_{in} * scale_{factor})
            W_out = round(W_{in} * scale_{factor})

        Bilinear interpolation:
        if:
            align_corners = False , align_mode = 0
            input : (N,C,H_in,W_in)
            output: (N,C,H_out,W_out) where:
            H_out = (H_{in}+0.5) * scale_{factor} - 0.5
            W_out = (W_{in}+0.5) * scale_{factor} - 0.5
        else:
            input : (N,C,H_in,W_in)
            output: (N,C,H_out,W_out) where:
            H_out = H_{in} * scale_{factor}
            W_out = W_{in} * scale_{factor}
        Bicubic interpolation:
        if:
            align_corners = False
            input : (N,C,H_in,W_in)
            output: (N,C,H_out,W_out) where:
            H_out = (H_{in}+0.5) * scale_{factor} - 0.5
            W_out = (W_{in}+0.5) * scale_{factor} - 0.5
        else:
            input : (N,C,H_in,W_in)
            output: (N,C,H_out,W_out) where:
            H_out = H_{in} * scale_{factor}
            W_out = W_{in} * scale_{factor}
        Trilinear interpolation:
        if:
            align_corners = False , align_mode = 0
            input : (N,C,D_in,H_in,W_in)
            output: (N,C,D_out,H_out,W_out) where:
            D_out = (D_{in}+0.5) * scale_{factor} - 0.5
            H_out = (H_{in}+0.5) * scale_{factor} - 0.5
            W_out = (W_{in}+0.5) * scale_{factor} - 0.5
        else:
            input : (N,C,D_in,H_in,W_in)
            output: (N,C,D_out,H_out,W_out) where:
            D_out = D_{in} * scale_{factor}
            H_out = H_{in} * scale_{factor}
            W_out = W_{in} * scale_{factor}

For details of linear interpolation, please refer to Wikipedia:
https://en.wikipedia.org/wiki/Linear_interpolation.

For details of nearest neighbor interpolation, please refer to Wikipedia:
https://en.wikipedia.org/wiki/Nearest-neighbor_interpolation.

For details of bilinear interpolation, please refer to Wikipedia:
https://en.wikipedia.org/wiki/Bilinear_interpolation.

For details of bicubic interpolation, please refer to Wikipedia:
https://en.wikipedia.org/wiki/Bicubic_interpolation

For details of trilinear interpolation, please refer to Wikipedia:
https://en.wikipedia.org/wiki/Trilinear_interpolation.

Parameters:
    x (Tensor): 3-D, 4-D or 5-D Tensor, its data type is float32, float64, or uint8, its data format is
         specified by :attr:`data_format`. If :attr:`data_format` is not provided, the data format will
         be presumed according to its dimension. See details in :attr:`data_format`.
    size (list|tuple|Tensor|None, optional): Output shape of image resize
         layer, the shape is (out_w, ) when input is a 3-D Tensor, the shape is (out_h, out_w)
         when input is a 4-D Tensor and is (out_d, out_h, out_w) when input is a 5-D Tensor.
         Default: None. If a list/tuple, each element can be an integer or a Tensor of shape: [1] or [].
         If a Tensor , its dimensions size should be a 1.
    scale_factor (float|Tensor|list|tuple|None, optional): The multiplier for the input height or width. At
         least one of :attr:`size` or :attr:`scale_factor` must be set.
         And :attr:`size` has a higher priority than :attr:`scale_factor`.Has to match input size if
         it is either a list or a tuple or a Tensor. If a list/tuple, each element can be an integer or a Tensor of shape: [1] or [].
         Default: None.
    mode (str, optional): The resample method. It supports 'linear', 'nearest', 'bilinear', 'area',
                   'bicubic' and 'trilinear' currently. Default: 'nearest'
    align_corners(bool, optional) :  An optional bool, If True, the centers of the 4 corner pixels of the
                           input and output tensors are aligned, preserving the values at the
                           corner pixels.
                           Default: False
    align_mode(int, optional)  :  An optional for linear/bilinear/trilinear interpolation. Refer to the formula in the example above,
                        it can be '0' for src_idx = scale_factor*(dst_index+0.5)-0.5 , can be '1' for
                        src_idx = scale_factor*dst_index.
    data_format (str, optional): Specify the data format of the input, and the data format of
         the output will be consistent with that of the input. An optional string from:`"NCW"`,
         `"NWC"`,  `"NCHW"`, `"NHWC"`, `"NCDHW"`, `"NDHWC"`. The default value is None.
         When :attr:`data_format` is not specified, it will be automatically inferred from the
         input dimension of :attr:`x`. When :attr:`x` is a 3-D Tensor, :attr:`data_format` will be
         set to `"NCW"`; When :attr:`x` is a 4-D Tensor, :attr:`data_format` will be set to
         `"NCHW"`; When :attr:`x` is a 5-D Tensor, :attr:`data_format` will be set to `"NCDHW"`.
         When it is `"NCHW"`, the data should be stored in the order of:
         `[batch_size, input_channels, input_height, input_width]`. When it is `"NCDHW"`, the
         data should be stored in the order of: `[batch_size, input_channels, input_depth, input_height, input_width]`.
    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:
    A 3-D, 4-D or 5-D Tensor, with the same data format of the input :attr:`x`.

Examples:
    .. code-block:: pycon

        >>> import paddle
        >>> import paddle.nn as nn

        >>> input_data = paddle.randn(shape=(2, 3, 6, 10)).astype(paddle.float32)
        >>> upsample_out = paddle.nn.Upsample(size=[12, 12])
        >>> output = upsample_out(x=input_data)
        >>> print(output.shape)
        paddle.Size([2, 3, 12, 12])

)rt   )rA   rn   ro   rp   rq   rv   rw   re   s           rh   upsampler     s    V 	] ri   c                (   [        5       (       a  [        R                  " XX#5      $ [        U SSS/S5        [        USSS/S5        XUS.nUb  X5S'   [	        S0 [        5       D6nUR                  U R                  S9nUR                  S	US
U0S9  U$ )a0  

This layer performs bilinear on two inputs.
See :ref:`api_paddle_nn_Bilinear` for details and output shape.

Parameters:
    x1 (Tensor): the first input tensor, it's data type should be float32, float64.
    x2 (Tensor): the second input tensor, it's data type should be float32, float64.
    weight (Tensor): The learnable weights of this layer, shape is [out_features, in1_features, in2_features].
    bias (Tensor, optional): The learnable bias(Bias) of this layer, shape is [1, out_features]. If it is set to None, no bias will be added to the output units. The default value is None.
    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`. Default: None.

Returns:
    Tensor: A 2-D Tensor of shape [batch_size, out_features].

Examples:
    .. code-block:: pycon

        >>> import paddle
        >>> import paddle.nn.functional as F

        >>> x1 = paddle.randn((5, 5)).astype(paddle.float32)
        >>> x2 = paddle.randn((5, 4)).astype(paddle.float32)
        >>> w = paddle.randn((1000, 5, 4)).astype(paddle.float32)
        >>> b = paddle.randn((1, 1000)).astype(paddle.float32)
        >>> result = F.bilinear(x1, x2, w, b)
        >>> print(result.shape)
        paddle.Size([5, 1000])
x1rG   rH   r/   x2)rT   rU   WeightBiasrR   bilinear_tensor_productr   )rW   rX   rY   )r/   )	r   r   r/   r   r
   r[   rc   rS   rd   )r   r   weightbiasre   rX   rf   rg   s           rh   r/   r/     s    L rv44 TIy+A:N TIy+A:Nf5!6N468477bhh7G*6E3< 	 	
 
ri   axisc           
     	  ^^ [        T[        [        [        [        R
                  45      (       d  [        S5      e[        T[        [        45      (       a  TS:X  a  U $ TS:  d  TS:  a  [        S5      eTS;  a  [        S5      eU(       a+  [        U[        [        [        45      (       d  [        S5      eUGcq  SnTS	:X  a  S
OTm[        5       (       a  [        R                  R                  5       R                  S:w  a(  [        R                  R                  5       R                  nU(       a(  [        R                   " U STU(       + TUb  UOSUSL5      $ [        R"                  " U STU(       + TUb  UOSUSL5      $ [%        S#0 ['        5       D6n[)        U S/ SQS5        UR+                  U R,                  S9n	UR+                  [.        R0                  R2                  R4                  SS9n
UU4S jnU" UR6                  TU(       + U5      nUR9                  SSU /0U	/U
/S.US9  U	$ U(       a  [;        S5      e[=        5       (       d  [)        U S/ SQS5        U R,                  nST-
  nU(       Ga,  [=        5       (       a  TS:X  a  [        R>                  " U SS9$ [A        5       (       a6  [        T[        [        45      (       a  TS:X  a  [        R>                  " U SS9$ TS:X  a  [        R>                  " U SU-  S9OU nU RB                  n[=        5       (       d  [        RB                  " U 5      n[        U[        5      (       a  U/O
[        U5      n[E        U5      S:  d  [G        U5      [I        U5      S-
  :  a$  [        S[I        U5       S[G        U5       S35      e[I        U5      [I        U5      :  a#  [        S[I        U5       S[I        U5       35      eS/[I        U5      -  n[=        5       (       d  U H  nWU   UU'   M     OU H  nUU   UU'   M     [        RJ                  " USSSS 9n[M        S/TSS!9m[        RN                  " UT5      n[        RP                  " X5      n[        RP                  " UU5      n[        RR                  " UUUS"9nU$ TS	:X  a  [        R>                  " XS9nU$ U nU$ )$a  
Dropout is a regularization technique for reducing overfitting by preventing
neuron co-adaption during training. The dropout operator randomly sets the
outputs of some units to zero, while upscale others according to the given
dropout probability.

Args:
    x (Tensor): The input tensor. The data type is float16, float32 or float64.
    p (float|int, optional): Probability of setting units to zero. Default: 0.5.
    axis (int|list|tuple, optional): The axis along which the dropout is performed. Default: None.
    training (bool, optional): A flag indicating whether it is in train phrase or not. Default: True.
    inplace (bool, optional): If set to ``True``, will do this operation in-place. Default: ``False``
    mode(str, optional): ['upscale_in_train'(default) | 'downscale_in_infer'].

        1. upscale_in_train (default), upscale the output at training time

            - train: :math:`out = input \times \frac{mask}{(1.0 - dropout\_prob)}`
            - inference: :math:`out = input`

        2. downscale_in_infer, downscale the output at inference

            - train: :math:`out = input \times mask`
            - inference: :math:`out = input \times (1.0 - dropout\_prob)`

    name (str, optional): Name for the operation, Default: None. For more information, please refer to :ref:`api_guide_Name`.

Returns:
    A Tensor representing the dropout, has same shape and data type as `x` .


Examples:
    We use ``p=0.5`` in the following description for simplicity.

    1. When ``axis=None`` , this is commonly used dropout, which dropout each element of x randomly.

    ..  code-block:: text

        Let's see a simple case when x is a 2d tensor with shape 2*3:
        [[1 2 3]
         [4 5 6]]
        we generate mask with the same shape as x, which is 2*3. The value of mask is
        sampled from a Bernoulli distribution randomly. For example, we may get such mask:
        [[0 1 0]
         [1 0 1]]
        So the output is obtained from elementwise multiply of x and mask:
        [[0 2 0]
         [4 0 6]]
        Using default setting, i.e. ``mode='upscale_in_train'`` ,
        if in training phase, the final upscale output is:
        [[0 4 0 ]
         [8 0 12]]
        if in test phase, the output is the same as input:
        [[1 2 3]
         [4 5 6]]
        we can also set ``mode='downscale_in_infer'`` , then
        if in training phase, the final output is:
        [[0 2 0]
         [4 0 6]]
        if in test phase, the scale output is:
        [[0.5 1.  1.5]
         [2.  2.5 3. ]]



    2. When ``axis!=None`` , this is useful for dropping whole channels from an image or sequence.

    ..  code-block:: text

        Let's see the simple case when x is a 2d tensor with shape 2*3 again:
        [[1 2 3]
         [4 5 6]]
        (1) If ``axis=0`` , this means the dropout is only performed in axis `0` .
            we generate mask with the shape 2*1. Only in axis `0` the value is randomly selected.
            For example, we may get such mask:
            [[1]
             [0]]
            The output is obtained from elementwise multiply of x and mask. Doing that the mask will be
            broadcast from 2*1 to 2*3:
            [[1 1 1]
             [0 0 0]]
            and the result after elementwise multiply is:
            [[1 2 3]
             [0 0 0]]
            then we can do upscale or downscale according to the setting of other arguments.
        (2) If ``axis=1`` , this means the dropout is only performed in axis `1` .
            we generate mask with the shape 1*3. Only in axis `1` the value is randomly selected.
            For example, we may get such mask:
            [[1 0 1]]
            Doing elementwise multiply the mask will be broadcast from 1*3 to 2*3:
            [[1 0 1]
             [1 0 1]]
            and the result after elementwise multiply is:
            [[1 0 3]
             [4 0 6]]
        (3) What about ``axis=[0, 1]`` ? This means the dropout is performed in all axes of x,
            which is the same case as default setting ``axis=None`` .
        (4) You may note that logically `axis=None` means the dropout is performed in none axis of x,
            We generate mask with the shape 1*1. Whole input is randomly selected or dropped.
            For example, we may get such mask:
            [[0]]
            Doing elementwise multiply the mask will be broadcast from 1*1 to 2*3:
            [[0 0 0]
             [0 0 0]]
            and the result after elementwise multiply is:
            [[0 0 0]
             [0 0 0]]
            Actually this is not what we want because all elements may set to zero~

    When x is a 4d tensor with shape `NCHW`, where `N` is batch size, `C` is the number of channels, H and W are the height and width of the feature, we can set ``axis=[0,1]`` and the dropout will be performed in channel `N` and `C`, `H` and `W` is tied, i.e. paddle.nn.dropout(x, p, axis=[0,1]) . Please refer to ``paddle.nn.functional.dropout2d`` for more details.
    Similarly, when x is a 5d tensor with shape `NCDHW`, where `D` is the depth of the feature, we can set ``axis=[0,1]`` to perform dropout3d. Please refer to ``paddle.nn.functional.dropout3d`` for more details.

    .. code-block:: python

        >>> import paddle
        >>> paddle.seed(2023)
        >>> x = paddle.to_tensor([[1,2,3], [4,5,6]]).astype(paddle.float32)
        >>> y_train = paddle.nn.functional.dropout(x, 0.5)
        >>> y_test = paddle.nn.functional.dropout(x, 0.5, training=False)
        >>> y_0 = paddle.nn.functional.dropout(x, axis=0)
        >>> y_1 = paddle.nn.functional.dropout(x, axis=1)
        >>> y_01 = paddle.nn.functional.dropout(x, axis=[0,1])
        >>> print(x)
        Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[1., 2., 3.],
         [4., 5., 6.]])
        >>> print(y_train)
        Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[2., 4., 0.],
        [8., 0., 0.]])
        >>> print(y_test)
        Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[1., 2., 3.],
         [4., 5., 6.]])
        >>> print(y_0)
        Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[2., 4., 6.],
         [8. , 10., 12.]])
        >>> print(y_1)
        Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[2. , 4. , 6. ],
         [8. , 10., 12.]])
        >>> print(y_01)
        Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[0., 0., 6.],
         [0., 0., 0.]])
z)p argument should be a number or Variabler   r@   !p argument should between 0 and 1)r4   r3   zBmode argument should be 'downscale_in_infer' or 'upscale_in_train'z/datatype of axis argument should be int or listNr4   downgrade_in_inferdropoutrA   rF   rE   rG   rH   rR   T)rS   r   c                   > Ub  US:X  a  U R                   S:w  a  U R                   n[        U[        5      (       a)  UR                  S/:w  a  [	        STR                   35      eUUUS LUb  UOSTS.nU$ )Nr   r@   zGRequired p.shape == [1] if type(p) is Variable, but received p.shape = )dropout_probis_testfix_seedseeddropout_implementation)random_seedr^   r   r]   r{   )progr   r   r  rZ   rp   ps        rh   	get_attrsdropout.<locals>.get_attrs  s    LDAI43C3Cq3H++D ( "((QC/#abcbibiajk  %1& $D 0$($4D!.2 ri   rT   )r   MaskrV   z(inplace not supported for dropout_nd yet      ?        r   r3   zNaxis value should be greater than or equal to 0 and less than dimensions of x:z, but get axis value: z:length of axis should not be greater than dimensions of x:z, but get length of axis: rG   rS   r   r   r]   
fill_valuerS   re   )r   )*r^   r   r_   r   r	   r   r{   rb   r`   ra   r   r   staticr   r  r   dropout_r   r
   r[   r   rc   rS   r   VarDescVarTypeUINT8main_programrd   NotImplementedErrorr   r   r   r]   r   r   r\   uniformr   greater_equalr   multiply)rA   r  r   traininginplacerp   re   r  rf   rg   maskr  rZ   rS   	keep_probscale_inputinput_shapeinput_shape_tensor	drop_axes
mask_shaper   random_tensor	keep_maskrets    `   `                  rh   r   r     s   x a%h		:;;CDD!c5\""6HUa!e@AA==P
 	
 Jtc4%788IJJ|$(,@$@ d 	 "##}}113??1D}}99;GG L ,D!$  >>(aD   !7fh7F$3CY ;;!'';JC<<ll**00 = D& f111(lDIEaSz!$v6	   J%:    $3CY E	  Q#X||AS11:a%#>#>18||AS11 -- Qa)m4  ''K"$$%+\\!_"",T3"7"7T$ZI9~!S^c+6F6J%J dehiteudv  wL  MP  QZ  M[  L\  \]  ^  9~K 00 PQTU`QaPbb|  ~A  BK  ~L  }M  N  s;//J"$$"A$6q$9JqM # #A$/NJqM # #NN)#M A31I>A,,]A>I ++k9KIu5I//+ytDCJ // Q0 
 J  
 Jri   c                6   US:  d  US:  a  [        SU 35      eU R                  nUS;  a  [        SU S35      eU(       a  [        R                  " S5        US:H  nU(       a  U R                  S5      n [        XSUS	9nU(       a  UR                  S5      nU$ )
a  
Randomly zero out entire 1D channels (feature maps) during training.

Args:
    input: Input tensor of shape [C, L] (2D) or [N, C, L] (3D)
    p: Probability of a channel being zeroed. Default: 0.5
    training: If False, returns input unchanged. Default: True
    inplace: If True, modifies input tensor in-place. Default: False
            WARNING: Currently not implemented (will behave as False).
            TODO: Implement in-place operation in future versions.
            Default: False

Returns:
    Tensor with the same shape as input, where entire channels are zeroed with probability p

Examples:
    .. code-block:: python

        >>> import paddle

        # Case 1: 3D input (batched)
        >>> x = paddle.randn([2, 3, 10])  # [N, C, L]
        >>> y_train = paddle.nn.functional.dropout1d(x, p=0.2)  # Training mode
        >>> y_test = paddle.nn.functional.dropout1d(x, p=0.2, training=False)  # Test mode
        >>> print("Original first channel:", x[0, 0, :])
        >>> print("Train output (may be zeroed):", y_train[0, 0, :])
        >>> print("Test output (always unchanged):", y_test[0, 0, :])

        # Case 2: 2D input (single sample)
        >>> x = paddle.randn([3, 8])  # [C, L]
        >>> y = paddle.nn.functional.dropout1d(x, p=0.5)
        >>> print("Input shape:", x.shape)
        >>> print("Output shape:", y.shape)
        >>> print("Zeroed channels count:", paddle.sum(y == 0).item())
r   r@   z+dropout probability must be in [0, 1], got )rJ   r   z&dropout1d expects 2D or 3D input, got Dzinplace=True is currently not supported in dropout1d and will be ignored. This parameter is reserved for future implementation.rJ   )r  r   r  )rb   r   RuntimeErrorwarningswarnr    r   r   )rj   r  r  r  r   need_squeezeresults          rh   	dropout1dr.    s    R 	1uAFqcJKK::D6CD6KLLD	
 19L" Ua(;F"Mri   c           	         U R                   n[        U5      S:w  a  [        S[        U5       S35      eUS;  a  [        SU S35      e[        U UUS:X  a  SS	/OSS
/USUS9$ )al  
Randomly zero out entire channels (in the batched input 4d tensor with the shape `NCHW` ,
a channel is a 2D feature map with the shape `HW` ). Each channel will be zeroed out independently
on every forward call with probability `p` using samples from a Bernoulli distribution.

See :ref:`api_paddle_nn_functional_dropout` for more details.

Args:
    x (Tensor):  The input is 4-D Tensor with shape [N, C, H, W] or [N, H, W, C].
                 The data type is float16, float32 or float64.
    p (float, optional): Probability of setting units to zero. Default: 0.5.
    training (bool, optional): A flag indicating whether it is in train phrase or not. Default: True.
    data_format (str, optional): Specify the data format of the input, and the data format of the output will be consistent with that of the input. An optional string from `NCHW` or `NHWC` . When it is `NCHW` , the data is stored in the order of: [batch_size, input_channels, input_height, input_width]. Default: `NCHW` .
    name (str, optional): Name for the operation, Default: None. For more information, please refer to :ref:`api_guide_Name`.

Returns:
    A Tensor representing the dropout2d, has same shape and data type as `x` .


Examples:
    .. code-block:: python

        >>> import paddle
        >>> paddle.seed(1)
        >>> x = paddle.randn(shape=(2, 3, 4, 5)).astype(paddle.float32)
        >>> y_train = paddle.nn.functional.dropout2d(x)  #train
        >>> y_test = paddle.nn.functional.dropout2d(x, training=False) #test
        >>> for i in range(2):
        ...     for j in range(3):
        ...         print(x[i,j,:,:])
        ...         print(y_train[i,j,:,:]) # may all 0
        ...         print(y_test[i,j,:,:])
        Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[-0.30557564,  0.11855337,  0.41220093, -0.09968963,  1.50014710],
         [ 1.24004936, -0.92485696,  0.08612321,  1.15149164, -0.09276631],
         [ 1.22873247, -1.46587241, -1.30802727,  0.19496460,  1.73776841],
         [ 0.40092674,  0.67630458,  0.72265440,  1.31720388, -1.41899264]])
        Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[-0.61115128,  0.23710674,  0.82440186, -0.19937925,  3.00029421],
         [ 2.48009872, -1.84971392,  0.17224643,  2.30298328, -0.18553263],
         [ 2.45746493, -2.93174481, -2.61605453,  0.38992921,  3.47553682],
         [ 0.80185348,  1.35260916,  1.44530880,  2.63440776, -2.83798528]])
        Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[-0.30557564,  0.11855337,  0.41220093, -0.09968963,  1.50014710],
         [ 1.24004936, -0.92485696,  0.08612321,  1.15149164, -0.09276631],
         [ 1.22873247, -1.46587241, -1.30802727,  0.19496460,  1.73776841],
         [ 0.40092674,  0.67630458,  0.72265440,  1.31720388, -1.41899264]])
        Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[ 0.88350385, -1.14767575,  0.51043051, -0.10051888, -0.61305630],
         [-0.12084112,  0.48506257, -1.13189507,  0.62806708, -0.80003673],
         [ 0.51513153, -0.08890446,  0.22753835,  0.11557858,  0.78117645],
         [ 1.47505593,  0.84618902, -0.38528305, -1.05887091,  0.16592593]])
        Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[ 1.76700771, -2.29535151,  1.02086103, -0.20103776, -1.22611260],
         [-0.24168225,  0.97012514, -2.26379013,  1.25613415, -1.60007346],
         [ 1.03026307, -0.17780893,  0.45507669,  0.23115715,  1.56235290],
         [ 2.95011187,  1.69237804, -0.77056611, -2.11774182,  0.33185187]])
        Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[ 0.88350385, -1.14767575,  0.51043051, -0.10051888, -0.61305630],
         [-0.12084112,  0.48506257, -1.13189507,  0.62806708, -0.80003673],
         [ 0.51513153, -0.08890446,  0.22753835,  0.11557858,  0.78117645],
         [ 1.47505593,  0.84618902, -0.38528305, -1.05887091,  0.16592593]])
        Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[-1.46668839, -0.38117948,  1.18678427,  0.38740095,  0.29117522],
         [-0.13538910, -0.14527084, -0.04912176, -0.26063353,  0.23640174],
         [ 0.45643106,  0.60587281, -1.03242552, -0.45319262, -1.57911122],
         [-0.08732958, -0.75898546,  0.14563090, -1.73751652, -0.89109969]])
        Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[-0., -0., 0. , 0. , 0. ],
         [-0., -0., -0., -0., 0. ],
         [0. , 0. , -0., -0., -0.],
         [-0., -0., 0. , -0., -0.]])
        Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[-1.46668839, -0.38117948,  1.18678427,  0.38740095,  0.29117522],
         [-0.13538910, -0.14527084, -0.04912176, -0.26063353,  0.23640174],
         [ 0.45643106,  0.60587281, -1.03242552, -0.45319262, -1.57911122],
         [-0.08732958, -0.75898546,  0.14563090, -1.73751652, -0.89109969]])
        Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[-0.32110816, -0.76044011,  0.34456784, -0.39410326,  0.37896338],
         [ 0.52747023,  0.72711533,  0.29204839,  0.72493637,  0.31128070],
         [ 0.58046782, -1.78499067, -1.67504823, -0.38590902, -0.26243693],
         [ 0.96669912,  0.43670532, -0.38109761,  0.78405094, -2.17882323]])
        Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[-0., -0., 0. , -0., 0. ],
         [0. , 0. , 0. , 0. , 0. ],
         [0. , -0., -0., -0., -0.],
         [0. , 0. , -0., 0. , -0.]])
        Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[-0.32110816, -0.76044011,  0.34456784, -0.39410326,  0.37896338],
         [ 0.52747023,  0.72711533,  0.29204839,  0.72493637,  0.31128070],
         [ 0.58046782, -1.78499067, -1.67504823, -0.38590902, -0.26243693],
         [ 0.96669912,  0.43670532, -0.38109761,  0.78405094, -2.17882323]])
        Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[ 0.17168395,  0.45112833,  0.63307828,  2.38763475, -1.27247131],
         [ 0.56171960, -1.09584677,  0.38300961, -0.57512099,  0.31011426],
         [-0.95336407, -1.04852903, -0.21312937, -0.53549880, -0.00074209],
         [ 2.22819090,  1.12403083, -0.04198794, -1.51167727, -0.42699185]])
        Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[0. , 0. , 0. , 0. , -0.],
         [0. , -0., 0. , -0., 0. ],
         [-0., -0., -0., -0., -0.],
         [0. , 0. , -0., -0., -0.]])
        Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[ 0.17168395,  0.45112833,  0.63307828,  2.38763475, -1.27247131],
         [ 0.56171960, -1.09584677,  0.38300961, -0.57512099,  0.31011426],
         [-0.95336407, -1.04852903, -0.21312937, -0.53549880, -0.00074209],
         [ 2.22819090,  1.12403083, -0.04198794, -1.51167727, -0.42699185]])
        Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[ 0.62503546, -0.20989063, -0.22046235, -0.38679042, -1.02590704],
         [ 1.04561794,  1.08428383, -0.52219963, -1.56003857,  0.89213932],
         [-0.16578521,  0.14524542, -0.45563069,  0.48180851,  1.35843253],
         [ 1.07669640, -0.84535235, -1.18651557,  0.79144061, -0.45565742]])
        Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[0. , -0., -0., -0., -0.],
         [0. , 0. , -0., -0., 0. ],
         [-0., 0. , -0., 0. , 0. ],
         [0. , -0., -0., 0. , -0.]])
        Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[ 0.62503546, -0.20989063, -0.22046235, -0.38679042, -1.02590704],
         [ 1.04561794,  1.08428383, -0.52219963, -1.56003857,  0.89213932],
         [-0.16578521,  0.14524542, -0.45563069,  0.48180851,  1.35843253],
         [ 1.07669640, -0.84535235, -1.18651557,  0.79144061, -0.45565742]])
rI   z*dimensions of x should be 4, but received z != 4r   zJAttr(data_format) should be 'NCHW' or 'NHWC'. Received Attr(data_format): r   r   r   r@   r   r3   r  r   r  rp   re   r]   r\   rb   r   rA   r  r  rw   re   r   s         rh   	dropout2dr3  E  s    D ''K
;18[9I8J%P
 	
 **""-a1
 	

 	
"f,aV1a& ri   c           	         U R                   n[        U5      S:w  a  [        S[        U5       S35      eUS;  a  [        SU S35      e[        U UUS:X  a  SS	/OSS
/USUS9$ )ae  
Randomly zero out entire channels (in the batched input 5d tensor with the shape `NCDHW` ,
a channel is a 3D feature map with the shape `DHW` ). Each channel will be zeroed out independently
on every forward call with probability `p` using samples from a Bernoulli distribution.

See :ref:`api_paddle_nn_functional_dropout` for more details.

Args:
    x (Tensor):  The input is 5-D Tensor with shape [N, C, D, H, W] or [N, D, H, W, C].
                 The data type is float32 or float64.
    p (float, optional): Probability of setting units to zero. Default: 0.5.
    training (bool, optional): A flag indicating whether it is in train phrase or not. Default: True.
    data_format (str, optional): Specify the data format of the input, and the data format of the output will be consistent with that of the input. An optional string from ``NCDHW`` or ``NDHWC``. When it is ``NCDHW`` , the data is stored in the order of: [batch_size, input_channels, input_depth, input_height, input_width]. Default: ``NCDHW`` .
    name (str, optional): Name for the operation, Default: None. For more information, please refer to :ref:`api_guide_Name`.

Returns:
    A Tensor representing the dropout3d, has same shape and data type with `x` .


Examples:
    .. code-block:: python

        >>> import paddle

        >>> x = paddle.randn(shape=(2, 3, 4, 5, 6)).astype(paddle.float32)
        >>> y_train = paddle.nn.functional.dropout3d(x)  #train
        >>> y_test = paddle.nn.functional.dropout3d(x, training=False) #test
        >>> print(x[0,0,:,:,:])
        >>> print(y_train[0,0,:,:,:]) # may all 0
        >>> print(y_test[0,0,:,:,:])

r   z*dimensions of x should be 5, but received z != 5r   zLAttr(data_format) should be 'NCDHW' or 'NDHWC'. Received Attr(data_format): r   r   r   r@   rI   r3   r0  r1  r2  s         rh   	dropout3dr5    s    P ''K
;18[9I8J%P
 	
 ,,""-a1
 	

 	
"g-aVAq6 ri   c                   [        U[        [        45      (       d  [        S5      eUS:  d  US:  a  [	        S5      e[        5       (       d  [        U S/ SQS5        U(       Ga  US:X  a  [        R                  " U SS	9$ S
nSnU* U-  nSU-
  SX'S-  -  -   -  S-  nU* U-  U-  n	U R                  n
U(       d  U R                  nOPU R                  S:  a  [	        S5      e[        U R                  S S 5      S/[        U R                  SS  5      -  -   n[        R                  " USSSS9n[        XSS9n[        R                   " X5      n[        R"                  " X5      n[        R$                  " [        USU
S9U5      n[        XU
S9n	[        R&                  " [        R(                  " X5      [        R                  " XS	95      n[        R&                  " [        R                  " XS	9XS9nU$ U $ )Nz#p argument should be a float or intr   r@   r   rA   r   alpha_dropoutr
  r  g,x?g2֫?rJ   g      z.Feature alpha dropout needs at least 2D input.rG   r	  r  r  r  )r^   r   r_   r{   rb   r   r   r   r   rS   r]   r   r`   r\   r  r   r  r   subtractaddr  )rA   feature_dropoutr  r  re   alphar   alpha_pabrS   r   r$  r%  	drop_maskyress                    rh   _feature_alpha_dropout_implrB    s    a%&&=>>1uA<== s?	
 6<<--11&5.!eA
N*+4BL1''Kvvz D  qwwr{+qcC4D.DDK ycs
 {	B((:	KK	1	OO{s%@)
	
 {>JJOOA)LL2
 jja11@
ri   c                    [        U SXUS9$ )a  
Alpha Dropout is a type of Dropout that maintains the self-normalizing property.
For an input with zero mean and unit standard deviation, the output of Alpha Dropout
maintains the original mean and standard deviation of the input.
Alpha Dropout fits well to SELU activate function by randomly setting activations to the negative saturation value.

Args:
    x (Tensor): The input tensor. The data type is bfloat16, float16, float32 or float64.
    p (float | int, optional): Probability of setting units to zero. Default 0.5.
    training (bool, optional): A flag indicating whether it is in train phrase or not. Default True.
    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 representing the dropout, has same shape and data type as `x`.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> paddle.seed(1)
        >>> x = paddle.to_tensor([[-1, 1], [-1, 1]]).astype(paddle.float32)
        >>> y_train = paddle.nn.functional.alpha_dropout(x, 0.5)
        >>> y_test = paddle.nn.functional.alpha_dropout(x, 0.5, training=False)
        >>> print(y_train)
        Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[-0.77919382,  1.66559887],
        [-0.10721093, -0.77919382]])
        >>> print(y_test)
        Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[-1.,  1.],
        [-1.,  1.]])
Fr:  r  r  re   rB  rA   r  r  re   s       rh   r7  r7  W  s    L '	5At ri   c                    [        U SXUS9$ )aP  
A channel is a feature map, Feature Alpha Dropout randomly masks out entire channels.
Alpha Dropout is a type of Dropout that maintains the self-normalizing property.
For an input with zero mean and unit standard deviation, the output of Alpha Dropout
maintains the original mean and standard deviation of the input.
Alpha Dropout fits well to SELU activate function by randomly setting activations to the negative saturation value.

Args:
    x (Tensor): The input tensor. The data type is bfloat16, float16, float32 or float64.
    p (float | int, optional): Probability of setting units to zero. Default 0.5.
    training (bool, optional): A flag indicating whether it is in train phrase or not. Default True.
    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 representing the dropout, has same shape and data type as `x`.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> paddle.seed(1)
        >>> x = paddle.to_tensor([[-1, 1], [-1, 1]]).astype(paddle.float32)
        >>> y_train = paddle.nn.functional.feature_alpha_dropout(x, 0.5)
        >>> y_test = paddle.nn.functional.feature_alpha_dropout(x, 0.5, training=False)
        >>> print(y_train)
        Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[-0.77919382,  1.66559887],
        [-0.10721093, -0.77919382]])
        >>> print(y_test)
        Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[-1.,  1.],
        [-1.,  1.]])
TrD  rE  rF  s       rh   feature_alpha_dropoutrH    s    N '	41d ri   c                |
   US;   d   SU S35       e[        U R                  5      n[        5       (       aE  [        U[        [
        R                  45      (       a   UR                  S:X  a  U R                  5       $ US:X  Ga  [        U[        [        45      (       Ga  [        U5      US-
  S-  :w  Ga  UnUn	[        U5      n
U
SU-  :  a(  SU-  U
-
  nU[        U[        5      (       a  S/OSU-  -   n[        U5      US-  :X  a  U(       d8  [        SU-  S-
  S	S	5       Vs/ s H  nUS-  S:X  a  XS-
     OXS-      PM     nn[        5       (       a"  [        R                  " X[        U	5      5      nU$ [        5       (       a`  [        U	[
        R                   R"                  5      (       a  [        R                  " XU	5      $ [        R                  " X[        U	5      5      $ [%        U S
/ SQS5        ['        U	S[        [(        [        4S5        [        U	[(        5      (       a  [        U	5      n	[+        S/0 [-        5       D6nUR/                  S
S9nUR1                  U5      nUR3                  SSU 0SU0XS.S9  U$ US;   d
   SU 35       eUc  US:X  a  SnOUS:X  a  SnOUS:X  a  SnUR5                  5       nUS;   d
   SU 35       eSS/SS/SS/S.nUUU   ;   d   S U S!UU    S"U 35       e/ n[        U[        [         R"                  45      (       a  US#;   aT  SnUS:X  a$  [7        [9        S$S%S&9U/SS'9nSS/n[;        U US'9n GOUS:X  a!  [7        U[9        S(S%S&9/SS'9nS/n[;        U US'9n OUS);   aR  SnUS:X  a#  [7        [9        S$S%S&9U/SS'9nSS/n[;        U US'9n OUS:X  a!  [7        U[9        S(S%S&9/SS'9nS/n[;        U US'9n O[        U5      nUS#;   a;  SnUS:X  a  SSSS/UQnSS/n[;        U US'9n O\US:X  a  / UQSPSPnS/n[;        U US'9n O@US);   a:  SnUS:X  a  SSSS/UQnSS/n[;        U US'9n OUS:X  a  / UQSPSPnS/n[;        U US'9n [=        5       (       a>  [        U[        5      (       a  UR?                  5       n[        R@                  " XX#U5      nOwX#US*.nSU /0n[        U[        5      (       a  U/US+'   / US,'   OUUS,'   [+        S00 [-        5       D6nUR/                  S.S9nUR1                  U5      nUR3                  S-USU0US9  [        U5      S:w  a
  [C        UUS'9nU$ s  snf )1a)#  
Pad tensor according to ``'pad'`` and ``'mode'``.

Note:
    1. Denote ``'x'``'s dimension as N (same in the following). If mode is ``'constant'``, the length
    of ``'pad'`` can be any even number less than or equal to 2*N.

    2. When mode is ``'constant'``, and ``'pad'`` is a list or tuple, and the length of ``'pad'`` is not
    equal to 2*(N - 2):
    2.1. If the length of ``'pad'`` is 2*N, the order of padding can be customized by ``'pad_from_left_axis'``.
    if ``'pad_from_left_axis'`` is True, then the padding order will be started from the first dimension of
    ``'x'`` and moving backward according to ``'pad'``; else if ``'pad_from_left_axis'`` is False, then the
    padding order will be started from the last dimension of ``'x'`` and moving forward according to ``'pad'``.
    2.2. Otherwise, the padding will be started from the last dimension.

    3. When mode is any of ``'reflect'``, ``'replicate'``, ``'circular'``, or ``'pad'`` is a tensor, or the
    length of ``'pad'`` is 2*(N - 2), and the dimension of ``'x'`` only supports 3-D, 4-D and 5-D.
    In these cases, input ``'x'`` will be padded on [D, H, W] axes according to ``'data_format'``. It will pad
    from the last dimension to the first dimension of [D, H, W] axes.
    Specifically, if N = 3, then the pad has the form (pad_left, pad_right); if N = 4, then the pad has the form
    (pad_left, pad_right, pad_top, pad_bottom); if N = 5, then the pad has the form (pad_left, pad_right,
    pad_top, pad_bottom, pad_front, pad_back).

    4. If mode is ``'reflect'``, pad[0] and pad[1] must be no greater than width-1. The height and depth
    dimension has the same condition.

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


Args:
    x (Tensor): The input tensor with data type float32, float64, int32, int64, complex64 or complex128.
    input: An alias for ``x`` , with identical behavior.
    pad (Tensor|list[int]|tuple[int]): The padding size with data type int. Refer to Note for details.
    mode (str, optional): Four modes: ``'constant'`` (default), ``'reflect'``, ``'replicate'``, ``'circular'``. Default is ``'constant'``.

       - 'constant' mode, uses a constant value to pad the input tensor.
       - 'reflect' mode, uses reflection of the input boundaries to pad the input tensor.
       - 'replicate' mode, uses input boundaries to pad the input tensor.
       - 'circular' mode, uses circular input to pad the input tensor.

    value (float, optional): The value to fill the padded areas in 'constant' mode . Default is :math:`0.0`.
    data_format (str, optional): An string from: ``'NCL'``, ``'NLC'``, ``'NHWC'``, ``'NCHW'``, ``'NCDHW'``, ``'NDHWC'``. Specify the data format of
       the input data when: 1. mode is any of ``'reflect'``, ``'replicate'`` or ``'circular'``; or 2. the input ``'pad'`` is a tensor;
       or 3. the length of ``'pad'`` is ``2*(x.ndim - 2)``. The default value is None, which means it will be automatically inferred from the
       input dimension of ``'x'``. When ``'x'`` is a 3-D Tensor, data_format will be set to ``'NCL'``; When ``'x'`` is a 4-D Tensor,
       data_format will be set to ``'NCHW'``; When ``'x'`` is a 5-D Tensor, data_format will be set to ``'NCDHW'``.
    pad_from_left_axis (bool, optional): The parameter is only valid when mode is ``'constant'`` and the input ``'pad'`` is
       length of ``'pad'`` is ``2*x.ndim``, the order of padding can be customized. If True, the padding will be started from
       the first axis of ``'x'``; if False, it will be started from the last axis of ``'x'``. Default: True.
    name (str|None, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: ``'None'``.

Returns:
    Tensor, a Tensor padded according to pad and mode and data type is same as input.

Example:

    .. code-block:: text

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

        Case 0:
            pad = [0, 0, 0, 0, 0, 0, 1, 1, 0, 0],
            mode = 'constant'
            value = 0
            pad_from_left_axis = True
            Out = [[[[[0., 0., 0.],
                      [1., 2., 3.],
                      [4., 5., 6.],
                      [0., 0., 0.]]]]]
            Out.shape = [1, 1, 1, 4, 3]

        Case 1:
            pad = [0, 0, 0, 0, 0, 0, 1, 1, 0, 0],
            mode = 'constant'
            value = 0
            pad_from_left_axis = False
            Out = [[[[[0., 0., 0.],
                      [0., 0., 0.]]],
                    [[[1., 2., 3.],
                      [4., 5., 6.]]],
                    [[[0., 0., 0.],
                      [0., 0., 0.]]]]]
            Out.shape = [1, 3, 1, 2, 3]

        Case 3:
            pad = [1, 0, 0, 1],
            mode = 'constant'
            value = 0
            Out = [[[[[0., 1., 2., 3.],
                      [0., 4., 5., 6.],
                      [0., 0., 0., 0.]]]]]
            Out.shape = [1, 1, 1, 3, 4]

        Case 4:
            pad = [2, 2, 1, 1, 0, 0],
            mode = 'constant'
            value = 0
            Out = [[[[[0. 0. 0. 0. 0. 0. 0.]
                      [0. 0. 1. 2. 3. 0. 0.]
                      [0. 0. 4. 5. 6. 0. 0.]
                      [0. 0. 0. 0. 0. 0. 0.]]]]]
            Out.shape = [1, 1, 1, 4, 7]

        Case 5:
            pad = [2, 2, 1, 1, 0, 0],
            mode = 'reflect'
            Out = [[[[[6. 5. 4. 5. 6. 5. 4.]
                      [3. 2. 1. 2. 3. 2. 1.]
                      [6. 5. 4. 5. 6. 5. 4.]
                      [3. 2. 1. 2. 3. 2. 1.]]]]]
            Out.shape = [1, 1, 1, 4, 7]

        Case 6:
            pad = [2, 2, 1, 1, 0, 0],
            mode = 'replicate'
            Out = [[[[[1. 1. 1. 2. 3. 3. 3.]
                      [1. 1. 1. 2. 3. 3. 3.]
                      [4. 4. 4. 5. 6. 6. 6.]
                      [4. 4. 4. 5. 6. 6. 6.]]]]]
            Out.shape = [1, 1, 1, 4, 7]

        Case 7:
            pad = [2, 2, 1, 1, 0, 0],
            mode = 'circular'
            Out = [[[[[5. 6. 4. 5. 6. 4. 5.]
                      [2. 3. 1. 2. 3. 1. 2.]
                      [5. 6. 4. 5. 6. 4. 5.]
                      [2. 3. 1. 2. 3. 1. 2.]]]]]
            Out.shape = [1, 1, 1, 4, 7]

Examples:
    .. code-block:: python

        >>> import paddle
        >>> import paddle.nn.functional as F

        >>> # example 1
        >>> x_shape = (1, 1, 3)
        >>> x = paddle.arange(paddle.prod(paddle.to_tensor(x_shape)), dtype="float32").reshape(x_shape) + 1
        >>> y = F.pad(x, [0, 0, 0, 0, 2, 3], value=1, mode='constant', data_format="NCL")
        >>> print(y)
        Tensor(shape=[1, 1, 8], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[[1., 1., 1., 2., 3., 1., 1., 1.]]])

        >>> # example 2
        >>> x_shape = (1, 1, 3)
        >>> x = paddle.arange(paddle.prod(paddle.to_tensor(x_shape)), dtype="float32").reshape(x_shape) + 1
        >>> y = F.pad(x, [2, 3], value=1, mode='constant', data_format="NCL")
        >>> print(y)
        Tensor(shape=[1, 1, 8], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[[1., 1., 1., 2., 3., 1., 1., 1.]]])

        >>> # example 3
        >>> x_shape = (1, 1, 2, 3)
        >>> x = paddle.arange(paddle.prod(paddle.to_tensor(x_shape)), dtype="float32").reshape(x_shape) + 1
        >>> y = F.pad(x, [1, 2, 1, 1], value=1, mode='circular')
        >>> print(y)
        Tensor(shape=[1, 1, 4, 6], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[[[6., 4., 5., 6., 4., 5.],
           [3., 1., 2., 3., 1., 2.],
           [6., 4., 5., 6., 4., 5.],
           [3., 1., 2., 3., 1., 2.]]]])

        >>> # example 4
        >>> x_shape = (1, 1, 3)
        >>> x = paddle.arange(paddle.prod(paddle.to_tensor(x_shape)), dtype="float32").reshape(x_shape) + 1
        >>> y = F.pad(x, [1, 0, 0, 1, 0, 0], value=0, mode='constant', pad_from_left_axis=True)
        >>> print(y)
        Tensor(shape=[2, 2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[[0., 0., 0.],
          [0., 0., 0.]],
         [[1., 2., 3.],
          [0., 0., 0.]]])

        >>> # example 5
        >>> x_shape = (1, 1, 3)
        >>> x = paddle.arange(paddle.prod(paddle.to_tensor(x_shape)), dtype="float32").reshape(x_shape) + 1
        >>> y = F.pad(x, [1, 0, 0, 1, 0, 0], value=0, mode='constant', pad_from_left_axis=False)
        >>> print(y)
        Tensor(shape=[1, 2, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[[0., 1., 2., 3.],
          [0., 0., 0., 0.]]])

        >>> # example 6
        >>> x_shape = (1, 1, 3)
        >>> x = paddle.arange(paddle.prod(paddle.to_tensor(x_shape)), dtype="float32").reshape(x_shape) + 1
        >>> y = F.pad(x, [1, 0, 0, 1], value=0, mode='constant')
        >>> print(y)
        Tensor(shape=[1, 2, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[[0., 1., 2., 3.],
          [0., 0., 0., 0.]]])
)r7   r8   r6   r9   zFmode should be one of constant, reflect, replicate, circular, but got r   r   r6   rJ   )r   r@   r   rA   )rF   rG   rH   r   int64	complex64
complex128rE   pad	pad_valuer   rT   r   )rQ   rN  rV   )r   rI   r   z4input tensor dimension must be in [3, 4, 5] but got r   NCLrI   r   r   r   )rO  r   r   NLCr   r   zNdata_format should be in one of [NCL, NCHW, NCDHW, NLC, NHWC, NDHWC], but got rP  r   r   zinput tensor dimension is z , it's data format should be in z	 but got )rO  r   r   )rI   r   rR   r   )rJ   )rP  r   r   )rp   r}   rw   PaddingsrQ   pad3drj   rM  )rS  )"r\   r]   r   r^   r   r   r#   rn   cloner`   ra   r   r   rM  r   r   r	   r   r   r   r_   r
   r[   r   rc   rd   r   r   r   r    r   tolistrS  r   )rA   rM  rp   r}   rw   pad_from_left_axisre   x_dimrQ   rN  padding_lenpad_len_for_paddingsr   rg   rf   rS   supported_format_mapunsqueezed_dimrZ   rX   s                       rh   rM  rM    s#   Z     QQUPVVWX  LEcHfmm455#((a-779
sT5M**Ha'	(mU"#$u9{#: *S$*?*?A3T$# H
 CEAI%*< q5y1}b"55A $%q5A:Q8E?B5  
 **Q%	*:;CJ==)VZZ%5%566zz!y99zz!uY/?@@ 	 	
  	9kE3+A5Ii%%i(I/fh/""C"877>8CL'@	 	 	
 
   F 
>eWE	F  A:KaZ KaZ!K##%KJJ 	-	!J
 5>FW
 .u55 
$UG+KL`afLgKhhqr}q~5 N##)),--22!KzeD8#>QG"#Qan5!c5W#=>QG"#an544!KzeD8#>QG"#Qan5!c5W#=>QG"#an53i22!Kz!Q(C("#Qan5!"lQll"#an544!Kz!Q(C("#Qan5!"lQll"#an5c8$$**,Cll14<kJsc8$$"%F: "E* #E*11""G"<77>%U 	 	
 >ac/Jas   "!T9z3.0.0zpaddle.nn.ZeroPad2DzPlease use class ZeroPad2D)since	update_tolevelreasonpaddingc           	         [        U USSUUS9$ )a  
Pads the input tensor boundaries with zero according to 'pad'.

Args:
    x(Tensor): The input tensor with data type float16/float32/float64/int32/int64.
    padding(int | Tensor | List[int] | Tuple[int]): The padding size with data type int.
        The input dimension should be 4 and pad has the form (pad_left, pad_right,
        pad_top, pad_bottom).
    data_format(str, optional): An string from: "NHWC", "NCHW". Specify the data format of
        the input data. Default: "NCHW".
    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:
    Tensor, padded with 0 according to pad and data type is same as input.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> import paddle.nn.functional as F

        >>> x_shape = paddle.to_tensor([1, 1, 2, 3])
        >>> x = paddle.arange(paddle.prod(x_shape), dtype="float32").reshape(x_shape) + 1
        >>> y = F.zeropad2d(x, [1, 2, 1, 1])
        >>> print(y)
        Tensor(shape=[1, 1, 4, 6], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[[[0., 0., 0., 0., 0., 0.],
           [0., 1., 2., 3., 0., 0.],
           [0., 4., 5., 6., 0., 0.],
           [0., 0., 0., 0., 0., 0.]]]])
r6   r   )rM  rp   r}   rw   re   rT  )rA   ra  rw   re   s       rh   	zeropad2drc  -	  s$    Z 	 ri   r   c                X   U R                   U   S:X  d  UR                   U   S:X  a  [        [        R                  " X5      US9$ [        R                  " U R                   U   /UR                   U   /5      n[        [        R                  " X5      US9n[        [        R                  " X 5      US9n[        [        R                  " X5      US9nUS   U R                   U   -  US   UR                   U   -  pUS:w  a  Xh-  nU	S:w  a  Xy-  n[        [        Xg-  X3-  S95      n
XZ-  nU$ )a  
Compute cosine similarity between x1 and x2 along axis.

Parameters:
    x1 (Tensor): First input. float32/double.
    x2 (Tensor): Second input. float32/double.
    axis (int, optional): Dimension of vectors to compute cosine similarity. Default is 1.
    eps(float, optional): Small value to avoid division by zero. Default is 1e-8.

Returns:
    Tensor, a Tensor representing cosine similarity between x1 and x2 along axis.

Examples:
    .. code-block:: text

        Case 0:
            x1 = [[0.8024077  0.9927354  0.27238318 0.8344984 ]
                 [0.48949873 0.5797396  0.65444374 0.66510963]
                 [0.1031398  0.9614342  0.08365563 0.6796464 ]
                 [0.10760343 0.7461209  0.7726148  0.5801006 ]]
            x2 = [[0.62913156 0.1536727  0.9847992  0.04591406]
                 [0.9098952  0.15715368 0.8671125  0.3156102 ]
                 [0.4427798  0.54136837 0.5276275  0.32394758]
                 [0.3769419  0.8535014  0.48041078 0.9256797 ]]
            axis = 1
            eps = 1e-8
            Out: [0.5275037  0.8368967  0.75037485 0.9245899]

Code Examples:
    .. code-block:: python

        >>> import paddle
        >>> import paddle.nn as nn

        >>> paddle.seed(1)
        >>> x1 = paddle.randn(shape=[2, 3])
        >>> x2 = paddle.randn(shape=[2, 3])

        >>> result = paddle.nn.functional.cosine_similarity(x1, x2, axis=0)
        >>> print(result)
        Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
        [ 0.97689527,  0.99996042, -0.55138415])

r   rQ  r@   )r   )r]   r   r   r  broadcast_shaper   r   )r   r   r   epsbsw12w1w2m1m2n12cos_sims               rh   cosine_similarityro  d	  s   ` 
xx~bhhtn16??2*66			$ 0288D>2B	CB
foob%D
1C	V__R$4	0B	V__R$4	0BURXXd^#RURXXd^%;	QwW	QwW
tBG+
,CiGNri   c                   [        5       (       a  [        R                  " XU5      $ [        5       (       a3  [        R                  " XSS5      nUb  [        R
                  " XB5      $ U$ [        S0 [        5       D6nU R                  n[        U S/ SQS5        [        US/ SQS5        U /U/S.nSSS.nUR                  U5      n	UR                  SUS	U	0US
9  Ub/  UR                  U5      n
UR                  SU	/U/S.S	U
/0SS0S
9  U
$ U	n
U
$ )a2
  

Fully-connected linear transformation operator. For each input :math:`X` ,
the equation is:

.. math::

    Out = XW + b

where :math:`W` is the weight and :math:`b` is the bias.

If the weight is a 2-D tensor of shape :math:`[in\_features, out\_features]` ,
input should be a multi-dimensional tensor of shape
:math:`[batch\_size, *, in\_features]` , where :math:`*` means any number of
additional dimensions. The linear operator multiplies input tensor with
weight and produces an output tensor of shape :math:`[batch\_size, *, out\_features]` ,
If :math:`bias` is not None, the bias should be a 1-D tensor of shape
:math:`[out\_features]` and will be added to the output.

Parameters:
    x (Tensor): Input tensor. The data type should be bfloat16, float16, float32 or float64.
    weight (Tensor): Weight tensor. The data type should be float16, float32 or float64.
    bias (Tensor, optional): Bias tensor. The data type should be float16, float32 or float64.
                             If it is set to None, no bias will be added to the output units.
    name (str, optional): Normally there is no need for user to set this parameter.
                          For detailed information, please refer to :ref:`api_guide_Name` .

Returns:
    Tensor, the shape is :math:`[batch\_size, *, out\_features]` and the
    data type is the same with input :math:`x` .

Examples:
    .. code-block:: python

        >>> import paddle
        >>> paddle.seed(2023)
        >>> x = paddle.randn((3, 2), dtype="float32")
        >>> print(x)
        Tensor(shape=[3, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[ 0.06132207,  1.11349595],
         [ 0.41906244, -0.24858207],
         [-1.85169315, -1.50370061]])
        >>> weight = paddle.full(shape=[2, 4], fill_value=0.5, dtype="float32", name="weight")
        >>> print(weight)
        Tensor(shape=[2, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[0.50000000, 0.50000000, 0.50000000, 0.50000000],
         [0.50000000, 0.50000000, 0.50000000, 0.50000000]])
        >>> bias = paddle.ones(shape=[4], dtype="float32", name="bias")
        >>> print(bias)
        Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
        [1., 1., 1., 1.])
        >>> y = paddle.nn.functional.linear(x, weight, bias)
        >>> print(y)
        Tensor(shape=[3, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[ 1.58740902,  1.58740902,  1.58740902,  1.58740902],
         [ 1.08524013,  1.08524013,  1.08524013,  1.08524013],
         [-0.67769694, -0.67769694, -0.67769694, -0.67769694]])
Fr,   rA   rD   rS   )rT   rU   )trans_xtrans_y	matmul_v2r   rV   elementwise_addr   r   )r,   )r   r   r,   r   matmulr9  r
   r[   rS   r   r   rc   rd   )rA   r   r   re   rg   rf   rS   rX   rZ   tmprA  s              rh   r,   r,   	  s<   @ }}Q--	mmAue4::c((J22 s?	
 	7		
 s&*!e477>CL	 	 	
 ;;EBC&!U$0rl	   
 C
ri   c           	     t   US:  d  US:  a  [        S5      e[        5       (       a   [        R                  " X[	        U5      5      $ [        U S/ SQS5        [        S0 [        5       D6nSU l        UR                  U R                  5      nUR                  SU(       a  XS.OS	U 0S
U0S[	        U5      0S9  U$ )ap	  
Label smoothing is a mechanism to regularize the classifier layer and is called
label-smoothing regularization (LSR).Label smoothing is proposed to encourage
the model to be less confident, since optimizing the log-likelihood of the
correct label directly may cause overfitting and reduce the ability of the
model to adapt.

Label smoothing replaces the ground-truth label :math:`y` with the weighted sum
of itself and some fixed distribution :math:`\mu`. For class :math:`k`,
i.e.

.. math::

    \\tilde{y_k} = (1 - \epsilon) * y_k + \epsilon * \mu_k,

where :math:`1 - \epsilon` and :math:`\epsilon` are the weights
respectively, and :math:`\\tilde{y}_k` is the smoothed label. Usually
uniform distribution is used for :math:`\mu`.

See more details about label smoothing in https://arxiv.org/abs/1512.00567.

Parameters:
    label(Tensor): The input variable containing the label data. The
                    label data should use one-hot representation. It's
                    a multidimensional tensor with a shape of
                    :math:`[N_1, ..., Depth]`, where Depth is class number. The dtype can be "float16" "float32" and "float64".
    prior_dist(Tensor, optional): The prior distribution to be used to smooth
                    labels. If not provided, an uniform distribution
                    is used. It's a multidimensional tensor with a shape of
                    :math:`[1, class\_num]` . The default value is None.
    epsilon(float, optional): The weight used to mix up the original ground-truth
                    distribution and the fixed distribution. The default value is
                    0.1.
    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:
    Tensor: The tensor containing the smoothed labels.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> paddle.disable_static()

        >>> x = paddle.to_tensor([[[0, 1, 0],
        >>>                     [ 1,  0, 1]]], dtype="float32", stop_gradient=False)

        >>> output = paddle.nn.functional.label_smooth(x)
        >>> print(output)
        Tensor(shape=[1, 2, 3], dtype=float32, place=Place(cpu), stop_gradient=False,
        [[[0.03333334, 0.93333334, 0.03333334],
        [0.93333334, 0.03333334, 0.93333334]]])
r	  r
  z-The value of epsilon must be between 0 and 1.labelrD   label_smoothT)rT   	PriorDistrT   r   epsilonrV   )ry  )rb   r   r   ry  r   r   r
   r[   r   rc   rS   rd   )rx  
prior_distr{  re   rf   smooth_labels         rh   ry  ry  
  s    z }#HII""5eGnEE3	 4684FE<<U[[IL
  1u%%.)  	 ri   c                   USL d#  Ub   [        US5      (       d  [        SU S35      e[        US5      (       a  UR                  5       (       d  gSnSnSnUSLau  [        R                  " 5       (       a[  [
        R                  R                  5       nUR                  nUc  UOUR                  U5      nUc  UR                  OUR                  nX!:  a  [        SU S	U 35      eSn	[        U R                  5       H  n
X-  n	M	     U	S
:w  a  U	S:  a  [        SU	 S35      e[        [        U R                  5      5      nUS:w  a  [        SU S35      eSnUb  US:X  a,  [        5       R                   S:w  a  [        5       R                   n[#        5       (       a(  [$        R&                  " U UUUUUUSLUb  U5      $ S5      $ [)        U SSS/S5        Sn[+        U40 [-        5       D6nUR/                  U R0                  S9nUR/                  U R0                  S9nUR3                  USU 0UUS.UUUUUUSLUb  UOSS.S9  UU4$ )a.  
Class center sample method is proposed from the paper PartialFC that only sample a subset of the class centers.
The process of sampling subset class centers is straightforward:

1. First select the positive class centers;
2. Then randomly sample negative class centers.

Specifically, given a label tensor, shape [batch_size], select all the positive class centers and randomly
sample negative class centers, then remap the input label tensor using the sampled class centers.

For more information, Partial FC: Training 10 Million Identities on a Single Machine
arxiv: https://arxiv.org/abs/2010.05222

Note:
    If the number of the positive class centers is greater than the input num_samples, it keeps all the positive
    class centers and the shape of sampled_class_center will be [num_positive_class_centers].

    The API supports CPU, single GPU and multi GPU.

    For data parallel mode, set ``group=False``.

    For model parallel mode, set ``group=None`` or the group instance return by paddle.distributed.new_group.

Args:
    label (Tensor): 1-D tensor with shape [N], each label in [0, num_classes)
    num_classes (int): A positive integer to specify the number of classes at local rank.
        Note that num_classes of each GPU can be different.
    num_samples (int): A positive integer to specify the number of class center to sample.
    group (Group, optional): The group instance return by paddle.distributed.new_group
        or ``None`` for global default group or ``False`` for data parallel (do not communication cross ranks).
        Default is ``None``.

Returns:
    Tuple of two ``Tensor`` : (remapped_label, sampled_class_center), remapped label using sampled class center,
    sampled class center from [0, num_classes).

Examples:

.. code-block:: python
    :name: code-example1

    >>> # CPU or single GPU
    >>> import paddle
    >>> num_classes = 20
    >>> batch_size = 10
    >>> num_samples = 6
    >>> paddle.seed(2023)
    >>> label = paddle.randint(low=0, high=num_classes, shape=[batch_size], dtype='int64')
    >>> remapped_label, sampled_class_index = paddle.nn.functional.class_center_sample(label, num_classes, num_samples)
    >>> print(label)
    Tensor(shape=[10], dtype=int64, place=Place(cpu), stop_gradient=True,
    [17, 10, 5 , 18, 8 , 8 , 19, 14, 10, 14])
    >>> print(remapped_label)
    Tensor(shape=[10], dtype=int64, place=Place(cpu), stop_gradient=True,
    [4, 2, 0, 5, 1, 1, 6, 3, 2, 3])
    >>> print(sampled_class_index)
    Tensor(shape=[7], dtype=int64, place=Place(cpu), stop_gradient=True,
    [5 , 8 , 10, 14, 17, 18, 19])

.. code-block:: python
    :name: code-example2

    >>> # doctest: +REQUIRES(env:DISTRIBUTED)
    >>> # Multi GPU, test_class_center_sample.py
    >>> import paddle
    >>> import paddle.distributed as dist
    >>> strategy = dist.fleet.DistributedStrategy()
    >>> dist.fleet.init(is_collective=True, strategy=strategy)
    >>> batch_size = 10
    >>> num_samples = 6
    >>> rank_id = dist.get_rank()
    >>> # num_classes of each GPU can be different, e.g num_classes_list = [10, 8]
    >>> num_classes_list = [10, 10]
    >>> num_classes = paddle.sum(paddle.to_tensor(num_classes_list))
    >>> label = paddle.randint(low=0, high=num_classes.item(), shape=[batch_size], dtype='int64')  # type: ignore[arg-type]
    >>> label_list = [] # type: ignore
    >>> dist.all_gather(label_list, label)
    >>> label = paddle.concat(label_list, axis=0)
    >>> remapped_label, sampled_class_index = paddle.nn.functional.class_center_sample(label, num_classes_list[rank_id], num_samples)

    >>> print(label)
    >>> print(remapped_label)
    >>> print(sampled_class_index)
    >>> #python -m paddle.distributed.launch --gpus=0,1 test_class_center_sample.py
    >>> # rank 0 output:
    Tensor(shape=[20], dtype=int64, place=CUDAPlace(0), stop_gradient=True,
    [10, 17, 15, 11, 9 , 12, 18, 18, 17, 18, 19, 2 , 8 , 13, 11, 13, 9 , 10, 0 , 4 ])
    Tensor(shape=[20], dtype=int64, place=CUDAPlace(0), stop_gradient=True,
    [6 , 11, 10, 7 , 4 , 8 , 12, 12, 11, 12, 13, 1 , 3 , 9 , 7 , 9 , 4 , 6 , 0 , 2 ])
    Tensor(shape=[6], dtype=int64, place=CUDAPlace(0), stop_gradient=True,
    [0, 2, 4, 8, 9, 3])
    >>> # rank 1 output:
    Tensor(shape=[20], dtype=int64, place=CUDAPlace(1), stop_gradient=True,
    [10, 17, 15, 11, 9 , 12, 18, 18, 17, 18, 19, 2 , 8 , 13, 11, 13, 9 , 10, 0 , 4 ])
    Tensor(shape=[20], dtype=int64, place=CUDAPlace(1), stop_gradient=True,
    [6 , 11, 10, 7 , 4 , 8 , 12, 12, 11, 12, 13, 1 , 3 , 9 , 7 , 9 , 4 , 6 , 0 , 2 ])
    Tensor(shape=[7], dtype=int64, place=CUDAPlace(1), stop_gradient=True,
    [0, 1, 2, 3, 5, 7, 8])
FN	is_memberzjExpected group is False, None or instance of paddle.distributed.collective.Group              (got group: )r   r@   z+Expected num_samples less than or equal to z, got num_samples r   z6Expected label_size > 0              (got label_size: z7Expected label_dims == 1              (got label_dims: rx  rJ  r   class_center_samplerR   Label)RemappedLabelSampledLocalClassCenter)num_classesnum_samplesring_idnranksrankr   r  rV   )hasattrrb   r  r   is_compiled_with_distr   distributedParallelEnvr  get_group_rank
world_sizer  r`   r]   r\   r   r  r   r   r  r   r
   r[   rc   rS   rd   )rx  r  r  groupr  r  r  parallel_envglobal_rank
label_sizer   
label_dimsr  op_typerf   remapped_labelsampled_class_centers                    rh   r  r  l
  s   R UNemwuk/J/J#
 	
 uk""5??+<+<GDFE%%''!--99;L&++K = ))+6 
 16\,,5<<F 9+FXYdXef
 	
 JEKK 
 !RJN)l!-
 	

 T%++&'JQ)l!-
 	

 D	';'='I'IQ'N#%11))$D	
 		
 +,	
 		
 w'*,A $G-FH-F>>kk ? N "DDkk E  +';

 '&D( ,D!
  " ///ri   r   kernel_sizestridedilation)rA   output_sizesrL   rN   rQ   rP   c                h   [        S0 [        5       D6n[        U S/ SQS5        [        U R                  5      S:X  d   S5       e[
        R                  " U R                  5      S:  d   S5       eS n[        U[        5      (       a  X/nO#U" U5      (       a  [        U5      S	:X  d   S
5       e[        U[        5      (       a  X"/nO#U" U5      (       a  [        U5      S	:X  d   S5       e[        U[        5      (       a  X3/nO#U" U5      (       a  [        U5      S	:X  d   S5       e[        U[        5      (       a  XU/nO#U" U5      (       a  [        U5      S	:X  d   S5       e[        U[        5      (       a  U/S-  nOV[        U[        [        45      (       a0  [        U5      S	:X  a  US	-  nO&[        U5      S:X  a  O[        S5      e[        S5      e[        5       (       a  [        R                  " XX#XE5      n	U	$ UR                  U R                   S9n	UR#                  SSU 0SU	0UUUUUS.S9  U	$ )aJ  

Combines an array of sliding local blocks into a large containing
tensor. also known as col2im when operated on batched 2D image tensor. Fold calculates each
combined value in the resulting large tensor by summing all values from all containing blocks.


For each input :math:`x` with shape [N, C_in , L], the output shape [N, C_out, H_out, W_out]
can be calculated as following.

.. math::

    H_{out} &= output\_size[0] \\
    W_{out} &= output\_size[1] \\
    C_{out} &= \frac{C_{in}}{kernel\_sizes[0]\times kernel\_sizes[1]} \\

Parameters:
    x(Tensor):                3-D Tensor, input tensor of format [N, C, L],
                              data type can be float32, float64, complex64 or complex128
    output_sizes(int|list|tuple):       The size of output size, should be [output_size_h, output_size_w]
                              or an integer o treated as [o, o].
    kernel_sizes(int|list|tuple):   The size of convolution kernel, should be [k_h, k_w]
                              or an integer k treated as [k, k].
    strides(int|list|tuple, optional):        The strides, should be [stride_h, stride_w]
                              or an integer stride treated as [stride, stride].
                              For default, strides will be [1, 1].
    paddings(int|list|tuple, optional):       The paddings of each dimension, should be
                              [padding_top, padding_left, padding_bottom, padding_right]
                              or [padding_h, padding_w] or an integer padding.
                              If [padding_h, padding_w] was given, it will expanded to
                              [padding_h, padding_w, padding_h, padding_w]. If an integer
                              padding was given, [padding, padding, padding, padding] will
                              be used. For default, paddings will be [0, 0, 0, 0]
    dilations(int|list|tuple, optional):      the dilations of convolution kernel, should be
                              [dilation_h, dilation_w], or an integer dilation treated as
                              [dilation, dilation]. For default, it will be [1, 1].
    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:
    The tensor formed by combining a group of sliding local blocks
    The output shape is [N, Cout, H, W] as described above.

Examples:

    .. code-block:: pycon

        >>> import paddle
        >>> import paddle.nn.functional as F

        >>> x = paddle.randn([2,3*2*2,12])
        >>> y = F.fold(x, output_sizes=[4, 5], kernel_sizes=2)
        >>> x = paddle.randn([2,3*2*2,12])
        >>> y = F.fold(x, output_sizes=[4, 5], kernel_sizes=2)
        >>> print(y.shape)
        paddle.Size([2, 3, 4, 5])

foldrA   )rG   rH   rK  rL  r   z'input should be the format of [N, C, L]r   z7The number of elements must greater or equal than zero.c                .    [        U [        [        45      $ rl   r   r   s    rh   r    fold.<locals>._is_list_or_tuple_  r   ri   rJ   zHoutput_sizes should either be an integer or a list/tuple of two integersrK   rM   rO   rI   zApaddings should either be an integer or a list of 2 or 4 integerszWUnexpected type of paddings, it should be either an integer or a listof 2 or 4 integersrR   rT   rU   )r  rL   rN   rQ   rP   rV   )r  )r
   r[   r   r\   r]   mathprodr^   r_   r`   ra   rb   r   r   r  rc   rS   rd   )
rA   r  rL   rN   rQ   rP   re   rf   r   rg   s
             rh   r  r  1  sV   ` ,68,F	3A6 qww<1GGG99QWW" A"/ ,$$$3!,//S5F!5K 	
V	
L ,$$$3!,//S5F!5K 	
V	
L '3$!'**G0A 	
Q	
B )S!!*	!),,#i.A2E 	
S	
F (C  :>	HtUm	,	,x=A!|H]aS  !
 	

 kk\H
" J 77agg7F8#J , ,"$&	 	 	
 Jri   )r@   r   r@   N)rA   r#   rL   r)   rN   r)   rQ   Size2 | Size4rP   r)   re   
str | Nonereturnr#   )NNr.   FNF)rj   r#   rn   ShapeLike | Nonero   ShapeLike | float | Nonerp   r2   rq   r   rr   bool | Noners   r   r  r#   )NNr.   Fr   NNN)rA   r#   rn   r  ro   r  rp   r2   rq   r   rv   r_   rw   8DataLayout1DVariant | DataLayout2D | DataLayout3D | Nonerr   r  re   r  r  r#   )r   r   r~   r   r  r#   )	NNr.   Fr   NNFN)rA   r#   rn   r  ro   r  rp   r2   rq   r   rv   r_   rw   r  rr   r  rs   r   re   r  r  r#   )NNr.   Fr   NN)rA   r#   rn   r  ro   r  rp   r2   rq   r   rv   r_   rw   r  re   r  r  r#   )NN)r   r#   r   r#   r   r#   r   Tensor | Nonere   r  r  r#   )      ?NTFr3   N)rA   r#   r  r   r   zint | Sequence[int] | Noner  r   r  r   rp   r5   re   r  r  r#   )r  TF)
rj   paddle.Tensorr  r   r  r   r  r   r  r  )r  Tr   N)rA   r#   r  r   r  r   rw   r%   re   r  r  r#   )r  Tr   N)rA   r#   r  r   r  r   rw   r&   re   r  r  r#   )TN)rA   r#   r:  r   r  r   r  r   re   r  r  r#   )r  TN)
rA   r#   r  r   r  r   re   r  r  r#   )r6   r
  NTN)rA   r#   rM  r(   rp   r:   r}   r   rw   zDataLayoutND | NonerW  r   re   r  r  r#   )r   N)
rA   r#   ra  r(   rw   r%   re   r  r  r#   )r@   g:0yE>)
r   r#   r   r#   r   r_   rf  r   r  r#   )
rA   r#   r   r#   r   r  re   r  r  r#   )Ng?N)
rx  r#   r|  r  r{  r   re   r  r  r#   rl   )
rx  r#   r  r_   r  r_   r  zGroup | bool | Noner  ztuple[Tensor, Tensor])rA   r#   r  r)   rL   r)   rN   r)   rQ   r  rP   r)   re   r  r  r#   )Y
__future__r   inspectr  r*  typingr   r   r   r   typing_extensionsr   r   r   r	   paddle.base.layer_helperr
   paddle.common_ops_importr   r   paddle.frameworkr   r   r   r   paddle.tensor.creationr   paddle.utilsr   paddle.utils.decorator_utilsr   r   paddle.utils.layers_utilsr   base.data_feederr   r   r   r   r   r   r   r   tensor.creationr   tensor.manipulationr   r    collections.abcr!   r"   r#   paddle._typingr$   r%   r&   r'   r(   r)   r*   &paddle.distributed.communication.groupr+   r2   __annotations__r5   r:   r?   __all__rC   rt   r   	signature__signature__r   r/   r   r.  r3  r5  rB  r7  rH  rM  rc  ro  r,   ry  r  r  rm   ri   rh   <module>r     s|   #    . .  &   0 C  ( # M E 
 . - $ 6(+   =")G#i  &&NOL)O$+?%	  #*(#i   TTT T 	T
 T T Tn 
 "-1&*.
 + 	
  (   
 
 "-1& 	*.
 + 	
   	A (   
g1X "-1& 	*.SS
S +S 	S
 S S 	AS (S S S Sl $--.BC 
 "%)& 	MM
M #M 	M
 M M 	AM M Mh 777 7 	7
 7 7t #w  '++kkk %k 	k
 k k k k !k`	 	AAA A 	A
 AL  &UUU U 	U
 U Ut  ';;; ; 	;
 ; ;D 999 9 	9
 9 9| 	((( ( 	(
 (Z 	))) ) 	)
 )X cG9%&  *'+#{{	{ { 	{
 %{ { { { '{| 
#
'	 !'	... . 	.
 ..b &%!8<<<<"%<05<< "<D 	kkk k 	k
 k` !%	WWW W 	W
 W| "&	B0B0B0 B0 	B0
 B0J Y&&:K \	 SSS S 	S
 S S S S	Sri   