Skip to content

Commit

Permalink
fix some batching code, only reset and step return numpy on cpu mode
Browse files Browse the repository at this point in the history
  • Loading branch information
StoneT2000 committed Jan 23, 2024
1 parent 3117874 commit 069d089
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 25 deletions.
37 changes: 18 additions & 19 deletions mani_skill2/envs/sapien_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ def get_obs(self):
if physx.is_gpu_enabled():
return obs
else:
return unbatch(to_numpy(obs))
return unbatch(obs)

def _get_obs_state_dict(self):
"""Get (ground-truth) state-based observations."""
Expand Down Expand Up @@ -414,16 +414,16 @@ def reward_mode(self):

def get_reward(self, obs: Any, action: Array, info: Dict):
if self._reward_mode == "sparse":
return to_tensor(info["success"])
reward = info["success"]
elif self._reward_mode == "dense":
return self.compute_dense_reward(obs=obs, action=action, info=info)
reward = self.compute_dense_reward(obs=obs, action=action, info=info)
elif self._reward_mode == "normalized_dense":
return self.compute_normalized_dense_reward(
reward = self.compute_normalized_dense_reward(
obs=obs, action=action, info=info
)
else:
raise NotImplementedError(self._reward_mode)

return reward
def compute_dense_reward(self, obs: Any, action: Array, info: Dict):
raise NotImplementedError

Expand Down Expand Up @@ -545,12 +545,15 @@ def reset(self, seed=None, options=None):
self._set_episode_rng(self._episode_seed)

self.initialize_episode()
obs = self.get_obs()
if physx.is_gpu_enabled():
# ensure all updates to object poses and configurations are applied on GPU after task initialization
self._scene._gpu_apply_all()
self._scene.px.gpu_update_articulation_kinematics()
self._scene._gpu_fetch_all()
return self.get_obs(), {}
else:
obs = to_numpy(obs)
return obs, {}

def _set_main_rng(self, seed):
"""Set the main random generator (e.g., to generate the seed for each episode)."""
Expand Down Expand Up @@ -616,24 +619,17 @@ def _clear_sim_state(self):
# -------------------------------------------------------------------------- #

def step(self, action: Union[None, np.ndarray, Dict]):
with sapien.profile("step_action"):
self.step_action(action)
self.step_action(action)
self._elapsed_steps += 1
# TODO (stao): I think evaluation should always occur first before generating observations
# as evaluation is more likely to use privileged information whereas observations only sometimes should include privileged information
with sapien.profile("get_obs"):
obs = self.get_obs()
obs = self.get_obs()
info = self.get_info(obs=obs)
reward = self.get_reward(obs=obs, action=action, info=info)
terminated = info["success"]
if self.num_envs == 1:
terminated = terminated[0]
reward = reward[0]

if physx.is_gpu_enabled():
return obs, reward, terminated, torch.Tensor(False), info
else:
return unbatch(obs, reward, terminated.item(), False, to_numpy(info))
# On CPU sim mode, we always return numpy / python primitives without any batching.
return unbatch(to_numpy(obs), to_numpy(reward), to_numpy(terminated), False, to_numpy(info))

def step_action(self, action):
set_action = False
Expand Down Expand Up @@ -674,8 +670,11 @@ def get_info(self, **kwargs):
"""
info = dict(elapsed_steps=self._elapsed_steps)
info.update(self.evaluate(**kwargs))
return info

if physx.is_gpu_enabled():
return info
else:
return unbatch(info)

def _before_control_step(self):
pass

Expand Down
1 change: 1 addition & 0 deletions mani_skill2/envs/tasks/push_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

@register_env("PushCube-v0", max_episode_steps=50)
class PushCubeEnv(BaseEnv):
# Specify some supported robot types
agent: Union[Panda, Xmate3Robotiq]

# set some commonly used values
Expand Down
9 changes: 3 additions & 6 deletions mani_skill2/utils/sapien_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,9 @@ def to_tensor(array: Union[torch.Tensor, np.array, Sequence]):
def _to_numpy(array: Union[Array, Sequence]) -> np.ndarray:
if isinstance(array, (dict)):
return {k: _to_numpy(v) for k, v in array.items()}
if isinstance(array, str):
return array
if torch is not None:
if isinstance(array, torch.Tensor):
return array.cpu().numpy()
if isinstance(array, np.ndarray):
if isinstance(array, torch.Tensor):
return array.cpu().numpy()
if isinstance(array, np.ndarray) or isinstance(array, bool) or isinstance(array, str) or isinstance(array, float) or isinstance(array, int):
return array
else:
return np.array(array)
Expand Down

0 comments on commit 069d089

Please sign in to comment.