Skip to content

Commit feb2f8f

Browse files
authored
Support len for SPDL dataloader
Differential Revision: D78133462 Pull Request resolved: #839
1 parent 8d75f53 commit feb2f8f

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)