
    NgO                         d Z ddlZddlmZ ddl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dS )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k    s"| j        d         k    s| j        d         |k    rt	          d          t                    D ]&} | |                                                  'dS )zCondConv initializer function.   r   r   z<CondConv variables must have shape [num_experts, num_params]N)npprodlenshape
ValueErrorrangeview)weight
num_paramsiexpert_shapeinitializernum_expertss      S/var/www/html/ai-engine/env/lib/python3.11/site-packages/timm/layers/cond_conv2d.pycondconv_initializerz6get_condconv_initializer.<locals>.condconv_initializer   s    W\**
""fl1o&D&DQ:--NP P Q{## 	6 	6AKq	|445555	6 	6     )r   r   r   r   s   ``` r   get_condconv_initializerr      s0    6 6 6 6 6 6 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_padding   r    F   c
                 >   t          t          |                                            || _        || _        t          |          | _        t          |          | _        t          ||||          \  }
}|| _	        t          |
          | _
        t          |          | _        || _        |	| _        | j        | j        | j        z  f| j        z   | _        d}| j        D ]}||z  }t          j                            t          j        | j        |                    | _        |rO| j        f| _        t          j                            t          j        | j        | j                            | _        n|                     dd            |                                  d S )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__+   s   j$((***&($[11''*;[(+D +D +D''1 --!(++&!.0@DK0OPSWScc# 	# 	#B"h((d6FHX)Y)YZZ 	2#02DO**5<8H$J[+\+\]]DII##FD11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
                   d S d S )N   )ar   )r?   b)r   r   r   initkaiming_uniform_mathsqrtr   r0   r   r*   r   r   uniform_r4   )r7   init_weightfan_inbound	init_biass        r   r6   zCondConv2d.reset_parametersI   s    .BG,	!===t?OQUQbd dDK   9 WT.qrr233F	&)))E0(UFe<<<d>NPTP_a aIIdi      ! r   c           
         |j         \  }}}}t          j        || j                  }|| j        z  | j        | j        z  f| j        z   }|                    |          }d }	| j	        7t          j        || j	                  }	|	                    || j        z            }	|
                    d||z  ||          }| j        r.t          |||	| j        | j        | j        | j        |z            }
n2t!          j        |||	| j        | j        | j        | j        |z            }
|
                    g d                              || j        |
j         d         |
j         d                   }
|
S )Nr   )r(   r.   r)   r/   )r   r   r   r$   )r   r1   matmulr   r"   r!   r/   r-   r   r*   reshaper#   r   r(   r.   r)   Fconv2dpermute)r7   xrouting_weightsBCHWr   new_weight_shaper*   outs              r   forwardzCondConv2d.forwardT   so   W
1aot{;; 1143Ct{3RSVZVff-..9 <;;D99Q!2233D IIaQ1%% 	@64T\t{Q@ @ @CC (64T\t{Q@ @ @C kk,,,'',,Q0A39R=RUR[\^R_``& 
r   )r$   r   r%   r   r   Fr&   )	__name__
__module____qualname____doc____constants__r,   r6   rZ   __classcell__)r<   s   @r   r    r    "   sy          GFFM>?UV           <	! 	! 	!' ' ' ' ' ' 'r   r    )r^   rC   	functoolsr   numpyr   r1   r   torch.nnr   rO   helpersr   r   r.   r	   r   Moduler    r   r   r   <module>rf      s                       $ $ $ $ $ $       $ $ $ $ $ $ & & & & & &
  
  
 Y Y Y Y Y Y Y Y Y Yr   