
    Vh                     x    d dl mZmZ d dlZd dlmZ d dlmc mZ ddl	m
Z
 dgZ G d dej                  e
      Zy)    )AnyOptionalN   )ReferenceQuantizedModuleLinearc                       e Zd ZdZdZ	 	 	 	 ddedededeej                     deej                     d	eeeef      d
df fdZd
efdZdej                   d
ej                   fdZedej(                  d	eeef   d
d fd       Z xZS )r   aN  A reference quantized linear module that fits into the FX
    Graph Mode Quantization workflow
    activation will be floating point Tensor, we will store floating
    point weight as well in the module, but in forward we'll quantize
    and dequantize the weight before running the floating point functional
    linear operator.
    TNin_featuresout_featuresbias_devicedtypeweight_qparamsreturnc                 P    t         |   |||||       | j                  ||       y )N)super__init___init_weight_qparams)selfr	   r
   r   r   r   r   	__class__s          ^/home/dcms/DCMS/lib/python3.12/site-packages/torch/ao/nn/quantized/reference/modules/linear.pyr   zLinear.__init__   s*     	lE65I!!.&9    c                      y)NzQuantizedLinear(Reference) )r   s    r   	_get_namezLinear._get_name$   s    +r   xc                 h    | j                         }t        j                  ||| j                        }|S )aR  
        we have:
        w(float) -- quant - dequant         x(float) ------------- F.linear ---

        In the full model, we will see
        w(float) -- quant - *dequant         x -- quant --- *dequant --  *F.linear --- *quant - dequant
        and the backend should be able to fuse the ops with `*` into a quantized linear
        )
get_weightFlinearbias)r   r   weight_quant_dequantresults       r   forwardzLinear.forward'   s-      $0!1499=r   float_linearc                    t        |j                  |j                  |j                  d u|j                  j
                  |j                  j                  |      }t        j                  j                  |j                  j                               |_        |j                  <t        j                  j                  |j                  j                               |_        |S )N)r   r   r   )r   r	   r
   r    weightr   r   torchnn	Parameterdetach)clsr$   r   qref_linears       r   
from_floatzLinear.from_float6   s     $$%%T)&&--%%++)
 #XX//0C0C0J0J0LM($xx11,2C2C2J2J2LMKr   )TNNN)__name__
__module____qualname____doc___IS_REFERENCEintboolr   r'   r   r   dictstrr   r   r   Tensorr#   classmethodr(   r   r-   __classcell__)r   s   @r   r   r      s     M )-'+37
:
: 
: 	
:
 &
: $
: !c3h0
: 

:,3 , %,,  996:38n	 r   )typingr   r   r'   torch.nnr(   torch.nn.functional
functionalr   utilsr   __all__r   r   r   r   <module>r@      s3          + *8RYY0 8r   