Ë
    T^(hÑ=  ã                   ó¤   — 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m	Z	 ddl
mZmZ d„ Zdd„Zd„ Z G d	„ d
«      Z G d„ de«      Z G d„ de	«      Zy)é    N)ÚOptionalé   )ÚTrainerCallback)ÚIntervalStrategyÚ
has_lengthc                 óp   — t        | «      } | dz  | dz  dz  | dz  }}}|dk7  r|› d|d›d|d›�S |d›d|d›�S )z$Format `t` (in seconds) to (h):mm:ssi  é<   r   ú:Ú02d)Úint)ÚtÚhÚmÚss       úY/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/utils/notebook.pyÚformat_timer      s]   € äˆA‹€AØ�4‰i˜!˜r™' R™¨¨R©ˆ!€q€AØ%&¨!¢VˆaˆS��!�C�˜˜!˜C˜Ð!ÐC°A°c°7¸!¸A¸c¸7Ð1CÐCó    c                 ó&   — d|› d| › d|› d|› d|› d�S )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ð Ø�w˜g e W¨O¸E¸7ð CØ€gð ð	ð r   c                 óÔ   — d}|dz  }| d   D ]  }|d|› d�z  }Œ |dz  }| dd D ];  }|d	z  }|D ]*  }t        |t        «      r|d
›n
t        |«      }|d|› d�z  }Œ, |dz  }Œ= |dz  }|S )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&   ,   s»   € à<€IØÐCÑC€IØ�1‰Xò -ˆØ�z !  GÐ,Ñ,‰	ð-àÐ5Ñ5€IØ�a�b�	ò #ˆØ�\Ñ!ˆ	Øò 	3ˆCÜ",¨S´%Ô"8�S˜‘I¼cÀ#»hˆCØ˜: c U¨'Ð2Ñ2‰Ið	3ð 	�]Ñ"‰	ð#ð Ð*Ñ*€IØÐr   c                   óz   — e Zd ZdZdZdZ	 	 	 	 ddedee   de	ded	   d
ef
d„Z
ddede	dee   fd„Zdd„Zd„ Zd„ Zy)ÚNotebookProgressBaraÀ  
    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                 óÌ   — || _         |€dn|| _        || _        || _        || _        d | _        d | _        d | _        d | _        d | _	        dt        j                  v rd| _        y y )NÚ Ú
VSCODE_PIDg      à?)r   r   r*   r+   r   Ú
last_valueÚcommentÚoutputr   r   ÚosÚenvironÚupdate_every)Úselfr   r   r*   r+   r   s         r   Ú__init__zNotebookProgressBar.__init__e   si   € ð ˆŒ
Ø"˜N‘b°ˆŒØˆŒ
ØˆŒØˆŒ
ØˆŒØˆŒØˆŒØˆŒ
ØˆŒ
Øœ2Ÿ:™:Ñ%Ø #ˆDÕð &r   r   Úforce_updater1   c                 óx  — || _         |�|| _        | j                  €ft        j                  «       x| _        | _        |x| _        | _        dx| _        | _        | j                  | _
        d| _        | j                  |«       y|| j                  k  r|sy|s@| j                  dkD  s1|t        | j                  | j                  z   | j                  «      k\  �rd| j                  dkD  r| xj                  dz  c_
        t        j                  «       }|| j                  z
  | _        || j                  kD  r"| j                  || j                  z
  z  | _        nd| _        || j                  k\  r0| j                  }d| _        | j                   s>| j#                  «        n-| j                  �!| j                  | j                  |z
  z  | _        | j                  |«       || _        || _        | j                  �| j                  dk(  rd| _        yt%        t'        | j(                  | j                  z  «      d«      | _        yy)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   r1   r0   ÚtimeÚ
start_timeÚ	last_timeÚstart_valueÚelapsed_timeÚpredicted_remainingÚwarmupÚfirst_callsÚwait_forÚ
update_barÚminr   Úaverage_time_per_itemr*   ÚcloseÚmaxr   r5   )r6   r   r8   r1   Úcurrent_times        r   ÚupdatezNotebookProgressBar.update{   så  € ð ˆŒ
ØÐØ"ˆDŒLØ�?‰?Ð"Ü/3¯y©y«{Ð:ˆDŒO˜dœnØ16Ð6ˆDÔ˜tœØ;?Ð?ˆDÔ Ô 8Ø#Ÿ{™{ˆDÔØˆDŒMØ�O‰O˜EÕ"Ø�d—o‘oÒ%©lØÙ˜T×-Ñ-°Ò1°U¼cÀ$Ç/Á/ÐTX×TaÑTaÑBaÐcg×cmÑcmÓ>nÓ5nØ×Ñ !Ò#Ø× Ò  AÑ%Õ ÜŸ9™9›;ˆLØ ,¨t¯©Ñ >ˆDÔà�t×'Ñ'Ò'Ø-1×->Ñ->À%È$×JZÑJZÑBZÑ-[�Õ*à-1�Ô*Ø˜Ÿ
™
Ò"ØŸ
™
�Ø+/�Ô(Ø—z’zØ—J‘J•LØ×+Ñ+Ð7Ø+/×+EÑ+EÈÏÉÐV[ÑI[Ñ+\�Ô(Ø�O‰O˜EÔ"Ø#ˆDŒOØ)ˆDŒNØ×*Ñ*Ð2¸×8RÑ8RÐVWÒ8WØ !�•ä #¤C¨×(9Ñ(9¸D×<VÑ<VÑ(VÓ$WÐYZÓ [�•ð/ 6or   c           
      ó  — dt        t        | j                  «      «      t        t        |«      «      z
  z  t        |«      z   }| j                  €d|› d| j                  › d�| _        nÉ| j
                  €.d|› d| j                  › dt        | j                  «      › �| _        n�d|› d| j                  › dt        | j                  «      › dt        | j
                  «      › �| _        | j                  dk(  r| xj                  dz  c_        n'| xj                  dd	| j                  z  d
›d�z  c_        | xj                  | j                  �t        | j                  «      dk(  rdnd| j                  › d�z  c_        | j                  «        y )Nú ú[ú/z : < :z < r   z, +inf it/sz, r   ú.2fz it/sú])
Úlenr    r   r>   r   r?   r   rE   r1   Údisplay)r6   r   r1   Úspaced_values       r   rC   zNotebookProgressBar.update_bar®   s[  € Øœc¤# d§j¡j£/Ó2´S¼¸U»³_ÑDÑEÌÈEË
ÑRˆØ×ÑÐ$Ø˜\˜N¨!¨D¯J©J¨<°vÐ>ˆD�JØ×%Ñ%Ð-Ø˜\˜N¨!¨D¯J©J¨<°q¼ÀT×EVÑEVÓ9WÐ8XÐYˆD�Jð �L�>  4§:¡: ,¨a´¸D×<MÑ<MÓ0NÐ/Oð PÜ × 8Ñ 8Ó9Ð:ð<ð ŒJð ×)Ñ)¨QÒ.Ø—
’
˜mÑ+–
à—
’
  1 t×'AÑ'AÑ#AÀ#Ð"FÀeÐLÑL•
à�
Š
˜TŸ\™\Ð1´S¸¿¹Ó5FÈ!Ò5K‘cÐSUÐVZ×VbÑVbÐUcÐcdÐQeÑe�
Ø�‰�r   c                 óÚ  — t        | j                  | j                  | j                  | j                  | j
                  «      | _        | j                  �| j                  j                  «        y | j                  €:t        j                  t        j                  | j                  «      d¬«      | _	        y | j                  j                  t        j                  | j                  «      «       y ©NT)Ú
display_id)r   r   r   r   r   r   r"   r+   rQ   r2   ÚdispÚHTMLrI   ©r6   s    r   rQ   zNotebookProgressBar.displayÁ   s•   € Ü*¨4¯:©:°t·z±zÀ4Ç;Á;ÐPT×PZÑPZÐ\`×\fÑ\fÓgˆŒØ�;‰;Ð"à�K‰K×ÑÔ!ØØ�;‰;ÐÜŸ,™,¤t§y¡y°·±Ó'@ÈTÔRˆD�Kà�K‰K×ÑœtŸy™y¨¯©Ó8Õ9r   c                 ó”   — | j                   €<| j                  �/| j                  j                  t        j                  d«      «       yyy)zCloses the progress bar.Nr.   )r+   r2   rI   rV   rW   rX   s    r   rF   zNotebookProgressBar.closeÌ   s7   € à�;‰;Ð 4§;¡;Ð#:Ø�K‰K×ÑœtŸy™y¨›}Õ-ð $;Ðr   )NTNé,  )FN©N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r@   r5   r   r   r    Úboolr7   rI   rC   rQ   rF   r   r   r   r(   r(   =   s“   „ ñ"ðH €FØ€Lð
 !%ØØ6:Øñ$àð$ð ˜‘ð$ð ð	$ð
 Ð2Ñ3ð$ð ó$ñ,1\˜Cð 1\¨tð 1\ÀhÈsÁmó 1\ófò&	:ó.r   r(   c                   ó>   ‡ — e Zd ZdZdˆ fd„	Zd„ Zd„ Zdd„Zd„ Zˆ xZ	S )	r,   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                 óJ   •— t         ‰| �  |«       |€d n|g| _        d | _        y r[   )Úsuperr7   Úinner_tableÚ	child_bar)r6   Ú	num_stepsÚcolumn_namesÚ	__class__s      €r   r7   z NotebookTrainingTracker.__init__Ü   s(   ø€ Ü‰Ñ˜Ô#Ø#/Ð#7™4¸l¸^ˆÔØˆ�r   c                 ó^  — t        | j                  | j                  | j                  | j                  | j
                  «      | _        | j                  �(| xj                  t        | j                  «      z  c_        | j                  �)| xj                  | j                  j                  z  c_        | j                  €:t        j                  t        j                  | j                  «      d¬«      | _
        y | j                  j                  t        j                  | j                  «      «       y rT   )r   r   r   r   r   r   r"   rd   r&   re   r2   rV   rQ   rW   rI   rX   s    r   rQ   zNotebookTrainingTracker.displayá   sÀ   € Ü*¨4¯:©:°t·z±zÀ4Ç;Á;ÐPT×PZÑPZÐ\`×\fÑ\fÓgˆŒØ×ÑÐ'Ø�NŠNÔ0°×1AÑ1AÓBÑB�NØ�>‰>Ð%Ø�NŠN˜dŸn™n×6Ñ6Ñ6�NØ�;‰;ÐÜŸ,™,¤t§y¡y°·±Ó'@ÈTÔRˆD�Kà�K‰K×ÑœtŸy™y¨¯©Ó8Õ9r   c                 ó*  — | j                   €8t        |j                  «       «      t        |j                  «       «      g| _         y| j                   d   }|j                  «       D ]  }||vsŒ|j	                  |«       Œ || j                   d<   t        | j                   «      dkD  r³| j                   d   }| j                   d   d   }|d   ||   k7  r3| j                   j	                  |D �cg c]  }||v r||   nd‘Œ c}«       y|}|D ],  }||j                  «       vsŒ||j                  |«         ||<   Œ. |D �cg c]  }||   ‘Œ	 c}| j                   d<   y| j                   j	                  |D �cg c]  }||   ‘Œ	 c}«       yc c}w c c}w c c}w )z…
        Write the values in the inner table.

        Args:
            values (`Dict[str, float]`): The values to display.
        Nr   r   éÿÿÿÿzNo Log)rd   ÚlistÚkeysÚvaluesÚappendrP   Úindex)r6   rn   ÚcolumnsÚkeyÚlast_valuesÚfirst_columnÚcÚ
new_valuess           r   Ú
write_linez"NotebookTrainingTracker.write_lineì   sŠ  € ð ×ÑÐ#Ü $ V§[¡[£]Ó 3´T¸&¿-¹-»/Ó5JÐKˆDÕà×&Ñ& qÑ)ˆGØ—{‘{“}ò (�Ø˜gÒ%Ø—N‘N 3Õ'ð(ð #*ˆD×Ñ˜QÑÜ�4×#Ñ#Ó$ qÒ(Ø"×.Ñ.¨rÑ2�Ø#×/Ñ/°Ñ2°1Ñ5�Ø˜q‘> V¨LÑ%9Ò9à×$Ñ$×+Ñ+Ð]dÖ,eÐXY¸!¸v¹+¨V°AªYÈ8Ñ-SÒ,eÕfð "(�JØ$ò J˜Ø J§O¡OÓ$5Ò5Ø,7¸¿¹ÀaÓ8HÑ,I˜J qšMðJð DKÖ+K¸a¨J°q«MÒ+K�D×$Ñ$ RÒ(à× Ñ ×'Ñ'¸GÖ(D°q¨°«Ò(DÕEùò -fùò ,Lùâ(Ds   Ã+FÄ<FÅ2Fc                 óB   — t        ||| |¬«      | _        | j                  S )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   r+   r   )r(   re   )r6   r   r   r   s       r   Ú	add_childz!NotebookTrainingTracker.add_child  s!   € ô -¨U¸6È$ÐV[Ô\ˆŒØ�~‰~Ðr   c                 ó2   — d| _         | j                  «        y)z0
        Closes the child progress bar.
        N)re   rQ   rX   s    r   Úremove_childz$NotebookTrainingTracker.remove_child  s   € ð ˆŒØ�‰�r   r[   )NrZ   )
r\   r]   r^   r_   r7   rQ   rw   ry   r{   Ú__classcell__)rh   s   @r   r,   r,   Ò   s"   ø„ ñõò
	:òFó>ör   r,   c                   óF   — e Zd ZdZd„ Zd„ Zd„ Zdd„Zd„ Zdd„Z	dd	„Z
d
„ Zy)ÚNotebookProgressCallbackzŠ
    A [`TrainerCallback`] that displays the progress of training or evaluation, optimized for Jupyter Notebooks or
    Google colab.
    c                 ó.   — d | _         d | _        d| _        y )NF)Útraining_trackerÚprediction_barÚ_force_next_updaterX   s    r   r7   z!NotebookProgressCallback.__init__&  s   € Ø $ˆÔØ"ˆÔØ"'ˆÕr   c                 ó   — |j                   t        j                  k(  rdnd| _        d| _        d| _        | j                  gdgz   }|j                   t        j                  k7  r|j                  d«       t        |j                  |«      | _
        y )NÚEpochÚStepr   úTraining LossúValidation Loss)Úeval_strategyr   ÚEPOCHrt   Útraining_lossÚlast_logÚNOro   r,   Ú	max_stepsr€   )r6   ÚargsÚstateÚcontrolÚkwargsrg   s         r   Úon_train_beginz'NotebookProgressCallback.on_train_begin+  s}   € Ø'+×'9Ñ'9Ô=M×=SÑ=SÒ'S™GÐY_ˆÔØˆÔØˆŒØ×)Ñ)Ð*¨oÐ->Ñ>ˆØ×ÑÔ!1×!4Ñ!4Ò4Ø×ÑÐ 1Ô2Ü 7¸¿¹ÈÓ VˆÕr   c                 ó&  — t        |j                  «      |j                  k(  rt        |j                  «      n|j                  d›}| j                  j                  |j                  dz   d|› d|j
                  › �| j                  ¬«       d| _        y )NrN   r   úEpoch rM   ©r1   r8   F)r   Úepochr€   rI   Úglobal_stepÚnum_train_epochsr‚   )r6   rŽ   r�   r�   r‘   r–   s         r   Úon_step_endz$NotebookProgressCallback.on_step_end4  s�   € Ü$'¨¯©Ó$4¸¿¹Ò$C”�E—K‘KÔ ÈEÏKÉKÐX[ÐK\ˆØ×Ñ×$Ñ$Ø×Ñ Ñ!Ø˜U˜G 1 U×%;Ñ%;Ð$<Ð=Ø×0Ñ0ð 	%ô 	
ð
 #(ˆÕr   Nc                 ón  — t        |«      sy | j                  €k| j                  �*| j                  j                  t	        |«      «      | _        nt        t	        |«      «      | _        | j                  j                  d«       y | j                  j                  | j                  j                  dz   «       y )Nr   )r   r�   r€   ry   rP   r(   rI   r   )r6   rŽ   r�   r�   Úeval_dataloaderr‘   s         r   Úon_prediction_stepz+NotebookProgressCallback.on_prediction_step=  sŽ   € Ü˜/Ô*ØØ×ÑÐ&Ø×$Ñ$Ð0Ø&*×&;Ñ&;×&EÑ&EÄcÈ/ÓFZÓ&[�Õ#ä&9¼#¸oÓ:NÓ&O�Ô#Ø×Ñ×&Ñ& qÕ)à×Ñ×&Ñ& t×':Ñ':×'@Ñ'@À1Ñ'DÕEr   c                 ó^   — | j                   �| j                   j                  «        d | _         y r[   )r�   rF   ©r6   rŽ   r�   r�   r‘   s        r   Ú
on_predictz#NotebookProgressCallback.on_predictI  s(   € Ø×ÑÐ*Ø×Ñ×%Ñ%Ô'Ø"ˆÕr   c                 ó¬   — |j                   t        j                  k(  r7d|v r2d|d   i}|j                  |d<   | j                  j                  |«       y y y )NÚlossr†   r…   )rˆ   r   rŒ   r—   r€   rw   )r6   rŽ   r�   r�   Úlogsr‘   rn   s          r   Úon_logzNotebookProgressCallback.on_logN  sW   € à×ÑÔ!1×!4Ñ!4Ò4¸À4¹Ø% t¨F¡|Ð4ˆFà"×.Ñ.ˆF�6‰NØ×!Ñ!×,Ñ,¨VÕ4ð	 :HÐ4r   c                 ó–  — | j                   ��·dddœ}t        |j                  «      D ]  }d|v sŒ|d   |d<    n | j                  dk(  rt	        |j
                  «      |d<   n|j                  |d<   d}|D ]+  }	|	j                  d«      sŒt        j                  d	d
|	«      }Œ- |j                  dd «      }
|j                  dd «      }
|j                  |› d�d «      }
|j                  |› d�d «      }
|j                  |› d�d «      }
|j                  |› d�d «      }
|j                  «       D ]R  \  }	}|	j                  d«      }dj                  |dd  D �cg c]  }|j                  «       ‘Œ c}«      }|dk(  rd}|||<   ŒT | j                   j                  |«       | j                   j!                  «        d | _        d| _        y y c c}w )NzNo log)r†   r‡   r¡   r†   r„   r…   ÚevalÚ_lossz\_loss$r.   Ú
total_flosr–   Ú_runtimeÚ_samples_per_secondÚ_steps_per_secondÚ_jit_compilation_timeÚ_rK   r   ÚLossr‡   T)r€   ÚreversedÚlog_historyrt   r   r–   r—   ÚendswithÚreÚsubÚpopr!   ÚsplitÚjoinÚ
capitalizerw   r{   r�   r‚   )r6   rŽ   r�   r�   Úmetricsr‘   rn   ÚlogÚmetric_key_prefixÚkr¬   ÚvÚsplitsÚpartÚnames                  r   Úon_evaluatez$NotebookProgressCallback.on_evaluateV  sß  € Ø× Ñ Ñ,Ø'/ÀHÑMˆFÜ × 1Ñ 1Ó2ò �Ø˜S’=Ø.1°&©k�F˜?Ñ+Ùðð
 × Ñ  GÒ+Ü"% e§k¡kÓ"2��w’à!&×!2Ñ!2��v‘Ø &ÐØò B�Ø—:‘:˜gÕ&Ü(*¯©¨z¸2¸qÓ(AÑ%ðBð —‘˜L¨$Ó/ˆAØ—‘˜G TÓ*ˆAØ—‘Ð0Ð1°Ð:¸DÓAˆAØ—‘Ð0Ð1Ð1DÐEÀtÓLˆAØ—‘Ð0Ð1Ð1BÐCÀTÓJˆAØ—‘Ð0Ð1Ð1FÐGÈÓNˆAØŸ™›ò !‘��1ØŸ™ ›�Ø—x‘x¸vÀaÀb¸zÖ J°t §¡Õ!2Ò JÓK�Ø˜6’>à,�DØ ��t’ð!ð ×!Ñ!×,Ñ,¨VÔ4Ø×!Ñ!×.Ñ.Ô0Ø"&ˆDÔà&*ˆDÕ#ðA -ùò. !Ks   ÅG
c                 ó¨   — | j                   j                  |j                  dt        |j                  «      › d|j
                  › �d¬«       d | _         y )Nr”   rM   Tr•   )r€   rI   r—   r   r–   r˜   rž   s        r   Úon_train_endz%NotebookProgressCallback.on_train_endy  sR   € Ø×Ñ×$Ñ$Ø×ÑØœS §¡Ó-Ð.¨a°×0FÑ0FÐ/GÐHØð 	%ô 	
ð
 !%ˆÕr   r[   )r\   r]   r^   r_   r7   r’   r™   rœ   rŸ   r£   r¿   rÁ   r   r   r   r~   r~      s2   „ ñò
(ò
Wò(ó
Fò#ó
5ó!+óF%r   r~   )rZ   )r3   r±   r:   Útypingr   ÚIPython.displayrQ   rV   Útrainer_callbackr   Útrainer_utilsr   r   r   r   r&   r(   r,   r~   r   r   r   ú<module>rÆ      sW   ðó 
Û 	Û Ý å å .ß 8òDóò÷"R.ñ R.ôjKÐ1ô Kô\_%˜õ _%r   