ray.train.Checkpoint#

class ray.train.Checkpoint(path: str | PathLike, filesystem: pyarrow.fs.FileSystem | None = None)#

A reference to data persisted as a directory in local or remote storage.

Access the checkpoint contents locally using checkpoint.to_directory() or checkpoint.as_directory.

path#
Type:

A path on the filesystem containing the checkpoint contents.

filesystem#
Type:

PyArrow FileSystem that can be used to access data at the path.

See also

ray.train.report

Report a checkpoint during training (with Ray Train/Tune).

ray.train.get_checkpoint

Get the latest checkpoint during training (for restoration).

Saving and Loading Checkpoints, Configuring Persistent Storage

Examples

Creating a checkpoint using Checkpoint.from_directory:

>>> from ray.train import Checkpoint
>>> checkpoint = Checkpoint.from_directory("/tmp/example_checkpoint_dir")
>>> checkpoint.filesystem  
<pyarrow._fs.LocalFileSystem object...
>>> checkpoint.path
'/tmp/example_checkpoint_dir'

Creating a checkpoint from a remote URI:

>>> checkpoint = Checkpoint("s3://bucket/path/to/checkpoint")
>>> checkpoint.filesystem  
<pyarrow._s3fs.S3FileSystem object...
>>> checkpoint.path
'bucket/path/to/checkpoint'

Creating a checkpoint with a custom filesystem:

>>> checkpoint = Checkpoint(
...     path="bucket/path/to/checkpoint",
...     filesystem=pyarrow.fs.S3FileSystem(),
... )
>>> checkpoint.filesystem  
<pyarrow._s3fs.S3FileSystem object...
>>> checkpoint.path
'bucket/path/to/checkpoint'

Accessing a checkpoint’s contents:

>>> import os  
>>> with checkpoint.as_directory() as local_checkpoint_dir:  
...    print(os.listdir(local_checkpoint_dir))  
['model.pt', 'optimizer.pt', 'misc.pt']

PublicAPI (beta): This API is in beta and may change before becoming stable.

Methods

__init__

Construct a Checkpoint.

as_directory

Returns checkpoint contents in a local directory as a context.

from_directory

Create checkpoint object from a local directory.

get_metadata

Return the metadata dict stored with the checkpoint.

set_metadata

Set the metadata stored with this checkpoint.

to_directory

Write checkpoint data to a local directory.

update_metadata

Update the metadata stored with this checkpoint.