a
    Þñh  ã                   @   s  d dl mZ ddlT ddlT d dlm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dgƒƒZeeed	�Zeejejd�ejejd�d	�Zeeed	�Zeee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 )é    )Ú
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| ƒ | ||¡S )Nz”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__© úZ/home/yuhw/wc/uboone-dl-vtx/venv/lib/python3.9/site-packages/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ejjejjf‡ fdd„	Z‡  ZS )ÚQConfigDynamica×  
    Describes how to dynamically quantize a layer or a part of the network by providing
    settings (observer classes) 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| ƒ | ||¡S )Nz›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   8   s    zQConfigDynamic.__new__)	r   r   r   r   Útorchr   ÚIdentityr   r   r   r   r   r   r   (   s   r   )ZdtypeÚfbgemmc                 C   sD   | dkrt tjdd�td�}n"| dkr<t tjdd�td�}nt}|S )Nr   T)Úreduce_ranger   ÚqnnpackF)r   ZHistogramObserverÚ	with_argsÚ#default_per_channel_weight_observerÚdefault_weight_observerÚdefault_qconfig©ÚbackendZqconfigr   r   r   Úget_default_qconfigQ   s    ÿÿr&   c                 C   sP   | 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}|S )	Nr   r   éÿ   T)ÚobserverZ	quant_minZ	quant_maxr   r   r   F)r   ZFakeQuantizer    ZMovingAverageMinMaxObserverZ%default_per_channel_weight_fake_quantÚdefault_weight_fake_quantÚdefault_qat_qconfigr$   r   r   r   Úget_default_qat_qconfig\   s$    ýüýür+   )r   )r   )!Úcollectionsr   r(   Zfake_quantizeZtorch.nnr   r   Zdefault_observerr"   r#   Zdefault_debug_observerZdefault_debug_qconfigr!   Zdefault_per_channel_qconfigr   Zdefault_dynamic_quant_observerZdefault_dynamic_qconfigZPlaceholderObserverr    r   Úfloat16Zfloat16_dynamic_qconfigZper_channel_dynamic_qconfigZdefault_float_qparams_observerZfloat_qparams_dynamic_qconfigZdefault_fake_quantr)   r*   r   Zdefault_weight_only_qconfigZdefault_activation_only_qconfigr&   r+   r   r   r   r   Ú<module>   sJ   ÿÿÿÿÿÿÿÿÿÿ
