U
    Kc                     @   sV   d dl Z d dlm  mZ G dd de jjjjZG dd deZG dd deZ	dS )    Nc                       s<   e Zd Zddd fddZedd Zed	d
 Z  ZS )
_BatchNormh㈵>皙?Nreturnc                    sN   ||d}t  j|||ddf| | dtjd| | dtjd| d S )	NdevicedtypeTscale      ?
zero_pointr   )r   )r   )super__init__Zregister_buffertorchZtensorselfnum_featuresepsmomentumr   r	   Zfactory_kwargs	__class__ K/tmp/pip-unpacked-wheel-gikjz4vx/torch/ao/nn/quantized/modules/batchnorm.pyr      s    
z_BatchNorm.__init__c                 C   sf   |j }t|| jkr|d }| \}}| |j|j}|j|_|j|_|j|_|j	|_	||_
||_|S )Nr   )activation_post_processtype_NNI_BN_RELU_MODULEZcalculate_qparamsr   r   weightbiasrunning_meanrunning_varr
   r   )clsmodr   r
   r   new_modr   r   r   
from_float   s    z_BatchNorm.from_floatc                 C   sP   | |j |j|j|jj|jjd}|j|_|j|_|j|_|j|_||_	||_
|S Nr   )r   r   r   r   r   r	   r   r   r   r
   r   )r    ZbnZoutput_scaleZoutput_zero_pointZqbnr   r   r   from_reference   s    z_BatchNorm.from_reference)r   r   NN)	__name__
__module____qualname__r   staticmethodr#   classmethodr%   __classcell__r   r   r   r   r      s
   
r   c                       s^   e Zd ZdZejZddd fddZdd	 Zd
d Z	e
je
jdddZedd Z  ZS )BatchNorm2dzEThis is the quantized version of :class:`~torch.nn.BatchNorm2d`.
    r   r   Nr   c                    s"   ||d}t  j|||f| d S r$   r   r   r   r   r   r   r   1   s    
zBatchNorm2d.__init__c                 C   s   dS )NZQuantizedBatchNorm2dr   r   r   r   r   	_get_name5   s    zBatchNorm2d._get_namec                 C   s   t |jdkrtdd S )N   #Input shape must be `(N, C, H, W)`!lenshape
ValueErrorr   inputr   r   r   _check_input_dim8   s    zBatchNorm2d._check_input_dimr7   r   c              
   C   s*   t jj|| j| j| j| j| j| j	| j
S N)r   ops	quantizedZbatch_norm2dr   r   r   r   r   r
   r   r6   r   r   r   forward>   s          zBatchNorm2d.forwardc                 C   s   t | |S r:   r   r#   r    r!   r   r   r   r#   E   s    zBatchNorm2d.from_float)r   r   NN)r&   r'   r(   __doc__nniZBNReLU2dr   r   r/   r8   r   Tensorr=   r*   r#   r+   r   r   r   r   r,   +   s   r,   c                       sX   e Zd ZdZejZd fdd	Zdd Zd	d
 Z	e
je
jdddZedd Z  ZS )BatchNorm3dzEThis is the quantized version of :class:`~torch.nn.BatchNorm3d`.
    r   r   Nc                    s"   ||d}t  j|||f| d S r$   r-   r   r   r   r   r   O   s    
zBatchNorm3d.__init__c                 C   s   dS )NZQuantizedBatchNorm3dr   r.   r   r   r   r/   S   s    zBatchNorm3d._get_namec                 C   s   t |jdkrtdd S )N   r1   r2   r6   r   r   r   r8   V   s    zBatchNorm3d._check_input_dimr9   c              
   C   s*   t jj|| j| j| j| j| j| j	| j
S r:   )r   r;   r<   Zbatch_norm3dr   r   r   r   r   r
   r   r6   r   r   r   r=   \   s          zBatchNorm3d.forwardc                 C   s   t | |S r:   r>   r?   r   r   r   r#   c   s    zBatchNorm3d.from_float)r   r   NN)r&   r'   r(   r@   rA   ZBNReLU3dr   r   r/   r8   r   rB   r=   r*   r#   r+   r   r   r   r   rC   I   s   rC   )
r   Ztorch.nn.intrinsicnnZ	intrinsicrA   modulesZ	batchnormr   r,   rC   r   r   r   r   <module>   s   '