o
    yqikV                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ d dlm	Z	 d dl
Zd dlmZ ddlmZ ddlmZmZmZmZ dZg ad	ad
gZd ad adaG dd dZdd Zdd ZesberieddZdS ed	dZdS )    N)Path)GeneratorType)PrettyTable   )logging)INFER_BENCHMARKINFER_BENCHMARK_OUTPUT_DIR"INFER_BENCHMARK_USE_CACHE_FOR_READPIPELINE_BENCHMARKZ_entry_point_FZinintial_predictorc                   @   s   e Zd Zdd Zd1ddZdd Zd	d
 Zdd Zdd Zdd Z	dd Z
dd Zedd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 ZdS )2	Benchmarkc                 C   s(   || _ i | _d| _g | _g | _g | _d S NF)_enabled_elapses_warmup_detail_list_summary_list_operation_list)selfenabled r   h/home/app/PaddleOCR-VL/.venv_paddleocr/lib/python3.10/site-packages/paddlex/inference/utils/benchmark.py__init__1   s   
zBenchmark.__init__NFc                    s    fdd}|S )Nc              
      sV  j s| S d u r| jt| trt| dst| j nt| s"t|  zt	 }t
 d }| d| W n! ttfyZ } zt jtd|  W Y d }~nd }~ww o^t}|r~t rjtdtjdd  t  fdd	}n!trt  fd
d	}ntrt  fdd	}t| tr|| _| S |S )N__call__   :z6Benchmark: failed to get source file and line number: zSWhen `is_read_operation` is `True`, the wrapped function should not be a generator.   )maxsizec                     s@   t dd | D } dd | D } | i |}t|}|S )Nc                 s   s&    | ]}t |trt|n|V  qd S N
isinstancelisttuple).0argr   r   r   	<genexpr>a   s    
zQBenchmark.timeit_with_options.<locals>._deco.<locals>._wrapper.<locals>.<genexpr>c                 S   s(   i | ]\}}|t |trt|n|qS r   r   r"   kvr   r   r   
<dictcomp>d   s    zRBenchmark.timeit_with_options.<locals>._deco.<locals>._wrapper.<locals>.<dictcomp>)r!   itemscopydeepcopy)argskwargsoutput)funcr   r   _wrapper_   s   
z>Benchmark.timeit_with_options.<locals>._deco.<locals>._wrapperc               
      s    d }t rtd|t dsda t }z | i |}W |t ds.da n|t ds9da w t|trE||S 	t | | |S )N@cNested calls detected: Check the timed modules and exclude nested calls to prevent double-counting.TF)
_is_measuring_timeRuntimeError
startswithENTRY_POINT_NAMEtimeperf_counterr   r   watch_generator_updater,   r-   operation_nameticr.   r/   locationnamer   r   r   r0   o   s&   
c               
      s   t d7 a td7 atdkr<td u r d an$t d kr<tdtdd  dtdd  d d d	t  dt d d }t } | i |}t|tr_	||S 
t | | td8 a|S )Nr   r1   z:Multiple top-level function calls detected:
  Function 1: r   z
    Location: z
  Function 2: zx
Only one top-level function can be tracked at a time.
Please call 'benchmark.reset()' between top-level function calls.)_step_level	_top_funcr4   splitr7   r8   r   r   watch_generator_simpler:   r;   r>   r   r   r0      s2   

)r   __qualname__r   typehasattr	TypeErrorr   callableinspectgetsourcefilegetsourcelinesOSErroruuiduuid4hexr   debugr	   isgeneratorfunctionr4   	functools	lru_cachewrapsr   r
   )func_or_clsZsource_fileZsource_lineeZ	use_cacher0   is_read_operationr@   r   )r/   r?   r   _deco;   sR   





 z,Benchmark.timeit_with_options.<locals>._decor   )r   r@   rZ   r[   r   rY   r   timeit_with_options9   s   szBenchmark.timeit_with_optionsc                 C   s   |   |S r   )r\   )r   rW   r   r   r   timeit      zBenchmark.timeitc                 C   s   | d S )N_)r5   )r   r@   r   r   r   _is_public_method   r^   zBenchmark._is_public_methodc                 C   sN   |j  D ]\}}t|r$| |r$|ds$|tvr$t||| | q|S )N__)__dict__r)   rJ   r`   r5   PIPELINE_FUNC_BLACK_LISTsetattrr]   )r   clsr@   r/   r   r   r   time_methods   s   zBenchmark.time_methodsc                        t   fdd}| S )Nc               
   3   s    	 z?t r	tdt dsda t } zt }W t ds'da nt ds2da w t |   |V  W n
 tyK   Y d S w q)NTr2   r1   F)	r3   r4   r5   r6   r7   r8   nextr:   StopIterationr=   item	generatorr@   r   r   r   wrapper   s.   

z*Benchmark.watch_generator.<locals>.wrapperrT   rV   r   rm   r@   rn   r   rl   r   r9      s   zBenchmark.watch_generatorc                    rg   )Nc                  3   sb    z*	 t  } zt }W n	 ty   Y nw t  |   |V  qW td8 ad S td8 aw )NTr   )r7   r8   rh   ri   r:   rB   rj   rl   r   r   rn      s   z1Benchmark.watch_generator_simple.<locals>.wrapperro   rp   r   rl   r   rE      s   z Benchmark.watch_generator_simplec                 C   s(   da dad ai | _g | _g | _g | _d S Nr   )rA   rB   rC   r   r   r   r   r   r   r   r   reset   s   
zBenchmark.resetc                 C   s6   |d }|| j v r| j | | d S |g| j |< d S )Ni  )r   append)r   Zelapser@   r   r   r   r:      s   
zBenchmark._updatec                 C   s   | j S r   )r   rr   r   r   r   logs   s   zBenchmark.logsc                 C   
   d| _ d S NTr   rr   r   r   r   start_timing     
zBenchmark.start_timingc                 C   rv   r   rx   rr   r   r   r   stop_timing  rz   zBenchmark.stop_timingc                 C   rv   rw   )r   rr   r   r   r   start_warmup	  rz   zBenchmark.start_warmupc                 C   s   d| _ |   d S r   )r   rs   rr   r   r   r   stop_warmup  s   zBenchmark.stop_warmupc                 C   s  dd | j  D }dddd}|D ]}|t dr#||} nqt|}|| }t||d< g }g }	d}
| D ]P\}}t||ksIJ t|}|dd }|dd }d	|vrbd
}|	||||||| f |		||f |t
v r|d  |7  < d}
q=||
  |7  < q=|d |d  |d  |d< |d |d  |d< |||d|d |d | f|||d|d |d | f|||d|d |d | f|||d|d |d | f|||d|d |d | f|||d|d |d | fg}|||	fS )Nc                 S   s   i | ]\}}||qS r   r   r%   r   r   r   r(     s    z$Benchmark.gather.<locals>.<dictcomp>r   )preprocessing	inferencepostprocessingr1   Z
end_to_endr~   r   r   Unknownr   r   coreotherZPreprocessingZ	InferenceZPostprocessingCoreOtherz
End-to-End)ru   r)   r5   r6   poplennpmeanrD   rt   _inference_operations)r   
batch_sizeru   summarykeyZbase_predictor_time_listZitersZ	instancesdetail_listoperation_listZop_tagr@   	time_listZavgr<   r?   summary_listr   r   r   gather  s   


	
	
	
	
	

3zBenchmark.gatherc                 C   sD  |  |\}}}| jr:g d}t|}dd |D }|| dtt|dd d}t	| t	| d S dd	g}t|}|| d
tt|dd d}t	| t	| g d}	t|	}dd |D }|| dtt|dd d}t	| t	| g d}t|}dd |D }|| dtt|dd d}t	| t	| t
r tt
}
|
jddd |	g|}tt|
d ddd}t|}|| W d    n1 sw   Y  |g|}tt|
d ddd}t|}|| W d    d S 1 sw   Y  d S d S )N)Iters
Batch Size	InstancesTypeAvg Time Per Iter (ms)Avg Time Per Instance (ms)c                 S   0   g | ]}|d d |d d|d df qS N   z.8f   r   r"   ir   r   r   
<listcomp>      $z%Benchmark.collect.<locals>.<listcomp>zWarmup Data
r    	OperationSource Code LocationOperation Info)r   r   r   r   r   r   c                 S   r   r   r   r   r   r   r   r     s   0 Detail Datac                 S   r   r   r   r   r   r   r   r     r   Summary DataTparentsexist_ok
detail.csvw newlinesummary.csv)r   r   r   add_rowscenterr   strrD   r   infor   r   mkdiropencsvwriter	writerows)r   r   r   r   r   summary_headtabletable_titleoperation_headdetail_headsave_dircsv_datafiler   r   r   r   collectw  sb   














$zBenchmark.collectc              	      sP  g  g }t  }g }d}d}| j D ]U\}}t|}|d}	t|	d }
t|	d }|	d }|	d }d|vr:d}|||f t||}|dkrQ|d7 }|}ndt|d  d	 | } 	|
||||f qt
|} jd
d d tt | }t|D ]*} | d }
 | d }t fddt|t |D | }|	|
||g qdd t|D }t D ]l\}}|d }
|d }|d }|d }| |d  d kr||d  	 |d  d  t||k r|	t|d i g t||k s||d  d |d d u r|g||d  d |< q||d  d | 	| qg }tt|D ]u}|| d }|| d }d}g }t| D ]#\}\}}t|| }|	|| dkr]|nd||g ||7 }qE|dkr|	g d |	|dt|| | g |	dd|g |	ddt|| | | g ||7 }q-|||fS )Nr   r1   r      r   r   r   z    z-> c                 S   s   | d S rq   r   )xr   r   r   <lambda>  s    z+Benchmark.gather_pipeline.<locals>.<lambda>)r   c                    s   g | ]} | d  qS )r   r   )r"   posZ	info_listr   r   r     s    z-Benchmark.gather_pipeline.<locals>.<listcomp>c                 S   s   g | ]}d gqS )r   r   )r"   r_   r   r   r   r     s    r   g        r   )r   r   r   ZLayerr   r   )setru   r)   r   sumrD   intaddmaxrt   r    sortr   range	enumerateget)r   r   r   r   Z	max_levelZloop_numr@   r   Zop_timepartssteplevelr<   r?   Zformat_operation_nameZstep_numidxZlevel_time_listr   Znew_summary_listr   Zop_dictZops_all_timeZop_info_listZop_all_timer   r   r   gather_pipeline  s   


"

zBenchmark.gather_pipelinec                 C   s.   | j r	| jr	| js|  \| _| _| _ d S d S r   )r   r   r   r   rr   r   r   r   _initialize_pipeline_data  s   z#Benchmark._initialize_pipeline_datac                 C   s$   |    |   |   |   d S r   )r   print_operation_infoprint_detail_dataprint_summary_datarr   r   r   r   print_pipeline_data#  s   zBenchmark.print_pipeline_datac                 C   sZ   |    ddg}t|}|| j dtt|dd d}t	| t	| d S )Nr   r   r   r   r   r   )
r   r   r   r   r   r   r   rD   r   r   )r   r   r   r   r   r   r   r   )  s   
zBenchmark.print_operation_infoc                 C   n   |    g d}t|}|| j dtt|dd d}d|jd< d|jd< t	
| t	
| d S )	NZStepr   	Time (ms)r   r   r   r   lr   r   )r   r   r   r   r   r   r   rD   alignr   r   )r   r   r   r   r   r   r   r   5     


zBenchmark.print_detail_datac                 C   r   )	NZLevelr   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   rD   r   r   r   )r   r   r   r   r   r   r   r   D  r   zBenchmark.print_summary_datac                 C   s   |    t|}|jddd g d}|g| j}tt|d ddd}t|}|| W d    n1 s9w   Y  g d}|g| j}tt|d	 ddd}t|}|| W d    d S 1 siw   Y  d S )
NTr   r   r   r   r   r   r   r   )	r   r   r   r   r   r   r   r   r   )r   Z	save_pathr   r   r   r   r   r   r   r   r   save_pipeline_dataS  s   

"zBenchmark.save_pipeline_datar   )__name__
__module__rF   r   r\   r]   r`   rf   r9   rE   rs   r:   propertyru   ry   r{   r|   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   0   s2    
w
gMYr   c                   C   s   t S r   r   r   r   r   r   get_inference_operationsm  s   r   c                 C   s   | a d S r   r   )valr   r   r   set_inference_operationsq  s   r   T)r   ) r*   r   rT   rK   r7   rO   pathlibr   typesr   numpyr   Zprettytabler   utilsr   Zutils.flagsr   r   r	   r
   r6   r   r3   rc   rA   rB   rC   r   r   r   Z	benchmarkr   r   r   r   <module>   s:       A