L i_zdZddlZddlZddlZddlmZddlmZmZddlZ ddl m Z ddl m Z mZmZmZddlmZdd lmZej*eZeGd d ZGd d ZeGddeZGddZGddeZGddeZGddeZGddeZGddeeZ y)zJ Callbacks to use with the Trainer class and customize the training loop. N) dataclass)OptionalUnion)tqdm)HPSearchBackendIntervalStrategy SaveStrategy has_length)TrainingArguments)loggingceZdZUdZdZeeed<dZe ed<dZ e ed<dZ e ed<dZ e ed <dZ e ed <dZee ed <dZe ed <dZe ed <dZeed<dZeeeefed<dZeeed<dZee ed<dZeeed<dZeed<dZeed<dZeed<dZeeed<dZeeeeeee effed<dZ eeded<dZ!defdZ"e#defdZ$d Z%d!Z&y)" TrainerStatea A class containing the [`Trainer`] inner state that will be saved along the model and optimizer when checkpointing and passed to the [`TrainerCallback`]. In all this class, one step is to be understood as one update step. When using gradient accumulation, one update step may require several forward and backward passes: if you use `gradient_accumulation_steps=n`, then one update step requires going through *n* batches. Args: epoch (`float`, *optional*): Only set during training, will represent the epoch the training is at (the decimal part being the percentage of the current epoch completed). global_step (`int`, *optional*, defaults to 0): During training, represents the number of update steps completed. max_steps (`int`, *optional*, defaults to 0): The number of update steps to do during the current training. logging_steps (`int`, *optional*, defaults to 500): Log every X updates steps eval_steps (`int`, *optional*): Run an evaluation every X steps. save_steps (`int`, *optional*, defaults to 500): Save checkpoint every X updates steps. train_batch_size (`int`, *optional*): The batch size for the training dataloader. Only needed when `auto_find_batch_size` has been used. num_input_tokens_seen (`int`, *optional*, defaults to 0): When tracking the inputs tokens, the number of tokens seen during training (number of input tokens, not the number of prediction tokens). total_flos (`float`, *optional*, defaults to 0): The total number of floating operations done by the model since the beginning of training (stored as floats to avoid overflow). log_history (`list[dict[str, float]]`, *optional*): The list of logs done since the beginning of training. best_metric (`float`, *optional*): When tracking the best model, the value of the best metric encountered so far. best_global_step (`int`, *optional*): When tracking the best model, the step at which the best metric was encountered. Used for setting `best_model_checkpoint`. best_model_checkpoint (`str`, *optional*): When tracking the best model, the value of the name of the checkpoint for the best model encountered so far. is_local_process_zero (`bool`, *optional*, defaults to `True`): Whether or not this process is the local (e.g., on one machine if training in a distributed fashion on several machines) main process. is_world_process_zero (`bool`, *optional*, defaults to `True`): Whether or not this process is the global main process (when training in a distributed fashion on several machines, this is only going to be `True` for one process). is_hyper_param_search (`bool`, *optional*, defaults to `False`): Whether we are in the process of a hyper parameter search using Trainer.hyperparameter_search. This will impact the way data will be logged in TensorBoard. stateful_callbacks (`list[StatefulTrainerCallback]`, *optional*): Callbacks attached to the `Trainer` that should have their states be saved or restored. Relevant callbacks should implement a `state` and `from_state` function. Nepochr global_step max_stepsi logging_steps eval_steps save_stepstrain_batch_sizenum_train_epochsnum_input_tokens_seen total_flos log_history best_metricbest_global_stepbest_model_checkpointTis_local_process_zerois_world_process_zeroFis_hyper_param_search trial_name trial_paramsTrainerCallbackstateful_callbacksc|jg|_|ji|_yt|jtryi}|jD]}t|tst dt ||jj}||vr?t||ts ||g||<||j|j|j||<||_y)NzNAll callbacks passed to be saved must inherit `ExportableState`, but received ) rr$ isinstancedictExportableState TypeErrortype __class____name__listappendstate)selfr$callbacknames c/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/transformers/trainer_callback.py __post_init__zTrainerState.__post_init__us    #!D   " " *&(D # // 6 "$  33 @!(_>#himnviwhxy ))22--&&8&>E4Ft4L3M*40&t,33HNN4DE/7~~/?&t, @'9D # json_pathctjtj|dddz}t |dd5}|j |dddy#1swYyxYw) zDSave the content of this instance in JSON format inside `json_path`.T)indent sort_keys wutf-8encodingN)jsondumps dataclassesasdictopenwrite)r0r6 json_stringfs r3 save_to_jsonzTrainerState.save_to_jsonsTjj!3!3D!9!tTW[[ )S7 3 !q GGK  ! ! !s AA!ct|d5}|j}ddd|ditjS#1swY$xYw)z3Create an instance from the content of `json_path`.r=r>N)rDreadr@loads)clsr6rGtexts r3load_from_jsonzTrainerState.load_from_jsonsG)g . !668D &TZZ%&&  s AA cdD]A}t||d}||dkrtj||z}t||d|Cy)z Calculates and stores the absolute value for logging, eval, and save steps based on if it was a proportion or not. )r evalsave_stepsNr)getattrmathceilsetattr)r0argsr step_kind num_stepss r3 compute_stepszTrainerState.compute_stepssY 5 ?I 6&:;I$q= $ )i*? @I 62I>  ?r5cr|j,|j |j|j|_d|_|>ddlm}|j tjk(r |jn|}|||_||_ ||_ |j|_ |j|_ y)zI Stores the initial training references needed in `self` Nr) hp_params)hp_name_trialr!r"transformers.integrationsr]hp_search_backendrSIGOPT assignmentsrrrr)r0trainerrrtrialr]rcs r3init_training_referencesz%TrainerState.init_training_referencess ?? &7>>+E&oognn=DO    ;/6/H/HOLbLb/b%++hmK )+ 6D " 0%,%B%B%D"%,%B%B%D"r5)'r, __module__ __qualname____doc__rrfloat__annotations__rintrrrrrrrrrr-r'strrrrrboolrr r!r"rr$r4rH classmethodrOr[rfrJr5r3rr#s`9v"E8E?!KIsM3JJ&*hsm*c!"3"J*.Kd3:&'.#'K%'&*hsm*+/8C=/"&4&"&4&"'4' $J $FJL(4U3sD+@%A ABCJ<@&7!89@96!c! 's'' ?Er5rc,eZdZdZdefdZedZy)r(aj A class for objects that include the ability to have its state be saved during `Trainer._save_checkpoint` and loaded back in during `Trainer._load_from_checkpoint`. These must implement a `state` function that gets called during the respective Trainer function call. It should only include parameters and attributes needed to recreate the state at a particular time, to avoid utilizing pickle/maintain standard file IO writing. Example: ```python class EarlyStoppingCallback(TrainerCallback, ExportableState): def __init__(self, early_stopping_patience: int = 1, early_stopping_threshold: Optional[float] = 0.0): self.early_stopping_patience = early_stopping_patience self.early_stopping_threshold = early_stopping_threshold # early_stopping_patience_counter denotes the number of times validation metrics failed to improve. self.early_stopping_patience_counter = 0 def state(self) -> dict: return { "args": { "early_stopping_patience": self.early_stopping_patience, "early_stopping_threshold": self.early_stopping_threshold, }, "attributes": { "early_stopping_patience_counter": self.early_stopping_patience_counter, } } ```returnctd)NzInternal class that just calls the list of callbacks in order.cg|_|D]}|j|||_||_||_||_d|_d|_td|jDs#tjd|jzyy)Nc3<K|]}t|tywN)r&DefaultFlowCallback.0cbs r3 z+CallbackHandler.__init__..sP2:b"56PszThe Trainer will not work properly if you don't have a `DefaultFlowCallback` in its callbacks. You should add one before training with `trainer.add_callback(DefaultFlowCallback). The current list ofcallbacks is :) callbacks add_callbackmodelprocessing_class optimizer lr_schedulertrain_dataloadereval_dataloaderanyloggerwarning callback_list)r0rrrrrrs r3__init__zCallbackHandler.__init__s "B   b ! " 0"( $#PPP NN$$$% Qr5cPt|tr|n|}t|tr|n |j}||jDcgc]}|jc}vr)tj d|ddz|j z|jj|ycc}w)NzYou are adding a zH to the callbacks of this Trainer, but there is already one. The currentzlist of callbacks is :)r&r*r+rrrrr.)r0r1rcb_classcs r3rzCallbackHandler.add_callbacks%h5XZ8)(D98x?Q?Q T^^< < < NN#H:-uv+,$$%  b! =sB#ct|tr=|jD]-}t||s|jj||cSy|jD]&}||k(s |jj||cSyrr&r*rremover0r1rs r3 pop_callbackzCallbackHandler.pop_callbackss h %nn b(+NN))"-I  nn >NN))"-I r5ct|tr;|jD]+}t||s|jj|yy|jj|yrrrs r3remove_callbackzCallbackHandler.remove_callbacksR h %nn b(+NN))"-  NN ! !( +r5cFdjd|jDS)Nr;c3HK|]}|jjywr)r+r,rs r3rz0CallbackHandler.callback_list..sH2..Hs ")joinrrts r3rzCallbackHandler.callback_listsyyHHHHr5rXr/rc*|jd|||S)Nr call_eventr0rXr/rs r3rzCallbackHandler.on_init_end}dE7CCr5c8d|_|jd|||S)NFr)r~rrs r3rzCallbackHandler.on_train_begins ',$/ugFFr5c*|jd|||S)Nrrrs r3rzCallbackHandler.on_train_end~tUGDDr5c8d|_|jd|||S)NFr)rrrs r3rzCallbackHandler.on_epoch_begins $)!/ugFFr5c*|jd|||S)Nrrrs r3rzCallbackHandler.on_epoch_endrr5cTd|_d|_d|_|jd|||S)NFr)rrrrrs r3rzCallbackHandler.on_step_begins/""'#eWEEr5c*|jd|||S)Nrrrs r3rz%CallbackHandler.on_pre_optimizer_step s6eWMMr5c*|jd|||S)Nrrrs r3rz!CallbackHandler.on_optimizer_steps2D%IIr5c*|jd|||S)Nrrrs r3rzCallbackHandler.on_substep_ends/ugFFr5c*|jd|||S)Nrrrs r3rzCallbackHandler.on_step_endrr5c<d|_|jd||||S)NFrr)rrr0rXr/rrs r3rzCallbackHandler.on_evaluates#"'}dE7GTTr5c.|jd||||S)Nrrrrs r3rzCallbackHandler.on_predicts|T5'7SSr5c8d|_|jd|||S)NFr)rrrs r3rzCallbackHandler.on_saves#y$w??r5c<d|_|jd||||S)NFr)logs)rr)r0rXr/rrs r3rzCallbackHandler.on_log#s#"xugDIIr5c*|jd|||S)Nrrrs r3rz"CallbackHandler.on_prediction_step's3T5'JJr5c |jD]_}t|||||f|j|j|j|j |j |jd|}|^|}a|S)N)rrrrrr)rrTrrrrrr)r0eventrXr/rrr1results r3rzCallbackHandler.call_event*s !H-WXu- jj!%!6!6..!..!%!6!6 $ 4 4  F!  ! r5N)r,rgrhrirrrrpropertyrr rr}rrrrrrrrrrrrrrrrrJr5r3rrsH& " ,IID 1D,DQ_DG#4G\GTbGE!2E<ER`EG#4G\GTbGE!2E<ER`EF"3FLFSaF N*;NLN[iNJ&7J JWeJG#4G\GTbGD 1D,DQ_DU 1U,UQ_UT0TTP^T@-@l@^@J,J\JNJK'8KKXfKr5rc8eZdZdZdededefdZdededefdZy)rzx A [`TrainerCallback`] that handles the default flow of the training loop for logs, evaluation and checkpoints. rXr/rc |jdk(r|jrd|_|jtj k(r#|j|j zdk(rd|_|jtj k(r<|j|jzdk(r |j|jkrd|_ |jtj k(r2|jdkDr#|j|jzdk(rd|_|j|jk\r+d|_|jtj k(rd|_|S)NrTr)rlogging_first_steprlogging_strategyr STEPSr eval_strategyr eval_delayr save_strategyr rrrr~rs r3rzDefaultFlowCallback.on_step_endCs#    !d&=&=!%G   $4$:$: :u?P?PSXSfSf?fjk?k!%G    "2"8"8 8!!E$4$4495#4#44&*G #   ,"4"4 4  1$!!E$4$449"&G     /+/G (!!\%7%77&*#r5c |jtjk(rd|_|jtjk(r |j |j krd|_|jtjk(rd|_ |S)NT) rr EPOCHrrrrrrr rrs r3rz DefaultFlowCallback.on_epoch_endcsp  $4$:$: :!%G    !1!7!7 7DOOu{{s@ 1,Q_@ !2 < R` r5rcLeZdZdZd defdZdZdZd dZdZ d Z d d Z d Z y)ProgressCallbackz A [`TrainerCallback`] that displays the progress of training or evaluation. You can modify `max_str_len` to control how long strings are truncated when logging. max_str_lenc.d|_d|_||_y)a! Initialize the callback with optional max_str_len parameter to control string truncation length. Args: max_str_len (`int`): Maximum length of strings to display in logs. Longer strings will be truncated with a message. N) training_barprediction_barr)r0rs r3rzProgressCallback.__init__ys!"&r5c b|jrt|jd|_d|_y)NT)total dynamic_ncolsr)rrrr current_steprs r3rzProgressCallback.on_train_begins&  & & $5??$ OD r5c |jrD|jj|j|jz |j|_yyr)rrupdaterrrs r3rzProgressCallback.on_step_endsC  & &    $ $U%6%69J9J%J K % 1 1D  'r5Nc |jr\t|rP|j(tt ||j dud|_|jj dyyy)NT)rleaverr)rr rrlenrr)r0rXr/rrrs r3rz#ProgressCallback.on_prediction_stepsb  & &:o+F""*&*o.d6G6G46O_c'#    & &q ) ,G &r5c x|jr.|j|jjd|_yyrrrclosers r3rzProgressCallback.on_evaluate6  & &"".##))+"&D  'r5c x|jr.|j|jjd|_yyrr rs r3rzProgressCallback.on_predictr r5c |jr|ji}|jD]R\}}t|tr8t ||j kDr dt |d|j d||<N|||<T|jdd} d|vrt|dd|d<|jjt |yyy)Nz%[String too long to display, length: z > z/. Consider increasing `max_str_len` if needed.]rrr8) rrrwr&rmrrpoproundrE) r0rXr/rrr shallow_logsryrz_s r3rzProgressCallback.on_logs  & &4+<+<+HL  (1a%#a&43C3C*C?Axs4K[K[J\]HH!O '(LO (  t4A,&(-l7.CQ(G W%    # #C $5 6!,I &r5c `|jr"|jjd|_yyr)rrr rs r3rzProgressCallback.on_train_ends*  & &    # # % $D  'r5)dr) r,rgrhrirlrrrrrrrrrJr5r3rrss6 'C ' 2 *' ' 7&%r5rceZdZdZddZy)PrinterCallbackz? A bare [`TrainerCallback`] that just prints the logs. Nc X|jdd}|jr t|yy)Nr)rrprint)r0rXr/rrrrs r3rzPrinterCallback.on_logs' HH\4 (  & & $K 'r5r)r,rgrhrirrJr5r3rrs r5rcFeZdZdZd dedeefdZdZdZ dZ de fd Z y ) EarlyStoppingCallbacka1 A [`TrainerCallback`] that handles early stopping. Args: early_stopping_patience (`int`): Use with `metric_for_best_model` to stop training when the specified metric worsens for `early_stopping_patience` evaluation calls. early_stopping_threshold(`float`, *optional*): Use with TrainingArguments `metric_for_best_model` and `early_stopping_patience` to denote how much the specified metric must improve to satisfy early stopping conditions. ` This callback depends on [`TrainingArguments`] argument *load_best_model_at_end* functionality to set best_metric in [`TrainerState`]. Note that if the [`TrainingArguments`] argument *save_steps* differs from *eval_steps*, the early stopping will not occur until the next save step. early_stopping_patienceearly_stopping_thresholdc.||_||_d|_y)Nrrrearly_stopping_patience_counter)r0rrs r3rzEarlyStoppingCallback.__init__s'>$(@%/0,r5c|jrtjntj}|j8|||jr-t ||jz |j kDrd|_y|xjdz c_y)Nrr)greater_is_betternpgreaterlessrabsrr)r0rXr/r metric_valueoperators r3check_metric_valuez(EarlyStoppingCallback.check_metric_valuesl!%!7!72::RWW    $ \5#4#4 5L5#4#4458U8UU34D 0  0 0A 5 0r5c |jstjd|jJd|jt j k7sJdy)NzUsing EarlyStoppingCallback without load_best_model_at_end=True. Once training is finished, the best model will not be loaded automatically.zBEarlyStoppingCallback requires metric_for_best_model to be definedzAEarlyStoppingCallback requires IntervalStrategy of steps or epoch)load_best_model_at_endrrmetric_for_best_modelrr NOrs r3rz$EarlyStoppingCallback.on_train_beginsb** NN^ ))5 P 5!!%5%8%88 O 8r5c |j}|jdsd|}|j|}|tj d|dy|j |||||j |jk\rd|_yy)Neval_z@early stopping required metric_for_best_model, but did not find z so early stopping is disabledT) r+ startswithgetrrr(rrr~)r0rXr/rrrmetric_to_checkr&s r3rz!EarlyStoppingCallback.on_evaluates44))'2 %o%67O{{?3   NNRSbRcd   eWlC  / /43O3O O+/G ( Pr5rqcR|j|jdd|jidS)N)rrrrrrts r3r/zEarlyStoppingCallback.states7,0+G+G,0,I,I 243W3W   r5N)rg) r,rgrhrirlrrjrr(rrr'r/rJr5r3rrs< 11S[\aSb1 6  0"  t  r5r)!rirBr@rUrtypingrrnumpyr" tqdm.autor trainer_utilsrr r r training_argsr utilsr get_loggerr,rrr(r}r#rrrrrrJr5r3r:s !"VV,   H % XEXE XEv))X : _:  : zO O d@o@F2/2jG%G%ToI O_I r5