Adding a thread safe RNG utility function#1529
Open
divyanshk wants to merge 1 commit intometa-pytorch:mainfrom
Open
Adding a thread safe RNG utility function#1529divyanshk wants to merge 1 commit intometa-pytorch:mainfrom
divyanshk wants to merge 1 commit intometa-pytorch:mainfrom
Conversation
|
@divyanshk has exported this pull request. If you are a Meta employee, you can view the originating Diff in D93776060. |
divyanshk
added a commit
to divyanshk/data
that referenced
this pull request
Feb 19, 2026
Summary: [Identical version of [PR](pytorch/pytorch#172659) but with alterations to keep BC with torchdata statefuldataloader] This includes part of the changes in pytorch/pytorch#161044 When using PyTorch's DataLoader with thread-based workers, all worker threads share the same global random number generator (RNG) state. This creates a race condition: multiple threads may call random functions like torch.randint() or torch.rand() simultaneously, leading to non-reproducible results. `torch.thread_safe_generator()` solves this by returning a thread-local generator when called from within a DataLoader thread worker. This PR: * we only include the utility public function to return the RNG. The RNG will be populated with the thread dataloader PR linked above. Right now this PR doesn't open any new functionality, the function will return `None` as RNG state isn't populated for thread workers (there are no thread workers right now - will land with PR#161044). * landing this function separately to enable integration with Torchvision random transforms. * Also, refactored `WorkerInfo` in `worker.py` to be a frozen dataclass. Differential Revision: D93776060
587364d to
2f7d6d3
Compare
Summary: [Identical version of [PR](pytorch/pytorch#172659) but with alterations to keep BC with torchdata statefuldataloader] This includes part of the changes in pytorch/pytorch#161044 When using PyTorch's DataLoader with thread-based workers, all worker threads share the same global random number generator (RNG) state. This creates a race condition: multiple threads may call random functions like torch.randint() or torch.rand() simultaneously, leading to non-reproducible results. `torch.thread_safe_generator()` solves this by returning a thread-local generator when called from within a DataLoader thread worker. This PR: * we only include the utility public function to return the RNG. The RNG will be populated with the thread dataloader PR linked above. Right now this PR doesn't open any new functionality, the function will return `None` as RNG state isn't populated for thread workers (there are no thread workers right now - will land with PR#161044). * landing this function separately to enable integration with Torchvision random transforms. * Also, refactored `WorkerInfo` in `worker.py` to be a frozen dataclass. Differential Revision: D93776060
2f7d6d3 to
89b7f67
Compare
divyanshk
added a commit
to divyanshk/pytorch
that referenced
this pull request
Feb 19, 2026
Summary: X-link: meta-pytorch/data#1529 [Identical version of [PR](pytorch#172659) but with alterations to keep BC with torchdata statefuldataloader] This includes part of the changes in pytorch#161044 When using PyTorch's DataLoader with thread-based workers, all worker threads share the same global random number generator (RNG) state. This creates a race condition: multiple threads may call random functions like torch.randint() or torch.rand() simultaneously, leading to non-reproducible results. `torch.thread_safe_generator()` solves this by returning a thread-local generator when called from within a DataLoader thread worker. This PR: * we only include the utility public function to return the RNG. The RNG will be populated with the thread dataloader PR linked above. Right now this PR doesn't open any new functionality, the function will return `None` as RNG state isn't populated for thread workers (there are no thread workers right now - will land with PR#161044). * landing this function separately to enable integration with Torchvision random transforms. * Also, refactored `WorkerInfo` in `worker.py` to be a frozen dataclass. Test Plan: contbuild & OSS CI Differential Revision: D93776060
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Equivalent unit test on pytorch/pytorch pytorch/pytorch#169116
Summary:
[Identical version of PR but with alterations to keep BC with torchdata statefuldataloader]
This includes part of the changes in pytorch/pytorch#161044
When using PyTorch's DataLoader with thread-based workers, all worker threads share the same global random number generator (RNG) state. This creates a race condition: multiple threads may call random functions like torch.randint() or torch.rand() simultaneously, leading to non-reproducible results.
torch.thread_safe_generator()solves this by returning a thread-local generator when called from within a DataLoader thread worker.This PR:
Noneas RNG state isn't populated for thread workers (there are no thread workers right now - will land with PR#161044).WorkerInfoinworker.pyto be a frozen dataclass.Differential Revision: D93776060