With Hugging Face Datasets, should a 300 GB text corpus on a 32 GB-RAM machine load memory-mapped or with streaming=True, and what does each give up?
answer
- RAM is not the limit
- Arrow on disk, paged in on access
- disk for source plus Arrow copy
- no len, no ds[i] when streaming
- to_iterable_dataset as the middle path
basics
~20 sRAM is not the limit: a Dataset is memory-mapped Arrow on disk, needing disk for downloads plus the Arrow copy but giving len, row access and exact shuffle. streaming=True writes nothing, iterating lazily with approximate shuffle and no map cache.
solid answer
~50 sA non-streaming `load_dataset` converts the corpus to Arrow in the Datasets cache and **memory-maps** it, so 32 GB of RAM is fine; the real cost is **disk**: the downloaded source files in the Hub cache, an Arrow copy that is uncompressed, and another Arrow file for every `map`. In return you get `len()`, `ds[i]`, exact `shuffle`, and `map(num_proc=...)` whose results are cached for the next run. `streaming=True` returns an `IterableDataset`: no download step, nothing written, training starts at once, but there is no length or row indexing, `shuffle` is a buffer, `map`/`filter` re-run lazily on every pass, and `num_proc` at load time raises `NotImplementedError`. With the disk to spare and several epochs ahead, prepare once and read the memory-mapped table, or turn it into a sharded stream with `to_iterable_dataset(num_shards=...)`; without the disk, or for one pass, stream.
go deeper
Recall that a regular Dataset is Arrow on disk and memory-mapped, and that streaming=True returns an IterableDataset you can only iterate.
Explain what each side gives up: disk and preparation time for the table; length, row access, exact shuffle and cached map for the stream.
Decide from disk budget, number of passes and needed operations, and know the middle path of preparing once and calling to_iterable_dataset with many shards.
Weigh where preprocessing cost should live for a team: a prepared, cached or published tokenized dataset reused by many runs, or a streaming pipeline that keeps storage flat.
## The misconception to clear first Most candidates hear "300 GB of data, 32 GB of RAM" and conclude the data cannot be loaded. In the Hugging Face **Datasets** library that is wrong. A regular `Dataset` is an **Apache Arrow** table stored in files on disk and **memory-mapped**: the operating system pages the bytes you touch into memory and drops them again under pressure. The library's own documentation shows a full Wikipedia split using about 50 MB of process memory. So the choice between a memory-mapped `Dataset` and a streaming `IterableDataset` is a choice about **disk, preparation time and what operations you need**, not about RAM. ## What the memory-mapped Dataset costs and buys A non-streaming `load_dataset("text", data_files=..., split="train")` (or a Hub repo id) runs a **prepare** step before you see a single row: - **Disk, part one**: remote source files are downloaded into the Hub cache (`~/.cache/huggingface/hub` by default). - **Disk, part two**: they are converted into typed Arrow files in the Datasets cache (`HF_DATASETS_CACHE`, `~/.cache/huggingface/datasets` by default). Arrow is **uncompressed**, so a compressed JSON Lines or Parquet source can grow on conversion. - **Disk, part three**: every `map` writes a **new Arrow cache file** holding its output, so a tokenization pass adds another large file. - **Time**: the download and conversion happen up front; `num_proc=` on `load_dataset` parallelises the preparation. What you get back is everything a table offers: `len(ds)`, random access with `ds[i]`, slicing, **exact** `shuffle` through an indices mapping, `train_test_split`, and `map`/`filter` with `num_proc` workers whose results are cached and reused on the next run. `keep_in_memory` stays off by default, so nothing is copied into RAM unless you ask for it. ## What streaming=True costs and buys `load_dataset(..., streaming=True)` returns an **`IterableDataset`**. Nothing is downloaded up front and nothing is written to disk; rows are read while you iterate, from the Hub or straight from local files. - **No length and no row index**: you iterate with a `for` loop; reaching the last row means reading every row before it. Integer indexing is not supported (column indexing such as `ds["text"]` is). - **Approximate shuffle**: `shuffle(seed=..., buffer_size=...)` samples from a buffer (1,000 rows by default) and shuffles the shard order, instead of permuting the whole table. - **Lazy transforms**: `map` and `filter` are recorded and applied on the fly, on **every** pass; there is no cache file, and `IterableDataset.map` has no `num_proc`. - **No parallel preparation**: `streaming=True` together with `num_proc` raises `NotImplementedError`, whose message points to the data-loading side for parallelism. - **Resumable iteration**: `state_dict()` and `load_state_dict()` record the shard and offset, so a stopped job can continue, although shuffle-buffer contents are lost on resume. ## Side by side | Need | Memory-mapped `Dataset` | Streaming `IterableDataset` | |---|---|---| | `len()` and `ds[i]` | yes | no | | Shuffle | exact, via indices mapping | buffer plus shard order | | `map` result reused next run | yes, cache file | no, recomputed lazily | | Disk needed | source files plus Arrow copies | none beyond what you save | | Time before the first row | download and conversion | almost none | | Preparation workers | `num_proc` on load and `map` | not supported | ## Choosing for the 300 GB case 1. **Check the disk, not the RAM.** If the machine cannot hold the source files, the Arrow copy and a tokenized copy, stream. 2. **Count the passes.** Several epochs and repeated experiments favour paying the preparation once and reusing the cached, tokenized table; a single pass or a quick look at the data favours streaming. 3. **Check the operations you need.** Exact shuffle, length-based schedules, sampling arbitrary rows or `train_test_split` need the table. 4. **Consider the middle path.** After preparing a `Dataset` once, `to_iterable_dataset(num_shards=64)` gives a sharded stream over the local Arrow files with sequential reads and a fast approximate shuffle (run `flatten_indices()` first if an exact shuffle left an indices mapping, or reads stay slow). The documentation notes it is faster than `streaming=True` over the same data because the files are already local. ## Related traps - An exact `shuffle` on a memory-mapped table leaves an indices mapping that can make reads several times slower; `flatten_indices()` rewrites contiguous data, at the price of another full copy. - Streaming moves the cost to the input pipeline: tokenization now runs during training, so it must keep up with the GPU. Spreading a stream across loader processes is the data-loading side's job; it parallelises by shard.
- You streamed the corpus, but the tokenizing map now slows every epoch. What are your options in Hugging Face Datasets?A streamed `map` is re-run on every pass because there is no cache file. Either prepare once (load non-streaming, `map(batched=True, num_proc=...)`, keep the cached Arrow file) and iterate that, or tokenize once and `push_to_hub` or `save_to_disk` the result so later runs stream already-tokenized rows.
- Why not just set keep_in_memory=True on the 32 GB machine to make the memory-mapped Dataset faster?`keep_in_memory=True` copies the Arrow table into RAM. For a 300 GB table that cannot fit, so the process would be killed or swap heavily. Memory-mapping exists precisely so that only the pages you touch occupy memory; leave `keep_in_memory` at its default here.
- How do you stop a streaming training job mid-epoch and resume it with Hugging Face Datasets?Save `ds.state_dict()` alongside the model checkpoint and call `load_state_dict()` before iterating again. The stream skips finished shards and fast-forwards inside the current one. Rows that were sitting in a shuffle buffer are not restored; the buffer refills with new data.
- A 300 GB dataset needs 300 GB of RAM unless you stream it
- keep_in_memory=True is the safe default for large datasets
- A streaming IterableDataset supports len() and ds[i] like a Dataset
- Streaming caches each map result so later epochs reuse it
- Passing num_proc with streaming=True parallelises the download
- Streaming shuffle permutes the whole corpus exactly