o
    Qe8"                    @   s  d dl m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mZmZmZ ddlmZmZ dd	l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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* g Z+dd Z,dd Z-dd Z.dd Z/		 	 		 	 	!	 	"	#	$	d4d%d&Z0		 	 	 	 	'	d5d(d)Z1		 	 	 	 	!	d6d*d"Z2		 	 	 	 	 		'	d7d+d,Z3		 	 	 	 	 		!	d8d-d.Z4		 	 	 	 	/	d9d0d1Z5		 	 	 	 	 		/	d:d2d3Z6dS );    )print_functionN   )get_cudnn_version)Variable)dygraph_utils)convert_to_list_is_symmetric_padding_contain_var_convert_to_tensor_list)check_variable_and_dtypecheck_dtype)	ParamAttr)LayerHelper)	unsqueezesqueeze)add)nn)_C_ops_legacy_C_ops)	get_flags)in_dynamic_mode)is_compiled_with_cuda)is_compiled_with_npu)is_compiled_with_rocm)_global_flags)_in_legacy_dygraph)in_dygraph_mode)_non_static_modec                 C   s   t | ttfS )N)
isinstancelisttuple)input r"   ID:\Projects\ConvertPro\env\Lib\site-packages\paddle/nn/functional/conv.py_is_list_or_tuple0   s   r$   c                 C   sT   |rt | d ddgkot | d ddgkS t | d ddgko)t | d ddgkS )Nr      )r   )paddingchannel_lastr"   r"   r#   "_zero_padding_in_batch_and_channel4   s   ((r)   c                 C   s.   |r| dd n| dd  }dd |D }|S )Nr&   r%      c                 S   s   g | ]	}|D ]}|qqS r"   r"   ).0Z	pad_a_dimelemr"   r"   r#   
<listcomp>=   s    z9_exclude_padding_in_batch_and_channel.<locals>.<listcomp>r"   )r'   r(   Zpadding_r"   r"   r#   %_exclude_padding_in_batch_and_channel;   s   r.   c                 C   sr  t | tr(|  } | dvrtd| | dkr d}dg| } nd}dg| } n}t| rt| d| krZt| d rZt| |sFtd| d}t| |} t	| |rY| dd d } nKt| d| krt | d t
rd}t| d| d	} t	| |r| dd d } n%t| |krt | d t
rd}t| |d	} ntd
| d}t| |d	} tdd | D std| | |fS )N)SAMEVALIDz8Unknown padding: '{}'. It can only be 'SAME' or 'VALID'.r0   r   r/   r*   zINon-zero padding({}) in the batch or channel dimensions is not supported.ZEXPLICITr'   zIn valid padding: {}c                 S   s   g | ]}|d kqS )r   r"   )r+   pr"   r"   r#   r-   p   s    z&_update_padding_nd.<locals>.<listcomp>zPInvalid padding, all value should be larger than or equal to 0, but received: {})r   strupper
ValueErrorformatr$   lenr)   r.   r   intr   all)r'   r(   Znum_dimspadding_algorithmr"   r"   r#   _update_padding_ndA   s\   



r:   r&   NCHWconv2dTFc                 C   sH  t  rq|
dkrqt| |||||||ddd}|d uro|	dk r%|	t| j n|	}	t| tr0| d } t|tr9|d }t|jt| jk rit|dd t|	D |j dd tt| j|	 d D  }t	||S t	||S |S t  r|
d	krt
| |||||||dddd|}|d ur|	dk r|	t| j n|	}	t|d
d t|	D |j dd tt| j|	 d D  }t	||S |S t  r|
dkrt| |||||||ddd}|d ur	|	dk r|	t| j n|	}	t||jdd tt| j|	 d D  }t	||S |S t rBd|d|d|d|d|d|ddd|d|f}tt|
| |g|R  }|d ur>tj|||	d}|S |}|S | g|gd}||||||d||d	}t| dg d|
 t|
fi t }|jdd}||}d|gi}|j|
|||d |d ur||}|jd|g|gd d!|gi|	|d"d |S |}|S )#Nr<   Fr%   r   c                 S      g | ]}d qS r&   r"   r+   ir"   r"   r#   r-          z_conv_nd.<locals>.<listcomp>c                 S   r=   r>   r"   r?   r"   r"   r#   r-      rA   r&   depthwise_conv2dc                 S   r=   r>   r"   r?   r"   r"   r#   r-      rA   c                 S   r=   r>   r"   r?   r"   r"   r#   r-      rA   conv3dc                 S   r=   r>   r"   r?   r"   r"   r#   r-      rA   stridespaddings	dilationsgroups	use_cudnn
use_mkldnnfuse_relu_before_depthwise_convr9   data_formataxisZInputFilter	rD   rE   rF   rG   rH   rI   rJ   r9   rK   xZfloat16Zfloat32Zfloat64Zinput_param_nameOutputtypeinputsoutputsattrselementwise_add)XYZOut)rM   rI   )r   r   r<   r6   shaper   r    Zreshaperanger   rB   rC   r   getattrr   r   rZ   r   r   localsinput_dtype"create_variable_for_type_inference	append_op)rQ   weightbiasstrider'   r9   dilationrG   rK   channel_dimop_typerH   rI   namepre_biasZtmp_biasrY   outrW   helperdtyperX   r"   r"   r#   _conv_ndy   s  


$
$"



ro   NCLc	                 C   s2  t  }	|	dur
d}
nd}
|dvrtd||dk}|rdnd}|r%d	nd
}t| jdkr6td| j| j| }|jd }|dk rMtd| j||dkrXtd||| dkrhtd|| j||| dkrxtd||j|t||d\}}t|dkrdgd | }nt|dkrdg| }ntd|dgt|dd }dgt|dd }t|dgd}d}t r||kr|dkr|| dkrd}d}
t	 r||kr||krd}nd}|rdnd}t| |gd} t
 rtt|| |||||||dddd|
}|durtj|||d}n}t rEd|d|d|d|d|
d dd!dd"|d#|f}tt|| |g|R  }|durDtj|||d}nK| g|gd$}|||||
dd||d%	}t| d&g d'd t|fi t }|jd(d)}||}d*|gi}|j||||d+ |durtj|||d}t||gd}|S ),aM  
    The convolution1D layer calculates the output based on the input, filter
    and strides, paddings, dilations, groups parameters. Input and
    Output are in NCL format, where N is batch size, C is the number of
    channels, L is the length of the feature.
    Filter is in MCK format, where M is the number of output image channels,
    C is the number of input image channels, K is the size of the kernel.
    If the groups is greater than 1, C will equal the number of input image
    channels divided by the groups. If bias attribution and activation type
    are provided, bias is added to the output of the convolution, and the
    corresponding activation function is applied to the final result.

    For each input :math:`X`, the equation is:

    .. math::

        Out = \sigma (W \ast X + b)

    Where:

    * :math:`X`: Input value, a tensor with NCL format.
    * :math:`W`: Kernel value, a tensor with MCK format.
    * :math:`\\ast`: Convolution operation.
    * :math:`b`: Bias value, a 2-D tensor with shape [M, 1].
    * :math:`\\sigma`: Activation function.
    * :math:`Out`: Output value, the shape of :math:`Out` and :math:`X` may be different.

    Example:

        - Input:

          Input shape: :math:`(N, C_{in}, L_{in})`

          Filter shape: :math:`(C_{out}, C_{in}, L_f)`

        - Output:

          Output shape: :math:`(N, C_{out}, L_{out})`

        Where

        .. math::

            L_{out} = \frac{(L_{in} + 2 * padding - (dilation * (L_f - 1) + 1))}{stride} + 1

    Args:
        x (Tensor): The input is 3-D Tensor with shape [N, C, L], the data type
            of input is float16 or float32 or float64.
        weight (Tensor): The convolution kernel with shape [M, C/g, K], where M is
            the number of output channels, g is the number of groups, K is the kernel's size.
        bias (Tensor, optional): The bias with shape [M,]. Default: None.
        stride (int|list|tuple, optional): The stride size. If stride is a list/tuple, it must
            contain one integers, (stride_size). Default: 1.
        padding(int|str|tuple|list, optional): The padding size. Padding could be in one of the following forms.
            1. a string in ['valid', 'same'].
            2. an int, which means the feature map is zero paded by size of `padding` on both sides.
            3. a list[int] or tuple[int] whose length is 1, which means the feature map is zero paded by size of `padding[0]` on both sides.
            4. a list[int] or tuple[int] whose length is 2. It has the form  [pad_before, pad_after].
            5. a list or tuple of pairs of ints. It has the form [[pad_before, pad_after], [pad_before, pad_after], ...]. Note that, the batch dimension and channel dimension are also included. Each pair of integers correspond to the amount of padding for a dimension of the input. Padding in batch dimension and channel dimension should be [0, 0] or (0, 0).
            The default value is 0.
        dilation (int|list|tuple, optional): The dilation size. If dilation is a list/tuple, it must
            contain one integer, (dilation_size). Default: 1.
        groups (int, optional): The groups number of the conv1d function. According to grouped
            convolution in Alex Krizhevsky's Deep CNN paper: when group=2,
            the first half of the filters is only connected to the first half
            of the input channels, while the second half of the filters is only
            connected to the second half of the input channels. Default: 1.
        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: `"NCL"`, `"NLC"`.
            The default is `"NCL"`. When it is `"NCL"`, the data is stored in the order of:
            `[batch_size, input_channels, feature_length]`.
        name(str, optional): For detailed information, please refer
           to :ref:`api_guide_Name`. Usually name is no need to set and
           None by default.

    Returns:
        A tensor representing the conv1d, whose data type is the
        same with input.

    Examples:
        .. code-block:: python

          import paddle
          import paddle.nn.functional as F

          x = paddle.to_tensor([[[4, 8, 1, 9],
                                 [7, 2, 0, 9],
                                 [6, 9, 2, 6]]], dtype="float32")
          w = paddle.to_tensor([[[9, 3, 4],
                                 [0, 0, 7],
                                 [2, 5, 6]],
                                [[0, 3, 4],
                                 [2, 9, 7],
                                 [5, 6, 8]]], dtype="float32")

          y = F.conv1d(x, w)
          print(y)
          # Tensor(shape=[1, 2, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
          #        [[[133., 238.],
          #          [160., 211.]]])
    NTFrp   NLCzKAttr(data_format) should be 'NCL' or 'NLC'. Received Attr(data_format): {}.rr   r%   r&   NHWCr;   r   @Input x should be 3D tensor, but received x with the shape of {}r   GThe channel dimension of the input({}) should be defined. Received: {}.zBThe groups of conv1d should be greater than 0. Received groups: {}the channel of input must be divisible by groups,received: the channel of input is {}, the shape of input is {}, the groups is {}the number of filters must be divisible by groups,received: the number of filters is {}, the shape of weight is {}, the groups is {}r*   zDThe size of padding's dimension should be 1 or 2. But got padding={}rf   rg   rL   r<   rB   rD   rE   rF   rG   rH   rI   rJ   r9   rK   rN   rP   r!   rR   rQ   rS   rT   rU   )r   r4   r5   r6   r]   r:   r   r   r   r   r   r_   r   r   rZ   r   r   r   r   r`   ra   rb   rc   r   )rQ   rd   re   rf   r'   rg   rG   rK   rj   cudnn_versionrH   r(   rh   conv2d_data_formatnum_channelsnum_filtersr9   l_typeZsqueeze_aixsrl   rY   rW   rm   rn   rX   r"   r"   r#   conv1d$  s  p







r   c	                 C   s  |dvrt d||dk}	|	rdnd}
t| jdkr$t d| j| j|
 }|jd }|dk r;t d	| j||dkrFt d
||| dkrVt d|| j||| dkrft d||j|t }t rr|durrdnd}t||	d\}}t|dd}t|dd}d}||kr|dkr|| dkrd}t rd}n%d}n"t	 rt
| |||||||ddd}|durtj|||
d}|S |S t d }t r||kr||krd}nd}t rtdd rd}t| |||||||||
||||S )a(  

    The convolution2D layer calculates the output based on the input, filter
    and strides, paddings, dilations, groups parameters. Input and
    Output are in NCHW or NHWC format, where N is batch size, C is the number of
    channels, H is the height of the feature, and W is the width of the feature.
    Filter is in MCHW format, where M is the number of output image channels,
    C is the number of input image channels, H is the height of the filter,
    and W is the width of the filter. If the groups is greater than 1,
    C will equal the number of input image channels divided by the groups.
    Please refer to UFLDL's `convolution
    <http://ufldl.stanford.edu/tutorial/supervised/FeatureExtractionUsingConvolution/>`_
    for more details.
    If bias attribution and activation type are provided, bias is added to the
    output of the convolution, and the corresponding activation function is
    applied to the final result.

    For each input :math:`X`, the equation is:

    ..  math::

        Out = \sigma (W \ast X + b)

    Where:

    * :math:`X`: Input value, a tensor with NCHW or NHWC format.
    * :math:`W`: Filter value, a tensor with MCHW format.
    * :math:`\\ast`: Convolution operation.
    * :math:`b`: Bias value, a 2-D tensor with shape [M, 1].
    * :math:`\\sigma`: Activation function.
    * :math:`Out`: Output value, the shape of :math:`Out` and :math:`X` may be different.

    Example:

        - Input:

          Input shape: :math:`(N, C_{in}, H_{in}, W_{in})`

          Filter shape: :math:`(C_{out}, C_{in}, H_f, W_f)`

        - Output:

          Output shape: :math:`(N, C_{out}, H_{out}, W_{out})`

        Where

        ..  math::

            H_{out}&= \frac{(H_{in} + 2 * paddings[0] - (dilations[0] * (H_f - 1) + 1))}{strides[0]} + 1 \\\\
            W_{out}&= \frac{(W_{in} + 2 * paddings[1] - (dilations[1] * (W_f - 1) + 1))}{strides[1]} + 1

    Args:
        x (Tensor): The input is 4-D Tensor with shape [N, C, H, W], the data type 
            of input is float16 or float32 or float64.
        weight (Tensor): The convolution kernel with shape [M, C/g, kH, kW], where M is
            the number of output channels, g is the number of groups, kH is the filter's
            height, kW is the filter's width. 
        bias (Tensor, optional): The bias with shape [M,].
        stride (int|list|tuple): The stride size. It means the stride in convolution. 
            If stride is a list/tuple, it must contain two integers, (stride_height, stride_width). 
            Otherwise, stride_height = stride_width = stride. Default: stride = 1.
        padding (string|int|list|tuple): The padding size. It means the number of zero-paddings
            on both sides for each dimension.If `padding` is a string, either 'VALID' or
            'SAME' which is the padding algorithm. If padding size is a tuple or list,
            it could be in three forms: `[pad_height, pad_width]` or
            `[pad_height_top, pad_height_bottom, pad_width_left, pad_width_right]`, and when 
            `data_format` is `"NCHW"`, `padding` can be in the form `[[0,0], [0,0], 
            [pad_height_top, pad_height_bottom], [pad_width_left, pad_width_right]]`.
            when `data_format` is `"NHWC"`, `padding` can be in the form
            `[[0,0], [pad_height_top, pad_height_bottom], [pad_width_left, pad_width_right], [0,0]]`.
            Default: padding = 0.
        dilation (int|list|tuple): The dilation size. It means the spacing between the kernel
            points. If dilation is a list/tuple, it must contain two integers, (dilation_height, 
            dilation_width). Otherwise, dilation_height = dilation_width = dilation. 
            Default: dilation = 1.
        groups (int): The groups number of the Conv2D Layer. According to grouped
            convolution in Alex Krizhevsky's Deep CNN paper: when group=2,
            the first half of the filters is only connected to the first half
            of the input channels, while the second half of the filters is only
            connected to the second half of the input channels. Default: groups=1.
        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"`, `"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): For detailed information, please refer 
           to :ref:`api_guide_Name`. Usually name is no need to set and 
           None by default.

    Returns:
        A Tensor representing the conv2d result, whose data type is the same with input. 

    Examples:
        .. code-block:: python

          import paddle
          import paddle.nn.functional as F

          x_var = paddle.randn((2, 3, 8, 8), dtype='float32')
          w_var = paddle.randn((6, 3, 3, 3), dtype='float32')

          y_var = F.conv2d(x_var, w_var)
          y_np = y_var.numpy()

          print(y_np.shape)
          # (2, 6, 6, 6)
    r;   rs   zMAttr(data_format) should be 'NCHW' or 'NHWC'. Received Attr(data_format): {}.rs   r%   r&      @Input x should be 4D tensor, but received x with the shape of {}r   ru   zBThe groups of conv2d should be greater than 0. Received groups: {}rv   rw   NTFr*   rf   rg   r<   rB   rL   ZFLAGS_use_mkldnnZFLAGS_conv2d_disable_cudnn)r4   r5   r6   r]   r   r   r:   r   r   r   r   r<   r   rZ   r   r   r   ro   )rQ   rd   re   rf   r'   rg   rG   rK   rj   r(   rh   r|   r}   rz   rH   r9   r~   rk   rl   rI   r"   r"   r#   r<   1  s   v



c                 C   s  t  }|dur
d}nd}|	dvrtd|	|	dk}|rdnd}t| jd	kr0td
| j| j| }|dk rBtd| j||dkrMtd||| dkr]td|| j|t||d\}}t|dkrs|dgd  }nt|dkr|dg }ntd|t|dddg }t|dddg }|du rg }n|dkrtdt|tt	t
frt|dddg }ntd|dkrg }n	t|dddg }t|dkr|d |d krtd|d |d d}|jd }||kr|dkr|dkr|sd}d}|r	dnd}|rdnd}t| |gd} t|dgd}t rBtt|| |||||||||
}|durAtj|||d}n}t rtd|d|d|d|d |d!|d"|d#|d$|f}tt|| |g|R  }|durstj|||d}nK| g|gd%}|||||||||d&	}t| d'g d(d t|fi t }|jd)d*}||}d+|gi}|j||||d, |durtj|||d}t||gd}|S )-aK  
    The 1-D convolution transpose layer calculates the output based on the input,
    filter, and dilation, stride, padding. Input(Input) and output(Output)
    are in 'NCL' format or 'NLC' where N is batch size, C is the number of channels,
    L is the length of the feature. The details of convolution transpose
    layer, please refer to the following explanation and references
    `therein <https://arxiv.org/pdf/1603.07285.pdf>`_.
    If bias attribution and activation type are provided, bias is added to
    the output of the convolution, and the corresponding activation function
    is applied to the final result.

    For each input :math:`X`, the equation is:

    .. math::

        Out = \sigma (W \ast X + b)

    Where:

    * :math:`X`: Input value, a 3-D Tensor with 'NCL' format or 'NLC' format.
    * :math:`W`: Filter value, a 3-D Tensor with 'MCK' format.
    * :math:`\\ast`: Convolution operation.
    * :math:`b`: Bias value, a 2-D Tensor with shape [M, 1].
    * :math:`\\sigma`: Activation function.
    * :math:`Out`: Output value, a 3-D Tensor with data format 'NCL' or 'NLC', the shape of :math:`Out` and :math:`X` may be different.

    Example:

        - Input:

          Input shape: :math:`(N, C_{in}, L_{in})`

          Filter shape: :math:`(C_{in}, C_{out}, L_f)`

        - Output:

          Output shape: :math:`(N, C_{out}, L_{out})`

        Where

        .. math::

           L^\prime_{out} &= (L_{in} - 1) * stride - pad_top - pad_bottom + dilation * (L_f - 1) + 1 + output_padding \\\\
           L_{out} &\in [ L^\prime_{out}, L^\prime_{out} + stride ]

    Note:
          The conv1d_transpose can be seen as the backward of the conv1d. For conv1d,
          when stride > 1, conv1d maps multiple input shape to the same output shape,
          so for conv1d_transpose, when stride > 1, input shape maps multiple output shape.
          If output_size is None, :math:`L_{out} = L^\prime_{out}`;
          else, the :math:`L_{out}` of the output size must between :math:`L^\prime_{out}`
          and :math:`L^\prime_{out} + stride`.

    Args:
        x(Tensor): 3-D tensor with [N, C, L] or [N, L, C] format,
                         its data type is float32 or float64.
        weight(Tensor): The convolution kernel, a Tensor with shape [C, M/g, K],
            where M is the number of output channels(filters), g is the number of groups,
            K is the size of the kernel.
        bias(Tensor, optional): The bias, a Tensor with shape [M, ].
        stride(int|tuple|list, optional): The stride size. It means the stride in transposed convolution.
            If stride is a list/tuple, it must contain one integer, `(stride_size)`.
            Default: stride = 1.
        padding(int|list|str|tuple, optional): The padding size. The padding argument effectively adds
             `dilation * (kernel - 1)` amount of zero-padding on both sides of input. If `padding` is a
             string, either 'VALID' or 'SAME' supported, which is the padding algorithm.
             If `padding` is a tuple or list, it could be in two forms:
             `[pad]` or `[pad_left, pad_right]`. Default: padding = 0.
        output_padding(int|list|tuple, optional): The count of zeros to be added to tail of each dimension.
             If it is a list/tuple, it must contain one integer. Default: 0.
        groups(int, optional): The groups number of the conv1d transpose function. Inspired by
            grouped convolution in Alex Krizhevsky's Deep CNN paper, in which
            when group=2, the first half of the filters is only connected to the
            first half of the input channels, while the second half of the
            filters is only connected to the second half of the input channels.
            Default: groups = 1.
        dilation(int|tuple|list, optional): The dilation size. It means the spacing between the kernel points.
            If dilation is a list/tuple, it must contain one integer, `(dilation_size)`.
            Default: dilation = 1.
        output_size(int|tuple|list, optional): The output image size. If output size is a
            tuple/list, it must contain one integer, `(feature_length)`. None if use
            filter_size(shape of weight), padding, and stride to calculate output_size.
        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: `"NCL"`, `"NLC"`.
            The default is `"NCL"`. When it is `"NCL"`, the data is stored in the order of:
            `[batch_size, input_channels, input_length]`.
        name(str, optional): For detailed information, please refer 
           to :ref:`api_guide_Name`. Usually name is no need to set and 
           None by default.

    Returns:
        A  tensor representing the result of 1-D transpose convolution, whose
        data type is the same with input. And its shape is (num_batches, channels, length)
        when data_format is `"NCL"` and (num_batches, length, channels) when data_format is
        `"NLC"`.

    Examples:
        .. code-block:: python

          import paddle
          import paddle.nn.functional as F

          # shape: (1, 2, 4)
          x = paddle.to_tensor([[[4, 0, 9, 7],
                                [8, 0, 9, 2,]]], dtype="float32")
          # shape: (2, 1, 2)
          w = paddle.to_tensor([[[7, 0]],
                                [[4, 2]]], dtype="float32")

          y = F.conv1d_transpose(x, w)
          print(y)
          # Tensor(shape=[1, 1, 5], dtype=float32, place=Place(gpu:0), stop_gradient=True,
          #        [[[60., 16., 99., 75., 4. ]]])
    NTFrq   zjAttr(data_format) of conv2d_transpose got wrong value: received {}, but only 'NCL' or 'NLC' are supported.rr   r%   r&   r   rt   r   ru   zLThe groups of conv1d_transpose should be greater than 0. Received groups: {}rv   r*   zAThe size of padding's dimension should 1 or 2. But got padding={}rf   rg   <output_padding option is mutually exclusive with output_sizeoutput_size1output_size should be int, or list, tuple of intsoutput_paddingzdThe size of output_padding should not be greater than stride.But got output_padding={} and stride={}conv2d_transposedepthwise_conv2d_transposerx   rs   r;   rL   rD   rE   r9   rF   rG   rH   rK   rN   	r   r   rD   rE   r9   rF   rG   rH   rK   r!   rR   rQ   rS   rT   rU   )r   r4   r5   r6   r]   r:   r   r   r   r    r7   r   r   r_   r   r   rZ   r   r   r   r   r`   ra   rb   rc   r   )rQ   rd   re   rf   r'   r   rG   rg   r   rK   rj   rz   rH   r(   rh   r|   r9   ri   r}   Zsqueeze_axisr{   rl   rY   rW   rm   rn   rX   r"   r"   r#   conv1d_transpose  s.  










r   c                 C   sd  |	dvrt d|	|	dk}|rdnd}t| jdkr$t d| j| j| }|dk r6t d	| j||dkrAt d
||| dkrQt d|| j|t }t r]|dur]dnd}t||d\}}t|dd}t|dd}|du rzg }nc|dkrt dt|t	t
frt|rt|}nKt|dd}nDt|trt|dd}n8t|trt|jdddgd t|jdkr|jd dks|jd dkr|jd dkr||g}nt dt d|dkrg }nt|dd}d}|jd }||kr|dkr|dkrd}d}t r/|dkrtjntj}|| |||||||||	
}|dur-tj|||dS |S t rfd|d|d|d|d|d|d |d!|d"|	f}tt|| |g|R  }|durbtj|||d}|S |}|S | g|gd#}|||||||||	d$	}t| d%g d&d t|fi t }|| j}d'|gi}|j||||d( |durtj|||d}|S |}|S ))a  

    The convolution2D transpose layer calculates the output based on the input,
    filter, and dilations, strides, paddings. Input(Input) and output(Output)
    are in NCHW or NHWC format. Where N is batch size, C is the number of channels,
    H is the height of the feature, and W is the width of the feature.
    Parameters(dilations, strides, paddings) are two elements. These two elements
    represent height and width, respectively. The details of convolution transpose
    layer, please refer to the following explanation and references
    `therein <https://arxiv.org/pdf/1603.07285.pdf>`_.
    If bias attribution and activation type are provided, bias is added to
    the output of the convolution, and the corresponding activation function
    is applied to the final result.
    See more detail in :ref:`api_nn_conv_ConvTranspose2d` .

    For each input :math:`X`, the equation is:

    ..  math::

        Out = \sigma (W \ast X + b)

    Where:

    * :math:`X`: Input value, a 4-D Tensor with NCHW or NHWC format.
    * :math:`W`: Filter value, a 4-D Tensor with MCHW format.
    * :math:`\\ast`: Convolution operation.
    * :math:`b`: Bias value, a 2-D Tensor with shape [M, 1].
    * :math:`\\sigma`: Activation function.
    * :math:`Out`: Output value, a 4-D Tensor with data format 'NCHW' or 'NHWC', the shape of :math:`Out` and :math:`X` may be different.

    Example:

        - Input:

          Input shape: :math:`(N, C_{in}, H_{in}, W_{in})`

          Filter shape: :math:`(C_{in}, C_{out}, H_f, W_f)`

        - Output:

          Output shape: :math:`(N, C_{out}, H_{out}, W_{out})`

        Where

        ..  math::

           H^\prime_{out} &= (H_{in} - 1) * strides[0] - pad_height_top - pad_height_bottom + dilations[0] * (H_f - 1) + 1 \\\\
           W^\prime_{out} &= (W_{in} - 1) * strides[1] - pad_width_left - pad_width_right + dilations[1] * (W_f - 1) + 1 \\\\
           H_{out} &\in [ H^\prime_{out}, H^\prime_{out} + strides[0] ] \\\\
           W_{out} &\in [ W^\prime_{out}, W^\prime_{out} + strides[1] ]

    Note:
          The conv2d_transpose can be seen as the backward of the conv2d. For conv2d, 
          when stride > 1, conv2d maps multiple input shape to the same output shape, 
          so for conv2d_transpose, when stride > 1, input shape maps multiple output shape.
          If output_size is None, :math:`H_{out} = H^\prime_{out}, W_{out} = W^\prime_{out}`; 
          else, the :math:`H_{out}` of the output size must between :math:`H^\prime_{out}` 
          and :math:`H^\prime_{out} + strides[0]`, and the :math:`W_{out}` of the output size must 
          between :math:`W^\prime_{out}` and :math:`W^\prime_{out} + strides[1]`.

    Args:
        x(Tensor): 4-D Tensor with [N, C, H, W] or [N, H, W, C] format,
            whose data type is float32 or float64.
        weight(Tensor): The convolution kernel, a Tensor with shape [C, M/g, kH, kW],
            where M is the number of output channels(filters), g is the number of groups,
            kH is the height of the kernel, and kW is the width of the kernel.
        bias(Tensor, optional): The bias, a Tensor with shape [M, ].
        stride(int|list|tuple, optional): The stride size. It means the stride in transposed convolution. 
            If stride is a list/tuple, it must contain two integers, (stride_height, stride_width). 
            Otherwise, stride_height = stride_width = stride. Default: stride = 1.
        padding(str|int|list|tuple, optional): The padding size. It means the number of zero-paddings 
            on both sides for each dimension. If `padding` is a string, either 'VALID' or 
            'SAME' which is the padding algorithm. If padding size is a tuple or list,
            it could be in three forms: `[pad_height, pad_width]` or 
            `[pad_height_top, pad_height_bottom, pad_width_left, pad_width_right]`,
            and when `data_format` is `"NCHW"`, `padding` can be in the form 
            `[[0,0], [0,0], [pad_height_top, pad_height_bottom], [pad_width_left, pad_width_right]]`.
            when `data_format` is `"NHWC"`, `padding` can be in the form 
            `[[0,0], [pad_height_top, pad_height_bottom], [pad_width_left, pad_width_right], [0,0]]`.
            Default: padding = 0.
        output_padding(int|list|tuple, optional): Additional size added to one side
            of each dimension in the output shape. Default: 0.
        groups(int, optional): The groups number of the Conv2D transpose layer. Inspired by
            grouped convolution in Alex Krizhevsky's Deep CNN paper, in which
            when group=2, the first half of the filters is only connected to the
            first half of the input channels, while the second half of the
            filters is only connected to the second half of the input channels.
            Default: groups = 1.
        dilation(int|list|tuple, optional): The dilation size. It means the spacing between the kernel points. 
            If dilation is a list/tuple, it must contain two integers, (dilation_height, dilation_width). 
            Otherwise, dilation_height = dilation_width = dilation. Default: dilation = 1.
        output_size(int|tuple|list, optional): The output image size. If output size is a
            tuple/list, it must contain two integers, (image_height, image_width). None if use
            filter_size(shape of weight), padding, and stride to calculate output_size.
        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"`, `"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): For detailed information, please refer 
           to :ref:`api_guide_Name`. Usually name is no need to set and 
           None by default.

    Returns:
        A Tensor representing the conv2d_transpose, whose
        data type is the same with input and shape is (num_batches, channels, out_h, 
        out_w) or (num_batches, out_h, out_w, channels). The tensor variable storing 
        transposed convolution result.

    Examples:
        .. code-block:: python

          import paddle
          import paddle.nn.functional as F

          x_var = paddle.randn((2, 3, 8, 8), dtype='float32')
          w_var = paddle.randn((3, 6, 3, 3), dtype='float32')

          y_var = F.conv2d_transpose(x_var, w_var)
          y_np = y_var.numpy()

          print(y_np.shape)
          # (2, 6, 10, 10)
    r   zlAttr(data_format) of conv2d_transpose got wrong value: received {}, but only 'NCHW' or 'NHWC' are supported.rs   r%   r&   r   r   r   ru   zLThe groups of conv2d_transpose should be greater than 0. Received groups: {}rv   NTFr*   rf   rg   r   r   Zint32Zint64r   z-output_size must contain one or two integers.zDoutput_size should be int or Tensor or list, tuple of ints or Tensorr   r   rL   rD   rE   r9   rF   rG   rH   rK   rN   r   rQ   rR   rT   rU   )r4   r5   r6   r]   r   r   r:   r   r   r   r    r	   r
   r7   r   r   rn   r   r   r   r   r   rZ   r   r_   r   r   r   r`   rb   rc   )rQ   rd   re   rf   r'   r   rg   rG   r   rK   rj   r(   rh   r|   rz   rH   r9   ri   r}   oprk   rY   rl   rW   rm   rX   r"   r"   r#   r   L  s0   











r   NCDHWc	                 C   s.  |dvrt d||dk}	|	rdnd}
t| jdkr$t d| j| j|
 }|jd }|dk r;t d	| j||dkrFt d
||| dkrTt d|||| dkrbt d||t }t rn|durndnd}t||	d\}}t|dd}t|dd}d}t| |||||||||
||d|S )a^  

    The convolution3D layer calculates the output based on the input, filter
    and strides, paddings, dilations, groups parameters. Input(Input) and
    Output(Output) are in NCDHW or NDHWC format. Where N is batch size C is the number of
    channels, D is the depth of the feature, H is the height of the feature,
    and W is the width of the feature. Convlution3D is similar with Convlution2D
    but adds one dimension(depth). If bias attribution and activation type are
    provided, bias is added to the output of the convolution, and the
    corresponding activation function is applied to the final result.

    For each input :math:`X`, the equation is:

    ..  math::

        Out = \sigma (W \ast X + b)

    In the above equation:

    * :math:`X`: Input value, a tensor with NCDHW or NDHWC format.
    * :math:`W`: Filter value, a tensor with MCDHW format.
    * :math:`\\ast`: Convolution operation.
    * :math:`b`: Bias value, a 2-D tensor with shape [M, 1].
    * :math:`\\sigma`: Activation function.
    * :math:`Out`: Output value, the shape of :math:`Out` and :math:`X` may be different.

    Example:

        - Input:

          Input shape: :math:`(N, C_{in}, D_{in}, H_{in}, W_{in})`

          Filter shape: :math:`(C_{out}, C_{in}, D_f, H_f, W_f)`

        - Output:
          Output shape: :math:`(N, C_{out}, D_{out}, H_{out}, W_{out})`

        Where

        ..  math::

            D_{out}&= \\frac{(D_{in} + 2 * paddings[0] - (dilations[0] * (D_f - 1) + 1))}{strides[0]} + 1 \\\\
            H_{out}&= \\frac{(H_{in} + 2 * paddings[1] - (dilations[1] * (H_f - 1) + 1))}{strides[1]} + 1 \\\\
            W_{out}&= \\frac{(W_{in} + 2 * paddings[2] - (dilations[2] * (W_f - 1) + 1))}{strides[2]} + 1

    Args:
        x (Tensor): The input is 5-D Tensor with shape [N, C, D, H, W], the data 
            type of input is float16 or float32 or float64.
        weight (Tensor): The convolution kernel, a Tensor with shape [M, C/g, kD, kH, kW],
            where M is the number of filters(output channels), g is the number of groups,
            kD, kH, kW are the filter's depth, height and width respectively.
        bias (Tensor, optional): The bias, a Tensor of shape [M, ].
        stride (int|list|tuple, optional): The stride size. It means the stride in convolution. If stride is a 
            list/tuple, it must contain three integers, (stride_depth, stride_height, stride_width). 
            Otherwise, stride_depth = stride_height = stride_width = stride. Default: stride = 1.
        padding (string|int|list|tuple, optional): The padding size. It means the number of zero-paddings 
            on both sides for each dimension. If `padding` is a string, either 'VALID' or
            'SAME' which is the padding algorithm. If padding size is a tuple or list,
            it could be in three forms: `[pad_depth, pad_height, pad_width]` or
            `[pad_depth_front, pad_depth_back, pad_height_top, pad_height_bottom, pad_width_left, pad_width_right]`,
            and when `data_format` is `"NCDHW"`, `padding` can be in the form
            `[[0,0], [0,0], [pad_depth_front, pad_depth_back], [pad_height_top, pad_height_bottom], [pad_width_left, pad_width_right]]`.
            when `data_format` is `"NDHWC"`, `padding` can be in the form
            `[[0,0], [pad_depth_front, pad_depth_back], [pad_height_top, pad_height_bottom], [pad_width_left, pad_width_right], [0,0]]`.
            Default: padding = 0.
        dilation (int|list|tuple, optional): The dilation size. It means the spacing between the kernel points. 
            If dilation is a list/tuple, it must contain three integers, (dilation_depth, dilation_height,
            dilation_width). Otherwise, dilation_depth = dilation_height = dilation_width = dilation. 
            Default: dilation = 1.
        groups (int, optional): The groups number of the Conv3D Layer. According to grouped
            convolution in Alex Krizhevsky's Deep CNN paper: when group=2,
            the first half of the filters is only connected to the first half
            of the input channels, while the second half of the filters is only
            connected to the second half of the input channels. Default: groups=1
        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"`, `"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|None, optional): For detailed information, please refer 
           to :ref:`api_guide_Name`. Usually name is no need to set and 
           None by default.

    Returns:
        A Tensor representing the conv3d, whose data type is 
        the same with input. If act is None, the tensor storing the 
        convolution result, and if act is not None, the tensor storing 
        convolution and non-linearity activation result.

    Examples:
        .. code-block:: python

            import paddle
            import paddle.nn.functional as F

            x_var = paddle.randn((2, 3, 8, 8, 8), dtype='float32')
            w_var = paddle.randn((6, 3, 3, 3, 3), dtype='float32')

            y_var = F.conv3d(x_var, w_var)
            y_np = y_var.numpy()

            print(y_np.shape)
            # (2, 6, 6, 6, 6)
    r   NDHWCOAttr(data_format) should be 'NCDHW' or 'NDHWC'. Received Attr(data_format): {}.r   r%   r&      @Input x should be 5D tensor, but received x with the shape of {}r   ru   zBThe groups of conv3d should be greater than 0. Received groups: {}mThe number of input channels must be divisible by Attr(groups). Received: number of channels({}), groups({}).zeThe number of filters must be divisible by Attr(groups). Received: number of filters({}), groups({}).NTFr   rf   rg   rC   )	r4   r5   r6   r]   r   r   r:   r   ro   )rQ   rd   re   rf   r'   rg   rG   rK   rj   r(   rh   r|   r}   rz   rH   r9   ri   r"   r"   r#   rC     s   s


rC   c                 C   s  |	dvrt d|	|	dk}|rdnd}t| jdkr$t d| j| j| }|jd }|dk r;t d	| j||dkrFt d
||| dkrTt d||t||d\}}t|dd}t|dd}|du rog }n|dkrwt dt|ttt	frt|dd}nt d|dkrg }nt|dd}t
 }t r|durdnd}d}|rdnd}t rt| |||||||||
}|durtj|||dS |S t rd|d|d|d|d|d|d|d|d |f}tt|| |g|R  }|durtj|||d}|S |}|S | g|gd!}|||||||||d"	}t|fi t }t| d#g d$d% || j}d&|gi}|j||||d' |durJtj|||d}|S |}|S )(a  
    The convolution3d transpose layer calculates the output based on the input,
    filter, and dilations, strides, paddings. Input(Input) and output(Output)
    are in NCDHW or NDHWC format. Where N is batch size, C is the number of channels,
    D is the depth of the feature, H is the height of the feature, and W
    is the width of the feature. Parameters(dilations, strides, paddings) are
    two elements. These two elements represent height and width, respectively.
    The details of convolution transpose layer, please refer to the following
    explanation and references `therein <https://arxiv.org/pdf/1603.07285.pdf>`_.
    If bias attribution and activation type are provided, bias is added to
    the output of the convolution, and the corresponding activation function
    is applied to the final result.
    See more detail in :ref:`api_nn_conv_ConvTranspose3d` .

    For each input :math:`X`, the equation is:

    ..  math::

        Out = \sigma (W \ast X + b)

    In the above equation:

    * :math:`X`: Input value, a Tensor with NCDHW or NDHWC format.
    * :math:`W`: Filter value, a Tensor with MCDHW format.
    * :math:`\\ast`: Convolution operation.
    * :math:`b`: Bias value, a 2-D Tensor with shape [M, 1].
    * :math:`\\sigma`: Activation function.
    * :math:`Out`: Output value, the shape of :math:`Out` and :math:`X` may be different.

    Example:

        - Input:

          Input shape: :math:`(N, C_{in}, D_{in}, H_{in}, W_{in})`

          Filter shape: :math:`(C_{in}, C_{out}, D_f, H_f, W_f)`

        - Output:

          Output shape: :math:`(N, C_{out}, D_{out}, H_{out}, W_{out})`

        Where

        ..  math::

           D^\prime_{out} &= (D_{in} - 1) * strides[0] - 2 * paddings[0] + dilations[0] * (D_f - 1) + 1 \\\\
           H^\prime_{out} &= (H_{in} - 1) * strides[1] - 2 * paddings[1] + dilations[1] * (H_f - 1) + 1 \\\\
           W^\prime_{out} &= (W_{in} - 1) * strides[2] - 2 * paddings[2] + dilations[2] * (W_f - 1) + 1 \\\\
           D_{out} &\in [ D^\prime_{out}, D^\prime_{out} + strides[0] ] \\\\
           H_{out} &\in [ H^\prime_{out}, H^\prime_{out} + strides[1] ] \\\\
           W_{out} &\in [ W^\prime_{out}, W^\prime_{out} + strides[2] ]

    Note:
          The conv3d_transpose can be seen as the backward of the conv3d. For conv3d, 
          when stride > 1, conv3d maps multiple input shape to the same output shape, 
          so for conv3d_transpose, when stride > 1, input shape maps multiple output shape.
          If output_size is None, :math:`H_{out} = H^\prime_{out}, :math:`H_{out} = \
          H^\prime_{out}, W_{out} = W^\prime_{out}`; else, the :math:`D_{out}` of the output 
          size must between :math:`D^\prime_{out}` and :math:`D^\prime_{out} + strides[0]`, 
          the :math:`H_{out}` of the output size must between :math:`H^\prime_{out}` 
          and :math:`H^\prime_{out} + strides[1]`, and the :math:`W_{out}` of the output size must 
          between :math:`W^\prime_{out}` and :math:`W^\prime_{out} + strides[2]`.

    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 
            of input is float32 or float64.
        weight (Tensor): The convolution kernel, a Tensor with shape [C, M/g, kD, kH, kW],
            where M is the number of filters(output channels), g is the number of groups,
            kD, kH, kW are the filter's depth, height and width respectively.
        bias (Tensor, optional): The bias, a Tensor of shape [M, ].
        stride(int|list|tuple, optional): The stride size. It means the stride in transposed convolution. 
            If stride is a list/tuple, it must contain three integers, (stride_depth, stride_height, 
            stride_width). Otherwise, stride_depth = stride_height = stride_width = stride. 
            Default: stride = 1.
        padding (string|int|list|tuple, optional): The padding size. It means the number of zero-paddings 
            on both sides for each dimension. If `padding` is a string, either 'VALID' or
            'SAME' which is the padding algorithm. If padding size is a tuple or list,
            it could be in three forms: `[pad_depth, pad_height, pad_width]` or
            `[pad_depth_front, pad_depth_back, pad_height_top, pad_height_bottom, pad_width_left, pad_width_right]`,
            and when `data_format` is `"NCDHW"`, `padding` can be in the form
            `[[0,0], [0,0], [pad_depth_front, pad_depth_back], [pad_height_top, pad_height_bottom], [pad_width_left, pad_width_right]]`.
            when `data_format` is `"NDHWC"`, `padding` can be in the form
            `[[0,0], [pad_depth_front, pad_depth_back], [pad_height_top, pad_height_bottom], [pad_width_left, pad_width_right], [0,0]]`.
            Default: padding = 0.
        output_padding(int|list|tuple, optional): Additional size added to one side
            of each dimension in the output shape. Default: 0.
        groups(int, optional): The groups number of the Conv3D transpose layer. Inspired by
            grouped convolution in Alex Krizhevsky's Deep CNN paper, in which
            when group=2, the first half of the filters is only connected to the
            first half of the input channels, while the second half of the
            filters is only connected to the second half of the input channels.
            Default: groups=1
        dilation(int|list|tuple, optional): The dilation size. It means the spacing between the kernel points. 
            If dilation is a list/tuple, it must contain three integers, (dilation_depth, dilation_height, 
            dilation_width). Otherwise, dilation_depth = dilation_height = dilation_width = dilation. 
            Default: dilation = 1.
        output_size(int|list|tuple, optional): The output image size. If output size is a
            list/tuple, it must contain three integers, (image_depth, image_height, image_width).
            None if use filter_size(shape of weight), padding, and stride to calculate output_size.
        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"`, `"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): For detailed information, please refer 
           to :ref:`api_guide_Name`. Usually name is no need to set and 
           None by default.

    Returns:
        A Tensor representing the conv3d_transpose, whose data
        type is the same with input and shape is (num_batches, channels, out_d, out_h, 
        out_w) or (num_batches, out_d, out_h, out_w, channels). If act is None, the tensor 
        variable storing the transposed convolution result, and if act is not None, the tensor 
        variable storing transposed convolution and non-linearity activation result.

    Examples:
       .. code-block:: python
          
          import paddle
          import paddle.nn.functional as F

          x_var = paddle.randn((2, 3, 8, 8, 8), dtype='float32')
          w_var = paddle.randn((3, 6, 3, 3, 3), dtype='float32')

          y_var = F.conv3d_transpose(x_var, w_var)
          y_np = y_var.numpy()

          print(y_np.shape)
          # (2, 6, 10, 10, 10)
    r   r   r   r%   r&   r   r   r   ru   zLThe groups of conv3d_transpose should be greater than 0. Received groups: {}r   r   rf   rg   Nr   r   r   r   TFconv3d_transposers   r;   rL   rE   r9   rD   rF   rG   rH   rK   rN   )	r   r   rE   r9   rD   rF   rG   rH   rK   rQ   rR   rC   rT   rU   )r4   r5   r6   r]   r:   r   r   r   r    r7   r   r   r   r   r   r   rZ   r   r_   r   r   r`   r   rb   rn   rc   )rQ   rd   re   rf   r'   r   rG   rg   r   rK   rj   r(   rh   r|   r}   r9   rz   rH   ri   Zdata_format_rk   rY   rl   rW   rm   rX   r"   r"   r#   r   >  s    


 

r   )Nr&   r   Nr&   r&   r;   r&   r<   TFN)Nr&   r   r&   r&   rp   N)Nr&   r   r&   r&   r;   N)	Nr&   r   r   r&   r&   Nrp   N)	Nr&   r   r   r&   r&   Nr;   N)Nr&   r   r&   r&   r   N)	Nr&   r   r   r&   r&   Nr   N)7
__future__r   numpynpZdevicer   Zstaticr   Zfluidr   Zfluid.layers.utilsr   r   r	   r
   Zfluid.data_feederr   r   Z	frameworkr   Zfluid.layer_helperr   Ztensor.manipulationr   r   Ztensor.mathr   Zfluid.layersr   Zpaddler   r   r   r   Zpaddle.devicer   r   r   Zpaddle.fluid.frameworkr   r   r   r   __all__r$   r)   r.   r:   ro   r   r<   r   r   rC   r   r"   r"   r"   r#   <module>   s   ;
 /
  
 r
  2
  <
 ?