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

Fast dataset iter #5030

Merged
merged 4 commits into from Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
67 changes: 55 additions & 12 deletions src/datasets/arrow_dataset.py
Expand Up @@ -1854,17 +1854,62 @@ def __len__(self):
"""
return self.num_rows

def _iter_batches(self, batch_size: int, decoded: bool = True):
"""Iterate through the batches of size `batch_size`.

If a formatting is set with :meth:`Dataset.set_format` rows will be returned with the
selected format.
"""
if self._indices is None and config.PYARROW_VERSION.major >= 8:
# Fast iteration
mariosasko marked this conversation as resolved.
Show resolved Hide resolved
format_kwargs = self._format_kwargs if self._format_kwargs is not None else {}
formatter = get_formatter(self._format_type, features=self.features, decoded=decoded, **format_kwargs)
for batch in self.data.to_reader(max_chunksize=batch_size):
pa_subtable = pa.Table.from_batches([batch])
formatted_output = format_table(
pa_subtable,
range(pa_subtable.num_rows),
formatter=formatter,
format_columns=self._format_columns,
output_all_columns=self._output_all_columns,
)
yield formatted_output
else:
for i in range(0, self.num_rows, batch_size):
yield self._getitem(
slice(i, i + batch_size),
decoded=decoded,
)

def _iter(self, decoded: bool = True):
"""Iterate through the examples.

If a formatting is set with :meth:`Dataset.set_format` rows will be returned with the
selected format.
"""
for index in range(self.num_rows):
yield self._getitem(
index,
decoded=decoded,
)
if self._indices is None and config.PYARROW_VERSION.major >= 8:
# Fast iteration
mariosasko marked this conversation as resolved.
Show resolved Hide resolved
format_kwargs = self._format_kwargs if self._format_kwargs is not None else {}
formatter = get_formatter(self._format_type, features=self.features, decoded=decoded, **format_kwargs)
batch_size = config.DEFAULT_ITER_BATCH_SIZE
for batch in self.data.to_reader(max_chunksize=batch_size):
for i in range(batch.num_rows):
batch_ex = batch.slice(i, 1)
pa_subtable = pa.Table.from_batches([batch_ex])
formatted_output = format_table(
pa_subtable,
0,
formatter=formatter,
format_columns=self._format_columns,
output_all_columns=self._output_all_columns,
)
yield formatted_output
else:
for i in range(self.num_rows):
yield self._getitem(
i,
decoded=decoded,
)

def __iter__(self):
"""Iterate through the examples.
Expand Down Expand Up @@ -2805,14 +2850,16 @@ def init_buffer_and_writer():

# Loop over single examples or batches and write to buffer/file if examples are to be updated
if not batched:
pbar_iterable = input_dataset._iter(decoded=False)
pbar_total = len(input_dataset)
pbar_iterable = input_dataset._iter(decoded=False)
else:
num_rows = (
len(input_dataset) if not drop_last_batch else len(input_dataset) // batch_size * batch_size
)
pbar_iterable = range(0, num_rows, batch_size)
pbar_total = (num_rows // batch_size) + 1 if num_rows % batch_size else num_rows // batch_size
pbar_iterable = itertools.islice(
input_dataset._iter_batches(batch_size, decoded=False), pbar_total
)
lhoestq marked this conversation as resolved.
Show resolved Hide resolved
pbar_unit = "ex" if not batched else "ba"
pbar_desc = (desc + " " if desc is not None else "") + "#" + str(rank) if rank is not None else desc
pbar = logging.tqdm(
Expand All @@ -2835,11 +2882,7 @@ def init_buffer_and_writer():
else:
writer.write(example)
else:
for i in pbar:
batch = input_dataset._getitem(
slice(i, i + batch_size),
decoded=False,
)
for i, batch in enumerate(pbar):
indices = list(
range(*(slice(i, i + batch_size).indices(input_dataset.num_rows)))
) # Something simpler?
Expand Down
3 changes: 3 additions & 0 deletions src/datasets/config.py
Expand Up @@ -168,6 +168,9 @@
# https://github.com/apache/arrow/blob/master/docs/source/cpp/arrays.rst#size-limitations-and-recommendations)
DEFAULT_MAX_BATCH_SIZE = 10_000

# Number of examples prefetched in `Dataset.__iter__`
DEFAULT_ITER_BATCH_SIZE = 10
mariosasko marked this conversation as resolved.
Show resolved Hide resolved

# Pickling tables works only for small tables (<4GiB)
# For big tables, we write them on disk instead
MAX_TABLE_NBYTES_FOR_PICKLING = 4 << 30
Expand Down
24 changes: 24 additions & 0 deletions src/datasets/table.py
Expand Up @@ -330,6 +330,30 @@ def to_pandas(self, *args, **kwargs):
def to_string(self, *args, **kwargs):
return self.table.to_string(*args, **kwargs)

def to_reader(self, *args, **kwargs):
"""
Convert the Table to a RecordBatchReader.

Note that this method is zero-copy, it merely exposes the same data under a different API.

Args:
max_chunksize (:obj:`int`, defaults to :obj:`None`)
Maximum size for RecordBatch chunks. Individual chunks may be smaller depending
on the chunk layout of individual columns.

Returns:
:obj:`pyarrow.RecordBatchReader`

<Tip warning={true}>

pyarrow >= 8.0.0 needs to be installed to use this method.

</Tip>
"""
if config.PYARROW_VERSION.major < 8:
raise NotImplementedError("`pyarrow>=8.0.0` is required to use this method")
return self.table.to_reader(*args, **kwargs)

def field(self, *args, **kwargs):
"""
Select a schema field by its column name or numeric index.
Expand Down