Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ jobs:
sh install.sh --all --python 3.10
else
# Unitree/DDS runtime support remains limited to Python 3.10.
uv sync --frozen --python '${{ matrix.python }}' --extra cuda --extra rslrl --extra skrl-jax --group test
uv sync --frozen --python '${{ matrix.python }}' --extra cuda --extra rslrl --extra appo --extra skrl-jax --group test
fi
.venv/bin/python -c 'import sys; assert ".".join(map(str, sys.version_info[:2])) == "${{ matrix.python }}"'

Expand Down
1 change: 1 addition & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ MotrixLab 是构建在 MotrixSim 仿真后端之上的强化学习框架,提
- `motrix_rl_supermimic`:独立的 SuperMimic world-model MBRL 实现与 provider,由 builtin 集成注册
- `motrix_rl_skrl`:SKRL PPO provider 插件(Torch 必需,额外 JAX runtime 可选)
- `motrix_rl_rslrl`:RSLRL PPO provider 插件(Torch)
- `motrix_rl_appo`:APPO 异步 PPO provider 插件(Torch,V-trace / IMPACT)
- `motrix_deploy`:框架无关的 artifact、运行时契约与部署 CLI
- `motrix_deploy_mujoco`:SceneCfg 支撑的 MuJoCo 后端插件
- `motrix_deploy_motrixsim`:SceneCfg 支撑的原生 MotrixSim 部署后端插件
Expand Down
20 changes: 20 additions & 0 deletions configs/task/cartpole/appo.ppo.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# @package _global_
defaults:
- /algo_base@algo: appo.ppo
- _self_
task:
env: cartpole
rllib: appo
algo: ppo
train_backend: null
num_envs: 64
play_num_envs: 16
seed: 1
algo:
rollout_steps: 16
max_iterations: 300
actor:
hidden_dims: [32, 32]
init_noise_std: 1.0
critic:
hidden_dims: [32, 32]
22 changes: 22 additions & 0 deletions configs/task/go2-walk-flat/appo.ppo.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# @package _global_
# Same environment count, rollout length and network capacity as rslrl.ppo.
defaults:
- /algo_base@algo: appo.ppo
- _self_
task:
env: go2-walk-flat
rllib: appo
algo: ppo
train_backend: null
num_envs: 1024
play_num_envs: 16
seed: 1
checkpoint:
interval: 100
algo:
rollout_steps: 24
max_iterations: 2000
algorithm:
learning_rate: 0.0003
desired_kl: 0.01
entropy_coef: 0.001
13 changes: 10 additions & 3 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# CPU-only hosts fall back to cuda.
#
# Built-in FastSAC and SKRL on Torch are always installed. --skrl-jax adds
# the JAX runtime, and --rslrl adds the RSLRL plugin; multiple flags combine.
# the JAX runtime, --rslrl adds RSLRL, and --appo adds asynchronous PPO; flags combine.

set -e

Expand All @@ -28,6 +28,7 @@ usage() {
echo " --skrl-torch Training backend: SKRL on PyTorch (default)"
echo " --skrl-jax Training backend: SKRL on JAX (Linux only)"
echo " --rslrl Training backend: RSL-RL on PyTorch"
echo " --appo Training backend: asynchronous PPO on PyTorch"
echo " -h, --help Show this help"
}

Expand All @@ -36,6 +37,7 @@ GPU=""
PYTHON=""
SKRL_JAX=""
RSLRL=""
APPO=""
DOCS=""

while [ $# -gt 0 ]; do
Expand Down Expand Up @@ -68,6 +70,9 @@ while [ $# -gt 0 ]; do
--rslrl)
RSLRL=1
;;
--appo)
APPO=1
;;
-h|--help)
usage
exit 0
Expand Down Expand Up @@ -223,10 +228,11 @@ if [ "$GPU" = hygon ]; then
trap 'rm -f "$REQUIREMENTS"' EXIT HUP INT TERM
set -- --frozen --no-hashes --no-header --no-annotate
if [ -n "$ALL" ]; then
set -- "$@" --all-packages --all-groups --extra rslrl --extra unitree --extra docs
set -- "$@" --all-packages --all-groups --extra rslrl --extra appo --extra unitree --extra docs
else
set -- "$@" --no-default-groups
[ -n "$RSLRL" ] && set -- "$@" --extra rslrl
[ -n "$APPO" ] && set -- "$@" --extra appo
[ -n "$DOCS" ] && set -- "$@" --extra docs
fi
# Prune transitive accelerator dependencies too. --no-deps below prevents
Expand All @@ -240,7 +246,7 @@ if [ "$GPU" = hygon ]; then
elif [ -n "$ALL" ]; then
if [ "$GPU" = rocm ]; then
set -x
uv sync "$@" --all-packages --all-groups --extra rocm --extra rslrl --extra unitree --extra docs
uv sync "$@" --all-packages --all-groups --extra rocm --extra rslrl --extra appo --extra unitree --extra docs
else
set -x
uv sync "$@" --all-packages --all-groups --all-extras --no-extra rocm
Expand All @@ -249,6 +255,7 @@ else
EXTRAS=""
[ -n "$SKRL_JAX" ] && EXTRAS="$EXTRAS --extra skrl-jax"
[ -n "$RSLRL" ] && EXTRAS="$EXTRAS --extra rslrl"
[ -n "$APPO" ] && EXTRAS="$EXTRAS --extra appo"
[ -n "$DOCS" ] && EXTRAS="$EXTRAS --extra docs"
set -x
uv sync "$@" --no-default-groups --extra "$GPU"$EXTRAS
Expand Down
19 changes: 15 additions & 4 deletions motrix_envs/src/motrix_envs/locomotion/quadruped/walk_np.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import torch
from motrix_robots.quadruped import QuadrupedRobotCfg

from motrix_env_core.array.env import ArrayEnvState
from motrix_env_core.array.env import ArrayEnvState, EnvObs, FinalObs
from motrix_env_core.direct.env import DirectEnv
from motrix_env_core.mdp.action_space import asymmetric_residual_action_space
from motrix_env_core.observations import ObservationGroupSpec, ObservationSpec
Expand Down Expand Up @@ -457,7 +457,8 @@ def apply_action(self, actions: torch.Tensor, state: ArrayEnvState) -> None:
self._ctrl_writes.execute()
return None

def compute_observation(self, state: ArrayEnvState) -> None:
def _build_observation(self) -> EnvObs:
"""Read refreshed task inputs without publishing or advancing task state."""
noise_cfg = self.cfg.noise_config
diff = self.get_dof_pos() - self.default_angles
noisy_gyro = self._obs_noise(self.get_gyro(), noise_cfg.scale_gyro)
Expand Down Expand Up @@ -490,8 +491,18 @@ def compute_observation(self, state: ArrayEnvState) -> None:
f"expected policy[{expected_policy}]; value={tuple(value.shape)}, "
f"expected value[{expected_value}]"
)
state.obs.policy.copy_(policy)
state.obs.value.copy_(value)
return EnvObs({"policy": policy, "value": value})

def compute_observation(self, state: ArrayEnvState) -> None:
observation = self._build_observation()
state.obs.policy.copy_(observation.policy)
state.obs.value.copy_(observation.value)

def _capture_final_obs(self, env_ids: torch.Tensor) -> FinalObs:
# Use the same pre-reset inputs and default noise stream as normal
# observation publication, but never write its alternating buffers.
observation = self._build_observation()
return FinalObs(env_ids.clone(), EnvObs({name: rows[env_ids] for name, rows in observation.groups.items()}))

def compute_transition(self, state: ArrayEnvState) -> None:
self.sim_data.execute()
Expand Down
101 changes: 101 additions & 0 deletions motrix_envs/tests/test_quadruped_final_observation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Copyright Motphys Technology Co., Ltd. 2025, 2026
# SPDX-License-Identifier: Apache-2.0

"""Pure task-input tests: no simulator is needed to protect snapshot semantics."""

import pytest
import torch

from motrix_env_core.array.env import ArrayEnvState, EnvObs
from motrix_envs.locomotion.quadruped.go2 import Go2WalkDirectEnvCfg
from motrix_envs.locomotion.quadruped.walk_np import QuadrupedWalkTask


class ObservationTask:
_build_observation = QuadrupedWalkTask._build_observation
_capture_final_obs = QuadrupedWalkTask._capture_final_obs
compute_observation = QuadrupedWalkTask.compute_observation
_obs_noise = QuadrupedWalkTask._obs_noise
_policy_obs_dim = QuadrupedWalkTask._policy_obs_dim
_value_obs_dim = QuadrupedWalkTask._value_obs_dim
get_dof_pos = QuadrupedWalkTask.get_dof_pos
get_dof_vel = QuadrupedWalkTask.get_dof_vel
get_local_linvel = QuadrupedWalkTask.get_local_linvel
get_gyro = QuadrupedWalkTask.get_gyro
get_gravity = QuadrupedWalkTask.get_gravity

def __init__(self, noise: bool) -> None:
self.cfg = Go2WalkDirectEnvCfg()
self.cfg.noise_config.level = 1.0 if noise else 0.0
self._num_action = 2
self._num_feet = 4
self.default_angles = torch.zeros(2)
self.sim_data = {
"joint_dof_pos": torch.arange(6, dtype=torch.float32).reshape(3, 2),
"joint_dof_vel": torch.arange(6, dtype=torch.float32).reshape(3, 2) + 10,
"local_linvel": torch.arange(9, dtype=torch.float32).reshape(3, 3) + 20,
"gyro": torch.arange(9, dtype=torch.float32).reshape(3, 3) + 30,
"upvector": torch.arange(9, dtype=torch.float32).reshape(3, 3) + 40,
}
self._commands = torch.arange(9, dtype=torch.float32).reshape(3, 3) + 50
self._current_actions = torch.arange(6, dtype=torch.float32).reshape(3, 2) + 60
self._feet_phase = torch.arange(12, dtype=torch.float32).reshape(3, 4) + 70
self._phase = torch.arange(3, dtype=torch.float32)


def empty_state(task: ObservationTask) -> ArrayEnvState:
return ArrayEnvState(
EnvObs(
{
"policy": torch.full((3, task._policy_obs_dim()), -100.0),
"value": torch.full((3, task._value_obs_dim()), -200.0),
}
),
torch.zeros(3),
torch.zeros(3, dtype=torch.bool),
torch.zeros(3, dtype=torch.bool),
torch.zeros(3, dtype=torch.long),
)


@pytest.mark.parametrize("noise", [False, True])
def test_final_observation_is_compact_owned_and_does_not_publish(noise: bool) -> None:
task = ObservationTask(noise)
state = empty_state(task)
before = {name: rows.clone() for name, rows in state.obs.groups.items()}
inputs = {name: rows.clone() for name, rows in task.sim_data.items()}
commands, phase, feet_phase = task._commands.clone(), task._phase.clone(), task._feet_phase.clone()
ids = torch.tensor([2, 0])
initial_rng = torch.get_rng_state()
final = task._capture_final_obs(ids)
final_rng = torch.get_rng_state()
for name, rows in state.obs.groups.items():
torch.testing.assert_close(rows, before[name])
# Replaying the same default-stream samples must reproduce a normal
# observation at the identical pre-reset state; capture itself advances it.
torch.set_rng_state(initial_rng)
task.compute_observation(state)
assert torch.equal(torch.get_rng_state(), final_rng)
if noise:
assert not torch.equal(initial_rng, final_rng)
for name, rows in final.obs.groups.items():
torch.testing.assert_close(rows, state.obs.groups[name][ids])
assert final.obs.policy.shape == (2, task._policy_obs_dim())
assert final.obs.value.shape == (2, task._value_obs_dim())
if not noise:
torch.testing.assert_close(final.obs.value[:, -3:], inputs["local_linvel"][ids])
for name, rows in task.sim_data.items():
torch.testing.assert_close(rows, inputs[name])
torch.testing.assert_close(task._commands, commands)
torch.testing.assert_close(task._phase, phase)
torch.testing.assert_close(task._feet_phase, feet_phase)
saved = {name: rows.clone() for name, rows in final.obs.groups.items()}
ids.zero_()
for rows in task.sim_data.values():
rows.zero_()
task._commands.zero_()
task._current_actions.zero_()
task.compute_observation(state)
assert final.env_ids.tolist() == [2, 0]
for name, rows in final.obs.groups.items():
torch.testing.assert_close(rows, saved[name])
24 changes: 24 additions & 0 deletions motrix_rl_appo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# motrix_rl_appo

Torch asynchronous PPO provider for MotrixLab, using V-trace value correction,
an IMPACT-style clipped importance-weighted policy objective, and a target actor.
The collector and learner run in separate processes with a bounded shared-memory
rollout queue. This package does not depend on Ray or RSL-RL.

Install from the workspace with `sh install.sh --appo` (add `--rslrl` for the
comparison baseline), then run:

```sh
mxlab train task=cartpole/appo.ppo
mxlab train task=go2-walk-flat/appo.ppo
```

The plugin owns its structured `algo_base/appo.ppo` configuration. The collector
handles all timeout final-observation bootstraps locally; the learner uses explicit
chunk endpoints. Checkpoints support warm resume, not exact restoration of the
collector's environment trajectory. Environments must supply pre-reset final
observations when they truncate (the included Go2 task supports this). Playback
uses the policy mean. ONNX export, multi-collector training, and compiled execution
are not provided; asynchronous training rendering is explicitly unsupported.

Algorithm and data ownership details: [APPO design](../wiki/design/appo.md).
20 changes: 20 additions & 0 deletions motrix_rl_appo/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[build-system]
requires = ["uv_build>=0.11.23,<0.12"]
build-backend = "uv_build"

[project]
name = "motrix-rl-appo"
version = "0.5.0"
description = "Torch APPO asynchronous actor-learner provider for MotrixLab."
authors = [{ name = "Motphys", email = "developers@motphys.com" }]
requires-python = ">=3.10,<3.14"
readme = "README.md"
license = "Apache-2.0"
dependencies = [
"motrix-env-core==0.5.0",
"motrix-rl==0.5.0",
"torch>=2.9,<3",
]

[project.entry-points."motrix_rl.frameworks"]
appo = "motrix_rl_appo.plugin:register"
2 changes: 2 additions & 0 deletions motrix_rl_appo/src/motrix_rl_appo/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Copyright Motphys Technology Co., Ltd. 2025, 2026
# SPDX-License-Identifier: Apache-2.0
Loading
Loading