forked from weipu-zhang/STORM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agents.py
206 lines (175 loc) · 6.91 KB
/
agents.py
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import copy
import torch
import torch.distributions as distributions
import torch.nn as nn
from sub_models.functions_losses import SymLogTwoHotLoss
from utils import EMAScalar
def percentile(x, percentage):
flat_x = torch.flatten(x)
kth = int(percentage * len(flat_x))
per = torch.kthvalue(flat_x, kth).values
return per
def calc_lambda_return(rewards, values, termination, gamma, lam, dtype=torch.float32):
# Invert termination to have 0 if the episode ended and 1 otherwise
inv_termination = (termination * -1) + 1
batch_size, batch_length = rewards.shape[:2]
# gae_step = torch.zeros((batch_size, ), dtype=dtype, device="cuda")
gamma_return = torch.zeros(
(batch_size, batch_length + 1), dtype=dtype, device="cuda"
)
gamma_return[:, -1] = values[:, -1]
for t in reversed(range(batch_length)): # with last bootstrap
gamma_return[:, t] = (
rewards[:, t]
+ gamma * inv_termination[:, t] * (1 - lam) * values[:, t]
+ gamma * inv_termination[:, t] * lam * gamma_return[:, t + 1]
)
return gamma_return[:, :-1]
class ActorCriticAgent(nn.Module):
def __init__(
self,
feat_dim,
num_layers,
hidden_dim,
action_dim,
gamma,
lambd,
entropy_coef,
use_amp,
) -> None:
super().__init__()
self.gamma = gamma
self.lambd = lambd
self.entropy_coef = entropy_coef
self.use_amp = use_amp
self.tensor_dtype = torch.bfloat16 if self.use_amp else torch.float32
self.symlog_twohot_loss = SymLogTwoHotLoss(255, -20, 20)
actor = [
nn.Linear(feat_dim, hidden_dim, bias=False),
nn.LayerNorm(hidden_dim),
nn.ReLU(),
]
for i in range(num_layers - 1):
actor.extend(
[
nn.Linear(hidden_dim, hidden_dim, bias=False),
nn.LayerNorm(hidden_dim),
nn.ReLU(),
]
)
self.actor = nn.Sequential(*actor, nn.Linear(hidden_dim, action_dim))
critic = [
nn.Linear(feat_dim, hidden_dim, bias=False),
nn.LayerNorm(hidden_dim),
nn.ReLU(),
]
for i in range(num_layers - 1):
critic.extend(
[
nn.Linear(hidden_dim, hidden_dim, bias=False),
nn.LayerNorm(hidden_dim),
nn.ReLU(),
]
)
self.critic = nn.Sequential(*critic, nn.Linear(hidden_dim, 255))
self.slow_critic = copy.deepcopy(self.critic)
self.lowerbound_ema = EMAScalar(decay=0.99)
self.upperbound_ema = EMAScalar(decay=0.99)
self.optimizer = torch.optim.Adam(self.parameters(), lr=3e-5, eps=1e-5)
self.scaler = torch.cuda.amp.GradScaler(enabled=self.use_amp)
@torch.no_grad()
def update_slow_critic(self, decay=0.98):
for slow_param, param in zip(
self.slow_critic.parameters(), self.critic.parameters()
):
slow_param.data.copy_(slow_param.data * decay + param.data * (1 - decay))
def policy(self, x):
logits = self.actor(x)
return logits
def value(self, x):
value = self.critic(x)
value = self.symlog_twohot_loss.decode(value)
return value
@torch.no_grad()
def slow_value(self, x):
value = self.slow_critic(x)
value = self.symlog_twohot_loss.decode(value)
return value
def get_logits_raw_value(self, x):
logits = self.actor(x)
raw_value = self.critic(x)
return logits, raw_value
@torch.no_grad()
def sample(self, latent, greedy=False):
self.eval()
with torch.autocast(
device_type="cuda", dtype=torch.bfloat16, enabled=self.use_amp
):
logits = self.policy(latent)
dist = distributions.Categorical(logits=logits)
if greedy:
action = dist.probs.argmax(dim=-1)
else:
action = dist.sample()
return action
def sample_as_env_action(self, latent, greedy=False):
action = self.sample(latent, greedy)
return action.detach().cpu().squeeze(-1).numpy()
def update(
self, latent, action, old_logprob, old_value, reward, termination, logger=None
):
"""
Update policy and value model
"""
self.train()
with torch.autocast(
device_type="cuda", dtype=torch.bfloat16, enabled=self.use_amp
):
logits, raw_value = self.get_logits_raw_value(latent)
dist = distributions.Categorical(logits=logits[:, :-1])
log_prob = dist.log_prob(action)
entropy = dist.entropy()
# decode value, calc lambda return
slow_value = self.slow_value(latent)
slow_lambda_return = calc_lambda_return(
reward, slow_value, termination, self.gamma, self.lambd
)
value = self.symlog_twohot_loss.decode(raw_value)
lambda_return = calc_lambda_return(
reward, value, termination, self.gamma, self.lambd
)
# update value function with slow critic regularization
value_loss = self.symlog_twohot_loss(
raw_value[:, :-1], lambda_return.detach()
)
slow_value_regularization_loss = self.symlog_twohot_loss(
raw_value[:, :-1], slow_lambda_return.detach()
)
lower_bound = self.lowerbound_ema(percentile(lambda_return, 0.05))
upper_bound = self.upperbound_ema(percentile(lambda_return, 0.95))
S = upper_bound - lower_bound
norm_ratio = torch.max(torch.ones(1).cuda(), S) # max(1, S) in the paper
norm_advantage = (lambda_return - value[:, :-1]) / norm_ratio
policy_loss = -(log_prob * norm_advantage.detach()).mean()
entropy_loss = entropy.mean()
loss = (
policy_loss
+ value_loss
+ slow_value_regularization_loss
- self.entropy_coef * entropy_loss
)
# gradient descent
self.scaler.scale(loss).backward()
self.scaler.unscale_(self.optimizer) # for clip grad
torch.nn.utils.clip_grad_norm_(self.parameters(), max_norm=100.0)
self.scaler.step(self.optimizer)
self.scaler.update()
self.optimizer.zero_grad(set_to_none=True)
self.update_slow_critic()
if logger is not None:
logger.log("ActorCritic/policy_loss", policy_loss.item())
logger.log("ActorCritic/value_loss", value_loss.item())
logger.log("ActorCritic/entropy_loss", entropy_loss.item())
logger.log("ActorCritic/S", S.item())
logger.log("ActorCritic/norm_ratio", norm_ratio.item())
logger.log("ActorCritic/total_loss", loss.item())