meta-pytorch / meta-pytorch/data

Make accessing WorkerInfo from within a DataPipe more convenient

Open
#1,084 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.3k
Forks
179
Avg merge
6d 1h
Merged PRs (30d)
2

Description

🚀 The feature
import torchdata.datapipes as dp
from torch.utils.data.datapipes.iter.sharding import SHARDING_PRIORITIES
from torchdata.dataloader2 import MultiProcessingReadingService, DataLoader2

my_worker_info = None

def abc(x):
    return x * my_worker_info.worker_id

def worker_init(dp, worker_info):
    global my_worker_info 
    my_worker_info = worker_info
    return dp
    
    
pipe = dp.iter.IterableWrapper(range(10))
pipe = pipe.map(abc)
pipe = pipe.sharding_filter(SHARDING_PRIORITIES.MULTIPROCESSING)

rs = MultiProcessingReadingService(num_workers=2, worker_init_fn=worker_init)
dl = DataLoader2(pipe, reading_service=rs)

for x in dl:
    print(x)

Output:

0
1
0
3
0
5
0
7
0
9

This seems to be the only way to my knowledge to access the WorkerInfo from within a DataPipe when using Dataloader2. Global state is obviously awkward and becomes a problem for larger coebases that aren't toy examples. It would be good if there was a more convenient way (and also uniform way wrt Dataloader) a kin to get_worker_info.

Traversing the graph and calling set_worker_info if available would be a good option for this IMO.

Motivation, pitch

I want to easily access the current WorkerInfo from my datapipe.

Alternatives

No response

Additional context

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing how DataLoader2 and MultiProcessingReadingService expose worker context to DataPipes, and compare that with DataLoader's get_worker_info API. Evaluate the proposed graph traversal and set_worker_info approach; done means DataPipes can access the current WorkerInfo without global state and the access is uniform with DataLoader.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.