Repository navigation
Add tensor bounds support to stateless uniform API - #199790
jbschlosser wants to merge 8 commits into
Conversation
Attention! native_functions.yaml was changedIf 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. |
PR Status: Approved 🚀Please fix all CI failures and trigger merge by commenting "@pytorchbot merge". ✅ AUTOMATED REVIEW - OUTDATED (earlier commit) - Ready for human review:
Findings (4): Reviewed commit: 🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/199790
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit a8123be with merge base d39020e ( 🟡 GREEN LIGHT - OUTDATED (earlier commit) - PR requires human review:
reason: This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
@claude is the CI failure related? |
|
Claude finished @jbschlosser's task in 30s —— View job No, the CI failure isn't related to this PR. The torchtitan job fails with the same error on the merge base, so trunk is already broken. DetailsThe only failing workflow is
To get a green signal, rebase onto |
There was a problem hiding this comment.
Automated review of 3d36afa: nothing blocking; ready for human review.
The PR adds a Tensor-bounds overload of philox_uniform with CPU and CUDA kernels, a meta registration, a reparameterized backward, a reinplace entry, and tests that check it matches the scalar overload bit for bit on CPU and CUDA. Nothing blocks a maintainer from reviewing it. The remaining issues are smaller: bounds that alias the output, double-backward and forward-mode AD, and a few gaps in test coverage.
⚪ 4 non-blocking findings
When low/high aliases self and already has self's dtype, low.to() returns self. CPU then overwrites self with [0,1) samples before reading the bound, while CUDA reads the original bound values, so the two backends disagree (e.g. t.philox_uniform(key, t, t + 1)). The CUDA path also has no overlap check, so a bound that partially overlaps self (s[:1]) races with the writes. Add at::assert_no_overlap(self, low/high) or clone the bounds when they overlap.
pytorch/tools/autograd/derivatives.yaml
Line 1404 in 3d36afa
The backward regenerates u with at::_philox_uniform(grad, key), and the scalar _philox_uniform has no derivative entry. Under create_graph=True (double backward, hessian) this raises 'derivative not implemented'; use grad.detach() or empty_like(grad) as the template. There is also no result: formula, so forward-mode AD (jvp/jacfwd) is unsupported.
pytorch/test/test_stateless_rng.py
Line 697 in 3d36afa
The eager grad test uses bounds with the same shape as the output, so the sum_to reduction is only checked compiled-vs-eager, never against a closed form. There is also no gradcheck. Add a check of broadcast (8,1) bounds against (grad*(1-u)).sum(1, keepdim=True), or a float64 gradcheck.
pytorch/test/test_stateless_rng.py
Line 643 in 3d36afa
The batched-key cases use elems_per_key of 37 and 3, neither a multiple of epc, so the vectorized branch of philox_multi_key_kernel (param_func(..., base + j)) never runs with tensor bounds. Add a batched case with an inner size divisible by 4.
This review is AI-generated and advisory. Each new automated review replaces the previous one.
|
@claude re-review this and make sure the findings from the automated review have been addressed |
|
Claude finished @jbschlosser's task in 1m 11s —— View job Three of the four earlier findings are fixed in 1a99e6a. The one left is forward-mode AD: the Status of earlier findings, plus one new itemStatus of earlier findings
New findingPerformance: at RecommendationApprove once 2b is addressed or documented. Everything else from the earlier review is fixed and tested. I checked the changes by reading the code only; I didn't run anything locally. |
|
@claude are the CI failures related? |
|
Claude finished @jbschlosser's task in 1m 3s —— View job Yes, the 3 DetailsFailing jobsAll 3 fail on the same test, Likely causeI read the log but did not reproduce this locally.
Possible fixes (pick one)
Option 1 is the smallest change and keeps the second-order check in every config. Not related
|
|
I think this is a fine thing to support |
[ghstack-poisoned]
|
@pytorchbot merge |
Merge startedYour 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 |
Merge failedReason: 1 jobs have failed, first few of them are: Check mergeability of ghstack PR / ghstack-mergeability-check Details for Dev Infra teamRaised by workflow job |
|
@pytorchbot merge |
Merge startedYour 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 |
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 Pull Request resolved: #204472 Approved by: https://github.com/ngimel
Stack from ghstack (oldest at bottom):
I've received beta tester feedback that it's useful to specify different bounds per generated item where bounds may be trained (i.e. allow gradients to flow). This PR addresses this by supporting an overload with tensor low, high bounds for the stateless RNG API
uniform(future work will apply tonormal/randintif this lands):Example usage:
Some details:
TensorIterator-based pass to rescale appropriately.TensorIteratorhandles broadcasting with non-contiguous bounds tensors, etc.OffsetCalculatorfor broadcasting with non-contiguous bounds tensors, etc.The key thing this overload gives is better performance vs. a post-applied manual scaling on
U(0, 1)samples.There's also a minor benefit that we bitwise match the eager numerics of the CUDA scalar bounds overload, since this kernel uses a fused multiply-add.
cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @chauhang @aakhundov @coconutruben @jataylo