
    piЩ                        d Z ddlmZ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Literal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     d/opt/services/ai/voice_agent/venv/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r8   r9   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_pointr9   r9   g      ?)superr:   
ValueErrorr0   r1   r2   r3   r   r4   
transposedoutput_paddingr5   _SUPPORTED_PADDINGr7   torch_empty_affine_quantizedlistqint8itemsr   floatset_weight_biasr=   r>   )r/   r0   r1   r2   r3   r   r4   rA   rB   r5   r6   r7   r8   r9   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/   rO   rP   s      r    rJ   z_ConvNd.set_weight_biasx       !!r(   c                     t         r,   r-   r/   s    r    r6   z_ConvNd.bias{   rT   r(   c                     t         r,   r-   rV   s    r    _weight_biasz_ConvNd._weight_bias~   rT   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#   r4   rB   r5   r6   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weightr6   r=   r>   )r?   _save_to_state_dictrX   rD   tensorr=   r>   )r/   destinationprefix	keep_varswbrQ   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,   )rX   r0   r1   r2   r3   r   r4   rA   rB   r5   r7   r=   r>   trainingr/   re   rf   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_   r6   r=   r>   F)rJ   poprI   r=   intr>   r?   _load_from_state_dict)	r/   
state_dictrc   local_metadatastrictmissing_keysunexpected_keys
error_msgsrQ   s	           r    rn   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"                     	   
               )r0   r1   r2   r3   r   r4   rA   rB   r5   r7   rJ   r=   r>   rh   )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__rD   nnModuler:   rj   r   )r/   memonew_instancer   s       r    __deepcopy__z_ConvNd.__deepcopy__   sR    Dz))$t*5  .!!#!!%(r(   c                 $    | j                  i       S r,   )r   rV   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_   r9   rD   rG   r   rI   r0   r1   r2   r3   r   r4   r5   r6   r7   rJ   calculate_qparamsr=   rm   r>   )clsmodactivation_post_processweight_post_processrO   qconv	act_scaleact_zps           r    	get_qconvz_ConvNd.get_qconv   s    &"%++"4"4"6CJJ'"((EKK7 	
8	
7 #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_   r6   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 3 9: >: #&"7"7&)&A&A#9 1 11 ,,/0 ##,,- 	
 d3i.!1 3	* ?*
 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<   )r0   r1   r2   r3   r   r4   r5   r6   r7   r_   r8   r9   get_quantized_weightrJ   rI   r=   rm   r>   )r   	ref_qconvoutput_scaleoutput_zero_pointr   rO   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__r:   rR   rJ   r6   rX   r]   r`   rD   jitexportrj   rn   r   r   r   classmethodr   staticmethodr   r   __classcell__rQ   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d   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   r0   r1   r2   r3   r   r4   r5   r6   r7   )r   r   	replicatecircularc                     |
|d}t        |      }t        |      }t        |t              r|n
t        |      }t        |      }t        |   ||||||dt        d      |||	fi | y Nr<   Fr   )r	   
isinstancer   r?   rR   r/   r0   r1   r2   r3   r   r4   r5   r6   r7   r8   r9   rK   rQ   s                r    r:   zConv1d.__init__  s~     %+U;k*'5'77;K8$ 	AJ	
 	
r(   c                      y)NQuantizedConv1dr   rV   s    r    	_get_namezConv1d._get_name       r(   re   rf   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   )r7   rD   r   	quantizedconv1d_prepackr3   r   r4   r5   _packed_paramsr   ri   s      r    rJ   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,   )rD   r   r   conv1d_unpackr   ri   s      r    rX   zConv1d._weight_bias  /    yy""001D1DE1!tr(   c                 (    | j                         d   S Nr   rX   rV   s    r    r_   zConv1d.weight        "1%%r(   c                 (    | j                         d   S Nr   r   rV   s    r    r6   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 )Nrv    Input shape must be `(N, C, L)`!r   r   mode)r#   shaper@   r7   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   rm   r   boolr   r:   r   rD   TensorrJ   rX   r_   r6   r   r   r   r   r   s   @r    r   r   a  st    D 02yyM8DO,8BH//HXd299o%>?QAD8HT"))_$=>O@D(8DO#<=DEIxbii(ABI MT#
#
 #
 	#

 #
 #
 #
 #
 #
 IJ#
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?   rR   r   s                r    r:   zConv2d.__init__  sq     %+U;K(v.? 	!H	
 	
r(   c                      y)NQuantizedConv2dr   rV   s    r    r   zConv2d._get_name3  r   r(   re   rf   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   )r7   rD   r   r   conv2d_prepackr3   r   r4   r5   r   r   ri   s      r    rJ   zConv2d.set_weight_bias6  r   r(   c                 6    | j                   j                         S r,   r   unpackrV   s    r    rX   zConv2d._weight_bias@      ""))++r(   c                 (    | j                         d   S r   r   rV   s    r    r_   zConv2d.weightC  r   r(   c                 (    | j                         d   S r   r   rV   s    r    r6   zConv2d.biasF  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   #Input shape must be `(N, C, H, W)`!r   r   )r#   r   r@   r7   r'   r   r   r   r   r   conv2dr   r=   r>   r   s      r    r   zConv2d.forwardI  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_floatW  r   r(   r   r   )"r   r   r   r   r   r   r   r   r   r   r   ConvBn2dr   r   r   r   
ConvReLU2dr   	ConvAdd2dr   ConvAddReLU2dr   r:   r   rD   r   rJ   rX   r_   r6   r   r   r   r   r   s   @r    r   r     s   $L 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?   rR   r   s                r    r:   zConv3d.__init__  s     y(V*VV($*U;k*'"8$ 	AJ	
 	
r(   c                      y)NQuantizedConv3dr   rV   s    r    r   zConv3d._get_name  r   r(   re   rf   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   )r7   rD   r   r   conv3d_prepackr3   r   r4   r5   r   r
   ri   s      r    rJ   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   rV   s    r    rX   zConv3d._weight_bias  r   r(   c                 (    | j                         d   S r   r   rV   s    r    r_   zConv3d.weight  r   r(   c                 (    | j                         d   S r   r   rV   s    r    r6   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 )Nrx   z&Input shape must be `(N, C, D, H, W)`!r   r   )r#   r   r@   r7   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   r:   r   rD   r   rJ   rX   r_   r6   r   r   r   r   r   s   @r    r   r   d  s   $L 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@   rQ   r   r?   rR   )r/   r0   r1   r2   r3   r   r4   rA   rB   r5   r6   r7   r8   r9   rK   rQ   s                  r    r:   z_ConvTransposeNd.__init__  st      7"=dnn>U>U=VW  %+U; 		
 	
r(   r2   r4   r   r   c                     t         j                  j                  t        t           g       }t        t        |            D ]'  }||   ||   dz
  z  ||   z
  }|j                  |       ) |S r   )rD   r   annotaterF   rm   r$   r#   append)r/   r2   r4   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_   r9   rD   rG   r   rI   r0   r1   r2   r3   r   rB   r5   r6   r4   r7   rJ   r   r   r=   rm   r>   )	r   r   r   msgr   rO   r   r   r   s	            r    r   z_ConvTransposeNd.from_float  s    ll+, (() 	 CyC---2s2-sI&W(WW&!kk002CJJ'"((EKK7 	
8	
7 #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<   )r0   r1   r2   r3   r   rB   r5   r6   r4   r7   r_   r8   r9   r   rJ   rI   r=   rm   r>   )r   
ref_qconvtr   r   r   rO   s         r    r   z_ConvTransposeNd.from_referenceK  s     ""##""%%OO4'##$$++##))
 113gz7L)01r(   )NNr   )r   r   r   r   r   r   modulesconvr*   r   r:   rF   rm   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?   r:   r/   r0   r1   r2   r3   r   rB   r5   r6   r4   r7   r8   r9   rK   rQ   s                 r    r:   zConvTranspose1d.__init__  x     %+U;k*'"8$ 0	
 	
r(   c                      y)NQuantizedConvTranspose1dr   rV   s    r    r   zConvTranspose1d._get_name      )r(   re   rf   r   c           	          t         j                  j                  j                  ||| j                  | j
                  | j                  | j                  | j                        | _	        y r,   )
rD   r   r   conv_transpose1d_prepackr3   r   rB   r4   r5   r   ri   s      r    rJ   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,   )rD   r   r   conv_transpose1d_unpackr   ri   s      r    rX   zConvTranspose1d._weight_bias  s/    yy""::4;N;NO1!tr(   c                 ,    | j                         \  }}|S r,   r   r/   re   r   s      r    r_   zConvTranspose1d.weight      ""$Ar(   c                 ,    | j                         \  }}|S r,   r   r/   r   rf   s      r    r6   zConvTranspose1d.bias  r/  r(   c                     t        |j                        dk7  rt        d      t        j                  j
                  j                  || j                  | j                  | j                        S )Nrv   r   )
r#   r   r@   rD   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   r:   r   rD   r   r   rJ   rX   r_   r6   r   r   r   r   r   s   @r    r   r   i      )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?   r:   r#  s                 r    r:   zConvTranspose2d.__init__  sv     %+U;K(v.?~.	
 	
r(   c                      y)NQuantizedConvTranspose2dr   rV   s    r    r   zConvTranspose2d._get_name7  r'  r(   re   rf   r   c           	          t         j                  j                  j                  ||| j                  | j
                  | j                  | j                  | j                        | _	        y r,   )
rD   r   r   conv_transpose2d_prepackr3   r   rB   r4   r5   r   ri   s      r    rJ   zConvTranspose2d.set_weight_bias:  r*  r(   c                 v    t         j                  j                  j                  | j                        \  }}||fS r,   )rD   r   r   conv2d_unpackr   ri   s      r    rX   zConvTranspose2d._weight_biasE  r   r(   c                 ,    | j                         \  }}|S r,   r   r.  s      r    r_   zConvTranspose2d.weightI  r/  r(   c                 ,    | j                         \  }}|S r,   r   r1  s      r    r6   zConvTranspose2d.biasM  r/  r(   c                     t        |j                        dk7  rt        d      t        j                  j                  || j                  | j                  | j                        S )Nrw   r   )	r#   r   r@   r   r   conv_transpose2dr   r=   r>   r4  s     r    r   zConvTranspose2d.forwardQ  sO     u{{q BCC}}--4&&

DOO
 	
r(   c                 2    t         j                  | |||      S r,   r6  r7  s       r    r   zConvTranspose2d.from_referenceZ  r8  r(   r9  )r   r   r   r   r   r   r   r   r   r   r:   r   rD   r   r   rJ   rX   r_   r6   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?   r:   r#  s                 r    r:   zConvTranspose3d.__init__  r$  r(   c                      y)NQuantizedConvTranspose3dr   rV   s    r    r   zConvTranspose3d._get_name  r'  r(   re   rf   r   c           	          t         j                  j                  j                  ||| j                  | j
                  | j                  | j                  | j                        | _	        y r,   )
rD   r   r   conv_transpose3d_prepackr3   r   rB   r4   r5   r   ri   s      r    rJ   zConvTranspose3d.set_weight_bias  r*  r(   c                 v    t         j                  j                  j                  | j                        \  }}||fS r,   )rD   r   r   conv3d_unpackr   ri   s      r    rX   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   r1  s      r    r6   zConvTranspose3d.bias  r/  r(   c                     t        |j                        dk7  rt        d      t        j                  j                  || j                  | j                  | j                        S )Nrx   z&Input shape must be `(N, C, T, H, W)`!)	r#   r   r@   r   r   conv_transpose3dr   r=   r>   r4  s     r    r   zConvTranspose3d.forward  sO     u{{q EFF}}--4&&

DOO
 	
r(   c                 2    t         j                  | |||      S r,   r6  r7  s       r    r   zConvTranspose3d.from_reference  r8  r(   r9  )r   r   r   r   r   r   r   r   r   r   r:   r   rD   r   r   rJ   rX   r_   r6   r   r   r   r   r   s   @r    r   r   a  r:  r(   r   ),r   typingr   r   r   rD   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__rC   rF   rm   r'   r*   r   r   r   r  r   r   r   r   r(   r    <module>rd     s    % . .  # # * *     + : : / < y) ,T#Y ,49 ,w% wt	~
W ~
B
W 
D@
W @
L|w |~z
& z
zx
& x
vz
& z
r(   