U
    <ºcc  ã                   @   s’   d dl Z d dlZ d dlZ d dlm  mZ d dlm  m  m	Z
 d dlmZ e
jjjZG dd„ de
jƒZG dd„ de
jƒZG dd„ de
jƒZdS )	é    N)Úfuse_conv_bn_weightsc                       s\   e Zd ZdZejjjZd‡ fdd„	Z	d	d
„ Z
dd„ Ze‡ fdd„ƒZe‡ fdd„ƒZ‡  ZS )Ú
ConvReLU1dzÌ
    A ConvReLU1d module is a fused module of Conv1d and ReLU

    We adopt the same interface as :class:`torch.ao.nn.quantized.Conv1d`.

    Attributes:
        Same as torch.ao.nn.quantized.Conv1d

    é   r   TÚzerosNc                    s*   t t| ƒj|||||||||	|
|d d S ©N©ÚstrideÚpaddingÚdilationÚgroupsÚbiasÚpadding_modeÚdeviceÚdtype)Úsuperr   Ú__init__©ÚselfZin_channelsZout_channelsZkernel_sizer   r	   r
   r   r   r   r   r   ©Ú	__class__© úR/tmp/pip-unpacked-wheel-gikjz4vx/torch/nn/intrinsic/quantized/modules/conv_relu.pyr      s    
        ýzConvReLU1d.__init__c                 C   s^   t |jƒdkrtdƒ‚| jdkrDt| jd d… ƒ}tj||| jd}tj	j
 || j| j| j¡S )Né   z Input shape must be `(N, C, L)`!r   r   ©Úmode)ÚlenÚshapeÚ
ValueErrorr   Ú_reverse_repeat_paddingr	   ÚFÚpadÚtorchÚopsÚ	quantizedZconv1d_reluÚ_packed_paramsÚscaleÚ
zero_point©r   ÚinputZ _reversed_padding_repeated_twicer   r   r   Úforward!   s    
ÿ   ÿzConvReLU1d.forwardc                 C   s   dS )NZQuantizedConvReLU1dr   ©r   r   r   r   Ú	_get_name.   s    zConvReLU1d._get_namec                    sX   t |ƒtjjjjkrHt|j|j|j	j
|j	j|j	j|j	j|j	jƒ\|_|_tt| ƒ |¡S ©N)Útyper!   ÚnnÚ	intrinsicÚqatÚConvBnReLU1dr   Úweightr   ÚbnÚrunning_meanÚrunning_varÚepsr   r   Ú
from_float©ÚclsÚmodr   r   r   r7   1   s         þzConvReLU1d.from_floatc                    s.   t |ƒtjjjkstdƒ‚tƒ  |d ||¡S )NzMBatchNorm1d should be fused into Conv1d before converting to reference moduler   )r-   r!   r.   r/   r1   ÚAssertionErrorr   Úfrom_reference©r9   Z	ref_qconvZoutput_scaleZoutput_zero_pointr   r   r   r<   9   s    ÿzConvReLU1d.from_reference)r   r   r   r   Tr   NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r!   r.   r/   r   Ú_FLOAT_MODULEr   r)   r+   Úclassmethodr7   r<   Ú__classcell__r   r   r   r   r      s"   	
             þr   c                       s\   e Zd ZdZejjjZd‡ fdd„	Z	d	d
„ Z
dd„ Ze‡ fdd„ƒZe‡ fdd„ƒZ‡  ZS )Ú
ConvReLU2dzÌ
    A ConvReLU2d module is a fused module of Conv2d and ReLU

    We adopt the same interface as :class:`torch.ao.nn.quantized.Conv2d`.

    Attributes:
        Same as torch.ao.nn.quantized.Conv2d

    r   r   Tr   Nc                    s*   t t| ƒj|||||||||	|
|d d S r   )r   rE   r   r   r   r   r   r   K   s    
        ýzConvReLU2d.__init__c                 C   sV   t |jƒdkrtdƒ‚| jdkr<t| jƒ}tj||| jd}tj	j
 || j| j| j¡S )Né   z#Input shape must be `(N, C, H, W)`!r   r   )r   r   r   r   r   r	   r   r    r!   r"   r#   Zconv2d_relur$   r%   r&   r'   r   r   r   r)   S   s    

ÿ   ÿzConvReLU2d.forwardc                 C   s   dS )NZQuantizedConvReLU2dr   r*   r   r   r   r+   _   s    zConvReLU2d._get_namec                    sX   t |ƒtjjjjkrHt|j|j|j	j
|j	j|j	j|j	j|j	jƒ\|_|_tt| ƒ |¡S r,   )r-   r!   r.   r/   r0   ÚConvBnReLU2dr   r2   r   r3   r4   r5   r6   r   rE   r7   r8   r   r   r   r7   b   s         þzConvReLU2d.from_floatc                    s.   t |ƒtjjjkstdƒ‚tƒ  |d ||¡S )NzMBatchNorm2d should be fused into Conv2d before converting to reference moduler   )r-   r!   r.   r/   rG   r;   r   r<   r=   r   r   r   r<   j   s    ÿzConvReLU2d.from_reference)r   r   r   r   Tr   NN)r>   r?   r@   rA   r!   r.   r/   rE   rB   r   r)   r+   rC   r7   r<   rD   r   r   r   r   rE   ?   s"   	
             þrE   c                       s\   e Zd ZdZejjjZd‡ fdd„	Z	d	d
„ Z
dd„ Ze‡ fdd„ƒZe‡ fdd„ƒZ‡  ZS )Ú
ConvReLU3dzÄ
    A ConvReLU3d module is a fused module of Conv3d and ReLU

    We adopt the same interface as :class:`torch.ao.nn.quantized.Conv3d`.

    Attributes: Same as torch.ao.nn.quantized.Conv3d

    r   r   Tr   Nc                    s:   |	dkst dƒ‚tt| ƒj|||||||||	|
|d d S )NZreflectz*Conv3d does not support reflection paddingr   )r;   r   rH   r   r   r   r   r   r   |   s    
        ýzConvReLU3d.__init__c                 C   sV   t |jƒdkrtdƒ‚| jdkr<t| jƒ}tj||| jd}tj	j
 || j| j| j¡S )Né   z&Input shape must be `(N, C, D, H, W)`!r   r   )r   r   r   r   r   r	   r   r    r!   r"   r#   Zconv3d_relur$   r%   r&   r'   r   r   r   r)   …   s    

ÿ   ÿzConvReLU3d.forwardc                 C   s   dS )NZQuantizedConvReLU3dr   r*   r   r   r   r+   ‘   s    zConvReLU3d._get_namec                    sX   t |ƒtjjjjkrHt|j|j|j	j
|j	j|j	j|j	j|j	jƒ\|_|_tt| ƒ |¡S r,   )r-   r!   r.   r/   r0   ÚConvBnReLU3dr   r2   r   r3   r4   r5   r6   r   rH   r7   r8   r   r   r   r7   ”   s    ù	zConvReLU3d.from_floatc                    s.   t |ƒtjjjkstdƒ‚tƒ  |d ||¡S )NzMBatchNorm3d should be fused into Conv3d before converting to reference moduler   )r-   r!   r.   r/   rJ   r;   r   r<   r=   r   r   r   r<   ¢   s    ÿzConvReLU3d.from_reference)r   r   r   r   Tr   NN)r>   r?   r@   rA   r!   r.   r/   rH   rB   r   r)   r+   rC   r7   r<   rD   r   r   r   r   rH   q   s"   
             þ	rH   )r!   Ztorch.ao.nn.intrinsicZtorch.nn.intrinsic.qatZtorch.nn.functionalr.   Z
functionalr   Ztorch.ao.nn.quantizedZaor#   ZnnqZtorch.nn.utilsr   ÚmodulesÚconvr   ZConv1dr   ZConv2drE   ZConv3drH   r   r   r   r   Ú<module>   s   
22