U
    d~
                     @   sn   d dl Z d dlZd dlm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edZdS )    N)repeat)
_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   D/tmp/pip-unpacked-wheel-ua33x9lu/torch/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   |  \}}|jtjtjfkr:t| t|t|tj}n|jtj	tj
fkrx|j}t| |tj|tj|tj}nD|jtjfkrt| |tj|tj|j|j}ntd|j |S )NzUnexpected qscheme )Zcalculate_qparamsZqschemetorchZper_tensor_symmetricZper_tensor_affineZquantize_per_tensorfloatintZqint8Zper_channel_symmetricZper_channel_affineZch_axisZquantize_per_channeltodoubleZint64Z per_channel_affine_float_qparamsZdtype
ValueError)Zfloat_wtZobserverZwt_scaleZwt_zpZqweightZwt_axisr   r   r   _quantize_weight   s6      
 
  
 
  r   c                    s    fdd}|S )zOConverts the argument to a tuple of size n
    with the first element repeated.c                    s4   t | tjjr&t|  krq&| d } q tt|  S )Nr   )
isinstancecollectionsr   Sequencelentupler   )xnr   r   parse$   s
    
z!_ntuple_from_first.<locals>.parser   )r    r!   r   r   r   _ntuple_from_first!   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itemsr   reprr   appendZ	_get_namer   join)
selfparamsZextra_linesr(   Zchild_lineskeymoduleZmod_strlinesZmain_strr   r   r   hide_packed_params_repr,   s&    


r3   r$   )r   r   	itertoolsr   r   Ztorch.nn.modules.moduler   nnModuleABCMetar   r   r"   r3   Z_pair_from_firstr   r   r   r   <module>   s   