-
Notifications
You must be signed in to change notification settings - Fork 325
/
sac.py
331 lines (292 loc) · 11.3 KB
/
sac.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
from __future__ import annotations
import time
import hydra
import torch
from tensordict.nn import TensorDictModule
from tensordict.nn.distributions import NormalParamExtractor
from torch import nn
from torch.distributions import Categorical, OneHotCategorical
from torchrl._utils import logger as torchrl_logger
from torchrl.collectors import SyncDataCollector
from torchrl.data import TensorDictReplayBuffer
from torchrl.data.replay_buffers.samplers import SamplerWithoutReplacement
from torchrl.data.replay_buffers.storages import LazyTensorStorage
from torchrl.envs import RewardSum, TransformedEnv
from torchrl.envs.libs.vmas import VmasEnv
from torchrl.envs.utils import ExplorationType, set_exploration_type
from torchrl.modules import ProbabilisticActor, TanhNormal, ValueOperator
from torchrl.modules.models.multiagent import MultiAgentMLP
from torchrl.objectives import DiscreteSACLoss, SACLoss, SoftUpdate, ValueEstimators
from utils.logging import init_logging, log_evaluation, log_training
from utils.utils import DoneTransform
def rendering_callback(env, td):
env.frames.append(env.render(mode="rgb_array", agent_index_focus=None))
@hydra.main(version_base="1.1", config_path="", config_name="sac")
def train(cfg: "DictConfig"): # noqa: F821
# Device
cfg.train.device = "cpu" if not torch.cuda.device_count() else "cuda:0"
cfg.env.device = cfg.train.device
# Seeding
torch.manual_seed(cfg.seed)
# Sampling
cfg.env.vmas_envs = cfg.collector.frames_per_batch // cfg.env.max_steps
cfg.collector.total_frames = cfg.collector.frames_per_batch * cfg.collector.n_iters
cfg.buffer.memory_size = cfg.collector.frames_per_batch
# Create env and env_test
env = VmasEnv(
scenario=cfg.env.scenario_name,
num_envs=cfg.env.vmas_envs,
continuous_actions=cfg.env.continuous_actions,
max_steps=cfg.env.max_steps,
device=cfg.env.device,
seed=cfg.seed,
categorical_actions=cfg.env.categorical_actions,
# Scenario kwargs
**cfg.env.scenario,
)
env = TransformedEnv(
env,
RewardSum(in_keys=[env.reward_key], out_keys=[("agents", "episode_reward")]),
)
env_test = VmasEnv(
scenario=cfg.env.scenario_name,
num_envs=cfg.eval.evaluation_episodes,
continuous_actions=cfg.env.continuous_actions,
max_steps=cfg.env.max_steps,
device=cfg.env.device,
seed=cfg.seed,
# Scenario kwargs
**cfg.env.scenario,
)
# Policy
if cfg.env.continuous_actions:
actor_net = nn.Sequential(
MultiAgentMLP(
n_agent_inputs=env.observation_spec["agents", "observation"].shape[-1],
n_agent_outputs=2 * env.action_spec.shape[-1],
n_agents=env.n_agents,
centralised=False,
share_params=cfg.model.shared_parameters,
device=cfg.train.device,
depth=2,
num_cells=256,
activation_class=nn.Tanh,
),
NormalParamExtractor(),
)
policy_module = TensorDictModule(
actor_net,
in_keys=[("agents", "observation")],
out_keys=[("agents", "loc"), ("agents", "scale")],
)
policy = ProbabilisticActor(
module=policy_module,
spec=env.full_action_spec_unbatched,
in_keys=[("agents", "loc"), ("agents", "scale")],
out_keys=[env.action_key],
distribution_class=TanhNormal,
distribution_kwargs={
"low": env.full_action_spec_unbatched[("agents", "action")].space.low,
"high": env.full_action_spec_unbatched[("agents", "action")].space.high,
},
return_log_prob=True,
)
# Critic
module = MultiAgentMLP(
n_agent_inputs=env.observation_spec["agents", "observation"].shape[-1]
+ env.action_spec.shape[-1], # Q critic takes action and value
n_agent_outputs=1,
n_agents=env.n_agents,
centralised=cfg.model.centralised_critic,
share_params=cfg.model.shared_parameters,
device=cfg.train.device,
depth=2,
num_cells=256,
activation_class=nn.Tanh,
)
value_module = ValueOperator(
module=module,
in_keys=[("agents", "observation"), env.action_key],
out_keys=[("agents", "state_action_value")],
)
else:
actor_net = nn.Sequential(
MultiAgentMLP(
n_agent_inputs=env.observation_spec["agents", "observation"].shape[-1],
n_agent_outputs=env.action_spec.space.n,
n_agents=env.n_agents,
centralised=False,
share_params=cfg.model.shared_parameters,
device=cfg.train.device,
depth=2,
num_cells=256,
activation_class=nn.Tanh,
),
)
policy_module = TensorDictModule(
actor_net,
in_keys=[("agents", "observation")],
out_keys=[("agents", "logits")],
)
policy = ProbabilisticActor(
module=policy_module,
spec=env.full_action_spec_unbatched,
in_keys=[("agents", "logits")],
out_keys=[env.action_key],
distribution_class=OneHotCategorical
if not cfg.env.categorical_actions
else Categorical,
return_log_prob=True,
)
# Critic
module = MultiAgentMLP(
n_agent_inputs=env.observation_spec["agents", "observation"].shape[-1],
n_agent_outputs=env.action_spec.space.n,
n_agents=env.n_agents,
centralised=cfg.model.centralised_critic,
share_params=cfg.model.shared_parameters,
device=cfg.train.device,
depth=2,
num_cells=256,
activation_class=nn.Tanh,
)
value_module = ValueOperator(
module=module,
in_keys=[("agents", "observation")],
out_keys=[("agents", "action_value")],
)
collector = SyncDataCollector(
env,
policy,
device=cfg.env.device,
storing_device=cfg.train.device,
frames_per_batch=cfg.collector.frames_per_batch,
total_frames=cfg.collector.total_frames,
postproc=DoneTransform(reward_key=env.reward_key, done_keys=env.done_keys),
)
replay_buffer = TensorDictReplayBuffer(
storage=LazyTensorStorage(cfg.buffer.memory_size, device=cfg.train.device),
sampler=SamplerWithoutReplacement(),
batch_size=cfg.train.minibatch_size,
)
if cfg.env.continuous_actions:
loss_module = SACLoss(
actor_network=policy,
qvalue_network=value_module,
delay_qvalue=True,
action_spec=env.full_action_spec_unbatched,
)
loss_module.set_keys(
state_action_value=("agents", "state_action_value"),
action=env.action_key,
reward=env.reward_key,
done=("agents", "done"),
terminated=("agents", "terminated"),
)
else:
loss_module = DiscreteSACLoss(
actor_network=policy,
qvalue_network=value_module,
delay_qvalue=True,
num_actions=env.action_spec.space.n,
action_space=env.full_action_spec_unbatched,
)
loss_module.set_keys(
action_value=("agents", "action_value"),
action=env.action_key,
reward=env.reward_key,
done=("agents", "done"),
terminated=("agents", "terminated"),
)
loss_module.make_value_estimator(ValueEstimators.TD0, gamma=cfg.loss.gamma)
target_net_updater = SoftUpdate(loss_module, eps=1 - cfg.loss.tau)
optim = torch.optim.Adam(loss_module.parameters(), cfg.train.lr)
# Logging
if cfg.logger.backend:
model_name = (
("Het" if not cfg.model.shared_parameters else "")
+ ("MA" if cfg.model.centralised_critic else "I")
+ "SAC"
)
logger = init_logging(cfg, model_name)
total_time = 0
total_frames = 0
sampling_start = time.time()
for i, tensordict_data in enumerate(collector):
torchrl_logger.info(f"\nIteration {i}")
sampling_time = time.time() - sampling_start
current_frames = tensordict_data.numel()
total_frames += current_frames
data_view = tensordict_data.reshape(-1)
replay_buffer.extend(data_view)
training_tds = []
training_start = time.time()
for _ in range(cfg.train.num_epochs):
for _ in range(cfg.collector.frames_per_batch // cfg.train.minibatch_size):
subdata = replay_buffer.sample()
loss_vals = loss_module(subdata)
training_tds.append(loss_vals.detach())
loss_value = (
loss_vals["loss_actor"]
+ loss_vals["loss_alpha"]
+ loss_vals["loss_qvalue"]
)
loss_value.backward()
total_norm = torch.nn.utils.clip_grad_norm_(
loss_module.parameters(), cfg.train.max_grad_norm
)
training_tds[-1].set("grad_norm", total_norm.mean())
optim.step()
optim.zero_grad()
target_net_updater.step()
collector.update_policy_weights_()
training_time = time.time() - training_start
iteration_time = sampling_time + training_time
total_time += iteration_time
training_tds = torch.stack(training_tds)
# More logs
if cfg.logger.backend:
log_training(
logger,
training_tds,
tensordict_data,
sampling_time,
training_time,
total_time,
i,
current_frames,
total_frames,
step=i,
)
if (
cfg.eval.evaluation_episodes > 0
and i % cfg.eval.evaluation_interval == 0
and cfg.logger.backend
):
evaluation_start = time.time()
with torch.no_grad(), set_exploration_type(ExplorationType.DETERMINISTIC):
env_test.frames = []
rollouts = env_test.rollout(
max_steps=cfg.env.max_steps,
policy=policy,
callback=rendering_callback,
auto_cast_to_device=True,
break_when_any_done=False,
# We are running vectorized evaluation we do not want it to stop when just one env is done
)
evaluation_time = time.time() - evaluation_start
log_evaluation(logger, rollouts, env_test, evaluation_time, step=i)
if cfg.logger.backend == "wandb":
logger.experiment.log({}, commit=True)
sampling_start = time.time()
collector.shutdown()
if not env.is_closed:
env.close()
if not env_test.is_closed:
env_test.close()
if __name__ == "__main__":
train()