a
    –¥»aµ-  ã                   @   sô  d dl mZ d dlmZmZmZ d dl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mZmZmZmZmZmZmZ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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-eeed�Z.ddd„Z/ddd„Z0eeee!f  ejj1ddœdd„Z2eee!df Z3e3eej1df edœdd„Z4e3e3dœdd„Z5dS ) é    )Ú
namedtuple)ÚUnionÚOptionalÚAnyN)ÚFakeQuantizeÚdefault_fake_quantÚ%default_per_channel_weight_fake_quantÚdefault_weight_fake_quantÚdefault_fused_act_fake_quantÚdefault_fused_wt_fake_quantÚFusedMovingAvgObsFakeQuantizeÚ'default_fused_per_channel_wt_fake_quanté   )
ÚHistogramObserverÚMovingAverageMinMaxObserverÚPlaceholderObserverÚdefault_debug_observerÚdefault_dynamic_quant_observerÚdefault_float_qparams_observerÚdefault_observerÚ#default_per_channel_weight_observerÚdefault_placeholder_observerÚdefault_weight_observerc                       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__© úW/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/torch/ao/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   P   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 )Nr1   T)Úreduce_ranger-   ÚqnnpackF)r   r   Ú	with_argsr   r   Údefault_qconfig)ÚbackendÚqconfigr&   r&   r'   Úget_default_qconfigr   s    ÿÿr8   c                 C   s¬   |d u rT| dkr,t tjtdddd�td�}n(| dkrPt tjtdddd�td�}nt}|d	kr¨| dkr€t tjtdddd�td�}n(| dkr¤t tjtdddd�t	d�}nt
}|S )
Nr1   r   éÿ   T)ÚobserverZ	quant_minZ	quant_maxr2   r-   r3   Fr   )r   r   r4   r   r   r	   Údefault_qat_qconfigr   r   r   Údefault_qat_qconfig_v2)r6   Úversionr7   r&   r&   r'   Úget_default_qat_qconfig}   sJ    ýüýüýüýür>   )r7   ÚmodÚreturnc                 C   sn   | d u rd S t |tjjƒp4t |tjjƒp4t |tjjƒ}|rj|  ¡ }t |tjjƒp\t |tjj	ƒ}|rjJ dƒ‚d S )NzGPer channel weight observer is not supported yet for ConvTranspose{n}d.)
r   r/   r   ZConvTranspose1dZConvTranspose2dZConvTranspose3dr#   ZquantizationZPerChannelMinMaxObserverZ%MovingAveragePerChannelMinMaxObserver)r7   r?   Zis_conv_transpose_modZexample_observerZis_per_channelr&   r&   r'   Úassert_valid_qconfig    s    ÿýþÿrA   )r7   Úmoduler@   c                    sl   ˆdu s| du s| j dkr| S ‡fdd„‰ ‡ fdd„}|| jƒ}|| jƒ}t| tƒr^t||ƒS t||ƒS dS )ab  This is a helper function for use in quantization prepare that updates a qconfig so that
    the constructors stored in the qconfig will create observers on the same device that
    'module' is on. This is intended to be used when the qconfigs are propagated to each
    module in order to avoid potential device alignment issues.

    Args:
        qconfig: QConfig or QConfigDynamic with obs constructors stored in activation and weight
        module: module which the qconfig is related to

    Return:
        qconfig: configured so that obs constructors set to construct on the same device as module
    Nr-   c                     sf   t ˆ tjjƒsJ ‚dd„ ˆ  ¡ D ƒdd„ ˆ  ¡ D ƒB } t| ƒdkrNtt| ƒƒnd }|d u r^d S d|iS )Nc                 S   s   h | ]
}|j ’qS r&   ©Údevice©Ú.0Úpr&   r&   r'   Ú	<setcomp>Ê   ó    zcadd_module_to_qconfig_obs_ctr.<locals>.get_factory_kwargs_based_on_module_device.<locals>.<setcomp>c                 S   s   h | ]
}|j ’qS r&   rC   rE   r&   r&   r'   rH   Ë   rI   r   rD   )	r   r/   r   r   Ú
parametersÚbuffersÚlenÚnextÚiter)ZdevicesrD   )rB   r&   r'   Ú)get_factory_kwargs_based_on_module_deviceÈ   s    ÿzPadd_module_to_qconfig_obs_ctr.<locals>.get_factory_kwargs_based_on_module_devicec                    sP   z | j d d�}|ƒ  | jˆ d�W S  ty6   |  Y S  tyJ   |  Y S 0 d S )N)Zfactory_kwargs)r4   Zwith_callable_argsÚAttributeErrorÚ	TypeError)Zoriginal_constructorÚcheck)rO   r&   r'   Ú1configure_constructor_to_put_obs_on_module_deviceÏ   s    zXadd_module_to_qconfig_obs_ctr.<locals>.configure_constructor_to_put_obs_on_module_device)Ú_fieldsr"   r#   r   r   r.   )r7   rB   rS   r"   r#   r&   )rO   rB   r'   Úadd_module_to_qconfig_obs_ctrµ   s    



rU   )Úq1Úq2c                 C   sz   dd„ }| d u s|d u r | |kS | d ur0|d us4J ‚z&|| j j|j jƒoX|| jj|jjƒW S  tyt   | |k Y S 0 d S )Nc                 S   s,   | j |j k}|o| j|jk}|o*| j|jkS )N)ÚfuncÚargsÚkeywords)Úp1Úp2Zsamer&   r&   r'   Úpartial_equalså   s    z&qconfig_equals.<locals>.partial_equals)r"   rG   r#   rP   )rV   rW   r]   r&   r&   r'   Úqconfig_equalsã   s    &r^   )r1   )r1   r   )6Úcollectionsr   Útypingr   r   r   r/   Ztorch.nnr   Z#torch.ao.quantization.fake_quantizer   r   r   r	   r
   r   r   r   r:   r   r   r   r   r   r   r   r   r   r   r   r5   Zdefault_debug_qconfigZdefault_per_channel_qconfigr.   Zdefault_dynamic_qconfigr4   Úfloat32Úfloat16Zfloat16_dynamic_qconfigZfloat16_static_qconfigZper_channel_dynamic_qconfigZ!float_qparams_weight_only_qconfigr;   r0   Zdefault_weight_only_qconfigZdefault_activation_only_qconfigr<   r8   r>   r   rA   Z
QConfigAnyrU   r^   r&   r&   r&   r'   Ú<module>   sl   (0ÿÿÿÿÿÿÿþÿÿÿ

#ÿÿþ.