
    %Vji)5                         d dl mZ d dl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Z d dlmZ ddlmZmZmZ dd	gZ G d
 dej        j                  Z G d d	e          ZdS )    )Iterable)OptionalN)fuse_linear_bn_weights)type_before_parametrizations   )_hide_packed_params_repr_quantize_weightWeightedQuantizedModuleLinearPackedParamsLinearc                        e Zd ZdZej        f fd	Zej        j        dej	        de
ej	                 ddfd            Zej        j        d             Zd	 Z fd
Z fdZd Z xZS )r      c                 d   t                                                       || _        | j        t          j        k    r%t          j        ddgddt          j                  }n7| j        t          j        k    r"t          j        ddgt          j                  }| 	                    |d            d S )Nr         ?r   scale
zero_pointdtyper   )
super__init__r   torchqint8_empty_affine_quantizedfloat16zerosfloatset_weight_bias)selfr   wq	__class__s      ^/root/voice-cloning/.venv/lib/python3.11/site-packages/torch/ao/nn/quantized/modules/linear.pyr   zLinearPackedParams.__init__   s    
:$$.Acau{  BB Z5=((aV5;777BR&&&&&    weightbiasreturnNc                 $   | j         t          j        k    r,t          j        j                            ||          | _        d S | j         t          j        k    r,t          j        j                            ||          | _        d S t          d          Nz.Unsupported dtype on dynamic quantized linear!)
r   r   r   ops	quantizedlinear_prepack_packed_paramsr   linear_prepack_fp16RuntimeError)r   r$   r%   s      r"   r   z"LinearPackedParams.set_weight_bias!   sx     :$$"')"5"D"DVT"R"RDZ5=(("')"5"I"I&RV"W"WDOPPPr#   c                    | j         t          j        k    r)t          j        j                            | j                  S | j         t          j        k    r)t          j        j                            | j                  S t          d          r(   )
r   r   r   r)   r*   linear_unpackr,   r   linear_unpack_fp16r.   r   s    r"   _weight_biaszLinearPackedParams._weight_bias,   sg    :$$9&44T5HIIIZ5=((9&99$:MNNNOPPPr#   c                     |S N r   xs     r"   forwardzLinearPackedParams.forward5   s    r#   c                     t                                          |||           | j        ||dz   <   |                                 ||dz   <   d S )Nr   r,   )r   _save_to_state_dictr   r3   r   destinationprefix	keep_varsr!   s       r"   r;   z&LinearPackedParams._save_to_state_dictH   sR    ##KCCC(,
FW$%151B1B1D1DF--...r#   c           	      J   |                     dd           }||dk     rt          j        | _        n(||dz            | _        |                    |dz              ||dk     rX|                     ||dz            ||dz                       |                    |dz              |                    |dz              |dk    r<||dz            \  }	}
|                    |dz              |                     |	|
           t                                          |||d|||           d S )	Nversion   r   r   r$   r%   r,   F)getr   r   r   popr   r   _load_from_state_dictr   
state_dictr>   local_metadatastrictmissing_keysunexpected_keys
error_msgsrA   r$   r%   r!   s              r"   rE   z(LinearPackedParams._load_from_state_dictM   sL    !$$Y55?gkkDJJ#FW$45DJNN6G+,,,?gkk  6H,-z&6//J   NN6H,---NN6F?+++a<<%f/?&?@LFDNN6$44555  ...%%	
 	
 	
 	
 	
r#   c                 N    |                                                                  S r5   )r3   __repr__r2   s    r"   rN   zLinearPackedParams.__repr__t   s       ""++---r#   )__name__
__module____qualname___versionr   r   r   jitexportTensorr   r   r3   r9   r;   rE   rN   __classcell__r!   s   @r"   r   r      s       H"[ 	' 	' 	' 	' 	' 	' YQlQ*25<*@Q	Q Q Q Q YQ Q Q  &E E E E E
%
 %
 %
 %
 %
N. . . . . . .r#   c                   $    e Zd ZdZdZej        ej        j        j	        fZ
dej        f fd	Zd Zd Zd Zdej        d	ej        fd
Z fdZ fdZd Zd Zd Zdej        deej                 d	dfdZedd            Zed             Z xZS )r   a  
    A quantized linear module with quantized tensor as inputs and outputs.
    We adopt the same interface as `torch.nn.Linear`, please see
    https://pytorch.org/docs/stable/nn.html#torch.nn.Linear for documentation.

    Similar to :class:`~torch.nn.Linear`, attributes will be randomly
    initialized at module creation time and will be overwritten later

    Attributes:
        weight (Tensor): the non-learnable quantized weights of the module of
                         shape :math:`(\text{out\_features}, \text{in\_features})`.
        bias (Tensor): the non-learnable bias of the module of shape :math:`(\text{out\_features})`.
                If :attr:`bias` is ``True``, the values are initialized to zero.
        scale: `scale` parameter of output Quantized Tensor, type: double
        zero_point: `zero_point` parameter for output Quantized Tensor, type: long

    Examples::

        >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_QENGINE)
        >>> m = nn.quantized.Linear(20, 30)
        >>> input = torch.randn(128, 20)
        >>> # xdoctest: +SKIP
        >>> input = torch.quantize_per_tensor(input, 1.0, 0, torch.quint8)
        >>> output = m(input)
        >>> print(output.size())
        torch.Size([128, 30])
    r   Tc                    t                                                       || _        || _        d }|r t	          j        |t          j                  }|t          j        k    r%t	          j        ||gddt          j                  }nB|t          j	        k    r#t	          j        ||gt          j                  }nt          d          t          |          | _        | j                            ||           d| _        d| _        d S )Nr   r   r   r   z1Unsupported dtype specified for quantized Linear!r   )r   r   in_featuresout_featuresr   r   r   r   r   r   r.   r   r,   r   r   r   )r   rZ   r[   bias_r   r%   qweightr!   s          r"   r   zLinear.__init__   s    
 '( 	@;|5;???DEK3{+1%+  GG em##k<"=U[QQQGGRSSS077++GT:::
r#   c                     dS )NQuantizedLinearr6   r2   s    r"   	_get_namezLinear._get_name   s      r#   c                     d| j          d| j         d| j         d| j         d|                                                                  
S )Nzin_features=z, out_features=z, scale=z, zero_point=z
, qscheme=)rZ   r[   r   r   r$   qschemer2   s    r"   
extra_reprzLinear.extra_repr   su    O4+ O OD<M O OW[Wa O O/O O59[[]]5J5J5L5LO O	
r#   c                 ,    t          | t                    S r5   )r   r   r2   s    r"   rN   zLinear.__repr__   s    '.@AAAr#   r8   r&   c                 x    t           j        j                            || j        j        | j        | j                  S r5   )r   r)   r*   linearr,   r   r   r7   s     r"   r9   zLinear.forward   s2    y"))t"14:t
 
 	
r#   c                     t                                          |||           t          j        | j                  ||dz   <   t          j        | j                  ||dz   <   d S )Nr   r   )r   r;   r   tensorr   r   r<   s       r"   r;   zLinear._save_to_state_dict   s[    ##KCCC(-TZ(@(@FW$%-2\$/-J-JF\)***r#   c           	         t          ||dz                      | _        |                    |dz              t          ||dz                      | _        |                    |dz              |                    dd           }||dk    rO|                    |dz             }	|                    |dz             }
|                    |dz   |	|dz   |
i           t                                          |||d	|||           d S )
Nr   r   rA   r   r$   r%   z_packed_params.weightz_packed_params.biasF)	r   r   rD   intr   rC   updater   rE   rF   s              r"   rE   zLinear._load_from_state_dict   s"    :fw&6788
v'(((j,)>?@@v,--- $$Y55?gll^^FX$566F>>&6/22D44f22D   	%%	
 	
 	
 	
 	
r#   c                 4    | j                                         S r5   )r,   r3   r2   s    r"   r3   zLinear._weight_bias  s    "//111r#   c                 6    |                                  d         S )Nr   r3   r2   s    r"   r$   zLinear.weight        ""1%%r#   c                 6    |                                  d         S )Nr   rn   r2   s    r"   r%   zLinear.bias  ro   r#   wbNc                 <    | j                             ||           d S r5   )r,   r   )r   rq   rr   s      r"   r   zLinear.set_weight_bias  s!    ++Aq11111r#   Fc           	         t          |d          rt          |          t          j        k    r^t	          |j        |j        |j        j        |j        j	        |j        j
        |j        j        |j        j                  \  |_        |_        |j        }|j        }nt          | j        t                    s| j        g| _        d                    d | j        D                       }d| j         d| dt%          |           }t          |          | j        v sJ |                                            t          |d          s
J d            |j        }t          |          t(          j        k    r|d	         }t          |d          s|j                                        n|j        }|s ||j                   |j        }|                                \  }}	|t2          j        k    s
J d
            t7          |j                                        |          }
 | |j        |j        |          }|                    |
|j                   t9          |          |_         tC          |	          |_"        |S )a}  Create a quantized module from an observed float module

        Args:
            mod (Module): a float module, either produced by torch.ao.quantization
                          utilities or provided by the user
            use_precomputed_fake_quant (bool): if True, the module will reuse min/max
                          values from the precomputed fake quant module.
        weight_fake_quantz, c                     g | ]	}|j         
S r6   )rO   ).0	float_mods     r"   
<listcomp>z%Linear.from_float.<locals>.<listcomp>;  s    GGG	#GGGr#   znnq.z.from_float only works for z, but got: qconfigz,Input float module must have qconfig definedr   z+Weight observer must have dtype torch.qint8r   )#hasattrr   nniqat
LinearBn1dr   r$   r%   bnrunning_meanrunning_varepsru   activation_post_process
isinstance_FLOAT_MODULEr   joinrO   typeformatnni
LinearReLUrz   r   calculate_qparamsr   r   r	   r   rZ   r[   r   r   rj   r   )clsmoduse_precomputed_fake_quantweight_post_processr   supported_modules	error_msgr   	act_scaleact_zpr]   qlinears               r"   
from_floatzLinear.from_float  s    3+,, $	+C00F4EEE'=JHF'F&FJFMFK( ($
CH #&"7&)&A##
 c/:: 8%(%6$7! $		GGS5FGGG! ! rs|qqHYqqfjknfofoqqI/448IIII  "" JII 3	**  > * '*&A#+C00CNBB!f s$788+""$$$*   * 	,  
+++#)3EEGG	6###%R###"3:#3#3#5#57JKK#cos'7uEEE222i(( [[r#   c                      | |j         |j                  }|                                }|                    ||j                   t          |          |_        t          |          |_        |S )a  Create a (fbgemm/qnnpack) quantized module from a reference quantized module

        Args:
            ref_qlinear (Module): a reference quantized linear 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
        )	rZ   r[   get_quantized_weightr   r%   r   r   rj   r   )r   ref_qlinearoutput_scaleoutput_zero_pointr   r]   s         r"   from_referencezLinear.from_reference[  sj     #k-{/GHH2244)9:::l++ !233r#   )F)rO   rP   rQ   __doc__rR   nnr   modulesrf   NonDynamicallyQuantizableLinearr   r   r   r   r`   rc   rN   rU   r9   r;   rE   r3   r$   r%   r   r   classmethodr   r   rV   rW   s   @r"   r   r   x   s        8 HY
 1 QRM8<EK      4! ! !
 
 
B B B
 
%, 
 
 
 
DK K K K K%
 %
 %
 %
 %
R2 2 2& & && & &2 2(5<2H 2T 2 2 2 2 ; ; ; [;z   [    r#   )collections.abcr   typingr   r   torch.ao.nn.intrinsicaor   	intrinsicr   torch.ao.nn.intrinsic.qatqatr|   torch.nntorch.nn.utils.fusionr   torch.nn.utils.parametrizer   utilsr   r	   r
   __all__Moduler   r   r6   r#   r"   <module>r      s[   % $ $ $ $ $        # # # # # # # # # # # # * * * * * * * * * * * * * * *       8 8 8 8 8 8 C C C C C C V V V V V V V V V V  
*b. b. b. b. b. b. b. b.Js s s s s$ s s s s sr#   