3
O4W^ú  ã               @   sF   d dl mZ G dd„ dejƒZG dd„ dejƒZG dd„ dejƒZdS )	é    )Únnc                   s*   e Zd ZdZd‡ fdd„	Zdd„ Z‡  ZS )Ú	QuantStuba  Quantize stub module, before calibration, this is same as an observer,
    it will be swapped as `nnq.Quantize` in `convert`.

    Args:
        qconfig: quantization configuration for the tensor,
            if qconfig is not provided, we will get qconfig from parent modules
    Nc                s   t t| ƒjƒ  |r|| _d S )N)Úsuperr   Ú__init__Úqconfig)Úselfr   )Ú	__class__© ú9/tmp/pip-build-e66j97b2/torch/torch/quantization/stubs.pyr      s    zQuantStub.__init__c             C   s   |S )Nr	   )r   Úxr	   r	   r
   Úforward   s    zQuantStub.forward)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Ú__classcell__r	   r	   )r   r
   r      s   r   c                   s(   e Zd ZdZ‡ fdd„Zdd„ Z‡  ZS )ÚDeQuantStubz…Dequantize stub module, before calibration, this is same as identity,
    this will be swapped as `nnq.DeQuantize` in `convert`.
    c                s   t t| ƒjƒ  d S )N)r   r   r   )r   )r   r	   r
   r      s    zDeQuantStub.__init__c             C   s   |S )Nr	   )r   r   r	   r	   r
   r      s    zDeQuantStub.forward)r   r   r   r   r   r   r   r	   r	   )r   r
   r      s   r   c                   s(   e Zd ZdZ‡ fdd„Zdd„ Z‡  ZS )ÚQuantWrapperaç  A wrapper class that wraps the input module, adds QuantStub and
    DeQuantStub and surround the call to module with call to quant and dequant
    modules.

    This is used by the `quantization` utility functions to add the quant and
    dequant modules, before `convert` function `QuantStub` will just be observer,
    it observes the input tensor, after `convert`, `QuantStub`
    will be swapped to `nnq.Quantize` which does actual quantization. Similarly
    for `DeQuantStub`.
    c                s\   t t| ƒjƒ  t|dƒr|jnd }| jdt|ƒƒ | jdtƒ ƒ | jd|ƒ | j|j	ƒ d S )Nr   ÚquantÚdequantÚmodule)
r   r   r   Úhasattrr   Z
add_moduler   r   ZtrainZtraining)r   r   r   )r   r	   r
   r   +   s    zQuantWrapper.__init__c             C   s   | j |ƒ}| j|ƒ}| j|ƒS )N)r   r   r   )r   ÚXr	   r	   r
   r   3   s    

zQuantWrapper.forward)r   r   r   r   r   r   r   r	   r	   )r   r
   r       s   
r   N)Ztorchr   ÚModuler   r   r   r	   r	   r	   r
   Ú<module>   s   