o
    hd                     @   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d	ed
e	j
fddZd	ed
e	j
fddZd	ejjjf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)calculate_parameters
parameterstotal_paramsmxy r   k/var/www/html/construction_image-detection-poc/venv/lib/python3.10/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_ops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   )
input_sizeoutput_sizekernel_sizegroupsbiasN)r   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      )	torchzerossizenumelr   calculate_convr   nelementr   )r	   r
   r   r   r   r   r   count_convNd_ver2,   s   *&r#   c                 C   sF   |d }t | }t| ddst| ddr|d9 }|  j|7  _dS )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   flopsr   r   r   count_normalization<   s
   r)   c                 C   s0   |d }|  }| js|  jt|7  _dS dS )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   	nelementsr   r   r   count_preluR   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   calculate_relu_flopsr   r   r   r   r   r   
count_relu[   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   calculate_softmax)r	   r
   r   	nfeatures
batch_sizer   r   r   count_softmaxa   s   r4   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   calculate_avgpool)r	   r
   r   num_elementsr   r   r   count_avgpoolj   s   r7   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   divDoubleTensorr   prodr    r   calculate_adaptive_avg)r	   r
   r   kernel	total_addr6   r   r   r   count_adap_avgpoolr   s   8
r>   c                 C   sV   | j dvrtd| j  d |  jd7  _dS |d }|  jt| j | 7  _dS )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   r   r   r   count_upsample{   s
   
rG   c                 C   s&   | j }| }|  jt||7  _dS )zWCounts total operations for nn.Linear layers using input and output element dimensions.N)in_featuresr    r   calculate_linear)r	   r
   r   	total_mulr6   r   r   r   count_linear   s   rK   )rD   torch.nnnntorch.nn.modules.convr   	calc_funcmultiply_addsr   r   r   Tensorr   r#   modules	batchnorm
_BatchNormr)   r-   r/   r4   r7   r>   rG   rK   r   r   r   r   <module>   s"   			