
    9j                     z    d dl Z d dlmZ d dlmc mZ d dlmZ d dlm	Z	m
Z
mZ dgZ G d dej                        Zy)    N)
LinearReLU)is_parametrized$transfer_parametrizations_and_paramstype_before_parametrizationsLinearc                   f     e Zd ZdZej
                  Z	 	 	 	 d	 d fdZd Ze	dd       Z
d Z xZS )	r   a  
    A linear module attached with FakeQuantize modules for weight,
    used for quantization aware training.

    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 `torch.nn.Linear`, with FakeQuantize modules initialized to
    default.

    Attributes:
        weight: fake quant module for weight
    c                     ||d}t        |   |||fi | |st        d      || _        |j	                  |      | _        y )N)devicedtypez'qconfig must be provided for QAT module)factory_kwargs)super__init__AssertionErrorqconfigweightweight_fake_quant)	selfin_featuresout_featuresbiasr   r
   r   r   	__class__s	           ^/media/conek/DATA/Code/OCR/venv/lib/python3.12/site-packages/torch/ao/nn/qat/modules/linear.pyr   zLinear.__init__"   sN     %+U;lDKNK !JKK!(~!N    c                 v    t        j                  || j                  | j                        | j                        S N)Flinearr   r   r   )r   inputs     r   forwardzLinear.forward2   s(    xxt55dkkBDIINNr   c           
      h   t        |      | j                  k7  rEt        d| j                   d| j                  j                   dt        |      j                         t	        |d      st        d      |j
                  st        d      t        |      t        k(  r|d   }|j
                  } | |j                  |j                  |j                  du|	      }t        |d
      rt        ||d
       n|j                  |_        t        |d      rt        ||d       |S |j                  |_	        |S )zCreate a qat module from a float module or qparams_dict
        Args: `mod` a float module, either produced by torch.ao.quantization utilities
        or directly from user
        zqat.z.from_float only works for z, got r   z,Input float module must have qconfig definedz,Input float module must have a valid qconfigr   N)r   r   r   r   )r   _FLOAT_MODULEr   __name__hasattrr   r   r   r   r   r   r   r   )clsmoduse_precomputed_fake_quantr   
qat_linears        r   
from_floatzLinear.from_float5   s&    (,0A0AA s||n$?$$--.f5QRU5V5_5_4`b  sI& !OPP{{ !OPP',
:a&C++OO%	

 3)0j(K #

J3'0j&I  "hhJOr   c                    t         j                  j                  | j                  | j                  | j
                  d u      }t         j                  j                  | j                  j                               |_        | j
                  <t         j                  j                  | j
                  j                               |_        |j                  | j                         |S r   )torchnnr   r   r   r   	Parameterr   detachtraintraining)r   r   s     r   to_floatzLinear.to_float[   s    d//$1F
 **4;;+=+=+?@99 ((,,TYY-=-=-?@FKT]]#r   )TNNN)returnN)F)r"   
__module____qualname____doc__r+   r   r!   r   r   classmethodr(   r0   __classcell__)r   s   @r   r   r      sR     IIM O 
O O # #Jr   )r*   torch.nnr+   torch.nn.functional
functionalr   torch.ao.nn.intrinsicr   torch.nn.utils.parametrizer   r   r   __all__r    r   r   <module>r>      s9        ,  *SRYY Sr   