
    Z j@                         S SK r S SKrS SKrS SKJrJr  S SKJr  SSK	J
r
  SSKJrJr  \" S5      rS\S-  S\S	\4S
 jrS rSS jrS r " S S5      r " S S\5      r " S S\
5      rg)    N)OptionalTypeVar   )TrainerCallback)IntervalStrategy
has_length_Txmsgreturnc                 "    U c  [        U5      eU $ N)RuntimeError)r
   r   s     l/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/transformers/utils/notebook.py_requirer      s    y3H    c                 p    [        U 5      n U S-  U S-  S-  U S-  p2nUS:w  a  U SUS SUS 3$ US SUS 3$ )z$Format `t` (in seconds) to (h):mm:ssi  <   r   :02d)int)thmss       r   format_timer   #   s[    AA4i!r'RR!A%&!VaS!C!C!CAc7!Ac71CCr   c                 &    SU SU  SU SU SU S3$ )Nz
    <div>
      z
      <progress value='z' max='z' style='width:z<px; height:20px; vertical-align: middle;'></progress>
      z
    </div>
     )valuetotalprefixlabelwidths        r   html_progress_barr$   *   s<    h wgeWOE7 Cg 	 r   c                     SnUS-  nU S    H  nUSU S3-  nM     US-  nU SS  HC  nUS	-  nU H0  n[        U[        5      (       a  US
 O
[        U5      nUSU S3-  nM2     US-  nME     US-  nU$ )z*Put the texts in `items` in an HTML table.z%<table border="1" class="dataframe">
z*  <thead>
 <tr style="text-align: left;">
r   z
      <th>z</th>
z    </tr>
  </thead>
  <tbody>
   Nz	    <tr>
z.6fz
      <td>z</td>
z
    </tr>
z  </tbody>
</table><p>)
isinstancefloatstr)items	html_codeilineelts        r   text_to_html_tabler/   5   s    <ICCI1Xz!G,,	 55Iab	\!	C",S%"8"8SIc#hC:cU'22I  	]"	  **Ir   c                       \ rS rSrSrSrSr    SS\S\S-  S\	S	\
S
   S\4
S jjrSS\S\	S\S-  4S jjrSS jrS rS rSrg)NotebookProgressBarF   aP  
A progress par for display in a notebook.

Class attributes (overridden by derived classes)

    - **warmup** (`int`) -- The number of iterations to do at the beginning while ignoring `update_every`.
    - **update_every** (`float`) -- Since calling the time takes some time, we only do it every presumed
      `update_every` seconds. The progress bar uses the average time passed up until now to guess the next value
      for which it will call the update.

Args:
    total (`int`):
        The total number of iterations to reach.
    prefix (`str`, *optional*):
        A prefix to add before the progress bar.
    leave (`bool`, *optional*, defaults to `True`):
        Whether or not to leave the progress bar once it's completed. You can always call the
        [`~utils.notebook.NotebookProgressBar.close`] method to make the bar disappear.
    parent ([`~notebook.NotebookTrainingTracker`], *optional*):
        A parent object (like [`~utils.notebook.NotebookTrainingTracker`]) that spawns progress bars and handle
        their display. If set, the object passed must have a `display()` method.
    width (`int`, *optional*, defaults to 300):
        The width (in pixels) that the bar will take.

Example:

```python
import time

pbar = NotebookProgressBar(100)
for val in range(100):
    pbar.update(val)
    time.sleep(0.07)
pbar.update(100)
```   g?Nr    r!   leaveparentNotebookTrainingTrackerr#   c                     Xl         Uc  SOUU l        X0l        X@l        XPl        S U l        S U l        S U l        S U l        S U l	        S[        R                  ;   a  SU l        g g )N 
VSCODE_PIDg      ?)r    r!   r4   r5   r#   
last_valuecommentoutputr   r"   osenvironupdate_every)selfr    r!   r4   r5   r#   s         r   __init__NotebookProgressBar.__init__n   sa     
"Nb



2::% #D &r   r   force_updater;   c                    Xl         Ub  X0l        U R                  cg  [        R                  " 5       =U l        U l        U=U l        U l        S=U l        U l        U R                  U l
        SU l        U R                  U5        gXR                  ::  a  U(       d  gU(       dB  U R                  S:  d2  U[        U R                  U R                  -   U R                  5      :  Gaj  U R                  S:  a  U =R                  S-  sl
        [        R                  " 5       nX@R                  -
  U l        XR                  :  a!  U R                  XR                  -
  -  U l        OSU l        XR                  :  a5  U R                  nSU l        U R                   (       d  U R#                  5         O.U R                  b!  U R                  U R                  U-
  -  U l        U R                  U5        Xl        X@l        U R                  b  U R                  S:X  a  SU l        g[%        ['        U R(                  U R                  -  5      S5      U l        gg)a>  
The main method to update the progress bar to `value`.

Args:
    value (`int`):
        The value to use. Must be between 0 and `total`.
    force_update (`bool`, *optional*, defaults to `False`):
        Whether or not to force and update of the internal state and display (by default, the bar will wait for
        `value` to reach the value it predicted corresponds to a time of more than the `update_every` attribute
        since the last update to avoid adding boilerplate).
    comment (`str`, *optional*):
        A comment to add on the left of the progress bar.
Nr&   r   )r   r;   r:   time
start_time	last_timestart_valueelapsed_timepredicted_remainingwarmupfirst_callswait_for
update_barminr    average_time_per_itemr4   closemaxr   r?   )r@   r   rC   r;   current_times        r   updateNotebookProgressBar.update   s    
"L??"/3yy{:DOdn166Dt;??D 8#{{DDMOOE"oo%lT--1Uc$//TXTaTaBacgcmcm>n5n!#  A% 99;L , >D'''-1->->%JZJZBZ-[*-1*

"

+/(zzJJL++7+/+E+EV[I[+\(OOE"#O)N**28R8RVW8W ! #C(9(9D<V<V(V$WYZ [/ 6or   c           
          S[        [        U R                  5      5      [        [        U5      5      -
  -  [        U5      -   nU R                  c  SU SU R                   S3U l        OU R
                  c.  SU SU R                   S[        U R                  5       3U l        OSU SU R                   S[        U R                  5       S[        U R
                  5       3U l        U R                  S:X  a  U =R                  S-  sl        O'U =R                  SS	U R                  -  S
 S3-  sl        U =R                  U R                  b  [        U R                  5      S:X  a  SOSU R                   S3-  sl        U R                  5         g )N [/z : < :z < r   z, +inf it/sz, r&   .2fz it/s])
lenr)   r    rI   r"   rJ   r   rP   r;   display)r@   r   r;   spaced_values       r   rN   NotebookProgressBar.update_bar   s[   c#djj/2SU_DEE
R$\N!DJJ<v>DJ%%-\N!DJJ<qTEVEV9W8XYDJ L>4::,aD<M<M0N/O P 8 89:< J ))Q.

m+


1t'A'A#A#"FeLL


T\\1S5F!5KcSUVZVbVbUccdQee
r   c                    [        U R                  U R                  U R                  U R                  U R
                  5      U l        U R                  b  U R                  R                  5         g U R                  c9  [        R                  " [        R                  " U R                  5      SS9U l	        g U R                  R                  [        R                  " U R                  5      5        g NT)
display_id)r$   r   r    r!   r"   r#   r+   r5   r]   r<   dispHTMLrT   r@   s    r   r]   NotebookProgressBar.display   s    *4::tzz4;;PTPZPZ\`\f\fg;;"KK!;;,,tyy'@TRDKKKtyy89r   c                     U R                   c>  U R                  b0  U R                  R                  [        R                  " S5      5        ggg)zCloses the progress bar.Nr8   )r5   r<   rT   rc   rd   re   s    r   rQ   NotebookProgressBar.close   s7    ;;4;;#:KKtyy}- $;r   )rP   r;   rI   rL   r+   r"   rG   r:   r4   r<   r5   rJ   r!   rF   rH   r    r?   r   rM   r#   )NTN,  )FNr   )__name__
__module____qualname____firstlineno____doc__rK   r?   r   r)   boolr   rA   rT   rN   r]   rQ   __static_attributes__r   r   r   r1   r1   F   s    "H FL
 "6:$$ d
$ 	$
 23$ $,1\C 1\t 1\cDj 1\f&	:.r   r1   c                   L   ^  \ rS rSrSrS	U 4S jjrS rS rS
S jrS r	Sr
U =r$ )r6      a  
An object tracking the updates of an ongoing training with progress bars and a nice table reporting metrics.

Args:
    num_steps (`int`): The number of steps during training. column_names (`list[str]`, *optional*):
        The list of column names for the metrics table (will be inferred from the first call to
        [`~utils.notebook.NotebookTrainingTracker.write_line`] if not set).
c                 L   > [         TU ]  U5        Uc  S OU/U l        S U l        g r   )superrA   inner_table	child_bar)r@   	num_stepscolumn_names	__class__s      r   rA    NotebookTrainingTracker.__init__   s(    ##/#74l^r   c                 d   [        U R                  U R                  U R                  U R                  U R
                  5      U l        U R                  b(  U =R                  [        U R                  5      -  sl        U R                  b)  U =R                  U R                  R                  -  sl        U R                  c9  [        R                  " [        R                  " U R                  5      SS9U l
        g U R                  R                  [        R                  " U R                  5      5        g ra   )r$   r   r    r!   r"   r#   r+   ru   r/   rv   r<   rc   r]   rd   rT   re   s    r   r]   NotebookTrainingTracker.display   s    *4::tzz4;;PTPZPZ\`\f\fg'NN01A1ABBN>>%NNdnn666N;;,,tyy'@TRDKKKtyy89r   c           	         U R                   c8  [        UR                  5       5      [        UR                  5       5      /U l         gU R                   S   nU H  nX2;  d  M
  UR	                  U5        M     X R                   S'   [        U R                   5      S:  a  U R                   S   nU R                   S   S   nUS   X   :w  a<  U R                   R	                  U Vs/ s H  oaR                  US5      PM     sn5        gUnU H  nXg;  d  M
  XBR                  U5         Xv'   M!     U Vs/ s H  ogU   PM	     snU R                   S'   gU R                   R	                  U Vs/ s H  oaU   PM	     sn5        gs  snf s  snf s  snf )ze
Write the values in the inner table.

Args:
    values (`dict[str, float]`): The values to display.
Nr   r&   zNo Log)ru   listkeysvaluesappendr\   getindex)r@   r   columnskeylast_valuesfirst_columnc
new_valuess           r   
write_line"NotebookTrainingTracker.write_line   se    # $V[[] 3T&--/5JKD&&q)G%NN3'  #*Q4##$q("..r2#//215q>V%99$$++g,VgZZ8-Dg,VW "(J$.,7a8H,IJM % DK+K7aqM7+KD$$R(  ''G(DGqG(DE -W ,L(Ds   "E>0F(Fc                 8    [        XXS9U l        U R                  $ )a  
Add a child progress bar displayed under the table of metrics. The child progress bar is returned (so it can be
easily updated).

Args:
    total (`int`): The number of iterations for the child progress bar.
    prefix (`str`, *optional*): A prefix to write on the left of the progress bar.
    width (`int`, *optional*, defaults to 300): The width (in pixels) of the progress bar.
)r!   r5   r#   )r1   rv   )r@   r    r!   r#   s       r   	add_child!NotebookTrainingTracker.add_child  s     -U$\~~r   c                 2    SU l         U R                  5         g)z 
Closes the child progress bar.
N)rv   r]   re   s    r   remove_child$NotebookTrainingTracker.remove_child!  s     r   )rv   r+   ru   r<   r   )Nri   )rj   rk   rl   rm   rn   rA   r]   r   r   r   rp   __classcell__)ry   s   @r   r6   r6      s'    
	:F> r   r6   c                   T    \ rS rSrSrS rS rS rSS jrS r	SS	 jr
SS
 jrS rSrg)NotebookProgressCallbacki)  z~
A [`TrainerCallback`] that displays the progress of training or evaluation, optimized for Jupyter Notebooks or
Google colab.
c                 .    S U l         S U l        SU l        g )NF)training_trackerprediction_bar_force_next_updatere   s    r   rA   !NotebookProgressCallback.__init__/  s     $""'r   c                 $   UR                   [        R                  :X  a  SOSU l        SU l        SU l        U R                  /S/-   nUR                   [        R                  :w  a  UR                  S5        [        UR                  U5      U l
        g )NEpochStepr   Training LossValidation Loss)eval_strategyr   EPOCHr   training_losslast_logNOr   r6   	max_stepsr   )r@   argsstatecontrolkwargsrx   s         r   on_train_begin'NotebookProgressCallback.on_train_begin4  s}    '+'9'9=M=S=S'SGY_))*o->>!1!4!44 12 7 Vr   c                 :   [        UR                  5      UR                  :X  a  [        UR                  5      OUR                  S n[        U R                  S5      nUR	                  UR
                  S-   SU SUR                   3U R                  S9  SU l        g )NrZ   z0on_train_begin must be called before on_step_endr&   Epoch rY   r;   rC   F)r   epochr   r   rT   global_stepnum_train_epochsr   )r@   r   r   r   r   r   tts          r   on_step_end$NotebookProgressCallback.on_step_end=  s    $'$4$CEKK EKKX[K\d++-_`
		!UG1U%;%;$<=00 	 	

 #(r   Nc                 |   [        U5      (       d  g U R                  cl  U R                  b*  U R                  R                  [	        U5      5      U l        O[        [	        U5      5      U l        U R                  R                  S5        g U R                  R                  U R                  R                  S-   5        g )Nr&   )r   r   r   r   r\   r1   rT   r   )r@   r   r   r   eval_dataloaderr   s         r   on_prediction_step+NotebookProgressCallback.on_prediction_stepG  s    /**&$$0&*&;&;&E&Ec/FZ&[#&9#o:N&O#&&q)&&t':':'@'@1'DEr   c                 `    U R                   b  U R                   R                  5         S U l         g r   )r   rQ   )r@   r   r   r   r   s        r   
on_predict#NotebookProgressCallback.on_predictS  s(    *%%'"r   c                     UR                   [        R                  :X  aE  SU;   a>  [        U R                  S5      nSUS   0nUR
                  US'   UR                  U5        g g g )Nlossz+on_train_begin must be called before on_logr   r   )r   r   r   r   r   r   r   )r@   r   r   r   logsr   r   r   s           r   on_logNotebookProgressCallback.on_logX  sb    !1!4!444$//1^_B%tF|4F"..F6NMM&! :H4r   c                    UR                   [        R                  :X  a  SOSU l        SSS.n[	        UR
                  5       H  nSU;   d  M  US   US'     O   U R                  S:X  a  [        UR                  5      US'   OUR                  US'   Uc  0 nSnU H3  n	U	R                  S5      (       d  M  [        R                  " S	S
U	5      nM5     UR                  SS 5      n
UR                  SS 5      n
UR                  U S3S 5      n
UR                  U S3S 5      n
UR                  U S3S 5      n
UR                  U S3S 5      n
UR                  5        HT  u  pU	R                  S5      nSR                  USS   Vs/ s H  oR!                  5       PM     sn5      nUS:X  a  SnXU'   MV     U R"                  b5  U R"                  nUR%                  U5        UR'                  5         SU l        Oc[*        R,                  " [*        R.                  " [1        [3        UR5                  5       5      [3        UR7                  5       5      /5      5      5        S U l        g s  snf )Nr   r   zNo log)r   r   r   r   eval_lossz\_loss$r8   
total_flosr   _runtime_samples_per_second_steps_per_second_model_preparation_time_rW   r&   Lossr   T)r   r   r   r   reversedlog_historyr   r   r   endswithresubpopr*   splitjoin
capitalizer   r   r   r   rc   r]   rd   r/   r   r   r   r   )r@   r   r   r   metricsr   r   logmetric_key_prefixkr   vsplitspartnamer   s                   r   on_evaluate$NotebookProgressCallback.on_evaluatea  s+    (,'9'9=M=S=S'SGY_#+IE--.C}*-f+' /
 '!%++.F7O"..F6N?G"Azz'""$&FF:r1$=!  KKd+KK&KK,-X6=KK,--@A4HKK,-->?FKK,--DEtLMMODAWWS\F886!":F:4__.:FGDv~(4L $   ,&&BMM&!OO&*D# LL#5tFKKM7JDQWQ^Q^Q`La6b#cde"! Gs   I
c                     [        U R                  S5      nUR                  UR                  S[	        UR
                  5       SUR                   3SS9  S U l        g )Nz1on_train_begin must be called before on_train_endr   rY   Tr   )r   r   rT   r   r   r   r   )r@   r   r   r   r   r   s         r   on_train_end%NotebookProgressCallback.on_train_end  s]    d++-`a
		S-.a0F0F/GH 	 	

 !%r   )r   r   r   r   r   r   r   )rj   rk   rl   rm   rn   rA   r   r   r   r   r   r   r   rp   r   r   r   r   r   )  s2    
(
W(
F#
".#`%r   r   )ri   )r=   r   rE   typingr   r   IPython.displayr]   rc   trainer_callbackr   trainer_utilsr   r   r	   r)   r   r   r$   r/   r1   r6   r   r   r   r   <module>r      s    
 	  $  . 8 T]T	   D"R. R.jK1 K\o% o%r   