Repository navigation
[RLlib] Fix policy_inference_after_training_w_connector.py onnx issue - #66697
Merged
ArturNiederfahrenhorst merged 1 commit intoOct 5, 2026
Conversation
Signed-off-by: Mark Towers <[email protected]>
Contributor
There was a problem hiding this comment.
Code Review
This pull request updates the ONNX export logic in the RLlib policy inference example to handle PyTorch version differences, conditionally using the Dynamo exporter for PyTorch 2.13+ and falling back to the legacy TorchScript exporter for older versions. Feedback was provided to prevent a TypeError on PyTorch versions older than 2.1, where the dynamo parameter is not supported, by conditionally including it in the export arguments.
ArturNiederfahrenhorst
approved these changes
Oct 5, 2026
nadongjun
pushed a commit
to nadongjun/ray
that referenced
this pull request
Oct 6, 2026
…ray-project#66697) ## Description `policy_inference_after_training_w_connector.py` started failing after we bumped pytorch to 2.10 The reason is that the onnx implementation has a bug that changed in 2.10 (raising the issue to us) and hasn't been fixed until 2.13 sadly (pytorch/pytorch#151200) where the onnx saved outputs have the wrong dimensions. As this hasn't been fixed upstream until torch 2.13, to fix the issue here, we just move to use `dynamo=False` which while deprecated hasn't been removed yet. Signed-off-by: Mark Towers <[email protected]> Co-authored-by: Mark Towers <[email protected]>
ArturNiederfahrenhorst
added a commit
that referenced
this pull request
Oct 6, 2026
## 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). --------- Signed-off-by: Artur Niederfahrenhorst <[email protected]> Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
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
policy_inference_after_training_w_connector.pystarted failing after we bumped pytorch to 2.10The reason is that the onnx implementation has a bug that changed in 2.10 (raising the issue to us) and hasn't been fixed until 2.13 sadly (pytorch/pytorch#151200) where the onnx saved outputs have the wrong dimensions.
As this hasn't been fixed upstream until torch 2.13, to fix the issue here, we just move to use
dynamo=Falsewhich while deprecated hasn't been removed yet.