Ë
    •\;j:  ã                   ó¦   — d dl Z d dlmZ  G d„ d«      Z G d„ d«      Z G d„ d«      Z G d	„ d
e«      Z G d„ d«      Z G d„ d«      Z e«       Z	d„ Z
y)é    N)ÚOrderedDictc                   ó.   — e Zd ZdZd„ Zd„ Zd„ Zd„ Zd„ Zy)Ú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                 ó   — g | _         y ©N)Úitems©Úselfs    ú^G:\00. PROJECTS\API\Inventory\templateJSON\kerjaOCR\Lib\site-packages\paddle/profiler/timer.pyÚ__init__zStack.__init__   s	   € Øˆ�
ó    c                 ó:   — | j                   j                  |«       y r   )r   Úappend)r
   Úitems     r   Úpushz
Stack.push   s   € Ø�
‰
×Ñ˜$Õr   c                 ó6   — | j                   j                  «       S r   )r   Úpopr	   s    r   r   z	Stack.pop   s   € Ø�z‰z�~‰~ÓÐr   c                 ó2   — t        | j                  «      dk(  S ©Nr   )Úlenr   r	   s    r   Úis_emptyzStack.is_empty"   s   € Ü�4—:‘:‹ !Ñ#Ð#r   c                 ón   — | j                  «       s%| j                  t        | j                  «      dz
     S y ©Né   )r   r   r   r	   s    r   Úpeekz
Stack.peek%   s*   € Ø�}‰}ŒØ—:‘:œc $§*¡*›o°Ñ1Ñ2Ð2àr   N)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   © r   r   r   r      s    „ ñò
ò ò ò$ór   r   c                   óH   — e Zd ZdZd„ Zd„ Zd„ Zdd„Zd„ Zd„ Z	d	„ Z
d
„ Zd„ Zy)ÚEventzt
    A Event is used to record the cost of every step and the cost of
    the total steps except skipped steps.
    c                 ó  — t        «       | _        t        «       | _        d| _        d| _        d| _        dt        d«      ddœ| _        dt        d«      ddœ| _        dt        d«      dœ| _	        d | _
        d| _        d| _        d| _        y )Nr   é
   Úinf)ÚmaxÚminÚtotal)r&   r'   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   zEvent.__init__2   s…   € Ü$0£NˆÔ!Ü#/£>ˆÔ ØˆÔØˆÔØˆŒØ&'´°e³ÀqÑIˆÔØ%&¬u°U«|ÀaÑHˆÔØ%&¬u°U«|Ñ<ˆÔØˆŒØˆÔð &ˆŒð &ˆ�r   c                 ól   — | j                   j                  «        | j                  j                  «        y r   )r+   Úresetr,   r	   s    r   r9   zEvent.resetD   s&   € Ø×!Ñ!×'Ñ'Ô)Ø× Ñ ×&Ñ&Õ(r   c                 ó¦   — | j                   j                  |«       | j                  | j                  k\  r| j	                  || j
                  «       y y r   )r+   Úrecordr.   r/   Ú_update_recordsr1   )r
   Úusetimes     r   Úrecord_readerzEvent.record_readerH   sC   € Ø×!Ñ!×(Ñ(¨Ô1Ø×Ñ˜tŸ~™~Ò-Ø× Ñ  ¨$×*=Ñ*=Õ>ð .r   Nc                 óš  — |€d| _         d| _        | j                  j                  ||«       | xj                  dz  c_        | j                  | j
                  k\  rq| j                  || j                  «       | j                   dk(  r$t        |«      |z  }| xj                  |z  c_	        nd|z  }| j                  || j                  «       y y )Nústeps/sr   r)   g      ð?)r6   r7   r,   r;   r.   r/   r<   r2   r0   r-   r3   )r
   r=   Únum_samplesÚcurrent_speeds       r   Úrecord_batchzEvent.record_batchM   sµ   € ØÐØ'ˆDŒOØ'ˆDŒOØ× Ñ ×'Ñ'¨°Ô=Ø×Ò˜AÑÕà×Ñ˜tŸ~™~Ò-Ø× Ñ  ¨$×*<Ñ*<Ô=Ø�‰ +Ò-Ü % kÓ 2°WÑ <�Ø×"Ò" kÑ1Ö"à # g¡�Ø× Ñ  °×0BÑ0BÕCð .r   c                 óz   — ||d   kD  r||d<   n||d   k  r||d<   d|j                  «       v r|dxx   |z  cc<   y y )Nr&   r'   r(   )Úkeys)r
   Úcurrent_recordÚrecordss      r   r<   zEvent._update_records]   sO   € Ø˜G E™NÒ*Ø+ˆG�EŠNØ˜g e™nÒ,Ø+ˆG�E‰NØ�g—l‘l“nÑ$Ø�GÓ Ñ.Ôð %r   c                 ó6   — | j                   j                  «       S r   )r+   Úget_averager	   s    r   Úreader_averagezEvent.reader_averagee   s   € Ø×(Ñ(×4Ñ4Ó6Ð6r   c                 ó6   — | j                   j                  «       S r   )r,   rI   r	   s    r   Úbatch_averagezEvent.batch_averageh   s   € Ø×'Ñ'×3Ñ3Ó5Ð5r   c                 óˆ   — | j                   dk(  r| j                  j                  «       S | j                  j                  «       S )Nr)   )r6   r,   Úget_ips_averageÚget_step_averager	   s    r   Úspeed_averagezEvent.speed_averagek   s8   € Ø�?‰?˜kÒ)Ø×+Ñ+×;Ñ;Ó=Ð=à×+Ñ+×<Ñ<Ó>Ð>r   c                 ó¬  — | j                   | j                  k  ri S d}d}d}| xj                   | j                  z  c_         | j                  d   t        | j                   «      z  }| j                  d   t        | j                   «      z  }| j
                  dk(  r&t        | j                  «      | j                  d   z  }n%t        | j                   «      | j                  d   z  }| j                  d   | j                  d   |dœ}| j                  d   | j                  d   |dœ}| j                  d   | j                  d   |dœ}||z  dz  }||||dœ}|S )	Nr   r(   r)   r&   r'   )r&   r'   Úavgéd   )Úreader_summaryÚbatch_summaryÚips_summaryÚreader_ratio)r.   r/   r1   r0   r2   r6   r-   r3   )	r
   Ú
reader_avgÚ	batch_avgÚ	speed_avgrT   rU   rV   rW   Úsummarys	            r   Úget_summaryzEvent.get_summaryq   sl  € Ø×Ñ˜tŸ~™~Ò-ØˆIàˆ
Øˆ	Øˆ	à×Ò˜DŸN™NÑ*ÕØ×(Ñ(¨Ñ1´E¸$×:JÑ:JÓ4KÑKˆ
Ø×&Ñ& wÑ/´%¸×8HÑ8HÓ2IÑIˆ	Ø�?‰?˜kÒ)Ü˜d×0Ñ0Ó1°D×4FÑ4FÀwÑ4OÑO‰Iä˜d×.Ñ.Ó/°$×2DÑ2DÀWÑ2MÑMˆIð ×&Ñ& uÑ-Ø×&Ñ& uÑ-Øñ
ˆð ×%Ñ% eÑ,Ø×%Ñ% eÑ,Øñ
ˆð ×%Ñ% eÑ,Ø×%Ñ% eÑ,Øñ
ˆð
 # YÑ.°#Ñ5ˆà,Ø*Ø&Ø(ñ	
ˆð ˆr   r   )r   r   r   r   r   r9   r>   rC   r<   rJ   rL   rP   r\   r    r   r   r"   r"   ,   s5   „ ñò
&ò$)ò?ó
Dò /ò7ò6ò?ó'r   r"   c                   ó.   — e Zd ZdZd„ Zd„ Zd„ Zd„ Zd„ Zy)ÚHookzG
    As the base class. All types of hooks should inherit from it.
    c                  ó   — y r   r    ©r
   Ú	benchmarks     r   Úbeginz
Hook.begin    ó   € Ør   c                  ó   — y r   r    r`   s     r   ÚendzHook.end£   rc   r   c                  ó   — y r   r    r`   s     r   Úbefore_readerzHook.before_reader¦   rc   r   c                  ó   — y r   r    r`   s     r   Úafter_readerzHook.after_reader©   rc   r   c                  ó   — y r   r    r`   s     r   Ú
after_stepzHook.after_step¬   rc   r   N)	r   r   r   r   rb   re   rg   ri   rk   r    r   r   r^   r^   ›   s    „ ñòòòòór   r^   c                   ó@   — e Zd ZdZd„ Zd„ Zd„ Zd„ Zd„ Zd„ Z	d„ Z
d	„ Zy
)Ú	TimerHookzd
    A hook for recording real-time performance and the summary
    performance of total steps.
    c                 óh   — t        j                  «       | _        t        j                  «       | _        y r   )ÚtimeitÚdefault_timerÚ
start_timeÚstart_readerr	   s    r   r   zTimerHook.__init__¶   s#   € Ü ×.Ñ.Ó0ˆŒÜ"×0Ñ0Ó2ˆÕr   c                 óº   — |j                   j                  t        «       «       |j                   j                  «       |_        t        j                  «       | _        y)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_eventro   rp   rq   r`   s     r   rb   zTimerHook.beginº   sA   € ð 	×Ñ×Ñœe›gÔ&Ø"+×"2Ñ"2×"7Ñ"7Ó"9ˆ	ÔÜ ×.Ñ.Ó0ˆ�r   c                 ó6   — t        j                  «       | _        y)zÒ
        Initialize the start time of the dataloader. This function will be
        called at the beginning of `next` method in `_DataLoaderIterMultiProcess` or
        `_DataLoaderIterSingleProcess`.

        N)ro   rp   rr   r`   s     r   rg   zTimerHook.before_readerÄ   s   € ô #×0Ñ0Ó2ˆÕr   c                 óÌ   — t        j                  «       | j                  z
  }|j                  �|j                  j                  r|dk(  ry|j                  j                  |«       y)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   )ro   rp   rr   ru   r5   r>   )r
   ra   Úreader_costs      r   ri   zTimerHook.after_readerÎ   sW   € ô ×*Ñ*Ó,¨t×/@Ñ/@Ñ@ˆà×$Ñ$Ð,Ø×+Ñ+×7Ò7Ø˜qÒ àØ×Ñ×-Ñ-¨kÕ:r   c                 ó
  — |j                   �|j                   j                  syt        j                  «       | j                  z
  }|j                   j                  ||j                  «       t        j                  «       | _        y)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)ru   r5   ro   rp   rq   rC   rA   )r
   ra   Ú
batch_costs      r   rk   zTimerHook.after_stepà   se   € ð ×#Ñ#Ð+Ø×'Ñ'×3Ò3àÜ×)Ñ)Ó+¨d¯o©oÑ=ˆ
Ø×Ñ×,Ñ,¨Z¸×9NÑ9NÔOÜ ×.Ñ.Ó0ˆ�r   c                 ó   — |j                   j                  «       ry| j                  |«       |j                   j                  «        |j                   j	                  «       |_        t        j                  «       | _        y)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)	rt   r   Ú_print_summaryr   r   ru   ro   rp   rq   r`   s     r   re   zTimerHook.endò   s`   € ð ×Ñ×$Ñ$Ô&ØØ×Ñ˜IÔ&Ø×Ñ×ÑÔØ"+×"2Ñ"2×"7Ñ"7Ó"9ˆ	ÔÜ ×.Ñ.Ó0ˆ�r   c                 ó(  — |j                   j                  «       }|sy t        dj                  dd«      «       |d   dk7  rt        dd|d   z  z   dz   «       t        d	|j                   j                  z  «       t        d
dj                  d«      d
dj                  d«      d
dj                  d«      d
dj                  d«      d
«	       |d   d   dk7  r| j                  d|d   «       | j                  d|d   «       | j                  d|d   «       y )NzPerf SummaryrS   Ú=rW   r   zReader Ratio: z%.3fÚ%zTime Unit: s, IPS Unit: %sÚ|Ú é   rR   r&   r'   rT   rx   rz   rU   ÚipsrV   )ru   r\   ÚprintÚcenterr7   Ú_print_stats)r
   ra   r[   s      r   r|   zTimerHook._print_summary  s  € Ø×)Ñ)×5Ñ5Ó7ˆÙØÜˆn×#Ñ# C¨Ó-Ô.Ø�>Ñ" aÒ'ÜÐ" V¨w°~Ñ/FÑ%GÑGÈ#ÑMÔNÜØ(¨I×,CÑ,C×,NÑ,NÑOô	
ô 	ØØ�I‰I�b‹MØØ�L‰L˜ÓØØ�L‰L˜ÓØØ�L‰L˜ÓØô
	
ð Ð#Ñ$ UÑ+¨qÒ0Ø×Ñ˜m¨WÐ5EÑ-FÔGØ×Ñ˜,¨°Ñ(@ÔAØ×Ñ˜% ¨Ñ!7Õ8r   c                 óÒ   — d|d   z  }d|d   z  }d|d   z  }t        d|j                  d«      d|j                  d«      d|j                  d«      d|j                  d«      d«	       y )Nz%.5frR   r&   r'   r€   r‚   )r„   r…   )r
   r   Úmessage_dictÚavg_strÚmax_strÚmin_strs         r   r†   zTimerHook._print_stats  su   € Ø˜L¨Ñ/Ñ0ˆØ˜L¨Ñ/Ñ0ˆØ˜L¨Ñ/Ñ0ˆÜØØ�K‰K˜‹OØØ�N‰N˜2ÓØØ�N‰N˜2ÓØØ�N‰N˜2ÓØõ
	
r   N)r   r   r   r   r   rb   rg   ri   rk   re   r|   r†   r    r   r   rm   rm   °   s/   „ ñò
3ò1ò3ò;ò$1ò$1ò 9ó6
r   rm   c                   ó6   — e Zd ZdZd„ Zd„ Zd	d„Zd„ Zd„ Zd„ Z	y)
r*   z>
    Record the cost of every step and count the average.
    c                 ó$   — | j                  «        y r   )r9   r	   s    r   r   zTimeAverager.__init__3  s   € Ø�
‰
�r   c                 ó.   — d| _         d| _        d| _        y r   ©Ú_total_itersÚ_total_timeÚ_total_samplesr	   s    r   r9   zTimeAverager.reset6  s   € ØˆÔØˆÔØˆÕr   Nc                 óˆ   — | xj                   dz  c_         | xj                  |z  c_        |r| xj                  |z  c_        y y r   r�   )r
   r=   rA   s      r   r;   zTimeAverager.record;  s=   € Ø×Ò˜QÑÕØ×Ò˜GÑ#ÕÙØ×Ò ;Ñ.Öð r   c                 óf   — | j                   dk(  ry| j                  t        | j                   «      z  S )zA
        Get the average cost of loading data or a step.
        r   )r�   r‘   r0   r	   s    r   rI   zTimeAverager.get_averageA  s1   € ð
 ×Ñ Ò!ØØ×Ñ¤%¨×(9Ñ(9Ó":Ñ:Ð:r   c                 ó~   — | j                   r| j                  dk(  ryt        | j                   «      | j                  z  S )zL
        Get the average throughput when speed mode is "samples/s".
        r   )r’   r�   r0   r‘   r	   s    r   rN   zTimeAverager.get_ips_averageJ  s9   € ð
 ×"Ò" d×&7Ñ&7¸1Ò&<ØÜ�T×(Ñ(Ó)¨D×,<Ñ,<Ñ<Ð<r   c                 óf   — | j                   dk(  ryt        | j                   «      | j                  z  S )zD
        Get the average speed when speed mode is "step/s".
        r   )r�   r0   r‘   r	   s    r   rO   zTimeAverager.get_step_averageS  s1   € ð
 ×Ñ Ò!ØÜ�T×&Ñ&Ó'¨$×*:Ñ*:Ñ:Ð:r   r   )
r   r   r   r   r   r9   r;   rI   rN   rO   r    r   r   r*   r*   .  s%   „ ñòò ó
/ò;ò=ó;r   r*   c                   óH   — e Zd ZdZd„ Zdd„Zd„ Zd„ Zd„ Zd„ Z	d	„ Z
d
„ Zd„ Zy)Ú	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                 óp   — d | _         t        t        «       ¬«      | _        d | _        t        «       | _        y )N)Ú
timer_hook)rA   r   rm   Úhooksru   r   rt   r	   s    r   r   zBenchmark.__init__e  s)   € ØˆÔÜ ¬I«KÔ8ˆŒ
Ø!ˆÔÜ“gˆ�r   Nc                 ó2   — || _         | j                  «        y)zl
        Record the statistic for the current step. It will be called in
        `Profiler.step()`.
        N)rA   rk   )r
   rA   s     r   ÚstepzBenchmark.stepk  s   € ð 'ˆÔØ�‰Õr   c                 óÞ  — d}| j                   j                  «       }| j                   j                  «       }|r|d|z  z  }|rT| j                   j                  dk(  rd| j                   _        n|dz   | j                   _        |dj                  d|«      z  }| j                   j                  «       }|r |d|d›d	| j                   j                  › �z  }| j                   j                  «        |S )
z†
        It returns the statistic of the current step as a string. It contains
        "reader_cost", "batch_cost" and "ips".
        r�   z reader_cost: %.5f sr@   z/sz {}: {:.5f} srz   z ips: z.3fÚ )ru   rJ   rL   r6   r7   ÚformatrP   r9   )r
   ÚunitÚmessagerJ   rL   rP   s         r   Ú	step_infozBenchmark.step_infot  sì   € ð ˆØ×+Ñ+×:Ñ:Ó<ˆØ×*Ñ*×8Ñ8Ó:ˆÙØÐ-°Ñ@Ñ@ˆGÙØ×!Ñ!×,Ñ,°	Ò9Ø09�×"Ñ"Õ-à04°t±�×"Ñ"Ô-Ø�×-Ñ-¨l¸MÓJÑJˆGØ×*Ñ*×8Ñ8Ó:ˆÙØØ˜ sÐ+¨1¨T×-?Ñ-?×-JÑ-JÐ,KÐLñˆGð 	×Ñ× Ñ Ô"Øˆr   c                 ód   — | j                   j                  «       D ]  }|j                  | «       Œ y r   )r›   Úvaluesrb   ©r
   Úhooks     r   rb   zBenchmark.begin�  s%   € Ø—J‘J×%Ñ%Ö'ˆDØ�J‰J�tÕñ (r   c                 ód   — | j                   j                  «       D ]  }|j                  | «       Œ y r   )r›   r¥   rg   r¦   s     r   rg   zBenchmark.before_reader‘  s'   € Ø—J‘J×%Ñ%Ö'ˆDØ×Ñ˜tÕ$ñ (r   c                 ód   — | j                   j                  «       D ]  }|j                  | «       Œ y r   )r›   r¥   ri   r¦   s     r   ri   zBenchmark.after_reader•  s'   € Ø—J‘J×%Ñ%Ö'ˆDØ×Ñ˜dÕ#ñ (r   c                 ód   — | j                   j                  «       D ]  }|j                  | «       Œ y r   )r›   r¥   rk   r¦   s     r   rk   zBenchmark.after_step™  s%   € Ø—J‘J×%Ñ%Ö'ˆDØ�O‰O˜DÕ!ñ (r   c                 ód   — | j                   j                  «       D ]  }|j                  | «       Œ y r   )r›   r¥   re   r¦   s     r   re   zBenchmark.end�  s$   € Ø—J‘J×%Ñ%Ö'ˆDØ�H‰H�T�Nñ (r   c                 óü  — | j                   €y | j                   j                  rn| j                   j                  €|| j                   _        y | j                   j                  j                  d   |j                  d   k7  rd| j                   _        y y | j                   j                  j                  d   |j                  d   k(  r8d| j                   _        t	        j
                  «       | j                  d   _        y y )NÚ_datasetFTrš   )ru   r5   r4   Ú__dict__ro   rp   r›   rq   )r
   r4   s     r   Úcheck_if_need_recordzBenchmark.check_if_need_record¡  sÜ   € Ø×ÑÐ%ØØ×Ñ×)Ò)à×!Ñ!×(Ñ(Ð0Ø,2�×"Ñ"Õ)à×"Ñ"×)Ñ)×2Ñ2°:Ñ>Ø—?‘? :Ñ.ò/ð 27�×"Ñ"Õ.ð/ð ×"Ñ"×)Ñ)×2Ñ2°:Ñ>Ø—?‘? :Ñ.ò/ð 26�×"Ñ"Ô.Ü6<×6JÑ6JÓ6L�—
‘
˜<Ñ(Õ3ð	/r   r   )r   r   r   r   r   r�   r£   rb   rg   ri   rk   re   r¯   r    r   r   r˜   r˜   ]  s5   „ ñòóòò2ò%ò$ò"òóMr   r˜   c                  ó   — t         S r   )Ú_benchmark_r    r   r   ra   ra   ¾  s   € ÜÐr   )ro   Úcollectionsr   r   r"   r^   rm   r*   r˜   r±   ra   r    r   r   Ú<module>r³      se   ðó Ý #÷ñ ÷2lñ l÷^ñ ô*{
�ô {
÷|,;ñ ,;÷^[Mñ [Mñ| ‹k€ór   