o
    Qe                    @   s  d dl Z d dlZd dlmZ d dlmZ ddlmZ ddlm	Z	 d dl
mZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZmZmZ ddlmZmZmZmZ ddlmZ d dlmZmZ d dlm Z  d dl!m"Z" d dlm#Z# d dl$mZ d dl
m%Z% g Z&d?ddZ'					 		d@ddZ(					 		d@ddZ)dAd d!Z*	dBd%d&Z+dCd'd(Z,dDd*d+Z-dEd,d-Z.dFd/d0Z/dGd1d2Z0dHd4d5Z1dAd6d7Z2dId9d:Z3dJd;d<Z4	d?d=d>Z5dS )K    N)LayerHelper)fill_constant   )concat)zeros)Variable)dygraph_utils)squeeze)	unsqueeze)clip)sum)sqrt)check_variable_and_dtypecheck_dtype
check_type)_varbase_creator_in_legacy_dygraphin_dygraph_mode_non_static_mode)_C_ops_legacy_C_ops)in_dynamic_mode)full)core)r   )default_main_program   c              	   C   sp  t di t }t| dddgd t| jdksJ dt|tr&||g}nt|tr1t|dks5J dt|tr?||g}nt|trJt|dksNJ d	t|trX||g}nt|trct|dksgJ d
t|trr|gd }nt|trt|dkr|d }nt|dkrntdtdt	 rt
| ||||S |j| jd}|jdd| id|i||||dd |S )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):   The size of convolution kernel, should be [k_h, k_w]
                                  or an integer k treated as [k, k].
        strides(int|list):        The strides, should be [stride_h, stride_w]
                                  or an integer stride treated as [sride, stride].
                                  For default, strides will be [1, 1].
        paddings(int|list):       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):      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 decriabled 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)
    unfoldxfloat32float64   z*input should be the format of [N, C, H, W]   zBkernel_sizes should either be an integer or a list of two integersz=strides should either be an integer or a list of two integersz?dilations should either be an integer or a list of two integersApaddings should either be an integer or a list of 2 or 4 integersWUnexpected type of paddings, it should be either an integer or a listof 2 or 4 integersdtypeXY)kernel_sizesstridespaddings	dilationstypeinputsoutputsattrsN)r   )r   localsr   lenshape
isinstanceintlist
ValueErrorr   r   r   "create_variable_for_type_inferencer%   	append_op)r   r(   r)   r*   r+   namehelperout r=   KD:\Projects\ConvertPro\env\Lib\site-packages\paddle/nn/functional/common.pyr   6   s`   C











r   nearestFNCHWc           "      C   s
  |  }|  }| }	g d}
||
vrtd|dv r't| jdkr'td|dv r=t| jdkr=t| jdkr=td	|d
v rLt| jdkrLtd|dkr[t| jdkr[td|du rg|du rgtdt|tsptd|dkr||dkr|td|dkr|dkrtd|dkrt|tst|t	st|t
rt|dkrtdt| jdkrtjj| |S t| jdkrtjj| |S t| jdkrtjj| |S td|	fi t }|jdd}t| jdkr|dvrtd| d t| jdkr|dvrtd| d t| jdkr!|d vr!td| d! d"d# }|d$ks4|d%ks4|d&kr6d$}|d'ksE|d(ksE|d)krGd'}|dkrNd*}d+| i}d,d,d,|	|||d-}|}|}|durn|durntd.|durt|t
rt sd/|_||d0< nt rt|t
rt| }nt|}t|D ]\}}t|t
r| d ||< q||std1d*}t|D ]\}}t|t
rd/}q|dksJ d2q|r"g }g }|D ]9}t|t
rd/|_|| |d, qt|tsJ |d3}tdgd3|d/|d4 || || q||d5< t| jdkrLt|dkr5td6|r?|d |d7< nttt|}|d |d7< t| jdkrt|d8kr_td9|ro|d |d:< |d |d7< nttt|}|d |d:< |d |d7< t| jdkrt|dkrtd;|r|d |d<< |d |d:< |d8 |d7< nttt|}|d |d<< |d |d:< |d8 |d7< nt rt|t
rt| }t|t
rd/|_||d=< nxt|tst|tr|dkrtd>g }tt| jd8 D ]}|| qttt||d?< nEt|ts"t|t	rWt|t| jd8 kr>td@t| jd8 t| j|D ]}|dkrKtd>q@ttt||d?< ntdAt r1g }|  D ]\}}|| || qet	|} |	dBkrt! rt"#| d0|v r|d0 ndd5|v r|d5 ndd=|v r|d= nd|dC |d< |d: |d7 d?|v r|d? ng |dD |dE |dF }!|!S t$j%| g| R  }!|!S |	dGkr)t! rt"&| d0|v r|d0 ndd5|v r|d5 ndd=|v r|d= nd|dC |d< |d: |d7 d?|v r|d? ng |dD |dE |dF }!|!S t$j'| g| R  }!|!S |	dHkrt! rvt"(| d0|v r>|d0 ndd5|v rH|d5 ndd=|v rR|d= nd|dC |d< |d: |d7 d?|v rh|d? ng |dD |dE |dF }!|!S t$j)| g| R  }!|!S |	dIkrt! rt"*| d0|v r|d0 ndd5|v r|d5 ndd=|v r|d= nd|dC |d< |d: |d7 d?|v r|d? ng |dD |dE |dF }!|!S t$j+| g| R  }!|!S |	dJkr/t! r&t",| d0|v r|d0 ndd5|v r|d5 ndd=|v r|d= nd|dC |d< |d: |d7 d?|v r|d? ng |dD |dE |dF }!|!S t$j-| g| R  }!|!S ||}!|j.d|	|dK|!i|dL |!S )Ma$  

    This API resizes a batch of images.

    The input must be a 3-D Tensor of the shape (num_batches, channels, in_w)
    or 4-D (num_batches, channels, in_h, in_w), 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 intput 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`.
        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].
             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.
             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
        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_indx+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 is `"NCHW"`. When it is `"NCHW"`, the data is stored in the order of:
            `[batch_size, input_channels, input_height, input_width]`. When it is `"NCHW"`, the data is 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 Tensor of the shape (num_batches, channels, out_w) or (num_batches, out_w, channels),
        A 4-D Tensor of the shape (num_batches, channels, out_h, out_w) or (num_batches, out_h, out_w, channels),
        or 5-D Tensor of the shape (num_batches, channels, out_d, out_h, out_w) or (num_batches, out_d, out_h, out_w, channels).


    Examples:
        .. code-block:: python

                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)
                    # [2L, 3L, 12L, 12L]

                # given scale
                output_2 = F.interpolate(x=input_data, scale_factor=[2,1])
                print(output_2.shape)
                # [2L, 3L, 12L, 10L]

                # bilinear interp
                output_3 = F.interpolate(x=input_data, scale_factor=[2,1], mode="bilinear")
                print(output_2.shape)
                # [2L, 3L, 12L, 10L]
    )LINEARBILINEAR	TRILINEARNEARESTBICUBICAREAzxThe 'resample' of image_resize can only be 'area', 'linear', 'bilinear', 'trilinear',  'bicubic' or 'nearest' currently.)rA   r   z!'linear' only support 3-D tensor.)rD   r       z*'NEAREST' only support 4-D  or 5-D tensor.)rB   rE   z1'bilinear' and 'bicubic' only support 4-D tensor.rC   z#'trilinear'only support 5-D tensor.Nz.One of size and scale_factor must not be None.z)Attr align_corners should be a bool valuer   r   zalign_mode can only be 0 or 1rD   zjalign_corners option can only be set with the interpolating modes: linear | bilinear | bicubic | trilinearrF   zoutput size can not be emptyz{}_interp_v2r   Zinput_param_name)NCW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.NCDHWNDHWCz> received but only `NCDHW` or `NDHWC` supported for 5-D input.c                 S      t | tp	t | tS Nr4   r6   tupledatar=   r=   r>   _is_list_or_turple_     z(interpolate.<locals>._is_list_or_turple_r@   rN   rI   rL   rO   rJ   Fr&   )out_dout_hout_winterp_methodalign_corners
align_modedata_layoutz3Only one of size or scale_factor should be defined.TZOutSizez+size should be a list or tuple or Variable.z>Each dimension size given in out_shape must be greater than 0.int32)Z	force_cpur<   Z
SizeTensorz,size length should be 2 for input 3-D tensorr[   r!   z-size length should be 2 for input 4-D tensor.rZ   z-size length should be 3 for input 5-D tensor.rY   ZScalez(Attr(scale) should be greater than zero.scalez6scale_shape length should be {} for input {}-D tensor.z@Attr(scale)'s type should be float, int, list, tuple, or Tensor.linearr_   r\   r]   r^   bilinearZ	trilinearr?   ZbicubicOutr,   )/upperlowerr7   r2   r3   r4   bool	TypeErrorr6   rS   r   paddlennZ
functionalZadaptive_avg_pool1dZadaptive_avg_pool2dZadaptive_avg_pool3dr   formatr1   input_dtyper   stop_gradientnumpy	enumerateappendr5   r8   r   mapfloatrangeitemsr   r   Zlinear_interpr   Zlinear_interp_v2Zbilinear_interpZbilinear_interp_v2Ztrilinear_interpZtrilinear_interp_v2Znearest_interpZnearest_interp_v2Zbicubic_interpZbicubic_interp_v2r9   )"r   sizescale_factormoder]   r^   data_formatr:   ZresampleZresample_typeZresample_methodsr;   r%   rV   r_   r.   r0   Z	out_shapera   idimZcontain_varZdim_idxZdim_sizeZnew_size_tensorZ	size_listZtemp_outZ
scale_listvalue	attr_listkvZdy_attrr<   r=   r=   r>   interpolate   sR   K$











XI
F7
4%
"

r   c                 C   s   t | ||||||S )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 4-D (num_batches, channels, in_h, in_w), 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 intput 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
    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}
    https://en.wikipedia.org/wiki/Linear_interpolation.
    For details of linear interpolation, please refer to Wikipedia:

    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`.
        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].
             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.
             Default: None.
        mode (str, optional): The resample method. It supports 'linear', 'nearest', 'bilinear',
                       '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_indx+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 is `"NCHW"`. When it is `"NCHW"`, the data is stored in the order of:
            `[batch_size, input_channels, input_height, input_width]`. When it is `"NCHW"`, the data is 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 Tensor of the shape (num_batches, channels, out_w) or (num_batches, out_w, channels),
        A 4-D Tensor of the shape (num_batches, channels, out_h, out_w) or (num_batches, out_h, out_w, channels),
        or 5-D Tensor of the shape (num_batches, channels, out_d, out_h, out_w) or (num_batches, out_d, out_h, out_w, channels).

        Examples:
        .. code-block:: python

                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)
                # [2L, 3L, 12L, 12L]

    )r   )r   ru   rv   rw   r]   r^   rx   r:   r=   r=   r>   upsample  s    @r   c                 C   s   t  rt| |||S t rt| |||S t| dddgd t|dddgd | ||d}|dur6||d< tdi t }|j| j	d	}|j
d
|d|id |S )a~  

    This layer performs bilinear on two inputs.
    See :ref:`api_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 (Parameter): The learnable weights of this layer, shape is [out_features, in1_features, in2_features].
       bias (Parameter, 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:: python

                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)
                # [5, 1000]
    x1r   r   rc   x2)r&   r'   ZWeightNZBiasr$   bilinear_tensor_productrd   )r-   r.   r/   )rc   )r   r   r   r   r   r   r   r1   r8   r%   r9   )r   r   weightbiasr:   r.   r;   r<   r=   r=   r>   rc     s   !
rc         ?Tupscale_in_trainc                    sj  t tttfstdt ttfr%dkr| S dk s!dkr%td dvr-td|r;t |tttfs;td|dkrd} d	krGd
n  t rt	 j
dkrVt	 j
}t rqt| d|  |durg|nd|du\}}|S t| dd| d|dud|dur|ndd \}}|S td&i t }	t| dg dd |	j| jd}|	jtjjjdd} fdd}
|
|	j| |}|	jdd| gi|g|gd|d |S t st| dddgd | j}d }|rt rdkrtj| ddS  d krtj| d| dn| }| j}t st| }t |tr|gnt|}t|dk s1t|t |d kr=td!!t |t|t |t |krRtd"!t |t |dgt | }t sj|D ]	}|| ||< q_n|D ]	}|| ||< qltj"|dddd#}t#dgdd$t$|}t%||}t%||}tj&|||d%}|S  d	krtj| |d}|S | }|S )'aU  
    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 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.
        mode(str, optional): ['upscale_in_train'(default) | 'downscale_in_infer'].

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

                - train: out = input * mask / ( 1.0 - dropout_prob )
                - inference: out = input

            2. downscale_in_infer, downscale the output at inference

                - train: out = input * mask
                - inference: out = input * (1.0 - dropout_prob)

        name (str, optional): Name for the operation (optional, default is 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`, 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`, we can set ``axis=[0,1]`` to perform dropout3d. Please refer to ``paddle.nn.functional.dropout3d`` for more details.

        .. code-block:: python

                import paddle

                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. , 0. , 6. ],
                #         [8. , 0. , 12.]])
                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,
                #        [[0. , 0. , 0. ],
                #         [8. , 10., 12.]])
                print(y_1)
                # Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
                #        [[2. , 0. , 6. ],
                #         [8. , 0. , 12.]])
                print(y_01)
                # Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
                #        [[0. , 0. , 0. ],
                #         [8. , 0. , 12.]])

    z)p argument should be a number or Variabler   r   !p argument should between 0 and 1)downscale_in_inferr   zBmode argument should be 'downscale_in_infer' or 'upscale_in_train'z/datatype of axis argument should be int or listNr   Zdowngrade_in_inferdropout_probis_testfix_seedseeddropout_implementationdropoutr   float16r   r   r$   T)r%   rm   c                    sj   |d u s|dkr| j dkr| j }t|tr#|jdgks#tdj|||d u|d ur.|nd d}|S )Nr   r   zIRequired p.shape == [1] if type(p) is Variable, but received p.shape = {})r   r   r   r   r   )random_seedr4   r   r3   rh   rk   )progr   r   r   r0   rw   pr=   r>   	get_attrs  s$   zdropout.<locals>.get_attrsr&   )rd   Maskr,   r   r         ?        ra   r   zhaxis value should be greater than or equal to 0 and less than dimensions of x:{}, but get axis value:{} zXlength of axis should not be greater than dimensions of x:{}, but get length of axis: {}r%   minmaxr3   Z
fill_valuer%   r:   )r   )'r4   rr   r5   r   rh   r7   r6   rS   r   r   r   r   r   r   r   r   r1   r   r8   r%   r   ZVarDescZVarTypeZUINT8Zmain_programr9   r   ri   ra   r3   r   r   r2   rk   uniformr   greater_equalcastmultiply)r   r   axistrainingrw   r:   r   r<   maskr;   r   r0   r%   Z	keep_probZscale_inputinput_shapeZinput_shape_tensorZ	drop_axesZ
mask_shapery   random_tensor	keep_maskretr=   r   r>   r     s    



$
r   c                 C   d   | j }t|dkrtdt||dvrtdt| t| ||dkr)ddgnddg|d	|d
S )a  
    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 ``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 float32 or float64.
        p (float): Probability of setting units to zero. Default 0.5.
        training (bool): 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` . The default is `NCHW` . When it is `NCHW` , the data is stored in the order of: [batch_size, input_channels, input_height, input_width].
        name (str, optional): Name for the operation (optional, default is 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

            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,:,:])

    r    z1dimensions of x should be 4, but received {} != 4rK   zMAttr(data_format) should be 'NCHW' or 'NHWC'. Received Attr(data_format): %s.r@   r   r   r   r   r   r   r   rw   r:   r3   r2   r7   rk   strr   r   r   r   rx   r:   r   r=   r=   r>   	dropout2d  s*   #r   rN   c                 C   r   )a  
    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 ``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): Probability of setting units to zero. Default 0.5.
        training (bool): 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``. The default is ``NCDHW`` . When it is ``NCDHW`` , the data is stored in the order of: [batch_size, input_channels, input_depth, input_height, input_width].
        name (str, optional): Name for the operation (optional, default is 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,:,:,:])

    rG   z1dimensions of x should be 5, but received {} != 5rM   zOAttr(data_format) should be 'NCDHW' or 'NDHWC'. Received Attr(data_format): %s.rN   r   r   r    r   r   r   r   r=   r=   r>   	dropout3d'  s*   "r   c                 C   sH  t |ttfstd|dk s|dkrtdt s#t| dddgd |r|dkr0tj| d	d
S d}d}| | }d| d||d    d }| | | }| j	}	| j
}
tj|
dd	dd}tdg|dd}t||}t||	}tt|
d|	d|}tdg||	d}tt| |tj||d
}tjtj||d
||d}|S | S )aO  
    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 float32 or float64.
        p (float | int): Probability of setting units to zero. Default 0.5.
        training (bool): A flag indicating whether it is in train phrase or not. Default True.
        name (str, optional): Name for the operation (optional, default is 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:
        .. code-block:: python

                import paddle

                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.10721093, -0.77919382],
                #         [-0.10721093,  1.66559887]]) (randomly)
                print(y_test)
                # Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
                #        [[-1.,  1.],
                #         [-1.,  1.]])
    z#p argument should be a float or intr   r   r   r   r   r   alpha_dropoutr   r   g,x?g2֫?r!   g      r   r   r   r   )r4   rr   r5   rh   r7   r   r   ri   ra   r%   r3   r   r   r   r   subtractaddr   )r   r   r   r:   alphara   Zalpha_pabr%   r   r   r   Z	drop_maskyresr=   r=   r>   r   a  sF   !

r   constantc                 C   s6  |dv sJ d || }|dv sJ d |t| j}|dkrt|ttfrt||d kr|}|}t rDt	| |t
|}	|	S t| dg dd	 t|d
t
ttfd	 t|tr`t
|}td/i t }
|
jdd}|
|}	|
jd	d| id|	i||dd |	S |dv sJ d |ddgddgddgd}||| v sJ d ||| |g }t|tr.|dv rd}|dkrttddd|gdd}dd g}t| |d} n|d krt|td!ddgdd}dg}t| |d} n|d"v r-d}|dkrttddd|gdd}ddg}t| |d} n|d kr-t|td!ddgdd}d#g}t| |d} ngt|}|dv rdd}|dkrOg d$| }dd g}t| |d} nF|d krc|ddg }dg}t| |d} n1|d"v rd}|dkrg d$| }ddg}t| |d} n|d kr|ddg }d#g}t| |d} t rt|tr|  }t| ||||}	n]t rt|tr|  }t| d%|d&|d'|d(|d)|}	n=|||d*}d| gi}t|tr|g|d+< g |d%< n||d%< td0i t }
|
jd-d}|
|}	|
jd,|d|	i|d t|dkrt|	|d}	|	S )1u  
    Pad tensor according to 'pad' and 'mode'.
    If mode is 'constant' and length of pad is twice as length of x dimension,
    then the padding will be started from the first dimension and moved back onto x
    according to 'pad' and 'value'.
    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.

    Parameters:
        x (Tensor): The input tensor with data type float32/double/int32/int64_t.
        pad (Tensor|list[int]|tuple[int]): The padding size with data type int.
            If mode is 'constant' and length of pad is twice as length of x dimension, then x will
            be padded from the first  dimension to the last dimension.
            Else: 1. If input dimension is 3, then the pad has the form (pad_left,
            pad_right). 2. If the input dimension is 4, then the pad has the form (pad_left, pad_right,
            pad_top, pad_bottom). 3. If the input dimension is 5, then the pad has the form
            (pad_left, pad_right, pad_top, pad_bottom, pad_front, pad_back).
        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. Default is "NCHW"，
        name (str, 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
                Out = [[[[[0., 0., 0.],
                          [1., 2., 3.],
                          [4., 5., 6.],
                          [0., 0., 0.]]]]]

            Case 1:
                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.]]]]]

            Case 2:
                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.]]]]]

            Case 3:
                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.]]]]]

            Case 4:
                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.]]]]]

    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)
            # [[[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)
            # [[[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)
            # [[[[6. 4. 5. 6. 4. 5.]
            #    [3. 1. 2. 3. 1. 2.]
            #    [6. 4. 5. 6. 4. 5.]
            #    [3. 1. 2. 3. 1. 2.]]]]
    )ZreflectZ	replicater   ZcircularzImode should be one of constant, reflect, replicate, circular, but got {}.)NCLr@   rN   NLCrL   rO   zPdata_format should be in one of [NCL, NCHW, NCDHW, NLC, NHWC, NDHWC], but got {}r   r!   r   )r   r   r   r`   int64Z	complex64Z
complex128pad	pad_valuerH   r&   rd   )r*   r   r,   )r   r    rG   z5input tesor dimension must be in [3, 4, 5] but got {}r   r   r@   rL   rN   rO   zIinput tensor dimension is {}, it's data format should be in {} but got {})r   r@   rN   r   )r    r`   r$   r   r   r    )r!   )r   rL   rO   r   )r   r   r   r   r*   rw   r{   rx   r:   )rw   r{   rx   ZPaddingspad3dinputNr   )r   )rk   re   r2   r3   r4   r6   rS   r   r   r   rr   r   r   r5   r   r   r1   rl   r8   r9   r   r   r
   rn   tolistr   r   r   r	   )r   r   rw   r{   rx   r:   Zx_dimr*   r   r<   r;   r%   Zsupported_format_mapZunsqueezed_dimr0   r.   r=   r=   r>   r     s  
p	

	
















r   c                 C   s   t | |dd||dS )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.

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

    Examples:
        .. code-block:: python

            import paddle
            import numpy as np
            import paddle.nn.functional as F

            x_shape = (1, 1, 2, 3)
            x = paddle.arange(np.prod(x_shape), dtype="float32").reshape(x_shape) + 1
            y = F.zeropad2d(x, [1, 2, 1, 1])
            # [[[[0. 0. 0. 0. 0. 0.]
            #    [0. 1. 2. 3. 0. 0.]
            #    [0. 4. 5. 6. 0. 0.]
            #    [0. 0. 0. 0. 0. 0.]]]]
    r   r   )r   rw   r{   rx   r:   r   )r   paddingrx   r:   r=   r=   r>   	zeropad2d  s   !r   :0yE>c           	      C   s`   t t| ||d}t t| | |d}t t|||d}tt|| || d}|| }|S )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)
            # [0.97689527,  0.99996042, -0.55138415]

    r   )r   )r   ri   r   r   r   )	r   r   r   ZepsZw12Zw1Zw2Zn12Zcos_simr=   r=   r>   cosine_similarity  s   ,r   c                 C   s  t  r
t| ||S t r#t| |dddd}|du r|S t||S tdi t }| j	}t
| dg dd t|dg dd | g|gd	}ddd
}||}	|jd|d|	i|d |dur||}
|jd|	g|gd	d|
gidt| jd id |
S |	}
|
S )a  

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

          x = paddle.randn((3, 2), dtype="float32")
          # x: [[-0.32342386 -1.200079  ]
          #     [ 0.7979031  -0.90978354]
          #     [ 0.40597573  1.8095392 ]]
          weight = paddle.full(shape=[2, 4], fill_value="0.5", dtype="float32", name="weight")
          # weight: [[0.5 0.5 0.5 0.5]
          #          [0.5 0.5 0.5 0.5]]
          bias = paddle.ones(shape=[4], dtype="float32", name="bias")
          # bias: [1. 1. 1. 1.]
          y = paddle.nn.functional.linear(x, weight, bias)
          # y: [[0.23824859 0.23824859 0.23824859 0.23824859]
          #     [0.9440598  0.9440598  0.9440598  0.9440598 ]
          #     [2.1077576  2.1077576  2.1077576  2.1077576 ]]
    trans_xFtrans_yNrb   r   r   r%   )r&   r'   )r   r   	matmul_v2rd   r,   elementwise_addr   r   )rb   )r   r   rb   r   r   r   r   r   r1   r%   r   r   r8   r9   r2   r3   )r   r   r   r:   Zpre_biasr;   r%   r.   r0   tmpr   r=   r=   r>   rb   %  sJ   3


rb   皙?c                 C   s   |dks|dk rt dt rt| |t|S t r&t| |dt|S t| dddgd t	di t
 }d	| _|| j}|jd|rJ| |d
nd| id|idt|id |S )a
  
    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 "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(gpu:0), 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.epsilonlabelr   r   label_smoothT)r&   Z	PriorDistr&   rd   r,   N)r   )r7   r   r   r   rr   ri   r   r   r   r   r1   rm   r8   r%   r9   )r   Z
prior_distr   r:   r;   Zsmooth_labelr=   r=   r>   r     s.   8
r   c                 C   s6  |dks|du st |dstd|t |dr| sdS d}d}d}|dkrJt rJtj }|j	}|du r;|n|
|}|du rG|jn|j}||krVtd||d}	t| jD ]}
|	|
9 }	q]|	dkrs|	dk rstd	|	tt| j}|dkrtd
|d}|du s|dkrt jdkrt j}t rt| ||||||du|dur|S dS t rt| d|d|d|d|d|d|dud|dur|nd\}}||fS t| dddgd d}t|fi t }|j| jd}|j| jd}|j|d| i||d||||||du|dur|nddd ||fS )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

    .. hint::
        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
        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)
        print(remapped_label)
        print(sampled_class_index)

        # the output is
        #Tensor(shape=[10], dtype=int64, place=CPUPlace, stop_gradient=True,
        #       [11, 5 , 1 , 3 , 12, 2 , 15, 19, 18, 19])
        #Tensor(shape=[10], dtype=int64, place=CPUPlace, stop_gradient=True,
        #       [4, 3, 0, 2, 5, 1, 6, 8, 7, 8])
        #Tensor(shape=[9], dtype=int64, place=CPUPlace, stop_gradient=True,
        #       [1 , 2 , 3 , 5 , 11, 12, 15, 18, 19])

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

        # required: 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')
        label_list = []
        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_memberzmExpected group is False, None or instance of paddle.distributed.collective.Group              (got group: {})r   r   zAExpected num_samples less than or equal to {}, got num_samples {}rX   z9Expected label_size > 0              (got label_size: {})z:Expected label_dims == 1              (got label_dims: {})num_classesnum_samplesring_idnranksrankr   r   r   r   r`   class_center_sampler$   Label)ZRemappedLabelZSampledLocalClassCenter)r   r   r   r   r   r   r   r,   )hasattrr7   rk   r   r   Zis_compiled_with_distri   distributedZParallelEnvr   Zget_group_rankZ
world_sizer   r6   r3   r2   r   r   r   r   r   r   r   r   r   r1   r8   r%   r9   )r   r   r   groupr   r   r   Zparallel_envZglobal_rankZ
label_sizerz   Z
label_dimsr   Zremapped_labelZsampled_class_centerZop_typer;   r=   r=   r>   r     s   h



r   c           
      C   s  t di t }t| dddgd t| jdksJ ddd }t|tr*||g}n||r4t|d	ks8J d
t|trB||g}n||rLt|d	ksPJ dt|trZ||g}n||rdt|d	kshJ dt|trr||g}n||r|t|d	ksJ dt|tr|gd }nt|trt|d	kr|d	 }nt|dkrntdtdt	 rt
| |||||}	|	S t rt| d|d|d|d|d|}	|	S |j| jd}	|jdd| id|	i|||||dd |	S )aR  
    
    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 or float64
        output_sizes(int|list|tuple):       The size of output size, should be [output_size_h, output_size_w]
                                  or an interger 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 [sride, 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 decriabled above.

    Examples:

        .. code-block:: python

            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)
            # y.shape = [2,3,4,5]

    foldr   r   r   r   z'input should be the format of [N, C, L]c                 S   rP   rQ   rR   rT   r=   r=   r>   rV    	  rW   z!fold.<locals>._is_list_or_turple_r!   zHoutput_sizes should either be an integer or a list/tuple of two integerszHkernel_sizes should either be an integer or a list/tuple of two integerszCstrides should either be an integer or a list/tuple of two integerszEdilations should either be an integer or a list/tuple of two integersr    r"   r#   output_sizesr(   r)   r*   r+   r$   r&   r'   )r   r(   r)   r*   r+   r,   N)r   )r   r1   r   r2   r3   r4   r5   r6   r7   r   r   r   r   r   r8   r%   r9   )
r   r   r(   r)   r*   r+   r:   r;   rV   r<   r=   r=   r>   r     s   =










r   )r   r   r   N)NNr?   Fr   r@   N)NN)r   NTr   N)r   Tr@   N)r   TrN   N)r   TN)r   r   r@   N)r@   N)r   r   )Nr   NrQ   )6warningsri   Zpaddle.fluid.layer_helperr   Zpaddle.fluid.layers.tensorr   Ztensorr   Ztensor.creationr   Zpaddle.staticr   Zfluidr   Ztensor.manipulationr	   r
   r   r   r   Zfluid.data_feederr   r   r   Zfluid.frameworkr   r   r   r   r   r   Zpaddle.frameworkr   Zpaddle.tensor.creationr   r   Zpaddle.fluid.frameworkr   __all__r   r   r   rc   r   r   r   r   r   r   r   rb   r   r   r   r=   r=   r=   r>   <module>   s   
 
    
 
E8
  
0
;
:
O  

+
4
a
U d