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

Torch Object Storage

Copy page
Download PDF
Best Practices
Accessing and storing TOS data in PyTorch jobs
Copy page
Download PDF
Accessing and storing TOS data in PyTorch jobs
This topic explains how to access and store TOS data in PyTorch jobs.​
Prerequisites​
  1. Before you begin using TOS, make sure that you have created a Volcano Engine account and completed real name verification.​
  1. Make sure you have activated TOS and created a TOS bucket. For instructions, see Quick start.​
  1. Your operating system is Linux x86-64.​
  1. You have installed Python 3.8 or a later version.​
warning​
Versions 3.12 and higher are not recommended, as they are not supported by PyTorch.​
Environment​
Before connecting to TOS, install PyTorch in a Python 3.8+ environment. For more information, see Pytorch.​
​
# Install torch dependencies​
pip3 install torch torchvision​
​
# Install torch lighting​
pip3 install lightning​
​
# Install torchdata​
pip3 install torchdata==0.9.0​
​
Use the example below to verify that PyTorch was installed successfully.​
​
import torch​
import torchvision​
​
# Save the checkpoint as a local file​
if __name__ == '__main__':​
model = torchvision.models.resnet18()​
​
with open('test.pt', 'wb') as writer:​
torch.save(model.state_dict(), writer)​
​
​
with open('test.pt', 'rb') as reader:​
stat_dict = torch.load(reader)​
​
model.load_state_dict(stat_dict)​
print(model)​
​
Integration methods​
​
Integration method​
Description​
Using fsspec​
You can encapsulate TOS as Python's standard read/write interfaces using tosfs, which implements the fsspec specification. Then, use the torchdata library to read from and write to the local path. For more information, see tosfs.​
Using FSX​
  • FSX supports mounting TOS buckets as a local file system on Linux. This approach is ideal for scenarios requiring file system semantics to access bucket data, such as when training model data in AI/machine learning applications that depend on file system operations.​
  • For information on the operating environment and any restrictions, see FSX Overview.​
Using S3 Connector​
  • S3 Connector, provided by Amazon Web Services (AWS), is a connector for PyTorch. It enhances the capabilities of torchdata for accessing S3 and supports map-style and iterable-style datasets, as well as checkpointing interfaces. For more information, see S3-connector-for-pytorch.​
  • Currently, only FNS buckets are supported. For more information, see Bucket type.​
Using tosutil​
You can use tosutil to mount a TOS bucket as a local file system directory. Then, use the torchdata library to read from and write to the local path.​
​
Steps​
The steps for different integration methods are as follows.​
​
fsspec
FSX
S3 Connector
tosutil
​
  1. Run this command to install tosfs.​
  • ​
    # Install tosfs for fsspec​
    pip install tosfs​
    ​
  1. Run this command to initialize the client.​
  • ​
    from tosfs.core import TosFileSystem​
    ​
    # Configure access credentials​
    if __name__ == '__main__':​
    tosfs = TosFileSystem(​
    key='<Your Volces AK>',​
    secret='<Your Volces SK>',​
    endpoint_url='http://tos-cn-beijing.volces.com',​
    region='cn-beijing',​
    )​
    ​
  1. Run this code to construct an iterable-style dataset.​
tip​
In this example, we use the bucketname bucket in the Beijing region. Modify the code according to your workloads.​
  • ​
    from torchdata.datapipes.iter import FSSpecFileLister, FSSpecFileOpener​
    ​
    if __name__ == '__main__':​
    kwargs = {​
    'key' : '<Your Volces AK>',​
    'secret' : '<Your Volces SK>',​
    'endpoint_url' : 'https://tos-cn-beijing.volces.com',​
    'region' :'cn-beijing'​
    }​
    ​
    bucket = 'bucketname'​
    print('test iterable-style datasets')​
    file_lister = FSSpecFileLister(root='tos://'+ bucket+'/shard-data/', **kwargs)​
    iterable_dataset = FSSpecFileOpener(file_lister, mode='rb', **kwargs)​
    # Iterate and read objects in the dataset​
    for _, item in iterable_dataset:​
    print(item.bucket, item.key)​
    data = item.read()​
    print(len(data))​
    ​
  1. Run this code to save or load the checkpoint.​
tip​
In this example, we use the bucketname bucket in the Beijing region. Modify the code according to your workloads.​
  • ​
    import torch​
    import torchvision​
    from tosfs.core import TosFileSystem​
    ​
    if __name__ == '__main__':​
    tosfs = TosFileSystem(​
    key='<Your Volces AK>',​
    secret='<Your Volces SK>',​
    endpoint_url='http://tos-cn-beijing.volces.com',​
    region='cn-beijing',​
    )​
    ​
    bucket = 'bucketname'​
    print('test checkpointing')​
    model = torchvision.models.resnet18()​
    ​
    # Encapsulate TOS as Python's standard read/write interfaces​
    with tosfs.open(bucket+ '/checkpoint/epoch1.ckpt', 'wb') as writer:​
    torch.save(model.state_dict(), writer)​
    ​
    with tosfs.open(bucket+'/checkpoint/epoch1.ckpt', 'rb') as reader:​
    stat_dict = torch.load(reader)​
    ​
    model.load_state_dict(stat_dict)​
    print(model)​
    ​
​
​
​
Last updated: 2026.08.25 10:39:45