Skip to content

Add Dataset.from_generator to materialize a stream or a Julia iterable - #111

Merged
CarloLucibello merged 3 commits into
mainfrom
cl/from-generator
Oct 20, 2026
Merged

CarloLucibello merged 3 commits into
mainfrom
cl/from-generator

Conversation

@CarloLucibello

Copy link
Copy Markdown
Collaborator

Closes #109.

Adds from_generator(source), also reachable as Dataset.from_generator and Dataset(stream). It writes the examples to a cached, memory-mapped Dataset in the "julia" format. source can be:

  • an IterableDataset, with its Julia map/filter steps, iterated in-process (replaces the ITER hack). The stream's features and split carry over.
  • a Julia iterable of Dicts/NamedTuples, or a function returning one. With batched = true it yields batches of columns, which are split into rows in Python.
  • a Python generator function, passed through unchanged.

How it behaves:

  • Caching: a Julia source is cached under its content fingerprint, as for map. A stream can't be fingerprinted, so without fingerprint it is rebuilt on every call, in datasets' temporary directory, so the HF cache doesn't fill up with unreusable copies.
  • Cache key: the fingerprint is carried by the generator's pickle (JuliaFunction), so features and split stay in the cache key. datasets' own fingerprint keyword would ignore them.
  • Errors: an error raised in the generator is rethrown instead of datasets' uninformative DatasetGenerationError.
  • num_proc > 1: throws an ArgumentError.

Also raises the floor to datasets >= 5.1 and removes the _consolidate workaround for older versions.

Tested on TinyStories validation (stream + batched Julia map + fingerprint): the rows are identical to the non-streaming map, and a new session reuses the cache.

🤖 Generated with Claude Code

CarloLucibello and others added 3 commits October 20, 2026 06:10
#109)

`from_generator(source)` (also `Dataset.from_generator` and `Dataset(stream)`) writes the
examples of an `IterableDataset`, a Julia iterable (or a function returning one, optionally
yielding batches of columns with `batched = true`), or a Python generator function to a
cached, memory-mapped `Dataset` in the "julia" format.

A Julia source is cached under its content fingerprint, as for `map`; a stream is rebuilt on
every call, in a temporary directory, unless given a `fingerprint`. The fingerprint is carried
by the generator's pickle, so `features` and `split` stay in the cache key. An error in the
generator is rethrown instead of `datasets`' uninformative `DatasetGenerationError`, and
`num_proc > 1` throws an `ArgumentError`.

Require `datasets >= 5.1` and drop the `_consolidate` workaround for older versions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Write `ds.map`/`ds.filter` (and `dd.`, `stream.`, `ds.shuffle`, ...) instead of a bare `map`
or `filter`, which reads as Base's function. Examples use the property calls, docstring headers
list `ds.map(f; kws...)` before the `map(f, ds::Dataset)` method, and Python-side names are
written `datasets.Dataset.map`. Also drop the obsolete `datasets` < 4.8.3 note from the
`RaggedVector` docstring.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`map` and `filter` are methods of `Base.map`/`Base.filter` on the package's types, so the
unqualified `map(f, ds)` is unambiguous and idiomatic Julia. Keeps dropping the obsolete
`datasets` < 4.8.3 note from the `RaggedVector` docstring.

This reverts commit 5243e07.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@CarloLucibello
CarloLucibello merged commit 80c5981 into main Oct 20, 2026
6 checks passed
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.

Dataset.from_generator: materialize a stream (or a Julia iterable) into a cached Dataset

1 participant