2525pd , _ = optional_import ("pandas" )
2626
2727
28+ def _source_shards_by_worker (data : Iterable [Any ]) -> bool :
29+ """Return whether the source declares that its iterator partitions by worker."""
30+ source_type = type (data )
31+ if "__iter__" in source_type .__dict__ :
32+ return bool (source_type .__dict__ .get ("_shards_by_worker" , False ))
33+ return bool (getattr (source_type , "_shards_by_worker" , False ))
34+
35+
2836class IterableDataset (_TorchIterableDataset ):
2937 """
3038 A generic dataset for iterable data source and an optional callable data transform
@@ -40,6 +48,8 @@ class IterableDataset(_TorchIterableDataset):
4048
4149 """
4250
51+ _shards_by_worker = True
52+
4353 def __init__ (self , data : Iterable [Any ], transform : Callable | None = None ) -> None :
4454 """
4555 Args:
@@ -77,9 +87,11 @@ class ShuffleBuffer(Randomizable, IterableDataset):
7787 epochs: number of epochs to iterate over the dataset, default to 1, -1 means infinite epochs.
7888 source_shards_by_worker: whether ``data`` already partitions its stream
7989 using ``torch.utils.data.get_worker_info``. ``None`` automatically
80- recognizes MONAI ``IterableDataset`` sources, ``True`` avoids a
81- second worker partition for any worker-aware source, and ``False``
82- preserves the outer partition for unsharded iterable datasets.
90+ recognizes built-in MONAI sources that declare worker partitioning.
91+ A subclass that overrides iteration without declaring that capability
92+ is treated as unsharded. ``True`` avoids a second worker partition
93+ for any worker-aware source, and ``False`` preserves the outer
94+ partition for unsharded iterable datasets.
8395
8496 Note:
8597 Both ``monai.data.DataLoader`` and ``torch.utils.data.DataLoader`` do not seed this class (as a subclass of
@@ -102,6 +114,8 @@ def run():
102114
103115 """
104116
117+ _shards_by_worker = True
118+
105119 def __init__ (
106120 self ,
107121 data ,
@@ -121,14 +135,15 @@ def __init__(
121135 epochs: number of source iterations, where ``-1`` means infinite.
122136 source_shards_by_worker: whether ``data`` already partitions its
123137 stream using ``torch.utils.data.get_worker_info``. ``None``
124- automatically recognizes MONAI ``IterableDataset`` sources.
138+ automatically recognizes built-in MONAI sources that declare
139+ worker partitioning.
125140 """
126141 super ().__init__ (data = data , transform = transform )
127142 self .size = buffer_size
128143 self .seed = seed
129144 self .epochs = epochs
130145 self .source_shards_by_worker = (
131- isinstance (data , IterableDataset ) if source_shards_by_worker is None else source_shards_by_worker
146+ _source_shards_by_worker (data ) if source_shards_by_worker is None else source_shards_by_worker
132147 )
133148 self ._idx = 0
134149
@@ -232,6 +247,8 @@ class CSVIterableDataset(IterableDataset):
232247
233248 """
234249
250+ _shards_by_worker = True
251+
235252 def __init__ (
236253 self ,
237254 src : str | Sequence [str ] | Iterable | Sequence [Iterable ],
0 commit comments