o
    #Õj,  ã                   @   sŠ  d dl Z i add„ Zdedededefdd	„Zd
d„ Zedƒdd„ ƒZedƒdd„ ƒZ	edƒdd„ ƒZ
dd„ Zedƒdd„ ƒZedƒdd„ ƒZedƒdd„ ƒZed ƒd!d"„ ƒZed#ƒd$d%„ ƒZed&ƒd'd(„ ƒZed)ƒd*d+„ ƒZd,d-„ Zed.ƒd/d0„ ƒZed1ƒd2d3„ ƒZed4ƒd5d6„ ƒZed7ƒd8d9„ ƒZed:ƒd;d<„ ƒZed=ƒd>d?„ ƒZed@ƒdAdB„ ƒZedCƒdDdE„ ƒZedFƒdGdH„ ƒZedIƒdJdK„ ƒZdS )Lé    Nc                 C   s   d}| D ]}||9 }q|S )Né   © )ÚsÚpÚvr   r   úS/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/utils/flops.pyÚprod   s   
r   Úop_typeÚinput_shapesÚattrsÚreturnc              
   C   sL   | t vrdS t |  }z|||ƒ}W |S  ty% } zW Y d}~dS d}~ww )zÿ
    count FLOPs for operation.

    Args:
        op_type (str): the type of operation.
        input_shapes (dict): the shapes of inputs.
        attrs (dict): the attributes of the operation.

    Returns:
        the total FLOPs of the operation.
    r   N)Ú_FLOPS_COMPUTE_FUNC_MAPÚ	Exception)r	   r
   r   ÚfuncÚflopsÚer   r   r   r      s   þ€ÿr   c                    s   ‡ fdd„}|S )z<
    register flops computation function for operation.
    c                    s   | t ˆ < | S ©N)r   )r   ©r	   r   r   Úregister8   s   z register_flops.<locals>.registerr   )r	   r   r   r   r   Úregister_flops3   s   r   Zc_embeddingc                 C   ó   dS )zbFLOPs computation for c_embedding op.
    For c_embedding(input):
        equation: flops = 0
    r   r   ©r
   r   r   r   r   Ú_c_embedding_flops@   ó   r   Zconv2dc                 C   s”  t |  d¡ƒdkr|  d¡d nd}|  d¡d }|  d¡d }| d¡}| d¡}| d¡}| d	¡}|d }	|d
 }
|d }t|dd… ƒ}t|dd… ƒ}t |ƒ}t|tƒr[|n|g| }t|tƒrg|n|g| }t|tƒrs|n|g| }g }t|ƒD ]#\}}|d||   || || d
  d
  ||  d
 }| |¡ q~|| }t|ƒ|
 | }|	t|ƒ }|| }d| }d}|durÆ|| }|| S )a   FLOPs computation for conv2d op.
    For conv2d(input,filter):
        active_elements = batch_size * numel(output)
        conv_flops = 2 * macs_per_position_conv * active_elements
        bias_flops = out_channels * active_elements
        equation: flops = conv_flops + bias_flops
    ZBiasr   NZInputÚFilterÚpaddingsÚstridesÚ	dilationsÚgroupsr   é   )ÚlenÚgetÚlistÚ
isinstanceÚ	enumerateÚappendr   )r
   r   ZbiasÚinputÚweightÚpaddingZstrideZdilationr   Z
batch_sizeZin_channelsZout_channelsZkernel_dimsZ
input_dimsÚlengthr   r   r   Zoutput_dimsÚidxZ	input_dimZ
output_dimZfilters_per_channelZmacs_conv_per_positionZactive_elementsZoverall_conv_macsZoverall_conv_flopsZoverall_bias_flopsr   r   r   Ú_conv2d_flopsI   sv   ÿý



ÿÿýý
ÿÿýý
ÿÿýý	
ÿþüüÿr+   Zdropoutc                 C   r   )zZFLOPs computation for dropout op.
    For dropout(input):
        equation: flops = 0
    r   r   r   r   r   r   Ú_dropout_flops˜   r   r,   c                 C   s˜   |   d¡d }|   d¡d }t|ƒ}t|ƒ}t||ƒ}g }t|ƒD ]&}||k r/||d |  nd}	||k r=||d |  nd}
| t|	|
ƒ¡ q!t|ƒS )NÚXr   ÚYr   )r!   r    ÚmaxÚranger%   r   )r
   r   Zinput_xZinput_yÚdim_xÚdim_yZ
dim_outputÚoutputÚiZin_xZin_yr   r   r   Ú_elementwise_flops_compute¡   s   
r5   Zelementwise_addc                 C   ó
   t | |ƒS )a7  FLOPs computation for elementwise_add op.
    For elementwise_add(input,other):
        input_shapes = [shape_of_input, shape_of_other]
        shape_of_input = [dim1, dim2, dim3 ...]
        shape_of_other = [odim1, odim2, odim3...]
        equation: flops = max(dim1, odim1) * max(dim2, odim2) * max()...
    ©r5   r   r   r   r   Ú_elementwise_add_flops¯   ó   
	r8   Zelementwise_mulc                 C   r6   )a6  FLOPs computation for elementwise_mul op.
    For elementwise_mul(input,other):
        input_shapes = [shape_of_input, shape_of_other]
        shape_of_input = [dim1, dim2, dim3 ...]
        shape_of_other = [odim1, odim2, odim3...]
        equation: flops = max(dim1, odim1) * max(dim2, odim2)* max()...
    r7   r   r   r   r   Ú_elementwise_mul_flops»   r9   r:   Zelementwise_divc                 C   r6   )a1  FLOPs computation for elementwise_div op.
    For elementwise_div(input,other):
        input_shapes = [shape_of_input, shape_of_other]
        shape_of_input = [dim1, dim2, dim3 ...]
        shape_of_other = [odim1, odim2, odim3...]
        equation: flops = max(dim1,odim1)*max(dim2,odim2)*max()...
    r7   r   r   r   r   Ú_elementwise_div_flopsÇ   r9   r;   Zgeluc                 C   ó   |   d¡d }t|ƒd S )z‹FLOPs computation for gelu op.
    For gelu(input):
        equation: flops = 5 * (numel)total number of elements in the input tensor.
    r-   r   é   ©r!   r   ©r
   r   r&   r   r   r   Ú_gelu_flopsÓ   ó   r@   Z
layer_normc                 C   s4   |   d¡d }t|ƒd }|  d¡r|t|ƒ7 }|S )a  FLOPs computation for layer_norm op.
    For layer_norm(input):
        equation:
        1): WITHOUT epsilon flops = 7 * (numel)total number of elements in the input tensor.
        2): WITH epsilon flops = 8 * (numel)total number of elements in the input tensor.
    r-   r   é   Úepsilonr>   )r
   r   r&   r   r   r   r   Ú_layer_norm_flopsÝ   s
   
rD   Úmatmulc              	   C   s8  t  |  d|  ddgg¡¡d ¡}t  |  d|  ddgg¡¡d ¡}| d¡s,| d¡r9|d |d	 |d	< |d< | d
¡sC| d¡rP|d |d	 |d	< |d< t|ƒ}t|ƒ}t||ƒ}g }t|dd	ƒD ]"}||krq|||  nd}	||kr}|||  nd}
| t|	|
ƒ¡ qet|ƒ|d  |d	  |d	  }d| S )añ  FLOPs computation for matmul op.
    For matmul(input,other):
        input_shapes = [shape_of_input, shape_of_other]
        shape_of_input =                  [dim1,dim2 ...dim_n_1,dim_n]  length:n
        shape_of_other = [odim1,odim2 ... odim(n-m)... odim_m_1,dim_m]  length:m
        suppose n > m and dim_n = odim_m_1:
        shape_of_output = [dim1, dim2 ... max(dim(n-m), odim(n-m)), max(dim(n-m+1), odim(n-m+1)) ... dim_n_1, dim_m]
        equation: flops = 2 * numel(output) * dim_n
    r-   Úxr   r.   ÚyZtranspose_XZtranspose_xéþÿÿÿéÿÿÿÿZtranspose_YZtranspose_yr   r   ©ÚcopyÚdeepcopyr!   r    r/   r0   r%   r   ©r
   r   Zx_shapeZy_shaper1   r2   Z
output_lenZoutput_shaper*   Zx_idxZy_idxZmacsr   r   r   Ú_matmul_flopsì   s(   ÿÿ
 rN   Z	matmul_v2c                 C   s  t  |  d¡d ¡}t  |  d¡d ¡}| d¡r&|d |d |d< |d< | d¡r8|d |d |d< |d< t|ƒ}t|ƒ}t||ƒ}g }t|ddƒD ]"}||krY|||  nd	}	||kre|||  nd	}
| t|	|
ƒ¡ qMt|ƒ|d  |d  |d  }d| S )
aú  FLOPs computation for matmul_v2 op.
    For matmul_v2(input,other):
        input_shapes = [shape_of_input, shape_of_other]
        shape_of_input =                   [dim1, dim2 ...dim_n_1, dim_n] length:n
        shape_of_other = [odim1, odim2 ... odim(n-m) ... odim_m_1, dim_m] length:m
        suppose n > m and dim_n = odim_m_1:
        shape_of_output = [dim1, dim2 ... max(dim(n-m), odim(n-m)), max(dim(n-m+1), odim(n-m+1))...dim_n_1, dim_m]
        equation: flops = 2 * numel(outputs) * dim_n
    r-   r   r.   Ztrans_xrH   rI   Ztrans_yr   r   rJ   rM   r   r   r   Ú_matmul_v2_flops  s    


 rO   c                 C   ó   |   d¡d }t|ƒS )z®FLOPs computation for relu_like ops.
    For elu/leaky_relu/prelu/relu/relu6/silu (input):
        equation: flops = (numel)total number of elements in the input tensor.
    r-   r   r>   r?   r   r   r   Ú_relu_class_flops/  s   rQ   Zeluc                 C   r6   r   ©rQ   r   r   r   r   Ú
_elu_flops8  ó   
rS   Z
leaky_reluc                 C   r6   r   rR   r   r   r   r   Ú_leaky_relu_flops=  rT   rU   Zpreluc                 C   r6   r   rR   r   r   r   r   Ú_prelu_flopsB  rT   rV   Zreluc                 C   r6   r   rR   r   r   r   r   Ú_relu_flopsG  rT   rW   Zrelu6c                 C   r6   r   rR   r   r   r   r   Ú_relu6_flopsL  rT   rX   Zsiluc                 C   r6   r   rR   r   r   r   r   Ú_silu_flopsQ  rT   rY   Zreshape2c                 C   r   )z\FLOPs computation for reshape2 op.
    For reshape2(input):
        equation: flops = 0
    r   r   r   r   r   r   Ú_reshape2_flopsV  r   rZ   Zsoftmaxc                 C   r<   )z‘FLOPs computation for softmax op.
    For softmax(input):
        equation: flops = 3 * (numel)total number of elements in the input tensor.
    r-   r   é   r>   r?   r   r   r   Ú_softmax_flops_  rA   r\   Z
transpose2c                 C   r   )z`FLOPs computation for transpose2 op.
    For transpose2(input):
        equation: flops = 0
    r   r   r   r   r   r   Ú_transpose2_flopsi  r   r]   Úpoolc                 C   rP   )z‡FLOPs computation for pool op.
    For pool(input):
        equation: flops = (numel)total number of elements in the input tensor.
    r-   r   r>   r?   r   r   r   Ú_pool_flopsr  s   r_   )rK   r   r   ÚstrÚdictÚintr   r   r   r+   r,   r5   r8   r:   r;   r@   rD   rN   rO   rQ   rS   rU   rV   rW   rX   rY   rZ   r\   r]   r_   r   r   r   r   Ú<module>   s^   

N




	

$
	







	
