Repository navigation
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
Open
copybara-service[bot] wants to merge 1 commit into
copybara-service[bot] wants to merge 1 commit into
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Raise an error when a plain dataset built on a PyTorch dataset uses plain Grain defaults.
A
grain.MapDatasetorgrain.IterDatasetclass that is called on agrain.experimental.torchdataset 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 ignoredworker_intra_op_threadsand did not pass tensors through Grain's shared memory.random_map_with_keypassed a JAX PRNG key to a plain callable.These now raise
TypeError. The message names the lost default and the fix:from_dataset,pipe, orgrain.experimental.torch.mp_prefetch. What does not use a default still works:batchwith abatch_fn,random_map_with_keywith aRandomMapWithKey, andmp_prefetchwithnum_workers=0, which starts no worker process. Plain Grain datasets can still be built ongrain.experimental.torchdatasets.BatchIterDataset,RandomMapWithKeyMapDataset,RandomMapWithKeyIterDatasetandProcessPrefetchIterDatasethave their parents validatebatch,random_map_with_keyandmp_prefetch, asBatchMapDatasetdoes forbatch. No plain Grain dataset rejects these.InterleaveIterDatasetforwards a request to the datasets that it interleaves, if they are given as a list or tuple. They are not its parents. It acceptselastic_iteratorfor them, soElasticIteratorstill does not look at them.grain.experimental.torchdatasets override_validate_parentsto reject the plain Grainbatch,random_map_with_keyandmp_prefetchoperations. A dataset class that is constructed directly, such asBatchIterDataset(ds, ...), is rejected like the dataset method.grain.experimental.torchputs under its ownmp_prefetchaccepts that operation for its parents, sograin.experimental.torch.mp_prefetchworks on a plain Grain dataset that is built on agrain.experimental.torchdataset.MapDatasetwhose elements aregrain.experimental.torchdatasets is not detected whenInterleaveIterDatasetinterleaves it, because the elements may only be created when they are read.📚 Documentation preview 📚: https://google-grain--1491.org.readthedocs.build/