o
    ri>                     @   s   d dl Z d dlZd dlZd dlmZmZ d dlmZ ddl	m
Z
 ddlmZmZ edZdedB ded	efd
dZdd ZdddZdd ZG dd dZG dd deZG dd de
ZdS )    N)OptionalTypeVar   )TrainerCallback)IntervalStrategy
has_length_Txmsgreturnc                 C   s   | d u rt || S N)RuntimeError)r	   r
    r   i/lsinfo/ai/hellotax_ai/llm_service/venv_embed/lib/python3.10/site-packages/transformers/utils/notebook.py_require   s   r   c                 C   sZ   t | } | d | d d | d }}}|dk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sr   r   r   format_time#   s    2r   ,  c                 C   s$   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>
    r   )valuetotalprefixlabelwidthr   r   r   html_progress_bar*   s   r    c                 C   s   d}|d7 }| d D ]
}|d| d7 }q
|d7 }| dd D ]$}|d	7 }|D ]}t |tr2|d
nt|}|d| d7 }q'|d7 }q|d7 }|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eltr   r   r   text_to_html_table5   s   
r*   c                   @   s~   e Zd ZdZdZdZ				ddededB d	ed
e	d def
ddZ
ddedededB fddZdddZdd Zdd ZdS )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?NTr   r   r   leaveparentNotebookTrainingTrackerr   c                 C   s`   || _ |d u r	dn|| _|| _|| _|| _d | _d | _d | _d | _d | _	dt
jv r.d| _d S d S )N 
VSCODE_PIDg      ?)r   r   r-   r.   r   
last_valuecommentoutputr   r   osenvironupdate_every)selfr   r   r-   r.   r   r   r   r   __init__n   s   

zNotebookProgressBar.__init__Fr   force_updater3   c                 C   s  || _ |dur
|| _| jdu r1t  | _| _| | _| _d | _| _| j	| _
d| _| | dS || jkr:|s:dS |sM| j
dksM|t| j| j | jkr| j
dkrY|  j
d8  _
t }|| j | _|| jkrr| j|| j  | _nd| _|| jkr| j}d| _| js|   n| jdur| j| j|  | _| | || _|| _| jdu s| jdkrd| _dS tt| j| j d| _dS dS )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   r3   r2   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   r7   )r8   r   r:   r3   current_timer   r   r   update   sF   
&





zNotebookProgressBar.updatec              	   C   s  dt t| jt t|  t| }| jd u r$d| d| j d| _nH| jd u r:d| d| j dt| j | _n2d| d| j dt| j dt| j | _| jdkr]|  jd7  _n|  jdd	| j d
d7  _|  j| jd u s{t | jdkr}dnd| j d7  _| 	  d S )N [/z : < :z < r   z, +inf it/sz, r!   .2fz it/s])
lenr$   r   r?   r   r@   r   rF   r3   display)r8   r   r3   spaced_valuer   r   r   rD      s   &

"
4zNotebookProgressBar.update_barc                 C   sr   t | j| j| j| j| j| _| jd ur| j  d S | j	d u r-t
jt
| jdd| _	d S | j	t
| j d S NT)
display_id)r    r   r   r   r   r   r&   r.   rQ   r4   dispHTMLrJ   r8   r   r   r   rQ      s   


zNotebookProgressBar.displayc                 C   s2   | j du r| jdur| jtd dS dS dS )zCloses the progress bar.Nr0   )r.   r4   rJ   rU   rV   rW   r   r   r   rG      s   zNotebookProgressBar.close)NTNr   )FNr   )__name__
__module____qualname____doc__rA   r7   r   r$   boolr   r9   rJ   rD   rQ   rG   r   r   r   r   r+   F   s.    $

3r+   c                       sD   e Zd ZdZd fdd	Zdd Zdd Zdd
dZdd Z  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).
    Nc                    s*   t  | |d u rd n|g| _d | _d S r   )superr9   inner_table	child_bar)r8   	num_stepscolumn_names	__class__r   r   r9      s   
z NotebookTrainingTracker.__init__c                 C   s   t | j| j| j| j| j| _| jd ur|  jt| j7  _| j	d ur+|  j| j	j7  _| j
d u r>tjt| jdd| _
d S | j
t| j d S rS   )r    r   r   r   r   r   r&   r^   r*   r_   r4   rU   rQ   rV   rJ   rW   r   r   r   rQ      s   


zNotebookTrainingTracker.displayc                    s  | j du rt t g| _ dS | j d }D ]}||vr&|| q|| j d< t| j dkrx| j d }| j d d }|d | krV| j fdd|D  dS  |D ]}| vri|||  |< qZ fdd|D | j d< dS | j fdd|D  dS )	z
        Write the values in the inner table.

        Args:
            values (`dict[str, float]`): The values to display.
        Nr   r!   c                    s   g | ]}  |d qS )zNo Log)get.0cvaluesr   r   
<listcomp>	  s    z6NotebookTrainingTracker.write_line.<locals>.<listcomp>c                       g | ]} | qS r   r   rf   )
new_valuesr   r   rk         c                    rl   r   r   rf   ri   r   r   rk     rn   )r^   listkeysrj   appendrP   index)r8   rj   columnskeylast_valuesfirst_columnrh   r   )rm   rj   r   
write_line   s(   




z"NotebookTrainingTracker.write_liner   c                 C   s   t ||| |d| _| 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+   r_   )r8   r   r   r   r   r   r   	add_child  s   
z!NotebookTrainingTracker.add_childc                 C   s   d| _ |   dS )z0
        Closes the child progress bar.
        N)r_   rQ   rW   r   r   r   remove_child!  s   z$NotebookTrainingTracker.remove_childr   )Nr   )
rX   rY   rZ   r[   r9   rQ   rw   rx   ry   __classcell__r   r   rb   r   r/      s    	
r/   c                   @   sV   e Zd ZdZdd Zdd Zdd Zdd	d
Zdd ZdddZ	dddZ
dd ZdS )NotebookProgressCallbackz
    A [`TrainerCallback`] that displays the progress of training or evaluation, optimized for Jupyter Notebooks or
    Google colab.
    c                 C   s   d | _ d | _d| _d S )NF)training_trackerprediction_bar_force_next_updaterW   r   r   r   r9   /  s   
z!NotebookProgressCallback.__init__c                 K   sX   |j tjkrdnd| _d| _d| _| jgdg }|j tjkr#|d t|j	|| _
d S )NEpochStepr   Training LossValidation Loss)eval_strategyr   EPOCHrv   training_losslast_logNOrq   r/   	max_stepsr|   )r8   argsstatecontrolkwargsra   r   r   r   on_train_begin4  s   
z'NotebookProgressCallback.on_train_beginc                 K   s`   t |j|jkrt |jn|jd}t| jd}|j|jd d| d|j | jd d| _d S )NrN   z0on_train_begin must be called before on_step_endr!   Epoch rM   r3   r:   F)r   epochr   r|   rJ   global_stepnum_train_epochsr~   )r8   r   r   r   r   r   ttr   r   r   on_step_end=  s   $
z$NotebookProgressCallback.on_step_endNc                 K   sj   t |sd S | jd u r)| jd ur| jt|| _ntt|| _| jd d S | j| jjd  d S )Nr!   )r   r}   r|   rx   rP   r+   rJ   r   )r8   r   r   r   eval_dataloaderr   r   r   r   on_prediction_stepG  s   

z+NotebookProgressCallback.on_prediction_stepc                 K   s   | j d ur
| j   d | _ d S r   )r}   rG   )r8   r   r   r   r   r   r   r   
on_predictS  s   


z#NotebookProgressCallback.on_predictc                 K   sL   |j tjkr"d|v r$t| jd}d|d i}|j|d< || d S d S d S )Nlossz+on_train_begin must be called before on_logr   r   )r   r   r   r   r|   r   rw   )r8   r   r   r   logsr   r   rj   r   r   r   on_logX  s   
zNotebookProgressCallback.on_logc                 K   sP  t | jd}ddd}t|jD ]}d|v r|d |d<  nq| jdkr,t|j|d< n|j|d< |d u r7i }d}	|D ]}
|
d	rIt	
d
d|
}	q;|dd }|dd }||	 dd }||	 dd }||	 dd }| D ]!\}
}|
d}ddd |dd  D }|dkrd}|||< qu|| |  d | _d| _d S )Nz0on_train_begin must be called before on_evaluatezNo log)r   r   r   r   r   r   eval_lossz\_loss$r0   
total_flosr   _runtime_samples_per_second_steps_per_second_rK   c                 S   s   g | ]}|  qS r   )
capitalize)rg   partr   r   r   rk   {  rn   z8NotebookProgressCallback.on_evaluate.<locals>.<listcomp>r!   Lossr   T)r   r|   reversedlog_historyrv   r   r   r   endswithresubpopr%   splitjoinrw   ry   r}   r~   )r8   r   r   r   metricsr   r   rj   logmetric_key_prefixkr   vsplitsnamer   r   r   on_evaluatea  s@   







z$NotebookProgressCallback.on_evaluatec                 K   s<   t | jd}|j|jdt|j d|j dd d | _d S )Nz1on_train_begin must be called before on_train_endr   rM   Tr   )r   r|   rJ   r   r   r   r   )r8   r   r   r   r   r   r   r   r   on_train_end  s   
z%NotebookProgressCallback.on_train_endr   )rX   rY   rZ   r[   r9   r   r   r   r   r   r   r   r   r   r   r   r{   )  s    	



	%r{   )r   )r5   r   r;   typingr   r   IPython.displayrQ   rU   trainer_callbackr   trainer_utilsr   r   r   r$   r   r   r    r*   r+   r/   r{   r   r   r   r   <module>   s    
 N