a
    óÃÃf  ã                   @   s°   d dl Z d dlmZ d dlmZ ddlT dZdd„ Zdd„ Zee	j
d	œd
d„Zee	j
d	œdd„Zejjjdœdd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )é    N)Ú_ConvNdé   )Ú*c                 C   s   t |  ¡ ƒ| jd< dS )zWCalculate and return the total number of learnable parameters in a given PyTorch model.r   N)Zcalculate_parametersÚ
parametersZtotal_params©ÚmÚxÚy© r
   úS/var/www/html/django/DPS/env/lib/python3.9/site-packages/thop/vision/basic_hooks.pyÚcount_parameters   s    r   c                 C   s   |  j tƒ 7  _ dS )zHIncrementally add zero operations to the model's total operations count.N)Ú	total_opsZcalculate_zero_opsr   r
   r
   r   Úzero_ops   s    r   )r   r	   c              	   C   s@   |d }|  j tt|jƒt|jƒt| jjƒ| j| jd7  _ dS )zmCalculate and add the number of convolutional operations (FLOPs) for a ConvNd layer to the model's total ops.r   )Z
input_sizeÚoutput_sizeZkernel_sizeÚgroupsÚbiasN)r   Zcalculate_conv2d_flopsÚlistÚshapeÚweightr   r   r   r
   r
   r   Úcount_convNd   s    
ûr   c                 C   sX   |d }t  | ¡ dd… | ¡ dd…  ¡ ¡ }|  jt| j ¡ | j ¡ |ƒ7  _dS )z]Calculates and updates total operations (FLOPs) for a convolutional layer in a PyTorch model.r   Nr   é   )	ÚtorchZzerosÚsizeÚnumelr   Zcalculate_convr   Únelementr   )r   r   r	   r   r
   r
   r   Úcount_convNd_ver2*   s    *r   )r   c                 C   sF   |d }t | ¡ ƒ}t| ddƒs,t| ddƒr4|d9 }|  j|7  _dS )ziCalculate and add the FLOPs for a batch normalization layer, including elementwise and affine operations.r   ZaffineFZelementwise_affiner   N)Zcalculate_normr   Úgetattrr   )r   r   r	   Zflopsr
   r
   r   Úcount_normalization:   s
    r   c                 C   s,   |d }|  ¡ }| js(|  jt|ƒ7  _dS )z]Calculate and update the total operation counts for a PReLU layer using input element number.r   N)r   Ztrainingr   Zcalculate_relu)r   r   r	   Z	nelementsr
   r
   r   Úcount_preluP   s    r   c                 C   s$   |d }|  j tt|jƒƒ7  _ dS )zACalculate and update the total operation counts for a ReLU layer.r   N)r   Zcalculate_relu_flopsr   r   r   r
   r
   r   Ú
count_reluY   s    r   c                 C   s:   |d }|  ¡ | j }| ¡ | }|  jt||ƒ7  _dS )zWCalculate and update the total operation counts for a Softmax layer in a PyTorch model.r   N)r   Údimr   r   Zcalculate_softmax)r   r   r	   Z	nfeaturesZ
batch_sizer
   r
   r   Úcount_softmax_   s    r!   c                 C   s   |  ¡ }|  jt|ƒ7  _dS )znCalculate and update the total number of operations (FLOPs) for an AvgPool layer based on the output elements.N)r   r   Zcalculate_avgpool)r   r   r	   Únum_elementsr
   r
   r   Úcount_avgpoolh   s    r#   c              	   C   sb   t  t  g |d jdd… ¢¡t  g |jdd… ¢¡¡}t  |¡}| ¡ }|  jt||ƒ7  _dS )zmCalculate and update the total operation counts for an AdaptiveAvgPool layer using kernel and element counts.r   r   N)r   ÚdivZDoubleTensorr   Úprodr   r   Zcalculate_adaptive_avg)r   r   r	   ZkernelZ	total_addr"   r
   r
   r   Úcount_adap_avgpoolp   s    8
r&   c                 C   sT   | j dvr.t d| j › d¡ |  jd7  _n"|d }|  jt| j | ¡ ƒ7  _dS )zMUpdate total operations counter for upsampling layers based on the mode used.)ZnearestZlinearZbilinearZbicubiczmode z* is not implemented yet, take it a zero opr   N)ÚmodeÚloggingÚwarningr   Zcalculate_upsampler   r   r
   r
   r   Úcount_upsampley   s
    
r*   c                 C   s&   | j }| ¡ }|  jt||ƒ7  _dS )zWCounts total operations for nn.Linear layers using input and output element dimensions.N)Zin_featuresr   r   Zcalculate_linear)r   r   r	   Z	total_mulr"   r
   r
   r   Úcount_linear‰   s    r+   )r(   Ztorch.nnÚnnZtorch.nn.modules.convr   Z	calc_funcZmultiply_addsr   r   r   ZTensorr   r   ÚmodulesZ	batchnormZ
_BatchNormr   r   r   r!   r#   r&   r*   r+   r
   r
   r
   r   Ú<module>   s    			