a
    óÃÃfÃ  ã                   @   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,eeee	e
dœdd„Zdd„ Zdd„ Zdd„ Zejdœdd„Zdd„ Zdd„ Zdd„ Zedœdd„Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd(d)„ Zd*d+„ ZdS )-é    Nc                 C   s   d}| D ]}||9 }q|S )z6Compute the product of all elements in the input list.é   © )Úin_listÚresÚ_r   r   úQ/var/www/html/django/DPS/env/lib/python3.9/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ÚDoubleTensorZnelement)Ú.0Úpr   r   r   Ú	<genexpr>   ó    z'calculate_parameters.<locals>.<genexpr>r	   )Z
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   Z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   Z
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-   s    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_flops2   s    r#   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_relu7   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$   )Z
batch_sizeZ	nfeaturesZ	total_expZ	total_addÚ	total_divÚ	total_opsr   r   r   Úcalculate_softmax=   s
    r)   c                 C   s   t  t| ƒg¡S )z<Calculate the average pooling size for a given input tensor.r&   r!   r   r   r   Úcalculate_avgpoolF   s    r*   c                 C   s    d}| | }t  t|| ƒg¡S )zOCalculate FLOPs for adaptive average pooling given kernel size and output size.r   r&   )r   r   r'   Z	kernel_opr   r   r   Úcalculate_adaptive_avgK   s    r+   )Úmodec                 C   sZ   |}| dkr|d9 }n4| dkr(|d9 }n"| dkr:|d9 }n| dkrJ|d9 }t  t|ƒg¡S )	z]Calculate the operations required for various upsample methods based on mode and output size.Zbicubici  Zbilinearé   Zlinearé   Z	trilinearé   r&   )r,   r   r(   r   r   r   Úcalculate_upsampleR   s    


r0   c                 C   s   t  t| | ƒg¡S )zTCalculate the linear operation count for given input feature and number of elements.r&   )Z
in_featureZnum_elementsr   r   r   Úcalculate_linear`   s    r1   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_matmule   s    

r6   c                 C   s   | S )z^Calculate the total number of operations for element-wise multiplication given the input size.r   r!   r   r   r   Úcounter_mull   s    r7   c                 C   s   | S )z\Computes the total scalar multiplications required for power operations based on input size.r   r!   r   r   r   Úcounter_powq   s    r8   c                 C   s   | S )ziCalculate the total number of scalar operations required for a square root operation given an input size.r   r!   r   r   r   Úcounter_sqrtv   s    r9   c                 C   s   | S )z]Calculate the total number of scalar operations for a division operation given an input size.r   r!   r   r   r   Úcounter_div{   s    r:   )F)r   Únumpyr3   r   r   r   r   r   Úlistr$   Úboolr   r    r"   r#   ZTensorr%   r)   r*   r+   Ústrr0   r1   r6   r7   r8   r9   r:   r   r   r   r   Ú<module>   s*   		