Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warmup input pipeline in the background. #245

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions clu/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,17 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""Common utilities for loading data for JAX users.
"""DatasetIterator is an interface for input pipelines."""
# pylint: disable=g-multiple-import
# pylint: disable=unused-import

This will long term replace clu.deterministic_data.
"""

from clu.data.dataset_iterator import DatasetIterator
from clu.data.dataset_iterator import TfDatasetIterator
from clu.data.dataset_iterator import (
Array,
ArraySpec,
DatasetIterator,
Element,
ElementSpec,
TfDatasetIterator,
PeekableDatasetIterator,
PyTree,
)
32 changes: 32 additions & 0 deletions clu/data/dataset_iterator.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@

import abc
import collections.abc
import concurrent.futures
import dataclasses
import os
import typing
from typing import Any, Mapping, Optional, Sequence, Tuple, TypeVar, Union

from absl import logging
from clu import asynclib
from etils import epath
import jax.numpy as jnp # Just for type checking.
import numpy as np
Expand Down Expand Up @@ -227,8 +229,12 @@ class PeekableDatasetIterator(DatasetIterator):
def __init__(self, it: DatasetIterator):
self._it = it
self._peek: Optional[Element] = None
self._peek_future = None

def __next__(self) -> Element:
if self._peek_future:
self._peek_future.result()
self._peek_future = None
if self._peek is None:
return next(self._it)
peek = self._peek
Expand All @@ -243,10 +249,36 @@ def element_spec(self) -> ElementSpec:
return self._it.element_spec

def peek(self) -> Element:
"""Returns the next element without consuming it.

This will get the next element from the underlying iterator. The element
is stored and return on the next call of __next__().

Returns:
The next element.
"""
if self._peek is None:
self._peek = next(self)
return self._peek

def peek_async(self) -> concurrent.futures.Future[Element]:
"""Same as peek() but returns the Future of the element.

Users can call this to warm up the iterator.

Returns:
Future with the next element. The element is also kept and returned on the
next call of __next__().
"""
pool = asynclib.Pool()

@pool
def async_peek():
return self.peek()

self._peek_future = async_peek()
return self._peek_future

def save(self, filename: epath.Path):
self._it.save(filename)

Expand Down
8 changes: 8 additions & 0 deletions clu/data/dataset_iterator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ def test_peekable_dataset_iterator(self):
self.assertEqual(next(it), {INDEX: [0, 2], "prime": [2, 5]})
self.assertEqual(next(it), {INDEX: [4, 5], "prime": [11, 13]})

def test_peekable_dataset_iterator_async(self):
it = self._create_iterator(0)
it = dataset_iterator.PeekableDatasetIterator(it)
it.peek_async()
self.assertEqual(next(it), {INDEX: [0, 2], "prime": [2, 5]})
self.assertEqual(next(it), {INDEX: [0, 2], "prime": [2, 5]})
self.assertEqual(next(it), {INDEX: [4, 5], "prime": [11, 13]})


if __name__ == "__main__":
tf.test.main()