U
    Kc                     @   s   d dl Z d dlZG dd de jjZe je je je je je jdddZe je je je je je jdddZ	d	d
 Z
ejeejf edddZdS )    Nc                       s@   e Zd Zdd Zdd Zdd Z fddZ fd	d
Z  ZS )ReferenceQuantizedModulec           
      C   s  |d krt jt jddd}|d | _|d | _| jd t jt jt jfksdttd| j d| 	  | jt jt j
t jt jfkrt|d t jr|d jnt j}|d	 }t|t jr|  nt j|t j|d
}| d| |d }t|t jr|  nt j|||d
}| d| | jt jt jfkrp|d }t|t jrP|  nt j|t j|d
}	| d|	 n| dt jdt j|d
 nN| dt jdt j|d
 | dt jdt j|d
 | dt jdt j|d
 d S )Ng      ?r   )qschemedtypescale
zero_pointr   r   z	qscheme: z' is not support in reference quantized r   r   )r   deviceweight_scaleweight_zero_pointZaxisweight_axis)torchper_tensor_affinequint8weight_qschemeweight_dtypeper_channel_affine per_channel_affine_float_qparamsAssertionError	ExceptionZ	_get_nameqint8quint4x2qint32
isinstanceTensorr   intclonedetachZtensorfloatZregister_buffer)
selfZweight_qparamsr   Zzero_point_dtypeZw_scaleZw_scale_tensorZw_zpZw_zp_tensorZw_axisZw_axis_tensor r   Q/tmp/pip-unpacked-wheel-gikjz4vx/torch/ao/nn/quantized/reference/modules/utils.py_init_weight_qparams   sd    

  


  z-ReferenceQuantizedModule._init_weight_qparamsc                 C   sT   t | jtjstt | jtjs$tt | jtjs6tt| j| j	| j
| j| j| jS )z
        Fake quantize (quantize and dequantize) the weight with
        the quantization parameters for weight, this is used to
        simulate the numerics for the quantized weight in a quantized
        model
        )r   r   r   r   r   r	   r
   _quantize_and_dequantize_weightweightr   r   r   r   r   r   
get_weight2   s     z#ReferenceQuantizedModule.get_weightc                 C   sT   t | jtjstt | jtjs$tt | jtjs6tt| j| j	| j
| j| j| jS N)r   r   r   r   r   r	   r
   _quantize_weightr"   r   r   r#   r   r   r   get_quantized_weightD   s    z-ReferenceQuantizedModule.get_quantized_weightc                    s2   t  ||| t||| j| j| j| j| j d S r%   )super_save_to_state_dict_save_weight_qparamsr   r   r   r	   r
   )r   destinationprefixZ	keep_vars	__class__r   r   r)   Q   s         z,ReferenceQuantizedModule._save_to_state_dictc           	   	      sN   t ||D ]&}t| ||||   |||  q
t |||d||| d S )NF)_get_weight_qparam_keyssetattrpopr(   _load_from_state_dict)	r   
state_dictr,   Zlocal_metadatastrictZmissing_keysZunexpected_keysZ
error_msgskeyr-   r   r   r2   W   s         z.ReferenceQuantizedModule._load_from_state_dict)	__name__
__module____qualname__r    r$   r'   r)   r2   __classcell__r   r   r-   r   r      s
   -r   r"   r   r   r   r	   r
   c                 C   s   |t jkr| |} | S |t jkrL|t jt jt jfkrt | |||} | S nB|t jt j	fkr|t jt jt j
t jfkrt | ||| |} | S td| d| d S )NzUnsupported dtype and qscheme: z, )r   Zfloat16tor   r   r   r   Zquantize_per_tensorr   r   r   quantize_per_channelitemr   r:   r   r   r   r&   a   s$    


   r&   c                 C   s8   |t jt jt jfkr0t| |||||}| }n| }|S )zV Quantize and then dequantize the weight based on
    the quantization parameters
    )r   r   r   r   r&   Z
dequantize)r"   r   r   r   r	   r
   Zweight_quantZweight_dequantr   r   r   r!   x   s     
     
r!   c                 C   sR   || |d < || |d < |d k	rN|| |d < || |d < |t jkrN|| |d < d S Nr   r   r   r	   r
   )r   r   )r+   r,   r   r   r   r	   r
   r   r   r   r*      s    
r*   )r3   r,   c                 C   sH   ddg}| |d  }|d k	rD| d | d |tjkrD| d |S r>   )appendr   r<   )r3   r,   keysr   r   r   r   r/      s    



r/   )r   typingnnModuler   r   r   r   r&   r!   r*   DictstrAnyr/   r   r   r   r   <module>   s(   ^
