U
    E‹Ñht  ã                
   @   sú  d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlm  m  m	Z
 d dlmZ d dlmZmZmZmZmZmZmZmZ ddlmZmZ d dlmZmZ d dlmZmZmZm Z m!Z!m"Z" d dl#m$Z$ d d	l%m&Z& d d	l%m&Z' d
dddddddddg
Z(ej)ej*j)ej+ej*j+iej*j)ej	j)ej*j+ej	j+idœZ,dd
„ Z-d6dd„Z.d7dd„Z/dd„ Z0dd„ Z1d8dd „Z2d9d!d"„Z3d#d$„ Z4d%d„ Z5d:d&d„Z6d'd(„ Z7d)d*„ Z8d;d+d„Z9dej:ddfd,d„Z;d<d-d„Z<d=d.d„Z=d>d0d„Z>d?d1d2„Z?d@d3d„Z@dAd4d5„ZAdS )Bé    N)Ú_FusedModule)Ú)get_default_dynamic_quant_module_mappingsÚ(get_default_static_quant_module_mappingsÚ2get_default_static_quant_reference_module_mappingsÚget_default_qat_module_mappingsÚ$get_default_qconfig_propagation_listÚno_observer_setÚ_has_special_act_post_processÚ_get_special_act_post_processé   )Úget_qparam_dictÚ)has_no_children_ignoring_parametrizations)ÚDeQuantStubÚQuantWrapper)Ú_add_module_to_qconfig_obs_ctrÚdefault_dynamic_qconfigÚfloat16_dynamic_qconfigÚ!float_qparams_weight_only_qconfigÚ&float_qparams_weight_only_qconfig_4bitÚ_activation_is_memoryless)Útype_before_parametrizations)Ú_is_activation_post_processÚget_default_custom_config_dictÚpropagate_qconfig_Úadd_quant_dequantÚprepareÚquantizeÚquantize_dynamicÚprepare_qatÚquantize_qatÚconvertÚswap_module)Ú%float_to_observed_custom_module_classÚ)observed_to_quantized_custom_module_classc                   C   s   t S )z,Defines the default custom config dict.
    )Ú_DEFAULT_CUSTOM_CONFIG_DICT© r%   r%   úL/tmp/user/2202/pip-unpacked-wheel-bf9o3bgk/torch/ao/quantization/quantize.pyr   >   s    Ú c           
      C   s®   |  t| ƒ|¡}|  ||¡}t| d|ƒ}tjjj || ¡ t|| ƒ}|| _|  	¡ D ]V\}}|rj|d | n|}	|dksš||  dg ¡ksRt
|ƒ|  dg ¡ksRt||||	ƒ qRdS )aò  This is a helper function for `propagate_qconfig_`

    Args:
        module: input module
        qconfig_dict: dictionary that maps from name of submodule to quantization
                     configuration
        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
        prepare_custom_config_dict: dictionary for custom handling of modules
                                    see docs for :func:`~torch.ao.quantization.prepare_fx`

    Return:
        None, module is modified inplace with qconfig attached
    ÚqconfigÚ.NZnon_traceable_module_nameZnon_traceable_module_class)Úgetr   ÚgetattrÚtorchÚaoZquantizationr(   Z_assert_valid_qconfigr   Únamed_childrenÚtypeÚ_propagate_qconfig_helper)
ÚmoduleÚqconfig_dictZqconfig_parentÚprefixÚprepare_custom_config_dictZmodule_qconfigZqconfig_with_device_checkÚnameÚchildÚmodule_prefixr%   r%   r&   r0   C   s&    
ÿþ   ÿr0   c                 C   s*   |dkri }|dkri }t | ||d� 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)
        prepare_custom_config_dict: dictionary for custom handling of modules
            see docs for :func:`~torch.ao.quantization.prepare_fx`

    Return:
        None, module is modified inplace with qconfig attached
    N)r4   )r0   )r1   r2   r4   r%   r%   r&   r   k   s
    c                 C   s
   |   |¡S )z3Forward hook that calls observer on the output
    ©Úactivation_post_process)ÚselfÚinputÚoutputr%   r%   r&   Ú_observer_forward_hook�   s    r=   c                 C   s   |   |d ¡S )z7Forward pre hook that calls observer on the output
    r   r8   )r:   r;   r%   r%   r&   Ú_observer_forward_pre_hook†   s    r>   Fc                 C   s8   t | dƒstdƒ‚|r&| jtdd�}n| jtdd�}d S )Nr9   zGExpect activation_post_process attribute already attached to the moduleT)Úprepend)ÚhasattrÚAssertionErrorÚregister_forward_pre_hookr>   Úregister_forward_hookr=   )r1   Úpre_hookÚhandler%   r%   r&   Ú&_register_activation_post_process_hook‹   s    ÿ ÿ ÿrF   c                    s,  |dkrt ƒ }|dkri }ˆ dkr`t| ƒ}t|ƒdksDtd|› �ƒ‚t|ƒdkr\tt|ƒƒnd‰ ddd„‰dd„ ‰d‡ ‡‡fd	d
„	}|  ¡ D �]6\}}t|ƒtj	fkrªqŒqŒt
t|ƒtjtjfƒröˆ|ƒrôt|dƒsætdt|ƒ› d�ƒ‚ˆ|jˆ ƒ|_qŒt|tƒ�rˆ|ƒ�rÄ||ƒ qŒ|dk	�rBt|ƒ|k�rBˆ|ƒ�rÄ||ƒ qŒt|ƒ�r`t|ƒ}	|||	ƒ qŒˆ|ƒ�r´t|ƒ|k�r´|t|ƒ  |¡}
t| ||
ƒ |t|ƒ tƒ k�rÄ||
ƒ qŒt|||ˆ |ƒ qŒt| ƒ�röt| tjjƒ�söt| ƒ|k�rö|| ƒ t| dƒ�r(t| tjjƒ�s(t| ƒ|k�r(|| ƒ dS )as  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
        qconfig_propagation_list: a list of quantizable modules that will have observers added to them
            if they are leaf nodes
        device: parent device, if any
        non_leaf_module_list: list of non-leaf modules we want to add observer

    Return:
        None, module is modified inplace with added observer modules and forward_hooks
    Nr   zR_add_observer_ only works with cpu or single-device CUDA modules, but got devices r   c                 S   s,   |d kr|   ¡ n|ƒ }|d k	r(| |¡ |S ©N)Ú
activationÚto)r(   ÚdeviceÚspecial_act_post_processrH   r%   r%   r&   Úget_activation_post_process¶   s    
z3_add_observer_.<locals>.get_activation_post_processc                 S   s   t | dƒo| jd k	S )Nr(   ©r@   r(   )Úmr%   r%   r&   Úneeds_observation¼   s    z)_add_observer_.<locals>.needs_observationc                    s>   ˆ| ƒr:t | tƒs:|  dˆ| jˆ |ƒ¡ t| t| jƒd� dS )zn Adds an activation post process module and register
        a pre or post hook that calls the module
        r9   ©rD   N)Ú
isinstancer   Z
add_moduler(   rF   r   )rN   rK   ©rJ   rL   rO   r%   r&   Úinsert_activation_post_process¿   s      ÿz6_add_observer_.<locals>.insert_activation_post_processr9   zfunctional class z- has no pre-defined `activation_post_process`Zweight_fake_quant)N)N)r   Ú_get_unique_devices_ÚlenrA   ÚnextÚiterr.   r   ÚnnZDropoutÚ
issubclassÚnnqZFloatFunctionalZQFunctionalr@   r(   r9   rQ   r   r	   r
   Ú
from_floatÚsetattrr   Ú_add_observer_r   r,   Z
Sequential)r1   Úqconfig_propagation_listZnon_leaf_module_listrJ   Úcustom_module_class_mappingÚdevicesrS   r5   r6   rK   Zobserved_childr%   rR   r&   r]   ˜   sZ    ÿ
ÿ






ÿ
ÿr]   c                 C   s$   dd„ |   ¡ D ƒdd„ |  ¡ D ƒB S )Nc                 S   s   h | ]
}|j ’qS r%   ©rJ   ©Ú.0Úpr%   r%   r&   Ú	<setcomp>ø   s     z'_get_unique_devices_.<locals>.<setcomp>c                 S   s   h | ]
}|j ’qS r%   ra   rb   r%   r%   r&   re   ù   s     )Ú
parametersÚbuffers©r1   r%   r%   r&   rT   ÷   s    ÿrT   c                 C   sD   t | ƒr t| dƒr | jr t| ƒS |  ¡ D ]\}}t|ƒ| j|< q(| 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   Ú_modules)r1   r5   r6   r%   r%   r&   r   û   s
    c                 C   s†   t j d¡ |dkrtƒ }| di ¡}|s4t | ¡} |}|dkrFtƒ }t| dd� t	dd„ |  
¡ D ƒƒsrt d¡ t| |||d� | S )	aƒ  Prepares a copy of the model for quantization calibration or quantization-aware training.

    Quantization configuration should be 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
        `inplace`: carry out model transformations in-place, the original module is mutated
        `allow_list`: list of quantizable modules
        `observer_non_leaf_module_list`: list of non-leaf modules we want to add observer
        `prepare_custom_config_dict`: customization configuration dictionary for prepare function

    .. code-block:: python

       # Example of prepare_custom_config_dict:
       prepare_custom_config_dict = {
           # user will manually define the corresponding observed
           # module class which has a from_float class method that converts
           # float custom module to observed custom module
           "float_to_observed_custom_module_class": {
               CustomModule: ObservedCustomModule
           }
        }

    z!quantization_api.quantize.prepareNr"   ©r2   c                 s   s   | ]}t |d ƒo|jV  qdS )r(   NrM   )rc   rN   r%   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)r_   )r,   Ú_CÚ_log_api_usage_oncer   r*   ÚcopyÚdeepcopyr   r   ÚanyÚmodulesÚwarningsÚwarnr]   )ÚmodelÚinplaceZ
allow_listÚobserver_non_leaf_module_listr4   r_   r^   r%   r%   r&   r     s&    

  þc                    sD   t ˆ dƒrtˆ jƒrtˆ dƒ d‡ fdd„	}|dd� |dd� d S )Nr9   Fc                    s^   | r
ˆ j nˆ j}| rtnt}tƒ }| ¡ D ]\}}||kr*| |¡ q*|D ]}| |¡ qJd S rG   )Ú_forward_pre_hooksÚ_forward_hooksr>   r=   ÚsetÚitemsÚaddÚpop)rD   Zhook_mapZobserver_hookZhandle_ids_to_removeZ	handle_idÚhook_fnrh   r%   r&   Úremove_hooksQ  s    z5_remove_activation_post_process.<locals>.remove_hooksTrP   )F)r@   r   r9   Údelattr)r1   r~   r%   rh   r&   Ú_remove_activation_post_processI  s    
ÿ


r€   c                 C   s0   |   ¡ D ]}t|ƒ qt| dƒr$| `t| ƒ dS )zŠClean up the qconfig left in the module so that new qconfig can be
    propagated.

    Args:
        module: module to be cleaned up
    r(   N)ÚchildrenÚ_remove_qconfigr@   r(   r€   )r1   r6   r%   r%   r&   r‚   _  s
    

r‚   c                 C   s\   t j d¡ |dkrtƒ }|s(t | ¡} |  ¡  t| dd� || f|žŽ  t| |dd� | S )aƒ  Quantize the input float model with post training static quantization.

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

    Args:
        model: input float model
        run_fn: a calibration function for calibrating the prepared model
        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.
    z"quantization_api.quantize.quantizeNT©ru   )	r,   rl   rm   r   rn   ro   Úevalr   r    )rt   Úrun_fnÚrun_argsÚmappingru   r%   r%   r&   r   n  s    
c                 C   st  t j d¡ |dkrÀ|t jkrHtjttjttjttj	ttj
ttjti}nv|t jkr|tjttjttjttj	ttj
ttjti}nB|t jkr˜tjttjti}n&|t jkr®tjti}ntd|› d�ƒ‚npt|tƒ�r0|t jkrÜt}n@|t jkrìt}n0|t jkrüt}n |t jk�rt}ntdt|ƒƒ‚tt|t |¡ƒƒ}|dk�r@tƒ }|�sPt | ¡} |   ¡  t!| |ƒ t"| |dd� | S )av  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:
        model: 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 QConfig instances.

            - A set of types and/or submodule names to apply dynamic quantization to,
              in which case the `dtype` argument is used to specify 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

    z*quantization_api.quantize.quantize_dynamicNz5Don't know how to quantize with default settings for z. Provide full qconfig pleasez.Unknown dtype specified for quantize_dynamic: Trƒ   )#r,   rl   rm   Úqint8rX   ZLinearr   ÚLSTMZGRUZLSTMCellZRNNCellZGRUCellZfloat16r   Zquint8ZEmbeddingBagr   Z	EmbeddingZquint4x2r   Ú
ValueErrorrQ   ry   ÚRuntimeErrorÚstrÚdictÚzipÚ	itertoolsÚrepeatr   rn   ro   r„   r   r    )rt   Zqconfig_specZdtyper‡   ru   Zdefault_qconfigr%   r%   r&   r   Š  s|    
      ú
      ú
  þ
 ÿ
ÿ





c                 C   sl   t j d¡ | jstdƒ‚|dkr(tƒ }|s6t | ¡} t| dd� t	| |ddd� t
| t| ¡ ƒdd� | S )	a  
    Prepares a copy of the model for quantization calibration or
    quantization-aware training and converts it to quantized version.

    Quantization configuration should be assigned preemptively
    to individual submodules in `.qconfig` attribute.

    Args:
        model: input model to be modified in-place
        mapping: dictionary that maps float modules to quantized modules to be
                 replaced.
        inplace: carry out model transformations in-place, the original module
                 is mutated
    z%quantization_api.quantize.prepare_qatz1prepare_qat only works on models in training modeNrj   TF)r‡   ru   Úremove_qconfig)rv   ru   )r,   rl   rm   ZtrainingrA   r   rn   ro   r   r    r   ry   Úvalues)rt   r‡   ru   r%   r%   r&   r   ß  s    
c                 C   sL   t j d¡ |st | ¡} |  ¡  t| dd� || f|žŽ  t| dd� | S )ag  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.
    z&quantization_api.quantize.quantize_qatTrƒ   )r,   rl   rm   rn   ro   Ztrainr   r    )rt   r…   r†   ru   r%   r%   r&   r   û  s    
Tc                 C   s>   t j d¡ |st | ¡} t| |d|||d� |r:t| ƒ | S )a¼  Converts submodules in input module to a different module according to `mapping`
    by calling `from_float` method on the target module class. And remove qconfig at the
    end if remove_qconfig is set to True.

    Args:
        `module`: prepared and calibrated module
        `mapping`: a dictionary that maps from source module type to target
                   module type, can be overwritten to allow swapping user defined
                   Modules
        `inplace`: carry out model transformations in-place, the original module
                   is mutated
        `convert_custom_config_dict`: custom configuration dictionary for convert function
        `use_precomputed_fake_quant`: a flag to enable use of precomputed fake quant

    .. code-block:: python

       # Example of convert_custom_config_dict:
       convert_custom_config_dict = {
           # user will manually define the corresponding quantized
           # module class which has a from_observed class method that converts
           # observed custom module to quantized custom module
           "observed_to_quantized_custom_module_class": {
               ObservedCustomModule: QuantizedCustomModule
           }
       }

    z!quantization_api.quantize.convertT)ru   Úis_referenceÚconvert_custom_config_dictÚuse_precomputed_fake_quant)r,   rl   rm   rn   ro   Ú_convertr‚   )r1   r‡   ru   r‘   r“   r”   r•   r%   r%   r&   r      s    
   ýc              	   C   s²   |dkr|rt ƒ ntƒ }|dkr&tƒ }| di ¡}|s@t | ¡} i }|  ¡ D ]D\}}	t|	tƒs~t	|	ƒ|kr~t
|	|d|||d� t|	|||ƒ||< qL| ¡ D ]\}
}|| j|
< qš| S )ao  Converts submodules in input module to a different module according to `mapping`
    by calling `from_float` method on the target module class

    Args:
        module: input module
        mapping: a dictionary that maps from source module type to target
                 module type, can be overwritten to allow swapping user defined
                 Modules
        inplace: carry out model transformations in-place, the original module
                 is mutated
        is_reference: a flag to enable quantized reference module
        use_precomputed_fake_quant: a flag to enable use of precomputed fake quant

    Nr#   T©r•   )r   r   r   r*   rn   ro   r.   rQ   r   r   r–   r!   rz   ri   )r1   r‡   ru   r“   r”   r•   r_   Zreassignr5   ÚmodÚkeyÚvaluer%   r%   r&   r–   ;  s.    
ÿ


ÿ þr–   c                 C   st  | }t | dƒ�rp| jdk	�rpd}t| ƒ|krD|t| ƒ  | ¡}d}n”t| ƒ|krØ|t| ƒ }t |dƒr¤|jr¤| jdk	szt‚| j ¡ }|| jƒ t|ƒ}| | |¡}n0t	 
|j¡}	d|	jkrÊ|j| |d�}n
| | ¡}d}|�rp| j ¡ D ]}
| |
¡ qè| j ¡ D ]}|tk	�r| |¡ �qt| ƒ}t|ƒdk�sBtd	|› �ƒ‚t|ƒd
k�r\tt|ƒƒnd}|�rp| |¡ |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(   NFTÚ_IS_REFERENCEr•   r—   r   zOswap_module only works with cpu or single-device CUDA modules, but got devices r   )r@   r(   r   Zfrom_observedr›   rA   Zweightr   r[   ÚinspectÚ	signaturerf   rw   r’   rB   rx   r=   rC   rT   rU   rV   rW   rI   )r˜   r‡   r_   r•   Únew_modZswappedZqmodZweight_post_processZweight_qparamsÚsigZpre_hook_fnr}   r`   rJ   r%   r%   r&   r!   f  sB    




ÿ
c                 C   sZ   dd„ }t | dƒr$| j|||ƒd < |  ¡ D ](\}}|rD||ƒ| n|}t|||ƒ q,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%   )r3   r%   r%   r&   Ú
get_prefix£  s    z&_get_observer_dict.<locals>.get_prefixr9   N)r@   r9   r.   Ú_get_observer_dict)r˜   Útarget_dictr3   r    r5   r6   r7   r%   r%   r&   r¡   ›  s    
r¡   )Nr'   N)NN)F)NNNN)FNNN)NF)NF)F)NFTFNF)NFFNF)F)r'   )Brn   r�   rr   rœ   r,   Ztorch.nnrX   Ztorch.ao.nn.quantizedr-   Z	quantizedrZ   Ztorch.ao.nn.intrinsicr   Z+torch.ao.quantization.quantization_mappingsr   r   r   r   r   r   r	   r
   Úutilsr   r   Ztorch.ao.quantization.stubsr   r   Ztorch.ao.quantization.qconfigr   r   r   r   r   r   Ztorch.nn.utils.parametrizer   Ztorch.ao.quantization.observerr   Zis_activation_post_processÚ__all__r‰   ZquantizableZMultiheadAttentionr$   r   r0   r   r=   r>   rF   r]   rT   r   r   r€   r‚   r   rˆ   r   r   r   r    r–   r!   r¡   r%   r%   r%   r&   Ú<module>   s˜   (
 ö  þ  þû     ÿ
(


_   þ
8
 ÿ
U

         ý
+       ý
+
5