Reading and Writing Checkpoints
Updated at:2025-11-03
Bostorchconnector allows direct reading and writing of checkpoints stored in BOS.
Taking the writing and loading of checkpoints for the results of resnet in a certain epoch as an example:
Bash
1from bostorchconnector import BosCheckpoint
2import torchvision
3import torch
4# Fill in <BUCKET>, <KEY> and the corresponding endpoint
5CHECKPOINT_URI="bos://<BUCKET>/<KEY>/"
6ENDPOINT="http://bj.bcebos.com"
7config = BosClientConfig(log_level=1)
8checkpoint = BosCheckpoint(endpoint=ENDPOINT, bos_client_config=config)
9model = torchvision.models.resnet18()
10# Save the checkpoint to Bos
11with checkpoint.writer(CHECKPOINT_URI + "epoch0.ckpt") as writer:
12 torch.save(model.state_dict(), writer)
13# Read the checkpoint from Bos
14with checkpoint.reader(CHECKPOINT_URI + "epoch0.ckpt") as reader:
15 state_dict = torch.load(reader)
16model.load_state_dict(state_dict)
