Skip to content

Add tensor bounds support to stateless normal API - #204472

Closed
jbschlosser wants to merge 8 commits into
gh/jbschlosser/302/basefrom
gh/jbschlosser/302/head
Closed

jbschlosser wants to merge 8 commits into
gh/jbschlosser/302/basefrom
gh/jbschlosser/302/head

Conversation

@jbschlosser

@jbschlosser jbschlosser commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Follow-up from #199790, applying the analogous extension to support tensor bounds for normal().

This PR also addresses a few leftover issues for uniform() that came up during review:

  • aliased parameters are now cloned instead of giving silently wrong results
  • adds a CPU check that self is floating point
  • uniform's forward-AD formula moved into a FunctionsManual helper
  • uniform's tests merged with normal's

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @chauhang @aakhundov @coconutruben @jataylo

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Nov 11, 2026

Copy link
Copy Markdown

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.

@pytorch-bot

pytorch-bot Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

PR Status: Approved 🚀

Please fix all CI failures and trigger merge by commenting "@pytorchbot merge".

✅ AUTOMATED REVIEW - OUTDATED (earlier commit) - Ready for human review:

This PR adds a `_philox_normal_.Tensor` overload so that
`torch.func._random.normal` and `normal_` accept tensor `mean` and `std`. It
includes CPU/CUDA kernels, a meta registration, autograd and an inductor
reinplace entry, and it finishes uniform's tensor overload. The review found no
issues and recommends Approve: every maintainer request is fixed at HEAD, and
the only change since the last review is formatting.

Reviewed commit: f68585a (NOT the current head 91e5229)

Review run

🐛 Report a bad review

🔗 Helpful Links

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

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

✅ You can merge normally! (3 Unrelated Failures)

As of commit 91e5229 with merge base bcff2f4 (image):

🟡 GREEN LIGHT - OUTDATED (earlier commit) - PR requires human review:

  • Triviality
    • New feature: adds the _philox_normal_.Tensor overload in native_functions.yaml with new CPU and CUDA kernels, which fits no trivial class
    • Not a bugfix, refactor, or established-pattern-only change; it adds new sampling math and new dispatch paths
  • Numerics and autograd
    • Adds new math in _philox_normal_tensor_cpu_, _philox_normal_tensor_cuda_, and the new philox_normal_params_backward
    • Adds new forward-AD and backward formulas in tools/autograd/derivatives.yaml, which need human review
  • Scope
    • Touches ATen kernels, autograd codegen, meta registrations, the inductor reinplace.py table, and torch/func/_random.py in one change
    • Added tests in test/test_stateless_rng.py look thorough, but they don't make a new op overload trivial

reason: not_trivial
Reviewed commit: 3302374 (NOT the current head 91e5229)

Inference job

🐛 Report a wrong verdict

NEW FAILURES SUPPRESSED BY AI (non-blocking) - The following job failed, but the AI CI Advisor judged the failure not to be evidence against this change:

  • BC Lint / bc_linter (gh)
    ##[error]API rate limit exceeded for installation. If you reach out to GitHub Support for help, please include the request ID 6038:2A25BD:218980B:6ACD944:6B0F47EE and timestamp 2026-12-01 22:19:26 UTC. For more on scraping GitHub and how it may affect your rights, please review our Terms of Service (https://docs.github.com/en/site-policy/github-terms/github-terms-of-service) - https://docs.github.com/en/rest/using-the-rest-api/getting-started-with-the-rest-api#rate-limiting

    AI verdict: AI verdict: infra issue

    The BC Lint job failed in its actions/checkout step for the pytorch/test-infra repo. That step hit "API rate limit exceeded for installation" three times while retrieving the default branch, so the linter never ran. The PR only touches stateless RNG kernels, autograd formulas, tests and Python code, with no workflow or CI changes that could cause a GitHub API rate limit.

    Full reasoning on HUD →

FLAKY - The following jobs failed but were likely due to flakiness present on trunk:

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

@pytorchgreenlight pytorchgreenlight Bot added the GL/human GreenLight marked as needing human review label Nov 11, 2026
@pytorchbot pytorchbot added the in progress PR is being worked on by the author, until the automated review accepts it label Nov 11, 2026
@jbschlosser jbschlosser added release notes: python_frontend python frontend release notes category topic: improvements topic category labels Nov 16, 2026
@jbschlosser
jbschlosser requested a review from ngimel November 16, 2026 19:48
[ghstack-poisoned]
github-actions[bot]

This comment was marked as outdated.

@pytorch-bot pytorch-bot Bot added ready for review Automated review passed, ready for a final review by a maintainer. and removed in progress PR is being worked on by the author, until the automated review accepts it labels Nov 16, 2026
[ghstack-poisoned]

@albanD albanD left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think the four below should be fixed

Comment thread torch/csrc/autograd/FunctionsManual.cpp Resolved
[ghstack-poisoned]
@pytorchgreenlight pytorchgreenlight Bot added GL/superseded GreenLight review is superseded by changes and removed GL/human GreenLight marked as needing human review labels Nov 23, 2026
[ghstack-poisoned]
@jbschlosser jbschlosser removed the ready for review Automated review passed, ready for a final review by a maintainer. label Nov 23, 2026
@pytorch-bot pytorch-bot Bot added the ciflow/torchtitan Run TorchTitan integration tests label Dec 1, 2026
@pytorchmergebot

Copy link
Copy Markdown
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

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: 1 jobs have failed, first few of them are: trunk / win-vs2022-cpu-py3 / test (default, 9, 12, windows.4xlarge.nonephemeral). Blocking jobs that the merge waited on for a Dr.CI or AI CI Advisor classification: 1.

Details for Dev Infra team

Raised by workflow job

@jbschlosser

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
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

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: 1 jobs have failed, first few of them are: trunk / win-vs2022-cpu-py3 / test (default, 9, 12, windows.4xlarge.nonephemeral)

Details for Dev Infra team

Raised by workflow job

@jbschlosser

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge -i

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: 1 jobs have failed, first few of them are: .github/workflows/trunk.yml / win-vs2022-cpu-py3 / test (default, 9, 12, windows.4xlarge.nonephemeral)

Details for Dev Infra team

Raised by workflow job

@jbschlosser

Copy link
Copy Markdown
Contributor Author

@claude why can't I merge and what should I do to fix it?

@claude

claude Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Claude finished @jbschlosser's task in 37s —— View job


The merge is blocked by a redis release on PyPI, not by your code. Your stack's merge base (Nov 19) predates the redis pin on main, so rebasing onto current main should fix it.

Details

Why it fails

  • The blocking job is trunk / win-vs2022-cpu-py3 / test (default, 9, 12) (inductor/test_nested_reduction_lanes).
  • .ci/docker/requirements-ci.txt on this branch has redis>=4.0.0 with no upper bound. The job log shows that CI installed redis-8.2.0, which was released on Nov 23.
  • Importing redis 8.2.0 raises BytesWarning: Comparison between bytes and string. The test runner uses python -bb, which turns that warning into an error, so the test file fails at import.
  • The same cause explains the other macOS and Windows inductor failures. Dr. CI's notes say main now pins redis<8.2.0. I couldn't confirm that myself because fetching main needed approval I didn't have.
  • Your last merge -i listed this job in its ignore set and still failed on the same job, so ignoring it doesn't get past it. Re-running the job on the current base won't help either, because the job will install 8.2.0 again.

What to do

Rebase the stack onto current main so it picks up the pin, let CI re-run, then merge:

@pytorchbot rebase

Locally, ghstack rebase / git rebase origin/main followed by ghstack also works. Your PR is mid-stack, and the PRs above it need the new base too, so a full ghstack run is appropriate here.

If you'd rather not wait for CI, @pytorchbot merge -f "<reason>" with a reason like "redis 8.2.0 BytesWarning, fixed on main by the pin" bypasses the checks. Rebasing is the cleaner option.

[ghstack-poisoned]
@jbschlosser

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
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

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

The merge job was canceled or timed out. This most often happen if two merge requests were issued for the same PR, or if merge job was waiting for more than 6 hours for tests to finish. In later case, please do not hesitate to reissue the merge command
For more information see pytorch-bot wiki.

@jbschlosser

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
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

pytorchmergebot pushed a commit that referenced this pull request Dec 2, 2026
In preparation for a move to make the stateless RNG APIs public, this PR addresses a few minor issues:
- CUDA: read a batched key with a non-unit last-dim stride correctly (previously gave wrong values), and
copy an 8-byte-aligned single key to aligned memory before the 16-byte vector load (previously
crashed). Batched keys with a unit last-dim stride are read in place instead of copied.
- Reject an output whose elements share memory, or that overlaps a dense key, before doing any work.
Previously internal overlap errored only in eager, after the compute, from copy_, and key aliasing
wasn't caught at all. Meta checks internal overlap too. `assert_no_overlap` can't see overlap with a
non-dense key, so CUDA copies a key that shares storage with the output instead of reading it while it's
overwritten.
- Write non-contiguous or misaligned outputs to a fresh buffer instead of a copy of the old values.
- Make eager and meta validation agree: CPU and CUDA share one key/output check, so errors come in the
same order on every path; meta checks tensor bounds before the key, like eager; CPU `uniform_`/`normal_`
reject integer outputs with a clear error; and meta `randint_` rejects an empty range.

Test Plan:
```
python test/test_stateless_rng.py -k test_noncontig_key -k test_overlapping_output -k
test_tensor_bounds_errors -k test_error_wrong_self_dtype -k test_error_op_empty_range
python test/test_stateless_rng.py
```

Authored with the assistance of an AI coding assistant (Claude).
Pull Request resolved: #206328
Approved by: https://github.com/pytorchgreenlight, https://github.com/albanD
ghstack dependencies: #204472
pytorchmergebot pushed a commit that referenced this pull request Dec 2, 2026
Expands `fold_in()` to support int64 scalar tensor data. This is useful because uint64 has limited support within PyTorch, and requiring it as the dtype for data makes doing math on such data annoying:
```python
import torch
import torch.func._random as random

key = random.key(42, device="cuda")
step = torch.tensor(0, device="cuda")  # int64, e.g. a training-step counter

# New: int64 data is accepted directly.
k = random.fold_in(key, step)

# Old (uint64 only): reinterpret the bits first. view() is zero-copy and
# matches the int overload for negatives; .to(torch.uint64) would add a copy.
k_old = random.fold_in(key, step.view(torch.uint64))
```
Pull Request resolved: #206339
Approved by: https://github.com/albanD
ghstack dependencies: #204472, #206328
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests ciflow/trunk Trigger trunk jobs on your pull request GL/superseded GreenLight review is superseded by changes Merged module: inductor ready for review Automated review passed, ready for a final review by a maintainer. release notes: python_frontend python frontend release notes category topic: improvements topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants