o
    ®Ÿ?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ÚMatMulZConv2DZDepthwiseConv2dNativeZBiasAddZ
Dilation2Dc                 C   s   ~ ~t  dd¡S )zReturns zero flops.Úflopsr   )r   ÚOpStats©ÚgraphÚnode© r/   ús/home/www/facesmatcher.com/pyenv/lib/python3.10/site-packages/tensorflow/python/profiler/internal/flops_registry.pyÚ_zero_flops,   s   r1   c                 C   s   d}| D ]}||9 }q|S )z(Computes product of element of the list.é   r/   )ÚlstÚresultÚitemr/   r/   r0   Ú_list_product2   s   
r6   r2   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)r-   r.   Úops_per_elementÚin_shaper/   r/   r0   Ú_unary_op_flops>   s   r>   r   r*   c                 C   ó
   t | |ƒS )z'Compute flops for Reciprocal operation.©r>   r,   r/   r/   r0   Ú_reciprocal_flopsE   ó   
rA   r   c                 C   r?   )z#Compute flops for Square operation.r@   r,   r/   r/   r0   Ú_square_flopsK   rB   rC   r   c                 C   ó   t | |ddS )z"Compute flops for Rsqrt operation.é   ©r<   r@   r,   r/   r/   r0   Ú_rsqrt_flopsQ   s   rG   r   c                 C   r?   )z Compute flops for Log operation.r@   r,   r/   r/   r0   Ú
_log_flopsX   rB   rH   r   c                 C   r?   )z Compute flops for Neg operation.r@   r,   r/   r/   r0   Ú
_neg_flops^   rB   rI   r	   c                 C   r?   )z&Compute flops for AssignSub operation.r@   r,   r/   r/   r0   Ú_assign_sub_flopsd   rB   rJ   r
   c                 C   r?   )z&Compute flops for AssignAdd operation.r@   r,   r/   r/   r0   Ú_assign_add_flopsj   rB   rK   r   c                 C   s2   t  | |jd ¡}| ¡  t d| ¡ d d ¡S )z#Compute flops for L2Loss operation.r   r*   é   r2   r7   ©r-   r.   r=   r/   r/   r0   Ú_l2_loss_flopsp   s   rN   r   c                 C   rD   )z$Compute flops for Softmax operation.é   rF   r@   r,   r/   r/   r0   Ú_softmax_flopsz   s   	rP   c                 C   s*   t  | |j¡}| ¡  t d| ¡ | ¡S )z6Common code which compute flops for binary operations.r*   )r   r8   Únamer:   r   r+   r;   )r-   r.   r<   Ú	out_shaper/   r/   r0   Ú_binary_per_element_op_flopsŠ   s   rS   r   r   c                 C   r?   )z Compute flops for Add operation.©rS   r,   r/   r/   r0   Ú
_add_flops‘   s   
rU   r   c                 C   r?   )z Compute flops for Sub operation.rT   r,   r/   r/   r0   Ú
_sub_flops˜   rB   rV   r   c                 C   r?   )z Compute flops for Mul operation.rT   r,   r/   r/   r0   Ú
_mul_flopsž   rB   rW   r   c                 C   r?   )z$Compute flops for RealDiv operation.rT   r,   r/   r/   r0   Ú_real_div_flops¤   rB   rX   r   c                 C   r?   )z$Compute flops for Maximum operation.rT   r,   r/   r/   r0   Ú_maximum_flopsª   rB   rY   r   c                 C   r?   )z$Compute flops for Minimum operation.rT   r,   r/   r/   r0   Ú_minimum_flops°   rB   rZ   r   c                 C   r?   )z Compute flops for Pow operation.rT   r,   r/   r/   r0   Ú
_pow_flops¶   rB   r[   r   c                 C   rD   )z&Compute flops for RsqrtGrad operation.é   rF   rT   r,   r/   r/   r0   Ú_rsqrt_grad_flops¼   ó   r]   r   c                 C   r?   )z)Compute flops for GreaterEqual operation.rT   r,   r/   r/   r0   Ú_greater_equal_flopsÂ   rB   r_   r   c                 C   r?   )z$Compute flops for Greater operation.rT   r,   r/   r/   r0   Ú_greater_flopsÈ   rB   r`   r   c                 C   r?   )z&Compute flops for LessEqual operation.rT   r,   r/   r/   r0   Ú_less_equal_flopsÎ   rB   ra   r   c                 C   r?   )z!Compute flops for Less operation.rT   r,   r/   r/   r0   Ú_less_flopsÔ   rB   rb   r   c                 C   r?   )z"Compute flops for Equal operation.rT   r,   r/   r/   r0   Ú_equal_flopsÚ   rB   rc   r   c                 C   r?   )z%Compute flops for NotEqual operation.rT   r,   r/   r/   r0   Ú_not_equal_flopsà   rB   rd   r   c                 C   rD   )z.Compute flops for SquaredDifference operation.rE   rF   rT   r,   r/   r/   r0   Ú_squared_difference_flopsæ   r^   re   c                 C   sX   t  | |jd ¡}| ¡  t  | |j¡}| ¡  | ¡ | | ¡ ||   }t d|¡S )z9Common code which compute flops for reduction operations.r   r*   )r   r8   r9   r:   rQ   r;   r   r+   )r-   r.   Úreduce_flopsÚfinalize_flopsr=   rR   Z	num_flopsr/   r/   r0   Ú_reduction_op_flopsð   s   
ÿrh   r   c                 C   s   t | |dddS )z!Compute flops for Mean operation.r2   ©rf   rg   ©rh   r,   r/   r/   r0   Ú_mean_flopsû   ó   rk   r   c                 C   ó   t | |dddS )z Compute flops for Sum operation.r2   r   ri   rj   r,   r/   r/   r0   Ú
_sum_flops  rl   rn   r   c                 C   rm   )z#Compute flops for ArgMax operation.r2   r   ri   rj   r,   r/   r/   r0   Ú_arg_max_flops	  rl   ro   r    c                 C   rm   )z#Compute flops for ArgMin operation.r2   r   ri   rj   r,   r/   r/   r0   Ú_arg_min_flops  rl   rp   r!   c                 C   rm   )z(Compute flops for BiasAddGrad operation.r2   r   ri   rj   r,   r/   r/   r0   Ú_bias_add_grad_flops  s   rq   c                 C   s   | j d jdkrtdƒ‚dS )z>Verifies data format for pooling and convolutional operations.Zdata_formats   NHWCz3Only NHWC format is supported in flops computationsN)ÚattrÚsÚ
ValueError)r.   r/   r/   r0   Ú_verify_conv_data_format$  s   ÿru   c                 C   sL   t |ƒ t | |j¡}| ¡  t|jd jjƒ}t|ƒ}t	 
d|| ¡  ¡S )z7Common code which compute flops for pooling operations.Úksizer*   )ru   r   r8   rQ   r:   Úlistrr   Úir6   r   r+   r;   )r-   r.   rR   Úkernel_shapeÚkernel_arear/   r/   r0   Ú_pool_flops+  s   r{   r"   c                 C   r?   )z$Compute flops for AvgPool operation.©r{   r,   r/   r/   r0   Ú_avg_pool_flopsC  rB   r}   r#   c                 C   r?   )z$Compute flops for MaxPool operation.r|   r,   r/   r/   r0   Ú_max_pool_flopsI  rB   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.r2   rv   r*   rE   )ru   r   r8   r9   r:   rw   rr   rx   r6   r   r+   r;   )r-   r.   Zout_backprop_shapery   rz   r/   r/   r0   Ú_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.rv   r2   r*   )ru   rw   rr   rx   r6   r   r8   r9   r:   r;   r   r+   )r-   r.   ry   rz   Zorig_out_shapeZmax_pool_opsr/   r/   r0   Ú_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.r2   ÚstridesrE   r*   éÿÿÿÿ)ru   r   r8   rQ   r:   r9   rw   rr   rx   r   r+   r;   ÚdimsÚvalue)r-   r.   rR   ry   Ústrides_shapeÚstrides_productr/   r/   r0   Ú_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   r2   rE   r*   r‚   )ru   r   r8   r9   r:   rQ   rw   rr   rx   r   r+   r;   rƒ   r„   )r-   r.   Zimage_shapery   r…   r†   r/   r/   r0   Ú_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*   r2   )	r9   r1   r   r8   r:   r   r+   r;   ÚlenrM   r/   r/   r0   Ú_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   r2   r*   rE   ©rr   Úbr   r8   r9   r:   ÚintrQ   ÚnpÚprodÚas_listr   r+   ©r-   r.   r‹   Za_shapeÚkZoutput_shapeZoutput_countr/   r/   r0   Ú_calc_mat_mul_flopsÁ  s   r”   ZBatchMatMulZBatchMatMulV2Z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*   rE   rŒ   r’   r/   r/   r0   Ú_calc_batch_mat_mul_flopsÑ  s   r–   )r2   )r2   r   )6Ú__doc__Únumpyr   Ztensorflow.python.frameworkr   r   ÚsetZIMPLEMENTED_OPSr1   r6   r>   ZRegisterStatisticsrA   rC   rG   rH   rI   rJ   rK   rN   rP   rS   rU   rV   rW   rX   rY   rZ   r[   r]   r_   r`   ra   rb   rc   rd   re   rh   rk   rn   ro   rp   rq   ru   r{   r}   r~   r   r€   r‡   rˆ   rŠ   r”   r–   r/   r/   r/   r0   Ú<module>   s¶   

















	
































	
























	



