
    j                         d dl Z d dlZd dlZd Zd Zd Zd Z	 ddeded	ed
e	de
de
fdZd Zd Zd Zdej        fdZd Zd Zd ZdefdZd Zd Zd Zd Zd Zd ZdS )    Nc                     d}| D ]}||z  }|S )z6Compute the product of all elements in the input list.    )in_listres_s      Z/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/thop/vision/calc_func.pyl_prodr
   	   s&    
C  qJ    c                      t          |           S )z6Calculate the sum of all numerical elements in a list.sum)r   s    r	   l_sumr      s    w<<r   c                 4    t          d | D                       S )z`Calculate the total number of parameters in a list of tensors using the product of their shapes.c              3   d   K   | ]+}t          j        |                                g          V  ,d S )N)torchDoubleTensornelement).0ps     r	   	<genexpr>z'calculate_parameters.<locals>.<genexpr>   s7      FFau!1::<<.11FFFFFFr   r   )
param_lists    r	   calculate_parametersr      s    FF:FFFFFFr   c                  ,    t          j        dg          S )z?Initializes and returns a tensor with all elements set to zero.r   r   r   r   r   r	   calculate_zero_opsr      s    qc"""r   F
input_sizeoutput_sizekernel_sizegroupsbias	transposec                     |r5|d         }t          |           ||z  z  t          |dd                   z  S | d         }t          |          ||z  z  t          |dd                   z  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!   r"   out_cin_cs           r	   calculate_conv2d_flopsr'       sw      PAj!!Uf_5{1228O8OOO!}k""dfn5{1228O8OOOr   c                 l    t          j        d           t          j        |||z  |z  | z   z  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groups        r	   calculate_convr-   .   s<    M1222{j5.@;.NQU.UVWXXXr   c                 2    t          j        d| z  g          S )zACompute the L2 norm of a tensor or array based on its input size.r$   r   r   s    r	   calculate_normr0   4   s    q:~.///r   c                     dS )z[Calculates the FLOPs for a ReLU activation function based on the input tensor's dimensions.r   r   r/   s    r	   calculate_relu_flopsr2   9   s    1r   c                 n    t          j        d           t          j        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/   s    r	   calculate_relur5   >   s-    M0111s:/000r   c                 n    |}|dz
  }|}| ||z   |z   z  }t          j        t          |          g          S )zJCompute FLOPs for a softmax activation given batch size and feature count.r   r   r   r4   )
batch_size	nfeatures	total_exp	total_add	total_div	total_opss         r	   calculate_softmaxr>   D   sC    IAIIi)3i?@Is9~~.///r   c                 F    t          j        t          |           g          S )z<Calculate the average pooling size for a given input tensor.r7   r/   s    r	   calculate_avgpoolr@   M   s    s:/000r   c                 Z    d}| |z   }t          j        t          ||z            g          S )zOCalculate FLOPs for adaptive average pooling given kernel size and output size.r   r7   )r   r   r<   	kernel_ops       r	   calculate_adaptive_avgrC   R   s2    Ii'Is9{#:;;<===r   modec                     |}| dk    r|dz  }n#| dk    r|dz  }n| dk    r|dz  }n| dk    r|dz  }t          j        t          |          g          S )	z]Calculate the operations required for various upsample methods based on mode and output size.bicubici  bilinear   linear   	trilinear   r7   )rD   r   r=   s      r	   calculate_upsamplerM   Y   sy    IyX					R					Q					Z	s9~~.///r   c                 L    t          j        t          | |z            g          S )zTCalculate the linear operation count for given input feature and number of elements.r7   )
in_featurenum_elementss     r	   calculate_linearrQ   g   s#    s:#<==>???r   c                     t          j        |           } t          j        |          }t          j        |           |d         z  S )z`Calculate the total number of operations for matrix multiplication given input and output sizes.)nparrayprod)r   r   s     r	   counter_matmulrW   l   s9    *%%J(;''K7:R00r   c                     | S )z^Calculate the total number of operations for element-wise multiplication given the input size.r   r/   s    r	   counter_mulrY   s       r   c                     | S )z\Computes the total scalar multiplications required for power operations based on input size.r   r/   s    r	   counter_powr\   x   rZ   r   c                     | S )ziCalculate the total number of scalar operations required for a square root operation given an input size.r   r/   s    r	   counter_sqrtr^   }   rZ   r   c                     | S )z]Calculate the total number of scalar operations for a division operation given an input size.r   r/   s    r	   counter_divr`      rZ   r   )FF)r)   numpyrT   r   r
   r   r   r   listr4   boolr'   r-   r0   r2   Tensorr5   r>   r@   rC   strrM   rQ   rW   rY   r\   r^   r`   r   r   r	   <module>rf      s            
G G G
# # # puP PP#'P6:PDGPOSPhlP P P PY Y Y0 0 0
  
1u| 1 1 1 10 0 01 1 1
> > >0S 0 0 0 0@ @ @
1 1 1  
  
  
    r   