# Callbacks

## Callbacks

Callbacks are objects that can customize the behavior of the training loop in the PyTorch [Trainer](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/trainer#transformers.Trainer) (this feature is not yet implemented in TensorFlow) that can inspect the training loop state (for progress reporting, logging on TensorBoard or other ML platforms…) and take decisions (like early stopping).

Callbacks are “read only” pieces of code, apart from the [TrainerControl](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/callback#transformers.TrainerControl) object they return, they cannot change anything in the training loop. For customizations that require changes in the training loop, you should subclass [Trainer](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/trainer#transformers.Trainer) and override the methods you need (see [trainer](https://huggingface.co/docs/transformers/main_classes/trainer) for examples).

By default a [Trainer](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/trainer#transformers.Trainer) will use the following callbacks:

* [DefaultFlowCallback](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/callback#transformers.DefaultFlowCallback) which handles the default behavior for logging, saving and evaluation.
* [PrinterCallback](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/callback#transformers.PrinterCallback) or [ProgressCallback](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/callback#transformers.ProgressCallback) to display progress and print the logs (the first one is used if you deactivate tqdm through the [TrainingArguments](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/trainer#transformers.TrainingArguments), otherwise it’s the second one).
* [TensorBoardCallback](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/callback#transformers.integrations.TensorBoardCallback) if tensorboard is accessible (either through PyTorch >= 1.4 or tensorboardX).
* [WandbCallback](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/callback#transformers.integrations.WandbCallback) if [wandb](https://www.wandb.com/) is installed.
* [CometCallback](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/callback#transformers.integrations.CometCallback) if [comet\_ml](https://www.comet.ml/site/) is installed.
* [MLflowCallback](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/callback#transformers.integrations.MLflowCallback) if [mlflow](https://www.mlflow.org/) is installed.
* [NeptuneCallback](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/callback#transformers.integrations.NeptuneCallback) if [neptune](https://neptune.ai/) is installed.
* [AzureMLCallback](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/callback#transformers.integrations.AzureMLCallback) if [azureml-sdk](https://pypi.org/project/azureml-sdk/) is installed.
* [CodeCarbonCallback](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/callback#transformers.integrations.CodeCarbonCallback) if [codecarbon](https://pypi.org/project/codecarbon/) is installed.
* [ClearMLCallback](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/callback#transformers.integrations.ClearMLCallback) if [clearml](https://github.com/allegroai/clearml) is installed.
* [DagsHubCallback](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/callback#transformers.integrations.DagsHubCallback) if [dagshub](https://dagshub.com/) is installed.
* [FlyteCallback](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/callback#transformers.integrations.FlyteCallback) if [flyte](https://flyte.org/) is installed.

The main class that implements callbacks is [TrainerCallback](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/callback#transformers.TrainerCallback). It gets the [TrainingArguments](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/trainer#transformers.TrainingArguments) used to instantiate the [Trainer](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/trainer#transformers.Trainer), can access that Trainer’s internal state via [TrainerState](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/callback#transformers.TrainerState), and can take some actions on the training loop via [TrainerControl](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/callback#transformers.TrainerControl).

### Available Callbacks

Here is the list of the available [TrainerCallback](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/callback#transformers.TrainerCallback) in the library:

#### class transformers.integrations.CometCallback

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/integrations/integration_utils.py#L832)

( )

A [TrainerCallback](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/callback#transformers.TrainerCallback) that sends the logs to [Comet ML](https://www.comet.ml/site/).

**setup**

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/integrations/integration_utils.py#L843)

( argsstatemodel )

Setup the optional Comet.ml integration.

Environment:

* **COMET\_MODE** (`str`, *optional*, defaults to `ONLINE`): Whether to create an online, offline experiment or disable Comet logging. Can be `OFFLINE`, `ONLINE`, or `DISABLED`.
* **COMET\_PROJECT\_NAME** (`str`, *optional*): Comet project name for experiments.
* **COMET\_OFFLINE\_DIRECTORY** (`str`, *optional*): Folder to use for saving offline experiments when `COMET_MODE` is `OFFLINE`.
* **COMET\_LOG\_ASSETS** (`str`, *optional*, defaults to `TRUE`): Whether or not to log training assets (tf event logs, checkpoints, etc), to Comet. Can be `TRUE`, or `FALSE`.

For a number of configurable items in the environment, see [here](https://www.comet.ml/docs/python-sdk/advanced/#comet-configuration-variables).

#### class transformers.DefaultFlowCallback

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/trainer_callback.py#L424)

( )

A [TrainerCallback](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/callback#transformers.TrainerCallback) that handles the default flow of the training loop for logs, evaluation and checkpoints.

#### class transformers.PrinterCallback

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/trainer_callback.py#L524)

( )

A bare [TrainerCallback](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/callback#transformers.TrainerCallback) that just prints the logs.

#### class transformers.ProgressCallback

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/trainer_callback.py#L474)

( )

A [TrainerCallback](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/callback#transformers.TrainerCallback) that displays the progress of training or evaluation.

#### class transformers.EarlyStoppingCallback

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/trainer_callback.py#L535)

( early\_stopping\_patience: int = 1early\_stopping\_threshold: typing.Optional\[float] = 0.0 )

Parameters

* **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. \`

A [TrainerCallback](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/callback#transformers.TrainerCallback) that handles early stopping.

This callback depends on [TrainingArguments](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/trainer#transformers.TrainingArguments) argument *load\_best\_model\_at\_end* functionality to set best\_metric in [TrainerState](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/callback#transformers.TrainerState). Note that if the [TrainingArguments](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/trainer#transformers.TrainingArguments) argument *save\_steps* differs from *eval\_steps*, the early stopping will not occur until the next save step.

#### class transformers.integrations.TensorBoardCallback

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/integrations/integration_utils.py#L579)

( tb\_writer = None )

Parameters

* **tb\_writer** (`SummaryWriter`, *optional*) — The writer to use. Will instantiate one if not set.

A [TrainerCallback](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/callback#transformers.TrainerCallback) that sends the logs to [TensorBoard](https://www.tensorflow.org/tensorboard).

#### class transformers.integrations.WandbCallback

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/integrations/integration_utils.py#L665)

( )

A [TrainerCallback](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/callback#transformers.TrainerCallback) that logs metrics, media, model checkpoints to [Weight and Biases](https://www.wandb.com/).

**setup**

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/integrations/integration_utils.py#L690)

( argsstatemodel\*\*kwargs )

Setup the optional Weights & Biases (*wandb*) integration.

One can subclass and override this method to customize the setup if needed. Find more information [here](https://docs.wandb.ai/guides/integrations/huggingface). You can also override the following environment variables:

Environment:

* **WANDB\_LOG\_MODEL** (`str`, *optional*, defaults to `"false"`): Whether to log model and checkpoints during training. Can be `"end"`, `"checkpoint"` or `"false"`. If set to `"end"`, the model will be uploaded at the end of training. If set to `"checkpoint"`, the checkpoint will be uploaded every `args.save_steps` . If set to `"false"`, the model will not be uploaded. Use along with `load_best_model_at_end()` to upload best model.

  Deprecated in 5.0

  Setting `WANDB_LOG_MODEL` as `bool` will be deprecated in version 5 of 🌍Transformers.
* **WANDB\_WATCH** (`str`, *optional* defaults to `"false"`): Can be `"gradients"`, `"all"`, `"parameters"`, or `"false"`. Set to `"all"` to log gradients and parameters.
* **WANDB\_PROJECT** (`str`, *optional*, defaults to `"boincai"`): Set this to a custom string to store results in a different project.
* **WANDB\_DISABLED** (`bool`, *optional*, defaults to `False`): Whether to disable wandb entirely. Set `WANDB_DISABLED=true` to disable.

#### class transformers.integrations.MLflowCallback

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/integrations/integration_utils.py#L932)

( )

A [TrainerCallback](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/callback#transformers.TrainerCallback) that sends the logs to [MLflow](https://www.mlflow.org/). Can be disabled by setting environment variable `DISABLE_MLFLOW_INTEGRATION = TRUE`.

**setup**

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/integrations/integration_utils.py#L951)

( argsstatemodel )

Setup the optional MLflow integration.

Environment:

* **HF\_MLFLOW\_LOG\_ARTIFACTS** (`str`, *optional*): Whether to use MLflow `.log_artifact()` facility to log artifacts. This only makes sense if logging to a remote server, e.g. s3 or GCS. If set to `True` or *1*, will copy each saved checkpoint on each save in [TrainingArguments](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/trainer#transformers.TrainingArguments)’s `output_dir` to the local or remote artifact storage. Using it without a remote storage will just copy the files to your artifact location.
* **MLFLOW\_EXPERIMENT\_NAME** (`str`, *optional*, defaults to `None`): Whether to use an MLflow experiment\_name under which to launch the run. Default to `None` which will point to the `Default` experiment in MLflow. Otherwise, it is a case sensitive name of the experiment to be activated. If an experiment with this name does not exist, a new experiment with this name is created.
* **MLFLOW\_TAGS** (`str`, *optional*): A string dump of a dictionary of key/value pair to be added to the MLflow run as tags. Example: `os.environ['MLFLOW_TAGS']='{"release.candidate": "RC1", "release.version": "2.2.0"}'`.
* **MLFLOW\_NESTED\_RUN** (`str`, *optional*): Whether to use MLflow nested runs. If set to `True` or *1*, will create a nested run inside the current run.
* **MLFLOW\_RUN\_ID** (`str`, *optional*): Allow to reattach to an existing run which can be usefull when resuming training from a checkpoint. When `MLFLOW_RUN_ID` environment variable is set, `start_run` attempts to resume a run with the specified run ID and other parameters are ignored.
* **MLFLOW\_FLATTEN\_PARAMS** (`str`, *optional*, defaults to `False`): Whether to flatten the parameters dictionary before logging.

#### class transformers.integrations.AzureMLCallback

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/integrations/integration_utils.py#L909)

( azureml\_run = None )

A [TrainerCallback](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/callback#transformers.TrainerCallback) that sends the logs to [AzureML](https://pypi.org/project/azureml-sdk/).

#### class transformers.integrations.CodeCarbonCallback

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/integrations/integration_utils.py#L1398)

( )

A [TrainerCallback](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/callback#transformers.TrainerCallback) that tracks the CO2 emission of training.

#### class transformers.integrations.NeptuneCallback

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/integrations/integration_utils.py#L1127)

( api\_token: typing.Optional\[str] = Noneproject: typing.Optional\[str] = Nonename: typing.Optional\[str] = Nonebase\_namespace: str = 'finetuning'run = Nonelog\_parameters: bool = Truelog\_checkpoints: typing.Optional\[str] = None\*\*neptune\_run\_kwargs )

Parameters

* **api\_token** (`str`, *optional*) — Neptune API token obtained upon registration. You can leave this argument out if you have saved your token to the `NEPTUNE_API_TOKEN` environment variable (strongly recommended). See full setup instructions in the [docs](https://docs.neptune.ai/setup/installation).
* **project** (`str`, *optional*) — Name of an existing Neptune project, in the form “workspace-name/project-name”. You can find and copy the name in Neptune from the project settings -> Properties. If None (default), the value of the `NEPTUNE_PROJECT` environment variable is used.
* **name** (`str`, *optional*) — Custom name for the run.
* **base\_namespace** (`str`, optional, defaults to “finetuning”) — In the Neptune run, the root namespace that will contain all of the metadata logged by the callback.
* **log\_parameters** (`bool`, *optional*, defaults to `True`) — If True, logs all Trainer arguments and model parameters provided by the Trainer.
* **log\_checkpoints** (`str`, *optional*) — If “same”, uploads checkpoints whenever they are saved by the Trainer. If “last”, uploads only the most recently saved checkpoint. If “best”, uploads the best checkpoint (among the ones saved by the Trainer). If `None`, does not upload checkpoints.
* **run** (`Run`, *optional*) — Pass a Neptune run object if you want to continue logging to an existing run. Read more about resuming runs in the [docs](https://docs.neptune.ai/logging/to_existing_object).
* \***\*neptune\_run\_kwargs** (*optional*) — Additional keyword arguments to be passed directly to the [`neptune.init_run()`](https://docs.neptune.ai/api/neptune#init_run) function when a new run is created.

TrainerCallback that sends the logs to [Neptune](https://app.neptune.ai/).

For instructions and examples, see the [Transformers integration guide](https://docs.neptune.ai/integrations/transformers) in the Neptune documentation.

#### class transformers.integrations.ClearMLCallback

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/integrations/integration_utils.py#L1427)

( )

A [TrainerCallback](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/callback#transformers.TrainerCallback) that sends the logs to [ClearML](https://clear.ml/).

Environment:

* **CLEARML\_PROJECT** (`str`, *optional*, defaults to `BOINCAI Transformers`): ClearML project name.
* **CLEARML\_TASK** (`str`, *optional*, defaults to `Trainer`): ClearML task name.
* **CLEARML\_LOG\_MODEL** (`bool`, *optional*, defaults to `False`): Whether to log models as artifacts during training.

#### class transformers.integrations.DagsHubCallback

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/integrations/integration_utils.py#L1067)

( )

A [TrainerCallback](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/callback#transformers.TrainerCallback) that logs to [DagsHub](https://dagshub.com/). Extends `MLflowCallback`

**setup**

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/integrations/integration_utils.py#L1081)

( \*args\*\*kwargs )

Setup the DagsHub’s Logging integration.

Environment:

* **HF\_DAGSHUB\_LOG\_ARTIFACTS** (`str`, *optional*): Whether to save the data and model artifacts for the experiment. Default to `False`.

#### class transformers.integrations.FlyteCallback

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/integrations/integration_utils.py#L1546)

( save\_log\_history: bool = Truesync\_checkpoints: bool = True )

Parameters

* **save\_log\_history** (`bool`, *optional*, defaults to `True`) — When set to True, the training logs are saved as a Flyte Deck.
* **sync\_checkpoints** (`bool`, *optional*, defaults to `True`) — When set to True, checkpoints are synced with Flyte and can be used to resume training in the case of an interruption.

A [TrainerCallback](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/callback#transformers.TrainerCallback) that sends the logs to [Flyte](https://flyte.org/). NOTE: This callback only works within a Flyte task.

Example:

Copied

```
# Note: This example skips over some setup steps for brevity.
from flytekit import current_context, task


@task
def train_hf_transformer():
    cp = current_context().checkpoint
    trainer = Trainer(..., callbacks=[FlyteCallback()])
    output = trainer.train(resume_from_checkpoint=cp.restore())
```

### TrainerCallback

#### class transformers.TrainerCallback

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/trainer_callback.py#L168)

( )

Parameters

* **args** ([TrainingArguments](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/trainer#transformers.TrainingArguments)) — The training arguments used to instantiate the [Trainer](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/trainer#transformers.Trainer).
* **state** ([TrainerState](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/callback#transformers.TrainerState)) — The current state of the [Trainer](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/trainer#transformers.Trainer).
* **control** ([TrainerControl](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/callback#transformers.TrainerControl)) — The object that is returned to the [Trainer](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/trainer#transformers.Trainer) and can be used to make some decisions.
* **model** ([PreTrainedModel](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/model#transformers.PreTrainedModel) or `torch.nn.Module`) — The model being trained.
* **tokenizer** ([PreTrainedTokenizer](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/tokenizer#transformers.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`.

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:

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:

Copied

```
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)
```

**on\_epoch\_begin**

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/trainer_callback.py#L236)

( args: TrainingArgumentsstate: TrainerStatecontrol: TrainerControl\*\*kwargs )

Event called at the beginning of an epoch.

**on\_epoch\_end**

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/trainer_callback.py#L242)

( args: TrainingArgumentsstate: TrainerStatecontrol: TrainerControl\*\*kwargs )

Event called at the end of an epoch.

**on\_evaluate**

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/trainer_callback.py#L268)

( args: TrainingArgumentsstate: TrainerStatecontrol: TrainerControl\*\*kwargs )

Event called after an evaluation phase.

**on\_init\_end**

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/trainer_callback.py#L218)

( args: TrainingArgumentsstate: TrainerStatecontrol: TrainerControl\*\*kwargs )

Event called at the end of the initialization of the [Trainer](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/trainer#transformers.Trainer).

**on\_log**

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/trainer_callback.py#L286)

( args: TrainingArgumentsstate: TrainerStatecontrol: TrainerControl\*\*kwargs )

Event called after logging the last logs.

**on\_predict**

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/trainer_callback.py#L274)

( args: TrainingArgumentsstate: TrainerStatecontrol: TrainerControlmetrics\*\*kwargs )

Event called after a successful prediction.

**on\_prediction\_step**

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/trainer_callback.py#L292)

( args: TrainingArgumentsstate: TrainerStatecontrol: TrainerControl\*\*kwargs )

Event called after a prediction step.

**on\_save**

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/trainer_callback.py#L280)

( args: TrainingArgumentsstate: TrainerStatecontrol: TrainerControl\*\*kwargs )

Event called after a checkpoint save.

**on\_step\_begin**

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/trainer_callback.py#L248)

( args: TrainingArgumentsstate: TrainerStatecontrol: TrainerControl\*\*kwargs )

Event called at the beginning of a training step. If using gradient accumulation, one training step might take several inputs.

**on\_step\_end**

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/trainer_callback.py#L261)

( args: TrainingArgumentsstate: TrainerStatecontrol: TrainerControl\*\*kwargs )

Event called at the end of a training step. If using gradient accumulation, one training step might take several inputs.

**on\_substep\_end**

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/trainer_callback.py#L255)

( args: TrainingArgumentsstate: TrainerStatecontrol: TrainerControl\*\*kwargs )

Event called at the end of an substep during gradient accumulation.

**on\_train\_begin**

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/trainer_callback.py#L224)

( args: TrainingArgumentsstate: TrainerStatecontrol: TrainerControl\*\*kwargs )

Event called at the beginning of training.

**on\_train\_end**

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/trainer_callback.py#L230)

( args: TrainingArgumentsstate: TrainerStatecontrol: TrainerControl\*\*kwargs )

Event called at the end of training.

Here is an example of how to register a custom callback with the PyTorch [Trainer](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/trainer#transformers.Trainer):

Copied

```
class MyCallback(TrainerCallback):
    "A callback that prints a message at the beginning of training"

    def on_train_begin(self, args, state, control, **kwargs):
        print("Starting training")


trainer = Trainer(
    model,
    args,
    train_dataset=train_dataset,
    eval_dataset=eval_dataset,
    callbacks=[MyCallback],  # We can either pass the callback class this way or an instance of it (MyCallback())
)
```

Another way to register a callback is to call `trainer.add_callback()` as follows:

Copied

```
trainer = Trainer(...)
trainer.add_callback(MyCallback)
# Alternatively, we can pass an instance of the callback class
trainer.add_callback(MyCallback())
```

### TrainerState

#### class transformers.TrainerState

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/trainer_callback.py#L35)

( epoch: typing.Optional\[float] = Noneglobal\_step: int = 0max\_steps: int = 0logging\_steps: int = 500eval\_steps: int = 500save\_steps: int = 500num\_train\_epochs: int = 0total\_flos: float = 0log\_history: typing.List\[typing.Dict\[str, float]] = Nonebest\_metric: typing.Optional\[float] = Nonebest\_model\_checkpoint: typing.Optional\[str] = Noneis\_local\_process\_zero: bool = Trueis\_world\_process\_zero: bool = Trueis\_hyper\_param\_search: bool = Falsetrial\_name: str = Nonetrial\_params: typing.Dict\[str, typing.Union\[str, float, int, bool]] = None )

Parameters

* **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.

A class containing the [Trainer](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/trainer#transformers.Trainer) inner state that will be saved along the model and optimizer when checkpointing and passed to the [TrainerCallback](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/callback#transformers.TrainerCallback).

In all this class, one step is to be understood as one update step. When using gradient accumulation, one update step may require several forward and backward passes: if you use `gradient_accumulation_steps=n`, then one update step requires going through *n* batches.

**load\_from\_json**

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/trainer_callback.py#L110)

( json\_path: str )

Create an instance from the content of `json_path`.

**save\_to\_json**

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/trainer_callback.py#L104)

( json\_path: str )

Save the content of this instance in JSON format inside `json_path`.

### TrainerControl

#### class transformers.TrainerControl

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/trainer_callback.py#L119)

( should\_training\_stop: bool = Falseshould\_epoch\_stop: bool = Falseshould\_save: bool = Falseshould\_evaluate: bool = Falseshould\_log: bool = False )

Parameters

* **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.

A class that handles the [Trainer](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/trainer#transformers.Trainer) control flow. This class is used by the [TrainerCallback](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/callback#transformers.TrainerCallback) to activate some switches in the training loop.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://boinc-ai.gitbook.io/transformers/api/main-classes/callbacks.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
