# How does Dataset.from\_generator store data bigger than RAM?

**URL:** <https://discuss.huggingface.co/t/how-does-dataset-from-generator-store-data-bigger-than-ram/159954>\
**Category:** 🤗Datasets\
**Created:** [June 19, 2025, 11:39am UTC](https://discuss.huggingface.co/t/how-does-dataset-from-generator-store-data-bigger-than-ram/159954 "2025-06-19T11:39:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jzju](https://avatars.discourse-cdn.com/v4/letter/j/ea666f/32.png) [@jzju](https://discuss.huggingface.co/u/jzju)\
**Post date:** [June 19, 2025, 11:39am UTC](https://discuss.huggingface.co/t/how-does-dataset-from-generator-store-data-bigger-than-ram/159954/1 "2025-06-19T11:39:40Z")

</div>

```auto
ids = Dataset.from_generator(gen)
ids.save_to_disk("ds")

```

I want to run code with results like the one above but save to disk when one shard is filled instead of keeping the whole generator of data in RAM. Is there a way to manually call flush shard every n iterations?

In the petabyte case the data won’t even fit on a disk and the generator and flush shoud read and write to a cloud bucket.

---

<div class="post-metadata">

**Author:** ![John6666](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/john6666/32/27664_2.png) [@John6666](https://discuss.huggingface.co/u/John6666)\
**Post date:** [June 19, 2025, 12:00pm UTC](https://discuss.huggingface.co/t/how-does-dataset-from-generator-store-data-bigger-than-ram/159954/2 "2025-06-19T12:00:46Z")

</div>

It seems like it could be done using the `writer_batch_size` parameter, but I’m not sure how to use it specifically…

> <https://github.com/huggingface/datasets/issues/6008>
>
> \### Describe the bug
> 
> Whenever I try to create a dataset which contains images u…sing \`Dataset.from\_generator\`, it freezes around 996 rows. I suppose it has something to do with memory consumption, but there's more memory available. I
> 
> Somehow it worked a few times but mostly this makes the datasets library much more cumbersome to work with because generators are the easiest way to turn an existing dataset into a Hugging Face dataset.
> 
> I've let it run in the frozen state for way longer than it can possibly take to load the actual dataset.
> 
> Let me know if you have ideas how to resolve it!
> 
> \### Steps to reproduce the bug
> 
> \`\`\`python
> from datasets import Dataset
> import numpy as np
> 
> def gen():
> for row in range(10000):
> yield {"i": np.random.rand(512, 512, 3)}
>         
> Dataset.from\_generator(gen)
> \# -\> 90% of the time gets stuck around 1000 rows
> \`\`\`
> 
> \### Expected behavior
> 
> Should continue and go through all the examples yielded by the generator, or at least throw an error or somehow communicate what's going on.
> 
> \### Environment info
> 
> \- \`datasets\` version: 2.8.0
> \- Platform: Linux-5.15.0-52-generic-x86\_64-with-glibc2.29
> \- Python version: 3.8.10
> \- PyArrow version: 12.0.1
> \- Pandas version: 1.5.1

> By default, we write data to disk (so it can be memory-mapped) every 1000 rows/samples. You can control this with the `writer_batch_size` parameter.

> <https://github.com/huggingface/datasets/issues/6422>
>
> \### Feature request
> 
> Add an argument in \`save\_to\_disk\` regarding batch size, whi…ch would be passed to \`shard\` and other methods.
> 
> \### Motivation
> 
> The \`Dataset.save\_to\_disk\` method currently calls \`shard\` without passing a \`writer\_batch\_size\` argument, thus implicitly using the default value (1000). This can result in RAM saturation when using a lot of processes on long text sequences or other modalities, or for specific IO configs.
> 
> \### Your contribution
> 
> I would be glad to submit a PR, as long as it does not imply extensive tests refactoring.

> <https://github.com/huggingface/datasets/issues/6279>
>
> \### Feature request
> 
> Hi, 
> 
> could you add an implementation of a batched \`Itera…bleDataset\`. It already support an option to do batch iteration via \`.iter(batch\_size=...)\` but this cannot be used in combination with a torch \`DataLoader\` since it just returns an iterator.
> 
> \### Motivation
> 
> The current implementation loads each element of a batch individually which can be very slow in cases of a big batch\_size. I did some experiments \[here\](https://discuss.huggingface.co/t/slow-dataloader-with-big-batch-size/57224) and using a batched iteration would speed up data loading significantly.
> 
> \### Your contribution
> 
> N/A
