
    hZ                     Z   S SK r S SKrS SKJr  S SKJr  S SKJrJrJ	r	J
r
JrJrJrJrJrJrJrJr  SrS rS rS\S\R,                  4S	 jrS\S\R,                  4S
 jrS\S\R,                  4S jrS\R4                  R6                  R8                  4S jrS rS rS r S r!S r"S r#S r$g)    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    [        U R                  5       5      U R                  S'   g)zWCalculate and return the total number of learnable parameters in a given PyTorch model.r   N)r
   
parameterstotal_paramsmxys      Q/home/james-whalen/.local/lib/python3.13/site-packages/thop/vision/basic_hooks.pycount_parametersr      s    ,Q\\^<ANN1    c                 >    U =R                   [        5       -  sl         g)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                     US   nU =R                   [        [        UR                  5      [        UR                  5      [        U R                  R                  5      U R
                  U R                  SS9-  sl         g)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                     US   nU =R                   [        [        UR                  5      [        UR                  5      [        U R                  R                  5      U R
                  U R                  SS9-  sl         g)r   r   Tr    Nr'   r   s      r   count_convtNdr-   ;   sX    	!AKK)=M(xxVV Kr   c                 @   US   n[         R                  " UR                  5       SS UR                  5       SS -   5      R                  5       nU =R                  [        U R                  R                  5       U R                  R                  5       U5      -  sl        g)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                     US   n[        UR                  5       5      n[        U SS5      (       d  [        U SS5      (       a  US-  nU =R                  U-  sl        g)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   sR     	
!A1779%Eq(E""ga1Eu&M&M
KK5Kr   c                     US   nUR                  5       nU R                  (       d  U =R                  [        U5      -  sl        gg)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    US   nU =R                   [        [        UR                  5      5      -  sl         g)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                     US   nUR                  5       U R                     nUR                  5       U-  nU =R                  [	        XC5      -  sl        g)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   ~   sB    	!AIi'JKK$Z;;Kr   c                 `    UR                  5       nU =R                  [        U5      -  sl        g)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           	      L   [         R                  " [         R                  " / US   R                  SS Q5      [         R                  " / UR                  SS Q5      5      n[         R                  " U5      nUR                  5       nU =R                  [        XE5      -  sl        g)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                    U R                   S;  a:  [        R                  " SU R                    S35        U =R                  S-  sl        gUS   nU =R                  [	        U R                   UR                  5       5      -  sl        g)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                 x    U R                   nUR                  5       nU =R                  [        X45      -  sl        g)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   