a
    Þñh„#  ã                	   @   sÚ   d dl Z d dlmZ ddlmZmZmZmZ d dlZG dd„ deƒZ	e	j
ed de je jdd	�Ze	j
ed
de je jdd	�Ze	j
ed
de je jdd d�Ze	j
ed de je jdd	�Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )é    N)ÚModuleé   )ÚMovingAverageMinMaxObserverÚHistogramObserverÚ%MovingAveragePerChannelMinMaxObserverÚ
_with_argsc                       s´   e Zd ZdZeddf‡ fdd„	Zejjddd„ƒZ	ejjd	d
„ ƒZ
ejjddd„ƒZejjdd„ ƒZejjdd„ ƒZdd„ ZeeƒZejjdd„ ƒZ‡ fdd„Z‡ fdd„Z‡  ZS )ÚFakeQuantizea   Simulate the quantize and dequantize operations in training time.
    The output of this module is given by

    x_out = (clamp(round(x/scale + zero_point), quant_min, quant_max)-zero_point)*scale



    * :attr:`scale` defines the scale factor used for quantization.

    * :attr:`zero_point` specifies the quantized value to which 0 in floating point maps to

    * :attr:`quant_min` specifies the minimum allowable quantized value.

    * :attr:`quant_max` specifies the maximum allowable quantized value.

    * :attr:`fake_quant_enable` controls the application of fake quantization on tensors, note that
      statistics can still be updated.

    * :attr:`observer_enable` controls statistics collection on tensors

    * :attr:`dtype` specifies the quantized dtype that is being emulated with fake-quantization,
                    allowable values are torch.qint8 and torch.quint8. The values of quant_min and
                    quant_max should be chosen to be consistent with the dtype


    Args:
        observer (module): Module for observing statistics on input tensors and calculating scale
                           and zero-point.
        quant_min (int): The minimum allowable quantized value.
        quant_max (int): The maximum allowable quantized value.
        observer_kwargs (optional): Arguments for the observer module

    Attributes:
        observer (Module): User provided module that collects statistics on the input tensor and
                           provides a method to calculate scale and zero-point.

    r   éÿ   c                    s   t t| ƒ ¡  ||ksJ dƒ‚|| _|| _|  dtjdgtjd�¡ |  dtjdgtjd�¡ |f i |¤Ž| _	t 
| j	j¡j|ksŠJ dƒ‚|t 
| j	j¡jks¦J dƒ‚|  dt d	g¡¡ |  d
t dg¡¡ | j	j| _| j	j| _t| j	dƒrö| j	jnd| _d S )Nz1quant_min must be less than or equal to quant_maxÚfake_quant_enabledr   )ÚdtypeÚobserver_enabledzquant_min out of boundzquant_max out of boundÚscaleg      ð?Ú
zero_pointr   Úch_axiséÿÿÿÿ)Úsuperr   Ú__init__Ú	quant_minÚ	quant_maxZregister_bufferÚtorchZtensorZuint8Úactivation_post_processZiinfor   ÚminÚmaxÚqschemeÚhasattrr   )ÚselfÚobserverr   r   Zobserver_kwargs©Ú	__class__© ú`/home/yuhw/wc/uboone-dl-vtx/venv/lib/python3.9/site-packages/torch/quantization/fake_quantize.pyr   ,   s&    
ÿ


ÿ
ÿzFakeQuantize.__init__Tc                 C   s   |rdnd| j d< d S ©Nr   r   )r
   ©r   Zenabledr   r   r    Úenable_fake_quantA   s    zFakeQuantize.enable_fake_quantc                 C   s   |   d¡ d S ©NF)r#   ©r   r   r   r    Údisable_fake_quantF   s    zFakeQuantize.disable_fake_quantc                 C   s   |rdnd| j d< d S r!   )r   r"   r   r   r    Úenable_observerJ   s    zFakeQuantize.enable_observerc                 C   s   |   d¡ d S r$   )r'   r%   r   r   r    Údisable_observerO   s    zFakeQuantize.disable_observerc                 C   s
   | j  ¡ S ©N)r   Úcalculate_qparamsr%   r   r   r    r*   S   s    zFakeQuantize.calculate_qparamsc                 C   sæ   | j d dkrz|  | ¡ ¡ |  ¡ \}}| | jj¡| | jj¡ }}| j |j	¡ | j 
|¡ | j |j	¡ | j 
|¡ | jd dkrâ| jtjks | jtjkrÀt || j| j| j| j| j¡}n"t |t| jƒt| jƒ| j| j¡}|S )Nr   r   )r   r   Údetachr*   Útor   Zdevicer   Zresize_ÚshapeZcopy_r
   r   r   Úper_channel_symmetricZper_channel_affineZ fake_quantize_per_channel_affiner   r   r   Zfake_quantize_per_tensor_affineÚfloatÚint)r   ÚXZ_scaleZ_zero_pointr   r   r    ÚforwardW   s$    ÿþzFakeQuantize.forwardc                 C   s,   d  | j| j| j| j| j| j| j| j| j	¡	S )Nz”fake_quant_enabled={}, observer_enabled={},            quant_min={}, quant_max={}, dtype={}, qscheme={}, ch_axis={},         scale={}, zero_point={})
Úformatr
   r   r   r   r   r   r   r   r   r%   r   r   r    Ú
extra_reprm   s
    ûzFakeQuantize.extra_reprc                    s4   t t| ƒ |||¡ | j||d < | j||d < d S ©Nr   r   )r   r   Ú_save_to_state_dictr   r   )r   ZdestinationÚprefixZ	keep_varsr   r   r    r6   v   s    z FakeQuantize._save_to_state_dictc              	      sf   ddg}|D ]8}	||	 }
|
|v r6||
 }t | |	|ƒ q|r| |
¡ qtt| ƒ |||||||¡ d S r5   )ÚsetattrÚappendr   r   Ú_load_from_state_dict)r   Z
state_dictr7   Zlocal_metadataÚstrictZmissing_keysZunexpected_keysZ
error_msgsZlocal_stateÚnameÚkeyÚvalr   r   r    r:   }   s    ÿz"FakeQuantize._load_from_state_dict)T)T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   ÚjitÚexportr#   r&   r'   r(   r*   r2   Úclassmethodr   Ú	with_argsr4   r6   r:   Ú__classcell__r   r   r   r    r      s$   %



r   r	   T)r   r   r   r   r   Úreduce_rangei€ÿÿÿé   F)r   r   r   r   r   rH   r   c                 C   s<   t | tjjƒr8| jj dd¡d }t dd|¡}|dkS dS )zM Returns true if given mod is an instance of FakeQuantize script module.
    Ú.r   z\.___torch_mangle_\d+Ú z-torch.quantization.fake_quantize.FakeQuantizeF)	Ú
isinstancer   rC   ZRecursiveScriptModuleZ_cZqualified_nameÚsplitÚreÚsub)ÚmodÚsuffixr<   r   r   r    Ú_is_fake_quant_script_moduleŸ   s
    rR   c                 C   s    t | ƒtkst| ƒr|  ¡  d S r)   )Útyper   rR   r&   ©rP   r   r   r    r&   ©   s    r&   c                 C   s    t | ƒtkst| ƒr|  ¡  d S r)   )rS   r   rR   r#   rT   r   r   r    r#   ­   s    r#   c                 C   s    t | ƒtkst| ƒr|  ¡  d S r)   )rS   r   rR   r(   rT   r   r   r    r(   ±   s    r(   c                 C   s    t | ƒtkst| ƒr|  ¡  d S r)   )rS   r   rR   r'   rT   r   r   r    r'   µ   s    r'   )r   Ztorch.nnr   r   r   r   r   r   rN   r   rF   Zquint8Zper_tensor_affineZdefault_fake_quantZqint8Zper_tensor_symmetricZdefault_weight_fake_quantr.   Z%default_per_channel_weight_fake_quantZdefault_histogram_fake_quantrR   r&   r#   r(   r'   r   r   r   r    Ú<module>   s>    

ÿ

ÿúû
