class torchdata.datapipes.iter.Forker(datapipe: IterDataPipe, num_instances: int, buffer_size: int = 1000, copy: Optional[Literal['shallow', 'deep']] = None)

Creates multiple instances of the same Iterable DataPipe (functional name: fork).

  • 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 can read relative to the slowest child DataPipe. Defaults to 1000. Use -1 for the unlimited buffer.

  • copy – copy strategy to use for items yielded by each branch. Supported options are None for no copying, "shallow" for shallow object copies, and "deep" for deep object copies. Defaults to None.


All branches of the forked pipeline return the identical object unless the copy parameter is supplied. If the object is mutable or contains mutable objects, changing them in one branch will affect all others.


>>> # xdoctest: +REQUIRES(module:torchdata)
>>> from torchdata.datapipes.iter import IterableWrapper
>>> source_dp = IterableWrapper(range(5))
>>> dp1, dp2 = source_dp.fork(num_instances=2)
>>> list(dp1)
[0, 1, 2, 3, 4]
>>> list(dp2)
[0, 1, 2, 3, 4]


Access comprehensive developer documentation for PyTorch

View Docs


Get in-depth tutorials for beginners and advanced developers

View Tutorials


Find development resources and get your questions answered

View Resources