U
    KcB                     @   s  d Z ddlZddlmZ ddlm  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 ddlm  m  mZ ddlZdd	d
dddgZG dd dejZG dd	 d	ejZG dd
 d
ejZG dd dejZG dd dejZG dd dejZdS )z*Dynamically quantized convolution modules.    N)Tensor)ops)	_size_1_t)_single_pair_triple)_reverse_repeat_paddingConv1dConv2dConv3dConvTranspose1dConvTranspose2dConvTranspose3dc                       sb   e Zd ZdZejZdZdZde	e	e
e
e
e
e	eed	 fdd	Zd
d ZdeeedddZ  ZS )r	   a  A dynamically quantized conv module with floating point tensors as inputs and outputs.

    For details on input arguments, parameters, and implementation see
    :class:`~torch.nn.Conv1d` and :class:`~torch.nn.quantized.dynamic.Conv1d` and

    Attributes:
        weight (Tensor):     packed tensor derived from the learnable weight
                             parameter.
        scale (Tensor):      scalar for the output scale
        zero_point (Tensor): scalar for the output zero point

    See :class:`~torch.nn.Conv1d` for other attributes.

    Examples::

        >>> m = nn.quantized.dynamic.Conv1d(16, 33, 3, stride=2)
        >>> input = torch.randn(20, 16, 100)
        >>> # xdoctest: +SKIP
        >>> output = m(input)

    N   r   Tzeros)	in_channelsout_channelskernel_sizestridepaddingdilationgroupsbiaspadding_modec              
      st   t d|   |
|d}t|}t|}t|tr<|nt|}t|}tt| j	|||||||||	f	| d S NfThe current implementation of the {} module has poor numerical accuracy and its use is not recommendeddevicedtype)
warningswarnformat	_get_namer   
isinstancestrsuperr	   __init__)selfr   r   r   r   r   r   r   r   r   r   r   reduce_rangefactory_kwargs	__class__ N/tmp/pip-unpacked-wheel-gikjz4vx/torch/ao/nn/quantized/dynamic/modules/conv.pyr&   -   s.    

       zConv1d.__init__c                 C   s   dS )NZDynamicQuantizedConv1dr,   r'   r,   r,   r-   r"   I   s    zConv1d._get_nameinputr(   returnc                 C   sV   t |jdkrtd| jdkrDt| jd d }tj||| jd}tj	
|| j|S )N    Input shape must be `(N, C, L)`!r   r   mode)lenshape
ValueErrorr   r   r   Fpadr   	quantizedZconv1d_dynamic_packed_paramsr'   r0   r(   Z _reversed_padding_repeated_twicer,   r,   r-   forwardL   s    
zConv1d.forward)	r   r   r   r   Tr   NNT)T)__name__
__module____qualname____doc__nnr	   _FLOAT_MODULE_NNIQAT_CONV_BN_MODULE_NNI_CONV_RELU_MODULEintr   boolr$   r&   r"   r   r>   __classcell__r,   r,   r*   r-   r	      s4            c                       sL   e Zd ZdZejZdZdZd fdd	Z	d	d
 Z
deeedddZ  ZS )r
   a|  A dynamically quantized conv module with floating point tensors as inputs and outputs.

    For details on input arguments, parameters, and implementation see
    :class:`~torch.nn.Conv2d` and :class:`~torch.nn.quantized.dynamic.Conv2d` and

    Attributes:
        weight (Tensor):     packed tensor derived from the learnable weight
                             parameter.
        scale (Tensor):      scalar for the output scale
        zero_point (Tensor): scalar for the output zero point

    See :class:`~torch.nn.Conv2d` for other attributes.

    Examples::

        >>> # With square kernels and equal stride
        >>> m = nn.quantized.dynamic.Conv2d(16, 33, 3, stride=2)
        >>> # non-square kernels and unequal stride and with padding
        >>> m = nn.quantized.dynamic.Conv2d(16, 33, (3, 5), stride=(2, 1), padding=(4, 2))
        >>> # non-square kernels and unequal stride and with padding and dilation
        >>> m = nn.quantized.dynamic.Conv2d(16, 33, (3, 5), stride=(2, 1), padding=(4, 2), dilation=(3, 1))
        >>> input = torch.randn(20, 16, 50, 100)
        >>> # xdoctest: +SKIP
        >>> output = m(input)

    Nr   r   Tr   c              
      sf   t d|   |
|d}t|}t|}t|}t|}tt| j|||||||||	f	| d S r   )r   r    r!   r"   r   r%   r
   r&   r'   r   r   r   r   r   r   r   r   r   r   r   r)   r*   r,   r-   r&   x   s.    

       zConv2d.__init__c                 C   s   dS )NZDynamicQuantizedConv2dr,   r.   r,   r,   r-   r"      s    zConv2d._get_namer/   c                 C   sN   t |jdkrtd| jdkr<t| j}tj||| jd}tj	
|| j|S )N   #Input shape must be `(N, C, H, W)`!r   r4   )r6   r7   r8   r   r   r   r9   r:   r   r;   Zconv2d_dynamicr<   r=   r,   r,   r-   r>      s    

  zConv2d.forward)r   r   r   r   Tr   NN)T)r?   r@   rA   rB   rC   r
   rD   rE   rF   r&   r"   r   rH   r>   rI   r,   r,   r*   r-   r
   Y   s                c                       sL   e Zd ZdZejZdZdZd fdd	Z	d	d
 Z
deeedddZ  ZS )r   a  A dynamically quantized conv module with floating point tensors as inputs and outputs.

    For details on input arguments, parameters, and implementation see
    :class:`~torch.nn.Conv3d` and :class:`~torch.nn.quantized.dynamic.Conv3d` and

    Attributes:
        weight (Tensor):     packed tensor derived from the learnable weight
                             parameter.
        scale (Tensor):      scalar for the output scale
        zero_point (Tensor): scalar for the output zero point

    See :class:`~torch.nn.Conv3d` for other attributes.

    Examples::

        >>> # With square kernels and equal stride
        >>> m = nn.quantized.dynamic.Conv3d(16, 33, 3, stride=2)
        >>> # non-square kernels and unequal stride and with padding
        >>> m = nn.quantized.dynamic.Conv3d(16, 33, (3, 5, 5), stride=(1, 2, 2), padding=(1, 2, 2))
        >>> # non-square kernels and unequal stride and with padding and dilation
        >>> m = nn.quantized.dynamic.Conv3d(16, 33, (3, 5, 5), stride=(1, 2, 2), padding=(1, 2, 2), dilation=(1, 2, 2))
        >>> input = torch.randn(20, 16, 56, 56, 56)
        >>> # xdoctest: +SKIP
        >>> output = m(input)

    Nr   r   Tr   c                    s~   t d|   |	dks$td|
|d}t|}t|}t|}t|}tt| j||||||dtd|||	f| d S )Nr   Zreflectz*Conv3d does not support reflection paddingr   Fr   )	r   r    r!   r"   AssertionErrorr   r%   r   _initrJ   r*   r,   r-   r&      s4    

         zConv3d.__init__c                 C   s   dS )NZDynamicQuantizedConv3dr,   r.   r,   r,   r-   r"      s    zConv3d._get_namer/   c                 C   sN   t |jdkrtd| jdkr<t| j}tj||| jd}tj	
|| j|S )N   z&Input shape must be `(N, C, D, H, W)`!r   r4   )r6   r7   r8   r   r   r   r9   r:   r   r;   Zconv3d_dynamicr<   r=   r,   r,   r-   r>      s    

  zConv3d.forward)r   r   r   r   Tr   NN)T)r?   r@   rA   rB   rC   r   rD   rE   rF   r&   r"   r   rH   r>   rI   r,   r,   r*   r-   r      s                c                	       sD   e Zd ZdZejZd fdd	Zd	d
 Zde	e
e	dddZ  ZS )r   a_  A dynamically quantized transposed convolution module with floating point tensors as inputs and outputs.

    For details on input arguments, parameters, and implementation see
    :class:`~torch.nn.ConvTranspose1d`.

    For special notes, please, see :class:`~torch.nn.quantized.dynamic.Conv1d`

    Attributes:
        weight (Tensor):     packed tensor derived from the learnable weight
                             parameter.
        scale (Tensor):      scalar for the output scale
        zero_point (Tensor): scalar for the output zero point
    See :class:`~torch.nn.ConvTranspose1d` for other attributes.

    Examples::

        >>> # With square kernels and equal stride
        >>> # xdoctest: +SKIP
        >>> m = nndq.ConvTranspose1d(16, 33, 3, stride=2)
        >>> # non-square kernels and unequal stride and with padding
        >>> m = nndq.ConvTranspose1d(16, 33, (3, 5), stride=(2, 1), padding=(4, 2))
        >>> output = m(input)
        >>> # exact output size can be also specified as an argument
        >>> downsample = nndq.Conv1d(16, 16, 3, stride=2, padding=1)
        >>> upsample = nndq.ConvTranspose1d(16, 16, 3, stride=2, padding=1)
        >>> h = downsample(input)
        >>> h.size()
        torch.Size([1, 16, 6])
        >>> output = upsample(h, output_size=input.size())
        >>> output.size()
        torch.Size([1, 16, 12])
    r   r   Tr   Nc                    sH   t d|   ||d}tt| j|||||||||	|
f
| d S r   )r   r    r!   r"   r%   r   r&   r'   r   r   r   r   r   Zoutput_paddingr   r   r   r   r   r   r)   r*   r,   r-   r&      s(    

        zConvTranspose1d.__init__c                 C   s   dS )NZDynamicQuantizedConvTranpose1dr,   r.   r,   r,   r-   r"     s    zConvTranspose1d._get_namer/   c                 C   s*   t |jdkrtdtjj|| j|S )Nr2   r3   )r6   r7   r8   torchr   r;   Zconv_transpose1d_dynamicr<   r'   r0   r(   r,   r,   r-   r>     s      zConvTranspose1d.forward)	r   r   r   r   Tr   r   NN)T)r?   r@   rA   rB   rC   r   rD   r&   r"   r   rH   r>   rI   r,   r,   r*   r-   r      s   !               c                	       sD   e Zd ZdZejZd fdd	Zd	d
 Zde	e
e	dddZ  ZS )r   ab  A dynamically quantized transposed convolution module with floating point tensors as inputs and outputs.

    For details on input arguments, parameters, and implementation see
    :class:`~torch.nn.ConvTranspose2d`.

    For special notes, please, see :class:`~torch.nn.quantized.dynamic.Conv2d`

    Attributes:
        weight (Tensor):     packed tensor derived from the learnable weight
                             parameter.
        scale (Tensor):      scalar for the output scale
        zero_point (Tensor): scalar for the output zero point
    See :class:`~torch.nn.ConvTranspose2d` for other attributes.

    Examples::

        >>> # With square kernels and equal stride
        >>> m = nnq.ConvTranspose2d(16, 33, 3, stride=2)
        >>> # non-square kernels and unequal stride and with padding
        >>> m = nnq.ConvTranspose2d(16, 33, (3, 5), stride=(2, 1), padding=(4, 2))
        >>> # xdoctest: +SKIP
        >>> output = m(input)
        >>> # exact output size can be also specified as an argument
        >>> downsample = nnq.Conv2d(16, 16, 3, stride=2, padding=1)
        >>> upsample = nnq.ConvTranspose2d(16, 16, 3, stride=2, padding=1)
        >>> h = downsample(input)
        >>> h.size()
        torch.Size([1, 16, 6, 6])
        >>> output = upsample(h, output_size=input.size())
        >>> output.size()
        torch.Size([1, 16, 12, 12])
    r   r   Tr   Nc                    sH   t d|   ||d}tt| j|||||||||	|
f
| d S r   )r   r    r!   r"   r%   r   r&   rP   r*   r,   r-   r&   <  s(    

        zConvTranspose2d.__init__c                 C   s   dS )NZDynamicQuantizedConvTranpose2dr,   r.   r,   r,   r-   r"   I  s    zConvTranspose2d._get_namer/   c                 C   s(   t |jdkrtdtj|| j|S )NrK   rL   )r6   r7   r8   r   r;   Zconv_transpose2d_dynamicr<   rR   r,   r,   r-   r>   L  s      zConvTranspose2d.forward)	r   r   r   r   Tr   r   NN)T)r?   r@   rA   rB   rC   r   rD   r&   r"   r   rH   r>   rI   r,   r,   r*   r-   r     s   !               c                	       sD   e Zd ZdZejZd fdd	Zd	d
 Zde	e
e	dddZ  ZS )r   ap  A dynamically quantized transposed convolution module with floating point tensors as inputs and outputs.

    For details on input arguments, parameters, and implementation see
    :class:`~torch.nn.ConvTranspose3d`.

    For special notes, please, see :class:`~torch.nn.quantized.dynamic.Conv3d`

    Attributes:
        weight (Tensor):     packed tensor derived from the learnable weight
                             parameter.
        scale (Tensor):      scalar for the output scale
        zero_point (Tensor): scalar for the output zero point
    See :class:`~torch.nn.ConvTranspose3d` for other attributes.

    Examples::

        >>> # With cubic kernels and equal stride
        >>> m = nnq.ConvTranspose3d(16, 33, 3, stride=2)
        >>> # non-cubic kernels and unequal stride and with padding
        >>> m = nnq.ConvTranspose3d(16, 33, (3, 3, 5), stride=(2, 1, 1), padding=(4, 2, 2))
        >>> # xdoctest: +SKIP
        >>> output = m(input)
        >>> # exact output size can be also specified as an argument
        >>> downsample = nnq.Conv3d(16, 16, 3, stride=2, padding=1)
        >>> upsample = nnq.ConvTranspose3d(16, 16, 3, stride=2, padding=1)
        >>> h = downsample(input)
        >>> h.size()
        torch.Size([1, 16, 6, 6, 6])
        >>> output = upsample(h, output_size=input.size())
        >>> output.size()
        torch.Size([1, 16, 12, 12, 12])
    r   r   Tr   Nc                    sH   t d|   ||d}tt| j|||||||||	|
f
| d S r   )r   r    r!   r"   r%   r   r&   rP   r*   r,   r-   r&   y  s(    

        zConvTranspose3d.__init__c                 C   s   dS )NZDynamicQuantizedConvTranpose3dr,   r.   r,   r,   r-   r"     s    zConvTranspose3d._get_namer/   c                 C   s(   t |jdkrtdtj|| j|S )NrO   z&Input shape must be `(N, C, T, H, W)`!)r6   r7   r8   r   r;   Zconv_transpose3d_dynamicr<   rR   r,   r,   r-   r>     s      zConvTranspose3d.forward)	r   r   r   r   Tr   r   NN)T)r?   r@   rA   rB   rC   r   rD   r&   r"   r   rH   r>   rI   r,   r,   r*   r-   r   U  s   !               )rB   rQ   Ztorch.nnrC   Ztorch.nn.functionalZ
functionalr9   r   Z
torch._opsr   Ztorch.nn.common_typesr   Ztorch.nn.modules.utilsr   r   r   Z"torch.ao.nn.quantized.modules.convr   Ztorch.ao.nn.quantizedZaor;   Znnqr   __all__r	   r
   r   r   r   r   r,   r,   r,   r-   <module>   s"   GAA==