Skip to content

Raise an error when a plain dataset built on a PyTorch dataset uses plain Grain defaults. - #1491

Open
copybara-service[bot] wants to merge 1 commit into
mainfrom
test_1028526143
Open

copybara-service[bot] wants to merge 1 commit into
mainfrom
test_1028526143

Conversation

@copybara-service

@copybara-service copybara-service Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Raise an error when a plain dataset built on a PyTorch dataset uses plain Grain defaults.

A grain.MapDataset or grain.IterDataset class that is called on a grain.experimental.torch dataset returns a plain Grain dataset. Its methods silently used the defaults of plain Grain:

  • InterleaveIterDataset([torch_ds, ...]).batch(...) returned NumPy arrays instead of tensors.
  • ThreadPrefetchIterDataset(torch_ds).mp_prefetch(...) did not limit the PyTorch threads of the workers. It ignored worker_intra_op_threads and did not pass tensors through Grain's shared memory.
  • random_map_with_key passed a JAX PRNG key to a plain callable.

These now raise TypeError. The message names the lost default and the fix: from_dataset, pipe, or grain.experimental.torch.mp_prefetch. What does not use a default still works: batch with a batch_fn, random_map_with_key with a RandomMapWithKey, and mp_prefetch with num_workers=0, which starts no worker process. Plain Grain datasets can still be built on grain.experimental.torch datasets.

  • BatchIterDataset, RandomMapWithKeyMapDataset, RandomMapWithKeyIterDataset and ProcessPrefetchIterDataset have their parents validate batch, random_map_with_key and mp_prefetch, as BatchMapDataset does for batch. No plain Grain dataset rejects these.
  • InterleaveIterDataset forwards a request to the datasets that it interleaves, if they are given as a list or tuple. They are not its parents. It accepts elastic_iterator for them, so ElasticIterator still does not look at them.
  • The grain.experimental.torch datasets override _validate_parents to reject the plain Grain batch, random_map_with_key and mp_prefetch operations. A dataset class that is constructed directly, such as BatchIterDataset(ds, ...), is rejected like the dataset method.
  • The dataset that grain.experimental.torch puts under its own mp_prefetch accepts that operation for its parents, so grain.experimental.torch.mp_prefetch works on a plain Grain dataset that is built on a grain.experimental.torch dataset.
  • A plain MapDataset whose elements are grain.experimental.torch datasets is not detected when InterleaveIterDataset interleaves it, because the elements may only be created when they are read.

📚 Documentation preview 📚: https://google-grain--1491.org.readthedocs.build/

…lain Grain defaults.

A `grain.MapDataset` or `grain.IterDataset` class that is called on a `grain.experimental.torch` dataset returns a plain Grain dataset. Its methods silently used the defaults of plain Grain:

- `InterleaveIterDataset([torch_ds, ...]).batch(...)` returned NumPy arrays instead of tensors.
- `ThreadPrefetchIterDataset(torch_ds).mp_prefetch(...)` did not limit the PyTorch threads of the workers. It ignored `worker_intra_op_threads` and did not pass tensors through Grain's shared memory.
- `random_map_with_key` passed a JAX PRNG key to a plain callable.

These now raise `TypeError`. The message names the lost default and the fix: `from_dataset`, `pipe`, or `grain.experimental.torch.mp_prefetch`. What does not use a default still works: `batch` with a `batch_fn`, `random_map_with_key` with a `RandomMapWithKey`, and `mp_prefetch` with `num_workers=0`, which starts no worker process. Plain Grain datasets can still be built on `grain.experimental.torch` datasets.

- `BatchIterDataset`, `RandomMapWithKeyMapDataset`, `RandomMapWithKeyIterDataset` and `ProcessPrefetchIterDataset` have their parents validate `batch`, `random_map_with_key` and `mp_prefetch`, as `BatchMapDataset` does for `batch`. No plain Grain dataset rejects these.
- `InterleaveIterDataset` forwards a request to the datasets that it interleaves, if they are given as a list or tuple. They are not its parents. It accepts `elastic_iterator` for them, so `ElasticIterator` still does not look at them.
- The `grain.experimental.torch` datasets override `_validate_parents` to reject the plain Grain `batch`, `random_map_with_key` and `mp_prefetch` operations. A dataset class that is constructed directly, such as `BatchIterDataset(ds, ...)`, is rejected like the dataset method.
- The dataset that `grain.experimental.torch` puts under its own `mp_prefetch` accepts that operation for its parents, so `grain.experimental.torch.mp_prefetch` works on a plain Grain dataset that is built on a `grain.experimental.torch` dataset.
- A plain `MapDataset` whose elements are `grain.experimental.torch` datasets is not detected when `InterleaveIterDataset` interleaves it, because the elements may only be created when they are read.

PiperOrigin-RevId: 1028526143

This branch has not been deployed

No deployments
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.

1 participant