Skip to content

[RLlib] Fix policy_inference_after_training_w_connector.py onnx issue - #66697

Merged
ArturNiederfahrenhorst merged 1 commit into
ray-project:masterfrom
pseudo-rnd-thoughts:rllib/fix-onnx-pytorch-bump
Oct 5, 2026
Merged

ArturNiederfahrenhorst merged 1 commit into
ray-project:masterfrom
pseudo-rnd-thoughts:rllib/fix-onnx-pytorch-bump

Conversation

@pseudo-rnd-thoughts

Copy link
Copy Markdown
Member

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.

@pseudo-rnd-thoughts
pseudo-rnd-thoughts requested a review from a team as a code owner October 3, 2026 15:21
@pseudo-rnd-thoughts pseudo-rnd-thoughts added rllib RLlib related issues go add ONLY when ready to merge, run all tests labels Oct 3, 2026

@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 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
ArturNiederfahrenhorst merged commit 961ce48 into ray-project:master Oct 5, 2026
5 checks passed
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]>
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