U
    Kc                     @   s   d dl Z d dlZd dlZd dlZd dlmZ G dd dejje jdZ	dd Z
dd	 Zd
d Zdd Zdd Zdd ZedZdS )    N)
_addindentc                   @   s"   e Zd ZdZeejdd ZdS )WeightedQuantizedModulezIWrapper for quantized modules than can be lowered from reference modules.c                 C   s   t d S )N)NotImplementedError)clsZ
ref_moduleZoutput_scaleZoutput_zero_point r   G/tmp/pip-unpacked-wheel-gikjz4vx/torch/ao/nn/quantized/modules/utils.pyfrom_reference	   s    z&WeightedQuantizedModule.from_referenceN)__name__
__module____qualname____doc__classmethodabcabstractmethodr   r   r   r   r   r      s   r   )	metaclassc                 C   s   t | dr| j} | S )Nactivation_post_process)hasattrr   )observerr   r   r   _get_weight_observer   s    
r   c                 C   sB   t | } |tjtjtjfkr>t|}| j|jkp<| j|j	k S dS )NF)
r   torchqint8Zquint8Zqint32Ziinfo	quant_minmin	quant_maxmax)r   dtypeinfor   r   r   _needs_weight_clamping   s
    
r   c           	      C   s   t || js| S t|}|j|j }}t|  |}t|  |}t	t
|  ||}|jtjtjfkrt|| | } n:|jtjtjtjfkrtj||||jd} ntd|j | S )N)ZaxisUnexpected qscheme )r   r   r   r   r   r   cloneZint_reprZfill_Zminimummaximumqschemeper_tensor_symmetricper_tensor_affineZ!_make_per_tensor_quantized_tensoritemper_channel_symmetricper_channel_affine per_channel_affine_float_qparamsZ"_make_per_channel_quantized_tensorch_axis
ValueError)	qweightr   ZscaleZzpZmin_Zmax_Z
qw_int_maxZ
qw_int_minZqw_intr   r   r   _clamp_weights   s$    r+   c                 C   s   |  \}}|jtjtjfkrHt| t|t|tj}t	||||}n|jtj
tjfkr|j}t| |tj|tj|tj}t	||||}nR|jtjfkrt| |tj|tj|j|j}t	||||}ntd|j |S )Nr   )Zcalculate_qparamsr!   r   r"   r#   Zquantize_per_tensorfloatintr   r+   r%   r&   r(   Zquantize_per_channeltodoubleZint64r'   r   r)   )Zfloat_wtr   Zwt_scaleZwt_zpr*   Zwt_axisr   r   r   _quantize_weight3   s<      
 
  
 
  r0   c                    s    fdd}|S )zOConverts the argument to a tuple of size n
    with the first element repeated.c                    s6   t | tjjr&t|  krq&| d } q tt|  S )Nr   )
isinstancecollectionsr   Sequencelentuple	itertoolsrepeat)xnr   r   parseL   s
    
z!_ntuple_from_first.<locals>.parser   )r:   r;   r   r9   r   _ntuple_from_firstI   s    r<   c           
      C   s   g }|   }|r|d}g }| j D ]<\}}t||r<q(t|}t|d}|d| d |  q(|| }|  d }	|rt	|dkr|s|	|d 7 }	n|	dd
| d 7 }	|	d7 }	|	S )	N
   (z):    r   z
  ))
extra_reprsplitZ_modulesitemsr1   reprr   appendZ	_get_namer4   join)
selfparamsZextra_linesrB   Zchild_lineskeymoduleZmod_strlinesZmain_strr   r   r   hide_packed_params_reprT   s&    


rM   r>   )r   r   r6   r2   Ztorch.nn.modules.moduler   nnModuleABCMetar   r   r   r+   r0   r<   rM   Z_pair_from_firstr   r   r   r   <module>   s   