o
    h=                     @   s   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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   o/var/www/html/construction_image-detection-poc/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                   @   s~   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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 | _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__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
   r/   )r0   r   r2   r+   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   r7   r   r8   r   r>   r+   display)r0   r   r+   spaced_valuer   r   r   r<      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&   rI   r,   dispHTMLrB   r0   r   r   r   rI      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,   rB   rM   rN   rO   r   r   r   r?      s   zNotebookProgressBar.close)NTNr   )FNN)__name__
__module____qualname____doc__r9   r/   r
   r   r   boolr1   rB   r<   rI   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 rP   )superr1   inner_table	child_bar)r0   	num_stepscolumn_names	__class__r   r   r1      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 rK   )r   r   r   r   r   r   r   rW   r"   rX   r,   rM   rI   rN   rB   rO   r   r   r   rI      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   rc         c                    rd   r   r   r^   ra   r   r   rc   	  rf   )rW   listkeysrb   appendrH   index)r0   rb   columnskeylast_valuesfirst_columnr`   r   )re   rb   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#   rX   )r0   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)rX   rI   rO   r   r   r   remove_child  s   z$NotebookTrainingTracker.remove_childrP   )Nr   )
rQ   rR   rS   rT   r1   rI   ro   rp   rq   __classcell__r   r   r[   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_updaterO   r   r   r   r1   &  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   EPOCHrn   training_losslast_logNOri   r'   	max_stepsrt   )r0   argsstatecontrolkwargsrZ   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 )NrF   r   Epoch rE   r+   r2   F)r
   epochrt   rB   global_stepnum_train_epochsrv   )r0   r   r   r   r   r   r   r   r   on_step_end4  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   ru   rt   rp   rH   r#   rB   r   )r0   r   r   r   eval_dataloaderr   r   r   r   on_prediction_step=  s   

z+NotebookProgressCallback.on_prediction_stepc                 K   s   | j d ur
| j   d | _ d S rP   )ru   r?   r0   r   r   r   r   r   r   r   
on_predictI  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lossry   rx   )r{   r   r   r   rt   ro   )r0   r   r   r   logsr   rb   r   r   r   on_logN  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)ry   rz   r   ry   rw   rx   eval_lossz\_loss$r(   
total_flosr   _runtime_samples_per_second_steps_per_second_jit_compilation_time_rC   c                 S   s   g | ]}|  qS r   )
capitalize)r_   partr   r   r   rc   n  rf   z8NotebookProgressCallback.on_evaluate.<locals>.<listcomp>r   Lossrz   T)rt   reversedlog_historyrn   r
   r   r   endswithresubpopr   splitjoinro   rq   ru   rv   )r0   r   r   r   metricsr   rb   logmetric_key_prefixkr   vsplitsnamer   r   r   on_evaluateV  s@   








z$NotebookProgressCallback.on_evaluatec                 K   s2   | j j|jdt|j d|j dd d | _ d S )Nr   rE   Tr   )rt   rB   r   r
   r   r   r   r   r   r   on_train_endy  s   
z%NotebookProgressCallback.on_train_endrP   )rQ   rR   rS   rT   r1   r   r   r   r   r   r   r   r   r   r   r   rs      s    	
	

#rs   )r   )r-   r   r3   typingr   IPython.displayrI   rM   trainer_callbackr   trainer_utilsr   r   r   r   r"   r#   r'   rs   r   r   r   r   <module>   s   
 N