-
Notifications
You must be signed in to change notification settings - Fork 40
/
common.py
493 lines (419 loc) · 17.4 KB
/
common.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
#
import pathlib
from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple, Type
from tensordict import TensorDictBase
from tensordict.nn import TensorDictModule, TensorDictSequential
from torchrl.data import (
Categorical,
Composite,
LazyTensorStorage,
OneHot,
ReplayBuffer,
TensorDictReplayBuffer,
)
from torchrl.data.replay_buffers import RandomSampler, SamplerWithoutReplacement
from torchrl.envs import (
Compose,
EnvBase,
InitTracker,
TensorDictPrimer,
Transform,
TransformedEnv,
)
from torchrl.objectives import LossModule
from torchrl.objectives.utils import HardUpdate, SoftUpdate, TargetNetUpdater
from benchmarl.models.common import ModelConfig
from benchmarl.utils import _read_yaml_config, DEVICE_TYPING
class Algorithm(ABC):
"""
Abstract class for an algorithm.
This should be overridden by implemented algorithms
and all abstract methods should be implemented.
Args:
experiment (Experiment): the experiment class
"""
def __init__(self, experiment):
self.experiment = experiment
self.device: DEVICE_TYPING = experiment.config.train_device
self.buffer_device: DEVICE_TYPING = experiment.config.buffer_device
self.experiment_config = experiment.config
self.model_config = experiment.model_config
self.critic_model_config = experiment.critic_model_config
self.on_policy = experiment.on_policy
self.group_map = experiment.group_map
self.observation_spec = experiment.observation_spec
self.action_spec = experiment.action_spec
self.state_spec = experiment.state_spec
self.action_mask_spec = experiment.action_mask_spec
self.has_independent_critic = (
experiment.algorithm_config.has_independent_critic()
)
self.has_centralized_critic = (
experiment.algorithm_config.has_centralized_critic()
)
self.has_critic = experiment.algorithm_config.has_critic
self.has_rnn = self.model_config.is_rnn or (
self.critic_model_config.is_rnn and self.has_critic
)
# Cached values that will be instantiated only once and then remain fixed
self._losses_and_updaters = {}
self._policies_for_loss = {}
self._policies_for_collection = {}
self._check_specs()
def _check_specs(self):
if self.state_spec is not None:
if len(self.state_spec.keys(True, True)) != 1:
raise ValueError(
"State spec must contain one entry per group"
" to follow the library conventions, "
"you can apply a transform to your environment to satisfy this criteria."
)
for group in self.group_map.keys():
if (
len(self.action_spec[group].keys(True, True)) != 1
or list(self.action_spec[group].keys())[0] != "action"
):
raise ValueError(
"Action spec must contain one entry per group named 'action'"
" to follow the library conventions, "
"you can apply a transform to your environment to satisfy this criteria."
)
if (
self.action_mask_spec is not None
and group in self.action_mask_spec.keys()
and (
len(self.action_mask_spec[group].keys(True, True)) != 1
or list(self.action_mask_spec[group].keys())[0] != "action_mask"
)
):
raise ValueError(
"Action mask spec must contain one entry per group named 'action_mask'"
" to follow the library conventions, "
"you can apply a transform to your environment to satisfy this criteria."
)
def get_loss_and_updater(self, group: str) -> Tuple[LossModule, TargetNetUpdater]:
"""
Get the LossModule and TargetNetUpdater for a specific group.
This function calls the abstract :class:`~benchmarl.algorithms.Algorithm._get_loss()` which needs to be implemented.
The function will cache the output at the first call and return the cached values in future calls.
Args:
group (str): agent group of the loss and updater
Returns: LossModule and TargetNetUpdater for the group
"""
if group not in self._losses_and_updaters.keys():
action_space = self.action_spec[group, "action"]
continuous = not isinstance(action_space, (Categorical, OneHot))
loss, use_target = self._get_loss(
group=group,
policy_for_loss=self.get_policy_for_loss(group),
continuous=continuous,
)
if use_target:
if self.experiment_config.soft_target_update:
target_net_updater = SoftUpdate(
loss, tau=self.experiment_config.polyak_tau
)
else:
target_net_updater = HardUpdate(
loss,
value_network_update_interval=self.experiment_config.hard_target_update_frequency,
)
else:
target_net_updater = None
self._losses_and_updaters.update({group: (loss, target_net_updater)})
return self._losses_and_updaters[group]
def get_replay_buffer(
self, group: str, transforms: List[Transform] = None
) -> ReplayBuffer:
"""
Get the ReplayBuffer for a specific group.
This function will check ``self.on_policy`` and create the buffer accordingly
Args:
group (str): agent group of the loss and updater
transforms (optional, list of Transform): Transforms to apply to the replay buffer ``.sample()`` call
Returns: ReplayBuffer the group
"""
memory_size = self.experiment_config.replay_buffer_memory_size(self.on_policy)
sampling_size = self.experiment_config.train_minibatch_size(self.on_policy)
if self.has_rnn:
sequence_length = -(
-self.experiment_config.collected_frames_per_batch(self.on_policy)
// self.experiment_config.n_envs_per_worker(self.on_policy)
)
memory_size = -(-memory_size // sequence_length)
sampling_size = -(-sampling_size // sequence_length)
sampler = SamplerWithoutReplacement() if self.on_policy else RandomSampler()
return TensorDictReplayBuffer(
storage=LazyTensorStorage(
memory_size,
device=self.device if self.on_policy else self.buffer_device,
),
sampler=sampler,
batch_size=sampling_size,
priority_key=(group, "td_error"),
transform=Compose(*transforms) if transforms is not None else None,
)
def get_policy_for_loss(self, group: str) -> TensorDictModule:
"""
Get the non-explorative policy for a specific group loss.
This function calls the abstract :class:`~benchmarl.algorithms.Algorithm._get_policy_for_loss()` which needs to be implemented.
The function will cache the output at the first call and return the cached values in future calls.
Args:
group (str): agent group of the policy
Returns: TensorDictModule representing the policy
"""
if group not in self._policies_for_loss.keys():
action_space = self.action_spec[group, "action"]
continuous = not isinstance(action_space, (Categorical, OneHot))
self._policies_for_loss.update(
{
group: self._get_policy_for_loss(
group=group,
continuous=continuous,
model_config=self.model_config,
)
}
)
return self._policies_for_loss[group]
def get_policy_for_collection(self) -> TensorDictSequential:
"""
Get the explorative policy for all groups together.
This function calls the abstract :class:`~benchmarl.algorithms.Algorithm._get_policy_for_collection()` which needs to be implemented.
The function will cache the output at the first call and return the cached values in future calls.
Returns: TensorDictSequential representing all explorative policies
"""
policies = []
for group in self.group_map.keys():
if group not in self._policies_for_collection.keys():
policy_for_loss = self.get_policy_for_loss(group)
action_space = self.action_spec[group, "action"]
continuous = not isinstance(action_space, (Categorical, OneHot))
policy_for_collection = self._get_policy_for_collection(
policy_for_loss,
group,
continuous,
)
self._policies_for_collection.update({group: policy_for_collection})
policies.append(self._policies_for_collection[group])
return TensorDictSequential(*policies)
def get_parameters(self, group: str) -> Dict[str, Iterable]:
"""
Get the dictionary mapping loss names to the relative parameters to optimize for a given group.
This function calls the abstract :class:`~benchmarl.algorithms.Algorithm._get_parameters()` which needs to be implemented.
Returns: a dictionary mapping loss names to a parameters' list
"""
return self._get_parameters(
group=group,
loss=self.get_loss_and_updater(group)[0],
)
def process_env_fun(
self,
env_fun: Callable[[], EnvBase],
) -> Callable[[], EnvBase]:
"""
This function can be used to wrap env_fun
Args:
env_fun (callable): a function that takes no args and creates an enviornment
Returns: a function that takes no args and creates an enviornment
"""
if self.has_rnn:
def model_fun():
env = env_fun()
spec_actor = self.model_config.get_model_state_spec()
spec_actor = Composite(
{
group: Composite(
spec_actor.expand(len(agents), *spec_actor.shape),
shape=(len(agents),),
)
for group, agents in self.group_map.items()
}
)
env = TransformedEnv(
env,
Compose(
*(
[InitTracker(init_key="is_init")]
+ (
[TensorDictPrimer(spec_actor, reset_key="_reset")]
if len(spec_actor.keys(True, True)) > 0
else []
)
)
),
)
return env
return model_fun
return env_fun
###############################
# Abstract methods to implement
###############################
@abstractmethod
def _get_loss(
self, group: str, policy_for_loss: TensorDictModule, continuous: bool
) -> Tuple[LossModule, bool]:
"""
Implement this function to return the LossModule for a specific group.
Args:
group (str): agent group of the loss
policy_for_loss (TensorDictModule): the policy to use in the loss
continuous (bool): whether to return a loss for continuous or discrete actions
Returns: LossModule and a bool representing if the loss should have target parameters
"""
raise NotImplementedError
@abstractmethod
def _get_parameters(self, group: str, loss: LossModule) -> Dict[str, Iterable]:
"""
Get the dictionary mapping loss names to the relative parameters to optimize for a given group loss.
Returns: a dictionary mapping loss names to a parameters' list
"""
raise NotImplementedError
@abstractmethod
def _get_policy_for_loss(
self, group: str, model_config: ModelConfig, continuous: bool
) -> TensorDictModule:
"""
Get the non-explorative policy for a specific group.
Args:
group (str): agent group of the policy
model_config (ModelConfig): model config class
continuous (bool): whether the policy should be continuous or discrete
Returns: TensorDictModule representing the policy
"""
raise NotImplementedError
@abstractmethod
def _get_policy_for_collection(
self, policy_for_loss: TensorDictModule, group: str, continuous: bool
) -> TensorDictModule:
"""
Implement this function to add an explorative layer to the policy used in the loss.
Args:
policy_for_loss (TensorDictModule): the group policy used in the loss
group (str): agent group
continuous (bool): whether the policy is continuous or discrete
Returns: TensorDictModule representing the explorative policy
"""
raise NotImplementedError
@abstractmethod
def process_batch(self, group: str, batch: TensorDictBase) -> TensorDictBase:
"""
This function can be used to reshape data coming from collection before it is passed to the policy.
Args:
group (str): agent group
batch (TensorDictBase): the batch of data coming from the collector
Returns: the processed batch
"""
raise NotImplementedError
def process_loss_vals(
self, group: str, loss_vals: TensorDictBase
) -> TensorDictBase:
"""
Here you can modify the loss_vals tensordict containing entries loss_name->loss_value
For example, you can sum two entries in a new entry, to optimize them together.
Args:
group (str): agent group
loss_vals (TensorDictBase): the tensordict returned by the loss forward method
Returns: the processed loss_vals
"""
return loss_vals
@dataclass
class AlgorithmConfig:
"""
Dataclass representing an algorithm configuration.
This should be overridden by implemented algorithms.
Implementors should:
1. add configuration parameters for their algorithm
2. implement all abstract methods
"""
def get_algorithm(self, experiment) -> Algorithm:
"""
Main function to turn the config into the associated algorithm
Args:
experiment (Experiment): the experiment class
Returns: the Algorithm
"""
return self.associated_class()(
**self.__dict__, # Passes all the custom config parameters
experiment=experiment,
)
@staticmethod
def _load_from_yaml(name: str) -> Dict[str, Any]:
yaml_path = (
pathlib.Path(__file__).parent.parent
/ "conf"
/ "algorithm"
/ f"{name.lower()}.yaml"
)
return _read_yaml_config(str(yaml_path.resolve()))
@classmethod
def get_from_yaml(cls, path: Optional[str] = None):
"""
Load the algorithm configuration from yaml
Args:
path (str, optional): The full path of the yaml file to load from.
If None, it will default to
``benchmarl/conf/algorithm/self.associated_class().__name__``
Returns: the loaded AlgorithmConfig
"""
if path is None:
config = AlgorithmConfig._load_from_yaml(
name=cls.associated_class().__name__
)
else:
config = _read_yaml_config(path)
return cls(**config)
@staticmethod
@abstractmethod
def associated_class() -> Type[Algorithm]:
"""
The algorithm class associated to the config
"""
raise NotImplementedError
@staticmethod
@abstractmethod
def on_policy() -> bool:
"""
If the algorithm has to be run on policy or off policy
"""
raise NotImplementedError
@staticmethod
@abstractmethod
def supports_continuous_actions() -> bool:
"""
If the algorithm supports continuous actions
"""
raise NotImplementedError
@staticmethod
@abstractmethod
def supports_discrete_actions() -> bool:
"""
If the algorithm supports discrete actions
"""
raise NotImplementedError
@staticmethod
def has_independent_critic() -> bool:
"""
If the algorithm uses an independent critic
"""
return False
@staticmethod
def has_centralized_critic() -> bool:
"""
If the algorithm uses a centralized critic
"""
return False
def has_critic(self) -> bool:
"""
If the algorithm uses a critic
"""
if self.has_centralized_critic() and self.has_independent_critic():
raise ValueError(
"Algorithm can either have a centralized critic or an indpendent one"
)
return self.has_centralized_critic() or self.has_independent_critic()