Navigation
๐Ÿ’พ Data & Storageยท 10 min read

Streaming Datasets

Mosaic StreamingDataset, WebDataset, HuggingFace streaming โ€” reading from cloud storage without downloading entire datasets.

streamingmosaicmlwebdatasethuggingfaceclouds3

At pre-training scale, datasets are too large to fit on local disk. Streaming datasets read data directly from cloud storage (S3, GCS) โ€” no full download needed, with built-in shuffling and multi-node coordination.


1. Why Streaming

Streaming Dataset

ApproachLocal disk requiredStartup timeMulti-nodeCloud-native
Download then trainFull dataset (10-50 TB)Hours-daysEach node downloadsโŒ
StreamingCache only (100 GB)SecondsBuilt-in shardingโœ…

For a 15T token dataset at ~20 TB: downloading to each node wastes storage and delays training start. Streaming reads on-demand.


2. Mosaic StreamingDataset (MDS)

from streaming import StreamingDataset, StreamingDataLoader

# Data stored as MDS shards on S3
dataset = StreamingDataset(
    remote='s3://my-bucket/training-data/',
    local='/tmp/cache/',           # local cache for speed
    shuffle=True,
    shuffle_block_size=1 << 18,    # 256K samples per shuffle block
    batch_size=32,
)

loader = StreamingDataLoader(
    dataset, 
    batch_size=32,
    num_workers=8,
)

Key Features

FeatureHow it works
Shard-basedData split into fixed-size shards (64-256 MB each)
Deterministic shuffleSame seed โ†’ same order across restarts (reproducibility)
Multi-node coordinationEach node gets disjoint shards (no duplicate reads)
ResumableTrack which shards consumed โ€” resume after crash
Local cacheDownloaded shards cached locally; evicted when cache full

Writing MDS Format

from streaming import MDSWriter

with MDSWriter(
    out='s3://my-bucket/training-data/',
    columns={'tokens': 'ndarray:int32', 'labels': 'ndarray:int32'},
    compression='zstd',
    size_limit=256 * 1024 * 1024,  # 256 MB per shard
) as writer:
    for sample in tokenized_dataset:
        writer.write(sample)

3. WebDataset

Tar-based format โ€” each shard is a .tar file containing samples:

import webdataset as wds

dataset = wds.WebDataset(
    "s3://bucket/shards/shard-{000000..009999}.tar",
    shardshuffle=True,
).decode("pil").to_tuple("jpg", "cls")

loader = wds.WebLoader(dataset, batch_size=64, num_workers=8)

Best for: Vision datasets (image + label per sample). Each tar contains ~1000 samples. Simple format, works with standard tools.


4. HuggingFace Streaming

from datasets import load_dataset

dataset = load_dataset(
    "allenai/c4",
    split="train",
    streaming=True,  # don't download, stream!
)

for example in dataset:
    tokens = tokenizer(example["text"])
    # process...

Pros: Simplest API, huge hub of datasets. Cons: Less control over shuffling, no multi-node coordination (must implement manually), slower than MDS/WebDataset for large-scale training.


5. Comparison

FeatureMDS (Mosaic)WebDatasetHF Streaming
FormatCustom binary shardsTar archivesArrow/Parquet
Shuffle qualityExcellent (block shuffle)Good (shard shuffle)Limited
Multi-nodeBuilt-inManualManual
Resume/checkpointBuilt-inManualManual
Compressionzstd, snappygzip, nonegzip
Best forLLM pre-trainingVision trainingQuick experiments
ThroughputHighestHighModerate

For production pre-training: MDS or WebDataset. For research/prototyping: HF streaming.


6. Cloud Storage Bandwidth

StorageRead bandwidthLatencyCost
Local NVMe3-7 GB/s~10 ฮผsIncluded
S3 (single stream)100-200 MB/s~50 ms$0.09/GB
S3 (parallel, 100 streams)10-20 GB/s~50 msSame
GCSSimilar to S3~30 ms$0.12/GB

Key: Cloud storage has high throughput but high latency. Streaming datasets use prefetching + local caching to hide this latency.

For data loading at larger scale (thousands of workers), see Data Loading at Scale.


Self-Check

  • Compare download-then-train vs streaming for a 20 TB dataset
  • Set up Mosaic StreamingDataset with S3 and local cache
  • Explain shard-based streaming: how shuffling and multi-node work
  • Choose between MDS, WebDataset, and HF streaming for a given use case
  • Calculate S3 bandwidth with parallel streams

References