forked from ray-project/ray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rlmodule_guide.py
448 lines (343 loc) · 13.7 KB
/
rlmodule_guide.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
# flake8: noqa
from ray.rllib.utils.annotations import override
from ray.rllib.core.models.specs.typing import SpecType
from ray.rllib.core.models.specs.specs_base import TensorSpec
# __enabling-rlmodules-in-configs-begin__
import torch
from pprint import pprint
from ray.rllib.algorithms.ppo import PPOConfig
config = (
PPOConfig()
.framework("torch")
.environment("CartPole-v1")
.rl_module(_enable_rl_module_api=True)
.training(_enable_learner_api=True)
)
algorithm = config.build()
# run for 2 training steps
for _ in range(2):
result = algorithm.train()
pprint(result)
# __enabling-rlmodules-in-configs-end__
# __constructing-rlmodules-sa-begin__
import gymnasium as gym
from ray.rllib.core.rl_module.rl_module import SingleAgentRLModuleSpec
from ray.rllib.core.testing.torch.bc_module import DiscreteBCTorchModule
env = gym.make("CartPole-v1")
spec = SingleAgentRLModuleSpec(
module_class=DiscreteBCTorchModule,
observation_space=env.observation_space,
action_space=env.action_space,
model_config_dict={"fcnet_hiddens": [64]},
)
module = spec.build()
# __constructing-rlmodules-sa-end__
# __constructing-rlmodules-ma-begin__
import gymnasium as gym
from ray.rllib.core.rl_module.rl_module import SingleAgentRLModuleSpec
from ray.rllib.core.rl_module.marl_module import MultiAgentRLModuleSpec
from ray.rllib.core.testing.torch.bc_module import DiscreteBCTorchModule
spec = MultiAgentRLModuleSpec(
module_specs={
"module_1": SingleAgentRLModuleSpec(
module_class=DiscreteBCTorchModule,
observation_space=gym.spaces.Box(low=-1, high=1, shape=(10,)),
action_space=gym.spaces.Discrete(2),
model_config_dict={"fcnet_hiddens": [32]},
),
"module_2": SingleAgentRLModuleSpec(
module_class=DiscreteBCTorchModule,
observation_space=gym.spaces.Box(low=-1, high=1, shape=(5,)),
action_space=gym.spaces.Discrete(2),
model_config_dict={"fcnet_hiddens": [16]},
),
},
)
marl_module = spec.build()
# __constructing-rlmodules-ma-end__
# __pass-specs-to-configs-sa-begin__
import gymnasium as gym
from ray.rllib.core.rl_module.rl_module import SingleAgentRLModuleSpec
from ray.rllib.core.testing.torch.bc_module import DiscreteBCTorchModule
from ray.rllib.core.testing.bc_algorithm import BCConfigTest
config = (
BCConfigTest()
.environment("CartPole-v1")
.rl_module(
_enable_rl_module_api=True,
rl_module_spec=SingleAgentRLModuleSpec(module_class=DiscreteBCTorchModule),
)
.training(
model={"fcnet_hiddens": [32, 32]},
_enable_learner_api=True,
)
)
algo = config.build()
# __pass-specs-to-configs-sa-end__
# __pass-specs-to-configs-ma-begin__
import gymnasium as gym
from ray.rllib.core.rl_module.rl_module import SingleAgentRLModuleSpec
from ray.rllib.core.rl_module.marl_module import MultiAgentRLModuleSpec
from ray.rllib.core.testing.torch.bc_module import DiscreteBCTorchModule
from ray.rllib.core.testing.bc_algorithm import BCConfigTest
from ray.rllib.examples.env.multi_agent import MultiAgentCartPole
config = (
BCConfigTest()
.environment(MultiAgentCartPole, env_config={"num_agents": 2})
.rl_module(
_enable_rl_module_api=True,
rl_module_spec=MultiAgentRLModuleSpec(
module_specs=SingleAgentRLModuleSpec(module_class=DiscreteBCTorchModule)
),
)
.training(
model={"fcnet_hiddens": [32, 32]},
_enable_learner_api=True,
)
)
# __pass-specs-to-configs-ma-end__
# __convert-sa-to-ma-begin__
import gymnasium as gym
from ray.rllib.core.rl_module.rl_module import SingleAgentRLModuleSpec
from ray.rllib.core.testing.torch.bc_module import DiscreteBCTorchModule
env = gym.make("CartPole-v1")
spec = SingleAgentRLModuleSpec(
module_class=DiscreteBCTorchModule,
observation_space=env.observation_space,
action_space=env.action_space,
model_config_dict={"fcnet_hiddens": [64]},
)
module = spec.build()
marl_module = module.as_multi_agent()
# __convert-sa-to-ma-end__
# __write-custom-sa-rlmodule-torch-begin__
from typing import Mapping, Any
from ray.rllib.core.rl_module.torch.torch_rl_module import TorchRLModule
from ray.rllib.core.rl_module.rl_module import RLModuleConfig
from ray.rllib.utils.nested_dict import NestedDict
import torch
import torch.nn as nn
class DiscreteBCTorchModule(TorchRLModule):
def __init__(self, config: RLModuleConfig) -> None:
super().__init__(config)
def setup(self):
input_dim = self.config.observation_space.shape[0]
hidden_dim = self.config.model_config_dict["fcnet_hiddens"][0]
output_dim = self.config.action_space.n
self.policy = nn.Sequential(
nn.Linear(input_dim, hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim, output_dim),
)
self.input_dim = input_dim
def _forward_inference(self, batch: NestedDict) -> Mapping[str, Any]:
with torch.no_grad():
return self._forward_train(batch)
def _forward_exploration(self, batch: NestedDict) -> Mapping[str, Any]:
with torch.no_grad():
return self._forward_train(batch)
def _forward_train(self, batch: NestedDict) -> Mapping[str, Any]:
action_logits = self.policy(batch["obs"])
return {"action_dist": torch.distributions.Categorical(logits=action_logits)}
# __write-custom-sa-rlmodule-torch-end__
# __write-custom-sa-rlmodule-tf-begin__
from typing import Mapping, Any
from ray.rllib.core.rl_module.tf.tf_rl_module import TfRLModule
from ray.rllib.core.rl_module.rl_module import RLModuleConfig
from ray.rllib.utils.nested_dict import NestedDict
import tensorflow as tf
class DiscreteBCTfModule(TfRLModule):
def __init__(self, config: RLModuleConfig) -> None:
super().__init__(config)
def setup(self):
input_dim = self.config.observation_space.shape[0]
hidden_dim = self.config.model_config_dict["fcnet_hiddens"][0]
output_dim = self.config.action_space.n
self.policy = tf.keras.Sequential(
[
tf.keras.layers.Dense(hidden_dim, activation="relu"),
tf.keras.layers.Dense(output_dim),
]
)
self.input_dim = input_dim
def _forward_inference(self, batch: NestedDict) -> Mapping[str, Any]:
return self._forward_train(batch)
def _forward_exploration(self, batch: NestedDict) -> Mapping[str, Any]:
return self._forward_train(batch)
def _forward_train(self, batch: NestedDict) -> Mapping[str, Any]:
action_logits = self.policy(batch["obs"])
return {"action_dist": tf.distributions.Categorical(logits=action_logits)}
# __write-custom-sa-rlmodule-tf-end__
# __extend-spec-checking-single-level-begin__
class DiscreteBCTorchModule(TorchRLModule):
...
@override(TorchRLModule)
def input_specs_exploration(self) -> SpecType:
# Enforce that input nested dict to exploration method has a key "obs"
return ["obs"]
@override(TorchRLModule)
def output_specs_exploration(self) -> SpecType:
# Enforce that output nested dict from exploration method has a key
# "action_dist"
return ["action_dist"]
# __extend-spec-checking-single-level-end__
# __extend-spec-checking-nested-begin__
class DiscreteBCTorchModule(TorchRLModule):
...
@override(TorchRLModule)
def input_specs_exploration(self) -> SpecType:
# Enforce that input nested dict to exploration method has a key "obs"
# and within that key, it has a key "global" and "local". There should
# also be a key "action_mask"
return [("obs", "global"), ("obs", "local"), "action_mask"]
# __extend-spec-checking-nested-end__
# __extend-spec-checking-torch-specs-begin__
class DiscreteBCTorchModule(TorchRLModule):
...
@override(TorchRLModule)
def input_specs_exploration(self) -> SpecType:
# Enforce that input nested dict to exploration method has a key "obs"
# and its value is a torch.Tensor with shape (b, h) where b is the
# batch size (determined at run-time) and h is the hidden size
# (fixed at 10).
return {"obs": TensorSpec("b, h", h=10, framework="torch")}
# __extend-spec-checking-torch-specs-end__
# __extend-spec-checking-type-specs-begin__
class DiscreteBCTorchModule(TorchRLModule):
...
@override(TorchRLModule)
def output_specs_exploration(self) -> SpecType:
# Enforce that output nested dict from exploration method has a key
# "action_dist" and its value is a torch.distribution.Categorical
return {"action_dist": torch.distribution.Categorical}
# __extend-spec-checking-type-specs-end__
# __write-custom-marlmodule-shared-enc-begin__
from ray.rllib.core.rl_module.torch.torch_rl_module import TorchRLModule
from ray.rllib.core.rl_module.marl_module import (
MultiAgentRLModuleConfig,
MultiAgentRLModule,
)
from ray.rllib.utils.nested_dict import NestedDict
import torch
import torch.nn as nn
class BCTorchRLModuleWithSharedGlobalEncoder(TorchRLModule):
"""An RLModule with a shared encoder between agents for global observation."""
def __init__(
self, encoder: nn.Module, local_dim: int, hidden_dim: int, action_dim: int
) -> None:
super().__init__(config=None)
self.encoder = encoder
self.policy_head = nn.Sequential(
nn.Linear(hidden_dim + local_dim, hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim, action_dim),
)
def _forward_inference(self, batch):
with torch.no_grad():
return self._common_forward(batch)
def _forward_exploration(self, batch):
with torch.no_grad():
return self._common_forward(batch)
def _forward_train(self, batch):
return self._common_forward(batch)
def _common_forward(self, batch):
obs = batch["obs"]
global_enc = self.encoder(obs["global"])
policy_in = torch.cat([global_enc, obs["local"]], dim=-1)
action_logits = self.policy_head(policy_in)
return {"action_dist": torch.distributions.Categorical(logits=action_logits)}
class BCTorchMultiAgentModuleWithSharedEncoder(MultiAgentRLModule):
def __init__(self, config: MultiAgentRLModuleConfig) -> None:
super().__init__(config)
def setup(self):
module_specs = self.config.modules
module_spec = next(iter(module_specs.values()))
global_dim = module_spec.observation_space["global"].shape[0]
hidden_dim = module_spec.model_config_dict["fcnet_hiddens"][0]
shared_encoder = nn.Sequential(
nn.Linear(global_dim, hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim, hidden_dim),
)
rl_modules = {}
for module_id, module_spec in module_specs.items():
rl_modules[module_id] = BCTorchRLModuleWithSharedGlobalEncoder(
encoder=shared_encoder,
local_dim=module_spec.observation_space["local"].shape[0],
hidden_dim=hidden_dim,
action_dim=module_spec.action_space.n,
)
self._rl_modules = rl_modules
# __write-custom-marlmodule-shared-enc-end__
# __pass-custom-marlmodule-shared-enc-begin__
import gymnasium as gym
from ray.rllib.core.rl_module.rl_module import SingleAgentRLModuleSpec
from ray.rllib.core.rl_module.marl_module import MultiAgentRLModuleSpec
spec = MultiAgentRLModuleSpec(
marl_module_class=BCTorchMultiAgentModuleWithSharedEncoder,
module_specs={
"local_2d": SingleAgentRLModuleSpec(
observation_space=gym.spaces.Dict(
{
"global": gym.spaces.Box(low=-1, high=1, shape=(2,)),
"local": gym.spaces.Box(low=-1, high=1, shape=(2,)),
}
),
action_space=gym.spaces.Discrete(2),
model_config_dict={"fcnet_hiddens": [64]},
),
"local_5d": SingleAgentRLModuleSpec(
observation_space=gym.spaces.Dict(
{
"global": gym.spaces.Box(low=-1, high=1, shape=(2,)),
"local": gym.spaces.Box(low=-1, high=1, shape=(5,)),
}
),
action_space=gym.spaces.Discrete(5),
model_config_dict={"fcnet_hiddens": [64]},
),
},
)
module = spec.build()
# __pass-custom-marlmodule-shared-enc-end__
# __checkpointing-begin__
import gymnasium as gym
import shutil
import tempfile
from ray.rllib.algorithms.ppo import PPOConfig
from ray.rllib.algorithms.ppo.ppo_catalog import PPOCatalog
from ray.rllib.algorithms.ppo.torch.ppo_torch_rl_module import PPOTorchRLModule
from ray.rllib.core.rl_module.rl_module import SingleAgentRLModuleSpec
config = PPOConfig().environment("CartPole-v1")
env = gym.make("CartPole-v1")
# Create an RL Module that we would like to checkpoint
module_spec = SingleAgentRLModuleSpec(
module_class=PPOTorchRLModule,
observation_space=env.observation_space,
action_space=env.action_space,
model_config_dict={"fcnet_hiddens": [32]},
catalog_class=PPOCatalog,
)
module = module_spec.build()
# Create the checkpoint
module_ckpt_path = tempfile.mkdtemp()
module.save_to_checkpoint(module_ckpt_path)
# Create a new RL Module from the checkpoint
module_to_load_spec = SingleAgentRLModuleSpec(
module_class=PPOTorchRLModule,
observation_space=env.observation_space,
action_space=env.action_space,
model_config_dict={"fcnet_hiddens": [32]},
catalog_class=PPOCatalog,
load_state_path=module_ckpt_path,
)
# Train with the checkpointed RL Module
config.rl_module(
rl_module_spec=module_to_load_spec,
_enable_rl_module_api=True,
)
algo = config.build()
algo.train()
# __checkpointing-end__
algo.stop()
shutil.rmtree(module_ckpt_path)