Skip to content

Conversation

@KarhouTam
Copy link
Contributor

@KarhouTam KarhouTam commented Oct 24, 2025

Fixes #165865

What this PR does?

  • Add generator arg to rand*_like APIs (rand_like(), randn_like(), randint_like()).
  • Add unit tests for rand*_like APIs
  • Add corresponding arg docs
  • Refactor rand*_like() codes in TensorFactories.cpp
  • Add corresponding and former missed items in VmapModeRegistrations.cpp

Example (using rand_like())

gen0 = torch.Generator()
gen1 = torch.Generator()
gen2 = torch.Generator()

gen0.manual_seed(42)
gen1.manual_seed(42)
gen2.manual_seed(2025)

tensor = torch.empty(10)

t0 = torch.rand_like(tensor, generator=gen0)
t1 = torch.rand_like(tensor, generator=gen1)
t2 = torch.rand_like(tensor, generator=gen2)

assert t0 == t1
assert t2 != t0
assert t2 != t1

cc @pbelevich @albanD

@pytorch-bot
Copy link

pytorch-bot bot commented Oct 24, 2025

🔗 Helpful Links

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

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

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

✅ You can merge normally! (1 Unrelated Failure)

As of commit 2394bc2 with merge base e67e3d9 (image):

FLAKY - The following job failed but was likely due to flakiness present on trunk:

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

@KarhouTam
Copy link
Contributor Author

@pytorchbot label "module: random" "release notes: random"

@pytorch-bot
Copy link

pytorch-bot bot commented Oct 24, 2025

Didn't find following labels among repository labels: release notes: random

@pytorch-bot pytorch-bot bot added the module: random Related to random number generation in PyTorch (rng generator) label Oct 24, 2025
@github-actions
Copy link
Contributor

Attention! native_functions.yaml was changed

If you are adding a new function or defaulted argument to native_functions.yaml, you cannot use it from pre-existing Python frontend code until our FC window passes (two weeks). Split your PR into two PRs, one which adds the new C++ functionality, and one that makes use of it from Python, and land them two weeks apart. See https://github.com/pytorch/pytorch/wiki/PyTorch's-Python-Frontend-Backward-and-Forward-Compatibility-Policy#forwards-compatibility-fc for more info.


Caused by:

@KarhouTam
Copy link
Contributor Author

@pytorchbot label "release notes: python_frontend"

@pytorch-bot pytorch-bot bot added the release notes: python_frontend python frontend release notes category label Oct 24, 2025
@KarhouTam KarhouTam marked this pull request as draft October 28, 2025 02:25
@KarhouTam KarhouTam marked this pull request as ready for review October 29, 2025 12:42
@KarhouTam
Copy link
Contributor Author

I think this PR is ready for review!
PTAL. @albanD @pbelevich

@jerryzh168 jerryzh168 added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Oct 30, 2025
@KarhouTam
Copy link
Contributor Author

Hi, @albanD @malfet @bobrenjc93. PTAL. If anything this PR missed, please let me know.

Copy link
Collaborator

@albanD albanD left a comment

Choose a reason for hiding this comment

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

SGTM!
Thanks for taking the time to look at this one!

@albanD
Copy link
Collaborator

albanD commented Nov 3, 2025

@pytorchbot merge

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label Nov 3, 2025
@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

pytorch-bot bot pushed a commit that referenced this pull request Nov 4, 2025
Fixes #165865

## What this PR does?

- [x] Add `generator` arg to `rand*_like` APIs (`rand_like()`, `randn_like()`, `randint_like()`).
- [x] Add unit tests for  `rand*_like` APIs
- [x] Add corresponding arg docs
- [x] Refactor `rand*_like()` codes in `TensorFactories.cpp`
- [x] Add corresponding and former missed items in `VmapModeRegistrations.cpp`

## Example (using `rand_like()`)

```python
gen0 = torch.Generator()
gen1 = torch.Generator()
gen2 = torch.Generator()

gen0.manual_seed(42)
gen1.manual_seed(42)
gen2.manual_seed(2025)

tensor = torch.empty(10)

t0 = torch.rand_like(tensor, generator=gen0)
t1 = torch.rand_like(tensor, generator=gen1)
t2 = torch.rand_like(tensor, generator=gen2)

assert t0 == t1
assert t2 != t0
assert t2 != t1
```

Pull Request resolved: #166160
Approved by: https://github.com/cyyever, https://github.com/albanD
pytorchmergebot pushed a commit that referenced this pull request Nov 20, 2025
…functions (#167824)

As the title stated.

PR #166160 add generator variants to `rand*_like()` functions, this PR is for finding out staled signatures and fixing them.

Pull Request resolved: #167824
Approved by: https://github.com/cyyever, https://github.com/albanD
JacobSzwejbka pushed a commit that referenced this pull request Dec 8, 2025
…functions (#167824)

As the title stated.

PR #166160 add generator variants to `rand*_like()` functions, this PR is for finding out staled signatures and fixing them.

Pull Request resolved: #167824
Approved by: https://github.com/cyyever, https://github.com/albanD
@KarhouTam KarhouTam deleted the add-generator-arg branch December 9, 2025 09:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk Trigger trunk jobs on your pull request Merged module: random Related to random number generation in PyTorch (rng generator) open source release notes: python_frontend python frontend release notes category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

randn_like should take a generator.

6 participants