
    Vh                        d Z ddlmZmZ ddlZddlmc mc mZ	 ddl
mc mc mc mZ ddlmZ ddlmc 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ZmZ g d
ZddhZde e!   de e!   fdZ" G d de      Z# G d de#      Z$ G d de#      Z% G d de#      Z& G d de#      Z' G d de'      Z( G d de'      Z) G d de'      Z*y) zQuantized convolution modules.    )ClassVarOptionalN)ops)	_size_1_t)_pair_single_triple)fuse_conv_bn_weights   )_quantize_weightWeightedQuantizedModule)Conv1dConv2dConv3dConvTranspose1dConvTranspose2dConvTranspose3dzerosreflectpaddingreturnc                      g }t               t              D ]'  |j                   fdt        d      D               ) |S )Nc              3   4   K   | ]  }z
  d z
       yw)r   N ).0_Nidxr   s     R/home/dcms/DCMS/lib/python3.12/site-packages/torch/ao/nn/quantized/modules/conv.py	<genexpr>z*_reverse_repeat_padding.<locals>.<genexpr>#   s     /WC!0D/Ws      )lenrangeextend)r    _reversed_padding_repeated_twicer   r   s   ` @@r   _reverse_repeat_paddingr&      sF    24$GAQx X(///WeTUh/WWX++    c                       e Zd Z	 	 	 	 	 	 	 	 ddZ	 	 	 d	 d fdZd Zd Zd Zd Z fdZ	e
j                  j                  d        Z fd	Ze
j                  j                  d
        Zd Zd Zedd       Zedd       Zed        Z xZS )_ConvNdc                     t         NNotImplementedError)selfin_channelsout_channelskernel_sizestrider   dilationgroupsbiaspadding_modedevicedtypes               r   __init__z_ConvNd.__init__(   s
     "!r'   c           
      D   ||d}t         |           ||	z  dk7  rt        d      ||	z  dk7  rt        d      || _        || _        || _        || _        || _        || _        || _	        || _
        |	| _        |t        vrt        d| d      || _        | j                  r||| j                  z  g}n||| j                  z  g}t        j                  |t!        |      z   fddt        j"                  d|j%                         D ci c]  \  }}|d	k7  s|| c}}}|
rNt        j&                  |fd	t        j(                  i|j%                         D ci c]  \  }}|d	k7  s|| c}}nd }| j+                  ||       d
| _        d| _        y c c}}w c c}}w )Nr7   r8   r   z'in_channels must be divisible by groupsz(out_channels must be divisible by groupsz'padding_mode' z* is not supported by quantized convolutionr   )scale
zero_pointr8   r8   g      ?)superr9   
ValueErrorr/   r0   r1   r2   r   r3   
transposedoutput_paddingr4   _SUPPORTED_PADDINGr6   torch_empty_affine_quantizedlistqint8itemsr   floatset_weight_biasr<   r=   )r.   r/   r0   r1   r2   r   r3   r@   rA   r4   r5   r6   r7   r8   factory_kwargsweight_shapekvqweight
bias_float	__class__s                       r   _initz_ConvNd._init9   s     %+U;1$FGG& A%GHH&(& $,11!,/YZ  )??')DEL(+*DEL//4,,
++	

 !/ 4 4 6G1!w,q!tG
  KKkk %3$8$8$:KDAqa7l1a4K  	 	Wj1
 H Ls   
FFF(Fc                     t         r+   r,   )r.   rN   rO   s      r   rI   z_ConvNd.set_weight_biasx       !!r'   c                     t         r+   r,   r.   s    r   r5   z_ConvNd.bias{   rS   r'   c                     t         r+   r,   rU   s    r   _weight_biasz_ConvNd._weight_bias~   rS   r'   c                    d}| j                   dt        | j                         z  k7  r|dz  }| j                  dt        | j                        z  k7  r|dz  }| j                  dt        | j                        z  k7  r|dz  }| j                  dk7  r|dz  }| j                         |d	z  } |j                  d
i | j                  S )Nzq{in_channels}, {out_channels}, kernel_size={kernel_size}, stride={stride}, scale={scale}, zero_point={zero_point})r   z, padding={padding})r   z, dilation={dilation}z!, output_padding={output_padding}r   z, groups={groups}z, bias=Falser   )r   r"   r3   rA   r4   r5   format__dict__)r.   ss     r   
extra_reprz_ConvNd.extra_repr   s    H 	
 <<4#dll"333&&A==D3t}}#555((A$T-@-@)A"AA44A;;!$$A99;Aqxx($--((r'   c                    t         |   |||       | j                         \  }}|||dz   <   |||dz   <   t        j                  | j
                        ||dz   <   t        j                  | j                        ||dz   <   y )Nweightr5   r<   r=   )r>   _save_to_state_dictrW   rC   tensorr<   r=   )r.   destinationprefix	keep_varswbrP   s         r   r_   z_ConvNd._save_to_state_dict   s{    #KC""$A)*FX%&'(FVO$(-TZZ(@FW$%-2\\$//-JF\)*r'   c                 N   | j                         \  }}| j                  | j                  | j                  | j                  | j
                  | j                  | j                  | j                  | j                  | j                  ||| j                  | j                  | j                  fS r+   )rW   r/   r0   r1   r2   r   r3   r@   rA   r4   r6   r<   r=   trainingr.   rd   re   s      r   __getstate__z_ConvNd.__getstate__   s    ""$AKKLLMMOOKKJJOOMM
 	
r'   c           	      d   | j                  ||dz      ||dz             |j                  |dz          |j                  |dz          t        ||dz            | _        |j                  |dz          t	        ||dz            | _        |j                  |dz          t        |   |||d|||       y )Nr^   r5   r<   r=   F)rI   poprH   r<   intr=   r>   _load_from_state_dict)	r.   
state_dictrb   local_metadatastrictmissing_keysunexpected_keys
error_msgsrP   s	           r   rm   z_ConvNd._load_from_state_dict   s     	Z(9:JvPV<WXv()v':fw&678
v'(j,)>?@v,-%	
r'   c                 8   |d   | _         |d   | _        |d   | _        |d   | _        |d   | _        |d   | _        |d   | _        |d   | _        |d	   | _        |d
   | _	        | j                  |d   |d          |d   | _        |d   | _        |d   | _        y )Nr   r   r!                     	   
               )r/   r0   r1   r2   r   r3   r@   rA   r4   r6   rI   r<   r=   rg   )r.   states     r   __setstate__z_ConvNd.__setstate__   s     8!!H 8AhQxa(#AhAh!!HU2Yb	22Y
)b	r'   c                     t        |       j                  t        |             }t        j                  j                  j                  |       | j                         }|j                  |       |S r+   )type__new__rC   nnModuler9   ri   r   )r.   memonew_instancer   s       r   __deepcopy__z_ConvNd.__deepcopy__   sR    Dz))$t*5  .!!#!!%(r'   c                 $    | j                  i       S r+   )r   rU   s    r   __copy__z_ConvNd.__copy__   s      $$r'   c                    ||j                   j                         } ||j                         |j                  t        j                  k(  sJ d       t        |j                  j                         |      } | |j                  |j                  |j                  |j                  |j                  |j                  |j                  |j                  du|j                  	      }|j!                  ||j                         ||j                  t        j                  k(  r|S |j#                         \  }}t        |      |_        t'        |      |_        |S )z&Creates a qconv object and returns it.N*Weight observer must have a dtype of qint8)qconfigr^   r8   rC   rF   r   rH   r/   r0   r1   r2   r   r3   r4   r5   r6   rI   calculate_qparamsr<   rl   r=   )clsmodactivation_post_processweight_post_processrN   qconv	act_scaleact_zps           r   	get_qconvz_ConvNd.get_qconv   s    &"%++"4"4"6CJJ'%%4	87	84"3::#3#3#57JKOOOOJJKKLLJJHHD 

 	gsxx0#+&,,;L 7 I I KIv	*EK"6{ELr'   c           	         t        |d      rt        |      | j                  k(  rt        |j                  |j
                  |j                  j                  |j                  j                  |j                  j                  |j                  j                  |j                  j
                        \  |_        |_        t        |d      sJ d       |j                  }|j                  }nt        |      | j                  k(  sFJ d| j                  z   dz   | j                  j                  z   dz   t        t        |            z          t        |d      sJ d       t        |d      sd n|j                  }t        |      | j                  | j                   | j"                  fv r|d	   }|j$                  j	                         }| j'                  |||      S )
Nweight_fake_quantr   z,Input QAT module must have observer attached nnq..from_float only works for z	 but got:r   -Input float module must have qconfig defined.r   )hasattrr   _NNIQAT_CONV_BN_MODULEr
   r^   r5   bnrunning_meanrunning_varepsr   r   _FLOAT_MODULE__name__str_NNI_CONV_RELU_MODULE_NNI_CONV_ADD_MODULE_NNI_CONV_ADD_RELU_MODULEr   r   )r   r   use_precomputed_fake_quantr   r   s        r   
from_floatz_ConvNd.from_float  s   3+, CyC666';JJHHFF''FF&&FFJJFFMMFFKK($
CH . >=>  #&"7"7&)&A&A#9 1 11 ,,/0 ##,,- 	
 d3i.!1 Y ?>? 
 s$=> 00 $
 Cy))((-- 
 !f"%++"4"4"6}}S"9;NOOr'   c                     | |j                   |j                  |j                  |j                  |j                  |j
                  |j                  |j                  du|j                  |j                  j                  |j                  j                        }|j                         }|j                  ||j                         t        |      |_        t!        |      |_        |S )a  Create a (fbgemm/qnnpack) quantized module from a reference quantized module
        Args:
            ref_qconv (Module): a reference quantized  module, either produced by torch.ao.quantization
                                utilities or provided by the user
            output_scale (float): scale for output Tensor
            output_zero_point (int): zero point for output Tensor
        Nr;   )r/   r0   r1   r2   r   r3   r4   r5   r6   r^   r7   r8   get_quantized_weightrI   rH   r<   rl   r=   )r   	ref_qconvoutput_scaleoutput_zero_pointr   rN   s         r   from_referencez_ConvNd.from_referenceD  s     !!""!!NN$&""##**""((
 002gy~~6L)01r'   r   r   r   r   Tr   NN)r   NN)r   Nr+   F)r   
__module____qualname__r9   rQ   rI   r5   rW   r\   r_   rC   jitexportri   rm   r   r   r   classmethodr   staticmethodr   r   __classcell__rP   s   @r   r)   r)   '   s     ": = 
=~"""):K YY
 
.
6 YY" " %  B +P +PZ  r'   r)   c                       e Zd ZU dZej
                  Zeeej
                        e	d<   e
j                  Zeeeej                           e	d<   ej                   Zeeeej                           e	d<   dZeeeej                           e	d<   dZeeeej                           e	d<   	 	 	 	 	 	 	 	 dded	ed
ededededededef fdZd Zdej6                  deej6                     ddfdZd Zd Zd Zd Z e!dd       Z" xZ#S )r   a`  Applies a 1D convolution over a quantized input signal composed of
    several quantized input planes.

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

    .. note::
        Only `zeros` is supported for the :attr:`padding_mode` argument.

    .. note::
        Only `torch.quint8` is supported for the input data type.


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

        >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_QENGINE)
        >>> m = nn.quantized.Conv1d(16, 33, 3, stride=2)
        >>> input = torch.randn(20, 16, 100)
        >>> # quantize input to quint8
        >>> # xdoctest: +SKIP
        >>> q_input = torch.quantize_per_tensor(input, scale=1.0, zero_point=0,
        ...                                     dtype=torch.quint8)
        >>> output = m(q_input)

    r   r   r   Nr   r   r/   r0   r1   r2   r   r3   r4   r5   r6   c                     |
|d}t        |      }t        |      }t        |t              r|n
t        |      }t        |      }t        |   ||||||dt        d      |||	fi | y Nr;   Fr   )r   
isinstancer   r>   rQ   r.   r/   r0   r1   r2   r   r3   r4   r5   r6   r7   r8   rJ   rP   s                r   r9   zConv1d.__init__  s~     %+U;k*'5'77;K8$ 	AJ	
 	
r'   c                      y)NQuantizedConv1dr   rU   s    r   	_get_namezConv1d._get_name       r'   rd   re   r   c                    | j                   dk(  r\t        j                  j                  j	                  ||| j
                  | j                  | j                  | j                        | _	        y t        j                  j                  j	                  ||| j
                  t        d      | j                  | j                        | _	        y Nr   r   )r6   rC   r   	quantizedconv1d_prepackr2   r   r3   r4   _packed_paramsr   rh   s      r   rI   zConv1d.set_weight_bias      '"'))"5"5"D"D1dkk4<<#D #())"5"5"D"D1dkk58T]]DKK#Dr'   c                 v    t         j                  j                  j                  | j                        \  }}||fS r+   )rC   r   r   conv1d_unpackr   rh   s      r   rW   zConv1d._weight_bias  /    yy""001D1DE1!tr'   c                 (    | j                         d   S Nr   rW   rU   s    r   r^   zConv1d.weight        "1%%r'   c                 (    | j                         d   S Nr   r   rU   s    r   r5   zConv1d.bias  r   r'   c                 Z   t        |j                        dk7  rt        d      | j                  dk7  r:t	        | j
                  d d       }t        j                  ||| j                        }t        j                  j                  || j                  | j                  | j                        S )Nru    Input shape must be `(N, C, L)`!r   r   mode)r"   shaper?   r6   r&   r   Fpadr   r   conv1dr   r<   r=   r.   inputr%   s      r   forwardzConv1d.forward  s     u{{q ?@@'/Ft||TVUVGW/X,EE7d>O>OE }}##4&&

DOO
 	
r'   c                 2    t         j                  | ||      S zCreates a quantized module from a float module or qparams_dict.

        Args:
            mod (Module): a float module, either produced by torch.ao.quantization
              utilities or provided by the user
        )r   r)   r   r   r   r   s      r   r   zConv1d.from_float  $     !!1K " 
 	
r'   r   r   )$r   r   r   __doc__r   r   r   r   r   __annotations__nniqatConvBn1dr   r   r   nni
ConvReLU1dr   r   r   rl   r   boolr   r9   r   rC   TensorrI   rW   r^   r5   r   r   r   r   r   s   @r   r   r   a  sn    D 02yyM8DO,8BH//HXd299o%>?QAD8HT"))_$=>O@D(8DO#<=DEIxbii(ABI ##
#
 #
 	#

 #
 #
 #
 #
 #
 #
J! (5<<2H T &&
 	
 	
r'   r   c                       e Zd ZU dZej
                  Zeeej
                        e	d<   e
j                  Zeeeej                           e	d<   ej                   Zeeeej                           e	d<   ej$                  Zeeej$                        e	d<   ej(                  Zeeej(                        e	d<   	 	 	 	 	 	 	 	 d fd	Zd	 Zd
ej2                  deej2                     ddfdZd Zd Zd Zd Zedd       Z  xZ!S )r   a  Applies a 2D convolution over a quantized input signal composed of
    several quantized input planes.

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

    .. note::
        Only `zeros` is supported for the :attr:`padding_mode` argument.

    .. note::
        Only `torch.quint8` is supported for the input data type.


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

        >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_QENGINE)
        >>> # With square kernels and equal stride
        >>> m = nn.quantized.Conv2d(16, 33, 3, stride=2)
        >>> # non-square kernels and unequal stride and with padding
        >>> m = nn.quantized.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.Conv2d(16, 33, (3, 5), stride=(2, 1), padding=(4, 2), dilation=(3, 1))
        >>> input = torch.randn(20, 16, 50, 100)
        >>> # quantize input to quint8
        >>> # xdoctest: +SKIP
        >>> q_input = torch.quantize_per_tensor(input, scale=1.0, zero_point=0, dtype=torch.quint8)
        >>> output = m(q_input)

    r   r   r   r   r   Nc                     |
|d}t        |      }t        |      }t        |      }t        |      }t        |   ||||||dt        d      |||	fi | y r   )r   r>   rQ   r   s                r   r9   zConv2d.__init__  sq     %+U;K(v.? 	!H	
 	
r'   c                      y)NQuantizedConv2dr   rU   s    r   r   zConv2d._get_name2  r   r'   rd   re   r   c                    | j                   dk(  r\t        j                  j                  j	                  ||| j
                  | j                  | j                  | j                        | _	        y t        j                  j                  j	                  ||| j
                  t        d      | j                  | j                        | _	        y r   )r6   rC   r   r   conv2d_prepackr2   r   r3   r4   r   r   rh   s      r   rI   zConv2d.set_weight_bias5  r   r'   c                 6    | j                   j                         S r+   r   unpackrU   s    r   rW   zConv2d._weight_bias?      ""))++r'   c                 (    | j                         d   S r   r   rU   s    r   r^   zConv2d.weightB  r   r'   c                 (    | j                         d   S r   r   rU   s    r   r5   zConv2d.biasE  r   r'   c                 T   t        |j                        dk7  rt        d      | j                  dk7  r7t	        | j
                        }t        j                  ||| j                        }t        j                  j                  || j                  | j                  | j                        S )Nrv   #Input shape must be `(N, C, H, W)`!r   r   )r"   r   r?   r6   r&   r   r   r   r   r   conv2dr   r<   r=   r   s      r   r   zConv2d.forwardH  s     u{{q BCC'/Ft||/T,EE7d>O>OE }}##4&&

DOO
 	
r'   c                 2    t         j                  | ||      S r   r   r   s      r   r   zConv2d.from_floatV  r   r'   r   r   )"r   r   r   r   r   r   r   r   r   r   r   ConvBn2dr   r   r   r   
ConvReLU2dr   	ConvAdd2dr   ConvAddReLU2dr   r9   r   rC   r   rI   rW   r^   r5   r   r   r   r   r   s   @r   r   r     s   $J 02yyM8DO,8BH//HXd299o%>?QAD8HT"))_$=>O:=--(4#67GCFCTCTxS->->(?@T "
H! (5<<2H T ,&&
 	
 	
r'   r   c                       e Zd ZU dZej
                  Zeeej
                        e	d<   e
j                  Zeeeej                           e	d<   ej                   Zeeeej                           e	d<   dZeeeej                           e	d<   dZeeeej                           e	d<   	 	 	 	 	 	 	 	 d fd	Zd	 Zd
ej.                  deej.                     ddfdZd Zd Zd Zd Zedd       Z xZS )r   a  Applies a 3D convolution over a quantized input signal composed of
    several quantized input planes.

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

    .. note::
        Only `zeros` is supported for the :attr:`padding_mode` argument.

    .. note::
        Only `torch.quint8` is supported for the input data type.


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

        >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_QENGINE)
        >>> # With square kernels and equal stride
        >>> m = nn.quantized.Conv3d(16, 33, 3, stride=2)
        >>> # non-square kernels and unequal stride and with padding
        >>> m = nn.quantized.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.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)
        >>> # quantize input to quint8
        >>> # xdoctest: +SKIP
        >>> q_input = torch.quantize_per_tensor(input, scale=1.0, zero_point=0, dtype=torch.quint8)
        >>> output = m(q_input)

    r   r   r   Nr   r   c                     |	dk7  sJ d       |
|d}t        |      }t        |      }t        |      }t        |      }t        |   ||||||dt        d      |||	fi | y )Nr   z*Conv3d does not support reflection paddingr;   Fr   )r	   r>   rQ   r   s                r   r9   zConv3d.__init__  s     y(V*VV($*U;k*'"8$ 	AJ	
 	
r'   c                      y)NQuantizedConv3dr   rU   s    r   r   zConv3d._get_name  r   r'   rd   re   r   c                    | j                   dk(  r\t        j                  j                  j	                  ||| j
                  | j                  | j                  | j                        | _	        y t        j                  j                  j	                  ||| j
                  t        d      | j                  | j                        | _	        y r   )r6   rC   r   r   conv3d_prepackr2   r   r3   r4   r   r	   rh   s      r   rI   zConv3d.set_weight_bias  s    '"'))"5"5"D"D1dkk4<<#D #())"5"5"D"D1dkk71:t}}dkk#Dr'   c                 6    | j                   j                         S r+   r   rU   s    r   rW   zConv3d._weight_bias  r   r'   c                 (    | j                         d   S r   r   rU   s    r   r^   zConv3d.weight  r   r'   c                 (    | j                         d   S r   r   rU   s    r   r5   zConv3d.bias  r   r'   c                 T   t        |j                        dk7  rt        d      | j                  dk7  r7t	        | j
                        }t        j                  ||| j                        }t        j                  j                  || j                  | j                  | j                        S )Nrw   z&Input shape must be `(N, C, D, H, W)`!r   r   )r"   r   r?   r6   r&   r   r   r   r   r   conv3dr   r<   r=   r   s      r   r   zConv3d.forward  s     u{{q EFF'/Ft||/T,EE7d>O>OE }}##4&&

DOO
 	
r'   c                 2    t         j                  | ||      S r   r   r   s      r   r   zConv3d.from_float  r   r'   r   r   ) r   r   r   r   r   r   r   r   r   r   r   ConvBn3dr   r   r   r   
ConvReLU3dr   r   r   r9   r   rC   r   rI   rW   r^   r5   r   r   r   r   r   s   @r   r   r   c  s   $J 02yyM8DO,8BH//HXd299o%>?QAD8HT"))_$=>O@D(8DO#<=DEIxbii(ABI #
J! (5<<2H T ,&&
 	
 	
r'   r   c            	            e Zd ZU eeej                  j                  j                        e	d<   	 	 d
 fd	Z
dee   dee   dee   dee   fdZedd       Zed	        Z xZS )_ConvTransposeNdr   c                     |dk7  r"t        d| j                  j                         ||d}t        |   |||||||||	|
|fi | y )Nr   z+Only "zeros" padding mode is supported for r;   )r?   rP   r   r>   rQ   )r.   r/   r0   r1   r2   r   r3   r@   rA   r4   r5   r6   r7   r8   rJ   rP   s                  r   r9   z_ConvTransposeNd.__init__  st      7"=dnn>U>U=VW  %+U; 		
 	
r'   r1   r3   r   r   c                     t         j                  j                  t        t           g       }t        t        |            D ]'  }||   ||   dz
  z  ||   z
  }|j                  |       ) |S r   )rC   r   annotaterE   rl   r#   r"   append)r.   r1   r3   r   reskdxr   s          r   _input_paddingz_ConvTransposeNd._input_padding  sj     ii  cB/[)* 	C3-;s#3a#7873<GCJJsO	 
r'   c                    d| j                   z   dz   | j                  j                   z   }t        |      | j                  k(  sJ |       t        |d      sJ d       |j                  j                         } ||j
                         |j                  t        j                  k(  sJ d       t        |j
                  j                         |      } | |j                  |j                  |j                  |j                  |j                  |j                   |j"                  |j$                  du|j&                  |j(                  
      }|j+                  ||j$                         t        |d      r'|j,                  j                  t        j                  k(  r|S |j,                  j/                         \  }}t        |      |_        t3        |      |_        |S )zCreates a quantized module from a float module or qparams_dict.
        Args:
            mod (Module): a float module, either produced by torch.ao.quantization
              utilities or provided by the user
        r   r   r   r   r   Nr   )r   r   r   r   r   r^   r8   rC   rF   r   rH   r/   r0   r1   r2   r   rA   r4   r5   r3   r6   rI   r   r   r<   rl   r=   )	r   r   r   msgr   rN   r   r   r   s	            r   r   z_ConvTransposeNd.from_float  s    ll+, (() 	 CyC---2s2-sI&W(WW&!kk002CJJ'%%4	87	84"3::#3#3#57JKOOOOJJKKJJHHD LL
 	gsxx067**00EKK?L # ; ; M M OIv	*EK"6{ELr'   c                     | |j                   |j                  |j                  |j                  |j                  |j
                  |j                  |j                  du|j                  |j                  |j                  j                  |j                  j                        }|j                         }|j                  ||j                         t        |      |_        t#        |      |_        |S )a  Create a (fbgemm/qnnpack) quantized module from a reference quantized module
        Args:
            ref_qconvt (Module): a reference quantized  module, either produced by torch.ao.quantization
                                 utilities or provided by the user
            output_scale (float): scale for output Tensor
            output_zero_point (int): zero point for output Tensor
        Nr;   )r/   r0   r1   r2   r   rA   r4   r5   r3   r6   r^   r7   r8   r   rI   rH   r<   rl   r=   )r   
ref_qconvtr   r   r   rN   s         r   r   z_ConvTransposeNd.from_referenceI  s     ""##""%%OO4'##$$++##))
 113gz7L)01r'   )NNr   )r   r   r   r   r   r   modulesconvr)   r   r9   rE   rl   r  r   r   r   r   r   r   s   @r   r  r    s    D!8!89:: $
L904S	DHI	c , ,\  r'   r  c                        e Zd ZU dZej
                  Zeeej
                        e	d<   	 	 	 	 	 	 	 	 	 d fd	Z
d Zdej                  deej                     ddfd	Zd
 Zd Zd Zd Zed        Z xZS )r   a  Applies a 1D transposed convolution operator over an input image
    composed of several input planes.
    For details on input arguments, parameters, and implementation see
    :class:`~torch.nn.ConvTranspose1d`.

    .. note:: Currently only the QNNPACK engine is implemented.
        Please, set the `torch.backends.quantized.engine = 'qnnpack'`

    For special notes, please, see :class:`~torch.ao.nn.quantized.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.ConvTranspose2d` for other attributes.

    Examples::

        >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_QENGINE)
        >>> torch.backends.quantized.engine = 'qnnpack'
        >>> from torch.ao.nn import quantized as nnq
        >>> # With square kernels and equal stride
        >>> m = nnq.ConvTranspose1d(16, 33, 3, stride=2)
        >>> # non-square kernels and unequal stride and with padding
        >>> m = nnq.ConvTranspose1d(16, 33, (3, 5), stride=(2, 1), padding=(4, 2))
        >>> input = torch.randn(20, 16, 50)
        >>> q_input = torch.quantize_per_tensor(input, scale=1.0, zero_point=0, dtype=torch.quint8)
        >>> output = m(q_input)
        >>> # exact output size can be also specified as an argument
        >>> input = torch.randn(1, 16, 12)
        >>> q_input = torch.quantize_per_tensor(input, scale=1.0, zero_point=0, dtype=torch.quint8)
        >>> downsample = nnq.Conv1d(16, 16, 3, stride=2, padding=1)
        >>> upsample = nnq.ConvTranspose1d(16, 16, 3, stride=2, padding=1)
        >>> h = downsample(q_input)
        >>> h.size()
        torch.Size([1, 16, 6])
        >>> # xdoctest: +SKIP("FIXME: output_size is not a parameter)
        >>> output = upsample(h, output_size=input.size())
        >>> output.size()
        torch.Size([1, 16, 12])
    r   Nc                     ||d}t        |      }t        |      }t        |      }t        |	      }	t        |      }t        |   ||||||	d||||
fi | y Nr;   T)r   r>   r9   r.   r/   r0   r1   r2   r   rA   r4   r5   r3   r6   r7   r8   rJ   rP   s                 r   r9   zConvTranspose1d.__init__  x     %+U;k*'"8$ 0	
 	
r'   c                      y)NQuantizedConvTranspose1dr   rU   s    r   r   zConvTranspose1d._get_name      )r'   rd   re   r   c           	          t         j                  j                  j                  ||| j                  | j
                  | j                  | j                  | j                        | _	        y r+   )
rC   r   r   conv_transpose1d_prepackr2   r   rA   r3   r4   r   rh   s      r   rI   zConvTranspose1d.set_weight_bias  J    #ii11JJKKLLMMKK
r'   c                 v    t         j                  j                  j                  | j                        \  }}||fS r+   )rC   r   r   conv_transpose1d_unpackr   rh   s      r   rW   zConvTranspose1d._weight_bias  s/    yy""::4;N;NO1!tr'   c                 ,    | j                         \  }}|S r+   r   r.   rd   r   s      r   r^   zConvTranspose1d.weight      ""$Ar'   c                 ,    | j                         \  }}|S r+   r   r.   r   re   s      r   r5   zConvTranspose1d.bias  r,  r'   c                     t        |j                        dk7  rt        d      t        j                  j
                  j                  || j                  | j                  | j                        S )Nru   r   )
r"   r   r?   rC   r   r   conv_transpose1dr   r<   r=   r.   r   s     r   r   zConvTranspose1d.forward  sU     u{{q ?@@yy""334&&

DOO
 	
r'   c                 2    t         j                  | |||      S r+   r  r   r   r  r   r   s       r   r   zConvTranspose1d.from_reference      ..\+<
 	
r'   	r   r   r   r   Tr   r   NN)r   r   r   r   r   r   r   r   r   r   r9   r   rC   r   r   rI   rW   r^   r5   r   r   r   r   r   s   @r   r   r   g      )V 9;8J8JM8D!3!345J #
J*	
 	
(5<<2H 	
T 	

 
 
r'   r   c                        e Zd ZU dZej
                  Zeeej
                        e	d<   	 	 	 	 	 	 	 	 	 d fd	Z
d Zdej                  deej                     ddfd	Zd
 Zd Zd Zd Zed        Z xZS )r   a~  Applies a 2D transposed convolution operator over an input image
    composed of several input planes.
    For details on input arguments, parameters, and implementation see
    :class:`~torch.nn.ConvTranspose2d`.

    For special notes, please, see :class:`~torch.ao.nn.quantized.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::

        >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_QENGINE)
        >>> # QNNPACK or FBGEMM as backend
        >>> torch.backends.quantized.engine = 'qnnpack'
        >>> # With square kernels and equal stride
        >>> import torch.ao.nn.quantized as nnq
        >>> 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))
        >>> input = torch.randn(20, 16, 50, 100)
        >>> q_input = torch.quantize_per_tensor(input, scale=1.0, zero_point=0, dtype=torch.quint8)
        >>> output = m(q_input)
        >>> # exact output size can be also specified as an argument
        >>> input = torch.randn(1, 16, 12, 12)
        >>> q_input = torch.quantize_per_tensor(input, scale=1.0, zero_point=0, dtype=torch.quint8)
        >>> downsample = nnq.Conv2d(16, 16, 3, stride=2, padding=1)
        >>> upsample = nnq.ConvTranspose2d(16, 16, 3, stride=2, padding=1)
        >>> h = downsample(q_input)
        >>> h.size()
        torch.Size([1, 16, 6, 6])
        >>> # xdoctest: +SKIP("FIXME: output_size is not a parameter)
        >>> output = upsample(h, output_size=input.size())
        >>> output.size()
        torch.Size([1, 16, 12, 12])
    r   Nc                     ||d}t        |      }t        |      }t        |      }t        |	      }	t        |      }t        |   ||||||	d||||
fi | y r  )r   r>   r9   r   s                 r   r9   zConvTranspose2d.__init__  sv     %+U;K(v.?~.	
 	
r'   c                      y)NQuantizedConvTranspose2dr   rU   s    r   r   zConvTranspose2d._get_name5  r$  r'   rd   re   r   c           	          t         j                  j                  j                  ||| j                  | j
                  | j                  | j                  | j                        | _	        y r+   )
rC   r   r   conv_transpose2d_prepackr2   r   rA   r3   r4   r   rh   s      r   rI   zConvTranspose2d.set_weight_bias8  r'  r'   c                 v    t         j                  j                  j                  | j                        \  }}||fS r+   )rC   r   r   conv2d_unpackr   rh   s      r   rW   zConvTranspose2d._weight_biasC  r   r'   c                 ,    | j                         \  }}|S r+   r   r+  s      r   r^   zConvTranspose2d.weightG  r,  r'   c                 ,    | j                         \  }}|S r+   r   r.  s      r   r5   zConvTranspose2d.biasK  r,  r'   c                     t        |j                        dk7  rt        d      t        j                  j                  || j                  | j                  | j                        S )Nrv   r   )	r"   r   r?   r   r   conv_transpose2dr   r<   r=   r1  s     r   r   zConvTranspose2d.forwardO  sO     u{{q BCC}}--4&&

DOO
 	
r'   c                 2    t         j                  | |||      S r+   r3  r4  s       r   r   zConvTranspose2d.from_referenceX  r5  r'   r6  )r   r   r   r   r   r   r   r   r   r   r9   r   rC   r   r   rI   rW   r^   r5   r   r   r   r   r   s   @r   r   r     s    'R 9;8J8JM8D!3!345J #
J*	
 	
(5<<2H 	
T 	

 
 
r'   r   c                        e Zd ZU dZej
                  Zeeej
                        e	d<   	 	 	 	 	 	 	 	 	 d fd	Z
d Zdej                  deej                     ddfd	Zd
 Zd Zd Zd Zed        Z xZS )r   a  Applies a 3D transposed convolution operator over an input image
    composed of several input planes.
    For details on input arguments, parameters, and implementation see
    :class:`~torch.nn.ConvTranspose3d`.

    .. note:: Currently only the FBGEMM engine is implemented.
        Please, set the `torch.backends.quantized.engine = 'fbgemm'`

    For special notes, please, see :class:`~torch.ao.nn.quantized.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::

        >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_QENGINE)
        >>> torch.backends.quantized.engine = 'fbgemm'
        >>> from torch.ao.nn import quantized as nnq
        >>> # 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))
        >>> input = torch.randn(20, 16, 50, 100, 100)
        >>> q_input = torch.quantize_per_tensor(input, scale=1.0, zero_point=0, dtype=torch.quint8)
        >>> output = m(q_input)
        >>> # exact output size can be also specified as an argument
        >>> input = torch.randn(1, 16, 12, 12, 12)
        >>> q_input = torch.quantize_per_tensor(input, scale=1.0, zero_point=0, dtype=torch.quint8)
        >>> downsample = nnq.Conv3d(16, 16, 3, stride=2, padding=1)
        >>> upsample = nnq.ConvTranspose3d(16, 16, 3, stride=2, padding=1)
        >>> h = downsample(q_input)
        >>> h.size()
        torch.Size([1, 16, 6, 6, 6])
        >>> # xdoctest: +SKIP("FIXME: output_size is not a parameter)
        >>> output = upsample(h, output_size=input.size())
        >>> output.size()
        torch.Size([1, 16, 12, 12, 12])
    r   Nc                     ||d}t        |      }t        |      }t        |      }t        |	      }	t        |      }t        |   ||||||	d||||
fi | y r  )r	   r>   r9   r   s                 r   r9   zConvTranspose3d.__init__  r!  r'   c                      y)NQuantizedConvTranspose3dr   rU   s    r   r   zConvTranspose3d._get_name  r$  r'   rd   re   r   c           	          t         j                  j                  j                  ||| j                  | j
                  | j                  | j                  | j                        | _	        y r+   )
rC   r   r   conv_transpose3d_prepackr2   r   rA   r3   r4   r   rh   s      r   rI   zConvTranspose3d.set_weight_bias  r'  r'   c                 v    t         j                  j                  j                  | j                        \  }}||fS r+   )rC   r   r   conv3d_unpackr   rh   s      r   rW   zConvTranspose3d._weight_bias  r   r'   c                 ,    | j                         \  }}|S r+   r   r+  s      r   r^   zConvTranspose3d.weight  r,  r'   c                 ,    | j                         \  }}|S r+   r   r.  s      r   r5   zConvTranspose3d.bias  r,  r'   c                     t        |j                        dk7  rt        d      t        j                  j                  || j                  | j                  | j                        S )Nrw   z&Input shape must be `(N, C, T, H, W)`!)	r"   r   r?   r   r   conv_transpose3dr   r<   r=   r1  s     r   r   zConvTranspose3d.forward  sO     u{{q EFF}}--4&&

DOO
 	
r'   c                 2    t         j                  | |||      S r+   r3  r4  s       r   r   zConvTranspose3d.from_reference  r5  r'   r6  )r   r   r   r   r   r   r   r   r   r   r9   r   rC   r   r   rI   rW   r^   r5   r   r   r   r   r   s   @r   r   r   _  r7  r'   r   )+r   typingr   r   rC   torch.ao.nn.intrinsicaor   	intrinsicr   torch.ao.nn.intrinsic.qatqatr   torch.nntorch.nn.functional
functionalr   
torch._opsr   torch.nn.common_typesr   torch.nn.modules.utilsr   r   r	   torch.nn.utilsr
   utilsr   r   __all__rB   rE   rl   r&   r)   r   r   r   r  r   r   r   r   r'   r   <module>ra     s    % %  # # * *     + : : / < y) ,T#Y ,49 ,w% wt	~
W ~
B~
W ~
B
W 
J|w |~z
& z
zx
& x
vz
& z
r'   