Repository navigation
Improve stateless RNG handling of strided keys and outputs - #206328
jbschlosser wants to merge 8 commits into
Conversation
[ghstack-poisoned]
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 (2): Reviewed commit: 🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/206328
Note: Links to docs will display an error until the docs builds have been completed. ❌ You can merge normally! (1 Unrelated Failure), 1 Unclassified FailureAs of commit ce5d5b0 with merge base bcff2f4 ( 🟢 GREEN LIGHT - OUTDATED (earlier commit) - PR approved to be merged without human review:
reason: UNCLASSIFIED FAILURE - DrCI could not classify the following job because the workflow did not run on the merge base. The failure may be pre-existing on trunk or introduced by this PR:
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. |
[ghstack-poisoned]
[ghstack-poisoned]
Superseded by a newer greenlight verdict.
[ghstack-poisoned]
AI-generated summary of changes (reviewed by me)
Also fixed: CPU |
[ghstack-poisoned]
[ghstack-poisoned]
There was a problem hiding this comment.
Automated review of 6ac8a26: nothing blocking; ready for human review.
This PR fixes how the stateless Philox RNG ops handle strided or misaligned keys and outputs: it rejects outputs that overlap themselves or a dense key, and makes the CPU, CUDA and meta paths validate in the same order. Recommendation: Approve. The earlier finding is fixed and only non-blocking points remain.
⚪ 2 non-blocking findings are attached to the code below, collapsed.
This review is AI-generated and advisory. Each new automated review replaces the previous one. Report a bad review
| for batched in batches: | ||
| k = batched.unsqueeze(-2) | ||
| shape = (*k.shape[:-2], 33) | ||
| expected = gen(k.clone(), shape, **kwargs) |
There was a problem hiding this comment.
⚪ Non-blocking: the new perm case in test_noncontig_key can't fail. perm.unsqueeze(-2) has shape (4,3,1,2) and strides (2,8,2,1), so it is…
Non-blocking: the new perm case in test_noncontig_key can't fail. perm.unsqueeze(-2) has shape (4,3,1,2) and strides (2,8,2,1), so it is non-overlapping and dense. clone() defaults to preserve_format, which keeps those strides, so expected = gen(k.clone(), ...) sends the same permuted strides down the same CUDA in-place path (PhiloxDistribution.cu:182, 220-227) as the call under test. A bug in how OffsetCalculator handles permuted strides would give the same wrong values on both sides and the assertion would still pass. Fix: build expected from k.contiguous() for this loop. The other cases (keys\[::2\], expand, offset slice) are fine because their clones come out contiguous.
| // single-key kernel loads it with ld_vec<16>, which needs 16-byte alignment. | ||
| // A key read in place must not share memory with output, and | ||
| // assert_no_overlap can't detect overlap with a non-dense key. | ||
| auto key_in = key.stride(-1) == 1 && canUse32BitIndexMath(key) ? key : key.contiguous(); |
There was a problem hiding this comment.
⚪ Non-blocking (code the last review already covered): an expanded key can now be copied in full when the old code read it in place.…
Non-blocking (code the last review already covered): an expanded key can now be copied in full when the old code read it in place. canUse32BitIndexMath(key) returns false whenever key.numel() >= INT32_MAX, before it looks at offsets, so a stride-0 expanded key with a tiny largest offset still gets copied by key.contiguous(). Example: a (2^30,) float32 output with random.key(0).expand(2**30, 2) allocates a 16 GiB key copy; the merge-base code read such a key in place through its strides. The in-place read only needs the largest element offset (sum of (size-1)*stride) to fit in the uint32 index math of OffsetCalculator<1>, so checking that offset instead of numel would avoid the copy. Normal size-1 broadcast keys are unaffected.
[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 |
|
Starting merge as part of PR stack under #206339 |
Merge failedReason: 1 jobs have failed, first few of them are: trunk / macos-py3-arm64 / test (default, 1, 3, macos-m1-stable). Blocking jobs that the merge waited on for a Dr.CI or AI CI Advisor classification: 1. Details for Dev Infra teamRaised by workflow job |
|
Starting merge as part of PR stack under #206339 |
2 similar comments
|
Starting merge as part of PR stack under #206339 |
|
Starting merge as part of PR stack under #206339 |
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
Stack from ghstack (oldest at bottom):
In preparation for a move to make the stateless RNG APIs public, this PR addresses a few minor issues:
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.
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_overlapcan't see overlap with anon-dense key, so CUDA copies a key that shares storage with the output instead of reading it while it's
overwritten.
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:
Authored with the assistance of an AI coding assistant (Claude).