GenesisWrapper¶
- torchrl.envs.GenesisWrapper(*args, **kwargs)[source]¶
TorchRL wrapper around a Genesis physics scene.
Genesis is a torch-native physics engine for general-purpose robotics and embodied AI. This wrapper keeps tensors on-device end-to-end: no numpy round-trips, no gym-style shims.
Customization is done by subclassing and overriding the hooks below;
GenesisWrapper(scene)works out of the box on any built scene with sensible defaults, so it’s still useful for one-off experiments.Subclass hooks (all optional):
_make_obs()– read observations offself._sceneand return adict[str, Tensor](or a singleTensor). Default: for each entity with DoFs, emit{entity.name}_qposand{entity.name}_qvelviaget_dofs_position()/get_dofs_velocity()._apply_action()– push the agent’s action into the scene beforescene.step()is called. Default: feedaction[..., :n_dofs]of the first actuated entity as a position target viacontrol_dofs_position()._compute_reward()– compute the per-step reward. Called once per physics substep whenframe_skip > 1and the results are summed. Default:0._compute_done()– compute the truncation flag. Default:self._current_step >= self._max_steps.
- Parameters:
scene (gs.Scene) – a pre-built Genesis scene.
max_steps (int, optional) – truncation horizon consulted by the default
_compute_done(). Defaults to1000.frame_skip (int, optional) – physics steps per env step. Defaults to
1.from_pixels (bool, optional) – if
True, render apixelsentry into the observation using the first camera on the scene. Cameras must be added viascene.add_camera()beforescene.build()(Genesis cannot add cameras post-build); a missing camera raisesValueError. Defaults toFalse.pixels_key (str, optional) – key under which the rendered frame is stored in the returned tensordict. Defaults to
"pixels".device (torch.device, optional) – torch device for returned tensors. When
None(the default), inferred fromgs.device— i.e. the device Genesis itself is running on. This avoids a silent per-step copy when the sim is on GPU but the wrapper was defaulting to CPU.batch_size (torch.Size, optional) – batch size for the env. If omitted, inferred from
scene.n_envs(()for non-batched scenes,(n_envs,)for batched scenes). If provided, validated againstscene.n_envs; a mismatch raisesValueError.allow_done_after_reset (bool, optional) – passed through to
EnvBase. Defaults toFalse.
Examples
Out-of-the-box use on an arbitrary scene:
>>> import genesis as gs >>> from torchrl.envs import GenesisWrapper >>> gs.init(backend=gs.cpu) >>> scene = gs.Scene(show_viewer=False) >>> scene.add_entity(gs.morphs.Plane()) >>> scene.add_entity( ... gs.morphs.MJCF(file="xml/franka_emika_panda/panda.xml") ... ) >>> scene.build() >>> env = GenesisWrapper(scene) >>> td = env.rollout(10)
Subclassing for a custom task:
>>> class ReachEnv(GenesisWrapper): ... def __init__(self, scene, target, **kwargs): ... self._franka = scene.entities[1] # set before super().__init__ ... self._target = target ... super().__init__(scene, **kwargs) ... def _make_obs(self): ... return {"qpos": self._franka.get_dofs_position()} ... def _apply_action(self, action): ... self._franka.control_dofs_position(action) ... def _compute_reward(self, action): ... ee = self._franka.get_link("hand").get_pos() ... return -(ee - self._target).norm()