3
O4W^N  ã            	   @   sâ   d dl mZmZmZmZ d dlZd dlmZ ddlm	Z	m
Z
mZm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S )é    )Úabsolute_importÚdivisionÚprint_functionÚunicode_literalsN)ÚModuleé   )ÚMovingAverageMinMaxObserverÚHistogramObserverÚ%MovingAveragePerChannelMinMaxObserverÚ
_with_argsc                   s„   e Zd ZdZeddf‡ fdd„	Zddd„Zd	d
„ Zddd„Zdd„ Z	dd„ Z
dd„ ZeeƒZ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| ƒjƒ  ||kstdƒ‚|| _|| _d| _d| _|f |Ž| _t	j
| jjƒj|ks^tdƒ‚|t	j
| jjƒjksztdƒ‚d | _d | _| jj| _| jj| _t| jdƒr®| jjnd| _d S )Nz1quant_min must be less than or equal to quant_maxTzquant_min out of boundzquant_max out of boundÚch_axisr   )Úsuperr   Ú__init__ÚAssertionErrorÚ	quant_minÚ	quant_maxÚfake_quant_enabledÚobserver_enabledÚobserverÚtorchZiinfoÚdtypeÚminÚmaxÚscaleÚ
zero_pointÚqschemeÚhasattrr   )Úselfr   r   r   Zobserver_kwargs)Ú	__class__© úA/tmp/pip-build-e66j97b2/torch/torch/quantization/fake_quantize.pyr   )   s    


zFakeQuantize.__init__Tc             C   s
   || _ | S )N)r   )r   Úenabledr!   r!   r"   Úenable_fake_quant:   s    zFakeQuantize.enable_fake_quantc             C   s
   | j dƒS )NF)r$   )r   r!   r!   r"   Údisable_fake_quant>   s    zFakeQuantize.disable_fake_quantc             C   s
   || _ | S )N)r   )r   r#   r!   r!   r"   Úenable_observerA   s    zFakeQuantize.enable_observerc             C   s
   | j dƒS )NF)r&   )r   r!   r!   r"   Údisable_observerE   s    zFakeQuantize.disable_observerc             C   s
   | j jƒ S )N)r   Úcalculate_qparams)r   r!   r!   r"   r(   H   s    zFakeQuantize.calculate_qparamsc             C   sˆ   | j r$| j|jƒ ƒ | jƒ \| _| _| jr„| jtj	ksB| jtj
krbtj|| j| j| j| j| jƒ}n"tj|t| jƒt| jƒ| j| jƒ}|S )N)r   r   Údetachr(   r   r   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   ÚXr!   r!   r"   ÚforwardK   s    zFakeQuantize.forwardc             C   s   dj | j| j| j| jƒS )NzNfake_quant_enabled={}, observer_enabled={},            scale={}, zero_point={})Úformatr   r   r   r   )r   r!   r!   r"   Ú
extra_repr[   s    zFakeQuantize.extra_reprc                s4   t t| ƒj|||ƒ | j||d < | j||d < d S )Nr   r   )r   r   Ú_save_to_state_dictr   r   )r   ZdestinationÚprefixZ	keep_vars)r    r!   r"   r1   a   s    z FakeQuantize._save_to_state_dictc                s@   |j |d ƒ| _|j |d ƒ| _tt| ƒj|||d|||ƒ d S )Nr   r   F)Úpopr   r   r   r   Ú_load_from_state_dict)r   Z
state_dictr2   Zlocal_metadataÚstrictZmissing_keysZunexpected_keysZ
error_msgs)r    r!   r"   r4   h   s    z"FakeQuantize._load_from_state_dict)T)T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r$   r%   r&   r'   r(   r.   Úclassmethodr   Ú	with_argsr0   r1   r4   Ú__classcell__r!   r!   )r    r"   r      s   "

r   r   T)r   r   r   r   r   Úreduce_rangeé€   é   F)r   r   r   r   r   r=   r   c             C   s   t | ƒtkr| jƒ  d S )N)Útyper   r%   )Úmodr!   r!   r"   r%   ‚   s    r%   c             C   s   t | ƒtkr| jƒ  d S )N)r@   r   r$   )rA   r!   r!   r"   r$   †   s    r$   c             C   s   t | ƒtkr| jƒ  d S )N)r@   r   r'   )rA   r!   r!   r"   r'   Š   s    r'   c             C   s   t | ƒtkr| jƒ  d S )N)r@   r   r&   )rA   r!   r!   r"   r&   Ž   s    r&   i€ÿÿÿi€ÿÿÿ)Ú
__future__r   r   r   r   r   Ztorch.nnr   r   r   r	   r
   r   r   r;   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_quantr%   r$   r'   r&   r!   r!   r!   r"   Ú<module>   s2   j

