
    謜ij                     f   d Z ddlZddlZddlZddlmZ ddlZddlmZ ddl	m
Z
mZmZ ddlmZ ddlmZ  ej"                  e      Ze G d	 d
             Z G d d      Ze G d de             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e      Zy)zJ
Callbacks to use with the Trainer class and customize the training loop.
    N)	dataclass)tqdm   )IntervalStrategySaveStrategy
has_length)TrainingArguments)loggingc                      e Zd ZU dZdZedz  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dz  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dz  ed<   dZedz  ed<   dZedz  ed<   dZeed<   dZeed<   dZeed<   dZedz  ed<   dZeeeez  ez  ez  f   dz  ed<   dZed   dz  ed<   d ZdefdZ e!defd       Z"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`].

    <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.
        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.
    Nepochr   global_step	max_stepsi  logging_steps
eval_steps
save_stepstrain_batch_sizenum_train_epochsnum_input_tokens_seen
total_floslog_historybest_metricbest_global_stepbest_model_checkpointTis_local_process_zerois_world_process_zeroFis_hyper_param_search
trial_nametrial_paramsTrainerCallbackstateful_callbacksc                    | j                   g | _         | j                  i | _        y t        | j                  t              ry i }| j                  D ]  }t        |t              st        dt        |             |j                  j                  }||v r?t        ||   t              s	||   g||<   ||   j                  |j                                |j                         ||<    || _        y )NzNAll callbacks passed to be saved must inherit `ExportableState`, but received )r   r!   
isinstancedictExportableState	TypeErrortype	__class____name__listappendstate)selfr!   callbacknames       Y/mnt/e/genesis-system/.venv/lib/python3.12/site-packages/transformers/trainer_callback.py__post_init__zTrainerState.__post_init__t   s    #!D""*&(D#//6 "$ 33 @!(_>#himnviwhxy   ))22-- &&8&>E4Ft4L3M*40&t,33HNN4DE/7~~/?&t,@ '9D#    	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-   r3   json_stringfs       r0   save_to_jsonzTrainerState.save_to_json   sT    jj!3!3D!9!tTW[[)S73 	!qGGK 	! 	! 	!s   AA!c                     t        |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`.r:   r;   N )rA   readr=   loads)clsr3   rD   texts       r0   load_from_jsonzTrainerState.load_from_json   sG     )g. 	!668D	&TZZ%&&	 	s   AAc                     dD ]A  }t        || d      }||dk  rt        j                  ||z        }t        | | d|       C y)z
        Calculates and stores the absolute value for logging,
        eval, and save steps based on if it was a proportion
        or not.
        )r
   evalsave_stepsNr   )getattrmathceilsetattr)r-   argsr   	step_kind	num_stepss        r0   compute_stepszTrainerState.compute_steps   sY     5 	?I6&:;I$q= $		)i*? @I62I>	?r2   c                    |j                   ,|j                   |j                  |j                        | _        d| _        |ddlm}  ||      | _        || _        || _        |j                         | _        |j                         | _	        y)zI
        Stores the initial training references needed in `self`
        Nr   )	hp_params)
hp_name_trialr   r   transformers.integrationsrZ   r   r   r   r   )r-   trainerr   r   trialrZ   s         r0   init_training_referencesz%TrainerState.init_training_references   s|     ??&7>>+E &oognn=DO ; )% 0D" 0%,%B%B%D"%,%B%B%D"r2   )%r)   
__module____qualname____doc__r   float__annotations__r   intr   r   r   r   r   r   r   r   r   r*   r$   strr   r   r   r   boolr   r   r   r   r!   r1   rE   classmethodrL   rX   r`   rG   r2   r0   r   r   "   s`   9v E54<KIsM3JJ#'cDj'c!"3"J*.Kd3:&'. $K$#'cDj'(,3:,"&4&"&4&"'4'!Jd
!?CL$sC%K#-445<C9=./$6=96!c ! 's ' '?Er2   r   c                   ,    e Zd ZdZdefdZed        Zy)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,
                }
            }
    ```returnc                     t        d      )Nz<You must implement a `state` function to utilize this class.)NotImplementedErrorr-   s    r0   r,   zExportableState.state   s    !"`aar2   c                 l     | di |d   }|d   j                         D ]  \  }}t        |||        |S )NrU   
attributesrG   )itemsrT   )rJ   r,   instancekvs        r0   
from_statezExportableState.from_state   sE    'v','--/ 	$DAqHa#	$r2   N)r)   ra   rb   rc   r$   r,   ri   ru   rG   r2   r0   r%   r%      s*    @bt b  r2   r%   c                   v    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def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)rx   rn   s    r0   _new_trainingzTrainerControl._new_training  s
    $)!r2   c                     d| _         y)z9Internal method that resets the variable for a new epoch.FN)ry   rn   s    r0   
_new_epochzTrainerControl._new_epoch  s
    !&r2   c                 .    d| _         d| _        d| _        y)z8Internal method that resets the variable for a new step.FN)rz   r{   r|   rn   s    r0   	_new_stepzTrainerControl._new_step  s     $r2   rk   c                 |    | j                   | j                  | j                  | j                  | j                  di dS )Nrx   ry   rz   r{   r|   rU   rp   r   rn   s    r0   r,   zTrainerControl.state  sC     )-(A(A%)%;%;#//#'#7#7"oo 	
 		
r2   N)r)   ra   rb   rc   rx   rh   re   ry   rz   r{   r|   r~   r   r   r$   r,   rG   r2   r0   rw   rw      sX    6 "'$&#t#K!OT!J*' 

t 

r2   rw   c                   P   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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)r    a0	  
    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.
        processing_class ([`PreTrainedTokenizer` or `BaseImageProcessor` or `ProcessorMixin` or `FeatureExtractionMixin`]):
            The processing class used for encoding the data. Can be a tokenizer, a processor, an image processor or a feature extractor.
        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 evaluation.
        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 [`~transformers.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)
    ```rU   r,   controlc                      y)zS
        Event called at the end of the initialization of the [`Trainer`].
        NrG   r-   rU   r,   r   kwargss        r0   on_init_endzTrainerCallback.on_init_endZ      r2   c                      y)z<
        Event called at the beginning of training.
        NrG   r   s        r0   on_train_beginzTrainerCallback.on_train_begin_  r   r2   c                      y)z6
        Event called at the end of training.
        NrG   r   s        r0   on_train_endzTrainerCallback.on_train_endd  r   r2   c                      y)z<
        Event called at the beginning of an epoch.
        NrG   r   s        r0   on_epoch_beginzTrainerCallback.on_epoch_begini  r   r2   c                      y)z6
        Event called at the end of an epoch.
        NrG   r   s        r0   on_epoch_endzTrainerCallback.on_epoch_endn  r   r2   c                      y)z
        Event called at the beginning of a training step. If using gradient accumulation, one training step might take
        several inputs.
        NrG   r   s        r0   on_step_beginzTrainerCallback.on_step_begins  r   r2   c                      y)zv
        Event called before the optimizer step but after gradient clipping. Useful for monitoring gradients.
        NrG   r   s        r0   on_pre_optimizer_stepz%TrainerCallback.on_pre_optimizer_stepy  r   r2   c                      y)z}
        Event called after the optimizer step but before gradients are zeroed out. Useful for monitoring gradients.
        NrG   r   s        r0   on_optimizer_stepz!TrainerCallback.on_optimizer_step~  r   r2   c                      y)zU
        Event called at the end of an substep during gradient accumulation.
        NrG   r   s        r0   on_substep_endzTrainerCallback.on_substep_end  r   r2   c                      y)z
        Event called at the end of a training step. If using gradient accumulation, one training step might take
        several inputs.
        NrG   r   s        r0   on_step_endzTrainerCallback.on_step_end  r   r2   c                      y)z9
        Event called after an evaluation phase.
        NrG   r   s        r0   on_evaluatezTrainerCallback.on_evaluate  r   r2   c                      y)z=
        Event called after a successful prediction.
        NrG   )r-   rU   r,   r   metricsr   s         r0   
on_predictzTrainerCallback.on_predict  r   r2   c                      y)z7
        Event called after a checkpoint save.
        NrG   r   s        r0   on_savezTrainerCallback.on_save  r   r2   c                      y)z;
        Event called after logging the last logs.
        NrG   r   s        r0   on_logzTrainerCallback.on_log  r   r2   c                      y)z7
        Event called after a prediction step.
        NrG   r   s        r0   on_prediction_stepz"TrainerCallback.on_prediction_step  r   r2   c                      y)z
        Event called before pushing the model to the hub, at the beginning of Trainer.push_to_hub and Trainer._push_from_checkpoint.
        NrG   r   s        r0   on_push_beginzTrainerCallback.on_push_begin  r   r2   N)r)   ra   rb   rc   r	   r   rw   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rG   r2   r0   r    r    '  s   /b 1 , Q_ 
#4 \ Tb 
!2 < R` 
#4 \ Tb 
!2 < R` 
"3 L Sa *; L [i 
&7  We 
#4 \ Tb 
 1 , Q_  1 , Q_ 
0  P^ 
- l ^ 
, \ N 
'8  Xf 
"3 L Sa r2   r    c                   ~   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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N)r#   DefaultFlowCallback.0cbs     r0   	<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processing_class	optimizerlr_schedulertrain_dataloadereval_dataloaderanyloggerwarningcallback_list)r-   r   r   r   r   r   r   s          r0   __init__zCallbackHandler.__init__  s     	"Bb!	"
 0"( $#PPPNN$ $$% Qr2   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#   r'   r(   r   r   r   r   r+   )r-   r.   r   cb_classcs        r0   r   zCallbackHandler.add_callback  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      r0   pop_callbackzCallbackHandler.pop_callback  ss    h%nn b(+NN))"-I
 nn >NN))"-Ir2   c                     t        |t              r;| j                  D ]+  }t        ||      s| j                  j                  |        y  y | j                  j                  |       y r   r   r   s      r0   remove_callbackzCallbackHandler.remove_callback  sR    h%nn b(+NN))"-
 NN!!(+r2   c                 F    dj                  d | j                  D              S )Nr8   c              3   H   K   | ]  }|j                   j                    y wr   )r(   r)   r   s     r0   r   z0CallbackHandler.callback_list.<locals>.<genexpr>  s     H2..Hs    ")joinr   rn   s    r0   r   zCallbackHandler.callback_list  s    yyHHHHr2   rU   r,   r   c                 *    | j                  d|||      S )Nr   
call_eventr-   rU   r,   r   s       r0   r   zCallbackHandler.on_init_end      }dE7CCr2   c                 8    d|_         | j                  d|||      S )NFr   )rx   r   r   s       r0   r   zCallbackHandler.on_train_begin  s     ',$/ugFFr2   c                 *    | j                  d|||      S )Nr   r   r   s       r0   r   zCallbackHandler.on_train_end      ~tUGDDr2   c                 8    d|_         | j                  d|||      S )NFr   )ry   r   r   s       r0   r   zCallbackHandler.on_epoch_begin  s     $)!/ugFFr2   c                 *    | j                  d|||      S )Nr   r   r   s       r0   r   zCallbackHandler.on_epoch_end  r   r2   c                 T    d|_         d|_        d|_        | j                  d|||      S )NFr   )r|   r{   rz   r   r   s       r0   r   zCallbackHandler.on_step_begin  s/    ""'#eWEEr2   c                 *    | j                  d|||      S )Nr   r   r   s       r0   r   z%CallbackHandler.on_pre_optimizer_step  s    6eWMMr2   c                 *    | j                  d|||      S )Nr   r   r   s       r0   r   z!CallbackHandler.on_optimizer_step  s    2D%IIr2   c                 *    | j                  d|||      S )Nr   r   r   s       r0   r   zCallbackHandler.on_substep_end  s    /ugFFr2   c                 *    | j                  d|||      S )Nr   r   r   s       r0   r   zCallbackHandler.on_step_end  r   r2   c                 <    d|_         | j                  d||||      S )NFr   r   )r{   r   r-   rU   r,   r   r   s        r0   r   zCallbackHandler.on_evaluate
  s#    "'}dE7GTTr2   c                 .    | j                  d||||      S )Nr   r   r   r   s        r0   r   zCallbackHandler.on_predict  s    |T5'7SSr2   c                 8    d|_         | j                  d|||      S )NFr   )rz   r   r   s       r0   r   zCallbackHandler.on_save  s    #y$w??r2   c                 <    d|_         | j                  d||||      S )NFr   )logs)r|   r   )r-   rU   r,   r   r   s        r0   r   zCallbackHandler.on_log  s#    "xugDIIr2   c                 *    | j                  d|||      S )Nr   r   r   s       r0   r   z"CallbackHandler.on_prediction_step  s    3T5'JJr2   c                 .     | j                   d|||fi |S )Nr   r   r   s        r0   r   zCallbackHandler.on_push_begin  s    teWOOOr2   c                     | j                   D ]_  } t        ||      |||f| j                  | j                  | j                  | j
                  | j                  | j                  d|}|^|}a |S )N)r   r   r   r   r   r   )r   rQ   r   r   r   r   r   r   )r-   eventrU   r,   r   r   r.   results           r0   r   zCallbackHandler.call_event  s     	!H-WXu- jj!%!6!6..!..!%!6!6 $ 4 4 F ! 	!  r2   N)r)   ra   rb   rc   r   r   r   r   propertyr   r	   r   rw   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rG   r2   r0   r   r     s2   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N*; NL N[i NJ&7 J JWe J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P"3 PL PSa Pr2   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.
    rU   r,   r   c                    |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|_        |j                  t        j
                  k(  rd|_        |S )Nr   Tr   )r   logging_first_stepr|   logging_strategyr   STEPSr   eval_strategyr   
eval_delayr{   save_strategyr   r   rz   r   rx   r   s        r0   r   zDefaultFlowCallback.on_step_end8  s#   !d&=&=!%G  $4$:$::u?P?PSXSfSf?fjk?k!%G "2"8"88!!E$4$4495#4#44&*G# ,"4"44  1$!!E$4$449"&G /+/G(!!\%7%77&*#r2   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   EPOCHr|   r   r   r   r{   r   r   rz   r   s        r0   r   z DefaultFlowCallback.on_epoch_endX  sp      $4$:$::!%G !1!7!77DOOu{{<Z&*G# !3!33"&Gr2   N)	r)   ra   rb   rc   r	   r   rw   r   r   rG   r2   r0   r   r   3  s@     1 , Q_ @!2 < R` r2   r   c                   L    e Zd ZdZddefdZd Zd ZddZd Z	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_barprediction_barr   )r-   r   s     r0   r   zProgressCallback.__init__n  s     !"&r2   c                 b    |j                   rt        |j                  d      | _        d| _        y )NT)totaldynamic_ncolsr   )r   r   r   r   current_stepr   s        r0   r   zProgressCallback.on_train_begin{  s&    && $5??$ ODr2   c                     |j                   rD| j                  j                  |j                  | j                  z
         |j                  | _        y y r   )r   r   updater   r   r   s        r0   r   zProgressCallback.on_step_end  sC    &&$$U%6%69J9J%JK % 1 1D 'r2   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-   rU   r,   r   r   r   s         r0   r   z#ProgressCallback.on_prediction_step  sb    &&:o+F""*&*o.d6G6G46O_c'# &&q) ,G&r2   c                 x    |j                   r.| j                  | j                  j                          d | _        y y r   r   r   closer   s        r0   r   zProgressCallback.on_evaluate  6    &&"".##))+"&D 'r2   c                 x    |j                   r.| j                  | j                  j                          d | _        y y r   r  r   s        r0   r   zProgressCallback.on_predict  r  r2   c                    |j                   r| j                  i }|j                         D ]i  \  }}t        |t              r7t        |      | j                  kD  rdt        |       d| j                   d||<   t        |t              r|d||<   e|||<   k |j                  dd       }	| j                  j                  t	        |             y y y )Nz%[String too long to display, length: z > z/. Consider increasing `max_str_len` if needed.].4gr   )
r   r   rq   r#   rg   r  r   rd   poprB   )
r-   rU   r,   r   r   r   shallow_logsrs   rt   _s
             r0   r   zProgressCallback.on_log  s    &&4+<+<+H L

 
(1a%#a&43C3C*C?Axs4K[K[J\ ]H H !O a')*3LO&'LO
(   t4A##C$56! ,I&r2   c                 `    |j                   r"| j                  j                          d | _        y y r   )r   r   r  r   s        r0   r   zProgressCallback.on_train_end  s*    &&##% $D 'r2   )d   r   )r)   ra   rb   rc   rf   r   r   r   r   r   r   r   r   rG   r2   r0   r   r   h  s6    
'C '
2
*''7&%r2   r   c                       e Zd ZdZddZy)PrinterCallbackz?
    A bare [`TrainerCallback`] that just prints the logs.
    Nc           	          |j                  dd       }|j                  rE|7|j                         D ci c]  \  }}|t        |t              r|dn| }}}t        |       y y c c}}w )Nr   r  )r  r   rq   r#   rd   print)	r-   rU   r,   r   r   r   r  rs   rt   s	            r0   r   zPrinterCallback.on_log  sd    HH\4(&&SWS]S]S_`41a*Q*>qgAE``$K '`s   !A%r   )r)   ra   rb   rc   r   rG   r2   r0   r  r    s    r2   r  c                   F    e Zd ZdZddededz  fdZd Zd Zd Z	d	e
f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_thresholdNc                 .    || _         || _        d| _        y )Nr   r  r  early_stopping_patience_counter)r-   r  r  s      r0   r   zEarlyStoppingCallback.__init__  s    '>$(@%/0,r2   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-   rU   r,   r   metric_valueoperators         r0   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0r2   c                     |j                   st        j                  d       |j                  J d       |j                  t
        j                  k7  sJ d       y )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_endr   r   metric_for_best_modelr   r   NOr   s        r0   r   z$EarlyStoppingCallback.on_train_begin  sb    **NN^ ))5 	
P	
5 !!%5%8%88 	
O	
8r2   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  rx   )r-   rU   r,   r   r   r   metric_to_checkr"  s           r0   r   z!EarlyStoppingCallback.on_evaluate  s    44))'2 %o%67O{{?3NNRSbRc d  eWlC//43O3OO+/G( Pr2   rk   c                 R    | j                   | j                  dd| j                  idS )N)r  r  r  r   r  rn   s    r0   r,   zEarlyStoppingCallback.state  s7     ,0+G+G,0,I,I
 243W3W
 	
r2   )r   g        )r)   ra   rb   rc   rf   rd   r   r$  r   r   r$   r,   rG   r2   r0   r  r    s<     1 1SX[_S_ 1	6
0"	
t 	
r2   r  )rc   r?   r=   rR   r   numpyr  	tqdm.autor   trainer_utilsr   r   r   training_argsr	   utilsr
   
get_loggerr)   r   r   r%   rw   r    r   r   r   r  r  rG   r2   r0   <module>r5     s       !   E E ,  
		H	% WE WE WEt) )X :
_ :
 :
zC CLCo CL2/ 2jG% G%T
o 
I
O_ I
r2   