
    9i_                     Z   d Z ddlZddlZddlmZ ddlmZmZmZmZ ddl	Z
ddlmZ ddlmZmZ ddlmZ dd	lmZ  ej(                  e      Ze G d
 d             Ze G d d             Z G d d      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Zy)zJ
Callbacks to use with the Trainer class and customize the training loop.
    N)	dataclass)DictListOptionalUnion)tqdm   )IntervalStrategy
has_length)TrainingArguments)loggingc                   T   e Zd ZU dZdZee   ed<   dZe	ed<   dZ
e	ed<   dZe	ed<   dZe	ed	<   dZe	ed
<   dZe	ed<   dZeed<   dZeeeef      ed<   dZee   ed<   dZee   ed<   dZeed<   dZeed<   dZeed<   dZeed<   dZeeeeee	ef   f   ed<   d ZdefdZedefd       Z y)TrainerStateaN
  
    A class containing the [`Trainer`] inner state that will be saved along the model and optimizer when checkpointing
    and passed to the [`TrainerCallback`].

    <Tip>

    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.

    </Tip>

    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.
        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_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.
    Nepochr   global_step	max_stepsi  logging_steps
eval_steps
save_stepsnum_train_epochs
total_floslog_historybest_metricbest_model_checkpointTis_local_process_zerois_world_process_zeroFis_hyper_param_search
trial_nametrial_paramsc                 ,    | j                   g | _         y y N)r   selfs    g/var/www/html/backtest/airagagent/rag_env/lib/python3.12/site-packages/transformers/trainer_callback.py__post_init__zTrainerState.__post_init__d   s    #!D $    	json_pathc                     t        j                  t        j                  |       dd      dz   }t	        |dd      5 }|j                  |       ddd       y# 1 sw Y   yxY w)	zDSave the content of this instance in JSON format inside `json_path`.   T)indent	sort_keys
wutf-8encodingN)jsondumpsdataclassesasdictopenwrite)r#   r'   json_stringfs       r$   save_to_jsonzTrainerState.save_to_jsonh   sT    jj!3!3D!9!tTW[[)S73 	!qGGK 	! 	! 	!s   AA!c                     t        |dd      5 }|j                         }ddd        | di t        j                        S # 1 sw Y   $xY w)z3Create an instance from the content of `json_path`.rr.   r/   N )r5   readr1   loads)clsr'   r8   texts       r$   load_from_jsonzTrainerState.load_from_jsonn   sI     )S73 	q668D	&TZZ%&&	 	s   AA)!__name__
__module____qualname____doc__r   r   float__annotations__r   intr   r   r   r   r   r   r   r   r   strr   r   r   boolr   r   r   r   r   r%   r9   classmethodrA   r<   r&   r$   r   r   "   s   -^ "E8E?!KIsM3JJcJ*.Kd3:&'.#'K%'+/8C=/"&4&"&4&"'4'J<@L$sE#uc4"7889@"!c ! 's ' 'r&   r   c                   j    e Zd ZU dZdZeed<   dZeed<   dZeed<   dZ	eed<   dZ
eed<   d Zd	 Zd
 Zy)TrainerControlaA  
    A class that handles the [`Trainer`] control flow. This class is used by the [`TrainerCallback`] to activate some
    switches in the training loop.

    Args:
        should_training_stop (`bool`, *optional*, defaults to `False`):
            Whether or not the training should be interrupted.

            If `True`, this variable will not be set back to `False`. The training will just stop.
        should_epoch_stop (`bool`, *optional*, defaults to `False`):
            Whether or not the current epoch should be interrupted.

            If `True`, this variable will be set back to `False` at the beginning of the next epoch.
        should_save (`bool`, *optional*, defaults to `False`):
            Whether or not the model should be saved at this step.

            If `True`, this variable will be set back to `False` at the beginning of the next step.
        should_evaluate (`bool`, *optional*, defaults to `False`):
            Whether or not the model should be evaluated at this step.

            If `True`, this variable will be set back to `False` at the beginning of the next step.
        should_log (`bool`, *optional*, defaults to `False`):
            Whether or not the logs should be reported at this step.

            If `True`, this variable will be set back to `False` at the beginning of the next step.
    Fshould_training_stopshould_epoch_stopshould_saveshould_evaluate
should_logc                     d| _         y)z<Internal method that resets the variable for a new training.FN)rN   r"   s    r$   _new_trainingzTrainerControl._new_training   s
    $)!r&   c                     d| _         y)z9Internal method that resets the variable for a new epoch.FN)rO   r"   s    r$   
_new_epochzTrainerControl._new_epoch   s
    !&r&   c                 .    d| _         d| _        d| _        y)z8Internal method that resets the variable for a new step.FN)rP   rQ   rR   r"   s    r$   	_new_stepzTrainerControl._new_step   s     $r&   N)rB   rC   rD   rE   rN   rJ   rG   rO   rP   rQ   rR   rT   rV   rX   r<   r&   r$   rM   rM   v   sL    6 "'$&#t#K!OT!J*' r&   rM   c                      e Zd ZdZdededefdZdededefdZdededefdZ	dededefdZ
dededefd	Zdededefd
ZdededefdZdededefdZdededefdZdededefdZdededefdZdededefdZdededefdZy)TrainerCallbacka  
    A class for objects that will inspect the state of the training loop at some events and take some decisions. At
    each of those events the following arguments are available:

    Args:
        args ([`TrainingArguments`]):
            The training arguments used to instantiate the [`Trainer`].
        state ([`TrainerState`]):
            The current state of the [`Trainer`].
        control ([`TrainerControl`]):
            The object that is returned to the [`Trainer`] and can be used to make some decisions.
        model ([`PreTrainedModel`] or `torch.nn.Module`):
            The model being trained.
        tokenizer ([`PreTrainedTokenizer`]):
            The tokenizer used for encoding the data.
        optimizer (`torch.optim.Optimizer`):
            The optimizer used for the training steps.
        lr_scheduler (`torch.optim.lr_scheduler.LambdaLR`):
            The scheduler used for setting the learning rate.
        train_dataloader (`torch.utils.data.DataLoader`, *optional*):
            The current dataloader used for training.
        eval_dataloader (`torch.utils.data.DataLoader`, *optional*):
            The current dataloader used for training.
        metrics (`Dict[str, float]`):
            The metrics computed by the last evaluation phase.

            Those are only accessible in the event `on_evaluate`.
        logs  (`Dict[str, float]`):
            The values to log.

            Those are only accessible in the event `on_log`.

    The `control` object is the only one that can be changed by the callback, in which case the event that changes it
    should return the modified version.

    The argument `args`, `state` and `control` are positionals for all events, all the others are grouped in `kwargs`.
    You can unpack the ones you need in the signature of the event using them. As an example, see the code of the
    simple [`~transformer.PrinterCallback`].

    Example:

    ```python
    class PrinterCallback(TrainerCallback):
        def on_log(self, args, state, control, logs=None, **kwargs):
            _ = logs.pop("total_flos", None)
            if state.is_local_process_zero:
                print(logs)
    ```argsstatecontrolc                      y)zS
        Event called at the end of the initialization of the [`Trainer`].
        Nr<   r#   r[   r\   r]   kwargss        r$   on_init_endzTrainerCallback.on_init_end        	r&   c                      y)z<
        Event called at the beginning of training.
        Nr<   r_   s        r$   on_train_beginzTrainerCallback.on_train_begin   rb   r&   c                      y)z6
        Event called at the end of training.
        Nr<   r_   s        r$   on_train_endzTrainerCallback.on_train_end   rb   r&   c                      y)z<
        Event called at the beginning of an epoch.
        Nr<   r_   s        r$   on_epoch_beginzTrainerCallback.on_epoch_begin   rb   r&   c                      y)z6
        Event called at the end of an epoch.
        Nr<   r_   s        r$   on_epoch_endzTrainerCallback.on_epoch_end   rb   r&   c                      y)z
        Event called at the beginning of a training step. If using gradient accumulation, one training step might take
        several inputs.
        Nr<   r_   s        r$   on_step_beginzTrainerCallback.on_step_begin       
 	r&   c                      y)zU
        Event called at the end of an substep during gradient accumulation.
        Nr<   r_   s        r$   on_substep_endzTrainerCallback.on_substep_end   rb   r&   c                      y)z
        Event called at the end of a training step. If using gradient accumulation, one training step might take
        several inputs.
        Nr<   r_   s        r$   on_step_endzTrainerCallback.on_step_end  rm   r&   c                      y)z9
        Event called after an evaluation phase.
        Nr<   r_   s        r$   on_evaluatezTrainerCallback.on_evaluate  rb   r&   c                      y)z=
        Event called after a successful prediction.
        Nr<   )r#   r[   r\   r]   metricsr`   s         r$   
on_predictzTrainerCallback.on_predict  rb   r&   c                      y)z7
        Event called after a checkpoint save.
        Nr<   r_   s        r$   on_savezTrainerCallback.on_save  rb   r&   c                      y)z;
        Event called after logging the last logs.
        Nr<   r_   s        r$   on_logzTrainerCallback.on_log  rb   r&   c                      y)z7
        Event called after a prediction step.
        Nr<   r_   s        r$   on_prediction_stepz"TrainerCallback.on_prediction_step%  rb   r&   N)rB   rC   rD   rE   r   r   rM   ra   rd   rf   rh   rj   rl   ro   rq   rs   rv   rx   rz   r|   r<   r&   r$   rZ   rZ      sr   /b 1 , Q_ #4 \ Tb !2 < R` #4 \ Tb !2 < R` "3 L Sa #4 \ Tb  1 , Q_  1 , Q_ 0  P^ - l ^ , \ N '8  Xf r&   rZ   c                   B   e Zd ZdZd Zd Zd Zd Zed        Z	de
ded	efd
Zde
ded	efdZde
ded	efdZde
ded	efdZde
ded	efdZde
ded	efdZde
ded	efdZde
ded	efdZde
ded	efdZde
ded	efdZde
ded	efdZde
ded	efdZde
ded	efdZd Zy)CallbackHandlerz>Internal class that just calls the list of callbacks in order.c                    g | _         |D ]  }| j                  |        || _        || _        || _        || _        d | _        d | _        t        d | j                   D              s#t        j                  d| j                  z          y y )Nc              3   <   K   | ]  }t        |t                y wr!   )
isinstanceDefaultFlowCallback.0cbs     r$   	<genexpr>z+CallbackHandler.__init__.<locals>.<genexpr>:  s     P2:b"56Ps   zThe 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_callbackmodel	tokenizer	optimizerlr_schedulertrain_dataloadereval_dataloaderanyloggerwarningcallback_list)r#   r   r   r   r   r   r   s          r$   __init__zCallbackHandler.__init__/  s     	"Bb!	"
""( $#PPPNN$ $$% Qr&   c                 P   t        |t              r |       n|}t        |t              r|n|j                  }|| j                  D cg c]  }|j                   c}v r)t        j                  d| ddz   | j                  z          | j                  j                  |       y c c}w )NzYou are adding a zH to the callbacks of this Trainer, but there is already one. The currentzlist of callbacks is
:)r   type	__class__r   r   r   r   append)r#   callbackr   cb_classcs        r$   r   zCallbackHandler.add_callbackB  s    %h5XZ8)(D98x?Q?QT^^<<<NN#H:-uv+,$$%
 	b! =s   B#c                    t        |t              r=| j                  D ]-  }t        ||      s| j                  j                  |       |c S  y | j                  D ]&  }||k(  s	| j                  j                  |       |c S  y r!   r   r   r   remover#   r   r   s      r$   pop_callbackzCallbackHandler.pop_callbackM  ss    h%nn b(+NN))"-I
 nn >NN))"-Ir&   c                     t        |t              r;| j                  D ]+  }t        ||      s| j                  j                  |        y  y | j                  j                  |       y r!   r   r   s      r$   remove_callbackzCallbackHandler.remove_callbackY  sR    h%nn b(+NN))"-
 NN!!(+r&   c                 F    dj                  d | j                  D              S )Nr,   c              3   H   K   | ]  }|j                   j                    y wr!   )r   rB   r   s     r$   r   z0CallbackHandler.callback_list.<locals>.<genexpr>d  s     H2..Hs    ")joinr   r"   s    r$   r   zCallbackHandler.callback_listb  s    yyHHHHr&   r[   r\   r]   c                 *    | j                  d|||      S )Nra   
call_eventr#   r[   r\   r]   s       r$   ra   zCallbackHandler.on_init_endf      }dE7CCr&   c                 8    d|_         | j                  d|||      S )NFrd   )rN   r   r   s       r$   rd   zCallbackHandler.on_train_begini  s     ',$/ugFFr&   c                 *    | j                  d|||      S )Nrf   r   r   s       r$   rf   zCallbackHandler.on_train_endm      ~tUGDDr&   c                 8    d|_         | j                  d|||      S )NFrh   )rO   r   r   s       r$   rh   zCallbackHandler.on_epoch_beginp  s     $)!/ugFFr&   c                 *    | j                  d|||      S )Nrj   r   r   s       r$   rj   zCallbackHandler.on_epoch_endt  r   r&   c                 T    d|_         d|_        d|_        | j                  d|||      S )NFrl   )rR   rQ   rP   r   r   s       r$   rl   zCallbackHandler.on_step_beginw  s/    ""'#eWEEr&   c                 *    | j                  d|||      S )Nro   r   r   s       r$   ro   zCallbackHandler.on_substep_end}  s    /ugFFr&   c                 *    | j                  d|||      S )Nrq   r   r   s       r$   rq   zCallbackHandler.on_step_end  r   r&   c                 <    d|_         | j                  d||||      S )NFrs   ru   )rQ   r   r#   r[   r\   r]   ru   s        r$   rs   zCallbackHandler.on_evaluate  s#    "'}dE7GTTr&   c                 .    | j                  d||||      S )Nrv   r   r   r   s        r$   rv   zCallbackHandler.on_predict  s    |T5'7SSr&   c                 8    d|_         | j                  d|||      S )NFrx   )rP   r   r   s       r$   rx   zCallbackHandler.on_save  s    #y$w??r&   c                 <    d|_         | j                  d||||      S )NFrz   )logs)rR   r   )r#   r[   r\   r]   r   s        r$   rz   zCallbackHandler.on_log  s#    "xugDIIr&   c                 *    | j                  d|||      S )Nr|   r   r   s       r$   r|   z"CallbackHandler.on_prediction_step  s    3T5'JJr&   c                     | j                   D ]_  } t        ||      |||f| j                  | j                  | j                  | j
                  | j                  | j                  d|}|^|}a |S )N)r   r   r   r   r   r   )r   getattrr   r   r   r   r   r   )r#   eventr[   r\   r]   r`   r   results           r$   r   zCallbackHandler.call_event  s     	!H-WXu- jj....!..!%!6!6 $ 4 4 F ! 	!  r&   N)rB   rC   rD   rE   r   r   r   r   propertyr   r   r   rM   ra   rd   rf   rh   rj   rl   ro   rq   rs   rv   rx   rz   r|   r   r<   r&   r$   r~   r~   ,  s   H&	"
, I ID 1 D, DQ_ DG#4 G\ GTb GE!2 E< ER` EG#4 G\ GTb GE!2 E< ER` EF"3 FL FSa FG#4 G\ GTb GD 1 D, DQ_ DU 1 U, UQ_ UT0 T TP^ T@- @l @^ @J, J\ JN JK'8 K KXf Kr&   r~   c                   8    e Zd ZdZdededefdZdededefdZy)r   zx
    A [`TrainerCallback`] that handles the default flow of the training loop for logs, evaluation and checkpoints.
    r[   r\   r]   c                 Z   |j                   dk(  r|j                  rd|_        |j                  t        j
                  k(  r#|j                   |j                  z  dk(  rd|_        |j                  t        j
                  k(  r<|j                   |j                  z  dk(  r |j                  |j                   k  rd|_
        |j                  t        j
                  k(  r2|j                  dkD  r#|j                   |j                  z  dk(  rd|_        |j                   |j                  k\  rd|_        |S )Nr	   Tr   )r   logging_first_steprR   logging_strategyr
   STEPSr   evaluation_strategyr   
eval_delayrQ   save_strategyr   rP   r   rN   r_   s        r$   rq   zDefaultFlowCallback.on_step_end  s   !d&=&=!%G  $4$:$::u?P?PSXSfSf?fjk?k!%G $$(8(>(>>!!E$4$4495#4#44&*G# "2"8"88  1$!!E$4$449"&G /+/G(r&   c                    |j                   t        j                  k(  rd|_        |j                  t        j                  k(  r |j
                  |j                  k  rd|_        |j                  t        j                  k(  rd|_	        |S )NT)
r   r
   EPOCHrR   r   r   r   rQ   r   rP   r_   s        r$   rj   z DefaultFlowCallback.on_epoch_end  st      $4$:$::!%G ##'7'='==$//UZU`U`B`&*G# !1!7!77"&Gr&   N)	rB   rC   rD   rE   r   r   rM   rq   rj   r<   r&   r$   r   r     s?     1 , Q_ :!2 < R` r&   r   c                   D    e Zd ZdZd Zd Zd ZddZd Zd Z	dd	Z
d
 Zy)ProgressCallbackzU
    A [`TrainerCallback`] that displays the progress of training or evaluation.
    c                      d | _         d | _        y r!   )training_barprediction_barr"   s    r$   r   zProgressCallback.__init__  s     "r&   c                 b    |j                   rt        |j                  d      | _        d| _        y )NT)totaldynamic_ncolsr   )r   r   r   r   current_stepr_   s        r$   rd   zProgressCallback.on_train_begin  s&    && $5??$ ODr&   c                     |j                   rD| j                  j                  |j                  | j                  z
         |j                  | _        y y r!   )r   r   updater   r   r_   s        r$   rq   zProgressCallback.on_step_end  sC    &&$$U%6%69J9J%JK % 1 1D 'r&   Nc                     |j                   r\t        |      rP| j                  (t        t	        |      | j
                  d u d      | _        | j                  j                  d       y y y )NT)r   leaver   r	   )r   r   r   r   lenr   r   )r#   r[   r\   r]   r   r`   s         r$   r|   z#ProgressCallback.on_prediction_step  sb    &&:o+F""*&*o.d6G6G46O_c'# &&q) ,G&r&   c                 x    |j                   r.| j                  | j                  j                          d | _        y y r!   r   r   closer_   s        r$   rs   zProgressCallback.on_evaluate  6    &&"".##))+"&D 'r&   c                 x    |j                   r.| j                  | j                  j                          d | _        y y r!   r   r_   s        r$   rv   zProgressCallback.on_predict  r   r&   c                     |j                   rD| j                  7|j                  dd       }| j                  j                  t	        |             y y y Nr   )r   r   popr6   rI   r#   r[   r\   r]   r   r`   _s          r$   rz   zProgressCallback.on_log  sG    &&4+<+<+Ht,A##CI. ,I&r&   c                 `    |j                   r"| j                  j                          d | _        y y r!   )r   r   r   r_   s        r$   rf   zProgressCallback.on_train_end  s*    &&##% $D 'r&   r!   )rB   rC   rD   rE   r   rd   rq   r|   rs   rv   rz   rf   r<   r&   r$   r   r     s/    #
2
*''/
%r&   r   c                       e Zd ZdZddZy)PrinterCallbackz?
    A bare [`TrainerCallback`] that just prints the logs.
    Nc                 X    |j                  dd       }|j                  rt        |       y y r   )r   r   printr   s          r$   rz   zPrinterCallback.on_log  s'    HH\4(&&$K 'r&   r!   )rB   rC   rD   rE   rz   r<   r&   r$   r   r     s    r&   r   c                   :    e Zd ZdZd	dedee   fdZd Zd Z	d 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_patienceearly_stopping_thresholdc                 .    || _         || _        d| _        y )Nr   )r   r   early_stopping_patience_counter)r#   r   r   s      r$   r   zEarlyStoppingCallback.__init__)  s    '>$(@%/0,r&   c                    |j                   rt        j                  nt        j                  }|j                  8 |||j                        r-t        ||j                  z
        | j                  kD  rd| _        y | xj                  dz  c_        y )Nr   r	   )greater_is_betternpgreaterlessr   absr   r   )r#   r[   r\   r]   metric_valueoperators         r$   check_metric_valuez(EarlyStoppingCallback.check_metric_value/  sl    !%!7!72::RWW$\5#4#45L5#4#4458U8UU34D000A50r&   c                     |j                   sJ d       |j                  J d       |j                  t        j                  k7  sJ d       y )Nz<EarlyStoppingCallback requires load_best_model_at_end = Truez?EarlyStoppingCallback requires metric_for_best_model is definedzAEarlyStoppingCallback requires IntervalStrategy of steps or epoch)load_best_model_at_endmetric_for_best_modelr   r
   NOr_   s        r$   rd   z$EarlyStoppingCallback.on_train_begin:  s^    **j,jj*&&2	ML	M2 $$(8(;(;;	ON	O;r&   c                    |j                   }|j                  d      sd| }|j                  |      }|t        j	                  d| d       y | j                  ||||       | j                  | j                  k\  rd|_        y y )Neval_z@early stopping required metric_for_best_model, but did not find z so early stopping is disabledT)	r   
startswithgetr   r   r   r   r   rN   )r#   r[   r\   r]   ru   r`   metric_to_checkr   s           r$   rs   z!EarlyStoppingCallback.on_evaluateC  s    44))'2 %o%67O{{?3NNRSbRc d  eWlC//43O3OO+/G( Pr&   N)r	   g        )rB   rC   rD   rE   rH   r   rF   r   r   rd   rs   r<   r&   r$   r   r     s1     1 1S[\aSb 1	6O0r&   r   )rE   r3   r1   r   typingr   r   r   r   numpyr   	tqdm.autor   trainer_utilsr
   r   training_argsr   utilsr   
get_loggerrB   r   r   rM   rZ   r~   r   r   r   r   r<   r&   r$   <module>r     s      ! . .   7 ,  
		H	% P' P' P'f .  .  . bA AHzo zz// /d/% /%do :0O :0r&   