Ë
    –\;j€-  ã                   óp  — d dl Z i ad„ Zdedededefd„Zd„ Z ed	«      d
„ «       Z ed«      d„ «       Z	 ed«      d„ «       Z
d„ Z ed«      d„ «       Z ed«      d„ «       Z ed«      d„ «       Z ed«      d„ «       Z ed«      d„ «       Z ed«      d„ «       Z ed«      d„ «       Zd„ Z ed«      d „ «       Z ed!«      d"„ «       Z ed#«      d$„ «       Z ed%«      d&„ «       Z ed'«      d(„ «       Z ed)«      d*„ «       Z ed+«      d,„ «       Z ed-«      d.„ «       Z ed/«      d0„ «       Z ed1«      d2„ «       Zy)3é    Nc                 ó"   — d}| D ]  }||z  }Œ	 |S )Né   © )ÚsÚpÚvs      ú[G:\00. PROJECTS\API\Inventory\templateJSON\kerjaOCR\Lib\site-packages\paddle/utils/flops.pyÚprodr
      s   € Ø	€AÛˆØ	ˆQ‰‰ð à€Hó    Úop_typeÚinput_shapesÚattrsÚreturnc                 ój   — | t         vryt         |    }	  |||«      }|S # t        $ r
}Y d}~yd}~ww xY 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Úes         r	   r   r      sG   € ð Ô-Ñ-Øä& wÑ/ˆð	Ù˜ uÓ-ˆEð ˆøô ò 	Üûð	ús   ”	 Ÿ	2­2c                 ó   ‡ — ˆ fd„}|S )z<
    register flops computation function for operation.
    c                 ó   •— | t         ‰<   | S ©N)r   )r   r   s    €r	   Úregisterz register_flops.<locals>.register8   s   ø€ à+/Ô Ñ(Øˆr   r   )r   r   s   ` r	   Úregister_flopsr   3   s   ø€ ô
ð
 €Or   Úc_embeddingc                  ó   — y)zbFLOPs computation for c_embedding op.
    For c_embedding(input):
        equation: flops = 0
    r   r   ©r   r   s     r	   Ú_c_embedding_flopsr   @   ó   € ð r   Úconv2dc                 óL  — t        | j                  d«      «      dkD  r| j                  d«      d   nd}| j                  d«      d   }| j                  d«      d   }|j                  d«      }|j                  d«      }|j                  d«      }|j                  d	«      }|d   }	|d
   }
|d   }t        |dd «      }t        |dd «      }t        |«      }t        |t        «      r|n|g|z  }t        |t        «      r|n|g|z  }t        |t        «      r|n|g|z  }g }t	        |«      D ]<  \  }}|d||   z  z   ||   ||   d
z
  z  d
z   z
  ||   z  d
z   }|j                  |«       Œ> ||z  }t        |«      |
z  |z  }|	t        |«      z  }||z  }d|z  }d}|�||z  }||z   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
    ÚBiasr   NÚInputÚFilterÚpaddingsÚstridesÚ	dilationsÚgroupsr   é   )ÚlenÚgetÚlistÚ
isinstanceÚ	enumerateÚappendr
   )r   r   ÚbiasÚinputÚweightÚpaddingÚstrideÚdilationr(   Ú
batch_sizeÚin_channelsÚout_channelsÚkernel_dimsÚ
input_dimsÚlengthr%   r&   r'   Úoutput_dimsÚidxÚ	input_dimÚ
output_dimÚfilters_per_channelÚmacs_conv_per_positionÚactive_elementsÚoverall_conv_macsÚoverall_conv_flopsÚoverall_bias_flopss                               r	   Ú_conv2d_flopsrF   I   sF  € ô ˆ|×Ñ Ó'Ó(¨1Ò,ð 	×Ñ˜Ó  Ò#àð 	ð
 ×Ñ˜WÓ% aÑ(€EØ×Ñ˜hÓ'¨Ñ*€Fà�i‰i˜
Ó#€GØ�Y‰Y�yÓ!€FØ�y‰y˜Ó%€HØ�Y‰Y�xÓ €Fà�q‘€JØ˜‘(€KØ˜!‘9€LÜ�v˜a˜b�zÓ"€KÜ�e˜A˜B�i“€JÜ�‹_€Fô �gœtÔ$ñ 	ð ð
ð ñð ô �fœdÔ#ñ 	ð ð
ð ñð ô �h¤Ô%ñ 	ð ð
ð ñð ð €KÜ# JÖ/‰ˆˆYàØ�(˜3‘-Ññ à˜‰~ ¨SÑ!1°AÑ!5Ñ6¸Ñ:ñ<ð �S‰\ñ	ð ñ	ˆ
ð
 	×Ñ˜:Õ&ð 0ð '¨&Ñ0Ðäˆ[Ó˜KÑ'Ð*=Ñ=ð ð !¤4¨Ó#4Ñ4€OØ.°Ñ@ÐØÐ.Ñ.ÐàÐàÐØ)¨OÑ;ÐàÐ 2Ñ2Ð2r   Údropoutc                  ó   — y)zZFLOPs computation for dropout op.
    For dropout(input):
        equation: flops = 0
    r   r   r   s     r	   Ú_dropout_flopsrI   ˜   r   r   c                 óN  — | j                  d«      d   }| j                  d«      d   }t        |«      }t        |«      }t        ||«      }g }t        |«      D ]A  }||k  r||dz
  |z
     nd}	||k  r||dz
  |z
     nd}
|j	                  t        |	|
«      «       ŒC t        |«      S )NÚXr   ÚYr   )r+   r*   ÚmaxÚranger/   r
   )r   r   Úinput_xÚinput_yÚdim_xÚdim_yÚ
dim_outputÚoutputÚiÚin_xÚin_ys              r	   Ú_elementwise_flops_computerX   ¡   s°   € Ø×Ñ˜sÓ# AÑ&€GØ×Ñ˜sÓ# AÑ&€GÜ�‹L€EÜ�‹L€EÜ�U˜EÓ"€JØ€FÜ�:ÖˆØ)*¨Uªˆw�u˜q‘y 1‘}Ò%¸ˆØ)*¨Uªˆw�u˜q‘y 1‘}Ò%¸ˆØ�‰”c˜$ “oÕ&ð ô �‹<Ðr   Úelementwise_addc                 ó   — 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()...
    ©rX   r   s     r	   Ú_elementwise_add_flopsr\   ¯   ó   € ô & l°EÓ:Ð:r   Úelementwise_mulc                 ó   — t        | |«      S )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()...
    r[   r   s     r	   Ú_elementwise_mul_flopsr`   »   r]   r   Úelementwise_divc                 ó   — t        | |«      S )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()...
    r[   r   s     r	   Ú_elementwise_div_flopsrc   Ç   r]   r   Úgeluc                 óF   — | j                  d«      d   }t        |«      dz  S )z‹FLOPs computation for gelu op.
    For gelu(input):
        equation: flops = 5 * (numel)total number of elements in the input tensor.
    rK   r   é   ©r+   r
   ©r   r   r1   s      r	   Ú_gelu_flopsri   Ó   ó'   € ð ×Ñ˜SÓ! !Ñ$€EÜ�‹;˜‰?Ðr   Ú
layer_normc                 óˆ   — | j                  d«      d   }t        |«      dz  }|j                  d«      r|t        |«      z  }|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.
    rK   r   é   Úepsilonrg   )r   r   r1   r   s       r	   Ú_layer_norm_flopsro   Ý   sE   € ð ×Ñ˜SÓ! !Ñ$€EÜ�‹K˜!‰O€EØ‡y�y�ÔØ”�e“ÑˆØ€Lr   Úmatmulc           	      óØ  — t        j                  | j                  d| j                  ddgg«      «      d   «      }t        j                  | j                  d| j                  ddgg«      «      d   «      }|j                  d«      s|j                  d«      r|d   |d	   c|d	<   |d<   |j                  d
«      s|j                  d«      r|d   |d	   c|d	<   |d<   t        |«      }t        |«      }t	        ||«      }g }t        |dd	«      D ];  }||k  r|||z
     nd}	||k  r|||z
     nd}
|j                  t	        |	|
«      «       Œ= t        |«      |d   z  |d	   z  |d	   z  }d|z  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
    rK   Úxr   rL   ÚyÚtranspose_XÚtranspose_xéþÿÿÿéÿÿÿÿÚtranspose_YÚtranspose_yr)   r   ©ÚcopyÚdeepcopyr+   r*   rM   rN   r/   r
   ©r   r   Úx_shapeÚy_shaperQ   rR   Ú
output_lenÚoutput_shaper=   Úx_idxÚy_idxÚmacss               r	   Ú_matmul_flopsr…   ì   s}  € ô �m‰mØ×Ñ˜˜l×.Ñ.¨s°a°S°EÓ:Ó;¸AÑ>ó€Gô �m‰mØ×Ñ˜˜l×.Ñ.¨s°a°S°EÓ:Ó;¸AÑ>ó€Gð ‡y�y�Ô 5§9¡9¨]Ô#;Ø#*¨2¡;°¸±Ð ˆ�‰�W˜R‘[à‡y�y�Ô 5§9¡9¨]Ô#;Ø#*¨2¡;°¸±Ð ˆ�‰�W˜R‘[Ü�‹L€EÜ�‹L€EÜ�U˜EÓ"€JØ€Lä�Z  BÖ'ˆØ(+¨uª�˜ ™Ò$¸!ˆØ(+¨uª�˜ ™Ò$¸!ˆØ×ÑœC  uÓ-Õ.ð (ô
 �Ó ¨¡Ñ+¨g°b©kÑ9¸GÀB¹KÑG€DØˆt‰8€Or   Ú	matmul_v2c                 óH  — t        j                  | j                  d«      d   «      }t        j                  | j                  d«      d   «      }|j                  d«      r|d   |d   c|d<   |d<   |j                  d«      r|d   |d   c|d<   |d<   t        |«      }t        |«      }t	        ||«      }g }t        |dd«      D ];  }||k  r|||z
     nd	}	||k  r|||z
     nd	}
|j                  t	        |	|
«      «       Œ= t        |«      |d   z  |d   z  |d   z  }d|z  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
    rK   r   rL   Útrans_xrv   rw   Útrans_yr)   r   rz   r}   s               r	   Ú_matmul_v2_flopsrŠ     s7  € ô �m‰m˜L×,Ñ,¨SÓ1°!Ñ4Ó5€GÜ�m‰m˜L×,Ñ,¨SÓ1°!Ñ4Ó5€GØ‡y�y�ÔØ#*¨2¡;°¸±Ð ˆ�‰�W˜R‘[Ø‡y�y�ÔØ#*¨2¡;°¸±Ð ˆ�‰�W˜R‘[Ü�‹L€EÜ�‹L€EÜ�U˜EÓ"€JØ€LÜ�Z  BÖ'ˆØ(+¨uª�˜ ™Ò$¸!ˆØ(+¨uª�˜ ™Ò$¸!ˆØ×ÑœC  uÓ-Õ.ð (ô
 �Ó ¨¡Ñ+¨g°b©kÑ9¸GÀB¹KÑG€DØˆt‰8€Or   c                 ó@   — | j                  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.
    rK   r   rg   rh   s      r	   Ú_relu_class_flopsrŒ   /  s#   € ð
 ×Ñ˜SÓ! !Ñ$€EÜ�‹;Ðr   Úeluc                 ó   — t        | |«      S r   ©rŒ   r   s     r	   Ú
_elu_flopsr�   8  ó   € ä˜\¨5Ó1Ð1r   Ú
leaky_reluc                 ó   — t        | |«      S r   r�   r   s     r	   Ú_leaky_relu_flopsr”   =  r‘   r   Úpreluc                 ó   — t        | |«      S r   r�   r   s     r	   Ú_prelu_flopsr—   B  r‘   r   Úreluc                 ó   — t        | |«      S r   r�   r   s     r	   Ú_relu_flopsrš   G  r‘   r   Úrelu6c                 ó   — t        | |«      S r   r�   r   s     r	   Ú_relu6_flopsr�   L  r‘   r   Úsiluc                 ó   — t        | |«      S r   r�   r   s     r	   Ú_silu_flopsr    Q  r‘   r   Úreshape2c                  ó   — y)z\FLOPs computation for reshape2 op.
    For reshape2(input):
        equation: flops = 0
    r   r   r   s     r	   Ú_reshape2_flopsr£   V  r   r   Úsoftmaxc                 óF   — | j                  d«      d   }t        |«      dz  S )z‘FLOPs computation for softmax op.
    For softmax(input):
        equation: flops = 3 * (numel)total number of elements in the input tensor.
    rK   r   é   rg   rh   s      r	   Ú_softmax_flopsr§   _  rj   r   Ú
transpose2c                  ó   — y)z`FLOPs computation for transpose2 op.
    For transpose2(input):
        equation: flops = 0
    r   r   r   s     r	   Ú_transpose2_flopsrª   i  r   r   Úpoolc                 ó@   — | j                  d«      d   }t        |«      S )z‡FLOPs computation for pool op.
    For pool(input):
        equation: flops = (numel)total number of elements in the input tensor.
    rK   r   rg   rh   s      r	   Ú_pool_flopsr­   r  s#   € ð ×Ñ˜SÓ! !Ñ$€EÜ�‹;Ðr   )r{   r   r
   ÚstrÚdictÚintr   r   r   rF   rI   rX   r\   r`   rc   ri   ro   r…   rŠ   rŒ   r�   r”   r—   rš   r�   r    r£   r§   rª   r­   r   r   r	   Ú<module>r±      s@  ðó àÐ òð�3ð  dð °4ð ¸Có ò0
ñ �Óñó ðñ �ÓñK3ó ðK3ñ\ �	Óñó ðòñ Ð!Ó"ñ;ó #ð;ñ Ð!Ó"ñ;ó #ð;ñ Ð!Ó"ñ;ó #ð;ñ �Óñó ðñ �Óñó ðñ �Óñ!ó ð!ñH �Óñó ðò:ñ �Óñ2ó ð2ñ �Óñ2ó ð2ñ �Óñ2ó ð2ñ �Óñ2ó ð2ñ �Óñ2ó ð2ñ �Óñ2ó ð2ñ �
Óñó ðñ �	Óñó ðñ �Óñó ðñ �Óñó ñr   