
    qi,5                         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)    )IterableN)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j                  dz  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      j/home/ubuntu/crypto_trading_bot/.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biasNreturnc                 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    sm    ::$"'))"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.forward2   s    r"   c                 x    t         |   |||       | j                  ||dz   <   | j                         ||dz   <   y )Nr   r+   )r   _save_to_state_dictr   r2   r   destinationprefix	keep_varsr    s       r!   r:   z&LinearPackedParams._save_to_state_dictE   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_msgsr@   r#   r$   r    s              r!   rD   z(LinearPackedParams._load_from_state_dictJ   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 r4   )r2   __repr__r1   s    r!   rM   zLinearPackedParams.__repr__q   s      "++--r"   )__name__
__module____qualname___versionr   r   r   jitexportTensorr   r2   r8   r:   rD   rM   __classcell__r    s   @r!   r
   r
      s    H"[[ 	' YYQell Q%,,:M QRV 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j"                  dz  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   rY   rZ   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QuantizedLinearr5   r1   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=)rY   rZ   r   r   r#   qschemer1   s    r!   
extra_reprzLinear.extra_repr   s\    4++,OD<M<M;NhW[WaWaVb c//**T[[]5J5J5L4MO	
r"   c                 "    t        | t              S r4   )r   r
   r1   s    r!   rM   zLinear.__repr__   s    '.@AAr"   r7   r%   c                     t         j                  j                  j                  || j                  j                  | j
                  | j                        S r4   )r   r(   r)   linearr+   r   r   r6   s     r!   r8   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   r@   r   r#   r$   z_packed_params.weightz_packed_params.biasF)	r   r   rC   intr   rB   updater   rD   rE   s              r!   rD   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 r4   )r+   r2   r1   s    r!   r2   zLinear._weight_bias  s    ""//11r"   c                 (    | j                         d   S )Nr   r2   r1   s    r!   r#   zLinear.weight        "1%%r"   c                 (    | j                         d   S )Nr   rm   r1   s    r!   r$   zLinear.bias  rn   r"   wbNc                 <    | j                   j                  ||       y r4   )r+   r   )r   rp   rq   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epsrt   activation_post_process
isinstance_FLOAT_MODULEr   joinrN   typeformatnni
LinearReLUru   r   calculate_qparamsr   r   r   r   rY   rZ   r   r   ri   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  s]    3+,+C0F4E4EE'=JJHHFF''FF&&FFJJFFMMFFKK($
CH #&"7"7&)&A&A#
 c//:%(%6%6$7! $		585F5FG	##G! s||n,GHYGZZefjknfoepqI/48I8II   "I 3	* >* '*&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( [= H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
        )	rY   rZ   get_quantized_weightr   r$   r   r   ri   r   )r   ref_qlinearoutput_scaleoutput_zero_pointr   r\   s         r!   from_referencezLinear.from_referenceY  s_     k--{/G/GH224)9)9:l+ !23r"   )F)rN   rO   rP   __doc__rQ   nnr   modulesre   NonDynamicallyQuantizableLinearr   r   r   r   r_   rb   rM   rT   r8   r:   rD   r2   r#   r$   r   classmethodr   r   rU   rV   s   @r!   r   r   u   s    8 HYY

 1 1 Q QRM8<EKK 4!
B
 
%,, 
DK%
R2&&2 2%,,2E 2$ 2 < <|  r"   )collections.abcr   r   torch.ao.nn.intrinsicaor   	intrinsicr   torch.ao.nn.intrinsic.qatqatrw   torch.nntorch.nn.utils.fusionr   torch.nn.utils.parametrizer   utilsr   r   r	   __all__Moduler
   r   r5   r"   r!   <module>r      sZ    %  # # * *  8 C V V  
*`. `.Ft$ tr"   