Add explicit batched inference for PyTorch pi0 policies#941
Open
taivu1998 wants to merge 1 commit into
Open
Conversation
wadeKeith
reviewed
May 12, 2026
wadeKeith
left a comment
Contributor
There was a problem hiding this comment.
Solid addition - the batched inference path addresses a real need. Policy.infer_batch() API is clean with good test coverage for sync and websocket paths. README and docs updated. LGTM! Reviewed by Hermes Agent.
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.
Summary
Adds an explicit batched inference path for policy users and websocket clients. The new
Policy.infer_batch([...])path applies existing input/output transforms per observation, stacks the transformed batch, and calls the model once with a leading batch dimension. Existinginfer(obs)behavior and shapes are preserved.This also adds
WebsocketClientPolicy.infer_batch([...])using an explicit{"batch": [...]}request payload, plus server-side routing for that payload.Root Cause
Issue #765 asks whether the PyTorch
pi0/pi0.5models support vectorized inference. The model core is batch-shaped, but the public policy/server path always treated incoming data as one observation: it transformed one dict, added a singleton batch dimension, sampled, and stripped index 0 from outputs. Passing already-batched observations therefore produced shapes like[1, B, ...]instead of true[B, ...]inference.Changes
BasePolicy.infer_batchthat raises by default instead of unsafe sequential fallback.Policy.infer_batchfor true batched sampling while keepingPolicy.infersingleton-compatible.PolicyRecorder.typing-extensionstoopenpi-clientmetadata because the client package already imports it.Validation
uv run --project packages/openpi-client pytest packages/openpi-client/src/openpi_client-> 24 passeduvx ruff check src/openpi/policies/policy.py src/openpi/policies/policy_test.py src/openpi/serving/websocket_policy_server.py src/openpi/serving/websocket_policy_server_test.py packages/openpi-client/src/openpi_client/base_policy.py packages/openpi-client/src/openpi_client/websocket_client_policy.py packages/openpi-client/src/openpi_client/websocket_client_policy_test.pyuvx ruff format --check src/openpi/policies/policy.py src/openpi/policies/policy_test.py src/openpi/serving/websocket_policy_server.py src/openpi/serving/websocket_policy_server_test.py packages/openpi-client/src/openpi_client/base_policy.py packages/openpi-client/src/openpi_client/websocket_client_policy.py packages/openpi-client/src/openpi_client/websocket_client_policy_test.pypython -m py_compile src/openpi/policies/policy.py src/openpi/policies/policy_test.py src/openpi/serving/websocket_policy_server.py src/openpi/serving/websocket_policy_server_test.py packages/openpi-client/src/openpi_client/base_policy.py packages/openpi-client/src/openpi_client/websocket_client_policy.py packages/openpi-client/src/openpi_client/websocket_client_policy_test.pyuv lock --checkgit diff --checkNote: root
uv run pytest src/openpi/policies/policy_test.py src/openpi/serving/websocket_policy_server_test.pycannot run on macOS arm64 becausejax-cuda12-plugin==0.5.3has no compatible wheel for this platform; it fails before test collection.Addresses #765.