MCPcopy Create free account
hub / github.com/pytorch/pytorch / _worker_loop

Function _worker_loop

torch/utils/data/_utils/worker.py:208–329  ·  view source on GitHub ↗
(dataset_kind, dataset, index_queue, data_queue, done_event,
                 auto_collation, collate_fn, drop_last, base_seed, init_fn, worker_id,
                 num_workers, persistent_workers, shared_seed)

Source from the content-addressed store, hash-verified

206 return state
207
208def _worker_loop(dataset_kind, dataset, index_queue, data_queue, done_event,
209 auto_collation, collate_fn, drop_last, base_seed, init_fn, worker_id,
210 num_workers, persistent_workers, shared_seed):
211 # See NOTE [ Data Loader Multiprocessing Shutdown Logic ] for details on the
212 # logic of this function.
213
214 try:
215 # Initialize C side signal handlers for SIGBUS and SIGSEGV. Python signal
216 # module's handlers are executed after Python returns from C low-level
217 # handlers, likely when the same fatal signal had already happened
218 # again.
219 # https://docs.python.org/3/library/signal.html#execution-of-python-signal-handlers
220 signal_handling._set_worker_signal_handlers()
221
222 torch.set_num_threads(1)
223 seed = base_seed + worker_id
224 random.seed(seed)
225 torch.manual_seed(seed)
226 if HAS_NUMPY:
227 np_seed = _generate_state(base_seed, worker_id)
228 import numpy as np
229 np.random.seed(np_seed)
230
231 from torch.utils.data import IterDataPipe
232 from torch.utils.data.graph_settings import apply_random_seed
233
234 shared_rng = torch.Generator()
235 if isinstance(dataset, IterDataPipe):
236 assert shared_seed is not None
237 shared_rng.manual_seed(shared_seed)
238 dataset = apply_random_seed(dataset, shared_rng)
239
240 global _worker_info
241 _worker_info = WorkerInfo(id=worker_id, num_workers=num_workers,
242 seed=seed, dataset=dataset)
243
244 from torch.utils.data import _DatasetKind
245
246 init_exception = None
247
248 try:
249 if init_fn is not None:
250 init_fn(worker_id)
251
252 fetcher = _DatasetKind.create_fetcher(dataset_kind, dataset, auto_collation, collate_fn, drop_last)
253 except Exception:
254 init_exception = ExceptionWrapper(
255 where=f"in DataLoader worker process {worker_id}")
256
257 # When using Iterable mode, some worker can exit earlier than others due
258 # to the IterableDataset behaving differently for different workers.
259 # When such things happen, an `_IterableDatasetStopIteration` object is
260 # sent over to the main process with the ID of this worker, so that the
261 # main process won't send more tasks to this worker, and will send
262 # `None` to this worker to properly exit it.
263 #
264 # Note that we cannot set `done_event` from a worker as it is shared
265 # among all processes. Instead, we set the `iteration_end` flag to

Callers

nothing calls this directly

Calls 15

is_aliveMethod · 0.95
apply_random_seedFunction · 0.90
ExceptionWrapperClass · 0.90
_generate_stateFunction · 0.85
isinstanceFunction · 0.85
WorkerInfoClass · 0.85
ManagerWatchdogClass · 0.85
set_num_threadsMethod · 0.80
create_fetcherMethod · 0.80
init_fnFunction · 0.50
seedMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…