o
    #jg                     @   s   g 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dZ			 							dddZ			 			
	dddZ			 			
		dddZ			 				dddZ			 					dddZdS )    )_C_opsin_dynamic_mode)LayerHelper)_update_padding_nd)convert_to_list   )addN   FNDHWCc                 C   s  |dksJ dd}|	dvrt d|	 dt| jdkr%t d| j |	d	k}|r-d
nd}t| jdkr>t d| j | j| }|dk rSt d| j d| dt|||\}}t||d}t||d}t rt| |||||||d ury|nd}|d urt||S |S | |d}||||||d}d}t	|fi t
 }|jddd}|jddd}|| j}|||d}|j||||d |d urt||S |S )Nr	    Currently, only support groups=1r   )r
   zAAttr(data_format) should be 'NDHWC'. Received Attr(data_format): .   z>Input x should be 5D tensor, but received x with the shape of r
   r   #The channel dimension of the input() should be defined. Received: stridedilation xZkernelZpaddingsZ	dilationsstridesgroupssubmkeysparse_conv3dint32TdtypeZstop_gradientoutrulebookcountertypeinputsoutputsattrs)
ValueErrorlenshaper   r   r   r   r   r   r   locals"create_variable_for_type_inference)create_sparse_variable_for_type_inferencer   	append_opr   weightbiasr   paddingr   r   r   r   data_formatnamedimsZchannel_lastZchannel_dimZnum_channelsZpadding_algorithmZpre_biasr%   r'   Zop_typehelperr!   r"   r&    r7   a/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/sparse/nn/functional/conv.py_conv3d   s   







r9   NHWCc                 C   s  |dksJ dd}|	dvrt d|	 dt| jdkr%t d| j |	d	k}|r-d
nd}t| jdkr>t d| j | j| }|dk rSt d| j d| dt|||\}}t||d}t||d}t r}t| |||||||d ury|nd}n<| |d}||||||d}d}t|fi t	 }|j
ddd}|j
ddd}|| j}|||d}|j||||d |d urt||S |S )Nr	   r      )r:   z@Attr(data_format) should be 'NHWC'. Received Attr(data_format): r      z>Input x should be 4D tensor, but received x with the shape of r:   r   r   r   r   r   r   r   r   r   r   r   Tr   r   r#   )r(   r)   r*   r   r   r   r   r   r   r+   r,   r-   r   r.   r   r/   r7   r7   r8   _conv2dr   s~   





r=   c	           	      C      t | ||||||dd||S )a  

    The sparse convolution3d functional calculates the output based on the input, filter
    and strides, paddings, dilations, groups parameters. Input(Input) and
    Output(Output) are multidimensional SparseCooTensors with a shape of
    :math:`[N, D, H, W, C]` . 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. If bias attribution is provided,
    bias is added to the output of the convolution.

    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 1-D tensor with shape [M].
    * :math:`Out`: Output value, the shape of :math:`Out` and :math:`X` may be different.

    Args:
        x (Tensor): The input is 5-D SparseCooTensor with shape [N, D, H, W, C], the data
            type of input is float16 or float32 or float64.
        weight (Tensor): The convolution kernel, a Tensor with shape [kD, kH, kW, C/g, M],
            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. Currently, only support 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 `"NDHWC"`. When it is `"NDHWC"`, the data is stored in the order of:
            `[batch_size, input_depth, input_height, input_width, input_channels]`.
        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 SparseCooTensor representing the conv3d, whose data type is the same with input.

    Examples:
        .. code-block:: python

            >>> import paddle

            >>> indices = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 1, 2], [1, 3, 2, 3]]
            >>> values = [[1], [2], [3], [4]]
            >>> indices = paddle.to_tensor(indices, dtype='int32')
            >>> values = paddle.to_tensor(values, dtype='float32')
            >>> dense_shape = [1, 1, 3, 4, 1]
            >>> sparse_x = paddle.sparse.sparse_coo_tensor(indices, values, dense_shape, stop_gradient=True)
            >>> weight = paddle.randn((1, 3, 3, 1, 1), dtype='float32')
            >>> y = paddle.sparse.nn.functional.conv3d(sparse_x, weight)
            >>> print(y.shape)
            [1, 1, 1, 2, 1]
    FNr9   	r   r0   r1   r   r2   r   r   r3   r4   r7   r7   r8   conv3d   s   [rA   c
           
      C      t | ||||||d|||	S )a<  

    The sparse submanifold convolution3d functional calculates the output based on the input, filter
    and strides, paddings, dilations, groups parameters. Input(Input) and
    Output(Output) are multidimensional SparseCooTensors with a shape of
    :math:`[N, D, H, W, C]` . 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. If bias attribution is provided,
    bias is added to the output of the convolution.

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

    ..  math::

        Out = 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 DHWCM format.
    * :math:`\\ast`: Submanifold Convolution operation, refer to the paper: https://arxiv.org/abs/1706.01307.
    * :math:`b`: Bias value, a 1-D tensor with shape [M].
    * :math:`Out`: Output value, the shape of :math:`Out` and :math:`X` may be different.

    Args:
        x (Tensor): The input is 5-D SparseCooTensor with shape [N, D, H, W, C], the data
            type of input is float16 or float32 or float64.
        weight (Tensor): The convolution kernel, a Tensor with shape [kD, kH, kW, C/g, M],
            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): 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 `"NHWC"`, `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. Currently, only support 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 `"NDHWC"`. When it is `"NDHWC"`, the data is stored in the order of:
            `[batch_size, input_depth, input_height, input_width, input_channels]`.
        key(str, optional): the key is used to save or use the same rulebook,
            the definition and role of rulebook refers to
            https://pdfs.semanticscholar.org/5125/a16039cabc6320c908a4764f32596e018ad3.pdf. The
            default value is None.
        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 SparseCooTensor representing the conv3d, whose data type is
        the same with input.

    Examples:
        .. code-block:: python

            >>> import paddle

            >>> indices = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 1, 2], [1, 3, 2, 3]]
            >>> values = [[1], [2], [3], [4]]
            >>> indices = paddle.to_tensor(indices, dtype='int32')
            >>> values = paddle.to_tensor(values, dtype='float32')
            >>> dense_shape = [1, 1, 3, 4, 1]
            >>> sparse_x = paddle.sparse.sparse_coo_tensor(indices, values, dense_shape, stop_gradient=True)
            >>> weight = paddle.randn((1, 3, 3, 1, 1), dtype='float32')
            >>> y = paddle.sparse.nn.functional.subm_conv3d(sparse_x, weight)
            >>> print(y.shape)
            [1, 1, 3, 4, 1]
    Tr?   
r   r0   r1   r   r2   r   r   r3   r   r4   r7   r7   r8   subm_conv3d1  s   arD   c	           	      C   r>   )a'  

    The sparse convolution2d functional calculates the output based on the input, filter
    and strides, paddings, dilations, groups parameters. Input(Input) and
    Output(Output) are multidimensional SparseCooTensors with a shape of
    :math:`[N, H, W, C]` . 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. If bias attribution is provided,
    bias is added to the output of the convolution.

    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 NHWC format.
    * :math:`W`: Filter value, a tensor with HWCM format.
    * :math:`\\ast`: Convolution operation.
    * :math:`b`: Bias value, a 1-D tensor with shape [M].
    * :math:`Out`: Output value, the shape of :math:`Out` and :math:`X` may be different.

    Args:
        x (Tensor): The input is 4-D SparseCooTensor with shape [N, H, W, C], the data
            type of input is float16 or float32 or float64.
        weight (Tensor): The convolution kernel, a Tensor with shape [kH, kW, C/g, M],
            where M is the number of filters(output channels), g is the number of groups,
            kD, kH, kW are the filter's 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 two integers, (stride_height, stride_width).
            Otherwise, 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_height, pad_width]` or
            `[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, 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.
        groups (int, optional): 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. Currently, only support 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: `"NHWC"`.
            The default is `"NHWC"`. When it is `"NHWC"`, the data is stored in the order of:
            `[batch_size, input_height, input_width, input_channels]`.
        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 SparseCooTensor representing the conv2d, whose data type is the same with input.

    Examples:
        .. code-block:: python

            >>> import paddle

            >>> indices = [[0, 0, 0, 0], [0, 0, 1, 2], [1, 3, 2, 3]]
            >>> values = [[1], [2], [3], [4]]
            >>> indices = paddle.to_tensor(indices, dtype='int32')
            >>> values = paddle.to_tensor(values, dtype='float32')
            >>> dense_shape = [1, 3, 4, 1]
            >>> sparse_x = paddle.sparse.sparse_coo_tensor(indices, values, dense_shape, stop_gradient=True)
            >>> weight = paddle.randn((3, 3, 1, 1), dtype='float32')
            >>> y = paddle.sparse.nn.functional.conv2d(sparse_x, weight)
            >>> print(y.shape)
            [1, 1, 2, 1]
    FNr=   r@   r7   r7   r8   conv2d  s   YrF   c
           
      C   rB   )a  

    The sparse submanifold convolution2d functional calculates the output based on the input, filter
    and strides, paddings, dilations, groups parameters. Input(Input) and
    Output(Output) are multidimensional SparseCooTensors with a shape of
    :math:`[N, H, W, C]` . 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. If bias attribution is provided,
    bias is added to the output of the convolution.

    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 NHWC format.
    * :math:`W`: Filter value, a tensor with HWCM format.
    * :math:`\\ast`: Submanifold Convolution operation, refer to the paper: https://arxiv.org/abs/1706.01307.
    * :math:`b`: Bias value, a 1-D tensor with shape [M].
    * :math:`Out`: Output value, the shape of :math:`Out` and :math:`X` may be different.

    Args:
        x (Tensor): The input is 4-D SparseCooTensor with shape [N, H, W, C], the data
            type of input is float16 or float32 or float64.
        weight (Tensor): The convolution kernel, a Tensor with shape [kH, kW, C/g, M],
            where M is the number of filters(output channels), g is the number of groups,
            kD, kH, kW are the filter's 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 two integers, (stride_height, stride_width).
            Otherwise, 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_height, pad_width]` or
            `[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, 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.
        groups (int, optional): 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. Currently, only support 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: `"NHWC"`.
            The default is `"NHWC"`. When it is `"NHWC"`, the data is stored in the order of:
            `[batch_size, input_height, input_width, input_channels]`.
        key(str, optional): the key is used to save or use the same rulebook,
            the definition and role of rulebook refers to
            https://pdfs.semanticscholar.org/5125/a16039cabc6320c908a4764f32596e018ad3.pdf. The
            default value is None.
        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 SparseCooTensor representing the conv2d, whose data type is the same with input.

    Examples:
        .. code-block:: python

            >>> import paddle

            >>> indices = [[0, 0, 0, 0], [0, 0, 1, 2], [1, 3, 2, 3]]
            >>> values = [[1], [2], [3], [4]]
            >>> indices = paddle.to_tensor(indices, dtype='int32')
            >>> values = paddle.to_tensor(values, dtype='float32')
            >>> dense_shape = [1, 3, 4, 1]
            >>> sparse_x = paddle.sparse.sparse_coo_tensor(indices, values, dense_shape, stop_gradient=True)
            >>> weight = paddle.randn((3, 3, 1, 1), dtype='float32')
            >>> y = paddle.sparse.nn.functional.subm_conv2d(sparse_x, weight)
            >>> print(y.shape)
            [1, 3, 4, 1]
    TrE   rC   r7   r7   r8   subm_conv2d	  s   ^rG   )	Nr	   r   r	   r	   FNr
   N)	Nr	   r   r	   r	   FNr:   N)Nr	   r   r	   r	   r
   N)Nr	   r   r	   r	   r
   NN)Nr	   r   r	   r	   r:   N)Nr	   r   r	   r	   r:   NN)__all__Zpaddler   r   Zpaddle.base.layer_helperr   Zpaddle.nn.functional.convr   Zpaddle.utilsr   binaryr   r9   r=   rA   rD   rF   rG   r7   r7   r7   r8   <module>   sx   
\
X
m
s
k