CoolFace
Apppublic

Aluode/PerceptionLabPortable

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
notebook.cpython-310.pyc207 linesDownload Raw Back to __pycache__
1o

20�Yi�=�@s�ddlZddlZddlZddlmZddlmZddlm	Z	ddl3mZmZdd�Z
dd	d4�Zdd�ZGd
d�d�ZGdd�de�ZGdd�de	�ZdS)�N)�Optional�)�TrainerCallback)�IntervalStrategy�5has_lengthcCsZt|�}|d|dd|d}}}|dkr$|�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��E:\DocsHouse\542 percep lab latest\PerceptionLab\PerceptionLab_Portable\python_embed\Lib\site-packages\transformers/utils/notebook.py�format_times 2r�,cCs$d|�d|�d|�d|�d|�d�S)Nz6    <div>7      z8      <progress value='z' max='z' style='width:z<px; height:20px; vertical-align: middle;'></progress>9      z10    </div>11    r)�value�total�prefix�label�widthrrr�html_progress_bar!s�����rcCs�d}|d7}|dD]12}|d|�d�7}q13|d7}|dd�D]$}|d	7}|D]}t|t�r2|d14�nt|�}|d|�d�7}q'|d
7}q|d7}|S)z*Put the texts in `items` in an HTML table.z%<table border="1" class="dataframe">15z*  <thead>16 <tr style="text-align: left;">17rz18      <th>z</th>19z    </tr>20  </thead>21  <tbody>22�Nz	    <tr>23z.6fz24      <td>z</td>25z26    </tr>27z  </tbody>28</table><p>)�29isinstance�float�str)�items�	html_code�i�line�eltrrr�text_to_html_table,s30r"c@s~eZdZdZdZdZ				ddedeed	e	d31eddef32d
d�Z33ddede	deefdd�Zddd�Zdd�Z
dd�ZdS)�NotebookProgressBara�34    A progress par for display in a notebook.35 36    Class attributes (overridden by derived classes)37 38        - **warmup** (`int`) -- The number of iterations to do at the beginning while ignoring `update_every`.39        - **update_every** (`float`) -- Since calling the time takes some time, we only do it every presumed40          `update_every` seconds. The progress bar uses the average time passed up until now to guess the next value41          for which it will call the update.42 43    Args:44        total (`int`):45            The total number of iterations to reach.46        prefix (`str`, *optional*):47            A prefix to add before the progress bar.48        leave (`bool`, *optional*, defaults to `True`):49            Whether or not to leave the progress bar once it's completed. You can always call the50            [`~utils.notebook.NotebookProgressBar.close`] method to make the bar disappear.51        parent ([`~notebook.NotebookTrainingTracker`], *optional*):52            A parent object (like [`~utils.notebook.NotebookTrainingTracker`]) that spawns progress bars and handle53            their display. If set, the object passed must have a `display()` method.54        width (`int`, *optional*, defaults to 300):55            The width (in pixels) that the bar will take.56 57    Example:58 59    ```python60    import time61 62    pbar = NotebookProgressBar(100)63    for val in range(100):64        pbar.update(val)65        time.sleep(0.07)66    pbar.update(100)67    ```�g�������?NTrrr�leave�parent�NotebookTrainingTrackerrcCs`||_|dur	dn||_||_||_||_d|_d|_d|_d|_d|_	dt68jvr.d|_dSdS)N�Z69VSCODE_PIDg�?)
rrr%r&r�70last_value�comment�outputrr�os�environ�update_every)�selfrrr%r&rrrr�__init__es7172�zNotebookProgressBar.__init__Fr�force_updater*cCs�||_|dur73||_|jdur1t��|_|_||_|_d|_|_|j	|_74d|_|�|�dS||jkr:|s:dS|sM|j75dksM|t
|j|j|j�kr�|j76dkrY|j77d8_78t��}||j|_||jkrr|j||j|_nd|_||jkr�|j}d|_|js�|��n|jdur�|j|j||_|�|�||_||_|jdus�|jdkr�d|_dStt|j|j�d�|_dSdS)a�79        The main method to update the progress bar to `value`.80 81        Args:82            value (`int`):83                The value to use. Must be between 0 and `total`.84            force_update (`bool`, *optional*, defaults to `False`):85                Whether or not to force and update of the internal state and display (by default, the bar will wait for86                `value` to reach the value it predicted corresponds to a time of more than the `update_every` attribute87                since the last update to avoid adding boilerplate).88            comment (`str`, *optional*):89                A comment to add on the left of the progress bar.90        Nrr)rr*r)�time�91start_timeZ	last_timeZstart_value�elapsed_time�predicted_remaining�warmupZfirst_calls�wait_for�92update_bar�minr�average_time_per_itemr%�close�maxr93r.)r/rr1r*�current_timerrr�update{sF94&959697�9899100�zNotebookProgressBar.updatec	Csdtt|j��tt|��t|�}|jdur$d|�d|j�d�|_nH|jdur:d|�d|j�dt|j���|_n2d|�d|j�dt|j��dt|j���|_|jdkr]|jd7_n|jdd	|jd101�d�7_|j|jdus{t|j�dkr}dnd|j�d�7_|�	�dS)
N� �[�/z : < :z < rz, +inf it/sz, r�.2fz it/s�])102�lenrrr4rr5rr:r*�display)r/rr*Zspaced_valuerrrr8�s&103104"��1054zNotebookProgressBar.update_barcCsrt|j|j|j|j|j�|_|jdur|j��dS|j	dur-t106jt107�|j�dd�|_	dS|j	�t108�|j��dS�NT)Z109display_id)
rrrrrrrr&rEr+�disp�HTMLr>�r/rrrrE�s110111112zNotebookProgressBar.displaycCs2|jdur|jdur|j�t�d��dSdSdS)zCloses the progress bar.Nr()r&r+r>rGrHrIrrrr;�s�zNotebookProgressBar.close)NTNr)FN�N)�__name__�113__module__�__qualname__�__doc__r6r.r114rr�boolr0r>r8rEr;rrrrr#=s.$�����115�1163r#csDeZdZdZd�fdd�	Zdd�Zdd�Zdd117d�Zdd
�Z�Z	S)r'a�118    An object tracking the updates of an ongoing training with progress bars and a nice table reporting metrics.119 120    Args:121        num_steps (`int`): The number of steps during training. column_names (`list[str]`, *optional*):122            The list of column names for the metrics table (will be inferred from the first call to123            [`~utils.notebook.NotebookTrainingTracker.write_line`] if not set).124    Ncs*t��|�|durdn|g|_d|_dSrJ)�superr0�inner_table�	child_bar)r/Z	num_steps�column_names��	__class__rrr0�s125z NotebookTrainingTracker.__init__cCs�t|j|j|j|j|j�|_|jdur|jt|j�7_|j	dur+|j|j	j7_|j126dur>tjt�
|j�dd�|_127dS|j128�t�
|j��dSrF)rrrrrrrrQr"rRr+rGrErHr>rIrrrrE�s129130131zNotebookTrainingTracker.displaycs|jdurt����t����g|_dS|jd}�D]}||vr&|�|�q||jd<t|j�dkrx|jd}|jdd}|d�|krV|j��fdd�|D��dS��|D]}|�vri||�|��|<qZ�fdd�|D�|jd<dS|j��fdd�|D��dS)	z�132        Write the values in the inner table.133 134        Args:135            values (`dict[str, float]`): The values to display.136        Nrr�����csg|]}��|d��qS)zNo Log)�get��.0�c��valuesrr�137<listcomp>sz6NotebookTrainingTracker.write_line.<locals>.<listcomp>c�g|]}�|�qSrrrX)�138new_valuesrrr]�cr^rrrXr[rrr]	r`)rQ�list�keysr\�appendrD�index)r/r\�columns�key�last_values�first_columnrZr)r_r\r�139write_line�s(140141142�143144�z"NotebookTrainingTracker.write_linercCst||||d�|_|jS)a�145        Add a child progress bar displayed under the table of metrics. The child progress bar is returned (so it can be146        easily updated).147 148        Args:149            total (`int`): The number of iterations for the child progress bar.150            prefix (`str`, *optional*): A prefix to write on the left of the progress bar.151            width (`int`, *optional*, defaults to 300): The width (in pixels) of the progress bar.152        )rr&r)r#rR)r/rrrrrr�	add_childs153z!NotebookTrainingTracker.add_childcCsd|_|��dS)z0154        Closes the child progress bar.155        N)rRrErIrrr�remove_childsz$NotebookTrainingTracker.remove_childrJ)Nr)156rKrLrMrNr0rErirjrk�
__classcell__rrrTrr'�s	157
r'c@sVeZdZdZdd�Zdd�Zdd�Zdd	d158�Zdd�Zdd
d�Z	ddd�Z159dd�ZdS)�NotebookProgressCallbackz�160    A [`TrainerCallback`] that displays the progress of training or evaluation, optimized for Jupyter Notebooks or161    Google colab.162    cCsd|_d|_d|_dS)NF)�training_tracker�prediction_bar�_force_next_updaterIrrrr0&s163z!NotebookProgressCallback.__init__cKsX|jtjkrdnd|_d|_d|_|jgdg}|jtjkr#|�d�t|j	|�|_164dS)N�Epoch�Stepr�
Training Loss�Validation Loss)�
eval_strategyr�EPOCHrhZ
training_lossZlast_log�NOrcr'Z	max_stepsrn)r/�args�state�control�kwargsrSrrr�on_train_begin+s165z'NotebookProgressCallback.on_train_begincKsVt|j�|jkr
t|j�n|jd�}|jj|jdd|�d|j��|jd�d|_dS)NrBr�Epoch rA�r*r1F)r166�epochrnr>�global_step�num_train_epochsrp)r/rxryrzr{rrrr�on_step_end4s$�167z$NotebookProgressCallback.on_step_endNcKsjt|�sdS|jdur)|jdur|j�t|��|_ntt|��|_|j�d�dS|j�|jjd�dS)Nr)rrornrjrDr#r>r)r/rxryrzZeval_dataloaderr{rrr�on_prediction_step=s168169z+NotebookProgressCallback.on_prediction_stepcKs|jdur170|j��d|_dSrJ)ror;�r/rxryrzr{rrr�171on_predictIs172173174z#NotebookProgressCallback.on_predictcKsB|jtjkrd|vrd|di}|j|d<|j�|�dSdSdS)N�lossrsrr)rurrwr�rnri)r/rxryrzZlogsr{r\rrr�on_logNs175176�zNotebookProgressCallback.on_logcKs\|jdur�ddd�}t|j�D]}d|vr|d|d<nq|jdkr+t|j�|d<n|j|d<d}|D]}	|	�d�rBt�	d	d177|	�}q4|�178dd�}179|�180dd�}181|�182|�d
�d�}183|�184|�d�d�}185|�186|�d�d�}187|�188|�d�d�}189|��D]!\}	}|	�d�}d�
dd�|dd�D��}
|
dkr�d}
|||
<qw|j�|�|j��d|_d|_dSdS)NzNo log)rsrtr�rsrqrr�evalZ_lossz\_loss$r(Z190total_flosrZ_runtimeZ_samples_per_secondZ_steps_per_secondZ_jit_compilation_time�_r?cSsg|]}|���qSr)�191capitalize)rY�partrrrr]nr`z8NotebookProgressCallback.on_evaluate.<locals>.<listcomp>rZLossrtT)rn�reversedZlog_historyrhr192rr��endswith�re�sub�popr�split�joinrirkrorp)r/rxryrzZmetricsr{r\�logZmetric_key_prefix�kr��vZsplits�namerrr�on_evaluateVs@193194�195196197�198199200201�z$NotebookProgressCallback.on_evaluatecKs2|jj|jdt|j��d|j��dd�d|_dS)Nr}rATr~)rnr>r�r202rr�r�rrr�on_train_endys�203z%NotebookProgressCallback.on_train_endrJ)rKrLrMrNr0r|r�r�r�r�r�r�rrrrrm s	204	205206#rm)r)r,r�r2�typingr�IPython.displayrErGZtrainer_callbackrZ
trainer_utilsrrrrr"r#r'rmrrrr�<module>s207N
Aluode/PerceptionLabPortable · CoolFace