-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathmux_longest.py
More file actions
55 lines (41 loc) · 1.91 KB
/
Copy pathmux_longest.py
File metadata and controls
55 lines (41 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
from typing import Set, Sized
from torch.utils.data.datapipes._decorator import functional_datapipe
from torch.utils.data.datapipes.datapipe import IterDataPipe
@functional_datapipe("mux_longest")
class MultiplexerLongestIterDataPipe(IterDataPipe):
r"""
Yields one element at a time from each of the input Iterable DataPipes (functional name: ``mux_longest``). As in,
one element from the 1st input DataPipe, then one element from the 2nd DataPipe in the next iteration,
and so on. It skips over DataPipes that are exhausted, and ends when all input DataPipes are exhausted.
Args:
datapipes: Iterable DataPipes that will take turn to yield their elements, until they are all exhausted
Example:
.. testcode::
dp1, dp2, dp3 = IterableWrapper(range(5)), IterableWrapper(range(10, 12)), IterableWrapper(range(20, 25))
print(list(dp1.mux_longest(dp2, dp3)))
.. testoutput::
[0, 10, 20, 1, 11, 21, 2, 22, 3, 23, 4, 24]
"""
def __init__(self, *datapipes):
self.datapipes = datapipes
def __iter__(self):
iterators = [iter(x) for x in self.datapipes]
finished: Set[int] = set()
while len(finished) < len(iterators):
for i in range(len(iterators)):
if i not in finished:
try:
value = next(iterators[i])
yield value
except StopIteration:
finished.add(i)
def __len__(self):
if all(isinstance(dp, Sized) for dp in self.datapipes):
return sum(len(dp) for dp in self.datapipes)
else:
raise TypeError(f"{type(self).__name__} instance doesn't have valid length")