
    sg                     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J d       || _        |j                  |      | _        y )N)devicedtypez'qconfig must be provided for QAT module)factory_kwargs)super__init__qconfigweightweight_fake_quant)	selfin_featuresout_featuresbiasr   r
   r   r   	__class__s	           Q/var/www/html/venv/lib/python3.12/site-packages/torch/ao/nn/qat/modules/linear.pyr   zLinear.__init__!   sM     %+U;lDKNKAAAw!(~!N    c                 v    t        j                  || j                  | j                        | j                        S N)Flinearr   r   r   )r   inputs     r   forwardzLinear.forward0   s(    xxt55dkkBDIINNr   c                 *   t        |      | j                  k(  s.J d| j                  z   dz   | j                  j                  z          t        |d      sJ d       |j                  sJ 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 r   z,Input float module must have qconfig definedz,Input float module must have a valid qconfigr   N)r   r   r   r   )r   _FLOAT_MODULE__name__hasattrr   r   r   r   r   r   r   r   )clsmoduse_precomputed_fake_quantr   
qat_linears        r   
from_floatzLinear.from_float3   s    ,C0C4E4EE 	
ll+, (()	
E sI&V(VV&{{JJJ{',
: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_floatX   s    d//$1F
 **4;;+=+=+?@99 ((,,TYY-=-=-?@FKT]]#r   )TNNN)returnN)F)r!   
__module____qualname____doc__r*   r   r    r   r   classmethodr'   r/   __classcell__)r   s   @r   r   r      sR     IIM O 
OO " "H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        ,  *PRYY Pr   