Skip to content
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
7 changes: 4 additions & 3 deletions src/lightning/pytorch/loops/fetchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from collections import deque
from collections.abc import Iterator
from typing import Any, Optional

Expand Down Expand Up @@ -98,7 +99,7 @@ def __init__(self, prefetch_batches: int = 1) -> None:
if prefetch_batches < 0:
raise ValueError("`prefetch_batches` should at least be 0.")
self.prefetch_batches = prefetch_batches
self.batches: list[Any] = []
self.batches: deque[Any] = deque()

@override
def __iter__(self) -> "_PrefetchDataFetcher":
Expand All @@ -122,7 +123,7 @@ def __next__(self) -> _ITERATOR_RETURN:
if self.batches:
# there are pre-fetched batches already from a previous `prefetching` call.
# consume one
batch = self.batches.pop(0)
batch = self.batches.popleft()
try:
# refill the consumed batch
self.batches.append(super().__next__())
Expand All @@ -140,7 +141,7 @@ def __next__(self) -> _ITERATOR_RETURN:
@override
def reset(self) -> None:
super().reset()
self.batches = []
self.batches.clear()


class _DataLoaderIterDataFetcher(_DataFetcher):
Expand Down
4 changes: 4 additions & 0 deletions tests/tests_pytorch/loops/test_fetchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ def generate():
# validate reset works properly.
assert generate() == expected
assert fetcher.fetched == 3
iter(fetcher)
assert len(fetcher.batches) == (0 if dataset_cls is SizedDataset else min(prefetch_batches, 3))
fetcher.reset()
assert not fetcher.batches


@pytest.mark.parametrize("multiple_iterables", [False, True])
Expand Down
Loading