forked from pytorch/rl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_actors.py
566 lines (505 loc) · 20.3 KB
/
test_actors.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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
# 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 argparse
import pytest
import torch
from _utils_internal import get_default_devices
from tensordict import TensorDict
from tensordict.nn import TensorDictModule
from torch import nn
from torchrl.data import (
CompositeSpec,
DiscreteTensorSpec,
MultiOneHotDiscreteTensorSpec,
OneHotDiscreteTensorSpec,
)
from torchrl.modules import MLP, SafeModule
from torchrl.modules.tensordict_module.actors import (
_process_action_space_spec,
ActorValueOperator,
DistributionalQValueActor,
DistributionalQValueHook,
DistributionalQValueModule,
ProbabilisticActor,
QValueActor,
QValueHook,
QValueModule,
ValueOperator,
)
class TestQValue:
def test_qvalue_hook_wrong_action_space(self):
with pytest.raises(
ValueError, match="action_space was not specified/not compatible"
):
QValueHook(action_space="wrong_value")
def test_distributional_qvalue_hook_wrong_action_space(self):
with pytest.raises(
ValueError, match="action_space was not specified/not compatible"
):
DistributionalQValueHook(action_space="wrong_value", support=None)
def test_distributional_qvalue_hook_conflicting_spec(self):
spec = OneHotDiscreteTensorSpec(3)
_process_action_space_spec("one-hot", spec)
_process_action_space_spec("one_hot", spec)
_process_action_space_spec("one_hot", None)
_process_action_space_spec(None, spec)
with pytest.raises(
ValueError, match="The action spec and the action space do not match"
):
_process_action_space_spec("multi-one-hot", spec)
spec = MultiOneHotDiscreteTensorSpec([3, 3])
_process_action_space_spec("multi-one-hot", spec)
_process_action_space_spec(spec, spec)
with pytest.raises(
ValueError, match="Passing an action_space as a TensorSpec and a spec"
):
_process_action_space_spec(OneHotDiscreteTensorSpec(3), spec)
with pytest.raises(
ValueError, match="action_space cannot be of type CompositeSpec"
):
_process_action_space_spec(CompositeSpec(), spec)
with pytest.raises(KeyError, match="action could not be found in the spec"):
_process_action_space_spec(None, CompositeSpec())
with pytest.raises(
ValueError, match="Neither action_space nor spec was defined"
):
_process_action_space_spec(None, None)
@pytest.mark.parametrize(
"action_space, expected_action",
(
("one_hot", [0, 0, 1, 0, 0]),
("categorical", 2),
),
)
@pytest.mark.parametrize("key", ["somekey", None])
def test_qvalue_module_0_dim_batch(self, action_space, expected_action, key):
if key is not None:
module = QValueModule(action_space=action_space, action_value_key=key)
else:
module = QValueModule(action_space=action_space)
key = "action_value"
in_values = torch.tensor([1.0, -1.0, 100.0, -2.0, -3.0])
# test tensor
action, values, chosen_action_value = module(in_values)
assert (torch.tensor(expected_action, dtype=torch.long) == action).all()
assert (values == in_values).all()
assert (torch.tensor([100.0]) == chosen_action_value).all()
# test tensor, keyword
action, values, chosen_action_value = module(**{key: in_values})
assert (torch.tensor(expected_action, dtype=torch.long) == action).all()
assert (values == in_values).all()
assert (torch.tensor([100.0]) == chosen_action_value).all()
# test tensor, tensordict
td = module(TensorDict({key: in_values}, []))
action = td["action"]
values = td[key]
if key != "action_value_keys":
assert "action_value_keys" not in td.keys()
chosen_action_value = td["chosen_action_value"]
assert (torch.tensor(expected_action, dtype=torch.long) == action).all()
assert (values == in_values).all()
assert (torch.tensor([100.0]) == chosen_action_value).all()
@pytest.mark.parametrize(
"action_space, expected_action",
(
("one_hot", [0, 0, 1, 0, 0]),
("categorical", 2),
),
)
@pytest.mark.parametrize("model_type", ["td", "nn"])
@pytest.mark.parametrize("key", ["somekey", None])
def test_qvalue_actor_0_dim_batch(
self, action_space, expected_action, key, model_type
):
if model_type == "nn":
model = nn.Identity()
else:
out_keys = ["action_value"] if key is None else [key]
model = TensorDictModule(
nn.Identity(),
in_keys=["observation"],
out_keys=out_keys,
)
if key is not None:
module = QValueActor(model, action_space=action_space, action_value_key=key)
else:
module = QValueActor(model, action_space=action_space)
key = "action_value"
in_values = torch.tensor([1.0, -1.0, 100.0, -2.0, -3.0])
# test tensor
action, values, chosen_action_value = module(in_values)
assert (torch.tensor(expected_action, dtype=torch.long) == action).all()
assert (values == in_values).all()
assert (torch.tensor([100.0]) == chosen_action_value).all()
# test tensor, keyword
action, values, chosen_action_value = module(**{"observation": in_values})
assert (torch.tensor(expected_action, dtype=torch.long) == action).all()
assert (values == in_values).all()
assert (torch.tensor([100.0]) == chosen_action_value).all()
# test tensor, tensordict
td = module(TensorDict({"observation": in_values}, []))
action = td["action"]
values = td[key]
if key != "action_value_keys":
assert "action_value_keys" not in td.keys()
chosen_action_value = td["chosen_action_value"]
assert (torch.tensor(expected_action, dtype=torch.long) == action).all()
assert (values == in_values).all()
assert (torch.tensor([100.0]) == chosen_action_value).all()
@pytest.mark.parametrize(
"action_space, expected_action",
(
("one_hot", [0, 0, 1, 0, 0]),
("categorical", 2),
),
)
def test_qvalue_hook_0_dim_batch(self, action_space, expected_action):
hook = QValueHook(action_space=action_space)
in_values = torch.tensor([1.0, -1.0, 100.0, -2.0, -3.0])
action, values, chosen_action_value = hook(
net=None, observation=None, values=in_values
)
assert (torch.tensor(expected_action, dtype=torch.long) == action).all()
assert (values == in_values).all()
assert (torch.tensor([100.0]) == chosen_action_value).all()
@pytest.mark.parametrize(
"action_space, expected_action",
(
("one_hot", [[0, 0, 1, 0, 0], [1, 0, 0, 0, 0]]),
("categorical", [2, 0]),
),
)
def test_qvalue_hook_1_dim_batch(self, action_space, expected_action):
hook = QValueHook(action_space=action_space)
in_values = torch.tensor(
[
[1.0, -1.0, 100.0, -2.0, -3.0],
[5.0, 4.0, 3.0, 2.0, -5.0],
]
)
action, values, chosen_action_value = hook(
net=None, observation=None, values=in_values
)
assert (torch.tensor(expected_action, dtype=torch.long) == action).all()
assert (values == in_values).all()
assert (torch.tensor([[100.0], [5.0]]) == chosen_action_value).all()
@pytest.mark.parametrize(
"action_space, expected_action",
(
("one_hot", [0, 0, 1, 0, 0]),
("categorical", 2),
),
)
@pytest.mark.parametrize("key", ["somekey", None])
def test_distributional_qvalue_module_0_dim_batch(
self, action_space, expected_action, key
):
support = torch.tensor([-2.0, 0.0, 2.0])
if key is not None:
module = DistributionalQValueModule(
action_space=action_space, support=support, action_value_key=key
)
else:
key = "action_value"
module = DistributionalQValueModule(
action_space=action_space, support=support
)
in_values = torch.nn.LogSoftmax(dim=-1)(
torch.tensor(
[
[1.0, -1.0, 11.0, -2.0, 30.0],
[1.0, -1.0, 1.0, -2.0, -3.0],
[1.0, -1.0, 10.0, -2.0, -3.0],
]
)
)
# tensor
action, values = module(in_values)
expected_action = torch.tensor(expected_action, dtype=torch.long)
assert action.shape == expected_action.shape
assert (action == expected_action).all()
assert values.shape == in_values.shape
assert (values == in_values).all()
# tensor, keyword
action, values = module(**{key: in_values})
expected_action = torch.tensor(expected_action, dtype=torch.long)
assert action.shape == expected_action.shape
assert (action == expected_action).all()
assert values.shape == in_values.shape
assert (values == in_values).all()
# tensor, tensordict
td = module(TensorDict({key: in_values}, []))
action = td["action"]
values = td[key]
if key != "action_value":
assert "action_value" not in td.keys()
expected_action = torch.tensor(expected_action, dtype=torch.long)
assert action.shape == expected_action.shape
assert (action == expected_action).all()
assert values.shape == in_values.shape
assert (values == in_values).all()
@pytest.mark.parametrize(
"action_space, expected_action",
(
("one_hot", [0, 0, 1, 0, 0]),
("categorical", 2),
),
)
@pytest.mark.parametrize("model_type", ["td", "nn"])
@pytest.mark.parametrize("key", ["somekey", None])
def test_distributional_qvalue_actor_0_dim_batch(
self, action_space, expected_action, key, model_type
):
support = torch.tensor([-2.0, 0.0, 2.0])
if model_type == "nn":
model = nn.Identity()
else:
if key is not None:
model = TensorDictModule(
nn.Identity(), in_keys=["observation"], out_keys=[key]
)
else:
model = TensorDictModule(
nn.Identity(), in_keys=["observation"], out_keys=["action_value"]
)
if key is not None:
module = DistributionalQValueActor(
model, action_space=action_space, support=support, action_value_key=key
)
else:
key = "action_value"
module = DistributionalQValueActor(
model, action_space=action_space, support=support
)
in_values = torch.nn.LogSoftmax(dim=-1)(
torch.tensor(
[
[1.0, -1.0, 11.0, -2.0, 30.0],
[1.0, -1.0, 1.0, -2.0, -3.0],
[1.0, -1.0, 10.0, -2.0, -3.0],
]
)
)
# tensor
action, values = module(in_values)
expected_action = torch.tensor(expected_action, dtype=torch.long)
assert action.shape == expected_action.shape
assert (action == expected_action).all()
assert values.shape == in_values.shape
assert (values == in_values.log_softmax(-2)).all()
# tensor, keyword
action, values = module(observation=in_values)
expected_action = torch.tensor(expected_action, dtype=torch.long)
assert action.shape == expected_action.shape
assert (action == expected_action).all()
assert values.shape == in_values.shape
assert (values == in_values.log_softmax(-2)).all()
# tensor, tensordict
td = module(TensorDict({"observation": in_values}, []))
action = td["action"]
values = td[key]
expected_action = torch.tensor(expected_action, dtype=torch.long)
assert action.shape == expected_action.shape
assert (action == expected_action).all()
assert values.shape == in_values.shape
assert (values == in_values.log_softmax(-2)).all()
@pytest.mark.parametrize(
"action_space, expected_action",
(
("one_hot", [0, 0, 1, 0, 0]),
("categorical", 2),
),
)
def test_distributional_qvalue_hook_0_dim_batch(
self, action_space, expected_action
):
support = torch.tensor([-2.0, 0.0, 2.0])
hook = DistributionalQValueHook(action_space=action_space, support=support)
in_values = torch.nn.LogSoftmax(dim=-1)(
torch.tensor(
[
[1.0, -1.0, 11.0, -2.0, 30.0],
[1.0, -1.0, 1.0, -2.0, -3.0],
[1.0, -1.0, 10.0, -2.0, -3.0],
]
)
)
action, values = hook(net=None, observation=None, values=in_values)
expected_action = torch.tensor(expected_action, dtype=torch.long)
assert action.shape == expected_action.shape
assert (action == expected_action).all()
assert values.shape == in_values.shape
assert (values == in_values).all()
@pytest.mark.parametrize(
"action_space, expected_action",
(
("one_hot", [[0, 0, 1, 0, 0], [1, 0, 0, 0, 0]]),
("categorical", [2, 0]),
),
)
def test_qvalue_hook_categorical_1_dim_batch(self, action_space, expected_action):
support = torch.tensor([-2.0, 0.0, 2.0])
hook = DistributionalQValueHook(action_space=action_space, support=support)
in_values = torch.nn.LogSoftmax(dim=-1)(
torch.tensor(
[
[
[1.0, -1.0, 11.0, -2.0, 30.0],
[1.0, -1.0, 1.0, -2.0, -3.0],
[1.0, -1.0, 10.0, -2.0, -3.0],
],
[
[11.0, -1.0, 7.0, -1.0, 20.0],
[10.0, 19.0, 1.0, -2.0, -3.0],
[1.0, -1.0, 0.0, -2.0, -3.0],
],
]
)
)
action, values = hook(net=None, observation=None, values=in_values)
expected_action = torch.tensor(expected_action, dtype=torch.long)
assert action.shape == expected_action.shape
assert (action == expected_action).all()
assert values.shape == in_values.shape
assert (values == in_values).all()
@pytest.mark.parametrize("device", get_default_devices())
def test_value_based_policy(device):
torch.manual_seed(0)
obs_dim = 4
action_dim = 5
action_spec = OneHotDiscreteTensorSpec(action_dim)
def make_net():
net = MLP(in_features=obs_dim, out_features=action_dim, depth=2, device=device)
for mod in net.modules():
if hasattr(mod, "bias") and mod.bias is not None:
mod.bias.data.zero_()
return net
actor = QValueActor(spec=action_spec, module=make_net(), safe=True)
obs = torch.zeros(2, obs_dim, device=device)
td = TensorDict(batch_size=[2], source={"observation": obs})
action = actor(td).get("action")
assert (action.sum(-1) == 1).all()
actor = QValueActor(spec=action_spec, module=make_net(), safe=False)
obs = torch.randn(2, obs_dim, device=device)
td = TensorDict(batch_size=[2], source={"observation": obs})
action = actor(td).get("action")
assert (action.sum(-1) == 1).all()
actor = QValueActor(spec=action_spec, module=make_net(), safe=False)
obs = torch.zeros(2, obs_dim, device=device)
td = TensorDict(batch_size=[2], source={"observation": obs})
action = actor(td).get("action")
with pytest.raises(AssertionError):
assert (action.sum(-1) == 1).all()
@pytest.mark.parametrize(
"spec", [None, OneHotDiscreteTensorSpec(3), MultiOneHotDiscreteTensorSpec([3, 2])]
)
@pytest.mark.parametrize(
"action_space", [None, "one-hot", "one_hot", "mult-one-hot", "mult_one_hot"]
)
def test_qvalactor_construct(
spec,
action_space,
):
kwargs = {}
if spec is not None:
kwargs["spec"] = spec
if action_space is not None:
kwargs["action_space"] = action_space
kwargs["module"] = TensorDictModule(
lambda x: x, in_keys=["x"], out_keys=["action_value"]
)
if spec is None and action_space is None:
with pytest.raises(
ValueError, match="Neither action_space nor spec was defined"
):
QValueActor(**kwargs)
return
if (
type(spec) is MultiOneHotDiscreteTensorSpec
and action_space not in ("mult-one-hot", "mult_one_hot", None)
) or (
type(spec) is OneHotDiscreteTensorSpec
and action_space not in ("one-hot", "one_hot", None)
):
with pytest.raises(
ValueError, match="The action spec and the action space do not match"
):
QValueActor(**kwargs)
return
QValueActor(**kwargs)
@pytest.mark.parametrize("device", get_default_devices())
def test_value_based_policy_categorical(device):
torch.manual_seed(0)
obs_dim = 4
action_dim = 5
action_spec = DiscreteTensorSpec(action_dim)
def make_net():
net = MLP(in_features=obs_dim, out_features=action_dim, depth=2, device=device)
for mod in net.modules():
if hasattr(mod, "bias") and mod.bias is not None:
mod.bias.data.zero_()
return net
actor = QValueActor(
spec=action_spec, module=make_net(), safe=True, action_space="categorical"
)
obs = torch.zeros(2, obs_dim, device=device)
td = TensorDict(batch_size=[2], source={"observation": obs})
action = actor(td).get("action")
assert (0 <= action).all() and (action < action_dim).all()
actor = QValueActor(
spec=action_spec, module=make_net(), safe=False, action_space="categorical"
)
obs = torch.randn(2, obs_dim, device=device)
td = TensorDict(batch_size=[2], source={"observation": obs})
action = actor(td).get("action")
assert (0 <= action).all() and (action < action_dim).all()
@pytest.mark.parametrize("device", get_default_devices())
def test_actorcritic(device):
common_module = SafeModule(
module=nn.Linear(3, 4), in_keys=["obs"], out_keys=["hidden"], spec=None
).to(device)
module = SafeModule(nn.Linear(4, 5), in_keys=["hidden"], out_keys=["param"])
policy_operator = ProbabilisticActor(
module=module, in_keys=["param"], spec=None, return_log_prob=True
).to(device)
value_operator = ValueOperator(nn.Linear(4, 1), in_keys=["hidden"]).to(device)
op = ActorValueOperator(
common_operator=common_module,
policy_operator=policy_operator,
value_operator=value_operator,
).to(device)
td = TensorDict(
source={"obs": torch.randn(4, 3)},
batch_size=[
4,
],
).to(device)
td_total = op(td.clone())
policy_op = op.get_policy_operator()
td_policy = policy_op(td.clone())
value_op = op.get_value_operator()
td_value = value_op(td)
torch.testing.assert_close(td_total.get("action"), td_policy.get("action"))
torch.testing.assert_close(
td_total.get("sample_log_prob"), td_policy.get("sample_log_prob")
)
torch.testing.assert_close(td_total.get("state_value"), td_value.get("state_value"))
value_params = set(
list(op.get_value_operator().parameters()) + list(op.module[0].parameters())
)
value_params2 = set(value_op.parameters())
assert len(value_params.difference(value_params2)) == 0 and len(
value_params.intersection(value_params2)
) == len(value_params)
policy_params = set(
list(op.get_policy_operator().parameters()) + list(op.module[0].parameters())
)
policy_params2 = set(policy_op.parameters())
assert len(policy_params.difference(policy_params2)) == 0 and len(
policy_params.intersection(policy_params2)
) == len(policy_params)
if __name__ == "__main__":
args, unknown = argparse.ArgumentParser().parse_known_args()
pytest.main([__file__, "--capture", "no", "--exitfirst"] + unknown)