
    hZ                     R   d dl Z d dlZd dlmZ d dlmZ d dlmZmZm	Z	m
Z
mZmZmZmZmZmZmZmZ dZd Zd Zdedej,                  fd	Zdedej,                  fd
Zdedej,                  fdZdej4                  j6                  j8                  fdZd Zd Zd Z d Z!d Z"d Z#d Z$y)    N)_ConvNd)calculate_adaptive_avgcalculate_avgpoolcalculate_convcalculate_conv2d_flopscalculate_linearcalculate_normcalculate_parameterscalculate_relucalculate_relu_flopscalculate_softmaxcalculate_upsamplecalculate_zero_ops   c                 P    t        | j                               | j                  d<   y)zWCalculate and return the total number of learnable parameters in a given PyTorch model.r   N)r
   
parameterstotal_paramsmxys      U/var/www/html/eduruby.in/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_opsr   r   s      r   zero_opsr       s    KK%''Kr   r   r   c           
         |d   }| xj                   t        t        |j                        t        |j                        t        | j                  j                        | j
                  | j                  d      z  c_         y)mCalculate and add the number of convolutional operations (FLOPs) for a ConvNd layer to the model's total ops.r   F
input_sizeoutput_sizekernel_sizegroupsbias	transposeNr   r   listshapeweightr$   r%   r   s      r   count_convNdr+   %   sX    	!AKK)=M(xxVV Kr   c           
         |d   }| xj                   t        t        |j                        t        |j                        t        | j                  j                        | j
                  | j                  d      z  c_         y)r   r   Tr    Nr'   r   s      r   count_convtNdr-   ;   sX    	!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   r   r%   nelementr*   )r   r   r   r"   s       r   count_convNd_ver2r5   I   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)r	   r3   getattrr   )r   r   r   flopss       r   count_normalizationr;   Y   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)r3   trainingr   r   )r   r   r   	nelementss       r   count_prelur?   o   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   r   r(   r)   r   s      r   
count_relurA   x   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)r2   dimr3   r   r   )r   r   r   	nfeatures
batch_sizes        r   count_softmaxrF   ~   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)r3   r   r   )r   r   r   num_elementss       r   count_avgpoolrI      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)r0   divDoubleTensorr)   prodr3   r   r   )r   r   r   kernel	total_addrH   s         r   count_adap_avgpoolrP      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bicubic	trilinearzmode z* is not implemented yet, take it a zero opr   N)modeloggingwarningr   r   r4   r   s      r   count_upsamplerZ      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_featuresr3   r   r   )r   r   r   	total_mulrH   s        r   count_linearr^      s.    I 779LKK#I|<<Kr   )%rX   r0   torch.nnnntorch.nn.modules.convr   thop.vision.calc_funcr   r   r   r   r   r	   r
   r   r   r   r   r   multiply_addsr   r   Tensorr+   r-   r5   modules	batchnorm
_BatchNormr;   r?   rA   rF   rI   rP   rZ   r^    r   r   <module>ri      s       )    =
(
G 5<< ,W ELL W W W 	2:://:: 	,17<3C@"=r   