U
    yh                     @   s   d dl Z d dlZd dlZd dlZd dlmZ dg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)
_addindentWeightedQuantizedModulec                   @   s"   e Zd ZdZeejdd ZdS )r   zIWrapper 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   U/var/www/html/venv/lib/python3.8/site-packages/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   )	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_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   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_weight8   s<      
 
  
 
  r2   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   parseQ   s
    
z!_ntuple_from_first.<locals>.parser   )r<   r=   r   r;   r   _ntuple_from_firstN   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itemsr3   reprr   appendZ	_get_namer6   join)
selfparamsextra_linesrD   Zchild_lineskeymoduleZmod_strlinesZmain_strr   r   r   _hide_packed_params_reprY   s&    


rP   r@   )r   r   r8   r4   Ztorch.nn.modules.moduler   __all__nnModuleABCMetar   r   r   r-   r2   r>   rP   Z_pair_from_firstr   r   r   r   <module>   s   