Skip to content

Fix IterableDataset state_dict shard_example_idx counting #7539

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
32 changes: 28 additions & 4 deletions src/datasets/iterable_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,16 +317,40 @@ def __iter__(self):

def _iter_arrow(self):
shard_idx_start = self._state_dict["shard_idx"] if self._state_dict else 0
for gen_kwags in islice(_split_gen_kwargs(self.kwargs, max_num_jobs=self.num_shards), shard_idx_start, None):
kwargs_with_shuffled_shards = (
_shuffle_gen_kwargs(self.generator, self.kwargs) if hasattr(self, "generator") else self.kwargs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this expected ? I think ShuffledDataSourcesArrowExamplesIterable has its own _iter_arrow() implementation

)

for gen_kwags in islice(
_split_gen_kwargs(kwargs_with_shuffled_shards, max_num_jobs=self.num_shards), shard_idx_start, None
):
shard_example_idx_start = self._state_dict["shard_example_idx"] if self._state_dict else 0
shard_example_idx = 0

examples_seen_in_current_shard = 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how is it different from shard_example_idx ?


for key, pa_table in self.generate_tables_fn(**gen_kwags):
shard_example_idx += len(pa_table)
if shard_example_idx <= shard_example_idx_start:
batch_size = len(pa_table)

if shard_example_idx + batch_size <= shard_example_idx_start:
shard_example_idx += batch_size
continue

if shard_example_idx < shard_example_idx_start:
offset = shard_example_idx_start - shard_example_idx
pa_table = pa_table.slice(offset)
examples_seen_in_current_shard = offset
Comment on lines +339 to +342
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this needed ? we always yield full tables, so it's unlikely we end up with a shard_example_idx that doesn't land exactly on a table boundary (except if the dataset state is manually crafted maybe)


if self._state_dict:
self._state_dict["shard_example_idx"] += len(pa_table)
examples_in_current_batch = len(pa_table)
self._state_dict["shard_example_idx"] = (
shard_example_idx_start + examples_seen_in_current_shard + examples_in_current_batch
)

yield key, pa_table
shard_example_idx += batch_size
examples_seen_in_current_shard += len(pa_table)

if self._state_dict:
self._state_dict["shard_idx"] += 1
self._state_dict["shard_example_idx"] = 0
Expand Down
Loading