Training with an RL Agent#
In the previous tutorials, we covered how to define an RL task environment, register
it into the gym registry, and interact with it using a random agent. We now move
on to the next step: training an RL agent to solve the task.
Although the envs.ManagerBasedRLEnv conforms to the gymnasium.Env interface,
it is not exactly a gym environment. The input and outputs of the environment are
not numpy arrays, but rather based on torch tensors with the first dimension being the
number of environment instances.
Additionally, most RL libraries expect their own variation of an environment interface.
For example, Stable-Baselines3 expects the environment to conform to its
VecEnv API which expects a list of numpy arrays instead of a single tensor. Similarly,
RSL-RL, RL-Games and SKRL expect a different interface. Since there is no one-size-fits-all
solution, we do not base the envs.ManagerBasedRLEnv on any particular learning library.
Instead, we implement wrappers to convert the environment into the expected interface.
These are specified in the isaaclab_rl module.
In this tutorial, we will use Stable-Baselines3 to train an RL agent to solve the cartpole balancing task.
Caution
Wrapping the environment with the respective learning framework’s wrapper should happen in the end,
i.e. after all other wrappers have been applied. This is because the learning framework’s wrapper
modifies the interpretation of environment’s APIs which may no longer be compatible with gymnasium.Env.
The Code#
For this tutorial, we use the training implementation from Stable-Baselines3 workflow in the
isaaclab_rl.entrypoints.backends.train_sb3 module.
Code for train_sb3.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"""Stable-Baselines3 training logic for the unified reinforcement learning entrypoint."""
7
8from __future__ import annotations
9
10import argparse
11import contextlib
12import logging
13import os
14import random
15import signal
16import sys
17import time
18from datetime import datetime
19from pathlib import Path
20
21from isaaclab.app import add_launcher_args, report_activity
22
23from isaaclab_rl.entrypoints.common import (
24 CHECKPOINT_SELECTORS,
25 add_common_train_args,
26 apply_env_overrides,
27 apply_video_recording,
28 configure_io_descriptors,
29 create_isaaclab_env,
30 dump_train_configs,
31 enable_cameras_for_video,
32 pre_launch_video_config,
33 resolve_checkpoint_selector,
34 set_hydra_args,
35 show_run_summary,
36 startup_screen,
37 wrap_training_capture,
38 write_run_manifest,
39)
40
41import isaaclab_tasks # noqa: F401
42
43logger = logging.getLogger(__name__)
44
45# PLACEHOLDER: Extension template (do not remove this comment)
46with contextlib.suppress(ImportError):
47 import isaaclab_tasks_experimental # noqa: F401
48
49
50def _cleanup_pbar(*args):
51 """Stop training and clean up rich progress bars on Ctrl+C."""
52 import gc
53
54 tqdm_objects = [obj for obj in gc.get_objects() if "tqdm" in type(obj).__name__]
55 for tqdm_object in tqdm_objects:
56 if "tqdm_rich" in type(tqdm_object).__name__:
57 tqdm_object.close()
58 raise KeyboardInterrupt
59
60
61def _parse_args(argv: list[str]) -> argparse.Namespace:
62 """Parse Stable-Baselines3 training arguments."""
63 from isaaclab_tasks.utils import setup_preset_cli
64
65 parser = argparse.ArgumentParser(description="Train an RL agent with Stable-Baselines3.")
66 add_common_train_args(
67 parser,
68 agent_default="sb3_cfg_entry_point",
69 agent_help="Name of the RL agent configuration entry point.",
70 include_distributed=False,
71 )
72 parser.add_argument("--log_interval", type=int, default=100_000, help="Log data every n timesteps.")
73 parser.add_argument("--checkpoint", type=str, default=None, help="Checkpoint path, or latest/best.")
74 parser.add_argument(
75 "--keep_all_info",
76 action="store_true",
77 default=False,
78 help="Use a slower SB3 wrapper but keep all the extra training info.",
79 )
80 add_launcher_args(parser)
81 args_cli, hydra_args = setup_preset_cli(parser, argv, agent_library="sb3")
82 enable_cameras_for_video(args_cli)
83 set_hydra_args(hydra_args)
84 return args_cli
85
86
87def run(argv: list[str]) -> None:
88 """Train a Stable-Baselines3 agent."""
89 import numpy as np
90 from stable_baselines3 import PPO
91 from stable_baselines3.common.callbacks import CheckpointCallback, LogEveryNTimesteps
92 from stable_baselines3.common.vec_env import VecNormalize
93
94 from isaaclab.app import launch_simulation
95 from isaaclab.envs import DirectMARLEnvCfg
96 from isaaclab.utils.seed import configure_seed
97
98 from isaaclab_rl.sb3 import Sb3VecEnvWrapper, process_sb3_cfg
99
100 from isaaclab_tasks.utils import resolve_task_config
101
102 signal.signal(signal.SIGINT, _cleanup_pbar)
103
104 args_cli = _parse_args(argv)
105 with startup_screen(args_cli, num_stages=3) as screen:
106 env_cfg, agent_cfg = resolve_task_config(args_cli.task, args_cli.agent)
107 show_run_summary(screen, args_cli, env_cfg, library="sb3", action="train")
108 pre_launch_video_config(env_cfg, args_cli=args_cli)
109 screen.stage("Launching simulation")
110 with launch_simulation(env_cfg, args_cli):
111 if args_cli.seed == -1:
112 args_cli.seed = random.randint(0, 10000)
113
114 apply_env_overrides(args_cli, env_cfg)
115 agent_cfg["seed"] = args_cli.seed if args_cli.seed is not None else agent_cfg["seed"]
116 if args_cli.max_iterations is not None:
117 agent_cfg["n_timesteps"] = args_cli.max_iterations * agent_cfg["n_steps"] * env_cfg.scene.num_envs
118
119 env_cfg.seed = agent_cfg["seed"]
120
121 run_info = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
122 log_root_path = os.path.abspath(os.path.join("logs", "sb3", args_cli.task))
123 print(f"[INFO] Logging experiment in directory: {log_root_path}")
124 print(f"Exact experiment name requested from command line: {run_info}")
125 log_dir = os.path.join(log_root_path, run_info)
126 write_run_manifest(
127 log_dir,
128 library="sb3",
129 task=args_cli.task,
130 metadata={"agent": args_cli.agent},
131 )
132 dump_train_configs(log_dir, env_cfg, agent_cfg)
133
134 command = " ".join(sys.orig_argv)
135 (Path(log_dir) / "command.txt").write_text(command)
136
137 agent_cfg = process_sb3_cfg(agent_cfg, env_cfg.scene.num_envs)
138 policy_arch = agent_cfg.pop("policy")
139 n_timesteps = agent_cfg.pop("n_timesteps")
140
141 configure_io_descriptors(env_cfg, args_cli, logger)
142 env_cfg.log_dir = log_dir
143 apply_video_recording(env_cfg, log_dir, args_cli)
144
145 screen.stage("Creating environment")
146 env = create_isaaclab_env(
147 args_cli.task,
148 env_cfg,
149 args_cli,
150 convert_marl_to_single_agent=isinstance(env_cfg, DirectMARLEnvCfg),
151 )
152 env = wrap_training_capture(env, log_dir, args_cli)
153
154 screen.stage("Preparing agent")
155 start_time = time.time()
156 report_activity("Wrapping environment")
157 env = Sb3VecEnvWrapper(env, fast_variant=not args_cli.keep_all_info)
158 report_activity(None)
159
160 norm_keys = {"normalize_input", "normalize_value", "clip_obs"}
161 norm_args = {}
162 for key in norm_keys:
163 if key in agent_cfg:
164 norm_args[key] = agent_cfg.pop(key)
165
166 if norm_args and norm_args.get("normalize_input"):
167 print(f"Normalizing input, {norm_args=}")
168 env = VecNormalize(
169 env,
170 training=True,
171 norm_obs=norm_args["normalize_input"],
172 norm_reward=norm_args.get("normalize_value", False),
173 clip_obs=norm_args.get("clip_obs", 100.0),
174 gamma=agent_cfg["gamma"],
175 clip_reward=np.inf,
176 )
177
178 report_activity("Building policy")
179 agent = PPO(policy_arch, env, verbose=1, tensorboard_log=log_dir, **agent_cfg)
180 report_activity(None)
181 if args_cli.checkpoint in CHECKPOINT_SELECTORS:
182 checkpoint_path = resolve_checkpoint_selector(
183 log_root_path,
184 args_cli.checkpoint,
185 library="sb3",
186 task=args_cli.task,
187 checkpoint_pattern=r"model(?:_.*)?\.zip",
188 preferred_checkpoint_pattern=r"model\.zip",
189 metadata={"agent": args_cli.agent},
190 )
191 agent = agent.load(checkpoint_path, env, print_system_info=True)
192 elif args_cli.checkpoint is not None:
193 agent = agent.load(args_cli.checkpoint, env, print_system_info=True)
194
195 # configure_seed must run after PPO construction/load so torch determinism does not disturb
196 # SB3's initialization
197 if args_cli.deterministic:
198 configure_seed(env_cfg.seed, torch_deterministic=True)
199
200 checkpoint_callback = CheckpointCallback(save_freq=1000, save_path=log_dir, name_prefix="model", verbose=2)
201 callbacks = [checkpoint_callback, LogEveryNTimesteps(n_steps=args_cli.log_interval)]
202
203 screen.close()
204 with contextlib.suppress(KeyboardInterrupt):
205 agent.learn(
206 total_timesteps=n_timesteps,
207 callback=callbacks,
208 progress_bar=True,
209 log_interval=None,
210 )
211
212 agent.save(os.path.join(log_dir, "model"))
213 print("Saving to:")
214 print(os.path.join(log_dir, "model.zip"))
215
216 if isinstance(env, VecNormalize):
217 print("Saving normalization")
218 env.save(os.path.join(log_dir, "model_vecnormalize.pkl"))
219
220 print(f"Training time: {round(time.time() - start_time, 2)} seconds")
221 env.close()
The Code Explained#
Most of the code above is boilerplate code to create logging directories, saving the parsed configurations, and setting up different Stable-Baselines3 components. For this tutorial, the important part is creating the environment and wrapping it with the Stable-Baselines3 wrapper.
There are three wrappers used in the code above:
gymnasium.wrappers.RecordVideo: This wrapper records a video of the environment and saves it to the specified directory. This is useful for visualizing the agent’s behavior during training.wrappers.sb3.Sb3VecEnvWrapper: This wrapper converts the environment into a Stable-Baselines3 compatible environment.stable_baselines3.common.vec_env.VecNormalize: This wrapper normalizes the environment’s observations and rewards.
Each of these wrappers wrap around the previous wrapper by following env = wrapper(env, *args, **kwargs)
repeatedly. The final environment is then used to train the agent. For more information on how these
wrappers work, please refer to the Wrapping environments documentation.
The Code Execution#
We train a PPO agent from Stable-Baselines3 to solve the cartpole balancing task.
Training the agent#
There are three main ways to train the agent. Each of them has their own advantages and disadvantages. It is up to you to decide which one you prefer based on your use case.
Headless execution#
When no visualizer is requested, no interactive visualizer window is opened during training. This is useful when training on a remote server or when you do not need live visual feedback, which can add some compute cost. Rendering can still be active for sensor/camera data capture when enabled by the workflow.
uv run isaaclab train --rl_library sb3 --task Isaac-Cartpole --num_envs 64
./isaaclab.sh train --rl_library sb3 --task Isaac-Cartpole --num_envs 64
Headless execution with off-screen render#
Since the above command does not open an interactive visualizer, it is not possible to monitor behavior
live in a viewport window. To capture visual output during training, enable camera/sensor rendering
in the workflow and pass --video to record the agent behavior.
uv run --extra video isaaclab train --rl_library sb3 --task Isaac-Cartpole --num_envs 64 --video
./isaaclab.sh train --rl_library sb3 --task Isaac-Cartpole --num_envs 64 --video
The videos are saved to the logs/sb3/Isaac-Cartpole/<run-dir>/videos/train directory. You can open these videos
using any video player.
For tasks with on-scene cameras, you can also save the sensor image outputs directly during training
with --capture_env_sensors. See Capturing sensor frames during training for the available
options and output formats.
Interactive execution#
While the above two methods are useful for training the agent, they don’t allow you to interact with the simulation to see what is happening. In this case, run the training command as follows:
uv run isaaclab train --rl_library sb3 --task Isaac-Cartpole --num_envs 64 --viz kit
./isaaclab.sh train --rl_library sb3 --task Isaac-Cartpole --num_envs 64 --viz kit
This will open the Kit visualizer window and you can see the agent training in the environment. However, this
can slow down the training process because interactive visual feedback is enabled. As a workaround, you
can switch between different render modes in the "Isaac Lab" window that is docked on the bottom-right
corner of the screen. To learn more about these render modes, please check the
sim.SimulationContext.RenderMode class.
Viewing the logs#
On a separate terminal, you can monitor the training progress by executing the following command:
# execute from the root directory of the repository
uv run python -m tensorboard.main --logdir logs/sb3/Isaac-Cartpole
# execute from the root directory of the repository
./isaaclab.sh -p -m tensorboard.main --logdir logs/sb3/Isaac-Cartpole
Playing the trained agent#
Once the training is complete, you can visualize the trained agent by executing the following command:
# execute from the root directory of the repository
uv run isaaclab play --rl_library sb3 --task Isaac-Cartpole --num_envs 32 --viz kit
# execute from the root directory of the repository
./isaaclab.sh play --rl_library sb3 --task Isaac-Cartpole --num_envs 32 --viz kit
The above command will load the latest checkpoint from the logs/sb3/Isaac-Cartpole
directory. You can also specify a specific checkpoint by passing the --checkpoint flag.