You need to enable JavaScript to run this app.
Torch Object Storage

Torch Object Storage

Copy page
Download PDF
Using TOS PyTorch Connector
TosCheckpoint
Copy page
Download PDF
TosCheckpoint
This topic explains how to read and write checkpoints directly from TOS using TosCheckpoint.​
Prerequisites​
You have installed and configured TOS PyTorch Connector. For instructions, see Installing TOS PyTorch Connector and Configuring TOS PyTorch Connector.​
TosObjectWriter​
The TosObjectWriter() method of TosCheckpoint allows you to write data to checkpoints.​
Parameters​
Before using it, you must configure the parameters for TosCheckpoint. For more information, see Passing credentials.​
Code example​
The following example demonstrates how to write checkpoints using TosCheckpoint in PyTorch. Replace the parameters, such as region, endpoint, AK, and SK, with the values you need.​
​
import torch​
import torchvision​
from tostorchconnector import TosCheckpoint, CredentialProvider​
​
checkpoint_uri = 'tos://bucket/key'​
region = 'cn-beijing'​
endpoint = 'http://tos-cn-beijing.ivolces.com'​
ak = '<your-access-key>'​
sk = '<your-secret-key>'​
​
with TosCheckpoint(region, endpoint, cred=CredentialProvider(ak, sk)) as checkpoint:​
model = torchvision.models.resnet18()​
with checkpoint.writer(checkpoint_uri) as writer:​
torch.save(model, writer)​
​
TosObjectReader​
The TosObjectReader() method of TosCheckpoint allows you to read checkpoints from TOS.​
Parameters​
Before using it, you must configure the parameters for TosCheckpoint. For more information, see Passing credentials.​
Code example​
The following example demonstrates how to read checkpoints from TOS using TosCheckpoint in PyTorch. Replace the parameters, such as region, endpoint, AK, and SK, with the values you need.​
​
import torch​
from tostorchconnector import TosCheckpoint, CredentialProvider​
​
checkpoint_uri = 'tos://bucket/key'​
region = 'cn-beijing'​
endpoint = 'http://tos-cn-beijing.ivolces.com'​
ak = '<your-access-key>'​
sk = '<your-secret-key>'​
​
with TosCheckpoint(region, endpoint, cred=CredentialProvider(ak, sk)) as checkpoint:​
with checkpoint.reader(checkpoint_uri) as reader:​
model = torch.load(reader)​
print(model)​
​
Last updated: 2026.03.23 11:37:42