File size: 6,266 Bytes
471c82f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | # Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
import torch
from torch import Tensor, nn
def compute_returns(self, rewards, values, dones, last_values, gamma, lam):
advantage = 0
returns = torch.zeros_like(values)
for step in reversed(range(self.num_transitions_per_env)):
if step == self.num_transitions_per_env - 1:
next_values = last_values
else:
next_values = values[step + 1]
next_is_not_terminal = 1.0 - dones[step].float()
delta = rewards[step] + next_is_not_terminal * gamma * next_values - values[step]
advantage = delta + next_is_not_terminal * gamma * lam * advantage
returns[step] = advantage + values[step]
# Compute and normalize the advantages
advantages = returns - values
advantages = (advantages - advantages.mean()) / (advantages.std() + 1e-8)
class RolloutStorage(nn.Module):
def __init__(self, num_envs, num_transitions_per_env, device="cpu"):
super().__init__()
self.device = device
self.num_transitions_per_env = num_transitions_per_env
self.num_envs = num_envs
# rnn - for storing hidden states during rollout
self.saved_hidden_states_a = None
self.saved_hidden_states_c = None
self.step = 0
self.stored_keys = list()
def register_key(self, key: str, shape=(), dtype=torch.float):
# This class was partially copied from https://github.com/NVlabs/ProtoMotions/blob/94059259ba2b596bf908828cc04e8fc6ff901114/phys_anim/agents/utils/data_utils.py
assert not hasattr(self, key), key
assert isinstance(shape, (list, tuple)), f"shape must be a list or tuple, got {type(shape)}"
buffer = torch.zeros(
(self.num_transitions_per_env, self.num_envs) + shape, dtype=dtype, device=self.device
)
self.register_buffer(key, buffer, persistent=False)
self.stored_keys.append(key)
def increment_step(self):
self.step += 1
def update_key(self, key: str, data: Tensor):
# This class was partially copied from https://github.com/NVlabs/ProtoMotions/blob/94059259ba2b596bf908828cc04e8fc6ff901114/phys_anim/agents/utils/data_utils.py
assert not data.requires_grad
assert self.step < self.num_transitions_per_env, "Rollout buffer overflow"
try:
getattr(self, key)[self.step].copy_(data)
except:
import ipdb
ipdb.set_trace()
def batch_update_data(self, key: str, data: Tensor):
# This class was partially copied from https://github.com/NVlabs/ProtoMotions/blob/94059259ba2b596bf908828cc04e8fc6ff901114/phys_anim/agents/utils/data_utils.py
assert not data.requires_grad
getattr(self, key)[:] = data
# self.store_dict[key] += self.total_sum()
def _save_hidden_states(self, hidden_states):
"""Save hidden states for recurrent policies.
Args:
hidden_states: Tuple of (actor_hidden_states, critic_hidden_states)
Each can be None, a tensor (GRU), or a tuple of tensors (LSTM)
"""
if hidden_states is None or hidden_states == (None, None):
return
# make a tuple out of GRU hidden state to match the LSTM format
hid_a = (
hidden_states[0]
if isinstance(hidden_states[0], tuple)
else (hidden_states[0],) if hidden_states[0] is not None else None
)
hid_c = (
hidden_states[1]
if isinstance(hidden_states[1], tuple)
else (hidden_states[1],) if hidden_states[1] is not None else None
)
# initialize if needed
if hid_a is not None and self.saved_hidden_states_a is None:
self.saved_hidden_states_a = [
torch.zeros(self.num_transitions_per_env, *hid_a[i].shape, device=self.device)
for i in range(len(hid_a))
]
if hid_c is not None and self.saved_hidden_states_c is None:
self.saved_hidden_states_c = [
torch.zeros(self.num_transitions_per_env, *hid_c[i].shape, device=self.device)
for i in range(len(hid_c))
]
# copy the states - CRITICAL: must detach for proper TBPTT
# We save detached hidden states from rollout to use as initial states during training
# This prevents backprop through the entire rollout history
if hid_a is not None:
for i in range(len(hid_a)):
self.saved_hidden_states_a[i][self.step].copy_(hid_a[i].detach())
if hid_c is not None:
for i in range(len(hid_c)):
self.saved_hidden_states_c[i][self.step].copy_(hid_c[i].detach())
def clear(self):
self.step = 0
def get_statistics(self):
raise NotImplementedError
done = self.dones
done[-1] = 1
flat_dones = done.permute(1, 0, 2).reshape(-1, 1)
done_indices = torch.cat(
(
flat_dones.new_tensor([-1], dtype=torch.int64),
flat_dones.nonzero(as_tuple=False)[:, 0],
)
)
trajectory_lengths = done_indices[1:] - done_indices[:-1]
return trajectory_lengths.float().mean(), self.rewards.mean()
def query_key(self, key: str):
assert hasattr(self, key), key
return getattr(self, key)
def mini_batch_generator(self, num_mini_batches, num_epochs=8):
batch_size = self.num_envs * self.num_transitions_per_env
mini_batch_size = batch_size // num_mini_batches
indices = torch.randperm(
num_mini_batches * mini_batch_size, requires_grad=False, device=self.device
)
_buffer_dict = {key: getattr(self, key)[:].flatten(0, 1) for key in self.stored_keys}
for epoch in range(num_epochs):
for i in range(num_mini_batches):
start = i * mini_batch_size
end = (i + 1) * mini_batch_size
batch_idx = indices[start:end]
_batch_buffer_dict = {key: _buffer_dict[key][batch_idx] for key in self.stored_keys}
yield _batch_buffer_dict
|