Skip to content

Feat: support ONNX exportation and loading#103

Draft
shuheng-liu wants to merge 7 commits intomainfrom
feat/onnx-export
Draft

Feat: support ONNX exportation and loading#103
shuheng-liu wants to merge 7 commits intomainfrom
feat/onnx-export

Conversation

@shuheng-liu
Copy link
Collaborator

What this does

Explain what this PR does. Feel free to tag your PR with the appropriate label(s).

Examples:

Title Label
Fixes #[issue] (🐛 Bug)
Adds new Feature (🗃️ Feature)
Optimizes something (⚡️ Performance)
Updates docs (📝 Documentation)

How it was tested

Explain/show how you tested your changes.

Examples:

  • Added test_something in tests/test_stuff.py.
  • Added new_feature and checked that training converges with policy X on dataset/environment Y.
  • Optimized some_function, it now runs X times faster than previously.

How to checkout & try? (for the reviewer)

Provide a simple way for the reviewer to try out your changes.

Examples:

pytest -sx tests/test_stuff.py::test_something
python src/opentau/scripts/train.py --some.option=true
opentau-train --config_path=configs/examples/pi05_training_config.json

Checklist

  • I have added Google-style docstrings to important functions and ensured function parameters are typed.
  • My PR includes policy-related changes.
    • If the above is checked: I have run the GPU pytests (pytest -m "gpu") and regression tests.

Note: Before submitting this PR, please read the contributor guideline.

Copilot AI review requested due to automatic review settings February 4, 2026 22:39
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds ONNX export and inference capabilities for the PI05 policy model. The implementation separates traceable neural network operations (exported to ONNX) from non-traceable operations like tokenization and state discretization (handled externally in Python).

Changes:

  • Adds onnx_inference.py script for running inference with exported ONNX models
  • Refactors export_to_onnx.py to support PI05 using the Dynamo ONNX exporter with pre-tokenized inputs
  • Updates dtype handling in PI05 model files to use float32 during ONNX export instead of bfloat16

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
src/opentau/scripts/onnx_inference.py New inference script for ONNX models with pre-tokenization, image preprocessing, and action sampling
src/opentau/scripts/export_to_onnx.py Refactored to export PI05 models with pre-tokenized inputs using Dynamo exporter
src/opentau/policies/pi05/paligemma_with_expert.py Fixed Cache import from transformers; added dtype selection for ONNX export; guards bfloat16 casting during compilation
src/opentau/policies/pi05/modeling_pi05.py Added dtype selection function for ONNX export compatibility
src/opentau/policies/normalize.py Skips validation assertions during ONNX export and torch compilation

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Returns:
Action tensor of shape (batch, n_action_steps, action_dim).
"""
print("Starting forward pass of the wrapper...")
Copy link

Copilot AI Feb 4, 2026

Choose a reason for hiding this comment

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

Debug print statements should be removed or replaced with proper logging. These print statements will be executed during ONNX export tracing, which may clutter the output. Consider using logging.debug() instead.

Copilot uses AI. Check for mistakes.
# `policy.model.forward` returns a (batch_size, n_action_steps, action_dim) tensor, but the queue
# effectively has shape (n_action_steps, batch_size, *), hence the transpose.
actions = actions.transpose(0, 1)
print("Finished forward pass of the wrapper")
Copy link

Copilot AI Feb 4, 2026

Choose a reason for hiding this comment

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

Debug print statement should be removed or replaced with proper logging. Consider using logging.debug() instead.

Copilot uses AI. Check for mistakes.
Comment on lines +116 to +118
# `policy.model.forward` returns a (batch_size, n_action_steps, action_dim) tensor, but the queue
# effectively has shape (n_action_steps, batch_size, *), hence the transpose.
actions = actions.transpose(0, 1)
Copy link

Copilot AI Feb 4, 2026

Choose a reason for hiding this comment

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

The comment states "policy.model.forward returns a (batch_size, n_action_steps, action_dim) tensor" but this is inside the forward method of the wrapper, and the code is calling policy.model.sample_actions, not policy.model.forward. The comment appears to be outdated or incorrect. Additionally, the transpose operation converts from (batch, n_action_steps, action_dim) to (n_action_steps, batch, action_dim), which contradicts the ONNX export that declares the output as "actions" without specifying this transposition in the documentation.

Copilot uses AI. Check for mistakes.
Comment on lines +105 to +109
providers = provider or (
["CUDAExecutionProvider", "CPUExecutionProvider"]
if ort.get_device() == "GPU"
else ["CPUExecutionProvider"]
)
Copy link

Copilot AI Feb 4, 2026

Choose a reason for hiding this comment

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

The provider parameter is a string but is assigned directly to providers which expects a list. When a user passes a string (e.g., "CUDAExecutionProvider"), it will be used as-is instead of being wrapped in a list. This should be: providers = [provider] if provider else (...)

Copilot uses AI. Check for mistakes.
*images: Variable number of image tensors, each of shape (batch, 3, H, W).

Returns:
Action tensor of shape (batch, n_action_steps, action_dim).
Copy link

Copilot AI Feb 4, 2026

Choose a reason for hiding this comment

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

The docstring states the return shape is (batch, n_action_steps, action_dim), but the actual return shape after the transpose on line 118 is (n_action_steps, batch, action_dim). This inconsistency will confuse users of the exported ONNX model. The docstring should be updated to match the actual output shape.

Suggested change
Action tensor of shape (batch, n_action_steps, action_dim).
Action tensor of shape (n_action_steps, batch, action_dim).

Copilot uses AI. Check for mistakes.
@shuheng-liu shuheng-liu marked this pull request as draft February 4, 2026 22:57
@shuheng-liu shuheng-liu changed the title Feat: support exporting model to ONNX and load it Feat: support ONNX exportation and loading Feb 4, 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.

1 participant