
    Ngq                     V    d Z ddlZddlmZ  G d dej        j                  ZddZdS )a=   Split BatchNorm

A PyTorch BatchNorm layer that splits input batch into N equal parts and passes each through
a separate BN layer. The first split is passed through the parent BN layers with weight/bias
keys the same as the original BN. All other splits pass through BN sub-layers under the '.aux_bn'
namespace.

This allows easily removing the auxiliary BN layers after training to efficiently
achieve the 'Auxiliary BatchNorm' as described in the AdvProp Paper, section 4.2,
'Disentangled Learning via An Auxiliary BN'

Hacked together by / Copyright 2020 Ross Wightman
    Nc                   >     e Zd Z	 	 d fd	Zdej        f fdZ xZS )	SplitBatchNorm2dh㈵>皙?T   c                     t                                                     |dk    s
J d            || _        t          j        fdt          |dz
            D                       | _        d S )N   z=Should have at least one aux BN layer (num_splits at least 2)c           	      @    g | ]}t          j                  S  )nnBatchNorm2d).0_affineepsmomentumnum_featurestrack_running_statss     W/var/www/html/ai-engine/env/lib/python3.11/site-packages/timm/layers/split_batchnorm.py
<listcomp>z-SplitBatchNorm2d.__init__.<locals>.<listcomp>   s?     %u %u %uYZBN<h@STT%u %u %u    )super__init__
num_splitsr   
ModuleListrangeaux_bn)selfr   r   r   r   r   r   	__class__s    ````` r   r   zSplitBatchNorm2d.__init__   s    sHf>QRRRA~~~^~~~$m %u %u %u %u %u %u %u %u^cdnqrdr^s^s%u %u %u v vr   inputc                    | j         r|j        d         | j        z  }|j        d         || j        z  k    s
J d            |                    |          }t	                                          |d                   g}t          | j                  D ],\  }}|                     |||dz                                 -t          j
        |d          S t	                                          |          S )Nr   z1batch size must be evenly divisible by num_splitsr	   )dim)trainingshaper   splitr   forward	enumerater   appendtorchcat)r   r    
split_sizesplit_inputxiar   s          r   r&   zSplitBatchNorm2d.forward   s    = 		*Q4?:J;q>Z$/%AAAACvAAA++j11KQ001A!$+.. 0 01;q1u-..////9QA&&&&77??5)))r   )r   r   TTr   )__name__
__module____qualname__r   r)   Tensorr&   __classcell__)r   s   @r   r   r      sp        DH67v v v v v v
*U\ 
* 
* 
* 
* 
* 
* 
* 
* 
* 
*r   r   r   c                    | }t          | t          j        j        j        j                  r| S t          | t          j        j        j        j                  rt          | j	        | j
        | j        | j        | j        |          }| j        |_        | j        |_        | j        |_        | j        rt| j        j                                                                        |j        _        | j        j                                                                        |j        _        |j        D ]}| j                                        |_        | j                                        |_        | j                                        |_        | j        rt| j        j                                                                        |j        _        | j        j                                                                        |j        _        |                                 D ]*\  }}|                    |t3          ||                     +~ |S )a  
    Recursively traverse module and its children to replace all instances of
    ``torch.nn.modules.batchnorm._BatchNorm`` with `SplitBatchnorm2d`.
    Args:
        module (torch.nn.Module): input module
        num_splits: number of separate batchnorm layers to split input across
    Example::
        >>> # model is an instance of torch.nn.Module
        >>> model = timm.models.convert_splitbn_model(model, num_splits=2)
    )r   )
isinstancer)   r   modulesinstancenorm_InstanceNorm	batchnorm
_BatchNormr   r   r   r   r   r   running_meanrunning_varnum_batches_trackedweightdataclonedetachbiasr   named_children
add_moduleconvert_splitbn_model)moduler   modauxnamechilds         r   rF   rF   )   s    C&%(*7EFF &%(*4?@@ BV_fm&:? ? ? ". ,"("<= 	>$m06688??AACJO"K,2244;;==CHM: 	B 	BC%288::C$06688CO&,&@&F&F&H&HC#} B"(-"4":":"<"<"C"C"E"E
 & 0 6 6 8 8 ? ? A A,,.. R Ret25ZPPPQQQQJr   )r   )__doc__r)   torch.nnr   r   r   rF   r   r   r   <module>rN      sy           * * * * *ux+ * * *." " " " " "r   