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)
MapKeyZipper¶
- class torchdata.datapipes.iter.MapKeyZipper(source_iterdatapipe: IterDataPipe, map_datapipe: MapDataPipe, key_fn: Callable, merge_fn: Optional[Callable] = None, keep_key: bool = False)¶
Joins the items from the source IterDataPipe with items from a MapDataPipe (functional name:
zip_with_map
). The matching is done by the providedkey_fn
, which maps an item fromsource_iterdatapipe
to a key that should exist in themap_datapipe
. The return value is created by themerge_fn
, which returns a tuple of the two items by default.- Parameters:
source_iterdatapipe – IterDataPipe from which items are yield and will be combined with an item from
map_datapipe
map_datapipe – MapDataPipe that takes a key from
key_fn
, and returns an itemkey_fn – Function that maps each item from
source_iterdatapipe
to a key that exists inmap_datapipe
keep_key – Option to yield the matching key along with the items in a tuple, resulting in
(key, merge_fn(item1, item2))
.merge_fn – Function that combines the item from
source_iterdatapipe
and the matching item frommap_datapipe
, by default a tuple is created
Example:
from torchdata.datapipes.iter import IterableWrapper from torchdata.datapipes.map import SequenceWrapper def merge_fn(tuple_from_iter, value_from_map): return tuple_from_iter[0], tuple_from_iter[1] + value_from_map dp1 = IterableWrapper([('a', 1), ('b', 2), ('c', 3)]) mapdp = SequenceWrapper({'a': 100, 'b': 200, 'c': 300, 'd': 400}) res_dp = dp1.zip_with_map(map_datapipe=mapdp, key_fn=itemgetter(0), merge_fn=merge_fn) print(list(res_dp))
Output:
[('a', 101), ('b', 202), ('c', 303)]