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

Torch Object Storage

Copy page
Download PDF
Using TOS PyTorch Connector
TosMapDataset
Copy page
Download PDF
TosMapDataset
This topic explains how to construct a dataset using TosMapDataset.​
Prerequisites​
You have installed and configured TOS PyTorch Connector. For instructions, see Installing TOS PyTorch Connector and Configuring TOS PyTorch Connector.​
Constructing a dataset​
How to construct​
There are two ways to construct a dataset using TosMapDataset.​
  • TOS_URI prefix: This option is ideal for scenarios where TOS paths follow a consistent pattern.​
  • List of TOS URIs: This option is ideal for scenarios where TOS paths are specific but scattered.​
Constructing a dataset using a TOS_URI prefix​
The following example demonstrates how to construct a dataset using the from_prefix() method of TosMapDataset.​
​
class TosMapDataset(torch.utils.data.Dataset):​
# Construct a dataset using from_prefix​
@classmethod​
def from_prefix(cls, prefix: str, *, region: str, endpoint: Optional[str] = None,​
transform: Callable[[TosObjectReader], Any] = None,​
cred: Optional[CredentialProvider] = None,​
client_conf: Optional[TosClientConfig] = None,​
use_native_client: bool = True,​
reader_type: Optional[ReaderType] = None,​
buffer_size: Optional[int] = None,​
enable_crc: bool = True,​
prefetch_concurrency: int = 0):​
... ​
​
Constructing a dataset from a list of TOS URIs​
The following example demonstrates how to construct a dataset using the from_urls() method of TosMapDataset.​
​
class TosMapDataset(torch.utils.data.Dataset):​
# Construct a dataset using from_urls​
@classmethod​
def from_urls(cls, urls: Union[str, Iterator[str]], *, region: str, endpoint: Optional[str] = None,​
transform: Callable[[TosObjectReader], Any] = None,​
cred: Optional[CredentialProvider] = None,​
client_conf: Optional[TosClientConfig] = None,​
use_native_client: bool = True,​
reader_type: Optional[ReaderType] = None,​
buffer_size: Optional[int] = None,​
enable_crc: bool = True,​
prefetch_concurrency: int = 0)​
... ​
​
Supported parameters​
​
Parameter​
Required​
Type​
Example value​
Description​
urls​
Yes​
str | Iterator[str]​
['tos://bucket/key1', 'tos://bucket/key2']​
Format for a single URL: tos://{bucketname}/{objectname}.​
prefix​
Yes​
str​
tos://bucket/prefix​
Format: tos://{bucketname}/{prefix}.​
region​
Yes​
str​
cn-beijing​
TOS region ID.​
endpoint​
No​
str​
http://tos-cn-beijing.ivolces.com​
TOS endpoint.​
transform​
No​
Callable[[TosObjectReader], Any]​
​
def transform(item):​
data = item.read()​
return item.key, item.size​
​
Transformation function.​
cred​
No​
CredentialProvider​
CredentialProvider('ak', 'sk')​
Used to pass credentials.​
client_conf​
No​
TosClientConfig​
-​
Used to configure the connector.​
reader_type​
No​
ReaderType​
ReaderType.RANGED​
The reader type for TosObjectReader. Possible values:​
  • ReaderType.SEQUENTIAL: A reader that caches all data.​
  • ReaderType.RANGED: A reader that only caches data up to the block size.​
  • ReaderType.DIRECT: A reader without a cache.​
buffer_size​
No​
int​
8388608​
The size of a cache block. This parameter is only valid when ReaderType is ReaderType.RANGED.​
enable_crc​
No​
bool​
True​
Specify whether to check data integrity for uploads and sequential reads.​
prefetch_concurrency​
No​
int​
32​
The number of concurrent reads when preloading the dataset.​
​
Code example​
The following example demonstrates how to create a DataLoader in PyTorch. Replace the parameters, such as region, endpoint, AK, and SK, with the values you need.​
​
from tostorchconnector import CredentialProvider​
from tostorchconnector import TosMapDataset​
from torch.utils.data import DataLoader​
​
# Dataset path​
dataset_uri = 'tos://bucket/prefix'​
region = 'cn-beijing'​
# TOS endpoint​
endpoint = 'http://tos-cn-beijing.ivolces.com'​
ak = '<your-access-key>'​
sk = '<your-secret-key>'​
​
# Dataset batch size​
BATCH_SIZE = 256​
# Number of worker processes​
NUM_WORKERS = 32​
# Data transformation function​
def transform(item):​
data = item.read()​
return item.key, item.size​
​
# Build a dataset object from the specified dataset prefix using TosMapDataset.from_prefix​
with TosMapDataset.from_prefix(dataset_uri,​
region=region, endpoint=endpoint,​
transform=transform,​
cred=CredentialProvider(​
ak=ak,​
sk=sk)) as dataset:​
# Create a DataLoader based on the constructed dataset ​
train_loader = DataLoader(dataset=dataset, batch_size=BATCH_SIZE, num_workers=NUM_WORKERS, shuffle=True)​
# Iterate for training/reading using the DataLoader​
for data in train_loader:​
print(data) ​
​
​
Last updated: 2026.03.23 11:37:42