
    sh&5                         S SK Jr  S SKJr  S SKrS SKJs  Js  Jr	  S SK
Js  Js  Js  Jr  S SKJr  S SKJr  S SKJr  SSKJrJrJr  SS	/r " S
 S\R                  R.                  5      r " S S	\5      rg)    )Iterable)OptionalN)fuse_linear_bn_weights)type_before_parametrizations   )_hide_packed_params_repr_quantize_weightWeightedQuantizedModuleLinearPackedParamsLinearc                     ^  \ rS rSrSr\R                  4U 4S jjr\R                  R                  S\R                  S\\R                     SS4S j5       r\R                  R                  S	 5       rS
 rU 4S jrU 4S jrS rSrU =r$ )r         c                 d  > [         TU ]  5         Xl        U R                  [        R                  :X  a(  [        R
                  " SS/SS[        R                  S9nOCU R                  [        R                  :X  a%  [        R                  " SS/[        R                  S9nU R                  WS 5        g )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      x/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/torch/ao/nn/quantized/modules/linear.pyr   LinearPackedParams.__init__   s~    
::$..Acau{{B ZZ5==(aV5;;7BR&    weightbiasreturnNc                 L   U R                   [        R                  :X  a/  [        R                  R                  R                  X5      U l        g U R                   [        R                  :X  a/  [        R                  R                  R                  X5      U l        g [        S5      e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   "LinearPackedParams.set_weight_bias!   sj     ::$"'))"5"5"D"DV"RDZZ5==("'))"5"5"I"I&"WDOPPr%   c                 \   U R                   [        R                  :X  a3  [        R                  R                  R                  U R                  5      $ U R                   [        R                  :X  a3  [        R                  R                  R                  U R                  5      $ [        S5      er*   )
r   r   r   r+   r,   linear_unpackr.   r   linear_unpack_fp16r0   r    s    r#   _weight_biasLinearPackedParams._weight_bias,   sp    ::$99&&44T5H5HIIZZ5==(99&&99$:M:MNNOPPr%   c                     U$ N r    xs     r#   forwardLinearPackedParams.forward5   s    r%   c                 r   > [         TU ]  XU5        U R                  XS-   '   U R                  5       XS-   '   g )Nr   r.   )r   _save_to_state_dictr   r6   r    destinationprefix	keep_varsr"   s       r#   r@   &LinearPackedParams._save_to_state_dictH   s:    #KC(,

W$%151B1B1D--.r%   c           	        > UR                  SS 5      nUb  US:  a  [        R                  U l        O XS-      U l        UR	                  US-   5        Ub  US:  aD  U R                  XS-      XS-      5        UR	                  US-   5        UR	                  US-   5        US:X  a.  XS-      u  pUR	                  US-   5        U R                  X5        [        TU ]  UUUSUUU5        g )	Nversion   r   r   r&   r'   r.   F)getr   r   r   popr   r   _load_from_state_dictr    
state_dictrC   local_metadatastrictmissing_keysunexpected_keys
error_msgsrG   r&   r'   r"   s              r#   rK   (LinearPackedParams._load_from_state_dictM   s     !$$Y5?gkDJ#W$45DJNN6G+,?gk  H,-z6//J NN6H,-NN6F?+a<%/?&?@LFNN6$445  .%	
r%   c                 >    U R                  5       R                  5       $ r9   )r6   __repr__r5   s    r#   rU   LinearPackedParams.__repr__t   s      "++--r%   )r.   r   )__name__
__module____qualname____firstlineno___versionr   r   r   jitexportTensorr   r   r6   r=   r@   rK   rU   __static_attributes____classcell__r"   s   @r#   r   r      s    H"[[ 	' YYQllQ*25<<*@Q	Q Q YYQ Q&E
%
N. .r%   c                     ^  \ rS rSrSrSr\R                  \R                  R                  R                  4rS\R                  4U 4S jjrS rS rS rS	\R$                  S
\R$                  4S jrU 4S jrU 4S jrS rS rS rS\R$                  S\\R$                     S
S4S jr\SS j5       r\S 5       rSrU =r$ )r   x   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U ]  5         Xl        X l        S nU(       a#  [        R
                  " U[        R                  S9nU[        R                  :X  a'  [        R                  " X!/SS[        R                  S9nODU[        R                  :X  a%  [        R
                  " X!/[        R                  S9nO[        S5      e[        U5      U l        U R                  R                  Xe5        SU l        SU l        g )Nr   r   r   r   z1Unsupported dtype specified for quantized Linear!r   )r   r   in_featuresout_featuresr   r   r   r   r   r   r0   r   r.   r   r   r   )r    re   rf   bias_r   r'   qweightr"   s          r#   r   Linear.__init__   s    
 '(;;|5;;?DEKK33+1%++G emm#kk<"=U[[QGRSS07++G:
r%   c                     g)NQuantizedLinearr:   r5   s    r#   	_get_nameLinear._get_name   s     r%   c                     SU R                    SU R                   SU R                   SU R                   SU R	                  5       R                  5        3
$ )Nzin_features=z, out_features=z, scale=z, zero_point=z
, qscheme=)re   rf   r   r   r&   qschemer5   s    r#   
extra_reprLinear.extra_repr   s\    4++,OD<M<M;NhW[WaWaVb c//**T[[]5J5J5L4MO	
r%   c                 "    [        U [        5      $ r9   )r   r   r5   s    r#   rU   Linear.__repr__   s    '.@AAr%   r<   r(   c                     [         R                  R                  R                  XR                  R                  U R
                  U R                  5      $ r9   )r   r+   r,   linearr.   r   r   r;   s     r#   r=   Linear.forward   s:    yy""))""114::t
 	
r%   c                    > [         TU ]  XU5        [        R                  " U R                  5      XS-   '   [        R                  " U R
                  5      XS-   '   g )Nr   r   )r   r@   r   tensorr   r   rA   s       r#   r@   Linear._save_to_state_dict   sF    #KC(-TZZ(@W$%-2\\$//-J\)*r%   c           	        > [        XS-      5      U l        UR                  US-   5        [        XS-      5      U l        UR                  US-   5        UR                  SS 5      nUb  US:X  aC  UR                  US-   5      n	UR                  US-   5      n
UR                  US-   U	US-   U
05        [        TU ]!  UUUS	UUU5        g )
Nr   r   rG   r   r&   r'   z_packed_params.weightz_packed_params.biasF)	r   r   rJ   intr   rI   updater   rK   rL   s              r#   rK   Linear._load_from_state_dict   s     :w&678
v'(j,)>?@v,- $$Y5?gl^^FX$56F>>&6/2D44f22D 	%	
r%   c                 6    U R                   R                  5       $ r9   )r.   r6   r5   s    r#   r6   Linear._weight_bias  s    ""//11r%   c                 (    U R                  5       S   $ )Nr   r6   r5   s    r#   r&   Linear.weight        "1%%r%   c                 (    U R                  5       S   $ )Nr   r   r5   s    r#   r'   Linear.bias  r   r%   wbNc                 :    U R                   R                  X5        g r9   )r.   r   )r    r   r   s      r#   r   Linear.set_weight_bias  s    ++A1r%   c           	         [        US5      (       a  [        U5      [        R                  :X  a  [	        UR
                  UR                  UR                  R                  UR                  R                  UR                  R                  UR                  R
                  UR                  R                  5      u  Ul        Ul        UR                  nUR                  nGO/[        U R                  [        5      (       d  U R                  /U l        SR!                  U R                   Vs/ s H  oUR"                  PM     sn5      nSU R"                   SU S[%        U5       3n[        U5      U R                  ;   d   UR'                  5       5       e[        US5      (       d   S5       eUR                  n[        U5      [(        R*                  :X  a  US   n[        US5      (       d  UR,                  R                  5       OUR                  nU(       d  U" UR
                  5        UR.                  nUR1                  5       u  pU[2        R4                  :X  d   S	5       e[7        UR
                  R9                  5       U5      nU " UR:                  UR<                  US
9nUR?                  XR                  5        [9        U	5      Ul         [C        U
5      Ul"        U$ s  snf )aM  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epsr   activation_post_process
isinstance_FLOAT_MODULEr   joinrW   typeformatnni
LinearReLUr   r   calculate_qparamsr   r   r	   r   re   rf   r   r   r{   r   )clsmoduse_precomputed_fake_quantweight_post_processr   	float_modsupported_modules	error_msgr   	act_scaleact_zprh   qlinears                r#   
from_floatLinear.from_float  sn    3+,,+C0F4E4EE'=JJHHFF''FF&&FFJJFFMMFFKK($
CH #&"7"7&)&A&A#
 c//::%(%6%6$7! $		585F5FG5F	##5FG! s||n,GHYGZZefjknfoepqI,S1S5F5FF"!"FY  >=>  '*&A&A#+C0CNNB!f s$788 ""$**   *  

+#))3EEG	#R%RR#"3::#3#3#57JKcoos'7'7uE2i( [= Hs   )Kc                     U " UR                   UR                  5      nUR                  5       nUR                  XQR                  5        [        U5      Ul        [        U5      Ul        U$ )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
)	re   rf   get_quantized_weightr   r'   r   r   r{   r   )r   ref_qlinearoutput_scaleoutput_zero_pointr   rh   s         r#   from_referenceLinear.from_referenceZ  s]     k--{/G/GH224)9)9:l+ !23r%   )r.   re   rf   r   r   )F) rW   rX   rY   rZ   __doc__r[   nnr   modulesru   NonDynamicallyQuantizableLinearr   r   r   r   rl   rp   rU   r^   r=   r@   rK   r6   r&   r'   r   r   classmethodr   r   r_   r`   ra   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 ; ;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   r:   r%   r#   <module>r      s]    %   # # * *  8 C V V  
*b. b.Jr$ rr%   