[Performance] LSTM/GRU scan: canonical strides + cuDNN flat-storage clones + thread-local recurrent mode#3754
Merged
Merged
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/3754
Note: Links to docs will display an error until the docs builds have been completed. ❗ 1 Active SEVsThere are 1 currently active SEVs. If your PR is affected, please view them below: ❌ 3 New Failures, 1 Cancelled JobAs of commit 244f61c with merge base 0a01ee8 ( NEW FAILURES - The following jobs have failed:
CANCELLED JOB - The following job was cancelled. Please retry:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This was referenced May 15, 2026
vmoens
commented
May 15, 2026
Comment on lines
+1137
to
+1138
| hidden_per_step = _canonical_contiguous(hidden0_in[..., layer, :]) | ||
| cell_per_step = _canonical_contiguous(hidden1_in[..., layer, :]) |
Collaborator
Author
There was a problem hiding this comment.
what if we were doing that for hidden0_in before the loop? Would the view still be contiguous?
vmoens
commented
May 15, 2026
| b_hh = zeros if b_hh is None else b_hh | ||
|
|
||
| hidden_per_step = hidden_in[..., layer, :] | ||
| hidden_per_step = _canonical_contiguous(hidden_in[..., layer, :]) |
Collaborator
Author
There was a problem hiding this comment.
ditto, should we do it before the loop?
This was referenced May 15, 2026
This was referenced May 17, 2026
vmoens
added a commit
that referenced
this pull request
May 18, 2026
…lones + thread-local recurrent mode Three intertwined fixes to the scan / triton recurrent backends. - Canonical-stride check. A `[1, 4, 5]` tensor with strides `(5, 5, 1)` passes `is_contiguous()` but `torch._higher_order_ops.scan` and the triton kernels read strides directly and reject non-canonical layouts. Add `_canonical_stride` + `_canonical_contiguous` and re-materialize inputs / hidden buffers when strides drift off the C-canonical layout. - cuDNN flat-storage aliasing. `nn.LSTM` / `nn.GRU` with cuDNN flatten all per-layer parameters into a single storage; the scan HOP tracer walks the FakeTensor graph and rejects the aliased per-layer views as inputs. Clone the weight views before closing the scan body. The per-layer carry now also clones `x_t` and the transpose+flatten output (the only remaining aliasing edge) so the existing `.clone()` on the full `torch.stack(...)` carry can drop. - Thread-local `recurrent_mode`. `_ContextManager` was a single mutable module-level flag, so spawning a collector worker thread saw the parent's recurrent_mode setting. Wrap in `_RecurrentModeContextManager` using `contextvars.ContextVar` so per-thread state is isolated. Tests cover: `_canonical_contiguous` no-op vs re-materialize, scan/triton output parity under non-canonical strides, scan-under-torch.compile aliasing pin for both LSTM and GRU, LSTM scan/pad PPO-advantage parity, non-canonical hidden-buffer regression for scan, and a thread-local `set_recurrent_mode` test. ghstack-source-id: 40fca02 Pull-Request: #3754 Co-authored-by: Cursor <cursoragent@cursor.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stack from ghstack (oldest at bottom):
Three intertwined fixes to the scan / triton recurrent backends.
Canonical-stride check. A
[1, 4, 5]tensor with strides(5, 5, 1)passes
is_contiguous()buttorch._higher_order_ops.scanand thetriton kernels read strides directly and reject non-canonical layouts.
Add
_canonical_stride+_canonical_contiguousand re-materializeinputs / hidden buffers when strides drift off the C-canonical layout.
cuDNN flat-storage aliasing.
nn.LSTM/nn.GRUwith cuDNN flattenall per-layer parameters into a single storage; the scan HOP tracer
walks the FakeTensor graph and rejects the aliased per-layer views as
inputs. Clone the weight views before closing the scan body. The
per-layer carry now also clones
x_tand the transpose+flatten output(the only remaining aliasing edge) so the existing
.clone()on thefull
torch.stack(...)carry can drop.Thread-local
recurrent_mode._ContextManagerwas a single mutablemodule-level flag, so spawning a collector worker thread saw the
parent's recurrent_mode setting. Wrap in
_RecurrentModeContextManagerusing
contextvars.ContextVarso per-thread state is isolated.Tests cover:
_canonical_contiguousno-op vs re-materialize, scan/tritonoutput parity under non-canonical strides, scan-under-torch.compile
aliasing pin for both LSTM and GRU, LSTM scan/pad PPO-advantage parity,
non-canonical hidden-buffer regression for scan, and a thread-local
set_recurrent_modetest.