
    ёi8                         S SK r S SKJr   " S S5      r " S S5      r " S S5      r " S	 S
\5      r " S S5      r " S S5      r\" 5       r	S r
g)    N)OrderedDictc                   6    \ rS rSrSrS rS rS rS rS r	Sr
g	)
Stack   zz
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                     / U l         g Nitemsselfs    U/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/profiler/timer.py__init__Stack.__init__   s	    
    c                 :    U R                   R                  U5        g r   )r
   append)r   items     r   push
Stack.push   s    

$r   c                 6    U R                   R                  5       $ r   )r
   popr   s    r   r   	Stack.pop   s    zz~~r   c                 2    [        U R                  5      S:H  $ Nr   )lenr
   r   s    r   is_emptyStack.is_empty"   s    4::!##r   c                 x    U R                  5       (       d%  U R                  [        U R                  5      S-
     $ g N   )r   r
   r   r   s    r   peek
Stack.peek%   s,    }}::c$**o122r   r	   N)__name__
__module____qualname____firstlineno____doc__r   r   r   r   r!   __static_attributes__ r   r   r   r      s     
  $r   r   c                   R    \ rS rSrSrS rS rS rSS jrS r	S	 r
S
 rS rS rSrg)Event,   zh
A Event is used to record the cost of every step and the cost of
the total steps except skipped steps.
c                    [        5       U l        [        5       U l        SU l        SU l        SU l        S[        S5      SS.U l        S[        S5      SS.U l        S[        S5      S.U l	        S U l
        SU l        SU l        SU l        g )Nr   
   inf)maxmintotal)r0   r1   T	samples/s)TimeAveragerreader_cost_averagerbatch_cost_averagertotal_samplestotal_iters	skip_iterfloatreader_recordsbatch_recordsspeed_recordsreaderneed_record
speed_mode
speed_unitr   s    r   r   Event.__init__2   s    $0N!#/> &'eqI%&uU|aH%&uU|< & &r   c                 l    U R                   R                  5         U R                  R                  5         g r   )r5   resetr6   r   s    r   rD   Event.resetD   s&    !!'')  &&(r   c                     U R                   R                  U5        U R                  U R                  :  a  U R	                  XR
                  5        g g r   )r5   recordr8   r9   _update_recordsr;   )r   usetimes     r   record_readerEvent.record_readerH   sA    !!((1t~~-  *=*=> .r   Nc                    Uc  SU l         SU l        U R                  R                  X5        U =R                  S-  sl        U R                  U R
                  :  ap  U R                  XR                  5        U R                   S:X  a$  [        U5      U-  nU =R                  U-  sl	        OSU-  nU R                  X0R                  5        g g )Nsteps/sr    r3   g      ?)r@   rA   r6   rG   r8   r9   rH   r<   r:   r7   r=   )r   rI   num_samplescurrent_speeds       r   record_batchEvent.record_batchM   s    'DO'DO  ''=At~~-  *<*<=+- %k 2W <""k1" #g  0B0BC .r   c                 z    XS   :  a  XS'   OXS   :  a  XS'   SUR                  5       ;   a  US==   U-  ss'   g g )Nr0   r1   r2   )keys)r   current_recordrecordss      r   rH   Event._update_records]   sG    EN*+ENen,+ENglln$G. %r   c                 6    U R                   R                  5       $ r   )r5   get_averager   s    r   reader_averageEvent.reader_averagee   s    ((4466r   c                 6    U R                   R                  5       $ r   )r6   rX   r   s    r   batch_averageEvent.batch_averageh   s    ''3355r   c                     U R                   S:X  a  U R                  R                  5       $ U R                  R                  5       $ )Nr3   )r@   r6   get_ips_averageget_step_averager   s    r   speed_averageEvent.speed_averagek   s8    ??k)++;;==++<<>>r   c                    U R                   U R                  ::  a  0 $ SnSnSnU =R                   U R                  -  sl         U R                  S   [        U R                   5      -  nU R                  S   [        U R                   5      -  nU R
                  S:X  a&  [        U R                  5      U R                  S   -  nO%[        U R                   5      U R                  S   -  nU R                  S   U R                  S   US.nU R                  S   U R                  S   US.nU R                  S   U R                  S   US.nX-  S-  nUUUUS.nU$ )	Nr   r2   r3   r0   r1   )r0   r1   avgd   )reader_summarybatch_summaryips_summaryreader_ratio)r8   r9   r;   r:   r<   r@   r7   r=   )	r   
reader_avg	batch_avg	speed_avgrf   rg   rh   ri   summarys	            r   get_summaryEvent.get_summaryq   sj   t~~-I
		DNN*((1E$:J:J4KK
&&w/%8H8H2II	??k)d001D4F4Fw4OOId../$2D2DW2MMI &&u-&&u-
 %%e,%%e,
 %%e,%%e,

 #.#5,*&(	
 r   )r6   r<   r?   r>   r5   r;   r9   r@   r=   rA   r8   r7   r   )r#   r$   r%   r&   r'   r   rD   rJ   rP   rH   rY   r\   ra   rn   r(   r)   r   r   r+   r+   ,   s5    
&$)?
D /76?'r   r+   c                   6    \ rS rSrSrS rS rS rS rS r	Sr
g	)
Hook   z?
As the base class. All types of hooks should inherit from it.
c                     g r   r)   r   	benchmarks     r   begin
Hook.begin       r   c                     g r   r)   rt   s     r   endHook.end   rx   r   c                     g r   r)   rt   s     r   before_readerHook.before_reader   rx   r   c                     g r   r)   rt   s     r   after_readerHook.after_reader   rx   r   c                     g r   r)   rt   s     r   
after_stepHook.after_step   rx   r   r)   N)r#   r$   r%   r&   r'   rv   rz   r}   r   r   r(   r)   r   r   rq   rq      s     r   rq   c                   H    \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rSrg)	TimerHook   zX
A hook for recording real-time performance and the summary
performance of total steps.
c                 l    [         R                  " 5       U l        [         R                  " 5       U l        g r   )timeitdefault_timer
start_timestart_readerr   s    r   r   TimerHook.__init__   s#     ..0"002r   c                     UR                   R                  [        5       5        UR                   R                  5       Ul        [
        R                  " 5       U l        g)zz
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_eventr   r   r   rt   s     r   rv   TimerHook.begin   sA     	eg&"+"2"2"7"7"9	 ..0r   c                 8    [         R                  " 5       U l        g)z
Initialize the start time of the dataloader. This function will be
called at the beginning of `next` method in `_DataLoaderIterMultiProcess` or
`_DataLoaderIterSingleProcess`.

N)r   r   r   rt   s     r   r}   TimerHook.before_reader   s     #002r   c                     [         R                  " 5       U R                  -
  nUR                  b!  UR                  R                  (       a  US:X  a  gUR                  R                  U5        g)a0  
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   )r   r   r   r   r?   rJ   )r   ru   reader_costs      r   r   TimerHook.after_reader   sW     **,t/@/@@$$,++77q --k:r   c                    UR                   b  UR                   R                  (       d  g[        R                  " 5       U R                  -
  nUR                   R                  X!R                  5        [        R                  " 5       U l        g)aU  
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?   r   r   r   rP   rN   )r   ru   
batch_costs      r   r   TimerHook.after_step   sc     ##+''33))+doo=
,,Z9N9NO ..0r   c                    UR                   R                  5       (       a  gU R                  U5        UR                   R                  5         UR                   R	                  5       Ul        [        R                  " 5       U l        g)z
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)	r   r   _print_summaryr   r!   r   r   r   r   rt   s     r   rz   TimerHook.end   sc     $$&&I&"+"2"2"7"7"9	 ..0r   c                 N   UR                   R                  5       nU(       d  g [        SR                  SS5      5        US   S:w  a#  [        SSR	                  US   5      -   S-   5        [        S	UR                   R
                   35        [        S
SR                  S5      S
SR                  S5      S
SR                  S5      S
SR                  S5      S
5	        US   S   S:w  a  U R                  SUS   5        U R                  SUS   5        U R                  SUS   5        g )NzPerf Summaryre   =ri   r   zReader Ratio: z{:.3f}%zTime Unit: s, IPS Unit: |    rd   r0   r1   rf   r   r   rg   ipsrh   )r   rn   printcenterformatrA   _print_stats)r   ru   rm   s      r   r   TimerHook._print_summary  s!   ))557n##C-.>"a' //'."9:;
 	()@)@)K)K(LMNIIbMLLLLLL
	
 #$U+q0mW5E-FG,(@A%!78r   c                    SR                  US   5      nSR                  US   5      nSR                  US   5      n[        SUR                  S5      SUR                  S5      SUR                  S5      SUR                  S5      S5	        g )Nz{:.5f}rd   r0   r1   r   r   )r   r   r   )r   r   message_dictavg_strmax_strmin_strs         r   r   TimerHook._print_stats  s    //,u"56//,u"56//,u"56KKONN2NN2NN2
	
r   )r   r   N)r#   r$   r%   r&   r'   r   rv   r}   r   r   rz   r   r   r(   r)   r   r   r   r      s/    
313;$1$1 9:
r   r   c                   @    \ rS rSrSrS rS rSS jrS rS r	S	 r
S
rg)r4   i0  z6
Record the cost of every step and count the average.
c                 $    U R                  5         g r   )rD   r   s    r   r   TimeAverager.__init__5  s    

r   c                 .    SU l         SU l        SU l        g r   _total_iters_total_time_total_samplesr   s    r   rD   TimeAverager.reset8  s    r   Nc                     U =R                   S-  sl         U =R                  U-  sl        U(       a  U =R                  U-  sl        g g r   r   )r   rI   rN   s      r   rG   TimeAverager.record=  s=    QG#;. r   c                 h    U R                   S:X  a  gU R                  [        U R                   5      -  $ )z1
Get the average cost of loading data or a step.
r   )r   r   r:   r   s    r   rX   TimeAverager.get_averageC  s1    
 !%(9(9":::r   c                     U R                   (       a  U R                  S:X  a  g[        U R                   5      U R                  -  $ )z<
Get the average throughput when speed mode is "samples/s".
r   )r   r   r:   r   r   s    r   r_   TimeAverager.get_ips_averageL  s9    
 ""d&7&71&<T(()D,<,<<<r   c                 h    U R                   S:X  a  g[        U R                   5      U R                  -  $ )z4
Get the average speed when speed mode is "step/s".
r   )r   r:   r   r   s    r   r`   TimeAverager.get_step_averageU  s1    
 !T&&'$*:*:::r   )r   r   r   r   )r#   r$   r%   r&   r'   r   rD   rG   rX   r_   r`   r(   r)   r   r   r4   r4   0  s%     
/;=;r   r4   c                   R    \ rS rSrSrS rSS jrS rS rS r	S	 r
S
 rS rS rSrg)	Benchmarki_  z
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                 j    S U l         [        [        5       S9U l        S U l        [        5       U l        g )N)
timer_hook)rN   r   r   hooksr   r   r   r   s    r   r   Benchmark.__init__g  s)     IK8
!gr   Nc                 0    Xl         U R                  5         g)zT
Record the statistic for the current step. It will be called in
`Profiler.step()`.
N)rN   r   )r   rN   s     r   stepBenchmark.stepm  s     'r   c                    SnU R                   R                  5       nU R                   R                  5       nU(       a
  USUS S3-  nU(       aU  U R                   R                  S:X  a  SU R                   l        OUS-   U R                   l        USR                  SU5      -  nU R                   R                  5       nU(       a   US	US
 SU R                   R                   3-  nU R                   R                  5         U$ )zn
It returns the statistic of the current step as a string. It contains
"reader_cost", "batch_cost" and "ips".
r   z reader_cost: z.5fz srM   z/sz {}: {:.5f} sr   z ips: z.3f )r   rY   r\   r@   rA   r   ra   rD   )r   unitmessagerY   r\   ra   s         r   	step_infoBenchmark.step_infov  s     ++::<**88:s';2>>G!!,,	909""-04t""---lMJJG**88:s+1T-?-?-J-J,KLG 	  "r   c                 h    U R                   R                  5        H  nUR                  U 5        M     g r   )r   valuesrv   r   hooks     r   rv   Benchmark.begin  s%    JJ%%'DJJt (r   c                 h    U R                   R                  5        H  nUR                  U 5        M     g r   )r   r   r}   r   s     r   r}   Benchmark.before_reader  s'    JJ%%'Dt$ (r   c                 h    U R                   R                  5        H  nUR                  U 5        M     g r   )r   r   r   r   s     r   r   Benchmark.after_reader  s'    JJ%%'Dd# (r   c                 h    U R                   R                  5        H  nUR                  U 5        M     g r   )r   r   r   r   s     r   r   Benchmark.after_step  s%    JJ%%'DOOD! (r   c                 h    U R                   R                  5        H  nUR                  U 5        M     g r   )r   r   rz   r   s     r   rz   Benchmark.end  s$    JJ%%'DHHTN (r   c                    U R                   c  g U R                   R                  (       ao  U R                   R                  c  XR                   l        g U R                   R                  R                  S   UR                  S   :w  a  SU R                   l        g g U R                   R                  R                  S   UR                  S   :X  a9  SU R                   l        [        R
                  " 5       U R                  S   l        g g )N_datasetFTr   )r   r?   r>   __dict__r   r   r   r   )r   r>   s     r   check_if_need_recordBenchmark.check_if_need_record  s    %))!!((0,2"")""))22:>??:./ 27""./ ""))22:>??:./ 26"".6<6J6J6L

<(3	/r   )r   r   r   rN   r   )r#   r$   r%   r&   r'   r   r   r   rv   r}   r   r   rz   r   r(   r)   r   r   r   r   _  s5    2%$"Mr   r   c                      [         $ r   )_benchmark_r)   r   r   ru   ru     s    r   )r   collectionsr   r   r+   rq   r   r4   r   r   ru   r)   r   r   <module>r      se     # 2l l^ *}
 }
@,; ,;^[M [M| kr   