a
    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rH| 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   W/var/www/html/assistant/venv/lib/python3.9/site-packages/transformers/utils/notebook.pyformat_time   s     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 ]H}|d	7 }|D ].}t |trd|d
nt|}|d| d7 }qN|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                   @   sj   e Zd ZdZdZdZdeee e	ed edd	d
Z
dee	ee d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   NotebookTrainingTracker)r   r   leaveparentr   c                 C   s\   || _ |d u rdn|| _|| _|| _|| _d | _d | _d | _d | _d | _	dt
jv rXd| _d S )N Z
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__F)r   force_updater*   c                 C   s  || _ |dur|| _| jdu rbt  | _| _| | _| _d | _| _| j	| _
d| _| | n || jkrt|stdS |s| j
dks|t| j| j | jkr| j
dkr|  j
d8  _
t }|| j | _|| jkr| j|| j  | _nd| _|| jkr| j}d| _| js4|   n| jdur4| j| j|  | _| | || _|| _| jdu sb| jdkrjd| _ntt| j| j d| _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_timeZ	last_timeZstart_valueelapsed_timepredicted_remainingwarmupZfirst_callswait_for
update_barminr   average_time_per_itemr&   closemaxr
   r.   )r/   r   r1   r*   current_timer   r   r   update{   sB    
(



zNotebookProgressBar.updatec              	   C   s  dt t| jt t|  t| }| jd u rHd| d| j d| _n| jd u rtd| d| j dt| j | _ndd| 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 st | jdkrdnd| j d7  _| 	  d S )N [/z : < :z < r   z, +inf it/sz, r   .2fz it/s])
lenr   r   r4   r   r5   r   r:   r*   display)r/   r   r*   Zspaced_valuer   r   r   r8      s    &

"
4zNotebookProgressBar.update_barc                 C   sp   t | j| j| j| j| j| _| jd ur4| j  d S | j	d u rXt
jt
| jdd| _	n| j	t
| j d S NT)Z
display_id)r   r   r   r   r   r   r   r'   rE   r+   dispHTMLr>   r/   r   r   r   rE      s    


zNotebookProgressBar.displayc                 C   s*   | j du r&| jdur&| jtd dS )zCloses the progress bar.Nr(   )r'   r+   r>   rG   rH   rI   r   r   r   r;      s    zNotebookProgressBar.close)NTNr   )FN)N)__name__
__module____qualname____doc__r6   r.   r
   r   r   boolr0   r>   r8   rE   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 N)superr0   inner_table	child_bar)r/   Z	num_stepscolumn_names	__class__r   r   r0      s    z NotebookTrainingTracker.__init__c                 C   s   t | j| j| j| j| j| _| jd ur:|  jt| j7  _| j	d urV|  j| j	j7  _| j
d u rztjt| jdd| _
n| j
t| j d S rF   )r   r   r   r   r   r   r   rQ   r"   rR   r+   rG   rE   rH   r>   rI   r   r   r   rE      s    


zNotebookTrainingTracker.displayc                    s  | j du r&t t g| _ n| j d }D ]}||vr4|| q4|| j d< t| j dkr| j d }| j d d }|d | kr| j fdd|D  n@ |D ]}| vr|||  |< q fdd|D | j d< n| 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>       z6NotebookTrainingTracker.write_line.<locals>.<listcomp>c                    s   g | ]} | qS r   r   rX   )
new_valuesr   r   r]     r^   c                    s   g | ]} | qS r   r   rX   r[   r   r   r]   	  r^   )rQ   listkeysr\   appendrD   index)r/   r\   columnskeylast_valuesfirst_columnrZ   r   )r_   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#   rR   )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)rR   rE   rI   r   r   r   remove_child  s    z$NotebookTrainingTracker.remove_child)N)Nr   )
rJ   rK   rL   rM   r0   rE   rh   ri   rj   __classcell__r   r   rT   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_updaterI   r   r   r   r0   &  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rF|d t|j	|| _
d S )NEpochStepr   Training LossValidation Loss)eval_strategyr   EPOCHrg   Ztraining_lossZlast_logNOrb   r%   Z	max_stepsrm   )r/   argsstatecontrolkwargsrS   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 )NrB   r   Epoch rA   r*   r1   F)r
   epochrm   r>   global_stepnum_train_epochsro   )r/   rw   rx   ry   rz   r~   r   r   r   on_step_end4  s    $z$NotebookProgressCallback.on_step_endNc                 K   sh   t |sd S | jd u rP| jd ur4| jt|| _ntt|| _| jd n| j| jjd  d S )Nr   )r   rn   rm   ri   rD   r#   r>   r   )r/   rw   rx   ry   Zeval_dataloaderrz   r   r   r   on_prediction_step=  s    

z+NotebookProgressCallback.on_prediction_stepc                 K   s   | j d ur| j   d | _ d S rO   )rn   r;   r/   rw   rx   ry   rz   r   r   r   
on_predictI  s    

z#NotebookProgressCallback.on_predictc                 K   s:   |j tjkr6d|v r6d|d i}|j|d< | j| d S )Nlossrr   rq   )rt   r   rv   r   rm   rh   )r/   rw   rx   ry   Zlogsrz   r\   r   r   r   on_logN  s    
zNotebookProgressCallback.on_logc                 K   s\  | j d urXddd}t|jD ]}d|v r |d |d<  q>q | jdkrXt|j|d< n
|j|d< d}|D ]}	|	drjt	d	d
|	}qj|
dd }
|
dd }
|
| dd }
|
| dd }
|
| dd }
|
| dd }
| D ]D\}	}|	d}ddd |dd  D }|dkr,d}|||< q| j | | j   d | _d| _d S )NzNo log)rr   rs   r   rr   rp   rq   evalZ_lossz\_loss$r(   Z
total_flosr~   Z_runtimeZ_samples_per_secondZ_steps_per_secondZ_jit_compilation_time_r?   c                 S   s   g | ]}|  qS r   )
capitalize)rY   partr   r   r   r]   n  r^   z8NotebookProgressCallback.on_evaluate.<locals>.<listcomp>r   ZLossrs   T)rm   reversedZlog_historyrg   r
   r~   r   endswithresubpopr   splitjoinrh   rj   rn   ro   )r/   rw   rx   ry   Zmetricsrz   r\   logZmetric_key_prefixkr   vZ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|   rA   Tr}   )rm   r>   r   r
   r~   r   r   r   r   r   on_train_endy  s    z%NotebookProgressCallback.on_train_end)N)N)N)rJ   rK   rL   rM   r0   r{   r   r   r   r   r   r   r   r   r   r   rl      s   		


#rl   )r   )r,   r   r2   typingr   IPython.displayrE   rG   Ztrainer_callbackr   Ztrainer_utilsr   r   r   r   r"   r#   r%   rl   r   r   r   r   <module>   s   
 N