Path: blob/master/site/en-snapshot/guide/migrate/fault_tolerance.ipynb
39067 views
Copyright 2021 The TensorFlow Authors.
Migrate the fault tolerance mechanism
Fault tolerance refers to a mechanism of periodically saving the states of trackable objects, such as parameters and models. This enables you to recover them in the event of a program/machine failure during training.
This guide first demonstrates how to add fault tolerance to training with tf.estimator.Estimator in TensorFlow 1 by specifying metric saving with tf.estimator.RunConfig. Then, you will learn how to implement fault tolerance for training in Tensorflow 2 in two ways:
If you use the Keras
Model.fitAPI, you can pass thetf.keras.callbacks.BackupAndRestorecallback to it.If you use a custom training loop (with
tf.GradientTape), you can arbitrarily save checkpoints using thetf.train.Checkpointandtf.train.CheckpointManagerAPIs.
Both of these methods will back up and restore the training states in checkpoint files.
Setup
Install tf-nightly, as the frequency of checkpoint saving at a particular step with the save_freq argument in tf.keras.callbacks.BackupAndRestore is introduced from TensorFlow 2.10:
TensorFlow 1: Save checkpoints with tf.estimator.RunConfig
In TensorFlow 1, you can configure a tf.estimator to save checkpoints every step by configuring tf.estimator.RunConfig.
In this example, start by writing a hook that artificially throws an error during the fifth checkpoint:
Next, configure tf.estimator.Estimator to save every checkpoint and use the MNIST dataset:
Begin training the model. An artificial exception will be raised by the hook you defined earlier.
Rebuild the tf.estimator.Estimator using the last saved checkpoint and continue training:
TensorFlow 2: Back up and restore with a callback and Model.fit
In TensorFlow 2, if you use the Keras Model.fit API for training, you can provide the tf.keras.callbacks.BackupAndRestore callback to add the fault tolerance functionality.
To help demonstrate this, first start by defining a Keras Callback class that artificially throws an error during the fourth epoch checkpoint:
Then, define and instantiate a simple Keras model, define the loss function, call Model.compile, and set up a tf.keras.callbacks.BackupAndRestore callback that will save the checkpoints in a temporary directory at epoch boundaries:
Start training the model with Model.fit. During training, checkpoints will be saved thanks to tf.keras.callbacks.BackupAndRestore instantiated above, while the InterruptAtEpoch class will raise an artificial exception to simulate a failure after the fourth epoch.
Next, instantiate the Keras model, call Model.compile, and continue training the model with Model.fit from a previously saved checkpoint:
Define another Callback class that artificially throws an error during the 140th step:
Note: This section uses features that are only available in tf-nightly until Tensorflow 2.10 is released.
To make sure the checkpoints are saved every 30 steps, set the save_freq in the BackupAndRestore callback to 30. The InterruptAtStep will raise an artificial exception to simulate a failure at epoch 1 and step 40 (total step count 140). The checkpoint would be last saved at epoch 1 and step 20.
Next, instantiate the Keras model, call Model.compile, and continue training the model with Model.fit from a previously saved checkpoint. Notice that the training starts from epoch 2 and step 21.
TensorFlow 2: Write manual checkpoints with a custom training loop
If you use a custom training loop in TensorFlow 2, you can implement a fault tolerance mechanism with the tf.train.Checkpoint and tf.train.CheckpointManager APIs.
This example demonstrates how to:
Use a
tf.train.Checkpointobject to manually create a checkpoint, where the trackable objects you want to save are set as attributes.Use a
tf.train.CheckpointManagerto manage multiple checkpoints.
Start by defining and instantiating the Keras model, the optimizer, and the loss function. Then, create a Checkpoint that manages two objects with trackable states (the model and the optimizer), as well as a CheckpointManager for logging and keeping several checkpoints in a temporary directory.
Now, implement a custom training loop where after the first epoch every time a new epoch starts the last checkpoint is loaded:
Next steps
To learn more about fault tolerance and checkpointing in TensorFlow 2, consider the following documentation:
The
tf.keras.callbacks.BackupAndRestorecallback API docs.The
tf.train.Checkpointandtf.train.CheckpointManagerAPI docs.The Training checkpoints guide, including the Writing checkpoints section.
You may also find the following material related to distributed training useful:
The Fault tolerance section in the Multi-worker training with Keras tutorial.
The Handing task failure section in the Parameter server training tutorial.
View on TensorFlow.org
Run in Google Colab
View source on GitHub
Download notebook