Skip to content

Commit 035b070

Browse files
Victor Bourginfacebook-github-bot
authored andcommitted
Support len for SPDL dataloader (#839)
Summary: Dataloader length may be useful, in particular to estimate the number of steps in an epoch. We add support for dataloader length for SPDL HiveDataLoaders that use the SPDL datasource. Reviewed By: ynonaolga Differential Revision: D78133462
1 parent 8d75f53 commit 035b070

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

src/spdl/source/utils.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import random
1414
import sys
1515
import time
16-
from collections.abc import Iterable, Iterator, Sequence
16+
from collections.abc import Iterable, Iterator, Sequence, Sized
1717
from typing import TypeVar
1818

1919
from ._type import IterableWithShuffle
@@ -220,6 +220,14 @@ def __iter__(self) -> Iterator[T]:
220220
if self._shuffle_last:
221221
self._shuffle()
222222

223+
def __len__(self) -> int:
224+
if isinstance(self.src, Sized):
225+
return len(self.src)
226+
else:
227+
raise TypeError(
228+
f"Source iterator of type {type(self.src)} does not support length"
229+
)
230+
223231

224232
def embed_shuffle(
225233
src: IterableWithShuffle[T], /, *, shuffle_last: bool = False, epoch: int = 0

0 commit comments

Comments
 (0)