---
local: callbacks
sections:
- local: transformers.integrations.CometCallback
  title: Available Callbacks
- local: transformers.TrainerCallback
  title: TrainerCallback
- local: transformers.TrainerState
  title: TrainerState
- local: transformers.TrainerControl
  title: TrainerControl
title: Callbacks
---
<script>
import Tip from "./Tip.svelte";
import Youtube from "./Youtube.svelte";
import Docstring from "./Docstring.svelte";
import CodeBlock from "./CodeBlock.svelte";
import CodeBlockFw from "./CodeBlockFw.svelte";
import DocNotebookDropdown from "./DocNotebookDropdown.svelte";
import IconCopyLink from "./IconCopyLink.svelte";
export let fw: "pt" | "tf"
</script>
<!--Copyright 2020 The HuggingFace Team. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
-->

<h1 id="callbacks">Callbacks</h1>

Callbacks are objects that can customize the behavior of the training loop in the PyTorch
[Trainer](/docs/transformers/pr_15592/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](/docs/transformers/pr_15592/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](/docs/transformers/pr_15592/en/main_classes/trainer#transformers.Trainer) and override the methods you need (see [trainer](trainer) for examples).

By default a [Trainer](/docs/transformers/pr_15592/en/main_classes/trainer#transformers.Trainer) will use the following callbacks:

- [DefaultFlowCallback](/docs/transformers/pr_15592/en/main_classes/callback#transformers.DefaultFlowCallback) which handles the default behavior for logging, saving and evaluation.
- [PrinterCallback](/docs/transformers/pr_15592/en/main_classes/callback#transformers.PrinterCallback) or [ProgressCallback](/docs/transformers/pr_15592/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](/docs/transformers/pr_15592/en/main_classes/trainer#transformers.TrainingArguments), otherwise
  it's the second one).
- [TensorBoardCallback](/docs/transformers/pr_15592/en/main_classes/callback#transformers.integrations.TensorBoardCallback) if tensorboard is accessible (either through PyTorch >= 1.4
  or tensorboardX).
- [WandbCallback](/docs/transformers/pr_15592/en/main_classes/callback#transformers.integrations.WandbCallback) if [wandb](https://www.wandb.com/) is installed.
- [CometCallback](/docs/transformers/pr_15592/en/main_classes/callback#transformers.integrations.CometCallback) if [comet_ml](https://www.comet.ml/site/) is installed.
- [MLflowCallback](/docs/transformers/pr_15592/en/main_classes/callback#transformers.integrations.MLflowCallback) if [mlflow](https://www.mlflow.org/) is installed.
- [AzureMLCallback](/docs/transformers/pr_15592/en/main_classes/callback#transformers.integrations.AzureMLCallback) if [azureml-sdk](https://pypi.org/project/azureml-sdk/) is
  installed.
- [CodeCarbonCallback](/docs/transformers/pr_15592/en/main_classes/callback#transformers.integrations.CodeCarbonCallback) if [codecarbon](https://pypi.org/project/codecarbon/) is
  installed.

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


<h2 id="transformers.integrations.CometCallback">Available Callbacks</h2>

Here is the list of the available [TrainerCallback](/docs/transformers/pr_15592/en/main_classes/callback#transformers.TrainerCallback) in the library:

<div class="docstring">

<docstring><name>class transformers.integrations.CometCallback</name><anchor>transformers.integrations.CometCallback</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/integrations.py#L660</source><parameters>[]</parameters></docstring>

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



<div class="docstring">
<docstring><name>setup</name><anchor>transformers.integrations.CometCallback.setup</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/integrations.py#L671</source><parameters>[{"name": "args", "val": ""}, {"name": "state", "val": ""}, {"name": "model", "val": ""}]</parameters></docstring>

Setup the optional Comet.ml integration.

Environment:
COMET_MODE (`str`, *optional*):
Whether to create an online, offline experiment or disable Comet logging. Can be "OFFLINE", "ONLINE",
or "DISABLED". Defaults to "ONLINE".
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*):
Whether or not to log training assets (tf event logs, checkpoints, etc), to Comet. Can be "TRUE", or
"FALSE". Defaults to "TRUE".

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


</div></div>

<div class="docstring">

<docstring><name>class transformers.DefaultFlowCallback</name><anchor>transformers.DefaultFlowCallback</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/trainer_callback.py#L406</source><parameters>[]</parameters></docstring>

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


</div>

<div class="docstring">

<docstring><name>class transformers.PrinterCallback</name><anchor>transformers.PrinterCallback</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/trainer_callback.py#L494</source><parameters>[]</parameters></docstring>

A bare [TrainerCallback](/docs/transformers/pr_15592/en/main_classes/callback#transformers.TrainerCallback) that just prints the logs.


</div>

<div class="docstring">

<docstring><name>class transformers.ProgressCallback</name><anchor>transformers.ProgressCallback</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/trainer_callback.py#L452</source><parameters>[]</parameters></docstring>

A [TrainerCallback](/docs/transformers/pr_15592/en/main_classes/callback#transformers.TrainerCallback) that displays the progress of training or evaluation.


</div>

<div class="docstring">

<docstring><name>class transformers.EarlyStoppingCallback</name><anchor>transformers.EarlyStoppingCallback</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/trainer_callback.py#L505</source><parameters>[{"name": "early_stopping_patience", "val": ": int = 1"}, {"name": "early_stopping_threshold", "val": ": typing.Optional[float] = 0.0"}]</parameters><paramsdesc>- **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. `</paramsdesc><paramgroups>0</paramgroups></docstring>

A [TrainerCallback](/docs/transformers/pr_15592/en/main_classes/callback#transformers.TrainerCallback) that handles early stopping.



This callback depends on [TrainingArguments](/docs/transformers/pr_15592/en/main_classes/trainer#transformers.TrainingArguments) argument *load_best_model_at_end* functionality to set best_metric
in [TrainerState](/docs/transformers/pr_15592/en/main_classes/callback#transformers.TrainerState).


</div>

<div class="docstring">

<docstring><name>class transformers.integrations.TensorBoardCallback</name><anchor>transformers.integrations.TensorBoardCallback</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/integrations.py#L446</source><parameters>[{"name": "tb_writer", "val": " = None"}]</parameters><paramsdesc>- **tb_writer** (`SummaryWriter`, *optional*) --
  The writer to use. Will instantiate one if not set.</paramsdesc><paramgroups>0</paramgroups></docstring>

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




</div>

<div class="docstring">

<docstring><name>class transformers.integrations.WandbCallback</name><anchor>transformers.integrations.WandbCallback</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/integrations.py#L534</source><parameters>[]</parameters></docstring>

A [TrainerCallback](/docs/transformers/pr_15592/en/main_classes/callback#transformers.TrainerCallback) that sends the logs to [Weight and Biases](https://www.wandb.com/).



<div class="docstring">
<docstring><name>setup</name><anchor>transformers.integrations.WandbCallback.setup</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/integrations.py#L551</source><parameters>[{"name": "args", "val": ""}, {"name": "state", "val": ""}, {"name": "model", "val": ""}, {"name": "**kwargs", "val": ""}]</parameters></docstring>

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/integrations/huggingface). You can also override the following environment
variables:

Environment:
WANDB_LOG_MODEL (`bool`, *optional*, defaults to `False`):
Whether or not to log model as artifact at the end of training. Use along with
*TrainingArguments.load_best_model_at_end* to upload best model.
WANDB_WATCH (`str`, *optional* defaults to `"gradients"`):
Can be `"gradients"`, `"all"` or `"false"`. Set to `"false"` to disable gradient logging or `"all"` to
log gradients and parameters.
WANDB_PROJECT (`str`, *optional*, defaults to `"huggingface"`):
Set this to a custom string to store results in a different project.
WANDB_DISABLED (`bool`, *optional*, defaults to `False`):
Whether or not to disable wandb entirely. Set *WANDB_DISABLED=true* to disable.


</div></div>

<div class="docstring">

<docstring><name>class transformers.integrations.MLflowCallback</name><anchor>transformers.integrations.MLflowCallback</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/integrations.py#L759</source><parameters>[]</parameters></docstring>

A [TrainerCallback](/docs/transformers/pr_15592/en/main_classes/callback#transformers.TrainerCallback) that sends the logs to [MLflow](https://www.mlflow.org/).



<div class="docstring">
<docstring><name>setup</name><anchor>transformers.integrations.MLflowCallback.setup</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/integrations.py#L776</source><parameters>[{"name": "args", "val": ""}, {"name": "state", "val": ""}, {"name": "model", "val": ""}]</parameters></docstring>

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
whatever is in [TrainingArguments](/docs/transformers/pr_15592/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.


</div></div>

<div class="docstring">

<docstring><name>class transformers.integrations.AzureMLCallback</name><anchor>transformers.integrations.AzureMLCallback</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/integrations.py#L736</source><parameters>[{"name": "azureml_run", "val": " = None"}]</parameters></docstring>

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


</div>

<div class="docstring">

<docstring><name>class transformers.integrations.CodeCarbonCallback</name><anchor>transformers.integrations.CodeCarbonCallback</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/integrations.py#L919</source><parameters>[]</parameters></docstring>

A [TrainerCallback](/docs/transformers/pr_15592/en/main_classes/callback#transformers.TrainerCallback) that tracks the CO2 emission of training.


</div>

<h2 id="transformers.TrainerCallback">TrainerCallback</h2>

<div class="docstring">

<docstring><name>class transformers.TrainerCallback</name><anchor>transformers.TrainerCallback</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/trainer_callback.py#L159</source><parameters>[]</parameters><paramsdesc>- **args** ([TrainingArguments](/docs/transformers/pr_15592/en/main_classes/trainer#transformers.TrainingArguments)) --
  The training arguments used to instantiate the [Trainer](/docs/transformers/pr_15592/en/main_classes/trainer#transformers.Trainer).
- **state** ([TrainerState](/docs/transformers/pr_15592/en/main_classes/callback#transformers.TrainerState)) --
  The current state of the [Trainer](/docs/transformers/pr_15592/en/main_classes/trainer#transformers.Trainer).
- **control** ([TrainerControl](/docs/transformers/pr_15592/en/main_classes/callback#transformers.TrainerControl)) --
  The object that is returned to the [Trainer](/docs/transformers/pr_15592/en/main_classes/trainer#transformers.Trainer) and can be used to make some decisions.
- **model** ([PreTrainedModel](/docs/transformers/pr_15592/en/main_classes/model#transformers.PreTrainedModel) or `torch.nn.Module`) --
  The model being trained.
- **tokenizer** ([PreTrainedTokenizer](/docs/transformers/pr_15592/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`.</paramsdesc><paramgroups>0</paramgroups></docstring>

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


<div class="docstring">
<docstring><name>on\_epoch\_begin</name><anchor>transformers.TrainerCallback.on_epoch_begin</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/trainer_callback.py#L227</source><parameters>[{"name": "args", "val": ": TrainingArguments"}, {"name": "state", "val": ": TrainerState"}, {"name": "control", "val": ": TrainerControl"}, {"name": "**kwargs", "val": ""}]</parameters></docstring>

Event called at the beginning of an epoch.


</div>
<div class="docstring">
<docstring><name>on\_epoch\_end</name><anchor>transformers.TrainerCallback.on_epoch_end</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/trainer_callback.py#L233</source><parameters>[{"name": "args", "val": ": TrainingArguments"}, {"name": "state", "val": ": TrainerState"}, {"name": "control", "val": ": TrainerControl"}, {"name": "**kwargs", "val": ""}]</parameters></docstring>

Event called at the end of an epoch.


</div>
<div class="docstring">
<docstring><name>on\_evaluate</name><anchor>transformers.TrainerCallback.on_evaluate</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/trainer_callback.py#L259</source><parameters>[{"name": "args", "val": ": TrainingArguments"}, {"name": "state", "val": ": TrainerState"}, {"name": "control", "val": ": TrainerControl"}, {"name": "**kwargs", "val": ""}]</parameters></docstring>

Event called after an evaluation phase.


</div>
<div class="docstring">
<docstring><name>on\_init\_end</name><anchor>transformers.TrainerCallback.on_init_end</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/trainer_callback.py#L209</source><parameters>[{"name": "args", "val": ": TrainingArguments"}, {"name": "state", "val": ": TrainerState"}, {"name": "control", "val": ": TrainerControl"}, {"name": "**kwargs", "val": ""}]</parameters></docstring>

Event called at the end of the initialization of the [Trainer](/docs/transformers/pr_15592/en/main_classes/trainer#transformers.Trainer).


</div>
<div class="docstring">
<docstring><name>on\_log</name><anchor>transformers.TrainerCallback.on_log</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/trainer_callback.py#L271</source><parameters>[{"name": "args", "val": ": TrainingArguments"}, {"name": "state", "val": ": TrainerState"}, {"name": "control", "val": ": TrainerControl"}, {"name": "**kwargs", "val": ""}]</parameters></docstring>

Event called after logging the last logs.


</div>
<div class="docstring">
<docstring><name>on\_prediction\_step</name><anchor>transformers.TrainerCallback.on_prediction_step</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/trainer_callback.py#L277</source><parameters>[{"name": "args", "val": ": TrainingArguments"}, {"name": "state", "val": ": TrainerState"}, {"name": "control", "val": ": TrainerControl"}, {"name": "**kwargs", "val": ""}]</parameters></docstring>

Event called after a prediction step.


</div>
<div class="docstring">
<docstring><name>on\_save</name><anchor>transformers.TrainerCallback.on_save</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/trainer_callback.py#L265</source><parameters>[{"name": "args", "val": ": TrainingArguments"}, {"name": "state", "val": ": TrainerState"}, {"name": "control", "val": ": TrainerControl"}, {"name": "**kwargs", "val": ""}]</parameters></docstring>

Event called after a checkpoint save.


</div>
<div class="docstring">
<docstring><name>on\_step\_begin</name><anchor>transformers.TrainerCallback.on_step_begin</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/trainer_callback.py#L239</source><parameters>[{"name": "args", "val": ": TrainingArguments"}, {"name": "state", "val": ": TrainerState"}, {"name": "control", "val": ": TrainerControl"}, {"name": "**kwargs", "val": ""}]</parameters></docstring>

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


</div>
<div class="docstring">
<docstring><name>on\_step\_end</name><anchor>transformers.TrainerCallback.on_step_end</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/trainer_callback.py#L252</source><parameters>[{"name": "args", "val": ": TrainingArguments"}, {"name": "state", "val": ": TrainerState"}, {"name": "control", "val": ": TrainerControl"}, {"name": "**kwargs", "val": ""}]</parameters></docstring>

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


</div>
<div class="docstring">
<docstring><name>on\_substep\_end</name><anchor>transformers.TrainerCallback.on_substep_end</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/trainer_callback.py#L246</source><parameters>[{"name": "args", "val": ": TrainingArguments"}, {"name": "state", "val": ": TrainerState"}, {"name": "control", "val": ": TrainerControl"}, {"name": "**kwargs", "val": ""}]</parameters></docstring>

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


</div>
<div class="docstring">
<docstring><name>on\_train\_begin</name><anchor>transformers.TrainerCallback.on_train_begin</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/trainer_callback.py#L215</source><parameters>[{"name": "args", "val": ": TrainingArguments"}, {"name": "state", "val": ": TrainerState"}, {"name": "control", "val": ": TrainerControl"}, {"name": "**kwargs", "val": ""}]</parameters></docstring>

Event called at the beginning of training.


</div>
<div class="docstring">
<docstring><name>on\_train\_end</name><anchor>transformers.TrainerCallback.on_train_end</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/trainer_callback.py#L221</source><parameters>[{"name": "args", "val": ": TrainingArguments"}, {"name": "state", "val": ": TrainerState"}, {"name": "control", "val": ": TrainerControl"}, {"name": "**kwargs", "val": ""}]</parameters></docstring>

Event called at the end of training.


</div></div>

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

```python
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:

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

<h2 id="transformers.TrainerState">TrainerState</h2>

<div class="docstring">

<docstring><name>class transformers.TrainerState</name><anchor>transformers.TrainerState</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/trainer_callback.py#L35</source><parameters>[{"name": "epoch", "val": ": typing.Optional[float] = None"}, {"name": "global_step", "val": ": int = 0"}, {"name": "max_steps", "val": ": int = 0"}, {"name": "num_train_epochs", "val": ": int = 0"}, {"name": "total_flos", "val": ": float = 0"}, {"name": "log_history", "val": ": typing.List[typing.Dict[str, float]] = None"}, {"name": "best_metric", "val": ": typing.Optional[float] = None"}, {"name": "best_model_checkpoint", "val": ": typing.Optional[str] = None"}, {"name": "is_local_process_zero", "val": ": bool = True"}, {"name": "is_world_process_zero", "val": ": bool = True"}, {"name": "is_hyper_param_search", "val": ": bool = False"}, {"name": "trial_name", "val": ": str = None"}, {"name": "trial_params", "val": ": typing.Dict[str, typing.Union[str, float, int, bool]] = None"}]</parameters><paramsdesc>- **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.
- **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.</paramsdesc><paramgroups>0</paramgroups></docstring>

A class containing the [Trainer](/docs/transformers/pr_15592/en/main_classes/trainer#transformers.Trainer) inner state that will be saved along the model and optimizer when checkpointing
and passed to the [TrainerCallback](/docs/transformers/pr_15592/en/main_classes/callback#transformers.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>





<div class="docstring">
<docstring><name>load\_from\_json</name><anchor>transformers.TrainerState.load_from_json</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/trainer_callback.py#L101</source><parameters>[{"name": "json_path", "val": ": str"}]</parameters></docstring>
Create an instance from the content of `json_path`.

</div>
<div class="docstring">
<docstring><name>save\_to\_json</name><anchor>transformers.TrainerState.save_to_json</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/trainer_callback.py#L95</source><parameters>[{"name": "json_path", "val": ": str"}]</parameters></docstring>
Save the content of this instance in JSON format inside `json_path`.

</div></div>

<h2 id="transformers.TrainerControl">TrainerControl</h2>

<div class="docstring">

<docstring><name>class transformers.TrainerControl</name><anchor>transformers.TrainerControl</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/trainer_callback.py#L110</source><parameters>[{"name": "should_training_stop", "val": ": bool = False"}, {"name": "should_epoch_stop", "val": ": bool = False"}, {"name": "should_save", "val": ": bool = False"}, {"name": "should_evaluate", "val": ": bool = False"}, {"name": "should_log", "val": ": bool = False"}]</parameters><paramsdesc>- **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.</paramsdesc><paramgroups>0</paramgroups></docstring>

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




</div>
