ml-explore / ml-explore/mlx-data

Set seed with prefetch for reproducibility

Open
#74 0 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
483
Forks
62
PR merge metrics
No merged PRs in 30d

Description

Currently, the only way to the seed is with mlx.data.core.set_state, but this only controls the seed for .shuffle(). When using .prefetch with num_threads > 1, the samples returned are not deterministic and therefore not reproducible.

Is there a way to set the seed when prefetching with more than one thread?

import mlx.data.core as dmx
from mlx.data.datasets import load_mnist

dmx.set_state(42)

train = load_mnist(root=None, train=True)
dset = (
    train.shuffle()
    .to_stream()
    .key_transform("image", lambda x: x.astype("float32") / 255)
    .batch(32)
    .prefetch(prefetch_size=4, num_threads=4) # non-deterministic with > 1 thread
)

for i, data in enumerate(dset):
    print(data["image"].sum())
    if i == 2:
        break

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing dmx.set_state and the prefetch(prefetch_size=4, num_threads=4) entry point shown in the Python example. Investigate how multiple prefetch threads affect sample ordering and seed state. Done means repeated runs with the same seed and more than one thread return the same samples and batch values.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
data-engineering
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.