Skip to content

Commit 8f9072d

Browse files
committed
Update RSL-RL configs to work with newer versions 4.0 and 5.0
1 parent 252202a commit 8f9072d

7 files changed

Lines changed: 193 additions & 81 deletions

File tree

scripts/reinforcement_learning/rsl_rl/cli_args.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ def update_rsl_rl_cfg(agent_cfg: RslRlBaseRunnerCfg, args_cli: argparse.Namespac
8282
agent_cfg.load_run = args_cli.load_run
8383
if args_cli.checkpoint is not None:
8484
agent_cfg.load_checkpoint = args_cli.checkpoint
85+
if args_cli.experiment_name is not None:
86+
agent_cfg.experiment_name = args_cli.experiment_name
8587
if args_cli.run_name is not None:
8688
agent_cfg.run_name = args_cli.run_name
8789
if args_cli.logger is not None:

scripts/reinforcement_learning/rsl_rl/play.py

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@
5555
app_launcher = AppLauncher(args_cli)
5656
simulation_app = app_launcher.app
5757

58+
"""Check for installed RSL-RL version."""
59+
60+
import importlib.metadata as metadata
61+
62+
from packaging import version
63+
64+
installed_version = metadata.version("rsl-rl-lib")
65+
5866
"""Rest everything follows."""
5967

6068
import os
@@ -76,7 +84,13 @@
7684
from isaaclab.utils.assets import retrieve_file_path
7785
from isaaclab.utils.dict import print_dict
7886

79-
from isaaclab_rl.rsl_rl import RslRlBaseRunnerCfg, RslRlVecEnvWrapper, export_policy_as_jit, export_policy_as_onnx
87+
from isaaclab_rl.rsl_rl import (
88+
RslRlBaseRunnerCfg,
89+
RslRlVecEnvWrapper,
90+
export_policy_as_jit,
91+
export_policy_as_onnx,
92+
handle_deprecated_rsl_rl_cfg,
93+
)
8094
from isaaclab_rl.utils.pretrained_checkpoint import get_published_pretrained_checkpoint
8195

8296
from isaaclab_tasks.utils import get_checkpoint_path
@@ -100,6 +114,9 @@ def main(env_cfg: ManagerBasedRLEnvCfg | DirectRLEnvCfg | DirectMARLEnvCfg, agen
100114
agent_cfg: RslRlBaseRunnerCfg = cli_args.update_rsl_rl_cfg(agent_cfg, args_cli)
101115
env_cfg.scene.num_envs = args_cli.num_envs if args_cli.num_envs is not None else 64
102116

117+
# handle deprecated configurations
118+
agent_cfg = handle_deprecated_rsl_rl_cfg(agent_cfg, installed_version)
119+
103120
# set the environment seed
104121
# note: certain randomizations occur in the environment initialization so we set the seed here
105122
env_cfg.seed = agent_cfg.seed
@@ -189,27 +206,31 @@ def main(env_cfg: ManagerBasedRLEnvCfg | DirectRLEnvCfg | DirectMARLEnvCfg, agen
189206
# obtain the trained policy for inference
190207
policy = runner.get_inference_policy(device=env.unwrapped.device)
191208

192-
# extract the neural network module
193-
# we do this in a try-except to maintain backwards compatibility.
194-
try:
195-
# version 2.3 onwards
196-
policy_nn = runner.alg.policy
197-
except AttributeError:
198-
# version 2.2 and below
199-
policy_nn = runner.alg.actor_critic
200-
201-
# extract the normalizer
202-
if hasattr(policy_nn, "actor_obs_normalizer"):
203-
normalizer = policy_nn.actor_obs_normalizer
204-
elif hasattr(policy_nn, "student_obs_normalizer"):
205-
normalizer = policy_nn.student_obs_normalizer
206-
else:
207-
normalizer = None
208-
209-
# export policy to onnx/jit
209+
# export the trained policy to JIT and ONNX formats
210210
export_model_dir = os.path.join(os.path.dirname(resume_path), "exported")
211-
export_policy_as_jit(policy_nn, normalizer=normalizer, path=export_model_dir, filename="policy.pt")
212-
export_policy_as_onnx(policy_nn, normalizer=normalizer, path=export_model_dir, filename="policy.onnx")
211+
212+
if version.parse(installed_version) >= version.parse("4.0.0"):
213+
# use the new export functions for rsl-rl >= 4.0.0
214+
runner.export_policy_to_jit(path=export_model_dir, filename="policy.pt")
215+
runner.export_policy_to_onnx(path=export_model_dir, filename="policy.onnx")
216+
else:
217+
# extract the neural network for rsl-rl < 4.0.0
218+
if version.parse(installed_version) >= version.parse("2.3.0"):
219+
policy_nn = runner.alg.policy
220+
else:
221+
policy_nn = runner.alg.actor_critic
222+
223+
# extract the normalizer
224+
if hasattr(policy_nn, "actor_obs_normalizer"):
225+
normalizer = policy_nn.actor_obs_normalizer
226+
elif hasattr(policy_nn, "student_obs_normalizer"):
227+
normalizer = policy_nn.student_obs_normalizer
228+
else:
229+
normalizer = None
230+
231+
# export to JIT and ONNX
232+
export_policy_as_jit(policy_nn, normalizer=normalizer, path=export_model_dir, filename="policy.pt")
233+
export_policy_as_onnx(policy_nn, normalizer=normalizer, path=export_model_dir, filename="policy.onnx")
213234

214235
dt = env.unwrapped.step_dt
215236

@@ -226,7 +247,10 @@ def main(env_cfg: ManagerBasedRLEnvCfg | DirectRLEnvCfg | DirectMARLEnvCfg, agen
226247
# env stepping
227248
obs, _, dones, _ = env.step(actions)
228249
# reset recurrent states for episodes that have terminated
229-
policy_nn.reset(dones)
250+
if version.parse(installed_version) >= version.parse("4.0.0"):
251+
policy.reset(dones)
252+
else:
253+
policy_nn.reset(dones)
230254
if args_cli.video:
231255
timestep += 1
232256
# Exit the play loop after recording one video

scripts/reinforcement_learning/rsl_rl/play_cs.py

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@
5656
app_launcher = AppLauncher(args_cli)
5757
simulation_app = app_launcher.app
5858

59+
"""Check for installed RSL-RL version."""
60+
61+
import importlib.metadata as metadata
62+
63+
from packaging import version
64+
65+
installed_version = metadata.version("rsl-rl-lib")
66+
5967
"""Rest everything follows."""
6068

6169
import os
@@ -78,7 +86,13 @@
7886
from isaaclab.utils.assets import retrieve_file_path
7987
from isaaclab.utils.dict import print_dict
8088

81-
from isaaclab_rl.rsl_rl import RslRlBaseRunnerCfg, RslRlVecEnvWrapper, export_policy_as_jit, export_policy_as_onnx
89+
from isaaclab_rl.rsl_rl import (
90+
RslRlBaseRunnerCfg,
91+
RslRlVecEnvWrapper,
92+
export_policy_as_jit,
93+
export_policy_as_onnx,
94+
handle_deprecated_rsl_rl_cfg,
95+
)
8296
from isaaclab_rl.utils.pretrained_checkpoint import get_published_pretrained_checkpoint
8397

8498
from isaaclab_tasks.utils import get_checkpoint_path
@@ -102,6 +116,9 @@ def main(env_cfg: ManagerBasedRLEnvCfg | DirectRLEnvCfg | DirectMARLEnvCfg, agen
102116
agent_cfg: RslRlBaseRunnerCfg = cli_args.update_rsl_rl_cfg(agent_cfg, args_cli)
103117
env_cfg.scene.num_envs = args_cli.num_envs if args_cli.num_envs is not None else 64
104118

119+
# handle deprecated configurations
120+
agent_cfg = handle_deprecated_rsl_rl_cfg(agent_cfg, installed_version)
121+
105122
# set the environment seed
106123
# note: certain randomizations occur in the environment initialization so we set the seed here
107124
env_cfg.seed = agent_cfg.seed
@@ -229,27 +246,31 @@ def main(env_cfg: ManagerBasedRLEnvCfg | DirectRLEnvCfg | DirectMARLEnvCfg, agen
229246
# obtain the trained policy for inference
230247
policy = runner.get_inference_policy(device=env.unwrapped.device)
231248

232-
# extract the neural network module
233-
# we do this in a try-except to maintain backwards compatibility.
234-
try:
235-
# version 2.3 onwards
236-
policy_nn = runner.alg.policy
237-
except AttributeError:
238-
# version 2.2 and below
239-
policy_nn = runner.alg.actor_critic
240-
241-
# extract the normalizer
242-
if hasattr(policy_nn, "actor_obs_normalizer"):
243-
normalizer = policy_nn.actor_obs_normalizer
244-
elif hasattr(policy_nn, "student_obs_normalizer"):
245-
normalizer = policy_nn.student_obs_normalizer
246-
else:
247-
normalizer = None
248-
249-
# export policy to onnx/jit
249+
# export the trained policy to JIT and ONNX formats
250250
export_model_dir = os.path.join(os.path.dirname(resume_path), "exported")
251-
export_policy_as_jit(policy_nn, normalizer=normalizer, path=export_model_dir, filename="policy.pt")
252-
export_policy_as_onnx(policy_nn, normalizer=normalizer, path=export_model_dir, filename="policy.onnx")
251+
252+
if version.parse(installed_version) >= version.parse("4.0.0"):
253+
# use the new export functions for rsl-rl >= 4.0.0
254+
runner.export_policy_to_jit(path=export_model_dir, filename="policy.pt")
255+
runner.export_policy_to_onnx(path=export_model_dir, filename="policy.onnx")
256+
else:
257+
# extract the neural network for rsl-rl < 4.0.0
258+
if version.parse(installed_version) >= version.parse("2.3.0"):
259+
policy_nn = runner.alg.policy
260+
else:
261+
policy_nn = runner.alg.actor_critic
262+
263+
# extract the normalizer
264+
if hasattr(policy_nn, "actor_obs_normalizer"):
265+
normalizer = policy_nn.actor_obs_normalizer
266+
elif hasattr(policy_nn, "student_obs_normalizer"):
267+
normalizer = policy_nn.student_obs_normalizer
268+
else:
269+
normalizer = None
270+
271+
# export to JIT and ONNX
272+
export_policy_as_jit(policy_nn, normalizer=normalizer, path=export_model_dir, filename="policy.pt")
273+
export_policy_as_onnx(policy_nn, normalizer=normalizer, path=export_model_dir, filename="policy.onnx")
253274

254275
dt = env.unwrapped.step_dt
255276

@@ -266,7 +287,10 @@ def main(env_cfg: ManagerBasedRLEnvCfg | DirectRLEnvCfg | DirectMARLEnvCfg, agen
266287
# env stepping
267288
obs, _, dones, _ = env.step(actions)
268289
# reset recurrent states for episodes that have terminated
269-
policy_nn.reset(dones)
290+
if version.parse(installed_version) >= version.parse("4.0.0"):
291+
policy.reset(dones)
292+
else:
293+
policy_nn.reset(dones)
270294
if args_cli.video:
271295
timestep += 1
272296
# Exit the play loop after recording one video

scripts/reinforcement_learning/rsl_rl/train.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
from isaaclab.utils.dict import print_dict
9898
from isaaclab.utils.io import dump_yaml
9999

100-
from isaaclab_rl.rsl_rl import RslRlBaseRunnerCfg, RslRlVecEnvWrapper
100+
from isaaclab_rl.rsl_rl import RslRlBaseRunnerCfg, RslRlVecEnvWrapper, handle_deprecated_rsl_rl_cfg
101101

102102
from isaaclab_tasks.utils import get_checkpoint_path
103103
from isaaclab_tasks.utils.hydra import hydra_task_config
@@ -125,6 +125,9 @@ def main(env_cfg: ManagerBasedRLEnvCfg | DirectRLEnvCfg | DirectMARLEnvCfg, agen
125125
args_cli.max_iterations if args_cli.max_iterations is not None else agent_cfg.max_iterations
126126
)
127127

128+
# handle deprecated configurations
129+
agent_cfg = handle_deprecated_rsl_rl_cfg(agent_cfg, installed_version)
130+
128131
# set the environment seed
129132
# note: certain randomizations occur in the environment initialization so we set the seed here
130133
env_cfg.seed = agent_cfg.seed

source/robot_lab/robot_lab/tasks/manager_based/locomotion/velocity/config/quadruped/anymal_d/__init__.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,39 @@
1010
##
1111

1212
gym.register(
13-
id="RobotLab-Isaac-Velocity-Flat-Anymal-D-v0",
13+
id="Isaac-Velocity-Flat-Anymal-D-v0",
1414
entry_point="isaaclab.envs:ManagerBasedRLEnv",
1515
disable_env_checker=True,
1616
kwargs={
1717
"env_cfg_entry_point": f"{__name__}.flat_env_cfg:AnymalDFlatEnvCfg",
1818
"rsl_rl_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:AnymalDFlatPPORunnerCfg",
19+
"rsl_rl_recurrent_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:AnymalDFlatPPORunnerRecurrentCfg",
1920
"rsl_rl_distillation_cfg_entry_point": (
2021
f"{agents.__name__}.rsl_rl_distillation_cfg:AnymalDFlatDistillationRunnerCfg"
2122
),
22-
"rsl_rl_with_symmetry_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:AnymalDFlatPPORunnerWithSymmetryCfg",
23-
"cusrl_cfg_entry_point": f"{agents.__name__}.cusrl_ppo_cfg:AnymalDFlatTrainerCfg",
24-
"cusrl_distillation_cfg_entry_point": (
25-
f"{agents.__name__}.cusrl_distillation_cfg:AnymalDFlatDistillationTrainerCfg"
26-
),
27-
"cusrl_with_symmetry_cfg_entry_point": (
28-
f"{agents.__name__}.cusrl_ppo_cfg:AnymalDFlatTrainerCfgWithSymmetryAugmentation"
23+
"rsl_rl_distillation_recurrent_cfg_entry_point": (
24+
f"{agents.__name__}.rsl_rl_distillation_cfg:AnymalDFlatDistillationRunnerRecurrentCfg"
2925
),
26+
"rsl_rl_with_symmetry_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:AnymalDFlatPPORunnerWithSymmetryCfg",
27+
"skrl_cfg_entry_point": f"{agents.__name__}:skrl_flat_ppo_cfg.yaml",
3028
},
3129
)
3230

3331
gym.register(
34-
id="RobotLab-Isaac-Velocity-Rough-Anymal-D-v0",
32+
id="Isaac-Velocity-Flat-Anymal-D-Play-v0",
3533
entry_point="isaaclab.envs:ManagerBasedRLEnv",
3634
disable_env_checker=True,
3735
kwargs={
38-
"env_cfg_entry_point": f"{__name__}.rough_env_cfg:AnymalDRoughEnvCfg",
39-
"rsl_rl_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:AnymalDRoughPPORunnerCfg",
40-
"rsl_rl_with_symmetry_cfg_entry_point": (
41-
f"{agents.__name__}.rsl_rl_ppo_cfg:AnymalDRoughPPORunnerWithSymmetryCfg"
36+
"env_cfg_entry_point": f"{__name__}.flat_env_cfg:AnymalDFlatEnvCfg_PLAY",
37+
"rsl_rl_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:AnymalDFlatPPORunnerCfg",
38+
"rsl_rl_recurrent_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:AnymalDFlatPPORunnerRecurrentCfg",
39+
"rsl_rl_distillation_cfg_entry_point": (
40+
f"{agents.__name__}.rsl_rl_distillation_cfg:AnymalDFlatDistillationRunnerCfg"
4241
),
43-
"cusrl_cfg_entry_point": f"{agents.__name__}.cusrl_ppo_cfg:AnymalDRoughTrainerCfg",
44-
"cusrl_with_symmetry_cfg_entry_point": (
45-
f"{agents.__name__}.cusrl_ppo_cfg:AnymalDRoughTrainerCfgWithSymmetryAugmentation"
42+
"rsl_rl_distillation_recurrent_cfg_entry_point": (
43+
f"{agents.__name__}.rsl_rl_distillation_cfg:AnymalDFlatDistillationRunnerRecurrentCfg"
4644
),
45+
"rsl_rl_with_symmetry_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:AnymalDFlatPPORunnerWithSymmetryCfg",
46+
"skrl_cfg_entry_point": f"{agents.__name__}:skrl_flat_ppo_cfg.yaml",
4747
},
4848
)

source/robot_lab/robot_lab/tasks/manager_based/locomotion/velocity/config/quadruped/anymal_d/agents/rsl_rl_distillation_cfg.py

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
from isaaclab_rl.rsl_rl import (
1212
RslRlDistillationAlgorithmCfg,
1313
RslRlDistillationRunnerCfg,
14-
RslRlDistillationStudentTeacherCfg,
14+
RslRlMLPModelCfg,
15+
RslRlRNNModelCfg,
1516
)
1617

1718

@@ -21,18 +22,43 @@ class AnymalDFlatDistillationRunnerCfg(RslRlDistillationRunnerCfg):
2122
max_iterations = 300
2223
save_interval = 50
2324
experiment_name = "anymal_d_flat"
24-
obs_groups = {"policy": ["policy"], "teacher": ["policy"]}
25-
policy = RslRlDistillationStudentTeacherCfg(
26-
init_noise_std=0.1,
27-
noise_std_type="scalar",
28-
student_obs_normalization=False,
29-
teacher_obs_normalization=False,
30-
student_hidden_dims=[512, 256, 128],
31-
teacher_hidden_dims=[512, 256, 128],
25+
obs_groups = {"student": ["policy"], "teacher": ["policy"]}
26+
student = RslRlMLPModelCfg(
27+
hidden_dims=[128, 128, 128],
3228
activation="elu",
29+
obs_normalization=False,
30+
distribution_cfg=RslRlMLPModelCfg.GaussianDistributionCfg(init_std=0.1),
31+
)
32+
teacher = RslRlMLPModelCfg(
33+
hidden_dims=[128, 128, 128],
34+
activation="elu",
35+
obs_normalization=False,
36+
distribution_cfg=RslRlMLPModelCfg.GaussianDistributionCfg(init_std=0.0),
3337
)
3438
algorithm = RslRlDistillationAlgorithmCfg(
3539
num_learning_epochs=2,
3640
learning_rate=1.0e-3,
3741
gradient_length=15,
3842
)
43+
44+
45+
@configclass
46+
class AnymalDFlatDistillationRunnerRecurrentCfg(AnymalDFlatDistillationRunnerCfg):
47+
student = RslRlRNNModelCfg(
48+
hidden_dims=[128, 128, 128],
49+
activation="elu",
50+
obs_normalization=False,
51+
distribution_cfg=RslRlMLPModelCfg.GaussianDistributionCfg(init_std=0.1),
52+
rnn_type="lstm",
53+
rnn_hidden_dim=256,
54+
rnn_num_layers=1,
55+
)
56+
teacher = RslRlRNNModelCfg(
57+
hidden_dims=[128, 128, 128],
58+
activation="elu",
59+
obs_normalization=False,
60+
distribution_cfg=RslRlMLPModelCfg.GaussianDistributionCfg(init_std=0.0),
61+
rnn_type="lstm",
62+
rnn_hidden_dim=256,
63+
rnn_num_layers=1,
64+
)

0 commit comments

Comments
 (0)