Skip to content

Conversation

lhoestq
Copy link
Member

@lhoestq lhoestq commented Mar 13, 2025

Useful for dataset streaming for multimodal datasets, and especially for lerobot.
It speeds up streaming up to 20 times.

When decoding is enabled (default), media types are decoded:

  • audio -> dict of "array" and "sampling_rate" and "path"
  • image -> PIL.Image
  • video -> torchvision.io.VideoReader

You can enable multithreading using num_threads. This is especially useful to speed up remote
data streaming. However it can be slower than num_threads=0 for local data on fast disks.

PS: Disabling decoding is useful if you want to iterate on the paths or bytes of the media files
without actually decoding their content.

Example: Speed up streaming with multithreading:

>>> import os
>>> from datasets import load_dataset
>>> from tqdm import tqdm
>>> ds = load_dataset("sshh12/planet-textures", split="train", streaming=True)
>>> num_threads = min(32, (os.cpu_count() or 1) + 4)
>>> ds = ds.decode(num_threads=num_threads)
>>> for _ in tqdm(ds):  # 20 times faster !
...     ...

why not multiprocessing ? decoding is done with the GIL released in soundfile/PIL/torchvision so multiprocessing would just use more memory

TODO

  • test
  • add to docs

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@lhoestq lhoestq merged commit 7ad7379 into main Mar 14, 2025
13 of 15 checks passed
@lhoestq lhoestq deleted the add-iterable-dataset-faster-decode branch March 14, 2025 10:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants