perf: optimize g1 motion tracking state updates - #492
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR optimizes the G1 motion tracking “hot path” by reducing per-step allocations and repeated temporary array creation, mainly by reusing preallocated buffers and adding backend copy helpers to support efficient state reads across MuJoCo and Motrix.
Changes:
- Reworked
G1MotionTrackingEnv.update_state()/ obs / reward paths to reuse buffers and avoidtile/concatenateallocations. - Added reusable motion gather buffers (
MotionDataout-params) and backendcopy_body_state_w()helpers, plus row-wise sensor/body getters. - Added/expanded unit and smoke tests to enforce equivalence of the optimized math paths (relative transforms, anchor-frame writers, reward).
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| tests/utils/test_math_utils.py | Adds tests validating new batched quaternion/transform helpers match reference implementations. |
| tests/envs/test_env_configs.py | Extends env config/unit tests to cover new optimized fast paths and required buffers/flags. |
| tests/base/test_sim_backend_smoke.py | Adds backend smoke tests for copy_body_state_w() (MuJoCo & Motrix) and row-wise sensor equivalence. |
| src/unilab/envs/motion_tracking/g1/tracking.py | Main optimization: preallocated buffers, faster relative-transform math, reward computation reuse, reduced temporary allocations. |
| src/unilab/envs/motion_tracking/g1/motion_loader.py | Adds reusable MotionData buffer allocation and out parameter to avoid repeated allocations. |
| src/unilab/envs/motion_tracking/g1/motion_box_loader.py | Extends box motion loader to accept out buffers and optionally fill object state. |
| src/unilab/envs/common/rotation.py | Adds batched quaternion ops and anchor-frame transform helpers to avoid flatten/tile allocations. |
| src/unilab/base/backend/mujoco/backend.py | Adds copy_body_state_w(), get_body_pose_w_rows(), and get_sensor_data_rows() for efficient state/sensor reads. |
| src/unilab/base/backend/motrix/backend.py | Adds copy_body_state_w(), get_body_pose_w_rows(), and get_sensor_data_rows() for efficient state/sensor reads. |
| src/unilab/base/backend/base.py | Defines default get_body_state_w(), copy_body_state_w(), row-wise body pose getters, and row-wise sensor getter in base backend API. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
95
to
+119
| @@ -103,6 +105,18 @@ def get_motion_at_frame(self, frame_idx: np.ndarray) -> BoxMotionData: | |||
| body_lin_vel_w=base.body_lin_vel_w, | |||
| body_ang_vel_w=base.body_ang_vel_w, | |||
| ) | |||
| if ( | |||
| isinstance(out, BoxMotionData) | |||
| and out.object_pos_w is not None | |||
| and out.object_quat_w is not None | |||
| and out.object_lin_vel_w is not None | |||
| and out.object_ang_vel_w is not None | |||
| ): | |||
| np.take(self.object_pos_w, frame_idx, axis=0, out=out.object_pos_w) | |||
| np.take(self.object_quat_w, frame_idx, axis=0, out=out.object_quat_w) | |||
| np.take(self.object_lin_vel_w, frame_idx, axis=0, out=out.object_lin_vel_w) | |||
| np.take(self.object_ang_vel_w, frame_idx, axis=0, out=out.object_ang_vel_w) | |||
| return out | |||
TATP-233
approved these changes
May 25, 2026
TATP-233
added a commit
that referenced
this pull request
May 30, 2026
perf: optimize g1 motion tracking state updates
TATP-233
added a commit
that referenced
this pull request
May 30, 2026
perf: optimize g1 motion tracking state updates
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
Benchmark
Command shape:
Baseline: upstream/main 367d9c4
Current: optimize/g1-mt-update-state 455ecea
Raw benchmark outputs:
Validation
Additional run: