
    |hd                         d dl Z d dlmZ d dlmZ ddl dZd Zd Zdede	j                  fd	Zdede	j                  fd
Zdej                  j                  j                  fdZd Zd Zd Zd Zd Zd Zd Zy)    N)_ConvNd   )*c                 P    t        | j                               | j                  d<   y)zWCalculate and return the total number of learnable parameters in a given PyTorch model.r   N)calculate_parameters
parameterstotal_paramsmxys      V/var/www/html/test/engine/venv/lib/python3.12/site-packages/thop/vision/basic_hooks.pycount_parametersr      s    ,Q\\^<ANN1    c                 >    | xj                   t               z  c_         y)zHIncrementally add zero operations to the model's total operations count.N)	total_opscalculate_zero_opsr
   s      r   zero_opsr      s    KK%''Kr   r   r   c           	         |d   }| xj                   t        t        |j                        t        |j                        t        | j                  j                        | j
                  | j                        z  c_         y)zmCalculate and add the number of convolutional operations (FLOPs) for a ConvNd layer to the model's total ops.r   )
input_sizeoutput_sizekernel_sizegroupsbiasN)r   calculate_conv2d_flopslistshapeweightr   r   r
   s      r   count_convNdr      sU    	!AKK)=M(xxVV Kr   c                 >   |d   }t         j                  |j                         dd |j                         dd z         j                         }| xj                  t        | j                  j                         | j                  j                         |      z  c_        y)z]Calculates and updates total operations (FLOPs) for a convolutional layer in a PyTorch model.r   Nr      )	torchzerossizenumelr   calculate_convr   nelementr   )r   r   r   r   s       r   count_convNd_ver2r(   ,   sr    	!A ++affhrlQVVXab\9:@@BK KK>!&&//"3QXX5F5F5H+VVKr   c                     |d   }t        |j                               }t        | dd      st        | dd      r|dz  }| xj                  |z  c_        y)ziCalculate and add the FLOPs for a batch normalization layer, including elementwise and affine operations.r   affineFelementwise_affiner!   N)calculate_normr%   getattrr   )r   r   r   flopss       r   count_normalizationr/   <   sL     	
!A1779%Eq(E"ga1Eu&M
KK5Kr   c                     |d   }|j                         }| j                  s| xj                  t        |      z  c_        yy)z]Calculate and update the total operation counts for a PReLU layer using input element number.r   N)r%   trainingr   calculate_relu)r   r   r   	nelementss       r   count_prelur4   R   s5    	!A	I::	~i00 r   c                 p    |d   }| xj                   t        t        |j                              z  c_         y)zACalculate and update the total operation counts for a ReLU layer.r   N)r   calculate_relu_flopsr   r   r
   s      r   
count_relur7   [   s'    	!AKK'QWW66Kr   c                     |d   }|j                         | j                     }|j                         |z  }| xj                  t	        ||      z  c_        y)zWCalculate and update the total operation counts for a Softmax layer in a PyTorch model.r   N)r$   dimr%   r   calculate_softmax)r   r   r   	nfeatures
batch_sizes        r   count_softmaxr=   a   sD    	!AIi'JKK$Z;;Kr   c                 `    |j                         }| xj                  t        |      z  c_        y)znCalculate and update the total number of operations (FLOPs) for an AvgPool layer based on the output elements.N)r%   r   calculate_avgpool)r   r   r   num_elementss       r   count_avgpoolrA   j   s#     779LKK$\22Kr   c           	      F   t         j                  t         j                  g |d   j                  dd       t         j                  g |j                  dd             }t         j	                  |      }|j                         }| xj                  t        ||      z  c_        y)zmCalculate and update the total operation counts for an AdaptiveAvgPool layer using kernel and element counts.r   r!   N)r"   divDoubleTensorr   prodr%   r   calculate_adaptive_avg)r   r   r   kernel	total_addr@   s         r   count_adap_avgpoolrI   r   s    YYu))*=QqTZZ^*=>@R@RScVWV]V]^_^`VaSc@deF

6"I779LKK))\BBKr   c                 
   | j                   dvr9t        j                  d| j                    d       | xj                  dz  c_        y|d   }| xj                  t	        | j                   |j                               z  c_        y)zMUpdate total operations counter for upsampling layers based on the mode used.)nearestlinearbilinearbicubiczmode z* is not implemented yet, take it a zero opr   N)modeloggingwarningr   calculate_upsampler'   r
   s      r   count_upsamplerS   {   sf    vv   	%x'QRS	qaD	)!&&!**,??r   c                 z    | j                   }|j                         }| xj                  t        ||      z  c_        y)zWCounts total operations for nn.Linear layers using input and output element dimensions.N)in_featuresr%   r   calculate_linear)r   r   r   	total_mulr@   s        r   count_linearrX      s.    I 779LKK#I|<<Kr   )rP   torch.nnnntorch.nn.modules.convr   	calc_funcmultiply_addsr   r   r"   Tensorr   r(   modules	batchnorm
_BatchNormr/   r4   r7   r=   rA   rI   rS   rX    r   r   <module>rc      s      ) =
(

G 
5<< 
*W W W 	2:://:: 	,17<3C@ =r   