L i=ddlZddlZddlZddlmZddlmZddlm Z ddl m Z m Z dZ ddZdZGd d ZGd d eZGd de Zy)N)Optional)TrainerCallback)IntervalStrategy has_lengthcpt|}|dz|dzdz|dz}}}|dk7r |d|dd|dS|dd|dS)z$Format `t` (in seconds) to (h):mm:ssi<r:02d)int)thmss a/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/transformers/utils/notebook.py format_timers] AA4i!r'RR!qA%&!VaS!C!C !CAc7!Ac71CCc &d|d|d|d|d|d S)Nz
z z
)valuetotalprefixlabelwidths rhtml_progress_barr!s< hwgeWOE7C g  rcd}|dz }|dD] }|d|dz } |dz }|ddD];}|d z }|D]*}t|tr|d n t|}|d |d z },|d z }=|dz }|S)z*Put the texts in `items` in an HTML table.z% z* rz z Nz z.6fz z z
z
z

) isinstancefloatstr)items html_codeilineelts rtext_to_html_tabler&,s|j#n-|j!|j|j|z z|_|j|||_||_|j|jdk(rd|_ yt%t'|j(|jz d|_ yy)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. Nrr)rr1r0time start_time last_time start_value elapsed_timepredicted_remainingwarmup first_callswait_for update_barminraverage_time_per_itemr*closemaxr r5)r6rr8r1 current_times rupdatezNotebookProgressBar.update{s  "DL ?? "/3yy{ :DOdn16 6D t;? ?D  8#{{D DM OOE " doo %l  T--1Uc$//TXTaTaBacgcmcm>n5n!#  A% 99;L ,t >D t'''-1->->%$JZJZBZ-[*-1* " +/(zzJJL++7+/+E+EV[I[+\( OOE "#DO)DN**28R8RVW8W ! #C(9(9Drr?rrEr1display)r6rr1 spaced_values rrCzNotebookProgressBar.update_bars[c#djj/2SU_DEE R    $\N!DJJDJ  % % -\N!DJJ4::,a DeZdZdZdfd ZdZdZddZdZxZ 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). cJt|||dn|g|_d|_yr[)superr7 inner_table child_bar)r6 num_steps column_names __class__s rr7z NotebookTrainingTracker.__init__s( ##/#74l^rc^t|j|j|j|j|j |_|j(|xj t|jz c_|j)|xj |jj z c_|j:tjtj|j d|_ y|jjtj|j yrT)rrrrrrr"rdr&rer2rVrQrWrIrXs rrQzNotebookTrainingTracker.displays*4::tzz4;;PTPZPZ\`\f\fg    ' NN01A1AB BN >> % NNdnn66 6N ;; ,,tyy'@TRDK KK  tyy8 9rc |j8t|jt|jg|_y|jd}|D]}||vs|j |||jd<t |jdkDr|jd}|jdd}|d||k7r:|jj |Dcgc]}|j |dc}y|}|D]}||vs||j|||< |Dcgc]}|| c}|jd<y|jj |Dcgc]}|| c}ycc}wcc}wcc}w)z Write the values in the inner table. Args: values (`dict[str, float]`): The values to display. NrrzNo Log)rdlistkeysvaluesappendrPgetindex)r6rncolumnskey last_values first_columnc new_valuess r write_linez"NotebookTrainingTracker.write_linesw    # $V[[] 3T&--/5JKD &&q)G (g%NN3' (#*D  Q 4##$q("..r2 #//215 q>VL%99$$++g,VVZZ8-D,VW"(J$JJ.,7 a8H,IJqMJDK+KaJqM+KD$$R(  ''G(Dq(DE-W,L(DsE1' E6 E;cBt|||||_|jS)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. )rr+r)r(re)r6rrrs r add_childz!NotebookTrainingTracker.add_child s!-U6$V[\~~rc2d|_|jy)z0 Closes the child progress bar. N)rerQrXs r remove_childz$NotebookTrainingTracker.remove_childs rr[)NrZ) r\r]r^r_r7rQrxrzr| __classcell__)rhs@rr,r,s" :F> rr,cFeZdZdZdZdZdZd dZdZd dZ d d Z d Z y) NotebookProgressCallbackz A [`TrainerCallback`] that displays the progress of training or evaluation, optimized for Jupyter Notebooks or Google colab. c.d|_d|_d|_y)NF)training_trackerprediction_bar_force_next_updaterXs rr7z!NotebookProgressCallback.__init__&s $""'rc |jtjk(rdnd|_d|_d|_|jgdgz}|jtj k7r|jdt|j||_ y)NEpochStepr Training LossValidation Loss) eval_strategyrEPOCHru training_losslast_logNOror, max_stepsr)r6argsstatecontrolkwargsrgs ron_train_beginz'NotebookProgressCallback.on_train_begin+s}'+'9'9=M=S=S'SGY_ ))*o->>   !1!4!4 4    1 2 7 Vrc &t|j|jk(rt|jn |jd}|jj|jdzd|d|j |j d|_y)NrNrEpoch rMr1r8F)r epochrrI global_stepnum_train_epochsr)r6rrrrrs r on_step_endz$NotebookProgressCallback.on_step_end4s$' $4 $CEKK EKKX[K\ $$    !UG1U%;%;$<=00 % #(rNc nt|sy|jk|j*|jjt ||_nt t ||_|jj dy|jj |jjdzy)Nr)rrrrzrPr(rIr)r6rrreval_dataloaderrs ron_prediction_stepz+NotebookProgressCallback.on_prediction_step=s/*     &$$0&*&;&;&E&Ec/FZ&[#&9#o:N&O#    & &q )    & &t':':'@'@1'D Erc ^|j|jjd|_yr[)rrFr6rrrrs r on_predictz#NotebookProgressCallback.on_predictIs(    *    % % '"rc |jtjk(r7d|vr2d|di}|j|d<|jj |yyy)Nlossrr)rrrrrrx)r6rrrlogsrrns ron_logzNotebookProgressCallback.on_logNsW   !1!4!4 44%tF|4F"..F6N  ! ! , ,V 4 :H 4rc |jddd}t|jD]}d|vs|d|d<n|jdk(rt |j |d<n|j |d<d}|D]+} | jdstjd d | }-|jd d} |jd d} |j|d d} |j|dd} |j|dd} |j|dd} |jD]R\} } | jd} dj| ddD cgc]} | jc} }|dk(rd}| ||<T|jj||jj!d|_d|_yycc} w)NzNo log)rrrrrreval_lossz\_loss$r. total_flosr_runtime_samples_per_second_steps_per_second_jit_compilation_time_rKrLossrT)rreversed log_historyrur rrendswithresubpopr!splitjoin capitalizerxr|rr)r6rrrmetricsrrnlogmetric_key_prefixkrvsplitspartnames r on_evaluatez$NotebookProgressCallback.on_evaluateVs  ,'/HMF 1 12 S=.1&kF?+    G+"%ekk"2w!&!2!2v &  B::g&(*z2q(A% B L$/A GT*A 01:DAA 011DEtLA 011BCTJA 011FGNA  !1xxvabz Jt!2 JK6>,D t  !  ! ! , ,V 4  ! ! . . 0"&D &*D #A -.!KsG c |jj|jdt|jd|j dd|_y)NrrMTr)rrIrr rrrs r on_train_endz%NotebookProgressCallback.on_train_endysR $$   S-.a0F0F/GH % !%rr[) r\r]r^r_r7rrrrrrrrrrrr s2 ( W( F# 5!+F%rr)rZ)r3rr:typingrIPython.displayrQrVtrainer_callbackr trainer_utilsrrrrr&r(r,rrrrrsW .8D"R.R.jK1K\_%_%r