Repository navigation
Implement stateless RNG dropout - #204280
jbschlosser wants to merge 17 commits into
Conversation
[ghstack-poisoned]
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 (6): Reviewed commit: 🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/204280
Note: Links to docs will display an error until the docs builds have been completed. ❌ 7 Unclassified FailuresAs of commit 73087f8 with merge base bcff2f4 ( 🟡 GREEN LIGHT - OUTDATED (earlier commit) - PR requires human review:
reason: UNCLASSIFIED FAILURES - DrCI could not classify the following jobs because the workflow did not run on the merge base. The failures may be pre-existing on trunk or introduced by this PR:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
albanD
left a comment
There was a problem hiding this comment.
I haven't validated the fine grain part of the sampling kernel, but sounds good.
|
|
||
|
|
||
| def dropout( | ||
| key: torch.Tensor, input: torch.Tensor, p: float = 0.5, training: bool = True |
There was a problem hiding this comment.
Let's do the same positional and kwarg only as the other APIs here?
| "_philox_dropout: self must be a floating point tensor, got ", | ||
| self.scalar_type()); | ||
| TORCH_CHECK(p >= 0 && p <= 1, "_philox_dropout: p must be in [0, 1], got ", p); | ||
| auto result = at::empty_like(self); |
There was a problem hiding this comment.
This comment from the review is real:
On CUDA, a non-contiguous input (channels_last, transposed) causes up to three extra full copies, though the result is still correct. (1) at::empty_like(self) keeps the input's strides, so the helper's self.contiguous() (:194) copies the uninitialized result. (2) The kernel's output is then copy_'d back (:255). (3) Separately, input = self.contiguous() copies the input. Allocating the kernel's output contiguously and copying only when needed removes the copy of uninitialized memory. An offset calculator over self's strides, like make_bounds_offset_calculator in the sibling philox_uniform_tensor_cuda, would remove the input copy.
[ghstack-poisoned]
[ghstack-poisoned]
There was a problem hiding this comment.
Automated review of fc1bd4e: nothing blocking; ready for human review.
The PR adds aten::_philox_dropout, a stateless dropout whose mask comes from a PRNG key, with CPU/CUDA kernels, an auto_linear derivative, a meta kernel, and the torch.func._random.dropout wrapper. Both blocking findings from the last review are fixed at HEAD. Recommendation: Approve; every remaining point is non-blocking.
⚪ 6 non-blocking findings
Line 692 in fc1bd4e
Non-blocking, carried over: if p < 0.0 or p > 1.0 is false for NaN. With training=False, a NaN p silently returns input. With training=True, NaN reaches the kernel or meta check and raises RuntimeError, while -0.1 and 1.1 raise ValueError from the wrapper, so the same public function raises two different exception types for invalid p. test_errors (test/test_stateless_rng.py:1652-1653) pins this behavior. Fix: if not 0.0 <= p <= 1.0: raise ValueError(...), and update that assertion to expect ValueError.
Line 671 in fc1bd4e
Non-blocking, carried over: the docstring says the leading dims of input must be "broadcastable with *batch". The kernels actually require two different things: key.dim() == input.dim() + 1, and the key batch must expand one way to the input shape (is_expandable_to), which is not two-way broadcasting. The sentence was copied from the sibling docstrings, so a module-wide follow-up is fine.
pytorch/test/test_stateless_rng.py
Line 1635 in fc1bd4e
Non-blocking: test_opcheck checks less than it seems to. test_faketensor compares nothing for an aten-namespace op: fake_check runs CrossRefFakeMode(ignore_op_fn=is_builtin). test_autograd_registration returns early (with requires_grad=False at generate_tests.py:78-82, and with requires_grad=True at autograd_registration.py:99-102, because derivatives.yaml registers an Autograd kernel). Only test_schema and test_aot_dispatch_dynamic do meaningful work, and the latter runs the same kernel on both sides, so it cannot catch wrong values from the strided path. Meta strides are still checked by the FakeTensorMode assertion in test_noncontig_input. Either say so in a comment, or check the meta kernel directly under CrossRefFakeMode() without ignore_op_fn.
pytorch/test/test_stateless_rng.py
Line 1661 in fc1bd4e
Non-blocking, carried over: the "same device" regex matches the generic "Expected all tensors to be on the same device" error from the dispatcher, or FakeTensorDeviceMismatchError under fake mode. The op's own check (PhiloxDistribution.cu:173-175) and the meta check never run. Tighten the regex or add a comment.
pytorch/test/test_stateless_rng.py
Line 1603 in fc1bd4e
Non-blocking, carried over: two parts of test_noncontig_input add little. offset.copy_(x) is contiguous, so it takes the plain contiguous branch and cannot reach the helper's realignment branch. out.stride() == torch.empty_like(inp).stride() (:1610) just restates the implementation.
Non-blocking, carried over: the CPU path (:289-325) still allocates a full-size fp32/fp64 samples buffer (twice the input's size for half/bf16) and then makes a second cpu_kernel pass to apply the mask. The shared fill loop (:150-161) is serial. Passing the element index to the CPU helper's param_func, as the CUDA helper does, would allow a single pass.
This review is AI-generated and advisory. Each new automated review replaces the previous one. Report a bad review
Withdrawn: the automated review of fc1bd4e found nothing blocking.
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
Stack from ghstack (oldest at bottom):