U
    d                     @   s\   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 G dd dejZdS )    N)
LinearReLU)is_parametrizedtype_before_parametrizations$transfer_parametrizations_and_paramsc                       sJ   e Zd ZdZejZddd fddZdd Ze	d	d
 Z
dd Z  ZS )Lineara  
    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                    sB   ||d}t  j|||f| |s*td|| _|j|d| _d S )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__ ?/tmp/pip-unpacked-wheel-ua33x9lu/torch/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   forward$   s    zLinear.forwardc                 C   s   t || jks(td| j d | jj t|ds:td|jsHtdt |tkr\|d }|j}| |j|j|j	dk	|d}t
|d	rt||d	 n|j|_t
|d
rt||d
 n|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_MODULEr   __name__hasattrr   r   r   r   r   r   r   r   )clsmodr   Z
qat_linearr   r   r   
from_float'   s,    

zLinear.from_floatc                 C   s\   t j| j| j| jd k	}t j| j |_| jd k	rLt j| j |_|	| j
 |S r   )torchnnr   r   r   r   	Parameterr   detachZtrainZtraining)r   r   r   r   r   to_floatG   s    
zLinear.to_float)TNNN)r   
__module____qualname____doc__r%   r   r   r   r   classmethodr#   r(   __classcell__r   r   r   r   r      s         
r   )r$   Ztorch.nnr%   Ztorch.nn.functionalZ
functionalr   Ztorch.nn.intrinsicr   Ztorch.nn.utils.parametrizer   r   r   r   r   r   r   r   <module>   s
   