Bootstrap RSLRL rewards on environment timeouts

This commit is contained in:
8x54zj-m
2026-07-22 13:30:25 +08:00
parent 1072565d97
commit 57faf8880b
2 changed files with 32 additions and 3 deletions

View File

@@ -123,9 +123,9 @@ class RslrlNpEnvWrap(VecEnv):
batch_size=[self._num_envs], device=self._device) batch_size=[self._num_envs], device=self._device)
# Build extras dict (RSLRL calls it "extras" not "infos") # Build extras dict (RSLRL calls it "extras" not "infos")
extras = {} extras = {
if "time_outs" in state.info: "time_outs": torch.from_numpy(state.truncated.astype(np.float32)).to(self._device),
extras["time_outs"] = torch.from_numpy(state.info["time_outs"]).to(self._device) }
# 将 episode 各项奖励传入 TensorBoard消费后清除防止重复上报 # 将 episode 各项奖励传入 TensorBoard消费后清除防止重复上报
if "ep_report" in state.info: if "ep_report" in state.info:

View File

@@ -0,0 +1,29 @@
from types import SimpleNamespace
import numpy as np
import torch
from motrix_rl.rslrl.torch.wrap_vec_env import RslrlNpEnvWrap
def test_step_preserves_rewards_and_marks_only_truncations_as_timeouts():
state = SimpleNamespace(
obs=np.zeros((2, 3), dtype=np.float32),
reward=np.array([1.25, -0.5], dtype=np.float32),
done=np.array([True, True]),
terminated=np.array([True, False]),
truncated=np.array([False, True]),
info={},
)
wrapper = RslrlNpEnvWrap.__new__(RslrlNpEnvWrap)
wrapper._env = SimpleNamespace(step=lambda actions: state)
wrapper._device = torch.device("cpu")
wrapper._state = None
wrapper._num_envs = 2
wrapper.episode_length_buf = torch.zeros(2, dtype=torch.long)
_, rewards, dones, extras = wrapper.step(torch.zeros((2, 1)))
torch.testing.assert_close(rewards, torch.tensor([1.25, -0.5]))
torch.testing.assert_close(dones, torch.tensor([1.0, 1.0]))
torch.testing.assert_close(extras["time_outs"], torch.tensor([0.0, 1.0]))