U
    Kºc?  ã                    @   sP  d dl 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 ddlmZmZ ddlmZmZmZmZmZ ddlmZmZmZ dd	lmZmZmZ dd
lm Z  ddl!m"Z"m#Z# ddl$m%Z% ddl&m'Z'm(Z(m)Z) G dd„ de j*j+ƒZ,G dd„ de j*j+ƒZ-dddddddddddddddd d!d"d#d$d%d&d'dd(d)d*d+d,d-d.d/g Z.dS )0é    N)Ú	MaxPool2dé   )ÚReLU6Ú	HardswishÚELUÚ	LeakyReLUÚSigmoidÚSoftmaxÚMultiheadAttentionÚPReLU)ÚDropout)ÚBatchNorm2dÚBatchNorm3d)Ú	LayerNormÚ	GroupNormÚInstanceNorm1dÚInstanceNorm2dÚInstanceNorm3d)ÚConv1dÚConv2dÚConv3d)ÚConvTranspose1dÚConvTranspose2dÚConvTranspose3d)ÚLinear)Ú	EmbeddingÚEmbeddingBag)ÚLSTM)ÚFloatFunctionalÚFXFloatFunctionalÚQFunctionalc                       sT   e Zd ZU dZejed< ejed< d‡ fdd„	Zdd„ Ze	d	d
„ ƒZ
dd„ Z‡  ZS )ÚQuantizeaÈ  Quantizes an incoming tensor

    Args:
     `scale`: scale of the output Quantized Tensor
     `zero_point`: zero_point of output Quantized Tensor
     `dtype`: data type of output Quantized Tensor
     `factory_kwargs`: Dictionary of kwargs used for configuring initialization
         of internal buffers. Currently, `device` and `dtype` are supported.
         Example: `factory_kwargs={'device': 'cuda', 'dtype': torch.float64}`
         will initialize internal buffers as type `torch.float64` on the current CUDA device.
         Note that `dtype` only applies to floating-point buffers.

    Examples::
        >>> t = torch.tensor([[1., -1.], [1., -1.]])
        >>> scale, zero_point, dtype = 1.0, 2, torch.qint8
        >>> qm = Quantize(scale, zero_point, dtype)
        >>> # xdoctest: +SKIP
        >>> qt = qm(t)
        >>> print(qt)
        tensor([[ 1., -1.],
                [ 1., -1.]], size=(2, 2), dtype=torch.qint8, scale=1.0, zero_point=2)
    ÚscaleÚ
zero_pointNc              	      sl   t j |¡}tt| ƒ ¡  |  dt j|gf|Ž¡ |  dt j|gfdt jidd„ | 	¡ D ƒ—Ž¡ || _
d S )Nr"   r#   Údtypec                 S   s   i | ]\}}|d kr||“qS )r$   © )Ú.0ÚkÚvr%   r%   úJ/tmp/pip-unpacked-wheel-gikjz4vx/torch/ao/nn/quantized/modules/__init__.pyÚ
<dictcomp>;   s       z%Quantize.__init__.<locals>.<dictcomp>)ÚtorchÚnnÚfactory_kwargsÚsuperr!   Ú__init__Zregister_bufferZtensorÚlongÚitemsr$   )Úselfr"   r#   r$   r-   ©Ú	__class__r%   r)   r/   5   s    ÿÿzQuantize.__init__c                 C   s   t  |t| jƒt| jƒ| j¡S ©N)r+   Zquantize_per_tensorÚfloatr"   Úintr#   r$   )r2   ÚXr%   r%   r)   Úforward>   s     ÿzQuantize.forwardc                 C   s<   t | dƒst‚| j ¡ \}}t| ¡  ¡ | ¡  ¡ | jjƒS )NÚactivation_post_process)	ÚhasattrÚAssertionErrorr:   Zcalculate_qparamsr!   r6   Úitemr0   r$   )Úmodr"   r#   r%   r%   r)   Ú
from_floatB   s    zQuantize.from_floatc                 C   s   d  | j| j| j¡S )Nz!scale={}, zero_point={}, dtype={})Úformatr"   r#   r$   ©r2   r%   r%   r)   Ú
extra_reprH   s    zQuantize.extra_repr)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r+   ZTensorÚ__annotations__r/   r9   Ústaticmethodr?   rB   Ú__classcell__r%   r%   r3   r)   r!      s   


	
r!   c                       s4   e Zd ZdZ‡ fdd„Zdd„ Zedd„ ƒZ‡  ZS )Ú
DeQuantizeaÜ  Dequantizes an incoming tensor

    Examples::
        >>> input = torch.tensor([[1., -1.], [1., -1.]])
        >>> scale, zero_point, dtype = 1.0, 2, torch.qint8
        >>> qm = Quantize(scale, zero_point, dtype)
        >>> # xdoctest: +SKIP
        >>> quantized_input = qm(input)
        >>> dqm = DeQuantize()
        >>> dequantized = dqm(quantized_input)
        >>> print(dequantized)
        tensor([[ 1., -1.],
                [ 1., -1.]], dtype=torch.float32)
    c                    s   t t| ƒ ¡  d S r5   )r.   rJ   r/   rA   r3   r%   r)   r/   \   s    zDeQuantize.__init__c                 C   s   |  ¡ S r5   )Z
dequantize)r2   ZXqr%   r%   r)   r9   _   s    zDeQuantize.forwardc                 C   s   t ƒ S r5   )rJ   )r>   r%   r%   r)   r?   b   s    zDeQuantize.from_float)	rC   rD   rE   rF   r/   r9   rH   r?   rI   r%   r%   r3   r)   rJ   L   s
   rJ   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r
   r   r   r	   r   r   r   r   r    )/r+   Ztorch.ao.nn.quantizableZtorch.nn.modules.poolingr   Z
activationr   r   r   r   r   r	   r
   r   Zdropoutr   Z	batchnormr   r   Znormalizationr   r   r   r   r   Úconvr   r   r   r   r   r   Zlinearr   Zembedding_opsr   r   Zrnnr   Zfunctional_modulesr   r   r    r,   ÚModuler!   rJ   Ú__all__r%   r%   r%   r)   Ú<module>   s^   (2ß