
    kh@                         d Z ddlZddlmZ ddlZddlmZ ddlmZ ddl	m
Z
 ddlmZ dd	lmZ d
 Z G d dej                        Zy)z PyTorch Conditionally Parameterized Convolution (CondConv)

Paper: CondConv: Conditionally Parameterized Convolutions for Efficient Inference
(https://arxiv.org/abs/1904.04971)

Hacked together by / Copyright 2020 Ross Wightman
    N)partial)nn)
functional   )	to_2tuple)conv2d_same)get_padding_valuec                       fd}|S )Nc                    t        j                        }t        | j                        dk7  s$| j                  d   k7  s| j                  d   |k7  rt	        d      t              D ]  } | |   j                                y)zCondConv initializer function.   r   r   z<CondConv variables must have shape [num_experts, num_params]N)mathprodlenshape
ValueErrorrangeview)weight
num_paramsiexpert_shapeinitializernum_expertss      S/var/www/teggl/fontify/venv/lib/python3.12/site-packages/timm/layers/cond_conv2d.pycondconv_initializerz6get_condconv_initializer.<locals>.condconv_initializer   s|    YY|,
"fll1o&DQ:-NP Q{# 	6Aq	|45	6     )r   r   r   r   s   ``` r   get_condconv_initializerr      s    6  r   c                   <     e Zd ZdZg dZ	 	 d fd	Zd Zd Z xZS )
CondConv2daO   Conditionally Parameterized Convolution
    Inspired by: https://github.com/tensorflow/tpu/blob/master/models/official/efficientnet/condconv/condconv_layers.py

    Grouped convolution hackery for parallel execution of the per-sample kernel filters inspired by this discussion:
    https://github.com/pytorch/pytorch/issues/17983
    )in_channelsout_channelsdynamic_paddingc
                 <   t         t        |           || _        || _        t        |      | _        t        |      | _        t        ||||      \  }
}|| _	        t        |
      | _
        t        |      | _        || _        |	| _        | j                  | j                  | j                  z  f| j                  z   | _        d}| j                  D ]  }||z  }	 t        j                   j#                  t        j$                  | j                  |            | _        |r_| j                  f| _        t        j                   j#                  t        j$                  | j                  | j                              | _        n| j-                  dd        | j/                          y )N)stridedilationr   bias)superr    __init__r!   r"   r   kernel_sizer%   r	   r#   paddingr&   groupsr   weight_shapetorchr   	ParameterTensorr   
bias_shaper'   register_parameterreset_parameters)selfr!   r"   r*   r%   r+   r&   r,   r'   r   padding_valis_padding_dynamicweight_num_paramwd	__class__s                 r   r)   zCondConv2d.__init__*   sZ   j$(*&($[1'*;[(+D''1 -!(+&!..0@0@DKK0OPSWScScc## 	#B"	#hh((d6F6FHX)YZ#002DO**5<<8H8H$J[J[+\]DI##FD1r   c                    t        t        t        j                  j                  t        j                  d            | j                  | j                        } || j                         | j                  t        j                  | j                  dd        }dt        j                  |      z  }t        t        t        j                  j                  | |      | j                  | j                        } || j                         y y )N   )ar   )r<   b)r   r   r   initkaiming_uniform_r   sqrtr   r-   r   r'   r   uniform_r1   )r4   init_weightfan_inbound	init_biass        r   r3   zCondConv2d.reset_parametersH   s    .BGG,,		!=t?O?OQUQbQbdDKK 99 YYt0045F		&))E0((UFe<d>N>NPTP_P_aIdii  !r   c           
      n   |j                   \  }}}}t        j                  || j                        }|| j                  z  | j
                  | j                  z  f| j                  z   }|j                  |      }d }	| j                  >t        j                  || j                        }	|	j                  || j                  z        }	|j                  d||z  ||      }| j                  r>t        |||	| j                  | j                  | j                  | j                  |z        }
nGt!        j"                  |||	| j                  | j                  | j                  | j                  |z        }
|
j%                  g d      j                  || j                  |
j                   d   |
j                   d         }
|
S )Nr   )r%   r+   r&   r,   )r   r   r      )r   r.   matmulr   r"   r!   r,   r*   r   r'   reshaper#   r   r%   r+   r&   Fconv2dpermute)r4   xrouting_weightsBCHWr   new_weight_shaper'   outs              r   forwardzCondConv2d.forwardS   sm   WW
1aot{{; 1 1143C3Ct{{3RSVZVfVff-.99 <<;D99Q!2!223D IIaQ1%64T\\t{{Q@C ((64T\\t{{Q@C kk,',,Q0A0A399R=RUR[R[\^R_`& 
r   )rG   r    r   r   F   )	__name__
__module____qualname____doc____constants__r)   r3   rW   __classcell__)r9   s   @r   r    r    !   s%     GM>?UV <	!'r   r    )r]   r   	functoolsr   r.   r   torch.nnr   rL   helpersr   r   r+   r	   r   Moduler    r   r   r   <module>rd      s;        $  $ &
 Y Yr   