o
    Zh                     @   sb   d dl Z d dlmZ d dlm  mZ d dlmZ d dlm	Z	m
Z
mZ dgZG dd dejZdS )    N)
LinearReLU)is_parametrized$transfer_parametrizations_and_paramstype_before_parametrizationsLinearc                       sR   e Zd ZdZejZ				d	d fddZdd Ze	dd
dZ
dd Z  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
    TNreturnc                    sF   ||d}t  j|||fi | |sJ d|| _|j|d| _d S )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__ M/var/www/auris/lib/python3.10/site-packages/torch/ao/nn/qat/modules/linear.pyr   !   s
   
	zLinear.__init__c                 C   s   t || | j| jS N)Flinearr   r   r   )r   inputr   r   r   forward0   s   zLinear.forwardFc                 C   s   t || jksJ d| j d | jj t|dsJ d|js$J dt |tkr.|d }|j}| |j|j|jdu|d}t	|d	rJt
||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modZuse_precomputed_fake_quantr   Z
qat_linearr   r   r   
from_float3   s8   

zLinear.from_floatc                 C   s\   t j| j| j| jd u}t j| j |_| jd ur&t j| j |_|	| j
 |S r   )torchnnr   r   r   r   	Parameterr   detachtrainZtraining)r   r   r   r   r   to_floatX   s   
zLinear.to_float)TNNN)r   N)F)r   
__module____qualname____doc__r$   r   r   r   r   classmethodr"   r(   __classcell__r   r   r   r   r      s    $)r#   Ztorch.nnr$   Ztorch.nn.functionalZ
functionalr   Ztorch.ao.nn.intrinsicr   Ztorch.nn.utils.parametrizer   r   r   __all__r   r   r   r   r   <module>   s   