Shortcuts

Attention

June 2024 Status Update: Removing DataPipes and DataLoader V2

We are re-focusing the torchdata repo to be an iterative enhancement of torch.utils.data.DataLoader. We do not plan on continuing development or maintaining the [DataPipes] and [DataLoaderV2] solutions, and they will be removed from the torchdata repo. We’ll also be revisiting the DataPipes references in pytorch/pytorch. In release torchdata==0.8.0 (July 2024) they will be marked as deprecated, and in 0.9.0 (Oct 2024) they will be deleted. Existing users are advised to pin to torchdata==0.8.0 or an older version until they are able to migrate away. Subsequent releases will not include DataPipes or DataLoaderV2. Please reach out if you suggestions or comments (please use this issue for feedback)

LengthSetter

class torchdata.datapipes.iter.LengthSetter(source_datapipe: IterDataPipe[T_co], length: int)

Set the length attribute of the DataPipe, which is returned by __len__ (functional name: set_length). This can be used after DataPipes whose final length cannot be known in advance (e.g. filter). If you know the final length with certainty, you can manually set it, which can then be used by DataLoader or other DataPipes.

Note

This DataPipe differs from Header in that this doesn’t restrict the number of elements that can be yielded from the DataPipe; this is strictly used for setting an attribute so that it can be used later.

Parameters:
  • source_datapipe – a DataPipe

  • length – the integer value that will be set as the length

Example

>>> from torchdata.datapipes.iter import IterableWrapper
>>> dp = IterableWrapper(range(10)).filter(lambda x: x < 5).set_length(3)
>>> list(dp)  # Notice that the number of elements yielded is unchanged
[0, 1, 2, 3, 4]
>>> len(dp)
3
>>> header_dp = IterableWrapper(range(10)).filter(lambda x: x < 5).header(3)
>>> list(header_dp)  # Use `.header()` if you want to limit the number of elements yielded
[0, 1, 2]
>>> len(header_dp)
3

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources