3
O4W^y  ã               @   sö   d dl mZmZmZmZ d dlmZ ddlT ddlT d dl	j
Z
G dd„ deddd	gƒƒZeeed
�Zeeed�Zeeed
�ZG dd„ dedd	gƒƒZeed�Zeejejd�d�Zeeed
�Zeej
jed
�Zeeej
jd
�Zddd„Z ddd„Z!dS )é    )Úabsolute_importÚdivisionÚprint_functionÚunicode_literals)Ú
namedtupleé   )Ú*Nc                   s    e Zd ZdZ‡ fdd„Z‡  ZS )ÚQConfiga  
    Describes how to quantize a layer or a part of the network by providing
    settings (observer classes) for activations and weights respectively.


    Note that QConfig needs to contain observer **classes** (like MinMaxObserver) or a callable that returns
    instances on invocation, not the concrete observer instances themselves.
    Quantization preparation function will instantiate observers multiple times for each of the layers.


    Observer classes have usually reasonable default arguments, but they can be overwritten with `with_args`
    method (that behaves like functools.partial):

      my_qconfig = QConfig(activation=MinMaxObserver.with_args(dtype=torch.qint8),
                           weight=default_observer.with_args(dtype=torch.qint8))
    c                s4   t |tjƒst |tjƒr tdƒ‚tt| ƒj| ||ƒS )NzHQConfig received observer instance, please pass observer class instead. zLUse MyObserver.with_args(x=1) to override arguments to constructor if neededz”QConfig received observer instance, please pass observer class instead. Use MyObserver.with_args(x=1) to override arguments to constructor if needed)Ú
isinstanceÚnnÚModuleÚ
ValueErrorÚsuperr	   Ú__new__)ÚclsÚ
activationÚweight)Ú	__class__© ú;/tmp/pip-build-e66j97b2/torch/torch/quantization/QConfig.pyr      s    zQConfig.__new__)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú__classcell__r   r   )r   r   r	      s   r	   r   r   )r   r   )r   r   c                   s    e Zd ZdZ‡ fdd„Z‡  ZS )ÚQConfigDynamicaÖ  
    Describes how to dynamically quantize a layer or a part of the network by providing
    settings (observer classe) for weights.

    It's like QConfig, but for dynamic quantization.

    Note that QConfigDynamic needs to contain observer **classes** (like MinMaxObserver) or a callable that returns
    instances on invocation, not the concrete observer instances themselves.
    Quantization function will instantiate observers multiple times for each of the layers.

    Observer classes have usually reasonable default arguments, but they can be overwritten with `with_args`
    method (that behaves like functools.partial):

      my_qconfig = QConfigDynamic(weight=default_observer.with_args(dtype=torch.qint8))
    c                s&   t |tjƒrtdƒ‚tt| ƒj| |ƒS )NzOQConfigDynamic received observer instance, please pass observer class instead. zLUse MyObserver.with_args(x=1) to override arguments to constructor if neededz›QConfigDynamic received observer instance, please pass observer class instead. Use MyObserver.with_args(x=1) to override arguments to constructor if needed)r
   r   r   r   r   r   r   )r   r   )r   r   r   r   9   s    zQConfigDynamic.__new__)r   r   r   r   r   r   r   r   )r   r   r   )   s   r   )r   )ZdtypeÚfbgemmc             C   sH   | dkrt tjdd�td�}n&| dkr<t tjdd�td�}ntdƒ‚|S )Nr   T)Úreduce_range)r   r   ÚqnnpackFz0Unknown backend, please specify qconfig manually)r	   ZHistogramObserverÚ	with_argsÚ#default_per_channel_weight_observerÚdefault_weight_observerr   )ÚbackendÚqconfigr   r   r   Úget_default_qconfigK   s    

r$   c             C   sT   | dkr$t tjtdddd�td�}n,| dkrHt tjtdddd�td�}ntd	ƒ‚|S )
Nr   r   éÿ   T)ÚobserverZ	quant_minZ	quant_maxr   )r   r   r   Fz0Unknown backend, please specify qconfig manually)r	   ZFakeQuantizer   ZMovingAverageMinMaxObserverZ%default_per_channel_weight_fake_quantÚdefault_weight_fake_quantr   )r"   r#   r   r   r   Úget_default_qat_qconfigV   s    

r(   )r   )r   )"Ú
__future__r   r   r   r   Úcollectionsr   r&   Zfake_quantizeZtorch.nnr   r	   Zdefault_observerr!   Zdefault_qconfigZdefault_debug_observerZdefault_debug_qconfigr    Zdefault_per_channel_qconfigr   Zdefault_dynamic_qconfigZNoopObserverr   ZtorchZfloat16Zfloat16_dynamic_qconfigZdefault_fake_quantr'   Zdefault_qat_qconfigZIdentityZ!default_weight_only_quant_qconfigZ%default_activation_only_quant_qconfigr$   r(   r   r   r   r   Ú<module>   s,   


