Skip to content

Commit

Permalink
Add warning in a multiprocessing special case (#6830)
Browse files Browse the repository at this point in the history
Related to discussion
#6657

This code at least adds a warning if `set_track_meta(False)` and
`multiprocessing_context='spawn'` are used in the same code. However
this warning only triggers if `set_track_meta(False)` has been called
before the DataLoader has been initialized. I will append some example
code where this is not True, still the bug is triggered.
Imo this is still a MONAI bug even though in the discussion it was
claimed otherwise. The multiprocessing_context='spawn' has unintended
consequences and this is only true for MONAI and not for torch. (I
believe the problem is that with 'spawn' Python and all the libs get
reinitialized and thus _TRACK_META is reset to being True).

### Types of changes
<!--- Put an `x` in all the boxes that apply, and remove the not
applicable items -->
- [x] Non-breaking change (fix or new feature that would not break
existing functionality).
- [ ] Breaking change (fix or new feature that would cause existing
functionality to change).
- [ ] New tests added to cover the changes.
- [ ] Integration tests passed locally by running `./runtests.sh -f -u
--net --coverage`.
- [ ] Quick tests passed locally by running `./runtests.sh --quick
--unittests --disttests`.
- [ ] In-line docstrings updated.
- [ ] Documentation updated, tested `make html` command in the `docs/`
folder.

---------

Signed-off-by: Matthias Hadlich <matthiashadlich@posteo.de>
  • Loading branch information
matt3o authored Aug 7, 2023
1 parent 49a1ae5 commit cb257d2
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions monai/data/dataloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@

from __future__ import annotations

import warnings

import torch
from torch.utils.data import DataLoader as _TorchDataLoader
from torch.utils.data import Dataset

from monai.data.meta_obj import get_track_meta
from monai.data.utils import list_data_collate, set_rnd, worker_init_fn

__all__ = ["DataLoader"]
Expand Down Expand Up @@ -88,4 +91,16 @@ def __init__(self, dataset: Dataset, num_workers: int = 0, **kwargs) -> None:
if "worker_init_fn" not in kwargs:
kwargs["worker_init_fn"] = worker_init_fn

if (
"multiprocessing_context" in kwargs
and kwargs["multiprocessing_context"] == "spawn"
and not get_track_meta()
):
warnings.warn(
"Please be aware: Return type of the dataloader will not be a Tensor as expected but"
" a MetaTensor instead! This is because 'spawn' creates a new process where _TRACK_META"
" is initialized to True again. Context:_TRACK_META is set to False and"
" multiprocessing_context to spawn"
)

super().__init__(dataset=dataset, num_workers=num_workers, **kwargs)

0 comments on commit cb257d2

Please sign in to comment.