Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

Commit

Permalink
fix len calculation for new data loader (#4618)
Browse files Browse the repository at this point in the history
* fix len calculation for new data loader

* add test

Co-authored-by: Dirk Groeneveld <dirkg@allenai.org>
  • Loading branch information
epwalsh and dirkgr committed Sep 3, 2020
1 parent 8746361 commit d7124d4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
6 changes: 3 additions & 3 deletions allennlp/data/data_loaders/multi_process_data_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,9 @@ def __init__(
deque(self.iter_instances(), maxlen=0)

def __len__(self) -> int:
if self.max_instances_in_memory is None:
if self.batches_per_epoch is not None:
return self.batches_per_epoch
elif self.max_instances_in_memory is None:
# We haven't read the instances yet, so we do so now, caching them as we go.
if not self._instances:
deque(self.iter_instances(), maxlen=0)
Expand All @@ -218,8 +220,6 @@ def __len__(self) -> int:
return num_instances // batch_size
else:
return 1 + num_instances // batch_size
elif self.batches_per_epoch is not None:
return self.batches_per_epoch
else:
# We can't know the number of batches for a lazy loader when batches_per_epoch
# is not specified.
Expand Down
11 changes: 11 additions & 0 deletions tests/data/data_loaders/multi_process_data_loader_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,14 @@ def test_drop_last():
for batch in batches:
assert len(batch["index"]) == 16
assert len(batches) == 6


def test_batches_per_epoch():
loader = MultiProcessDataLoader(
MockDatasetReader(), "some path", batch_size=4, batches_per_epoch=10
)
vocab = Vocabulary.from_instances(loader.iter_instances())
loader.index_with(vocab)

assert len(loader) == 10
assert len(list(loader)) == 10

0 comments on commit d7124d4

Please sign in to comment.