break
self.assertEqual(list(range(10)), list(dp3)) # dp3 has to read from the start again
+ # Test Case: Each DataPipe inherits the source datapipe's length
+ dp1, dp2, dp3 = input_dp.fork(num_instances=3)
+ self.assertEqual(len(input_dp), len(dp1))
+ self.assertEqual(len(input_dp), len(dp2))
+ self.assertEqual(len(input_dp), len(dp3))
+
def test_demux_datapipe(self):
input_dp = IDP(range(10))
with self.assertRaises(ValueError):
next(it1)
+ # Test Case: __len__ not implemented
+ dp1, dp2 = input_dp.demux(num_instances=2, classifier_fn=lambda x: x % 2)
+ with self.assertRaises(TypeError):
+ len(dp1) # It is not implemented as we do not know length for each child in advance
+ with self.assertRaises(TypeError):
+ len(dp2)
+
def test_map_datapipe(self):
input_dp = IDP(range(10))
r""" :class:`ForkerIterDataPipe`.
Iterable DataPipe to create multiple instances of the same Iterable DataPipe.
- args:
+
+ Args:
datapipe: Iterable DataPipe being copied
num_instances: number of instances of the datapipe to create
buffer_size: this restricts how far ahead the leading child DataPipe
self.leading_ptr = 0
self.end_ptr: Optional[int] = None
+ def __len__(self):
+ return len(self.main_datapipe)
+
def get_next_element_by_instance(self, instance_id: int):
if self._datapipe_iterator is None:
self._datapipe_iterator = iter(self.main_datapipe)
Iteratable Datapipe that is a child of a main DataPipe. The instance of this class
will pass its instance_id to get the next value from its main DataPipe.
- args:
+
+ Args:
main_datapipe: Main DataPipe with a method 'get_next_element_by_instance(instance_id)'
instance_id: integer identifier of this instance
"""
# We want to separate the code for reset and yield, so that 'reset' exeutes before __next__ is called
return self.get_generator_by_instance(self.instance_id)
+ def __len__(self):
+ return len(self.main_datapipe)
+
def get_generator_by_instance(self, instance_id: int):
yield from self.main_datapipe.get_next_element_by_instance(self.instance_id)
Iterable DataPipe to split the input DataPipe into multiple child DataPipes, using the given
classification function. A list of the child DataPipes is returned from this operation.
- args:
+
+ Args:
datapipe: Iterable DataPipe being filtered
num_instances: number of instances of the DataPipe to create
classifier_fn: a function that maps values to an integer within the range [0, num_instances - 1] or None