Repository navigation
[RLlib] Fix the LSTM encoder's ONNX export under torch 2.10 - #66721
Merged
ArturNiederfahrenhorst merged 2 commits intoOct 6, 2026
Merged
ArturNiederfahrenhorst merged 2 commits into
ArturNiederfahrenhorst merged 2 commits into
Conversation
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
marked this pull request as ready for review
October 5, 2026 13:48
Contributor
There was a problem hiding this comment.
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.
ArturNiederfahrenhorst
enabled auto-merge (squash)
October 5, 2026 15:07
ArturNiederfahrenhorst
disabled auto-merge
October 5, 2026 15:40
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
The torch 2.10 bump (#65909) broke
examples/inference/policy_inference_after_training_w_connector_w_onnx: onnxruntime rejects the exported LSTM RLModule withBefore torch 2.13,
torch.exportrecordsnn.LSTM's state outputs with a spurious extra dim (pytorch/pytorch#151200).TorchLSTMEncodertransposes 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.
TorchLSTMEncoderreshapes 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):
Without the encoder change, both fail with the error above.
Written with AI assistance (Claude Code).