Ë
    S^(hKƒ  ã                   óz  — d Z ddlZddlZddlZddlmZ ddlmZmZ ddlZ	ddl
mZ ddlmZ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)ÚOptionalÚUnion)Útqdmé   )ÚHPSearchBackendÚIntervalStrategyÚSaveStrategyÚ
has_length)ÚTrainingArguments)Úloggingc                   ó°  — 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	   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   ed<   dZeed<   dZeed<   dZeed<   dZee   ed<   dZeeeeee	ef   f   ed<   dZ ed   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       ú[/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/trainer_callback.pyÚ__post_init__zTrainerState.__post_init__u   sü   € Ø×ÑÐ#Ø!ˆDÔØ×"Ñ"Ð*Ø&(ˆDÕ#Ü˜×/Ñ/´Ô6àð "$ÐØ ×3Ñ3ò @�Ü! (¬_Ô>Ü#ØhÔimÐnvÓiwÐhxÐyóð ð  ×)Ñ)×2Ñ2�ØÐ-Ñ-ô &Ð&8¸Ñ&>ÄÔEØ4FÀtÑ4LÐ3MÐ*¨4Ñ0Ø& tÑ,×3Ñ3°H·N±NÓ4DÕEà/7¯~©~Ó/?Ð& tÒ,ð@ð '9ˆ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)r0   r6   Újson_stringÚfs       r3   Úsave_to_jsonzTrainerState.save_to_json�   sT   € ä—j‘j¤×!3Ñ!3°DÓ!9À!ÈtÔTÐW[Ñ[ˆÜ�)˜S¨7Ô3ð 	!°qØ�G‰G�KÔ ÷	!÷ 	!ñ 	!ú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© )rD   Úreadr@   Úloads)Úclsr6   rG   Útexts       r3   Úload_from_jsonzTrainerState.load_from_json–   sG   € ô �) gÔ.ð 	°!Ø—6‘6“8ˆD÷	áÑ&”T—Z‘Z Ó%Ñ&Ð&÷	ð 	ú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)r0   Úargsr   Ú	step_kindÚ	num_stepss        r3   Úcompute_stepszTrainerState.compute_steps�   sY   € ð 5ò 	?ˆIÜ ¨¨°6Ð&:Ó;ˆIØÑ$Ø˜q’=Ü $§	¡	¨)°iÑ*?Ó @�IÜ˜  ¨6Ð2°IÕ>ñ	?r5   c                 ór  — |j                   �,|j                  � |j                  |j                  «      | _        d| _        |�>ddlm} |j                  t        j                  k(  r|j                  n|} ||«      | _        || _
        || _        |j                  «       | _        |j                  «       | _        y)zI
        Stores the initial training references needed in `self`
        Nr   )Ú	hp_params)Úhp_nameÚ_trialr!   r"   Útransformers.integrationsr]   Úhp_search_backendr   ÚSIGOPTÚassignmentsr   r   r   r   )r0   Útrainerr   r   Útrialr]   rc   s          r3   Úinit_training_referencesz%TrainerState.init_training_referencesª   s�   € ð �?‰?Ð&¨7¯>©>Ð+Eð &Ÿo™o¨g¯n©nÓ=ˆDŒOØ ˆÔØÐÝ;à/6×/HÑ/HÌO×LbÑLbÒ/b˜%×+Ò+ÐhmˆKÙ )¨+Ó 6ˆDÔà"ˆŒØ 0ˆÔØ%,×%BÑ%BÓ%DˆÔ"Ø%,×%BÑ%BÓ%DˆÕ"r5   )'r,   Ú
__module__Ú__qualname__Ú__doc__r   r   ÚfloatÚ__annotations__r   Úintr   r   r   r   r   r   r   r   r   r-   r'   Ústrr   r   r   r   Úboolr   r    r!   r"   r   r$   r4   rH   ÚclassmethodrO   r[   rf   rJ   r5   r3   r   r   #   sV  … ñ9ðv "€Eˆ8�E‰?Ó!Ø€K�ÓØ€IˆsÓØ€M�3ÓØ€J�ÓØ€J�ÓØ&*Ð�h˜s‘mÓ*ØÐ�cÓØ!"Ð˜3Ó"Ø€J�ÓØ*.€K��d˜3 ˜:Ñ&Ñ'Ó.Ø#'€K�˜%‘Ó'Ø&*Ð�h˜s‘mÓ*Ø+/Ð˜8 C™=Ó/Ø"&Ð˜4Ó&Ø"&Ð˜4Ó&Ø"'Ð˜4Ó'Ø $€J�˜‘Ó$Ø<@€L�$�s˜E # u¨c°4Ð"7Ñ8Ð8Ñ9Ó@Ø26Ð˜Ð.Ñ/Ó6ò9ð6! có !ð ð' sò 'ó ð'ò?óEr5   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©r0   s    r3   r/   zExportableState.stateà   s   € Ü!Ð"`ÓaÐar5   c                 ól   —  | di |d   ¤Ž}|d   j                  «       D ]  \  }}t        |||«       Œ |S )NrX   Ú
attributesrJ   )ÚitemsrW   )rM   r/   ÚinstanceÚkÚvs        r3   Ú
from_statezExportableState.from_stateã   sE   € áÑ'˜˜v™Ñ'ˆØ˜,Ñ'×-Ñ-Ó/ò 	$‰DˆAˆqÜ�H˜a Õ#ð	$àˆr5   N)r,   rg   rh   ri   r'   r/   ro   r{   rJ   r5   r3   r(   r(   ¿   s*   „ ñð@b�tó bð ñó ñr5   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)r~   rt   s    r3   Ú_new_trainingzTrainerControl._new_training  s
   € à$)ˆÕ!r5   c                 ó   — d| _         y)z9Internal method that resets the variable for a new epoch.FN)r   rt   s    r3   Ú
_new_epochzTrainerControl._new_epoch  s
   € à!&ˆÕr5   c                 ó.   — d| _         d| _        d| _        y)z8Internal method that resets the variable for a new step.FN)r€   r�   r‚   rt   s    r3   Ú	_new_stepzTrainerControl._new_step  s   € à ˆÔØ$ˆÔØˆ�r5   rq   c                 ó|   — | j                   | j                  | j                  | j                  | j                  dœi dœS )N©r~   r   r€   r�   r‚   ©rX   rv   rŠ   rt   s    r3   r/   zTrainerControl.state  sC   € ð )-×(AÑ(AØ%)×%;Ñ%;Ø#×/Ñ/Ø#'×#7Ñ#7Ø"Ÿo™oñð ñ	
ð 		
r5   N)r,   rg   rh   ri   r~   rn   rk   r   r€   r�   r‚   r„   r†   rˆ   r'   r/   rJ   r5   r3   r}   r}   ë   sX   … ñð6 "'Ð˜$Ó&Ø#Ð�tÓ#Ø€K�ÓØ!€O�TÓ!Ø€J�Óò*ò'ò ð

�tô 

r5   r}   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dededefd„Zdededefd„Zy)r#   aÇ	  
    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. This is deprecated in favour of `processing_class`.
        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)
    ```rX   r/   Úcontrolc                  ó   — y)zS
        Event called at the end of the initialization of the [`Trainer`].
        NrJ   ©r0   rX   r/   r�   Úkwargss        r3   Úon_init_endzTrainerCallback.on_init_end^  ó   € ð 	r5   c                  ó   — y)z<
        Event called at the beginning of training.
        NrJ   r�   s        r3   Úon_train_beginzTrainerCallback.on_train_begind  r’   r5   c                  ó   — y)z6
        Event called at the end of training.
        NrJ   r�   s        r3   Úon_train_endzTrainerCallback.on_train_endj  r’   r5   c                  ó   — y)z<
        Event called at the beginning of an epoch.
        NrJ   r�   s        r3   Úon_epoch_beginzTrainerCallback.on_epoch_beginp  r’   r5   c                  ó   — y)z6
        Event called at the end of an epoch.
        NrJ   r�   s        r3   Úon_epoch_endzTrainerCallback.on_epoch_endv  r’   r5   c                  ó   — y)z˜
        Event called at the beginning of a training step. If using gradient accumulation, one training step might take
        several inputs.
        NrJ   r�   s        r3   Úon_step_beginzTrainerCallback.on_step_begin|  ó   € ð
 	r5   c                  ó   — y)zv
        Event called before the optimizer step but after gradient clipping. Useful for monitoring gradients.
        NrJ   r�   s        r3   Úon_pre_optimizer_stepz%TrainerCallback.on_pre_optimizer_stepƒ  r’   r5   c                  ó   — y)z}
        Event called after the optimizer step but before gradients are zeroed out. Useful for monitoring gradients.
        NrJ   r�   s        r3   Úon_optimizer_stepz!TrainerCallback.on_optimizer_step‰  r’   r5   c                  ó   — y)zU
        Event called at the end of an substep during gradient accumulation.
        NrJ   r�   s        r3   Úon_substep_endzTrainerCallback.on_substep_end�  r’   r5   c                  ó   — y)z’
        Event called at the end of a training step. If using gradient accumulation, one training step might take
        several inputs.
        NrJ   r�   s        r3   Úon_step_endzTrainerCallback.on_step_end•  r�   r5   c                  ó   — y)z9
        Event called after an evaluation phase.
        NrJ   r�   s        r3   Úon_evaluatezTrainerCallback.on_evaluateœ  r’   r5   c                  ó   — y)z=
        Event called after a successful prediction.
        NrJ   )r0   rX   r/   r�   Úmetricsr�   s         r3   Ú
on_predictzTrainerCallback.on_predict¢  r’   r5   c                  ó   — y)z7
        Event called after a checkpoint save.
        NrJ   r�   s        r3   Úon_savezTrainerCallback.on_save¨  r’   r5   c                  ó   — y)z;
        Event called after logging the last logs.
        NrJ   r�   s        r3   Úon_logzTrainerCallback.on_log®  r’   r5   c                  ó   — y)z7
        Event called after a prediction step.
        NrJ   r�   s        r3   Úon_prediction_stepz"TrainerCallback.on_prediction_step´  r’   r5   N)r,   rg   rh   ri   r   r   r}   r‘   r”   r–   r˜   rš   rœ   rŸ   r¡   r£   r¥   r§   rª   r¬   r®   r°   rJ   r5   r3   r#   r#   )  sª  „ ñ1ðfÐ 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ô r5   r#   c                   ój  — 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„ 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     r3   ú	<genexpr>z+CallbackHandler.__init__.<locals>.<genexpr>É  s   è ø€ ÒP¸2”:˜bÔ"5×6ÑPùs   ‚zÔThe 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)r0   r»   r½   r¾   r¿   rÀ   r¹   s          r3   Ú__init__zCallbackHandler.__init__¾  s�   € ØˆŒØò 	"ˆBØ×Ñ˜bÕ!ð	"àˆŒ
Ø 0ˆÔØ"ˆŒØ(ˆÔØ $ˆÔØ#ˆÔäÑPÀÇÁÔPÔPÜ�N‰Nð$ð ×$Ñ$ñ%õð Qr5   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.   )r0   r1   r¹   Úcb_classÚcs        r3   r¼   zCallbackHandler.add_callbackÑ  s�   € Ü% h´Ô5‰XŒZ¸8ˆÜ)¨(´DÔ9‘8¸x×?QÑ?QˆØ¨T¯^©^Ö<¨˜Ÿ›Ò<Ñ<Ü�N‰NØ# 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©r0   r1   r¹   s      r3   Úpop_callbackzCallbackHandler.pop_callbackÜ  ss   € Ü�h¤Ô%Ø—n‘nò �Ü˜b (Õ+Ø—N‘N×)Ñ)¨"Ô-Ø’Iñð
 —n‘nò �Ø˜“>Ø—N‘N×)Ñ)¨"Ô-Ø’Iñr5   c                 óÐ   — t        |t        «      r;| j                  D ]+  }t        ||«      sŒ| j                  j                  |«        y  y | j                  j                  |«       y rµ   rÌ   rÎ   s      r3   Úremove_callbackzCallbackHandler.remove_callbackè  sR   € Ü�h¤Ô%Ø—n‘nò �Ü˜b (Õ+Ø—N‘N×)Ñ)¨"Ô-Ùñð
 �N‰N×!Ñ! (Õ+r5   c                 óF   — dj                  d„ | j                  D «       «      S )Nr;   c              3   óH   K  — | ]  }|j                   j                  –— Œ y ­wrµ   )r+   r,   r·   s     r3   rº   z0CallbackHandler.callback_list.<locals>.<genexpr>ó  s   è ø€ ÒH°2˜Ÿ™×.Õ.ÑHùs   ‚ ")Újoinr»   rt   s    r3   rÆ   zCallbackHandler.callback_listñ  s   € à�y‰yÑH¸¿¹ÔHÓHÐHr5   rX   r/   r�   c                 ó*   — | j                  d|||«      S )Nr‘   ©Ú
call_event©r0   rX   r/   r�   s       r3   r‘   zCallbackHandler.on_init_endõ  ó   € Ø�‰˜}¨d°E¸7ÓCÐCr5   c                 ó8   — d|_         | j                  d|||«      S )NFr”   )r~   r×   rØ   s       r3   r”   zCallbackHandler.on_train_beginø  s    € Ø',ˆÔ$Ø�‰Ð/°°u¸gÓFÐFr5   c                 ó*   — | j                  d|||«      S )Nr–   rÖ   rØ   s       r3   r–   zCallbackHandler.on_train_endü  ó   € Ø�‰˜~¨t°U¸GÓDÐDr5   c                 ó8   — d|_         | j                  d|||«      S )NFr˜   )r   r×   rØ   s       r3   r˜   zCallbackHandler.on_epoch_beginÿ  s    € Ø$)ˆÔ!Ø�‰Ð/°°u¸gÓFÐFr5   c                 ó*   — | j                  d|||«      S )Nrš   rÖ   rØ   s       r3   rš   zCallbackHandler.on_epoch_end  rÜ   r5   c                 óT   — d|_         d|_        d|_        | j                  d|||«      S )NFrœ   )r‚   r�   r€   r×   rØ   s       r3   rœ   zCallbackHandler.on_step_begin  s/   € Ø"ˆÔØ"'ˆÔØ#ˆÔØ�‰˜°°e¸WÓEÐEr5   c                 ó*   — | j                  d|||«      S )NrŸ   rÖ   rØ   s       r3   rŸ   z%CallbackHandler.on_pre_optimizer_step  s   € Ø�‰Ð6¸¸eÀWÓMÐMr5   c                 ó*   — | j                  d|||«      S )Nr¡   rÖ   rØ   s       r3   r¡   z!CallbackHandler.on_optimizer_step  s   € Ø�‰Ð2°D¸%ÀÓIÐIr5   c                 ó*   — | j                  d|||«      S )Nr£   rÖ   rØ   s       r3   r£   zCallbackHandler.on_substep_end  s   € Ø�‰Ð/°°u¸gÓFÐFr5   c                 ó*   — | j                  d|||«      S )Nr¥   rÖ   rØ   s       r3   r¥   zCallbackHandler.on_step_end  rÙ   r5   c                 ó<   — d|_         | j                  d||||¬«      S )NFr§   ©r©   )r�   r×   ©r0   rX   r/   r�   r©   s        r3   r§   zCallbackHandler.on_evaluate  s#   € Ø"'ˆÔØ�‰˜}¨d°E¸7ÈGˆÓTÐTr5   c                 ó.   — | j                  d||||¬«      S )Nrª   rå   rÖ   ræ   s        r3   rª   zCallbackHandler.on_predict  s   € Ø�‰˜|¨T°5¸'È7ˆÓSÐSr5   c                 ó8   — d|_         | j                  d|||«      S )NFr¬   )r€   r×   rØ   s       r3   r¬   zCallbackHandler.on_save  s   € Ø#ˆÔØ�‰˜y¨$°°wÓ?Ð?r5   c                 ó<   — d|_         | j                  d||||¬«      S )NFr®   )Úlogs)r‚   r×   )r0   rX   r/   r�   rê   s        r3   r®   zCallbackHandler.on_log#  s#   € Ø"ˆÔØ�‰˜x¨¨u°gÀDˆÓIÐIr5   c                 ó*   — | j                  d|||«      S )Nr°   rÖ   rØ   s       r3   r°   z"CallbackHandler.on_prediction_step'  s   € Ø�‰Ð3°T¸5À'ÓJÐJr5   c                 óâ   — | j                   D ]_  } t        ||«      |||f| j                  | j                  | j                  | j
                  | j                  | j                  dœ|¤Ž}|€Œ^|}Œa |S )N)r½   r¾   r¿   rÀ   rÁ   rÂ   )r»   rT   r½   r¾   r¿   rÀ   rÁ   rÂ   )r0   ÚeventrX   r/   r�   r�   r1   Úresults           r3   r×   zCallbackHandler.call_event*  sˆ   € ØŸ™ò 	!ˆHØ-”W˜X uÓ-ØØØðð —j‘jØ!%×!6Ñ!6ØŸ.™.Ø!×.Ñ.Ø!%×!6Ñ!6Ø $× 4Ñ 4ñð ñˆFð Ñ!Ø ‘ð	!ð  ˆr5   N)r,   rg   rh   ri   rÇ   r¼   rÏ   rÑ   ÚpropertyrÆ   r   r   r}   r‘   r”   r–   r˜   rš   rœ   rŸ   r¡   r£   r¥   r§   rª   r¬   r®   r°   r×   rJ   r5   r3   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ð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ór5   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.
    rX   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   r€   r   r~   r�   s        r3   r¥   zDefaultFlowCallback.on_step_endC  s#  € à×Ñ Ò! d×&=Ò&=Ø!%ˆGÔØ× Ñ Ô$4×$:Ñ$:Ò:¸u×?PÑ?PÐSX×SfÑSfÑ?fÐjkÒ?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Ø&*�Ô#àˆr5   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
   r€   r�   s        r3   rš   z DefaultFlowCallback.on_epoch_endc  sp   € à× Ñ Ô$4×$:Ñ$:Ò:Ø!%ˆGÔð ×ÑÔ!1×!7Ñ!7Ò7¸D¿O¹OÈuÏ{É{Ò<ZØ&*ˆGÔ#ð ×Ñ¤×!3Ñ!3Ò3Ø"&ˆGÔàˆr5   N)	r,   rg   rh   ri   r   r   r}   r¥   rš   rJ   r5   r3   r¶   r¶   >  s@   „ ñðÐ 1ð ¸,ð ÐQ_ó ð@Ð!2ð ¸<ð ÐR`ô r5   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ü   )r0   rü   s     r3   rÇ   zProgressCallback.__init__y  s   € ð !ˆÔØ"ˆÔØ&ˆÕr5   c                 ób   — |j                   rt        |j                  d¬«      | _        d| _        y )NT)ÚtotalÚdynamic_ncolsr   )r   r   r   rþ   Úcurrent_stepr�   s        r3   r”   zProgressCallback.on_train_begin†  s&   € Ø×&Ò&Ü $¨5¯?©?È$Ô OˆDÔØˆÕr5   c                 ó¤   — |j                   rD| j                  j                  |j                  | j                  z
  «       |j                  | _        y y rµ   )r   rþ   Úupdater   r  r�   s        r3   r¥   zProgressCallback.on_step_end‹  sC   € Ø×&Ò&Ø×Ñ×$Ñ$ U×%6Ñ%6¸×9JÑ9JÑ%JÔKØ %× 1Ñ 1ˆDÕð 'r5   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  )r0   rX   r/   r�   rÂ   r�   s         r3   r°   z#ProgressCallback.on_prediction_step�  sb   € Ø×&Ò&¬:°oÔ+FØ×"Ñ"Ð*Ü&*Ü˜oÓ.°d×6GÑ6GÈ4Ð6OÐ_cô'�Ô#ð ×Ñ×&Ñ& qÕ)ð ,GÐ&r5   c                 óx   — |j                   r.| j                  �| j                  j                  «        d | _        y y rµ   ©r   rÿ   Úcloser�   s        r3   r§   zProgressCallback.on_evaluate˜  ó6   € Ø×&Ò&Ø×"Ñ"Ð.Ø×#Ñ#×)Ñ)Ô+Ø"&ˆDÕð 'r5   c                 óx   — |j                   r.| j                  �| j                  j                  «        d | _        y y rµ   r
  r�   s        r3   rª   zProgressCallback.on_predictž  r  r5   c                 óž  — |j                   rÁ| j                  �´i }|j                  «       D ]R  \  }}t        |t        «      r8t        |«      | j                  kD  r dt        |«      › d| j                  › d�||<   ŒN|||<   ŒT |j                  dd «      }	d|v rt        |d   d«      |d<   | j                  j                  t	        |«      «       y y y )Nz%[String too long to display, length: z > z/. Consider increasing `max_str_len` if needed.]r   r   r8   )
r   rþ   rw   r&   rm   r  rü   ÚpopÚroundrE   )
r0   rX   r/   r�   rê   r�   Úshallow_logsry   rz   Ú_s
             r3   r®   zProgressCallback.on_log¤  sÞ   € Ø×&Ò&¨4×+<Ñ+<Ð+Hð ˆLØŸ
™
›ò (‘��1Ü˜a¤Ô%¬#¨a«&°4×3CÑ3CÒ*Cà?ÄÀAÃ¸xÀsÈ4×K[ÑK[ÐJ\ð ]Hð Hð ! ’Oð
 '(�L ’Oð(ð × Ñ  ¨tÓ4ˆAà˜,Ñ&Ü(-¨l¸7Ñ.CÀQÓ(G�˜WÑ%Ø×Ñ×#Ñ#¤C¨Ó$5Õ6ð! ,IÐ&r5   c                 ó`   — |j                   r"| j                  j                  «        d | _        y y rµ   )r   rþ   r  r�   s        r3   r–   zProgressCallback.on_train_end·  s*   € Ø×&Ò&Ø×Ñ×#Ñ#Ô%Ø $ˆDÕð 'r5   )éd   rµ   )r,   rg   rh   ri   rl   rÇ   r”   r¥   r°   r§   rª   r®   r–   rJ   r5   r3   rû   rû   s  s6   „ ññ
' Có 'òò
2ó
*ò'ò'ó7ó&%r5   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 )Nr   )r  r   Úprint)r0   rX   r/   r�   rê   r�   r  s          r3   r®   zPrinterCallback.on_logÂ  s'   € Ø�H‰H�\ 4Ó(ˆØ×&Ò&Ü�$�Kð 'r5   rµ   )r,   rg   rh   ri   r®   rJ   r5   r3   r  r  ½  s   „ ñôr5   r  c                   óF   — e Zd ZdZddedee   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_thresholdc                 ó.   — || _         || _        d| _        y )Nr   ©r  r  Úearly_stopping_patience_counter)r0   r  r  s      r3   rÇ   zEarlyStoppingCallback.__init__Ù  s   € Ø'>ˆÔ$Ø(@ˆÔ%à/0ˆÕ,r5   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  )r0   rX   r/   r�   Úmetric_valueÚoperators         r3   Úcheck_metric_valuez(EarlyStoppingCallback.check_metric_valueß  sl   € à!%×!7Ò!7”2—:’:¼R¿W¹WˆØ×ÑÐ$Ù�\ 5×#4Ñ#4Ô5Ü�L 5×#4Ñ#4Ñ4Ó5¸×8UÑ8UÒUà34ˆDÕ0à×0Ò0°AÑ5Ö0r5   c                 ó´   — |j                   st        j                  d«       |j                  €J d«       ‚|j                  t
        j                  k7  sJ d«       ‚y )NzŒUsing 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        r3   r”   z$EarlyStoppingCallback.on_train_beginê  sb   € Ø×*Ò*Ü�N‰Nð^ôð ×)Ñ)Ð5ð 	
ØPó	
Ð5ð ×!Ñ!Ô%5×%8Ñ%8Ò8ð 	
ØOó	
Ñ8r5   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  r~   )r0   rX   r/   r�   r©   r�   Úmetric_to_checkr&  s           r3   r§   z!EarlyStoppingCallback.on_evaluate÷  s—   € Ø×4Ñ4ˆØ×)Ñ)¨'Ô2Ø % oÐ%6Ð7ˆOØ—{‘{ ?Ó3ˆàÐÜ�N‰NØRÐSbÐRcð dð ôð à×Ñ  e¨W°lÔCØ×/Ñ/°4×3OÑ3OÒOØ+/ˆGÕ(ð Pr5   rq   c                 óR   — | j                   | j                  dœd| j                  idœS )N)r  r  r  r‹   r  rt   s    r3   r/   zEarlyStoppingCallback.state  s7   € ð ,0×+GÑ+GØ,0×,IÑ,Iñð
 2°4×3WÑ3Wðñ
ð 	
r5   N)r   g        )r,   rg   rh   ri   rl   r   rj   rÇ   r(  r”   r§   r'   r/   rJ   r5   r3   r  r  È  s<   „ ññ 1°ð 1ÐS[Ð\aÑSbó 1ò	6ò
ò0ð"	
�tô 	
r5   r  )!ri   rB   r@   rU   r   Útypingr   r   Únumpyr"  Ú	tqdm.autor   Útrainer_utilsr   r	   r
   r   Útraining_argsr   Úutilsr   Ú
get_loggerr,   rÄ   r   r(   r}   r#   r²   r¶   rû   r  r  rJ   r5   r3   ú<module>r:     sÜ   ðñó Û Û Ý !ß "ã Ý ç VÓ VÝ ,Ý ð 
ˆ×	Ñ	˜HÓ	%€ð ÷XEð XEó ðXE÷v)ñ )ðX ô:
�_ó :
ó ð:
÷zOñ Oôd@�oô @ôF2˜/ô 2ôjG%�ô G%ôT�oô ôI
˜O¨_õ I
r5   