3
O4W^ 5  ã               @   s  d dl mZmZmZmZ d dlZd dlZd dlZd dlZd dl	j
Z
d dlj
jZd dlj
jZddlmZmZmZmZ ddlmZmZ ddlmZmZ d#dd	„Zd$d
d„Zdd„ Zdd„ Zdd„ Z d%dd„Z!d&dd„Z"dej#ddfdd„Z$edfdd„Z%d'dd„Z&d(dd„Z'dd „ Z(d)d!d"„Z)dS )*é    )Úabsolute_importÚdivisionÚprint_functionÚunicode_literalsNé   )ÚDEFAULT_DYNAMIC_MODULE_MAPPINGÚDEFAULT_MODULE_MAPPINGÚDEFAULT_QAT_MODULE_MAPPINGÚ$DEFAULT_QCONFIG_PROPAGATE_WHITE_LIST)ÚDeQuantStubÚQuantWrapper)Údefault_dynamic_qconfigÚfloat16_dynamic_qconfigÚ c       	      C   s„   |dkrt }|jt| ƒ|ƒ}|j||ƒ}t| d|ƒ}t| ƒ|krF|| _x8| jƒ D ],\}}|rh|d | n|}t|||||ƒ qPW dS )ay  This is a helper function for `propagate_qconfig_`

    Args:
        module: input module
        qconfig_dict: dictionary that maps from name of submodule to quantization
                     configuration
        white_list: list of quantizable modules
        qconfig_parent: quantization config of parent module, we will fallback to
                       this config when there is no specified config for current
                       module
        prefix: corresponding prefix of the current module, used as key in
                qconfig_dict

    Return:
        None, module is modified inplace with qconfig attached
    NÚqconfigÚ.)r
   ÚgetÚtypeÚgetattrr   Únamed_childrenÚ_propagate_qconfig_helper)	ÚmoduleÚqconfig_dictZ
white_listZqconfig_parentÚprefixZmodule_qconfigÚnameÚchildÚmodule_prefix© r   ú</tmp/pip-build-e66j97b2/torch/torch/quantization/quantize.pyr      s    r   c             C   s   |dkri }t | |ƒ dS )a  Propagate qconfig through the module hierarchy and assign `qconfig`
    attribute on each leaf module

    Args:
        module: input module
        qconfig_dict: dictionary that maps from name or type of submodule to
            quantization configuration, qconfig applies to all submodules of a
            given module unless qconfig for the submodules are specified (when
            the submodule already has qconfig attribute)

    Return:
        None, module is modified inplace with qconfig attached
    N)r   )r   r   r   r   r   Úpropagate_qconfig_5   s    r   c             C   s
   | j |ƒS )z3Forward hook that calls observer on the output
    )Úobserver)ÚselfÚinputÚoutputr   r   r   Ú_observer_forward_hookG   s    r$   c             C   sŒ   xH| j ƒ D ]<}t|ƒtjkr>t|dƒrF|jdk	rF|jjƒ |_q
t|ƒ q
W t| dƒrˆ| jdk	rˆt	| j
ƒdkrˆ| jd| jjƒ ƒ | jtƒ dS )at  Add observer for the leaf child of the module.

    This function insert observer module to all leaf child module that
    has a valid qconfig attribute.

    Args:
        module: input module with qconfig attributes for all the leaf modules that we want to quantize

    Return:
        None, module is modified inplace with added observer modules and forward_hooks
    r   Nr   r    )Úchildrenr   ÚnnqZFloatFunctionalÚhasattrr   Z
activationr    Úadd_observer_ÚlenÚ_modulesZ
add_moduleZregister_forward_hookr$   )r   r   r   r   r   r(   L   s    r(   c             C   sN   t | jƒdkr&t| dƒr&| jr&t| ƒS x"| jƒ D ]\}}t|ƒ| j|< q0W | S )aƒ  Wrap the leaf child module in QuantWrapper if it has a valid qconfig
    Note that this function will modify the children of module inplace and it
    can return a new module which wraps the input module as well.

    Args:
        module: input module with qconfig attributes for all the leaf modules
                that we want to quantize

    Return:
        Either the inplace modified module with submodules wrapped in
        `QuantWrapper` based on qconfig or a new `QuantWrapper` module which
        wraps the input module, the latter case only happens when the input
        module is a leaf module and we want to quantize it.
    r   r   )r)   r*   r'   r   r   r   Úadd_quant_dequant)r   r   r   r   r   r   r+   g   s
    r+   Fc             C   sB   |st j| ƒ} t| ƒ tdd„ | jƒ D ƒƒs6tjdƒ t| ƒ | S )a  Prepares a copy of the model for quantization calibration or quantization-aware training.

    Quantization configuration can be passed as an `qconfig_dict` or assigned preemptively
    to individual submodules in `.qconfig` attribute.

    The model will be attached with observer or fake quant modules, and qconfig
    will be propagated.

    Args:
        model: input model to be modified in-place
        qconfig_dict: dictionary that maps from name or type of submodule to quantization
            configuration, qconfig applies to all submodules of a given
            module unless qconfig for the submodules are specified (when the
            submodule already has qconfig attribute)
        inplace: carry out model transformations in-place, the original module is mutated
    c             s   s   | ]}t |d ƒo|jV  qdS )r   N)r'   r   )Ú.0Úmr   r   r   ú	<genexpr>’   s    zprepare.<locals>.<genexpr>z¬None of the submodule got qconfig applied. Make sure you passed correct configuration through `qconfig_dict` or by assigning the `.qconfig` attribute directly on submodules)ÚcopyÚdeepcopyr   ÚanyÚmodulesÚwarningsÚwarnr(   )Úmodelr   Úinplacer   r   r   Úprepare}   s    

r7   c             C   sJ   |dkrt }|stj| ƒ} | jƒ  t| dd� || |ƒ t| |dd� | S )aå  Converts a float model to quantized model.

    First it will prepare the model for calibration or training, then it calls
    `run_fn` which will run the calibration step or training step,
    after that we will call `convert` which will convert the model to a
    quantized model.

    Args:
        model: input model
        run_fn: a function for evaluating the prepared model, can be a
            function that simply runs the prepared model or a training loop
        run_args: positional arguments for `run_fn`
        inplace: carry out model transformations in-place, the original module is mutated
        mapping: correspondence between original module types and quantized counterparts

    Return:
        Quantized model.
    NT)r6   )r   r/   r0   Úevalr7   Úconvert)r5   Úrun_fnÚrun_argsÚmappingr6   r   r   r   Úquantize™   s    

r=   c             C   sÔ   |dkrJ|t jkr$tjttjti}q–|t jkr:tjti}q–tdj	|ƒƒ‚nLt
|tƒr–|t jkrdt}n|t jkrtt}ntdt|ƒƒ‚tt|tj|ƒƒƒ}|dkr¢t}|s°tj| ƒ} | jƒ  t| |ƒ t| |dd� | S )a{  Converts a float model to dynamic (i.e. weights-only) quantized model.

    Replaces specified modules with dynamic weight-only quantized versions and output the quantized model.

    For simplest usage provide `dtype` argument that can be float16 or qint8. Weight-only quantization
    by default is performed for layers with large weights size - i.e. Linear and RNN variants.

    Fine grained control is possible with `qconfig` and `mapping` that act similarly to `quantize()`.
    If `qconfig` is provided, the `dtype` argument is ignored.

    Args:
        module: input model
        qconfig_spec: Either:
            * A dictionary that maps from name or type of submodule to quantization
              configuration, qconfig applies to all submodules of a given
              module unless qconfig for the submodules are specified (when the
              submodule already has qconfig attribute). Entries in the dictionary
              need to be QConfigDynamic instances.
            * A set of types and/or submodule names to apply dynamic quantization to,
              in which case the `dtype` argument is used to specifiy the bit-width
        inplace: carry out model transformations in-place, the original module is mutated
        mapping: maps type of a submodule to a type of corresponding dynamically quantized version
            with which the submodule needs to be replaced
    NzTDon't know how to quantize with default settings for {}. Provide full qconfig pleasez.Unknown dtype specified for quantize_dynamic: T)r6   )ÚtorchÚqint8ÚnnZLinearr   ZLSTMZfloat16r   Ú
ValueErrorÚformatÚ
isinstanceÚsetÚRuntimeErrorÚstrÚdictÚzipÚ	itertoolsÚrepeatr   r/   r0   r8   r   r9   )r5   Zqconfig_specZdtyper<   r6   Zdefault_qconfigr   r   r   Úquantize_dynamic¶   s.    






rK   c             C   s   t | |d�} t| |dd� | S )N)r6   T)r7   r9   )r5   r<   r6   r   r   r   Úprepare_qatò   s    rL   c             C   s<   |st j| ƒ} | jƒ  t| dd� || |ƒ t| dd� | S )ah  Do quantization aware training and output a quantized model

    Args:
        model: input model
        run_fn: a function for evaluating the prepared model, can be a
                function that simply runs the prepared model or a training 
                loop
        run_args: positional arguments for `run_fn`

    Return:
        Quantized model.
    T)r6   )r/   r0   ZtrainrL   r9   )r5   r:   r;   r6   r   r   r   Úquantize_qat÷   s    

rM   c       	      C   s”   |dkrt }|stj| ƒ} i }tjtjtjtjf}x<| jƒ D ]0\}}t	|ƒ|kr^t
||dd� t||ƒ||< q<W x|jƒ D ]\}}|| j|< qzW | S )a¯  Converts the float module with observers (where we can get quantization
    parameters) to a quantized module.

    Args:
        module: calibrated module with observers
        mapping: a dictionary that maps from float module type to quantized module type, can 
                 be overwrritten to allow swapping user defined Modules
        inplace: carry out model transformations in-place, the original module is mutated
    NT)r6   )r   r/   r0   ÚnniZConvBn2dZConvBnReLU2dZ
LinearReLUZ
ConvReLU2dr   r   r9   Úswap_moduleÚitemsr*   )	r   r<   r6   ZreassignZSWAPPABLE_MODULESr   ÚmodÚkeyÚvaluer   r   r   r9     s     

r9   c             C   sF   | }t | dƒr| jdk	s$t| ƒtkrBt| ƒ|krB|t| ƒ j| ƒ}|S )a	  Swaps the module if it has a quantized counterpart and it has an
    `observer` attached.

    Args:
        mod: input module
        mapping: a dictionary that maps from nn module to nnq module

    Return:
        The corresponding quantized module of `mod`
    r   N)r'   r   r   r   Ú
from_float)rQ   r<   Únew_modr   r   r   rO   -  s
     rO   c             C   s^   dd„ }t | dƒr$| j|||ƒd < x4| jƒ D ](\}}|rF||ƒ| n|}t|||ƒ q.W dS )a,  Traverse the modules and save all observers into dict.
    This is mainly used for quantization accuracy debug
    Args:
        mod: the top module we want to save all observers
        prefix: the prefix for the current module
        target_dict: the dictionary used to save all the observers
    c             S   s   | dkr| S | d S )Nr   r   r   )r   r   r   r   Ú
get_prefixG  s    z%get_observer_dict.<locals>.get_prefixr    N)r'   r    r   Úget_observer_dict)rQ   Útarget_dictr   rV   r   r   r   r   r   r   rW   ?  s    
rW   )NNr   )N)NF)NF)F)NF)r   )*Ú
__future__r   r   r   r   r/   rI   r3   r>   Ztorch.nnr@   Ztorch.nn.intrinsicZ	intrinsicrN   Ztorch.nn.quantizedZ	quantizedr&   Zdefault_mappingsr   r   r	   r
   Zstubsr   r   ZQConfigr   r   r   r   r$   r(   r+   r7   r=   r?   rK   rL   rM   r9   rO   rW   r   r   r   r   Ú<module>   s2   
 
!


;

!