Policy Inference in USD Environment

Policy Inference in USD Environment#

This deployment example runs a trained policy in a prebuilt USD scene using the training task’s observations, actions, and robot configuration.

In this tutorial, we will use the RSL RL library and the trained policy from the Humanoid Rough Terrain Isaac-Velocity-Rough-H1 task in a simple warehouse USD.

The Tutorial Code#

For this tutorial, we use the trained policy’s checkpoint exported as jit (which is an offline version of the policy).

The script resolves H1RoughEnvCfg with parse_env_cfg, including any physics preset passed on the command line. Calling its play_mode method applies the play/inference overrides (such as a reduced number of environments and disabled observation noise) on top of the training configuration.

In order to use a prebuilt USD environment instead of the terrain generator specified, we make the following changes to the config before passing it to the ManagerBasedRLEnv.

Code for policy_inference_in_usd.py
 1# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md).
 2# All rights reserved.
 3#
 4# SPDX-License-Identifier: BSD-3-Clause
 5
 6"""
 7This script demonstrates policy inference in a prebuilt USD environment.
 8
 9In this example, we use a locomotion policy to control the H1 robot. The robot was trained
10using Isaac-Velocity-Rough-H1. The robot is commanded to move forward at a constant velocity.
11
12.. code-block:: bash
13
14    # Run the script
15    uv run python scripts/tutorials/03_envs/policy_inference_in_usd.py --checkpoint /path/to/jit/checkpoint.pt
16
17"""
18
19import argparse
20
21from isaaclab.app import add_launcher_args, launch_simulation
22
23# add argparse arguments
24parser = argparse.ArgumentParser(description="Tutorial on inferencing a policy on an H1 robot in a warehouse.")
25parser.add_argument("--checkpoint", type=str, help="Path to model checkpoint exported as jit.", required=True)
26
27add_launcher_args(parser)
28# parse the arguments, forwarding unrecognized ones as Hydra-style task config overrides
29args_cli, hydra_overrides = parser.parse_known_args()
30
31import os
32
33import torch
34
35from isaaclab.envs import ManagerBasedRLEnv
36from isaaclab.terrains import TerrainImporterCfg
37from isaaclab.utils.assets import ISAAC_NUCLEUS_DIR, read_file
38
39from isaaclab_tasks.utils import parse_env_cfg
40
41
42def main():
43    """Main function."""
44    # load the trained jit policy
45    policy_path = os.path.abspath(args_cli.checkpoint)
46    file = read_file(policy_path)
47    policy = torch.jit.load(file, map_location=args_cli.device)
48
49    # setup environment
50    env_cfg = parse_env_cfg("Isaac-Velocity-Rough-H1", device=args_cli.device, num_envs=1, overrides=hydra_overrides)
51    env_cfg.play_mode()
52    env_cfg.curriculum = None
53    env_cfg.scene.terrain = TerrainImporterCfg(
54        prim_path="/World/ground",
55        terrain_type="usd",
56        usd_path=f"{ISAAC_NUCLEUS_DIR}/Environments/Simple_Warehouse/warehouse.usd",
57    )
58    # The warehouse is enclosed: start the height-scan rays below its roof so they hit the floor.
59    env_cfg.scene.height_scanner.offset.pos = (0.0, 0.0, 2.0)
60    if args_cli.device == "cpu":
61        env_cfg.sim.use_fabric = False
62
63    with launch_simulation(env_cfg, args_cli):
64        # create environment
65        env = ManagerBasedRLEnv(cfg=env_cfg)
66
67        # run inference with the policy
68        obs, _ = env.reset()
69        with torch.inference_mode():
70            while env.sim.is_headless_or_exist_active_visualizer():
71                action = policy(obs["policy"])
72                obs, _, _, _, _ = env.step(action)
73        env.close()
74
75
76if __name__ == "__main__":
77    main()

The script uses --device for both policy loading and simulation. It disables Fabric only when --device cpu is explicitly selected. The height scanner starts below the warehouse roof so its downward rays measure the floor.

Keep the same physics preset for training, export, and inference. The commands below use Newton MJWarp with the newton_gl visualizer and do not require Isaac Sim. For a PhysX checkpoint trained with Isaac Sim, use physics=isaacsim_physx throughout and install Isaac Sim; the Newton GL viewer can still be used. Cross-backend policy transfer needs additional validation; see Transfer Policies Between PhysX and Newton.

The Code Execution#

First, we need to train the Isaac-Velocity-Rough-H1 task by running the following:

uv run isaaclab train --rl_library rsl_rl --task Isaac-Velocity-Rough-H1 physics=newton_mjwarp
./isaaclab.sh train --rl_library rsl_rl --task Isaac-Velocity-Rough-H1 physics=newton_mjwarp

When the training is finished, we can visualize the result with the following command. To stop the simulation, you can either close the window, or press Ctrl+C in the terminal where you started the simulation.

uv run isaaclab play --rl_library rsl_rl --task Isaac-Velocity-Rough-H1 physics=newton_mjwarp --num_envs 64 --checkpoint logs/rsl_rl/h1_rough/EXPERIMENT_NAME/POLICY_FILE.pt --viz newton_gl
./isaaclab.sh play --rl_library rsl_rl --task Isaac-Velocity-Rough-H1 physics=newton_mjwarp --num_envs 64 --checkpoint logs/rsl_rl/h1_rough/EXPERIMENT_NAME/POLICY_FILE.pt --viz newton_gl

After running the play script, the policy will be exported to jit and onnx files under the experiment logs directory. Note that not all learning libraries support exporting the policy to a jit or onnx file. For libraries that don’t currently support this functionality, please refer to the corresponding play.py script for the library to learn about how to initialize the policy.

We can then load the warehouse asset and run inference on the H1 robot using the exported jit policy (policy.pt file in the exported/ directory).

uv run python scripts/tutorials/03_envs/policy_inference_in_usd.py --checkpoint logs/rsl_rl/h1_rough/EXPERIMENT_NAME/exported/policy.pt physics=newton_mjwarp --viz newton_gl
./isaaclab.sh -p scripts/tutorials/03_envs/policy_inference_in_usd.py --checkpoint logs/rsl_rl/h1_rough/EXPERIMENT_NAME/exported/policy.pt physics=newton_mjwarp --viz newton_gl
H1 policy running in a warehouse USD scene

In this tutorial, we learnt how to make minor modifications to an existing environment config to run policy inference in a prebuilt usd environment.