o
    »i eFD  ã                   @   sŽ  d Z ddlZddlmZ ddlmZ eg d¢ƒZdd„ Zdd	„ Z	d‹dd„Z
e dd¡dd„ ƒZe dd¡dd„ ƒZe dd¡dd„ ƒZe dd¡dd„ ƒZe dd¡dd„ ƒZe dd¡dd„ ƒZe d d¡d!d"„ ƒZe d#d¡d$d%„ ƒZe d&d¡d'd(„ ƒZd‹d)d*„Ze d+d¡e d,d¡d-d.„ ƒƒZe d/d¡d0d1„ ƒZe d2d¡d3d4„ ƒZe d5d¡d6d7„ ƒZe d8d¡d9d:„ ƒZe d;d¡d<d=„ ƒZe d>d¡d?d@„ ƒZe dAd¡dBdC„ ƒZe dDd¡dEdF„ ƒZe dGd¡dHdI„ ƒZe dJd¡dKdL„ ƒZ e dMd¡dNdO„ ƒZ!e dPd¡dQdR„ ƒZ"e dSd¡dTdU„ ƒZ#e dVd¡dWdX„ ƒZ$dŒdYdZ„Z%e d[d¡d\d]„ ƒZ&e d^d¡d_d`„ ƒZ'e dad¡dbdc„ ƒZ(e ddd¡dedf„ ƒZ)e dgd¡dhdi„ ƒZ*djdk„ Z+dldm„ Z,e dnd¡dodp„ ƒZ-e dqd¡drds„ ƒZ.e dtd¡dudv„ ƒZ/e dwd¡dxdy„ ƒZ0e dzd¡d{d|„ ƒZ1e d}d¡d~d„ ƒZ2e d€d¡dd‚„ ƒZ3e dƒd¡d„d…„ ƒZ4e d†d¡e d‡d¡e dˆd¡d‰dŠ„ ƒƒƒZ5dS )z=Register flops statistics for various TensorFlow operations.
é    N)Ú
graph_util)Úops)*Ú
ReciprocalÚSquareÚRsqrtÚLogÚNegÚ	AssignSubÚ	AssignAddÚL2LossÚSoftmaxÚAddÚSubÚMulÚRealDivÚMaximumÚMinimumÚPowÚ	RsqrtGradÚGreaterEqualÚGreaterÚ	LessEqualÚLessÚEqualÚNotEqualÚSquaredDifferenceÚAddV2ÚMeanÚSumÚArgMaxÚArgMinÚBiasAddGradÚAvgPoolÚMaxPoolÚAvgPoolGradÚMaxPoolGradÚConv2DBackpropInputÚConv2DBackpropFilterÚAddNÚMatMulÚConv2DÚDepthwiseConv2dNativeÚBiasAddÚ
Dilation2Dc                 C   s   ~ ~t  dd¡S )zReturns zero flops.Úflopsr   )r   ÚOpStats©ÚgraphÚnode© r3   úa/var/www/myenv/lib/python3.10/site-packages/tensorflow/python/profiler/internal/flops_registry.pyÚ_zero_flops,   s   r5   c                 C   s   d}| D ]}||9 }q|S )z(Computes product of element of the list.é   r3   )ÚlstÚresultÚitemr3   r3   r4   Ú_list_product2   s   
r:   r6   c                 C   s.   t  | |jd ¡}| ¡  t d| ¡ | ¡S )z5Common code which compute flops for unary operations.r   r.   ©r   Útensor_shape_from_node_def_nameÚinputÚassert_is_fully_definedr   r/   Únum_elements)r1   r2   Úops_per_elementÚin_shaper3   r3   r4   Ú_unary_op_flops>   s   rB   r   r.   c                 C   ó
   t | |ƒS )z'Compute flops for Reciprocal operation.©rB   r0   r3   r3   r4   Ú_reciprocal_flopsE   ó   
rE   r   c                 C   rC   )z#Compute flops for Square operation.rD   r0   r3   r3   r4   Ú_square_flopsK   rF   rG   r   c                 C   ó   t | |ddS )z"Compute flops for Rsqrt operation.é   ©r@   rD   r0   r3   r3   r4   Ú_rsqrt_flopsQ   s   rK   r   c                 C   rC   )z Compute flops for Log operation.rD   r0   r3   r3   r4   Ú
_log_flopsX   rF   rL   r   c                 C   rC   )z Compute flops for Neg operation.rD   r0   r3   r3   r4   Ú
_neg_flops^   rF   rM   r	   c                 C   rC   )z&Compute flops for AssignSub operation.rD   r0   r3   r3   r4   Ú_assign_sub_flopsd   rF   rN   r
   c                 C   rC   )z&Compute flops for AssignAdd operation.rD   r0   r3   r3   r4   Ú_assign_add_flopsj   rF   rO   r   c                 C   s2   t  | |jd ¡}| ¡  t d| ¡ d d ¡S )z#Compute flops for L2Loss operation.r   r.   é   r6   r;   ©r1   r2   rA   r3   r3   r4   Ú_l2_loss_flopsp   s   rR   r   c                 C   rH   )z$Compute flops for Softmax operation.é   rJ   rD   r0   r3   r3   r4   Ú_softmax_flopsz   s   	rT   c                 C   s*   t  | |j¡}| ¡  t d| ¡ | ¡S )z6Common code which compute flops for binary operations.r.   )r   r<   Únamer>   r   r/   r?   )r1   r2   r@   Ú	out_shaper3   r3   r4   Ú_binary_per_element_op_flopsŠ   s   rW   r   r   c                 C   rC   )z Compute flops for Add operation.©rW   r0   r3   r3   r4   Ú
_add_flops‘   s   
rY   r   c                 C   rC   )z Compute flops for Sub operation.rX   r0   r3   r3   r4   Ú
_sub_flops˜   rF   rZ   r   c                 C   rC   )z Compute flops for Mul operation.rX   r0   r3   r3   r4   Ú
_mul_flopsž   rF   r[   r   c                 C   rC   )z$Compute flops for RealDiv operation.rX   r0   r3   r3   r4   Ú_real_div_flops¤   rF   r\   r   c                 C   rC   )z$Compute flops for Maximum operation.rX   r0   r3   r3   r4   Ú_maximum_flopsª   rF   r]   r   c                 C   rC   )z$Compute flops for Minimum operation.rX   r0   r3   r3   r4   Ú_minimum_flops°   rF   r^   r   c                 C   rC   )z Compute flops for Pow operation.rX   r0   r3   r3   r4   Ú
_pow_flops¶   rF   r_   r   c                 C   rH   )z&Compute flops for RsqrtGrad operation.é   rJ   rX   r0   r3   r3   r4   Ú_rsqrt_grad_flops¼   ó   ra   r   c                 C   rC   )z)Compute flops for GreaterEqual operation.rX   r0   r3   r3   r4   Ú_greater_equal_flopsÂ   rF   rc   r   c                 C   rC   )z$Compute flops for Greater operation.rX   r0   r3   r3   r4   Ú_greater_flopsÈ   rF   rd   r   c                 C   rC   )z&Compute flops for LessEqual operation.rX   r0   r3   r3   r4   Ú_less_equal_flopsÎ   rF   re   r   c                 C   rC   )z!Compute flops for Less operation.rX   r0   r3   r3   r4   Ú_less_flopsÔ   rF   rf   r   c                 C   rC   )z"Compute flops for Equal operation.rX   r0   r3   r3   r4   Ú_equal_flopsÚ   rF   rg   r   c                 C   rC   )z%Compute flops for NotEqual operation.rX   r0   r3   r3   r4   Ú_not_equal_flopsà   rF   rh   r   c                 C   rH   )z.Compute flops for SquaredDifference operation.rI   rJ   rX   r0   r3   r3   r4   Ú_squared_difference_flopsæ   rb   ri   c                 C   sX   t  | |jd ¡}| ¡  t  | |j¡}| ¡  | ¡ | | ¡ ||   }t d|¡S )z9Common code which compute flops for reduction operations.r   r.   )r   r<   r=   r>   rU   r?   r   r/   )r1   r2   Úreduce_flopsÚfinalize_flopsrA   rV   Ú	num_flopsr3   r3   r4   Ú_reduction_op_flopsð   s   
ÿrm   r   c                 C   s   t | |dddS )z!Compute flops for Mean operation.r6   ©rj   rk   ©rm   r0   r3   r3   r4   Ú_mean_flopsû   ó   rp   r   c                 C   ó   t | |dddS )z Compute flops for Sum operation.r6   r   rn   ro   r0   r3   r3   r4   Ú
_sum_flops  rq   rs   r   c                 C   rr   )z#Compute flops for ArgMax operation.r6   r   rn   ro   r0   r3   r3   r4   Ú_arg_max_flops	  rq   rt   r    c                 C   rr   )z#Compute flops for ArgMin operation.r6   r   rn   ro   r0   r3   r3   r4   Ú_arg_min_flops  rq   ru   r!   c                 C   rr   )z(Compute flops for BiasAddGrad operation.r6   r   rn   ro   r0   r3   r3   r4   Ú_bias_add_grad_flops  s   rv   c                 C   s   | j d jdkrtdƒ‚dS )z>Verifies data format for pooling and convolutional operations.Údata_formats   NHWCz3Only NHWC format is supported in flops computationsN)ÚattrÚsÚ
ValueError)r2   r3   r3   r4   Ú_verify_conv_data_format$  s   ÿr{   c                 C   sL   t |ƒ t | |j¡}| ¡  t|jd jjƒ}t|ƒ}t	 
d|| ¡  ¡S )z7Common code which compute flops for pooling operations.Úksizer.   )r{   r   r<   rU   r>   Úlistrx   Úir:   r   r/   r?   )r1   r2   rV   Úkernel_shapeÚkernel_arear3   r3   r4   Ú_pool_flops+  s   r   r"   c                 C   rC   )z$Compute flops for AvgPool operation.©r   r0   r3   r3   r4   Ú_avg_pool_flopsC  rF   rƒ   r#   c                 C   rC   )z$Compute flops for MaxPool operation.r‚   r0   r3   r3   r4   Ú_max_pool_flopsI  rF   r„   r$   c                 C   sT   t |ƒ t | |jd ¡}| ¡  t|jd jjƒ}t|ƒ}t	 
d|| ¡  d ¡S )z(Compute flops for AvgPoolGrad operation.r6   r|   r.   rI   )r{   r   r<   r=   r>   r}   rx   r~   r:   r   r/   r?   )r1   r2   Úout_backprop_shaper   r€   r3   r3   r4   Ú_avg_pool_grad_flopsO  s   ÿÿr†   r%   c                 C   s\   t |ƒ t|jd jjƒ}t|ƒ}t | |jd ¡}| ¡  || 	¡  }t
 d|| 	¡  ¡S )z(Compute flops for MaxPoolGrad operation.r|   r6   r.   )r{   r}   rx   r~   r:   r   r<   r=   r>   r?   r   r/   )r1   r2   r   r€   Úorig_out_shapeÚmax_pool_opsr3   r3   r4   Ú_max_pool_grad_flops`  s   ÿr‰   r&   c                 C   s†   t |ƒ t | |j¡}| ¡  t | |jd ¡}| ¡  t|jd jjƒ}|d |d  }t	 
dd| ¡  | ¡  |jd j|  ¡S )z0Compute flops for Conv2DBackpropInput operation.r6   ÚstridesrI   r.   éÿÿÿÿ)r{   r   r<   rU   r>   r=   r}   rx   r~   r   r/   r?   ÚdimsÚvalue)r1   r2   rV   r   Ústrides_shapeÚstrides_productr3   r3   r4   Ú_conv_2d_backprop_input_flops|  s    ÿ
ÿþÿr   r'   c                 C   s†   t |ƒ t | |jd ¡}| ¡  t | |j¡}| ¡  t|jd jjƒ}|d |d  }t	 
dd| ¡  | ¡  |jd j|  ¡S )z1Compute flops for Conv2DBackpropFilter operation.r   rŠ   r6   rI   r.   r‹   )r{   r   r<   r=   r>   rU   r}   rx   r~   r   r/   r?   rŒ   r   )r1   r2   Úimage_shaper   rŽ   r   r3   r3   r4   Ú_conv_2d_backprop_filter_flopsœ  s   
ÿþÿr’   r(   c                 C   sH   |j st| |ƒS t | |j d ¡}| ¡  t d| ¡ t|j ƒd  ¡S )z!Compute flops for AddN operation.r   r.   r6   )	r=   r5   r   r<   r>   r   r/   r?   ÚlenrQ   r3   r3   r4   Ú_add_n_flops·  s
   
r”   r)   c                 C   s|   |j d j}t | |jd ¡}| ¡  |rt|d ƒ}nt|d ƒ}t | |j¡}| ¡  t 	| 
¡ ¡}t d|| d ¡S )z3Calculates the compute resources needed for MatMul.Útranspose_ar   r6   r.   rI   ©rx   Úbr   r<   r=   r>   ÚintrU   ÚnpÚprodÚas_listr   r/   ©r1   r2   r•   Úa_shapeÚkÚoutput_shapeÚoutput_countr3   r3   r4   Ú_calc_mat_mul_flopsÁ  s   r¡   ÚBatchMatMulÚBatchMatMulV2ÚBatchMatMulV3c                 C   s|   |j d j}t | |jd ¡}| ¡  |rt|d ƒ}nt|d ƒ}t | |j¡}| ¡  t 	| 
¡ ¡}t d|| d ¡S )z8Calculates the compute resources needed for BatchMatMul.r•   r   éþÿÿÿr‹   r.   rI   r–   rœ   r3   r3   r4   Ú_calc_batch_mat_mul_flopsÑ  s   r¦   )r6   )r6   r   )6Ú__doc__Únumpyr™   Útensorflow.python.frameworkr   r   ÚsetÚIMPLEMENTED_OPSr5   r:   rB   ÚRegisterStatisticsrE   rG   rK   rL   rM   rN   rO   rR   rT   rW   rY   rZ   r[   r\   r]   r^   r_   ra   rc   rd   re   rf   rg   rh   ri   rm   rp   rs   rt   ru   rv   r{   r   rƒ   r„   r†   r‰   r   r’   r”   r¡   r¦   r3   r3   r3   r4   Ú<module>   s¶   

















	
































	
























	



