a
    8Sic6                     @   sr   d Z ddlZddlmZmZmZ ddl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ddZdS )zGradient interface    N   )_single_pair_triplec              
      s   d u r t d dgt  t d }t|d krR| d  t|krvtd|d t fddfddt|D fd	dt|D }t|D ]:\}}	}
||	k s||
krtd
|	 dd  qt
fddt|D S )NzK_grad_input_padding 'dilation' argument not provided. Default of 1 is used.r      z)input_size must have {} elements (got {})c                    s>    | d d |   d|    d  |  |  d   S )Nr   r   )size)d)dilationgrad_outputkernel_sizepaddingstride I/var/www/html/django/DPS/env/lib/python3.9/site-packages/torch/nn/grad.pydim_size   s    (z%_grad_input_padding.<locals>.dim_sizec                    s   g | ]} |qS r   r   .0r   )r   r   r   
<listcomp>       z'_grad_input_padding.<locals>.<listcomp>c                    s    g | ]} | |  d  qS )r   r   r   )	min_sizesr   r   r   r      r   zarequested an input grad size of {}, but valid sizes range from {} to {} (for a grad_output of {})c                 3   s   | ]} | |  V  qd S )Nr   r   )
input_sizer   r   r   	<genexpr>%   r   z&_grad_input_padding.<locals>.<genexpr>)warningswarnlenlistdim
ValueErrorformatrangezipr   tuple)r
   r   r   r   r   r	   kZ	max_sizesr   min_sizemax_sizer   )r	   r   r
   r   r   r   r   r   r   _grad_input_padding   s.    
r%   c           	   
   C   s^   t |}t |}t |}|jd g}| du r4tdt|| ||||}t||d|||||S )a  
    Computes the gradient of conv1d with respect to the input of the convolution.
    This is same as the 1D transposed convolution operator under the hood but requires
    the shape of the gradient w.r.t. input to be specified explicitly.

    Args:
        input_size : Shape of the input gradient tensor
        weight: weight tensor (out_channels x in_channels/groups x kW)
        grad_output : output gradient tensor (minibatch x out_channels x oW)
        stride (int or tuple, optional): Stride of the convolution. Default: 1
        padding (int or tuple, optional): Zero-padding added to both sides of the input. Default: 0
        dilation (int or tuple, optional): Spacing between kernel elements. Default: 1
        groups (int, optional): Number of blocked connections from input channels to output channels. Default: 1

    Examples::

        >>> input = torch.randn(1,1,3, requires_grad=True)
        >>> weight = torch.randn(1,1,1, requires_grad=True)
        >>> output = F.conv1d(input, weight)
        >>> grad_output = torch.randn(output.shape)
        >>> grad_input = torch.autograd.grad(output, input, grad_output)
        >>> F.grad.conv1d_input(input.shape, weight, grad_output)

    r   Nz3grad.conv1d_input requires specifying an input_size)r   shaper   r%   torchconv_transpose1d	r   weightr
   r   r   r	   groupsr   Zgrad_input_paddingr   r   r   conv1d_input(   s    r,   c              
   C   s  t |}t |}t |}| jd }|jd }| jd }	| d|| d}| |jd |jd  d|jd }|  d| jd | jd  | jd } t| |d|||||	 }
|
 |	|
jd |	 |
jd }
|
jdd|| ||
jd dd	dd|d S )a!  
    Computes the gradient of conv1d with respect to the weight of the convolution.

    Args:
        input: input tensor of shape (minibatch x in_channels x iW)
        weight_size : Shape of the weight gradient tensor
        grad_output : output gradient tensor (minibatch x out_channels x oW)
        stride (int or tuple, optional): Stride of the convolution. Default: 1
        padding (int or tuple, optional): Zero-padding added to both sides of the input. Default: 0
        dilation (int or tuple, optional): Spacing between kernel elements. Default: 1
        groups (int, optional): Number of blocked connections from input channels to output channels. Default: 1

    Examples::

        >>> input = torch.randn(1,1,3, requires_grad=True)
        >>> weight = torch.randn(1,1,1, requires_grad=True)
        >>> output = F.conv1d(input, weight)
        >>> grad_output = torch.randn(output.shape)
        >>> grad_weight = torch.autograd.grad(output, filter, grad_output)
        >>> F.grad.conv1d_weight(input, weight.shape, grad_output)

    r   r   r   Nr   )
r   r&   
contiguousrepeatviewr'   conv1dsum	transposenarrowinputZweight_sizer
   r   r   r	   r+   Zin_channelsZout_channelsZ	min_batchZgrad_weightr   r   r   conv1d_weightQ   s4    



r7   c           	   
   C   sf   t |}t |}t |}|jd |jd f}| du r<tdt|| ||||}t||d|||||S )a  
    Computes the gradient of conv2d with respect to the input of the convolution.
    This is same as the 2D transposed convolution operator under the hood but requires
    the shape of the gradient w.r.t. input to be specified explicitly.

    Args:
        input_size : Shape of the input gradient tensor
        weight: weight tensor (out_channels x in_channels/groups x kH x kW)
        grad_output : output gradient tensor (minibatch x out_channels x oH x oW)
        stride (int or tuple, optional): Stride of the convolution. Default: 1
        padding (int or tuple, optional): Zero-padding added to both sides of the input. Default: 0
        dilation (int or tuple, optional): Spacing between kernel elements. Default: 1
        groups (int, optional): Number of blocked connections from input channels to output channels. Default: 1

    Examples::

        >>> input = torch.randn(1,1,3,3, requires_grad=True)
        >>> weight = torch.randn(1,1,1,2, requires_grad=True)
        >>> output = F.conv2d(input, weight)
        >>> grad_output = torch.randn(output.shape)
        >>> grad_input = torch.autograd.grad(output, input, grad_output)
        >>> F.grad.conv2d_input(input.shape, weight, grad_output)

    r      Nz3grad.conv2d_input requires specifying an input_size)r   r&   r   r%   r'   conv_transpose2dr)   r   r   r   conv2d_input   s    r:   c              
   C   s>  t |}t |}t |}| jd }|jd }| jd }	| d|| dd}| |jd |jd  d|jd |jd }|  d| jd | jd  | jd | jd } t| |d|||||	 }
|
 |	|
jd |	 |
jd |
jd }
|
jdd|| ||
jd |
jd dd	dd|d 	dd|d S )a/  
    Computes the gradient of conv2d with respect to the weight of the convolution.

    Args:
        input: input tensor of shape (minibatch x in_channels x iH x iW)
        weight_size : Shape of the weight gradient tensor
        grad_output : output gradient tensor (minibatch x out_channels x oH x oW)
        stride (int or tuple, optional): Stride of the convolution. Default: 1
        padding (int or tuple, optional): Zero-padding added to both sides of the input. Default: 0
        dilation (int or tuple, optional): Spacing between kernel elements. Default: 1
        groups (int, optional): Number of blocked connections from input channels to output channels. Default: 1

    Examples::

        >>> input = torch.randn(1,1,3,3, requires_grad=True)
        >>> weight = torch.randn(1,1,1,2, requires_grad=True)
        >>> output = F.conv2d(input, weight)
        >>> grad_output = torch.randn(output.shape)
        >>> grad_weight = torch.autograd.grad(output, filter, grad_output)
        >>> F.grad.conv2d_weight(input, weight.shape, grad_output)

    r   r   r   r8   Nr-   )
r   r&   r.   r/   r0   r'   conv2dr2   r3   r4   r5   r   r   r   conv2d_weight   sB    




r<   c           	   
   C   sn   t |}t |}t |}|jd |jd |jd f}| du rDtdt|| ||||}t||d|||||S )a  
    Computes the gradient of conv3d with respect to the input of the convolution.
    This is same as the 3D transposed convolution operator under the hood but requires
    the shape of the gradient w.r.t. input to be specified explicitly.

    Args:
        input_size : Shape of the input gradient tensor
        weight: weights tensor (out_channels x in_channels/groups x kT x kH x kW)
        grad_output : output gradient tensor (minibatch x out_channels x oT x oH x oW)
        stride (int or tuple, optional): Stride of the convolution. Default: 1
        padding (int or tuple, optional): Zero-padding added to both sides of the input. Default: 0
        dilation (int or tuple, optional): Spacing between kernel elements. Default: 1
        groups (int, optional): Number of blocked connections from input channels to output channels. Default: 1

    Examples::

        >>> input = torch.randn(2, 8, 10, 10, 20, requires_grad=True)
        >>> weight = torch.randn(4, 8, 2, 3, 3, requires_grad=True)
        >>> output = F.conv3d(input, weight)
        >>> grad_output = torch.randn(output.shape)
        >>> grad_input = torch.autograd.grad(output, input, grad_output)
        >>> F.grad.conv3d_input(input.shape, weight, grad_output)

    r   r8      Nz3grad.conv3d_input requires specifying an input_size)r   r&   r   r%   r'   conv_transpose3dr)   r   r   r   conv3d_input   s    r?   c              
   C   sj  t |}t |}t |}| jd }|jd }| jd }	|d|| ddd}| |jd |jd  d|jd |jd |jd }|  d| jd | jd  | jd | jd | jd } t| |d|||||	 }
|
 |	|
jd |	 |
jd |
jd |
jd }
|
jdd|| ||
jd |
jd |
jd dd	dd|d 	dd|d 	dd|d S )aH  
    Computes the gradient of conv3d with respect to the weight of the convolution.

    Args:
        input: input tensor of shape (minibatch x in_channels x iT x iH x iW)
        weight_size : Shape of the weight gradient tensor
        grad_output : output gradient tensor (minibatch x out_channels x oT x oH x oW)
        stride (int or tuple, optional): Stride of the convolution. Default: 1
        padding (int or tuple, optional): Zero-padding added to both sides of the input. Default: 0
        dilation (int or tuple, optional): Spacing between kernel elements. Default: 1
        groups (int, optional): Number of blocked connections from input channels to output channels. Default: 1

    Examples::

        >>> input = torch.randn(2, 8, 10, 10, 20, requires_grad=True)
        >>> weight = torch.randn(4, 8, 2, 3, 3, requires_grad=True)
        >>> output = F.conv3d(input, weight)
        >>> grad_output = torch.randn(output.shape)
        >>> grad_weight = torch.autograd.grad(output, weight, grad_output)
        >>> F.grad.conv3d_weight(input, weight.shape, grad_output)

    r   r   r   r8   r=   Nr-   )
r   r&   r/   r.   r0   r'   conv3dr2   r3   r4   r5   r   r   r   conv3d_weight  sD    





rA   )N)r   r   r   r   )r   r   r   r   )r   r   r   r   )r   r   r   r   )r   r   r   r   )r   r   r   r   )__doc__r'   modules.utilsr   r   r   r   r%   r,   r7   r:   r<   r?   rA   r   r   r   r   <module>   s   
 
)
0
)
4
)