3
O4W^­  ã               @   s~   d dl mZmZmZmZ d dlZd dlZd dljj	j
jZdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zefdd„Zdefdd„ZdS )é    )Úabsolute_importÚdivisionÚprint_functionÚunicode_literalsNc             C   s€   | j |j kstdƒ‚| j rl| jdks,tdƒ‚|j| jks@tdƒ‚|jsNtdƒ‚|js\tdƒ‚tjj	j
| |ƒS tjjj| |ƒS dS )aI  Given the conv and bn modules, fuses them and returns the fused module

    Args:
        conv: Module instance of type conv2d
        bn: Spatial BN instance that needs to be fused with the conv

    Examples::

        >>> m1 = nn.Conv2d(10, 20, 3)
        >>> b1 = nn.BatchNorm2d(20)
        >>> m2 = fuse_conv_bn(m1, b1)
    z:Conv and BN both must be in the same mode (train or eval).Nz2Only support fusing Conv2d that does not have biasz?Output channel of Conv2d must match num_features of BatchNorm2dz7Only support fusing BatchNorm2d with affine set to TruezGOnly support fusing BatchNorm2d with tracking_running_stats set to True)ÚtrainingÚAssertionErrorZbiasZnum_featuresZout_channelsZaffineZtrack_running_statsÚtorchÚnnÚ	intrinsicZConvBn2dÚutilsÚfuse_conv_bn_eval)ÚconvÚbn© r   ú@/tmp/pip-build-e66j97b2/torch/torch/quantization/fuse_modules.pyÚfuse_conv_bn   s    r   c             C   sh   | j |j   ko|j kn  s&tdƒ‚| j rJ|j s<tdƒ‚tj| ||ƒS tjtjjj	j
| |ƒ|ƒS dS )aI  Given the conv and bn modules, fuses them and returns the fused module

    Args:
        conv: Module instance of type conv2d
        bn: Spatial BN instance that needs to be fused with the conv

    Examples::

        >>> m1 = nn.Conv2d(10, 20, 3)
        >>> b1 = nn.BatchNorm2d(20)
        >>> m2 = fuse_conv_bn(m1, b1)
    z:Conv and BN both must be in the same mode (train or eval).z+We only support fusion of non-inplace ReLU.N)r   r   ÚinplaceÚtorch_fusedZConvBnReLU2dÚ
ConvReLU2dr   r	   r   Zfusionr   )r   r   Zrelur   r   r   Úfuse_conv_bn_relu!   s     r   c             C   s*   |j dƒ}| }x|D ]}t||ƒ}qW |S )NÚ.)ÚsplitÚgetattr)ÚmodelÚsubmodule_keyÚtokensÚcur_modÚsr   r   r   Ú_get_module9   s
    

r   c             C   sF   |j dƒ}|d d… }| }x|D ]}t||ƒ}q W t||d |ƒ d S )Nr   é   éÿÿÿÿr    )r   r   Úsetattr)r   r   Úmoduler   Z
sub_tokensr   r   r   r   r   Ú_set_moduleA   s    

r#   c             C   sÞ   t jjt jjftt jjt jjt jjftt jjt jjft jjjt jj	t jjft jjj
i}tdd„ | D ƒƒ}|j|dƒ}|dkrŠtdj|ƒƒ‚dgt| ƒ }|| Ž |d< x4tdt| ƒƒD ]"}t jjƒ ||< | d j|| _q´W |S )am  Returns a list of modules that fuses the operations specified
     in the input module list.

    Fuses only the following sequence of modules:
    conv, bn
    conv, bn, relu
    conv, relu
    linear, relu
    For these sequences, the first element in the output module list performs
    the fused operation. The rest of the elements are set to nn.Identity()
    c             s   s   | ]}t |ƒV  qd S )N)Útype)Ú.0Úmr   r   r   ú	<genexpr>^   s    z%fuse_known_modules.<locals>.<genexpr>NzCannot fuse modules: {}r   r   )r   r	   ZConv2dZBatchNorm2dr   ZReLUr   r
   r   ZLinearZ
LinearReLUÚtupleÚgetÚNotImplementedErrorÚformatÚlenÚrangeZIdentityr   )Úmod_listZOP_LIST_TO_FUSER_METHODÚtypesZfuser_methodÚnew_modÚir   r   r   Úfuse_known_modulesJ   s    r2   c             C   sT   g }x|D ]}|j t| |ƒƒ q
W ||ƒ}x$t|ƒD ]\}}t| ||| ƒ q4W d S )N)Úappendr   Ú	enumerater#   )r   Úmodules_to_fuseÚ
fuser_funcr.   ÚitemZnew_mod_listr1   r   r   r   Ú_fuse_modulesk   s    
r8   Fc             C   sL   |st j| ƒ} tdd„ |D ƒƒr.t| ||ƒ nx|D ]}t| ||ƒ q4W | S )aÉ  Fuses a list of modules into a single module

    Fuses only the following sequence of modules:
    conv, bn
    conv, bn, relu
    conv, relu
    linear, relu
    All other sequences are left unchanged.
    For these sequences, replaces the first item in the list
    with the fused module, replacing the rest of the modules
    with identity.

    Arguments:
        model: Model containing the modules to be fused
        modules_to_fuse: list of list of module names to fuse. Can also be a list
                         of strings if there is only a single list of modules to fuse.
        inplace: bool specifying if fusion happens in place on the model, by default
                 a new model is returned
        fuser_func: Function that takes in a list of modules and outputs a list of fused modules
                    of the same length. For example,
                    fuser_func([convModule, BNModule]) returns the list [ConvBNModule, nn.Identity()]
                    Defaults to torch.quantization.fuse_known_modules
    Returns:
        model with fused modules. A new copy is created if inplace=True.

    Examples::

            >>> m = myModel()
            >>> # m is a module containing  the sub-modules below
            >>> modules_to_fuse = [ ['conv1', 'bn1', 'relu1'], ['submodule.conv', 'submodule.relu']]
            >>> fused_m = torch.quantization.fuse_modules(m, modules_to_fuse)
            >>> output = fused_m(input)

            >>> m = myModel()
            >>> # Alternately provide a single list of modules to fuse
            >>> modules_to_fuse = ['conv1', 'bn1', 'relu1']
            >>> fused_m = torch.quantization.fuse_modules(m, modules_to_fuse)
            >>> output = fused_m(input)

    c             s   s   | ]}t |tƒV  qd S )N)Ú
isinstanceÚstr)r%   Zmodule_elementr   r   r   r'   ¤   s    zfuse_modules.<locals>.<genexpr>)ÚcopyÚdeepcopyÚallr8   )r   r5   r   r6   Zmodule_listr   r   r   Úfuse_modulesx   s    )

r>   )Ú
__future__r   r   r   r   r   r;   Z torch.nn.intrinsic.modules.fusedr	   r
   ÚmodulesZfusedr   r   r   r   r#   r2   r8   r>   r   r   r   r   Ú<module>   s   	!