o
    Qe 9                     @   s   d dl Z d dlZd dlmZ G dd deZG dd deZG dd deZG d	d
 d
eZG dd deZ	G dd deZ
e
 Zdd ZdS )    N)OrderedDictc                   @   8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )Stackz
    The stack in a Last-In/First-Out (LIFO) manner. New element is added at
    the end and an element is removed from that end.
    c                 C   s
   g | _ d S N)itemsself r	   ED:\Projects\ConvertPro\env\Lib\site-packages\paddle/profiler/timer.py__init__      
zStack.__init__c                 C   s   | j | d S r   )r   append)r   itemr	   r	   r
   push   s   z
Stack.pushc                 C   
   | j  S r   )r   popr   r	   r	   r
   r       r   z	Stack.popc                 C   s   t | jdkS Nr   )lenr   r   r	   r	   r
   is_empty#   s   zStack.is_emptyc                 C   s    |   s| jt| jd  S d S N   )r   r   r   r   r	   r	   r
   peek&   s   z
Stack.peekN)	__name__
__module____qualname____doc__r   r   r   r   r   r	   r	   r	   r
   r      s    r   c                   @   sZ   e Zd ZdZdd Zdd Zdd Zdd	d
Zdd Zdd Z	dd Z
dd Zdd ZdS )Eventzt
    A Event is used to record the cost of every step and the cost of
    the total steps except skipped steps.
    c                 C   sx   t  | _t  | _d| _d| _d| _tdtddd| _tdtddd| _	tdtdd| _
d | _d| _d| _d| _d S )Nr   
   inf)maxmintotal)r   r    T	samples/s)TimeAveragerreader_cost_averagerbatch_cost_averagertotal_samplestotal_iters	skip_iterdictfloatreader_recordsbatch_recordsspeed_recordsreaderneed_record
speed_mode
speed_unitr   r	   r	   r
   r   3   s   
zEvent.__init__c                 C   s   | j   | j  d S r   )r$   resetr%   r   r	   r	   r
   r2   E      
zEvent.resetc                 C   s.   | j | | j| jkr| || j d S d S r   )r$   recordr'   r(   _update_recordsr+   )r   usetimer	   r	   r
   record_readerI   s   zEvent.record_readerNc                 C   s   |d u r
d| _ d| _| j|| |  jd7  _| j| jkrE| || j | j dkr8t|| }|  j	|7  _	nd| }| || j
 d S d S )Nsteps/sr   r"   g      ?)r0   r1   r%   r4   r'   r(   r5   r,   r*   r&   r-   )r   r6   num_samplesZcurrent_speedr	   r	   r
   record_batchN   s   
zEvent.record_batchc                 C   sN   ||d kr||d< n
||d k r||d< d|  v r%|d  |7  < d S d S )Nr   r    r!   )keys)r   Zcurrent_recordrecordsr	   r	   r
   r5   ^   s   
zEvent._update_recordsc                 C   r   r   )r$   get_averager   r	   r	   r
   reader_averagef   r   zEvent.reader_averagec                 C   r   r   )r%   r=   r   r	   r	   r
   batch_averagei   r   zEvent.batch_averagec                 C   s   | j dkr
| j S | j S )Nr"   )r0   r%   get_ips_averageget_step_averager   r	   r	   r
   speed_averagel   s   


zEvent.speed_averagec           	      C   s   | j | jkri S d}d}d}|  j | j8  _ | jd t| j  }| jd t| j  }| jdkr:t| j| jd  }n
t| j | jd  }t| jd | jd |d}t| jd | jd |d}t| jd | jd |d}|| d }t||||d}|S )	Nr   r!   r"   r   r    )r   r    avgd   )reader_summarybatch_summaryips_summaryreader_ratio)	r'   r(   r+   r*   r,   r0   r&   r)   r-   )	r   Z
reader_avgZ	batch_avgZ	speed_avgrE   rF   rG   rH   summaryr	   r	   r
   get_summaryr   s<   



zEvent.get_summaryr   )r   r   r   r   r   r2   r7   r:   r5   r>   r?   rB   rJ   r	   r	   r	   r
   r   -   s    
r   c                   @   r   )HookzG
    As the base class. All types of hooks should inherit from it.
    c                 C      d S r   r	   r   	benchmarkr	   r	   r
   begin      z
Hook.beginc                 C   rL   r   r	   rM   r	   r	   r
   end   rP   zHook.endc                 C   rL   r   r	   rM   r	   r	   r
   before_reader   rP   zHook.before_readerc                 C   rL   r   r	   rM   r	   r	   r
   after_reader   rP   zHook.after_readerc                 C   rL   r   r	   rM   r	   r	   r
   
after_step   rP   zHook.after_stepN)	r   r   r   r   rO   rQ   rR   rS   rT   r	   r	   r	   r
   rK      s    rK   c                   @   sP   e Zd Z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S )	TimerHookze
    A hook for recording real-time performance and the summary
    performance of total steps. 
    c                 C   s   t  | _t  | _d S r   )timeitdefault_timer
start_timestart_readerr   r	   r	   r
   r      r3   zTimerHook.__init__c                 C   s(   |j t  |j  |_t | _dS )z
        Create the event for timing and initialize the start time of a step.
        This function will be called in `Profiler.start()`.
        N)eventsr   r   r   current_eventrV   rW   rX   rM   r	   r	   r
   rO      s   zTimerHook.beginc                 C   s   t  | _dS )z
        Initialize the start time of the dataloader. This function will be
        called at the beginning of `next` method in `_DataLoaderIterMultiProcess` or
        `_DataLoaderIterSingleProcess`.

        N)rV   rW   rY   rM   r	   r	   r
   rR      s   zTimerHook.before_readerc                 C   s<   t  | j }|jdu s|jjr|dkrdS |j| dS )aX  
        Record the cost of dataloader for the current step. Since the skipped steps
        are 10, it will update the maximum, minimum and the total time from the step
        11 to the current step. This function will be called at the end of `next`
        method in `_DataLoaderIterMultiProcess` or `_DataLoaderIterSingleProcess`.

        Nr   )rV   rW   rY   r[   r/   r7   )r   rN   reader_costr	   r	   r
   rS      s   	
zTimerHook.after_readerc                 C   sB   |j du s	|j jsdS t | j }|j ||j t | _dS )a  
        Record the cost for the current step. It will contain the cost of the loading
        data if there is a dataloader. Similar to `after_reader`, it will also update
        the maximum, minimum and the total time from the step 11 to the current step
        as well as the maximum and minimum speed of the model. This function will
        be called in `Profiler.step()`.

        N)r[   r/   rV   rW   rX   r:   r9   )r   rN   
batch_costr	   r	   r
   rT      s   
zTimerHook.after_stepc                 C   s<   |j  rdS | | |j   |j  |_t | _dS )a&  
        Print the performance summary of the model and pop the current event
        from the events stack. Since there may be nested timing events, such
        as evaluation in the training process, the current event needs to be
        update to the event at the top of the stack.

        N)	rZ   r   _print_summaryr   r   r[   rV   rW   rX   rM   r	   r	   r
   rQ      s   
	

zTimerHook.endc                 C   s   |j  }|s	d S tddd |d dkr#tdd|d   d  td	|j j  td
ddd
ddd
ddd
ddd
	 |d d dkrS| d|d  | d|d  | d|d  d S )NzPerf SummaryrD   =rH   r   zReader Ratio: z%.3f%zTime Unit: s, IPS Unit: %s|    rC   r   r    rE   r\   r]   rF   ipsrG   )r[   rJ   printcenterr1   _print_stats)r   rN   rI   r	   r	   r
   r^      s    
 zTimerHook._print_summaryc                 C   sX   d|d  }d|d  }d|d  }t d|dd|dd|dd|dd	 d S )Nz%.5frC   r   r    ra   rc   )re   rf   )r   r   Zmessage_dictZavg_strZmax_strZmin_strr	   r	   r
   rg     s   zTimerHook._print_statsN)r   r   r   r   r   rO   rR   rS   rT   rQ   r^   rg   r	   r	   r	   r
   rU      s    

rU   c                   @   sB   e Zd ZdZdd Zdd ZdddZd	d
 Zdd Zdd Z	dS )r#   z>
    Record the cost of every step and count the average.
    c                 C   s   |    d S r   )r2   r   r	   r	   r
   r     s   zTimeAverager.__init__c                 C   s   d| _ d| _d| _d S r   _total_iters_total_time_total_samplesr   r	   r	   r
   r2     s   
zTimeAverager.resetNc                 C   s6   |  j d7  _ |  j|7  _|r|  j|7  _d S d S r   rh   )r   r6   r9   r	   r	   r
   r4     s
   zTimeAverager.recordc                 C   s   | j dkrdS | jt| j  S )zA
        Get the average cost of loading data or a step.
        r   )ri   rj   r*   r   r	   r	   r
   r=   #     
zTimeAverager.get_averagec                 C   s$   | j r| jdkr
dS t| j | j S )zL
        Get the average throughput when speed mode is "samples/s".
        r   )rk   ri   r*   rj   r   r	   r	   r
   r@   ,  s   zTimeAverager.get_ips_averagec                 C   s   | j dkrdS t| j | j S )zD
        Get the average speed when speed mode is "step/s".
        r   )ri   r*   rj   r   r	   r	   r
   rA   5  rl   zTimeAverager.get_step_averager   )
r   r   r   r   r   r2   r4   r=   r@   rA   r	   r	   r	   r
   r#     s    
		r#   c                   @   sZ   e Zd ZdZdd Zd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S )	Benchmarka  
    A tool for the statistics of model performance. The `before_reader`
    and `after_reader` are called in the DataLoader to count the cost
    of loading the data. The `begin`, `step` and `end` are called to
    count the cost of a step or total steps.
    c                 C   s&   d | _ tt d| _d | _t | _d S )N)
timer_hook)r9   r   rU   hooksr[   r   rZ   r   r	   r	   r
   r   G  s   zBenchmark.__init__Nc                 C   s   || _ |   dS )zl
        Record the statistic for the current step. It will be called in
        `Profiler.step()`.
        N)r9   rT   )r   r9   r	   r	   r
   stepM  s   zBenchmark.stepc                 C   s   d}| j  }| j  }|r|d| 7 }|r/| j jdkr!d| j _n|d | j _|dd|f 7 }| j  }|r@|d|| j jf 7 }| j   |S )z
        It returns the statistic of the current step as a string. It contains
        "reader_cost", "batch_cost" and "ips".
        rb   z reader_cost: %.5f sr8   z/sz %s: %.5f sr]   z ips: %.3f %s)r[   r>   r?   r0   r1   rB   r2   )r   unitmessager>   r?   rB   r	   r	   r
   	step_infoV  s"   




zBenchmark.step_infoc                 C      | j  D ]}||  qd S r   )ro   valuesrO   r   hookr	   r	   r
   rO   n     zBenchmark.beginc                 C   rt   r   )ro   ru   rR   rv   r	   r	   r
   rR   r  rx   zBenchmark.before_readerc                 C   rt   r   )ro   ru   rS   rv   r	   r	   r
   rS   v  rx   zBenchmark.after_readerc                 C   rt   r   )ro   ru   rT   rv   r	   r	   r
   rT   z  rx   zBenchmark.after_stepc                 C   rt   r   )ro   ru   rQ   rv   r	   r	   r
   rQ   ~  rx   zBenchmark.endc                 C   s   | j d u rd S | j jr+| j jd u r|| j _d S | j jjd |jd kr)d| j _d S d S | j jjd |jd krEd| j _t | jd _d S d S )NZ_datasetFTrn   )r[   r/   r.   __dict__rV   rW   ro   rX   )r   r.   r	   r	   r
   check_if_need_record  s(   
	zBenchmark.check_if_need_recordr   )r   r   r   r   r   rp   rs   rO   rR   rS   rT   rQ   rz   r	   r	   r	   r
   rm   ?  s    
	rm   c                   C   s   t S r   )_benchmark_r	   r	   r	   r
   rN     rP   rN   )rV   loggingcollectionsr   objectr   r   rK   rU   r#   rm   r{   rN   r	   r	   r	   r
   <module>   s   gg/Y