Ë
    Dêñig  ã                   ó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                 óT   — t        | j                  d¬«      «      | j                  d<   y)zWCalculate and return the total number of learnable parameters in a given PyTorch model.F)Úrecurser   N)r
   Ú
parametersÚtotal_params©ÚmÚxÚys      úY/var/www/pod-logistic/pod-ai/venv/lib/python3.12/site-packages/thop/vision/basic_hooks.pyÚcount_parametersr      s    € ä,¨Q¯\©\À%¨\Ó-HÓI€A‡N�N�1Òó    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   € à‡K‚KÔ%Ó'Ñ'†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à‡K‚KÔ)Ü˜Ÿ™“=Ü˜Ÿ™“MÜ˜Ÿ™Ÿ™Ó(Ø�x‰xØ�V‰VØôñ †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à‡K‚KÔ)Ü˜Ÿ™“=Ü˜Ÿ™“MÜ˜Ÿ™Ÿ™Ó(Ø�x‰xØ�V‰VØôñ †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_ver2r6   I   sr   € à	ˆ!‰€Aô —+‘+˜aŸf™f›h r¨˜l¨Q¯V©V«X°a°b¨\Ñ9Ó:×@Ñ@ÓB€Kð ‡K‚K”> !§&¡&§/¡/Ó"3°Q·X±X×5FÑ5FÓ5HÈ+ÓVÑV†Kr   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_affiner0   N)r	   r4   Úgetattrr   )r   r   r   Úflopss       r   Úcount_normalizationr<   Y   sL   € ð 	
ˆ!‰€Aä˜1Ÿ7™7›9Ó%€EÜˆq�(˜EÔ"¤g¨aÐ1EÀuÔ&MØ�‰
ˆØ‡K‚K�5Ñ†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)r4   Ú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_relurB   x   s'   € à	ˆ!‰€AØ‡K‚KÔ'¬¨Q¯W©W«Ó6Ñ6†Kr   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)r3   Údimr4   r   r   )r   r   r   Ú	nfeaturesÚ
batch_sizes        r   Úcount_softmaxrG   ~   sD   € à	ˆ!‰€AØ—‘“˜Ÿ™‘€IØ—‘“˜iÑ'€Jà‡K‚KÔ$ 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)r4   r   r   )r   r   r   Únum_elementss       r   Úcount_avgpoolrJ   ‡   s#   € ð —7‘7“9€LØ‡K‚KÔ$ \Ó2Ñ2†Kr   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   r0   N)r1   ÚdivÚDoubleTensorr*   Úprodr4   r   r   )r   r   r   ÚkernelÚ	total_addrI   s         r   Úcount_adap_avgpoolrQ   �   s�   € ä�Y‰Y”u×)Ñ)Ð*=¨Q¨q©T¯Z©Z¸¸¨^Ð*=Ó>Ä×@RÑ@RÐScÐVW×V]ÑV]Ð^_Ð^`ÐVaÐScÓ@dÓe€FÜ—
‘
˜6Ó"€IØ—7‘7“9€LØ‡K‚KÔ)¨)°\ÓBÑB†Kr   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   r5   r   s      r   Úcount_upsampler[   ˜   sf   € à‡v�vð ñ ô 	�‰˜% §¡˜xÐ'QÐRÔSØ	�Š�qÑŽàˆa‰DˆØ	�ŠÔ)¨!¯&©&°!·*±*³,Ó?Ñ?Ž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_featuresr4   r   r   )r   r   r   Ú	total_mulrI   s        r   Úcount_linearr_   ©   s.   € à—‘€Ið —7‘7“9€Là‡K‚KÔ# I¨|Ó<Ñ<†Kr   )%rY   r1   Ú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.   r6   ÚmodulesÚ	batchnormÚ
_BatchNormr<   r@   rB   rG   rJ   rQ   r[   r_   © r   r   ú<module>rj      sÂ   ðó ã Ý Ý )÷÷ ÷ ó ð €òJò
(ð
�Gð  5§<¡<ó ð,�Wð  E§L¡Ló ðW˜ð W¨¯©ó Wð 	˜2Ÿ:™:×/Ñ/×:Ñ:ó 	ò,1ò7ò<ò3òCò@ó"=r   