a
    –¥»aú   ã                   @   sò   d Z ddlZddlZddlZddlmZmZ ddlmZm	Z	 dd„ Z
dd„ Zd	d
„ Ze	ee	dœdd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zejejed"œd#d$„Zeeeejeeeef d%œd&d'„ZdS )(z?
Utils shared by different modes of quantization (eager/graph)
é    N)Ú	QuantTypeÚquant_type_to_str)ÚTupleÚAnyc                 C   s   |   ¡ }| |¡ |S ©N)ÚcopyÚupdate)Zdefault_dictZadditional_dictÚd© r
   úU/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/torch/ao/quantization/utils.pyÚget_combined_dict
   s    
r   c                 C   s   | t jkp| t jkS r   )ÚtorchÚper_tensor_affineZper_tensor_symmetric©Úqschemer
   r
   r   Úis_per_tensor   s    
ÿr   c                 C   s   | t jt jt jfv S r   )r   Úper_channel_affineZ per_channel_affine_float_qparamsÚper_channel_symmetricr   r
   r
   r   Úis_per_channel   s    þr   )ÚobjÚfqnÚreturnc                 C   s   t  t| d¡| ¡S )zO
    Given an obj and a fqn such as "foo.bar.baz", returns gm.foo.bar.baz.
    Ú.)Ú	functoolsÚreduceÚgetattrÚsplit)r   r   r
   r
   r   Úgetattr_from_fqn   s    r   c                 C   s–   t | dƒr| jnd }| j}||dœ}|s,|S t|ƒr<tj}n2t|ƒr`|tjkrTtj}| j	|d< nt
d|› �ƒ‚||d< |  ¡ \}}||d< ||d< |S )Nr   )r   ÚdtypeZaxiszUnrecognized qscheme: ÚscaleÚ
zero_point)Úhasattrr   r   r   r   r   r   r   r   Zch_axisÚRuntimeErrorZcalculate_qparams)Zobserver_or_fake_quantr   r   Zqparamsr   r    r
   r
   r   Úget_qparam_dict   s"    

r#   c                 C   sH   t |ƒ}t|ƒ}| |i ¡}t| ƒ|v s<J d t| ƒ|¡ƒ‚|t| ƒ S )aÝ   Get the observed/quantized custom module class that we need
    to swap `custom_module` to
    Input:
        custom_module: input, can be an instance of either a float or observed custom module
        custom_module_class_mapping: the float to observed or observed to quantized custom module class mapping
        qconfig: qconfig configured for the custom module

    Output:
        corresponding observed/quantized custom module class for input custom module instance
    zFdid not find corresponding observed module class for {} in mapping: {})Úget_quant_typer   ÚgetÚtypeÚformat)Zcustom_moduleZcustom_module_class_mappingÚqconfigZ
quant_typeZquant_type_strZclass_mappingr
   r
   r   Úget_swapped_custom_module_class;   s    ÿr)   c                 C   s   | d usJ ‚|   ¡ }|jS r   )Ú
activationr   )r(   r*   r
   r
   r   Úactivation_dtypeM   s    r+   c                 C   s   | d usJ ‚|   ¡ }|jS r   )Úweightr   )r(   r,   r
   r
   r   Úweight_dtypeR   s    r-   c                 C   s   t | ƒtjtjtjfv S )z‡ Given a qconfig, decide if the activation needs to be
    quantized or not, this includes quantizing to quint8, qint8 and float16
    )r+   r   Úquint8Úqint8Úfloat16©r(   r
   r
   r   Ú"activation_is_statically_quantizedW   s    r2   c                 C   s   t | ƒtjtjfv S )zƒ Given a qconfig, decide if the activation needs to be
    quantized to int8 or not, this includes quantizing to quint8, qint8
    )r+   r   r.   r/   r1   r
   r
   r   Úactivation_is_int8_quantized]   s    r3   c                 C   s   t | ƒtjtjtjfv S )zL Given a qconfig, decide if the weight needs to be
    quantized or not
    )r-   r   r.   r/   r0   r1   r
   r
   r   Úweight_is_quantizedc   s    r4   c                 C   s   t | ƒtjtjfv S )zW Given a qconfig, decide if the weight needs to be statically
    quantized or not
    )r-   r   r.   r/   r1   r
   r
   r   Úweight_is_statically_quantizedi   s    r5   c                 C   s>   | dusJ ‚|   ¡ }|  ¡ }t|dƒr,|jnd}|j|j|fS )zk returns the qconfig tuple for qconfig:
    (activation_dtype, weight_dtype, activation_compute_dtype)
    NÚcompute_dtype)r*   r,   r!   r6   r   )r(   r*   r,   r6   r
   r
   r   Úget_qconfig_dtypeso   s
    r7   c                 C   sª   | d usJ ‚|   ¡ }|  ¡ }tjtjg}|j|v rb|j|v rBtjS t|dƒr\|j	|v r\tj
S tjS |jtjkr’|jtjkr€tj
S |jtjkr’tjS td |j|j¡ƒ‚d S )Nr6   zKUnrecognized dtype combination in get_quant_type: activation({}),weight({}))r*   r,   r   r.   r/   r   r   ZSTATICr!   r6   ZDYNAMICZWEIGHT_ONLYr0   ÚfloatÚ	Exceptionr'   )r(   r*   r,   Zstatic_dtypesr
   r
   r   r$   y   s$    

ÿr$   )Úmin_valÚmax_valr   c                 C   s    |   ¡ dks|  ¡ dkr&t d¡ dS |  ¡ dks>| ¡ dkr~| tdƒkrd|tdƒkrdt d¡ dS | |ksœJ d | |¡ƒ‚nt | |k¡sœJ d | |¡ƒ‚dS )z‰ Checks if the given minimum and maximum values are valid, meaning that
    they exist and the min value is less than the max value.
    r   zMmust run observer before calling calculate_qparams. Returning default values.FÚinfz-infz!min {} should be less than max {}T)ZnumelÚwarningsÚwarnZdimr8   r'   r   Úall)r:   r;   r
   r
   r   Úcheck_min_max_valid�   s(    ÿÿÿÿ
þr@   )Ú	quant_minÚ	quant_maxÚhas_customized_qranger   Úreduce_ranger   c           
      C   sø   |r¤d\}}| | }}|dur0|dur0|| }}|| d }	d|	  k rPdksZn J dƒ‚|t jkr~|	 d |	d d  } }nd|	d  } }|rð| d |d  } }nL|t jkrÆ|r¼d\} }qðd	\} }n*|t jkrè|rÞd
\} }qðd\} }nd\} }| |fS )ztCalculates actual qmin and qmax based on the quantization range,
    observer datatype and if range is reduced.
    )r   éÿ   Né   r   é   zRquantization range should be positive and not exceed the maximum bit range (=256).é   )iÀÿÿÿé?   )i€ÿÿÿé   )r   rJ   )r   é   )r   r/   r.   )
rA   rB   rC   r   rD   Zinitial_quant_minZinitial_quant_maxZcustom_quant_minZcustom_quant_maxZ
qrange_lenr
   r
   r   Úcalculate_qmin_qmax®   s:    
þÿÿ
þ






rL   )Ú__doc__r=   r   r   Z torch.ao.quantization.quant_typer   r   Útypingr   r   r   r   r   Ústrr   r#   r)   r+   r-   r2   r3   r4   r5   r7   r$   ZTensorÚboolr@   Úintr   rL   r
   r
   r
   r   Ú<module>   s.   

ÿ