Skip to content

Adds functionality to populate torch generator using torch.thread_safe_generator#9371

Open
divyanshk wants to merge 4 commits intopytorch:mainfrom
divyanshk:divyanshk/opt_generator
Open

Adds functionality to populate torch generator using torch.thread_safe_generator#9371
divyanshk wants to merge 4 commits intopytorch:mainfrom
divyanshk:divyanshk/opt_generator

Conversation

@divyanshk
Copy link

@divyanshk divyanshk commented Jan 30, 2026

Added thread-safe random number generation to all V2 torchvision random transforms to prevent race conditions when using DataLoader with thread-based workers (worker_method='thread').

This is based on torch.thread_safe_generator which returns dataloader thread-worker specific RNG or None otherwise.

@pytorch-bot
Copy link

pytorch-bot bot commented Jan 30, 2026

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/vision/9371

Note: Links to docs will display an error until the docs builds have been completed.

❌ 2 New Failures, 1 Pending, 2 Unrelated Failures

As of commit 6277f11 with merge base 48956e0 (image):

NEW FAILURES - The following jobs have failed:

BROKEN TRUNK - The following jobs failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla bot added the cla signed label Jan 30, 2026
@divyanshk divyanshk force-pushed the divyanshk/opt_generator branch from 1e0225e to b15da1c Compare January 30, 2026 17:35
Copy link
Member

@NicolasHug NicolasHug left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @divyanshk . I think the changes look reasonable.

One thing I'm wondering is how does this affect the multiprocess-based dataloaders? Currently, since TV is using the global torch RNG, that global generator will be seeded by torch using a different seed for each process/worker. This is the correct behavior since we want each worker to have a different RNG.

Is that behavior preserved now that we're using torch.thread_safe_generator()?

It'd be good to have tests ensure that's the case (both for multiprocess and multithreaded cases).

@divyanshk divyanshk force-pushed the divyanshk/opt_generator branch from b15da1c to 18bdef3 Compare February 25, 2026 22:20
@divyanshk
Copy link
Author

The multiprocessing case remains unchanged because torch.thread_safe_generator will return None for multiprocessing use-case. So for MP, there is no change. Earlier the torch.rand functions received None for generator arg, and now they would get the same. Also added a test case where I confirm the expected behavior for multiprocessing.

@divyanshk divyanshk force-pushed the divyanshk/opt_generator branch 2 times, most recently from c416fad to e7da958 Compare March 5, 2026 18:34
@divyanshk divyanshk force-pushed the divyanshk/opt_generator branch from e7da958 to 6277f11 Compare March 9, 2026 18:18
@divyanshk divyanshk marked this pull request as ready for review March 9, 2026 22:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants