o
    ZhW                     @   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 )N)NotImplementedError)clsZ
ref_moduleZoutput_scaleZoutput_zero_point r   R/var/www/auris/lib/python3.10/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v 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v rIt|| | } | S |jtjtjtjfv r`tj||||jd} | S td|j )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v r%t| t|t|tj}t	||||}|S |jtj
tjfv rL|j}t| |tj|tj|tj}t	||||}|S |jtjfv rot| |tj|tj|j|j}t	||||}|S td|j )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_weightE   s:   



r2   c                    s    fdd}|S )zOConverts the argument to a tuple of size n
    with the first element repeated.c                    sB   t | tjjrt|  krn| d } t | tjjstt|  S )Nr   )
isinstancecollectionsr   Sequencelentuple	itertoolsrepeat)xnr   r   parseh   s   z!_ntuple_from_first.<locals>.parser   )r<   r=   r   r;   r   _ntuple_from_firstd   s   r>   c           
      C   s   g }|   }|r|d}g }| j D ]\}}t||rqt|}t|d}|d| d |  q|| }|  d }	|rYt	|dkrN|sN|	|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_reprr   s&   


rP   r@   )r   r4   r8   r   Ztorch.nn.modules.moduler   __all__nnModuleABCMetar   r   r   r-   r2   r>   rP   Z_pair_from_firstr   r   r   r   <module>   s   	