|
| 1 | +# Copyright (c) 2022-2025, The Isaac Lab Project Developers. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# SPDX-License-Identifier: BSD-3-Clause |
| 5 | +# |
| 6 | +# Code taken from https://github.com/isaac-sim/IsaacLab/blob/5716d5600a1a0e45345bc01342a70bd81fac7889/source/isaaclab_rl/isaaclab_rl/rsl_rl/exporter.py |
| 7 | + |
| 8 | +import copy |
| 9 | +import os |
| 10 | +import torch |
| 11 | + |
| 12 | + |
| 13 | +def export_policy_as_onnx( |
| 14 | + actor_critic: object, |
| 15 | + path: str, |
| 16 | + normalizer: object | None = None, |
| 17 | + filename="policy.onnx", |
| 18 | + verbose=False, |
| 19 | +): |
| 20 | + """Export policy into a Torch ONNX file. |
| 21 | +
|
| 22 | + Args: |
| 23 | + actor_critic: The actor-critic torch module. |
| 24 | + normalizer: The empirical normalizer module. If None, Identity is used. |
| 25 | + path: The path to the saving directory. |
| 26 | + filename: The name of exported ONNX file. Defaults to "policy.onnx". |
| 27 | + verbose: Whether to print the model summary. Defaults to False. |
| 28 | + """ |
| 29 | + if not os.path.exists(path): |
| 30 | + os.makedirs(path, exist_ok=True) |
| 31 | + policy_exporter = _OnnxPolicyExporter(actor_critic, normalizer, verbose) |
| 32 | + policy_exporter.export(path, filename) |
| 33 | + |
| 34 | + |
| 35 | +""" |
| 36 | +Helper Classes - Private. |
| 37 | +""" |
| 38 | + |
| 39 | + |
| 40 | +class _TorchPolicyExporter(torch.nn.Module): |
| 41 | + """Exporter of actor-critic into JIT file.""" |
| 42 | + |
| 43 | + def __init__(self, actor_critic, normalizer=None): |
| 44 | + super().__init__() |
| 45 | + self.actor = copy.deepcopy(actor_critic.actor) |
| 46 | + self.is_recurrent = actor_critic.is_recurrent |
| 47 | + if self.is_recurrent: |
| 48 | + self.rnn = copy.deepcopy(actor_critic.memory_a.rnn) |
| 49 | + self.rnn.cpu() |
| 50 | + self.register_buffer( |
| 51 | + "hidden_state", |
| 52 | + torch.zeros(self.rnn.num_layers, 1, self.rnn.hidden_size), |
| 53 | + ) |
| 54 | + self.register_buffer( |
| 55 | + "cell_state", torch.zeros(self.rnn.num_layers, 1, self.rnn.hidden_size) |
| 56 | + ) |
| 57 | + self.forward = self.forward_lstm |
| 58 | + self.reset = self.reset_memory |
| 59 | + # copy normalizer if exists |
| 60 | + if normalizer: |
| 61 | + self.normalizer = copy.deepcopy(normalizer) |
| 62 | + else: |
| 63 | + self.normalizer = torch.nn.Identity() |
| 64 | + |
| 65 | + def forward_lstm(self, x): |
| 66 | + x = self.normalizer(x) |
| 67 | + x, (h, c) = self.rnn(x.unsqueeze(0), (self.hidden_state, self.cell_state)) |
| 68 | + self.hidden_state[:] = h |
| 69 | + self.cell_state[:] = c |
| 70 | + x = x.squeeze(0) |
| 71 | + return self.actor(x) |
| 72 | + |
| 73 | + def forward(self, x): |
| 74 | + return self.actor(self.normalizer(x)) |
| 75 | + |
| 76 | + @torch.jit.export |
| 77 | + def reset(self): |
| 78 | + pass |
| 79 | + |
| 80 | + def reset_memory(self): |
| 81 | + self.hidden_state[:] = 0.0 |
| 82 | + self.cell_state[:] = 0.0 |
| 83 | + |
| 84 | + def export(self, path, filename): |
| 85 | + os.makedirs(path, exist_ok=True) |
| 86 | + path = os.path.join(path, filename) |
| 87 | + self.to("cpu") |
| 88 | + traced_script_module = torch.jit.script(self) |
| 89 | + traced_script_module.save(path) |
| 90 | + |
| 91 | + |
| 92 | +class _OnnxPolicyExporter(torch.nn.Module): |
| 93 | + """Exporter of actor-critic into ONNX file.""" |
| 94 | + |
| 95 | + def __init__(self, actor_critic, normalizer=None, verbose=False): |
| 96 | + super().__init__() |
| 97 | + self.verbose = verbose |
| 98 | + self.actor = copy.deepcopy(actor_critic.actor) |
| 99 | + self.is_recurrent = actor_critic.is_recurrent |
| 100 | + if self.is_recurrent: |
| 101 | + self.rnn = copy.deepcopy(actor_critic.memory_a.rnn) |
| 102 | + self.rnn.cpu() |
| 103 | + self.forward = self.forward_lstm |
| 104 | + # copy normalizer if exists |
| 105 | + if normalizer: |
| 106 | + self.normalizer = copy.deepcopy(normalizer) |
| 107 | + else: |
| 108 | + self.normalizer = torch.nn.Identity() |
| 109 | + |
| 110 | + def forward_lstm(self, x_in, h_in, c_in): |
| 111 | + x_in = self.normalizer(x_in) |
| 112 | + x, (h, c) = self.rnn(x_in.unsqueeze(0), (h_in, c_in)) |
| 113 | + x = x.squeeze(0) |
| 114 | + return self.actor(x), h, c |
| 115 | + |
| 116 | + def forward(self, x): |
| 117 | + return self.actor(self.normalizer(x)) |
| 118 | + |
| 119 | + def export(self, path, filename): |
| 120 | + self.to("cpu") |
| 121 | + if self.is_recurrent: |
| 122 | + obs = torch.zeros(1, self.rnn.input_size) |
| 123 | + h_in = torch.zeros(self.rnn.num_layers, 1, self.rnn.hidden_size) |
| 124 | + c_in = torch.zeros(self.rnn.num_layers, 1, self.rnn.hidden_size) |
| 125 | + actions, h_out, c_out = self(obs, h_in, c_in) |
| 126 | + torch.onnx.export( |
| 127 | + self, |
| 128 | + (obs, h_in, c_in), |
| 129 | + os.path.join(path, filename), |
| 130 | + export_params=True, |
| 131 | + opset_version=11, |
| 132 | + verbose=self.verbose, |
| 133 | + input_names=["obs", "h_in", "c_in"], |
| 134 | + output_names=["actions", "h_out", "c_out"], |
| 135 | + dynamic_axes={}, |
| 136 | + ) |
| 137 | + else: |
| 138 | + obs = torch.zeros(1, self.actor[0].in_features) |
| 139 | + torch.onnx.export( |
| 140 | + self, |
| 141 | + obs, |
| 142 | + os.path.join(path, filename), |
| 143 | + export_params=True, |
| 144 | + opset_version=11, |
| 145 | + verbose=self.verbose, |
| 146 | + input_names=["obs"], |
| 147 | + output_names=["actions"], |
| 148 | + dynamic_axes={}, |
| 149 | + ) |
0 commit comments