o
    h
                     @   s   d dl Z d dlZd dlZdd Zdd Zdd Zdd	 Zd/dededede	de
f
ddZdd Zdd Zdd ZdejfddZdd Zdd Zdd Zd efd!d"Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. ZdS )0    Nc                 C   s   d}| D ]}||9 }q|S )z6Compute the product of all elements in the input list.    )in_listres_r   r   i/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/thop/vision/calc_func.pyl_prod	   s   
r   c                 C   s   t | S )z6Calculate the sum of all numerical elements in a list.sum)r   r   r   r   l_sum   s   r   c                 C   s   t dd | D S )z`Calculate the total number of parameters in a list of tensors using the product of their shapes.c                 s   s     | ]}t | gV  qd S )N)torchDoubleTensornelement).0pr   r   r   	<genexpr>   s    z'calculate_parameters.<locals>.<genexpr>r	   )
param_listr   r   r   calculate_parameters   s   r   c                   C   s   t dgS )z?Initializes and returns a tensor with all elements set to zero.r   r   r   r   r   r   r   calculate_zero_ops   s   r   F
input_sizeoutput_sizekernel_sizegroupsbiasc                 C   s,   | d }|}t |||  t |dd  S )zdCalculate FLOPs for a Conv2D layer using input/output sizes, kernel size, groups, and the bias flag.r      N)r   )r   r   r   r   r   in_cgr   r   r   calculate_conv2d_flops    s    r   c                 C   s&   t d t||| | |   gS )zgCalculate FLOPs for convolutional layers given bias, kernel size, output size, in_channels, and groups.zThis API is being deprecated.)warningswarnr   r   )r   r   r   
in_channelgroupr   r   r   calculate_conv)   s   
r#   c                 C   s   t d|  gS )zACompute the L2 norm of a tensor or array based on its input size.r   r   r   r   r   r   calculate_norm/      r%   c                 C   s   dS )z[Calculates the FLOPs for a ReLU activation function based on the input tensor's dimensions.r   r   r$   r   r   r   calculate_relu_flops4      r'   c                 C   s   t d tt| gS )zKConvert an input tensor to a DoubleTensor with the same value (deprecated).zThis API is being deprecated)r   r    r   r   intr$   r   r   r   calculate_relu9   s   
r*   c                 C   s0   |}|d }|}| || |  }t t|gS )zJCompute FLOPs for a softmax activation given batch size and feature count.r   r   r   r)   )
batch_size	nfeatures	total_exp	total_add	total_div	total_opsr   r   r   calculate_softmax?   s
   r2   c                 C   s   t t| gS )z<Calculate the average pooling size for a given input tensor.r+   r$   r   r   r   calculate_avgpoolH   r&   r3   c                 C   s    d}| | }t t|| gS )zOCalculate FLOPs for adaptive average pooling given kernel size and output size.r   r+   )r   r   r0   	kernel_opr   r   r   calculate_adaptive_avgM   s   r5   modec                 C   sZ   |}| dkr|d9 }n| dkr|d9 }n| dkr|d9 }n| dkr%|d9 }t t|gS )	z]Calculate the operations required for various upsample methods based on mode and output size.bicubici  bilinear   linear   	trilinear   r+   )r6   r   r1   r   r   r   calculate_upsampleT   s   


r>   c                 C   s   t t| | gS )zTCalculate the linear operation count for given input feature and number of elements.r+   )
in_featurenum_elementsr   r   r   calculate_linearb   s   rA   c                 C   s&   t | } t |}t | |d  S )z`Calculate the total number of operations for matrix multiplication given input and output sizes.)nparrayprod)r   r   r   r   r   counter_matmulg   s   

rF   c                 C      | S )z^Calculate the total number of operations for element-wise multiplication given the input size.r   r$   r   r   r   counter_muln   r(   rH   c                 C   rG   )z\Computes the total scalar multiplications required for power operations based on input size.r   r$   r   r   r   counter_pows   r(   rI   c                 C   rG   )ziCalculate the total number of scalar operations required for a square root operation given an input size.r   r$   r   r   r   counter_sqrtx   r(   rJ   c                 C   rG   )z]Calculate the total number of scalar operations for a division operation given an input size.r   r$   r   r   r   counter_div}   r(   rK   )F)r   numpyrC   r   r   r   r   r   listr)   boolr   r#   r%   r'   Tensorr*   r2   r3   r5   strr>   rA   rF   rH   rI   rJ   rK   r   r   r   r   <module>   s,    		