o
    h<                     @   s   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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   )TrainerCallback)IntervalStrategy
has_lengthc                 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s r   Q/var/www/html/ai/venv/lib/python3.10/site-packages/transformers/utils/notebook.py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_table,   s   
r"   c                   @   sz   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dZ
ddede	de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 S )N )r   r   r%   r&   r   
last_valuecommentoutput)selfr   r   r%   r&   r   r   r   r   __init__e   s   
zNotebookProgressBar.__init__Fr   force_updater*   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   r*   r)   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
   update_every)r,   r   r.   r*   current_timer   r   r   updatev   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   r3   r   r4   r   r:   r*   display)r,   r   r*   spaced_valuer   r   r   r8      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&   rF   r+   dispHTMLr?   r,   r   r   r   rF      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.Nr(   )r&   r+   r?   rJ   rK   rL   r   r   r   r;      s   zNotebookProgressBar.close)NTNr   )FNN)__name__
__module____qualname____doc__r5   r=   r
   r   r   boolr-   r?   r8   rF   r;   r   r   r   r   r#   =   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 rM   )superr-   inner_table	child_bar)r,   	num_stepscolumn_names	__class__r   r   r-      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 rH   )r   r   r   r   r   r   r   rT   r"   rU   r+   rJ   rF   rK   r?   rL   r   r   r   rF      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r|| j d }| j d d }|d | krX| j fdd|D  dS  |D ]}|  vrm|||  |< q\ 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 | ]}| v r | nd qS )zNo Logr   .0cvaluesr   r   
<listcomp>   s     z6NotebookTrainingTracker.write_line.<locals>.<listcomp>c                       g | ]} | qS r   r   r[   )
new_valuesr   r   r`         c                    ra   r   r   r[   r^   r   r   r`     rc   )rT   listkeysr_   appendrE   index)r,   r_   columnskeylast_valuesfirst_columnr]   r   )rb   r_   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#   rU   )r,   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)rU   rF   rL   r   r   r   remove_child  s   z$NotebookTrainingTracker.remove_childrM   )Nr   )
rN   rO   rP   rQ   r-   rF   rl   rm   rn   __classcell__r   r   rX   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_updaterL   r   r   r   r-   !  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)evaluation_strategyr   EPOCHrk   training_losslast_logNOrf   r'   	max_stepsrq   )r,   argsstatecontrolkwargsrW   r   r   r   on_train_begin&  s   
z'NotebookProgressCallback.on_train_beginc                 K   sV   t |j|jkrt |jn|jd}| jj|jd d| d|j | jd d| _d S )NrC   r   Epoch rB   r*   r.   F)r
   epochrq   r?   global_stepnum_train_epochsrs   )r,   r~   r   r   r   r   r   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   rr   rq   rm   rE   r#   r?   r   )r,   r~   r   r   eval_dataloaderr   r   r   r   on_prediction_step8  s   

z+NotebookProgressCallback.on_prediction_stepc                 K   s   | j d ur
| j   d | _ d S rM   )rr   r;   r,   r~   r   r   r   r   r   r   
on_predictD  s   


z#NotebookProgressCallback.on_predictc                 K   sB   |j tjkrd|v rd|d i}|j|d< | j| d S d S d S )Nlossrv   ru   )rx   r   r|   r   rq   rl   )r,   r~   r   r   logsr   r_   r   r   r   on_logI  s
   
zNotebookProgressCallback.on_logc                 K   s\  | j d urddd}t|jD ]}d|v r|d |d<  nq| jdkr+t|j|d< n|j|d< d}|D ]}	|	drBt	d	d
|	}q4|
dd }
|
dd }
|
| dd }
|
| dd }
|
| dd }
|
| dd }
| D ]!\}	}|	d}ddd |dd  D }|dkrd}|||< qw| j | | j   d | _d| _d S d S )NzNo log)rv   rw   r   rv   rt   ru   eval_lossz\_loss$r(   
total_flosr   _runtime_samples_per_second_steps_per_second_jit_compilation_time_r@   c                 S   s   g | ]}|  qS r   )
capitalize)r\   partr   r   r   r`   i  rc   z8NotebookProgressCallback.on_evaluate.<locals>.<listcomp>r   Lossrw   T)rq   reversedlog_historyrk   r
   r   r   endswithresubpopr   splitjoinrl   rn   rr   rs   )r,   r~   r   r   metricsr   r_   logmetric_key_prefixkr   vsplitsnamer   r   r   on_evaluateQ  s@   








z$NotebookProgressCallback.on_evaluatec                 K   s2   | j j|jdt|j d|j dd d | _ d S )Nr   rB   Tr   )rq   r?   r   r
   r   r   r   r   r   r   on_train_endt  s   
z%NotebookProgressCallback.on_train_endrM   )rN   rO   rP   rQ   r-   r   r   r   r   r   r   r   r   r   r   r   rp     s    	
	

#rp   )r   )r   r/   typingr   IPython.displayrF   rJ   trainer_callbackr   trainer_utilsr   r   r   r   r"   r#   r'   rp   r   r   r   r   <module>   s   
 N