o
    Zh%                     @   s   d dl mZmZ d dlZd dlmZ d dlmZ d dlm	Z	m
Z
mZ d dlmZmZmZ g dZG dd dejjjZG d	d
 d
eejZG dd deejZG dd deejZdS )    )ClassVarUnionN)_FusedModule)	_size_1_t	_size_2_t	_size_3_t)_pair_single_triple)Conv1dConv2dConv3dc                   @   s   e Zd ZU eeejjj  e	d< 			dde
de
dee
df dee
df deeee
df f d	ee
df d
edee
df de
dededdfddZdd ZedddZdd ZdS )_ConvNd_FLOAT_MODULENin_channelsout_channelskernel_size.stridepaddingdilation
transposedoutput_paddinggroupsbiaspadding_modereturnc                 C   s\   ||d}t jjjj| |||||||||	|
|fi | |s"J d|| _|j|d| _d S )N)devicedtypez'qconfig must be provided for QAT module)factory_kwargs)nnmodulesconvr   __init__qconfigweightweight_fake_quant)selfr   r   r   r   r   r   r   r   r   r   r   r#   r   r   r    r'   K/var/www/auris/lib/python3.10/site-packages/torch/ao/nn/qat/modules/conv.pyr"      s(   

z_ConvNd.__init__c                 C      |  || | j| jS NZ_conv_forwardr%   r$   r   r&   inputr'   r'   r(   forward6      z_ConvNd.forwardFc                 C   s   t || jksJ d| j d | jj t|dsJ d|js$J dtt |tr/|d }|j}| |j|j|j	|j
|j|j|j|jdu|j|d
}|j|_|j|_|S )	zCreate a qat module from a float module

        Args:
           `mod`: a float module, either produced by torch.ao.quantization utilities
           or directly from user
        zqat.z.from_float only works for r#   z,Input float module must have qconfig definedz,Input float module must have a valid qconfigr   N)r   r   r   r   r   r   r#   )typer   __name__hasattrr#   
issubclassr   r   r   r   r   r   r   r   r   r   r$   )clsmoduse_precomputed_fake_quantr#   Zqat_convr'   r'   r(   
from_float9   s:   z_ConvNd.from_floatc                 C   s   t | }|| j| j| j| j| j| j| j| j	du| j
	}tj| j |_| j	dur5tj| j	 |_	t|trZ|g}t|dsDJ | }|| |j| }|| j |S |S )zThis works for both single qat conv, and the qat conv - relu modules
        to convert the qat module to a floating point module
        N_FLOAT_RELU_MODULE)r0   _FLOAT_CONV_MODULEr   r   r   r   r   r   r   r   r   torchr   	Parameterr$   detachr3   r   r2   r8   appendr   trainZtraining)r&   r4   r!   r    ZreluZfusedr'   r'   r(   to_float\   s0   



z_ConvNd.to_float)NNNF)r1   
__module____qualname__r   r0   r   r    r!   r   __annotations__inttupler   strboolr"   r.   staticmethodr7   r?   r'   r'   r'   r(   r      sD   
 



	

%"r   c                       s   e Zd ZU dZejZeeej  e	d< ejZ
eeej  e	d< 									dd	ed
edededeeef dededededdf fddZed fdd	Z  ZS )r   aZ  
    A Conv1d module attached with FakeQuantize modules for weight,
    used for quantization aware training.

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

    Similar to :class:`~torch.nn.Conv2d`, with FakeQuantize modules initialized to
    default.

    Attributes:
        weight_fake_quant: fake quant module for weight
    r   r9      r   TzerosNr   r   r   r   r   r   r   r   r   r   c                    ^   t |}t |}t|tr|nt |}t |}t j||||||dt d|||	|
||d d S NFr   )r   r   r   r   r   r   r   r   r#   r   r   )r	   
isinstancerF   superr"   r&   r   r   r   r   r   r   r   r   r   r#   r   r   Zkernel_size_Zstride_Zpadding_Z	dilation_	__class__r'   r(   r"      (   
zConv1d.__init__Fc                       t  j| ||dS N)r6   rN   r7   r4   r5   r6   rP   r'   r(   r7         zConv1d.from_float	rI   r   rI   rI   TrJ   NNNr@   )r1   rA   rB   __doc__r   r   r   r   r0   rC   r9   rD   r   r   rF   rG   r"   classmethodr7   __classcell__r'   r'   rP   r(   r   |   sH   
 
	
$r   c                          e Zd ZU dZejZeeej  e	d< ejZ
eeej  e	d< 									dd	ed
edededeeef dededededdf fddZdd Zed fdd	Z  ZS )r   a  
    A Conv2d module attached with FakeQuantize modules for weight,
    used for quantization aware training.

    We adopt the same interface as `torch.nn.Conv2d`, please see
    https://pytorch.org/docs/stable/nn.html?highlight=conv2d#torch.nn.Conv2d
    for documentation.

    Similar to `torch.nn.Conv2d`, with FakeQuantize modules initialized to
    default.

    Attributes:
        weight_fake_quant: fake quant module for weight
    r   r9   rI   r   TrJ   Nr   r   r   r   r   r   r   r   r   r   c                    rK   rL   )r   rM   rF   rN   r"   rO   rP   r'   r(   r"      rR   zConv2d.__init__c                 C   r)   r*   r+   r,   r'   r'   r(   r.      r/   zConv2d.forwardFc                    rS   rT   rU   rV   rP   r'   r(   r7      rW   zConv2d.from_floatrX   r@   )r1   rA   rB   rY   r   r   r   r   r0   rC   r9   rD   r   r   rF   rG   r"   r.   rZ   r7   r[   r'   r'   rP   r(   r      J   
 
	
$r   c                       r\   )r   a  
    A Conv3d module attached with FakeQuantize modules for weight,
    used for quantization aware training.

    We adopt the same interface as `torch.nn.Conv3d`, please see
    https://pytorch.org/docs/stable/nn.html?highlight=conv3d#torch.nn.Conv3d
    for documentation.

    Similar to `torch.nn.Conv3d`, with FakeQuantize modules initialized to
    default.

    Attributes:
        weight_fake_quant: fake quant module for weight
    r   r9   rI   r   TrJ   Nr   r   r   r   r   r   r   r   r   r   c                    rK   rL   )r
   rM   rF   rN   r"   rO   rP   r'   r(   r"   	  rR   zConv3d.__init__c                 C   r)   r*   r+   r,   r'   r'   r(   r.   -  r/   zConv3d.forwardFc                    rS   rT   rU   rV   rP   r'   r(   r7   0  rW   zConv3d.from_floatrX   r@   )r1   rA   rB   rY   r   r   r   r   r0   rC   r9   rD   r   r   rF   rG   r"   r.   rZ   r7   r[   r'   r'   rP   r(   r      r]   r   )typingr   r   r:   Ztorch.nnr   Ztorch.ao.nn.intrinsicr   Ztorch.nn.common_typesr   r   r   Ztorch.nn.modules.utilsr   r	   r
   __all__r    r!   r   r   r   r   r'   r'   r'   r(   <module>   s   n;@