
    RЦi7                     d    S r SSKrSSKJr   " S S\R                  R                  5      rSS jrg)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                   b   ^  \ rS rSr       SU 4S jjrS\R                  4U 4S jjrSrU =r	$ )SplitBatchNorm2d   c	                    > XxS.n	[         TU ]  XX4U5        US:  d   S5       eX`l        [        R                  " [        US-
  5       V
s/ s H  n
[        R                  " XX4U40 U	D6PM     sn
5      U l        g s  sn
f )N)devicedtype   z=Should have at least one aux BN layer (num_splits at least 2))super__init__
num_splitsnn
ModuleListrangeBatchNorm2daux_bn)selfnum_featuresepsmomentumaffinetrack_running_statsr   r   r   dd_	__class__s              Z/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/timm/layers/split_batchnorm.pyr   SplitBatchNorm2d.__init__   s     /H>QRA~^^^~$mm:>*%
* NN<h@SZWYZ*%
  %
s   
#A:inputc                   > U R                   (       a  UR                  S   U R                  -  nUR                  S   X R                  -  :X  d   S5       eUR                  U5      n[        TU ]  US   5      /n[        U R                  5       H!  u  pVUR                  U" X5S-      5      5        M#     [        R                  " USS9$ [        TU ]  U5      $ )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#   SplitBatchNorm2d.forward(   s    ==Q4??:J;;q>Z//%AAvCvvA++j1KQ01A!$++.;1u-./ /99QA&&7?5))    )r   r   )gh㈵>g?TT   NN)
__name__
__module____qualname____firstlineno__r   r&   Tensorr#   __static_attributes____classcell__)r   s   @r   r   r      s4    
  $(
*U\\ 
* 
*r.   r   c           	         U n[        U [        R                  R                  R                  R
                  5      (       a  U $ [        U [        R                  R                  R                  R                  5      (       Ga  [        U R                  U R                  U R                  U R                  U R                  US9nU R                  Ul        U R                  Ul        U R                   Ul        U R                  (       a  U R"                  R$                  R'                  5       R)                  5       UR"                  l        U R*                  R$                  R'                  5       R)                  5       UR*                  l        UR,                   H  nU R                  R'                  5       Ul        U R                  R'                  5       Ul        U R                   R'                  5       Ul        U R                  (       d  Ms  U R"                  R$                  R'                  5       R)                  5       UR"                  l        U R*                  R$                  R'                  5       R)                  5       UR*                  l        M     U R/                  5        H  u  pEUR1                  U[3        XQS95        M      A U$ )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   rH   rH   5   s    C&%((**77EEFF&%((**44??@@V__fmm&&:? ".. ,,"("<"<==$mm00668??ACJJO"KK,,224;;=CHHM::C%2288:C$00668CO&,&@&@&F&F&HC#}}}"(--"4"4":":"<"C"C"E

 & 0 0 6 6 8 ? ? A  ,,.t25PQ /Jr.   )r/   )__doc__r&   torch.nnr   r   r   rH    r.   r   <module>rQ      s.      *uxx++  *F"r.   