Skip to content

[RLlib] Fix the LSTM encoder's ONNX export under torch 2.10 - #66721

Merged
ArturNiederfahrenhorst merged 2 commits into
ray-project:masterfrom
ArturNiederfahrenhorst:rllib-onnx-lstm-dynamic-batch
Oct 6, 2026
Merged

ArturNiederfahrenhorst merged 2 commits into
ray-project:masterfrom
ArturNiederfahrenhorst:rllib-onnx-lstm-dynamic-batch

Conversation

@ArturNiederfahrenhorst

@ArturNiederfahrenhorst ArturNiederfahrenhorst commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Description

The torch 2.10 bump (#65909) broke examples/inference/policy_inference_after_training_w_connector_w_onnx: onnxruntime rejects the exported LSTM RLModule with

Node (node_transpose_3) Op (Transpose) [TypeInferenceError] Invalid attribute perm {1, 0, 2, 3}, input shape = {1, 0, 256}

Before torch 2.13, torch.export records nn.LSTM's state outputs with a spurious extra dim (pytorch/pytorch#151200). TorchLSTMEncoder transposes the states back to batch-first, so the export carries a 4-D Transpose for a 3-D tensor. On torch 2.9 this only hit exports from a batch of two or more, and the example exports from its batch of one. Torch 2.10 breaks that case too.

#66697 worked around it in the example: the legacy TorchScript exporter (dynamo=False) below torch 2.13, the dynamo exporter from 2.13 on. That keeps the deprecated exporter on CI's torch. And from 2.13 on, the export from the example's batch of one fixes the batch dim at one, so the exported model rejects any other batch size.

This PR fixes the encoder instead. TorchLSTMEncoder reshapes the states to their (num_layers, B, hidden_dim) shape before transposing them. That is a no-op in eager mode and gives the export the right rank. The example goes back to its single dynamo-based export, without the version check.

Testing

Locally with CI's versions (torch 2.10.0, onnx 1.22.0, onnxscript 0.7.1, onnxruntime 1.24.4):

pytest python/ray/rllib/core/models/tests/test_recurrent_encoders.py   # 3 passed, also with onnxruntime 1.20.0 (py3.10 CI image)
python python/ray/rllib/examples/inference/policy_inference_after_training_w_connector.py \
    --stop-reward=150.0 --use-onnx-for-inference --num-gpus-per-learner=0   # exit 0, 10 episodes through onnxruntime

Without the encoder change, both fail with the error above.

Written with AI assistance (Claude Code).

ArturNiederfahrenhorst and others added 2 commits October 5, 2026 04:30
Before torch 2.13, `torch.export` records `nn.LSTM`'s state outputs with a
spurious extra dim (pytorch/pytorch#151200). `TorchLSTMEncoder` transposes the
states back to batch-first, so an ONNX export with `dynamo=True` carried a 4-D
Transpose for a 3-D tensor, which onnxruntime rejects. Since the torch 2.10
bump (ray-project#65909), this broke
`examples/inference/policy_inference_after_training_w_connector_w_onnx`.

Reshape the states to their (num_layers, B, hidden_dim) shape before the
transpose. That is a no-op in eager mode and gives the export the right rank,
with a dynamic batch dim. The example goes back to its single dynamo-based
export, replacing the legacy-exporter workaround from ray-project#66697, and a new test
exports an LSTM encoder and checks onnxruntime against torch at two batch sizes.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Signed-off-by: Artur Niederfahrenhorst <[email protected]>
Calling the `ONNXProgram` that `torch.onnx.export(..., dynamo=True)` returns
runs it in onnxruntime, so the test needs no temp file, `InferenceSession` or
input names. One configuration (2 layers) checked at one other batch size (3)
covers the fix; without it, the test still fails with onnxruntime's Transpose
error.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Signed-off-by: Artur Niederfahrenhorst <[email protected]>
@ArturNiederfahrenhorst
ArturNiederfahrenhorst marked this pull request as ready for review October 5, 2026 13:48
@ArturNiederfahrenhorst
ArturNiederfahrenhorst requested a review from a team as a code owner October 5, 2026 13:48

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request resolves an issue with exporting PyTorch LSTM encoders to ONNX using Dynamo (dynamo=True) on PyTorch versions prior to 2.13. It explicitly reshapes the LSTM state outputs to ensure correct ranks during export, adds a corresponding unit test for dynamic batch ONNX export, and simplifies the inference example by removing legacy TorchScript exporter fallbacks. There are no review comments to address, and no additional feedback is provided.

@pseudo-rnd-thoughts pseudo-rnd-thoughts left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@pseudo-rnd-thoughts pseudo-rnd-thoughts added rllib RLlib related issues go add ONLY when ready to merge, run all tests labels Oct 5, 2026
@ArturNiederfahrenhorst
ArturNiederfahrenhorst enabled auto-merge (squash) October 5, 2026 15:07
@ArturNiederfahrenhorst
ArturNiederfahrenhorst merged commit 62d243a into ray-project:master Oct 6, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

go add ONLY when ready to merge, run all tests rllib RLlib related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants