Skip to content

Run ONNX recurrent nodes with sequence-first modules - #18

Merged
Toni-SM merged 8 commits into
NVIDIA:developfrom
jvonmuralt:onnx-recurrent-sequence-first
Nov 27, 2026
Merged

Toni-SM merged 8 commits into
NVIDIA:developfrom
jvonmuralt:onnx-recurrent-sequence-first

Conversation

@jvonmuralt

@jvonmuralt jvonmuralt commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Description

Partially addresses #9.

The ONNX runtime ran its RNN/GRU/LSTM nodes with batch-first modules, so every call launched layout conversion kernels between the ONNX (seq, batch, features) layout and the modules' layout.

This PR:

  • Adds a PyTorch-compatible batch_first argument (default True) to the RNN, GRU and LSTM modules.
  • Builds the runtime's recurrent modules with batch_first=False. The input sequence is no longer converted, for any sequence length. For one direction, the output Y is a reshaped view of the module output. This replaces the single-time-step views from #<Toni's change>, which only covered seq_length == 1.
  • Keeps conversion kernels only where they are needed: reverse directions and the bidirectional output interleave.
  • Stores the final hidden state (and, for LSTM, the cell state) directly from the last time step's kernel, instead of separate copies per layer.
  • Falls back to the copy when the last-step kernel reads an array that overlaps the final states (e.g. h_n fed 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).

OnnxRuntime inference, CUDA graph replay, RTX 6000 Ada, best of 3 runs. Outputs are bit-identical to develop.

Model develop ff89d67 (µs) this PR (µs) speedup
GRU L1 H8 B1 T1 12.0 10.8 1.11×
GRU L2 H32 B64 T1 26.4 24.7 1.07×
GRU L2 H64 B4096 T1 90.3 88.4 1.02×
GRU L1 H32 B64 T8 91.9 83.3 1.10×
GRU L2 H64 B4096 T8 771.2 694.2 1.11×
GRU L1 H32 B64 T8 bidirectional 178.2 173.3 1.03×
LSTM L1 H8 B1 T1 16.0 15.7 1.02×
LSTM L2 H32 B64 T1 35.7 34.2 1.04×
LSTM L2 H64 B4096 T1 154.9 128.2 1.21×
LSTM L1 H32 B64 T8 127.6 117.1 1.09×
LSTM L2 H64 B4096 T8 1230.0 985.3 1.25×
LSTM L1 H32 B64 T8 bidirectional 252.5 244.0 1.03×

Newton's GRU actuator through OnnxRuntime now launches the same kernels as its direct GRUCell implementation (plus one Mul from the exported graph), with bit-identical forces.

Tests

  • Module tests run both layouts against PyTorch (forward and gradients).
  • New feedback tests feed the final states back as the next initial states or input (same arrays vs copies) for RNN, GRU and LSTM.
  • Runtime tests cover all three operators, sequence lengths 1 and 3 and all directions, and check which conversion and copy kernels run.
  • The full suite passes on CPU and CUDA (5289 passed).

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>
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>
@jvonmuralt

Copy link
Copy Markdown
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 (batch_first=False), for these reasons:

  • Any sequence length: the views only apply when seq_length == 1. With sequence-first modules, the input and (for one direction) the output need no conversion at any length, so T > 1 no longer launches 2 conversion kernels per layer.
  • No final-state copies: the last time step's kernel now writes the final hidden state (and, for the LSTM, the cell state) directly. Your commit already did this for the LSTM cell state; I extended it to the hidden state of all three modules. It falls back to the copy when the last step reads an array that overlaps the final states (e.g. h_n fed back as the next input or initial state), since other tile blocks may still read it.
  • Kept from your commit: the stacked-weight kernels and the unary element-wise fast path.

Kernel launches per call (2 layers, B=4096):

  • LSTM, T=1: develop 4 → this PR 2 (no hidden-state copies)
  • GRU, T=8: develop 22 → this PR 16 (no copies, no layout conversions)

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:

  • LSTM: 2 LSTM kernels, plus 2 copies on develop.
  • GRU: 16 step kernels, plus 2 copies and 4 conversions on develop.

@Toni-SM

Toni-SM commented Nov 27, 2026 •

Copy link
Copy Markdown
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

@Toni-SM Toni-SM closed this Nov 27, 2026
@Toni-SM Toni-SM reopened this Nov 27, 2026
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
Toni-SM force-pushed the onnx-recurrent-sequence-first branch from e55db1e to 27dda14 Compare November 27, 2026 18:05
@Toni-SM
Toni-SM merged commit 370f3c9 into NVIDIA:develop Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants