From f7892fa45b696897d31d65e281051cff37657d0c Mon Sep 17 00:00:00 2001 From: Victor Bourgin Date: Wed, 22 Jan 2025 14:39:03 -0800 Subject: [PATCH] Override `num_workers` from 0 to 1 in `get_pytorch_dataloader` (#330) Summary: To support a similar interface to the pytorch dataloader, we set `num_workers` to 1 if the input `num_workers` is 0 in `get_pytorch_dataloader` Reviewed By: moto-meta Differential Revision: D68357865 --- src/spdl/dataloader/_pytorch_dataloader.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/spdl/dataloader/_pytorch_dataloader.py b/src/spdl/dataloader/_pytorch_dataloader.py index 2ea74eef..6ade3893 100644 --- a/src/spdl/dataloader/_pytorch_dataloader.py +++ b/src/spdl/dataloader/_pytorch_dataloader.py @@ -13,6 +13,7 @@ import os import pickle import time +import warnings from collections.abc import Callable, Iterable, Iterator from concurrent.futures import ProcessPoolExecutor from multiprocessing.shared_memory import SharedMemory @@ -298,8 +299,14 @@ def get_pytorch_dataloader( if timeout is not None and timeout < 0: raise ValueError(f"`timeout` must be positive. Found: {timeout}.") - if num_workers < 1: + if num_workers < 0: raise ValueError(f"`num_workers` must be greater than 0. Found: {num_workers}") + elif num_workers == 0: + warnings.warn( + "`num_workers` is 0. Setting `num_workers` to 1 for single process dataloading.", + stacklevel=2, + ) + num_workers = 1 buffer_size = prefetch_factor * num_workers