U
    Kc                     @   sZ   d dl Z d dlm  m  mZ d dlmZ d dlm  m  m	Z
 G dd dejZdS )    N)_quantize_weightc                       sh   e Zd ZdZdZdejf fdd	Zdd Zdd	 Z	d
d Z
 fddZedd Zedd Z  ZS )Lineara  
    A dynamic quantized linear module with floating point tensor as inputs and outputs.
    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 :class:`torch.nn.Linear`, attributes will be randomly
    initialized at module creation time and will be overwritten later

    Attributes:
        weight (Tensor): the non-learnable quantized weights of the module which are of
                         shape :math:`(\text{out\_features}, \text{in\_features})`.
        bias (Tensor): the non-learnable floating point bias of the module of shape
                       :math:`(\text{out\_features})`. If :attr:`bias` is ``True``,
                       the values are initialized to zero.

    Examples::

        >>> m = nn.quantized.dynamic.Linear(20, 30)
        >>> input = torch.randn(128, 20)
        >>> # xdoctest: +SKIP
        >>> output = m(input)
        >>> print(output.size())
        torch.Size([128, 30])
       Tc                    s"   t t| j||||d d| _d S )Ndtyper   )superr   __init__version)selfin_featuresout_featuresZbias_r   	__class__ P/tmp/pip-unpacked-wheel-gikjz4vx/torch/ao/nn/quantized/dynamic/modules/linear.pyr   "   s    zLinear.__init__c                 C   s   | j jtjkrR| jd ks"| jdk r8tjj|| j j }q~tjjj|| j j dd}n,| j jtjkrvtjj	|| j j }nt
d||jS )Nr   T)Zreduce_rangez.Unsupported dtype on dynamic quantized linear!)_packed_paramsr   torchqint8r	   ops	quantizedZlinear_dynamicfloat16Zlinear_dynamic_fp16RuntimeErrorto)r
   xYr   r   r   forward*   s$        zLinear.forwardc                 C   s   dS )NZDynamicQuantizedLinearr   )r
   r   r   r   	_get_name:   s    zLinear._get_namec                 C   s>   d | j| j| jj}| jjtjkr:|d |   7 }|S )Nz)in_features={}, out_features={}, dtype={}z, qscheme={})	formatr   r   r   r   r   r   weightZqscheme)r
   Zextra_repr_strr   r   r   
extra_repr=   s      zLinear.extra_reprc           	   	      s2   | dd }|| _tt| |||d||| d S )Nr	   F)getr	   r   r   _load_from_state_dict)	r
   Z
state_dictprefixZlocal_metadatastrictZmissing_keysZunexpected_keysZ
error_msgsr	   r   r   r   r!   E   s      zLinear._load_from_state_dictc                 C   s>  t jjt jjjjt jjjjjt j	jj
jjg}t||ksRtdtdd |D  t|dsdtdt|tjkrz|d }|jdk	r|jjdk	r|j }nddlm} | }|j}|t jt jfkstd	|||j |t jkrt|j |}n |t jkr|j }ntd
| |j|j|d}|||j |S )zCreate a dynamic quantized module from a float module or qparams_dict

        Args:
            mod (Module): a float module, either produced by torch.ao.quantization
                          utilities or provided by the user
        z<nn.quantized.dynamic.Linear.from_float only works for one ofc                 S   s   g | ]
}|j qS r   )__name__).0Z	float_modr   r   r   
<listcomp>Y   s     z%Linear.from_float.<locals>.<listcomp>qconfigz,Input float module must have qconfig definedr   N)default_dynamic_qconfigzTThe only supported dtypes for dynamic quantized linear are qint8 and float16 got: {}z9Unsupported dtype specified for dynamic quantized Linear!r   ) r   nnr   modulesZlinearZNonDynamicallyQuantizableLinear	intrinsicZfusedZ
LinearReLUaoZqatZdynamictypeAssertionErrorstrhasattrnnir'   r   Ztorch.ao.quantization.qconfigr(   r   r   r   r   r   floatr   r   r   set_weight_biasbias)clsmodZfloat_modulesZweight_observerr(   r   qweightqlinearr   r   r   
from_floatL   s:     

zLinear.from_floatc                 C   s2   | |j |j|jd}| }|j}||| |S )a   Create a (fbgemm/qnnpack) dynamic quantized module from a reference quantized
        module
        Args:
            ref_qlinear (Module): a reference quantized  module, either produced by
            torch.ao.quantization functions or provided by the user
        r   )r   r   Zweight_dtypeZget_quantized_weightr4   r3   )r5   Zref_qlinearr8   r7   r4   r   r   r   from_references   s
    zLinear.from_reference)r$   
__module____qualname____doc___versionr   r   r   r   r   r   r!   classmethodr9   r:   __classcell__r   r   r   r   r      s   
&r   )r   Ztorch.ao.nn.quantizedr,   r)   r   ZnnqZ#torch.ao.nn.quantized.modules.utilsr   Ztorch.ao.nn.intrinsicr+   r1   r   r   r   r   r   <module>   s   