data icon indicating copy to clipboard operation
data copied to clipboard

Nodes/get worker info parallel mapper

Open psaikaushik opened this issue 7 months ago • 0 comments

Summary

Adds num_workers parameter to _apply_udf and sets a WorkerInfo object on each worker before the processing loop begins, so user-defined functions can identify which worker they are running in. Introduces torchdata.nodes.get_worker_info() backed by threading.local() — always returns the correct WorkerInfo for the calling worker regardless of thread vs. process mode. For process workers, torch.utils.data.get_worker_info() also works correctly (each process has isolated memory). For thread workers, torchdata.nodes.get_worker_info() is the safe API; torch.utils.data.get_worker_info() may race between threads so the thread-local version is preferred. Exports get_worker_info from torchdata.nodes.

Fixes #1388

Test plan

test_none_outside_worker — returns None when called from outside any worker test_thread_workers — all items report a valid worker_id ∈ [0, num_workers) and correct num_workers test_thread_workers_each_id_used — all worker ids appear across 40 items with 4 workers test_thread_workers_unique_seeds — each worker reports one consistent seed; seeds differ across workers test_process_workers_get_worker_info — torch.utils.data.get_worker_info() works in forkserver process workers test_process_workers_torchdata_get_worker_info — torchdata.nodes.get_worker_info() works in process workers test_num_workers_zero_no_worker_info — inline (num_workers=0) UDF sees None

psaikaushik avatar Apr 16 '26 21:04 psaikaushik