Skip to content

Commit 518b967

Browse files
committed
Add warning in a multiprocessing special case
Signed-off-by: Matthias Hadlich <[email protected]>
1 parent 65cf5fe commit 518b967

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

monai/data/dataloader.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,20 @@
1111

1212
from __future__ import annotations
1313

14+
import warnings
15+
1416
import torch
1517
from torch.utils.data import DataLoader as _TorchDataLoader
1618
from torch.utils.data import Dataset
1719

20+
from monai.apps.utils import get_logger
21+
from monai.data.meta_obj import get_track_meta
1822
from monai.data.utils import list_data_collate, set_rnd, worker_init_fn
1923

2024
__all__ = ["DataLoader"]
2125

26+
logger = get_logger(module_name=__name__)
27+
2228

2329
class DataLoader(_TorchDataLoader):
2430
"""
@@ -88,4 +94,16 @@ def __init__(self, dataset: Dataset, num_workers: int = 0, **kwargs) -> None:
8894
if "worker_init_fn" not in kwargs:
8995
kwargs["worker_init_fn"] = worker_init_fn
9096

97+
if (
98+
"multiprocessing_context" in kwargs
99+
and kwargs["multiprocessing_context"] == "spawn"
100+
and not get_track_meta()
101+
):
102+
warnings.warn(
103+
"Please be aware: Return type of the dataloader will not be a Tensor as expected but"
104+
" a MetaTensor instead! This is because 'spawn' creates a new process where _TRACK_META"
105+
" is initialized to True again. Context:_TRACK_META is set to False and"
106+
" multiprocessing_context to spawn"
107+
)
108+
91109
super().__init__(dataset=dataset, num_workers=num_workers, **kwargs)

0 commit comments

Comments
 (0)