
    sg@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y)    )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                 j   t         |           || _        | j                  t        j                  k(  r*t        j
                  ddgddt        j                        }nD| j                  t        j                  k(  r't        j                  ddgt        j                        }| j                  d        y )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      W/var/www/html/venv/lib/python3.12/site-packages/torch/ao/nn/quantized/modules/linear.pyr   zLinearPackedParams.__init__   s    
::$..Acau{{B ZZ5==(aV5;;7BR&    weightbiasreturnNc                 L   | j                   t        j                  k(  r0t        j                  j                  j                  ||      | _        y | j                   t        j                  k(  r0t        j                  j                  j                  ||      | _        y 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!   so     ::$"'))"5"5"D"DVT"RDZZ5==("'))"5"5"I"I&RV"WDOPPr#   c                 X   | j                   t        j                  k(  r3t        j                  j                  j                  | j                        S | j                   t        j                  k(  r3t        j                  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,   sp    ::$99&&44T5H5HIIZZ5==(99&&99$:M:MNNOPPr#   c                     |S N r   xs     r"   forwardzLinearPackedParams.forward5   s    r#   c                 x    t         |   |||       | j                  ||dz   <   | j                         ||dz   <   y )Nr   r,   )r   _save_to_state_dictr   r3   r   destinationprefix	keep_varsr!   s       r"   r;   z&LinearPackedParams._save_to_state_dictH   s@    #KC(,

FW$%151B1B1DF--.r#   c           	         |j                  dd       }||dk  rt        j                  | _        n!||dz      | _        |j	                  |dz          ||dk  rF| j                  ||dz      ||dz             |j	                  |dz          |j	                  |dz          |dk(  r1||dz      \  }	}
|j	                  |dz          | j                  |	|
       t        |   |||d|||       y )	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   s    !$$Y5?gkDJ#FW$45DJNN6G+,?gk  6H,-z&6//J NN6H,-NN6F?+a<%f/?&?@LFDNN6$445  .%	
r#   c                 >    | j                         j                         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"[[ 	' YYQllQ*25<<*@Q	Q Q YYQ Q&E
%
N.r#   c                   l    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                        }nF|t        j                  k(  r(t	        j
                  ||gt        j                        }nt        d      t        |      | _        | j                  j                  ||       d| _        d| _        y )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EKK33{+1%++G emm#kk<"=U[[QGRSS07++GT:
r#   c                      y)NQuantizedLinearr6   r2   s    r"   	_get_namezLinear._get_name   s     r#   c                     d| j                    d| j                   d| j                   d| j                   d| j	                         j                          
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   s\    4++,OD<M<M;NhW[WaWaVb c//**T[[]5J5J5L4MO	
r#   c                 "    t        | t              S r5   )r   r   r2   s    r"   rN   zLinear.__repr__   s    '.@AAr#   r8   r&   c                     t         j                  j                  j                  || j                  j                  | j
                  | j                        S r5   )r   r)   r*   linearr,   r   r   r7   s     r"   r9   zLinear.forward   s<    yy""))t""114::t
 	
r#   c                     t         |   |||       t        j                  | j                        ||dz   <   t        j                  | j
                        ||dz   <   y )Nr   r   )r   r;   r   tensorr   r   r<   s       r"   r;   zLinear._save_to_state_dict   sL    #KC(-TZZ(@FW$%-2\\$//-JF\)*r#   c           	         t        ||dz            | _        |j                  |dz          t        ||dz            | _        |j                  |dz          |j                  dd       }||dk(  rC|j                  |dz         }	|j                  |dz         }
|j                  |dz   |	|dz   |
i       t        | !  |||d	|||       y )
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
v'(j,)>?@v,- $$Y5?gl^^FX$56F>>&6/2D44f22D 	%	
r#   c                 6    | j                   j                         S r5   )r,   r3   r2   s    r"   r3   zLinear._weight_bias  s    ""//11r#   c                 (    | j                         d   S )Nr   r3   r2   s    r"   r$   zLinear.weight        "1%%r#   c                 (    | j                         d   S )Nr   rn   r2   s    r"   r%   zLinear.bias  ro   r#   wbNc                 <    | j                   j                  ||       y r5   )r,   r   )r   rq   rr   s      r"   r   zLinear.set_weight_bias  s    ++Aq1r#   c           	         t        |d      rt        |      t        j                  k(  rt	        |j
                  |j                  |j                  j                  |j                  j                  |j                  j                  |j                  j
                  |j                  j                        \  |_        |_        |j                  }|j                  }nt        | j                  t              s| j                  g| _        dj!                  | j                  D cg c]  }|j"                   c}      }d| j"                   d| dt%        |       }t        |      | j                  v sJ |j'                                t        |d      sJ d       |j                  }t        |      t(        j*                  k(  r|d   }t        |d      s|j,                  j                         n|j                  }|s ||j
                         |j.                  }|j1                         \  }	}
|t2        j4                  k(  sJ d	       t7        |j
                  j9                         |      } | |j:                  |j<                  |
      }|j?                  ||j                         t9        |	      |_         tC        |
      |_"        |S c c}w )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, 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
LinearReLUrv   r   calculate_qparamsr   r   r	   r   rZ   r[   r   r   rj   r   )clsmoduse_precomputed_fake_quantweight_post_processr~   	float_modsupported_modules	error_msgr   	act_scaleact_zpr]   qlinears                r"   
from_floatzLinear.from_float  sV    3+,+C0F4E4EE'=JJHHFF''FF&&FFJJFFMMFFKK($
CH #&"7"7&)&A&A#
 c//:%(%6%6$7! $		sO`O`*a)9+=+=*a bs||n,GHYGZZefjknfoepqI/48I8II]9K[K[K]]IY >=>  '*&A&A#+C0CNNB!f s$78 ""$**   *  

+#))3EEG	6#R%RR#"3::#3#3#57JKcoos'7'7uE2i( [7 +bs   Kc                      | |j                   |j                        }|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_referenceV  s_     k--{/G/GH224)9)9:l+ !2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    6 HYY

 1 1 Q QRM8<EKK 4!
B
 
%,, 
DK%
R2&&2 2(5<<2H 2T 2 7 7r  r#   )collections.abcr   typingr   r   torch.ao.nn.intrinsicaor   	intrinsicr   torch.ao.nn.intrinsic.qatqatrx   torch.nntorch.nn.utils.fusionr   torch.nn.utils.parametrizer   utilsr   r	   r
   __all__Moduler   r   r6   r#   r"   <module>r      s]    %   # # * *  8 C V V  
*b. b.Jn$ nr#   