Repository navigation
Run ONNX recurrent nodes with sequence-first modules - #18
Merged
Toni-SM merged 8 commits intoNov 27, 2026
Merged
Conversation
Add a PyTorch-compatible `batch_first` argument to the RNN, GRU and LSTM modules, so they can process sequence-first (sequence_length, batch_size, features) sequences. The ONNX runtime now builds its recurrent modules with batch_first=False, which matches the ONNX layout. The input sequence no longer needs a layout conversion, and for one direction the output sequence is a reshaped view of the module output. Conversion kernels remain only for reverse directions and the bidirectional output. Partially addresses NVIDIA#9. Signed-off-by: Julia von Muralt <jvonmuralt@nvidia.com>
4 tasks
Remove the unreachable copy of non-contiguous recurrent inputs: the runtime only passes contiguous arrays to its operations. Note in the module docstrings that batch_first defaults to batch-first, unlike PyTorch. Also assert in the layout test that the input is not copied. Signed-off-by: Julia von Muralt <jvonmuralt@nvidia.com>
The kernel of the last time step now also stores the hidden state into the final hidden state array, so the RNN and GRU modules no longer launch a copy per layer and direction. When a single time step reads the final hidden state array as its initial hidden state, the copy is kept, since other blocks of the kernel may still load it. Signed-off-by: Julia von Muralt <jvonmuralt@nvidia.com>
…equence-first Signed-off-by: Julia von Muralt <jvonmuralt@nvidia.com> # Conflicts: # tests/runtime/test_onnx_runtime_v2.py # warp_nn/modules/layers/lstm.py # warp_nn/runtime/_onnx_compilers.py
Signed-off-by: Julia von Muralt <jvonmuralt@nvidia.com>
Contributor
Author
|
@Toni-SM I saw that ff89d67 converts the recurrent layouts with views for a single time step. I merged develop and replaced those views with the sequence-first modules (
Kernel launches per call (2 layers, B=4096):
Timings vs develop ff89d67 (CUDA graph replay, RTX 6000 Ada, bit-identical outputs): 2–21% faster at T=1 and 9–25% faster at T=8. The full table is in the description. I wrote the launch counts in the comment per layer, not as raw spy output:
|
# Conflicts: # tests/runtime/test_onnx_runtime_v2.py
Collaborator
|
Closed in favor of the same PR opened internally to use GPU runners: 6571ecc. It has also been merged into GitHub to credit the author |
The changes of this pull request were already merged into develop (6571ecc). Take the develop tree so the pull request merges cleanly while keeping the contributor commits in the history.
Toni-SM
force-pushed
the
onnx-recurrent-sequence-first
branch
from
November 27, 2026 18:05
e55db1e to
27dda14
Compare
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.
Description
Partially addresses #9.
The ONNX runtime ran its
RNN/GRU/LSTMnodes with batch-first modules, so every call launched layout conversion kernels between the ONNX(seq, batch, features)layout and the modules' layout.This PR:
batch_firstargument (defaultTrue) to theRNN,GRUandLSTMmodules.batch_first=False. The input sequence is no longer converted, for any sequence length. For one direction, the outputYis a reshaped view of the module output. This replaces the single-time-step views from #<Toni's change>, which only coveredseq_length == 1.LSTM, the cell state) directly from the last time step's kernel, instead of separate copies per layer.h_nfed back as the next input, or as the initial state for a single time step), since other tile blocks may still read it.Merged with
develop(ff89d67): its stacked-weight kernels and unary element-wise fast path are kept as is.It does not address the rest of #9 (small-MLP overhead, load time).
OnnxRuntimeinference, CUDA graph replay, RTX 6000 Ada, best of 3 runs. Outputs are bit-identical todevelop.Newton's GRU actuator through
OnnxRuntimenow launches the same kernels as its directGRUCellimplementation (plus oneMulfrom the exported graph), with bit-identical forces.Tests
RNN,GRUandLSTM.