-
Notifications
You must be signed in to change notification settings - Fork 307
/
qmix_vdn.py
270 lines (233 loc) · 9.02 KB
/
qmix_vdn.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
# 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.
import time
import hydra
import torch
from tensordict.nn import TensorDictModule, TensorDictSequential
from torch import nn
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 EGreedyModule, QValueModule, SafeSequential
from torchrl.modules.models.multiagent import MultiAgentMLP, QMixer, VDNMixer
from torchrl.objectives import SoftUpdate, ValueEstimators
from torchrl.objectives.multiagent.qmixer import QMixerLoss
from utils.logging import init_logging, log_evaluation, log_training
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="qmix_vdn")
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=False,
max_steps=cfg.env.max_steps,
device=cfg.env.device,
seed=cfg.seed,
# 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=False,
max_steps=cfg.env.max_steps,
device=cfg.env.device,
seed=cfg.seed,
# Scenario kwargs
**cfg.env.scenario,
)
# Policy
net = 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,
)
module = TensorDictModule(
net, in_keys=[("agents", "observation")], out_keys=[("agents", "action_value")]
)
value_module = QValueModule(
action_value_key=("agents", "action_value"),
out_keys=[
env.action_key,
("agents", "action_value"),
("agents", "chosen_action_value"),
],
spec=env.unbatched_action_spec,
action_space=None,
)
qnet = SafeSequential(module, value_module)
qnet_explore = TensorDictSequential(
qnet,
EGreedyModule(
eps_init=0.3,
eps_end=0,
annealing_num_steps=int(cfg.collector.total_frames * (1 / 2)),
action_key=env.action_key,
spec=env.unbatched_action_spec,
),
)
if cfg.loss.mixer_type == "qmix":
mixer = TensorDictModule(
module=QMixer(
state_shape=env.unbatched_observation_spec[
"agents", "observation"
].shape,
mixing_embed_dim=32,
n_agents=env.n_agents,
device=cfg.train.device,
),
in_keys=[("agents", "chosen_action_value"), ("agents", "observation")],
out_keys=["chosen_action_value"],
)
elif cfg.loss.mixer_type == "vdn":
mixer = TensorDictModule(
module=VDNMixer(
n_agents=env.n_agents,
device=cfg.train.device,
),
in_keys=[("agents", "chosen_action_value")],
out_keys=["chosen_action_value"],
)
else:
raise ValueError("Mixer type not in the example")
collector = SyncDataCollector(
env,
qnet_explore,
device=cfg.env.device,
storing_device=cfg.train.device,
frames_per_batch=cfg.collector.frames_per_batch,
total_frames=cfg.collector.total_frames,
)
replay_buffer = TensorDictReplayBuffer(
storage=LazyTensorStorage(cfg.buffer.memory_size, device=cfg.train.device),
sampler=SamplerWithoutReplacement(),
batch_size=cfg.train.minibatch_size,
)
loss_module = QMixerLoss(qnet, mixer, delay_value=True)
loss_module.set_keys(
action_value=("agents", "action_value"),
local_value=("agents", "chosen_action_value"),
global_value="chosen_action_value",
action=env.action_key,
)
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 ""
) + cfg.loss.mixer_type.upper()
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
# Remove agent dimension from reward (since it is shared in QMIX/VDN)
tensordict_data.set(
("next", "reward"), tensordict_data.get(("next", env.reward_key)).mean(-2)
)
del tensordict_data["next", env.reward_key]
tensordict_data.set(
("next", "episode_reward"),
tensordict_data.get(("next", "agents", "episode_reward")).mean(-2),
)
del tensordict_data["next", "agents", "episode_reward"]
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"]
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()
qnet_explore[1].step(frames=current_frames) # Update exploration annealing
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=qnet,
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()