-
Notifications
You must be signed in to change notification settings - Fork 309
/
collectors.py
1374 lines (1217 loc) · 57.2 KB
/
collectors.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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# 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 abc
import os
import queue
import time
from collections import OrderedDict
from copy import deepcopy
from multiprocessing import connection, queues
from textwrap import indent
from typing import Callable, Iterator, Optional, Sequence, Tuple, Union
import numpy as np
import torch
from torch import multiprocessing as mp
from torch.utils.data import IterableDataset
from torchrl.envs.utils import set_exploration_mode, step_tensordict
from .. import _check_for_faulty_process, prod
from ..modules.tensordict_module import ProbabilisticTensorDictModule
from .utils import split_trajectories
__all__ = [
"SyncDataCollector",
"aSyncDataCollector",
"MultiaSyncDataCollector",
"MultiSyncDataCollector",
]
from torchrl.envs.transforms import TransformedEnv
from ..data import TensorSpec
from ..data.tensordict.tensordict import _TensorDict, TensorDict
from ..data.utils import CloudpickleWrapper, DEVICE_TYPING
from ..envs.common import _EnvClass
from ..envs.vec_env import _BatchedEnv
_TIMEOUT = 1.0
_MIN_TIMEOUT = 1e-3 # should be several orders of magnitude inferior wrt time spent collecting a trajectory
_MAX_IDLE_COUNT = int(os.environ.get("MAX_IDLE_COUNT", 1000))
class RandomPolicy:
def __init__(self, action_spec: TensorSpec):
"""Random policy for a given action_spec.
This is a wrapper around the action_spec.rand method.
$ python example_google.py
Args:
action_spec: TensorSpec object describing the action specs
Examples:
>>> from torchrl.data.tensor_specs import NdBoundedTensorSpec
>>> from torchrl.data.tensordict import TensorDict
>>> action_spec = NdBoundedTensorSpec(-torch.ones(3), torch.ones(3))
>>> actor = RandomPolicy(spec=action_spec)
>>> td = actor(TensorDict(batch_size=[])) # selects a random action in the cube [-1; 1]
"""
self.action_spec = action_spec
def __call__(self, td: _TensorDict) -> _TensorDict:
return td.set("action", self.action_spec.rand(td.batch_size))
def recursive_map_to_cpu(dictionary: OrderedDict) -> OrderedDict:
return OrderedDict(
**{
k: recursive_map_to_cpu(item)
if isinstance(item, OrderedDict)
else item.cpu()
for k, item in dictionary.items()
}
)
class _DataCollector(IterableDataset, metaclass=abc.ABCMeta):
def _get_policy_and_device(
self,
policy: Optional[
Union[ProbabilisticTensorDictModule, Callable[[_TensorDict], _TensorDict]]
] = None,
device: Optional[DEVICE_TYPING] = None,
) -> Tuple[
ProbabilisticTensorDictModule, torch.device, Union[None, Callable[[], dict]]
]:
"""From a policy and a device, assigns the self.device attribute to
the desired device and maps the policy onto it or (if the device is
ommitted) assigns the self.device attribute to the policy device.
Args:
create_env_fn (Callable or list of callables): an env creator
function (or a list of creators)
create_env_kwargs (dictionary): kwargs for the env creator
policy (ProbabilisticTensorDictModule, optional): a policy to be used
device (int, str or torch.device, optional): device where to place
the policy
"""
# if create_env_fn is not None:
# if create_env_kwargs is None:
# create_env_kwargs = dict()
# self.create_env_fn = create_env_fn
# if isinstance(create_env_fn, _EnvClass):
# env = create_env_fn
# else:
# env = self.create_env_fn(**create_env_kwargs)
# else:
# env = None
if policy is None:
if not hasattr(self, "env") or self.env is None:
raise ValueError(
"env must be provided to _get_policy_and_device if policy is None"
)
policy = RandomPolicy(self.env.action_spec)
try:
policy_device = next(policy.parameters()).device
except: # noqa
policy_device = (
torch.device(device) if device is not None else torch.device("cpu")
)
device = torch.device(device) if device is not None else policy_device
if device is None:
# if device cannot be found in policy and is not specified, set cpu
device = torch.device("cpu")
get_weights_fn = None
if policy_device != device:
get_weights_fn = policy.state_dict
policy = deepcopy(policy).requires_grad_(False).to(device)
if device == torch.device("cpu"):
policy.share_memory()
# if not (len(list(policy.parameters())) == 0 or next(policy.parameters()).is_shared()):
# raise RuntimeError("Provided policy parameters must be shared.")
return policy, device, get_weights_fn
def update_policy_weights_(self) -> None:
"""Update the policy weights if the policy of the data collector and the trained policy live on different devices."""
if self.get_weights_fn is not None:
self.policy.load_state_dict(self.get_weights_fn())
def __iter__(self) -> Iterator[_TensorDict]:
return self.iterator()
@abc.abstractmethod
def iterator(self) -> Iterator[_TensorDict]:
raise NotImplementedError
@abc.abstractmethod
def set_seed(self, seed: int) -> int:
raise NotImplementedError
@abc.abstractmethod
def state_dict(self) -> OrderedDict:
raise NotImplementedError
@abc.abstractmethod
def load_state_dict(self, state_dict: OrderedDict) -> None:
raise NotImplementedError
def __repr__(self) -> str:
string = f"{self.__class__.__name__}()"
return string
class SyncDataCollector(_DataCollector):
"""
Generic data collector for RL problems. Requires and environment constructor and a policy.
Args:
create_env_fn (Callable), returns an instance of _EnvClass class.
policy (Callable, optional): Policy to be executed in the environment.
Must accept _TensorDict object as input.
total_frames (int): lower bound of the total number of frames returned by the collector. The iterator will
stop once the total number of frames equates or exceeds the total number of frames passed to the
collector.
create_env_kwargs (dict, optional): Dictionary of kwargs for create_env_fn.
max_frames_per_traj (int, optional): Maximum steps per trajectory. Note that a trajectory can span over multiple batches
(unless reset_at_each_iter is set to True, see below). Once a trajectory reaches n_steps_max,
the environment is reset. If the environment wraps multiple environments together, the number of steps
is tracked for each environment independently. Negative values are allowed, in which case this argument
is ignored.
default: -1 (i.e. no maximum number of steps)
frames_per_batch (int): Time-length of a batch.
reset_at_each_iter and frames_per_batch == n_steps_max are equivalent configurations.
default: 200
init_random_frames (int, optional): Number of frames for which the policy is ignored before it is called.
This feature is mainly intended to be used in offline/model-based settings, where a batch of random
trajectories can be used to initialize training.
default=-1 (i.e. no random frames)
reset_at_each_iter (bool): Whether or not environments should be reset for each batch.
default=False.
postproc (Callable, optional): A Batcher is an object that will read a batch of data and return it in a useful format for training.
default: None.
split_trajs (bool): Boolean indicating whether the resulting TensorDict should be split according to the trajectories.
See utils.split_trajectories for more information.
device (int, str or torch.device, optional): The device on which the policy will be placed.
If it differs from the input policy device, the update_policy_weights_() method should be queried
at appropriate times during the training loop to accommodate for the lag between parameter configuration
at various times.
default = None (i.e. policy is kept on its original device)
seed (int, optional): seed to be used for torch and numpy.
pin_memory (bool): whether pin_memory() should be called on the outputs.
passing_device (int, str or torch.device, optional): The device on which the output TensorDict will be stored.
For long trajectories, it may be necessary to store the data on a different device than the one where
the policy is stored.
default = "cpu"
return_in_place (bool): if True, the collector will yield the same tensordict container with updated values
at each iteration.
default = False
exploration_mode (str, optional): interaction mode to be used when collecting data. Must be one of "random",
"mode" or "mean".
default = "random"
init_with_lag (bool, optional): if True, the first trajectory will be truncated earlier at a random step.
This is helpful to desynchronize the environments, such that steps do no match in all collected rollouts.
default = True
return_same_td (bool, optional): if True, the same TensorDict will be returned at each iteration, with its values
updated. This feature should be used cautiously: if the same tensordict is added to a replay buffer for instance,
the whole content of the buffer will be identical.
Default is False.
"""
def __init__(
self,
create_env_fn: Union[
_EnvClass, "EnvCreator", Sequence[Callable[[], _EnvClass]]
],
policy: Optional[
Union[ProbabilisticTensorDictModule, Callable[[_TensorDict], _TensorDict]]
] = None,
total_frames: Optional[int] = -1,
create_env_kwargs: Optional[dict] = None,
max_frames_per_traj: int = -1,
frames_per_batch: int = 200,
init_random_frames: int = -1,
reset_at_each_iter: bool = False,
postproc: Optional[Callable[[_TensorDict], _TensorDict]] = None,
split_trajs: bool = True,
device: DEVICE_TYPING = None,
passing_device: DEVICE_TYPING = "cpu",
seed: Optional[int] = None,
pin_memory: bool = False,
return_in_place: bool = False,
exploration_mode: str = "random",
init_with_lag: bool = False,
return_same_td: bool = False,
):
self.closed = True
if seed is not None:
torch.manual_seed(seed)
np.random.seed(seed)
if create_env_kwargs is None:
create_env_kwargs = {}
if not isinstance(create_env_fn, _EnvClass):
env = create_env_fn(**create_env_kwargs)
else:
env = create_env_fn
if create_env_kwargs:
if not isinstance(env, _BatchedEnv):
raise RuntimeError(
"kwargs were passed to SyncDataCollector but they can't be set "
f"on environment of type {type(create_env_fn)}."
)
env.update_kwargs(create_env_kwargs)
self.passing_device = torch.device(passing_device)
self.env: _EnvClass = env.to(self.passing_device)
self.closed = False
self.n_env = self.env.numel()
(self.policy, self.device, self.get_weights_fn,) = self._get_policy_and_device(
policy=policy,
device=device,
)
self.env_device = env.device
if not total_frames > 0:
total_frames = float("inf")
self.total_frames = total_frames
self.reset_at_each_iter = reset_at_each_iter
self.init_random_frames = init_random_frames
self.postproc = postproc
if self.postproc is not None:
self.postproc.to(self.passing_device)
self.max_frames_per_traj = max_frames_per_traj
self.frames_per_batch = -(-frames_per_batch // self.n_env)
self.pin_memory = pin_memory
self.exploration_mode = exploration_mode
self.init_with_lag = init_with_lag and max_frames_per_traj > 0
self.return_same_td = return_same_td
self._tensordict = env.reset()
self._tensordict.set(
"step_count", torch.zeros(*self.env.batch_size, 1, dtype=torch.int)
)
self._tensordict_out = TensorDict(
{},
batch_size=[*self.env.batch_size, self.frames_per_batch],
device=self.passing_device,
)
self.return_in_place = return_in_place
self.split_trajs = split_trajs
if self.return_in_place and self.split_trajs:
raise RuntimeError(
"the 'return_in_place' and 'split_trajs' argument are incompatible, but found to be both "
"True. split_trajs=True will cause the output tensordict to have an unpredictable output "
"shape, which prevents caching and overwriting the tensors."
)
self._td_env = None
self._td_policy = None
self._has_been_done = None
self._exclude_private_keys = True
def set_seed(self, seed: int) -> int:
"""Sets the seeds of the environments stored in the DataCollector.
Args:
seed (int): integer representing the seed to be used for the environment.
Returns:
Output seed. This is useful when more than one environment is contained in the DataCollector, as the
seed will be incremented for each of these. The resulting seed is the seed of the last environment.
Examples:
>>> env_fn = lambda: GymEnv("Pendulum-v1")
>>> env_fn_parallel = lambda: ParallelEnv(6, env_fn)
>>> collector = SyncDataCollector(env_fn_parallel)
>>> out_seed = collector.set_seed(1) # out_seed = 6
"""
return self.env.set_seed(seed)
def iterator(self) -> Iterator[_TensorDict]:
"""Iterates through the DataCollector.
Yields: _TensorDict objects containing (chunks of) trajectories
"""
total_frames = self.total_frames
i = -1
self._frames = 0
while True:
i += 1
self._iter = i
tensordict_out = self.rollout()
self._frames += tensordict_out.numel()
if self._frames >= total_frames:
self.env.close()
if self.split_trajs:
tensordict_out = split_trajectories(tensordict_out)
if self.postproc is not None:
tensordict_out = self.postproc(tensordict_out)
if self._exclude_private_keys:
excluded_keys = [
key for key in tensordict_out.keys() if key.startswith("_")
]
tensordict_out = tensordict_out.exclude(*excluded_keys, inplace=True)
if self.return_same_td:
yield tensordict_out
else:
yield tensordict_out.clone()
del tensordict_out
if self._frames >= self.total_frames:
break
def _cast_to_policy(self, td: _TensorDict) -> _TensorDict:
policy_device = self.device
if hasattr(self.policy, "in_keys"):
td = td.select(*self.policy.in_keys)
if self._td_policy is None:
self._td_policy = td.to(policy_device)
else:
if td.device == torch.device("cpu") and self.pin_memory:
td.pin_memory()
self._td_policy.update(td, inplace=True)
return self._td_policy
def _cast_to_env(
self, td: _TensorDict, dest: Optional[_TensorDict] = None
) -> _TensorDict:
env_device = self.env_device
if dest is None:
if self._td_env is None:
self._td_env = td.to(env_device)
else:
self._td_env.update(td, inplace=True)
return self._td_env
else:
return dest.update(td, inplace=True)
def _reset_if_necessary(self) -> None:
done = self._tensordict.get("done")
steps = self._tensordict.get("step_count")
done_or_terminated = done | (steps == self.max_frames_per_traj)
if self._has_been_done is None:
self._has_been_done = done_or_terminated
else:
self._has_been_done = self._has_been_done | done_or_terminated
if not self._has_been_done.all() and self.init_with_lag:
_reset = torch.zeros_like(done_or_terminated).bernoulli_(
1 / self.max_frames_per_traj
)
_reset[self._has_been_done] = False
done_or_terminated = done_or_terminated | _reset
if done_or_terminated.any():
traj_ids = self._tensordict.get("traj_ids").clone()
steps = steps.clone()
if len(self.env.batch_size):
self._tensordict.masked_fill_(done_or_terminated.squeeze(-1), 0)
self._tensordict.set("reset_workers", done_or_terminated)
else:
self._tensordict.zero_()
self._tensordict.update(self.env.reset(), inplace=True)
if self._tensordict.get("done").any():
raise RuntimeError(
f"Got {sum(self._tensordict.get('done'))} done envs after reset."
)
if len(self.env.batch_size):
self._tensordict.del_("reset_workers")
traj_ids[done_or_terminated] = traj_ids.max() + torch.arange(
1, done_or_terminated.sum() + 1, device=traj_ids.device
)
steps[done_or_terminated] = 0
self._tensordict.set("traj_ids", traj_ids) # no ops if they already match
self._tensordict.set("step_count", steps)
@torch.no_grad()
def rollout(self) -> _TensorDict:
"""Computes a rollout in the environment using the provided policy.
Returns:
_TensorDict containing the computed rollout.
"""
if self.reset_at_each_iter:
self._tensordict.update(self.env.reset(), inplace=True)
self._tensordict.fill_("step_count", 0)
n = self.env.batch_size[0] if len(self.env.batch_size) else 1
self._tensordict.set("traj_ids", torch.arange(n).unsqueeze(-1))
tensordict_out = []
with set_exploration_mode(self.exploration_mode):
for t in range(self.frames_per_batch):
if self._frames < self.init_random_frames:
self.env.rand_step(self._tensordict)
else:
td_cast = self._cast_to_policy(self._tensordict)
td_cast = self.policy(td_cast)
self._cast_to_env(td_cast, self._tensordict)
self.env.step(self._tensordict)
step_count = self._tensordict.get("step_count")
step_count += 1
tensordict_out.append(self._tensordict.clone())
self._reset_if_necessary()
self._tensordict.update(
step_tensordict(
self._tensordict.exclude("reward", "done"), keep_other=True
),
inplace=True,
)
if self.return_in_place and len(self._tensordict_out.keys()) > 0:
tensordict_out = torch.stack(tensordict_out, len(self.env.batch_size))
tensordict_out = tensordict_out.select(*self._tensordict_out.keys())
return self._tensordict_out.update_(tensordict_out)
return torch.stack(
tensordict_out,
len(self.env.batch_size),
out=self._tensordict_out,
) # dim 0 for single env, dim 1 for batch
def reset(self, index=None, **kwargs) -> None:
"""Resets the environments to a new initial state."""
if index is not None:
# check that the env supports partial reset
if prod(self.env.batch_size) == 0:
raise RuntimeError("resetting unique env with index is not permitted.")
reset_workers = torch.zeros(
*self.env.batch_size,
1,
dtype=torch.bool,
device=self.env.device,
)
reset_workers[index] = 1
td_in = TensorDict({"reset_workers": reset_workers}, self.env.batch_size)
self._tensordict[index].zero_()
else:
td_in = None
self._tensordict.zero_()
if td_in:
self._tensordict.update(td_in, inplace=True)
self._tensordict.update(self.env.reset(**kwargs), inplace=True)
self._tensordict.fill_("step_count", 0)
def shutdown(self) -> None:
"""Shuts down all workers and/or closes the local environment."""
if not self.closed:
self.closed = True
del self._tensordict, self._tensordict_out
if not self.env.is_closed:
self.env.close()
del self.env
def __del__(self):
self.shutdown() # make sure env is closed
def state_dict(self) -> OrderedDict:
"""Returns the local state_dict of the data collector (environment
and policy).
Returns:
an ordered dictionary with fields `"policy_state_dict"` and
`"env_state_dict"`.
"""
if isinstance(self.env, TransformedEnv):
env_state_dict = self.env.transform.state_dict()
elif isinstance(self.env, _BatchedEnv):
env_state_dict = self.env.state_dict()
else:
env_state_dict = OrderedDict()
if hasattr(self.policy, "state_dict"):
policy_state_dict = self.policy.state_dict()
state_dict = OrderedDict(
policy_state_dict=policy_state_dict,
env_state_dict=env_state_dict,
)
else:
state_dict = OrderedDict(env_state_dict=env_state_dict)
return state_dict
def load_state_dict(self, state_dict: OrderedDict, **kwargs) -> None:
"""Loads a state_dict on the environment and policy.
Args:
state_dict (OrderedDict): ordered dictionary containing the fields
`"policy_state_dict"` and `"env_state_dict"`.
"""
strict = kwargs.get("strict", True)
if strict or "env_state_dict" in state_dict:
self.env.load_state_dict(state_dict["env_state_dict"], **kwargs)
if strict or "policy_state_dict" in state_dict:
self.policy.load_state_dict(state_dict["policy_state_dict"], **kwargs)
def __repr__(self) -> str:
env_str = indent(f"env={self.env}", 4 * " ")
policy_str = indent(f"policy={self.policy}", 4 * " ")
td_out_str = indent(f"td_out={self._tensordict_out}", 4 * " ")
string = f"{self.__class__.__name__}(\n{env_str},\n{policy_str},\n{td_out_str})"
return string
class _MultiDataCollector(_DataCollector):
"""Runs a given number of DataCollectors on separate processes.
Args:
create_env_fn (list of Callabled): list of Callables, each returning an instance of _EnvClass
policy (Callable, optional): Instance of ProbabilisticTensorDictModule class.
Must accept _TensorDict object as input.
total_frames (int): lower bound of the total number of frames returned by the collector. In parallel settings,
the actual number of frames may well be greater than this as the closing signals are sent to the
workers only once the total number of frames has been collected on the server.
create_env_kwargs (dict, optional): A (list of) dictionaries with the arguments used to create an environment
max_frames_per_traj: Maximum steps per trajectory. Note that a trajectory can span over multiple batches
(unless reset_at_each_iter is set to True, see below). Once a trajectory reaches n_steps_max,
the environment is reset. If the environment wraps multiple environments together, the number of steps
is tracked for each environment independently. Negative values are allowed, in which case this argument
is ignored.
default: -1 (i.e. no maximum number of steps)
frames_per_batch (int): Time-length of a batch.
reset_at_each_iter and frames_per_batch == n_steps_max are equivalent configurations.
default: 200
init_random_frames (int): Number of frames for which the policy is ignored before it is called.
This feature is mainly intended to be used in offline/model-based settings, where a batch of random
trajectories can be used to initialize training.
default=-1 (i.e. no random frames)
reset_at_each_iter (bool): Whether or not environments should be reset for each batch.
default=False.
postproc (callable, optional): A PostProcessor is an object that will read a batch of data and process it in a
useful format for training.
default: None.
split_trajs (bool): Boolean indicating whether the resulting TensorDict should be split according to the trajectories.
See utils.split_trajectories for more information.
devices (int, str, torch.device or sequence of such, optional): The devices on which the policy will be placed.
If it differs from the input policy device, the update_policy_weights_() method should be queried
at appropriate times during the training loop to accommodate for the lag between parameter configuration
at various times.
default = None (i.e. policy is kept on its original device)
passing_devices (int, str, torch.device or sequence of such, optional): The devices on which the output
TensorDict will be stored. For long trajectories, it may be necessary to store the data on a different
device than the one where the policy is stored.
default = "cpu"
update_at_each_batch (bool): if True, the policy weights will be updated every time a batch of trajectories
is collected.
default=False
init_with_lag (bool, optional): if True, the first trajectory will be truncated earlier at a random step.
This is helpful to desynchronize the environments, such that steps do no match in all collected rollouts.
default = True
exploration_mode (str, optional): interaction mode to be used when collecting data. Must be one of "random",
"mode" or "mean".
default = "random"
"""
def __init__(
self,
create_env_fn: Sequence[Callable[[], _EnvClass]],
policy: Optional[
Union[ProbabilisticTensorDictModule, Callable[[_TensorDict], _TensorDict]]
] = None,
total_frames: Optional[int] = -1,
create_env_kwargs: Optional[Sequence[dict]] = None,
max_frames_per_traj: int = -1,
frames_per_batch: int = 200,
init_random_frames: int = -1,
reset_at_each_iter: bool = False,
postproc: Optional[Callable[[_TensorDict], _TensorDict]] = None,
split_trajs: bool = True,
devices: DEVICE_TYPING = None,
seed: Optional[int] = None,
pin_memory: bool = False,
passing_devices: Union[DEVICE_TYPING, Sequence[DEVICE_TYPING]] = "cpu",
update_at_each_batch: bool = False,
init_with_lag: bool = False,
exploration_mode: str = "random",
):
self.closed = True
self.create_env_fn = create_env_fn
self.num_workers = len(create_env_fn)
self.create_env_kwargs = (
create_env_kwargs
if create_env_kwargs is not None
else [dict() for _ in range(self.num_workers)]
)
# Preparing devices:
# We want the user to be able to choose, for each worker, on which
# device will the policy live and which device will be used to store
# data. Those devices may or may not match.
# One caveat is that, if there is only one device for the policy, and
# if there are multiple workers, sending the same device and policy
# to be copied to each worker will result in multiple copies of the
# same policy on the same device.
# To go around this, we do the copies of the policy in the server
# (this object) to each possible device, and send to all the
# processes their copy of the policy.
def device_err_msg(device_name, devices_list):
return (
f"The length of the {device_name} argument should match the "
f"number of workers of the collector. Got len("
f"create_env_fn)={self.num_workers} and len("
f"passing_devices)={len(devices_list)}"
)
if isinstance(devices, (str, int, torch.device)):
devices = [torch.device(devices) for _ in range(self.num_workers)]
elif devices is None:
devices = [None for _ in range(self.num_workers)]
elif isinstance(devices, Sequence):
if len(devices) != self.num_workers:
raise RuntimeError(device_err_msg("devices", devices))
devices = [torch.device(_device) for _device in devices]
else:
raise ValueError(
"devices should be either None, a torch.device or equivalent "
"or an iterable of devices. "
f"Found {type(devices)} instead."
)
self._policy_dict = {}
self._get_weights_fn_dict = {}
for i, _device in enumerate(devices):
_policy, _device, _get_weight_fn = self._get_policy_and_device(
policy=policy,
device=_device,
)
if _device not in self._policy_dict:
self._policy_dict[_device] = _policy
self._get_weights_fn_dict[_device] = _get_weight_fn
devices[i] = _device
self.devices = devices
if isinstance(passing_devices, (str, int, torch.device)):
self.passing_devices = [
torch.device(passing_devices) for _ in range(self.num_workers)
]
elif isinstance(passing_devices, Sequence):
if len(passing_devices) != self.num_workers:
raise RuntimeError(device_err_msg("passing_devices", passing_devices))
self.passing_devices = [
torch.device(_passing_device) for _passing_device in passing_devices
]
else:
raise ValueError(
"passing_devices should be either a torch.device or equivalent or an iterable of devices. "
f"Found {type(passing_devices)} instead."
)
self.total_frames = total_frames if total_frames > 0 else float("inf")
self.reset_at_each_iter = reset_at_each_iter
self.postprocs = postproc
self.max_frames_per_traj = max_frames_per_traj
self.frames_per_batch = frames_per_batch
self.seed = seed
self.split_trajs = split_trajs
self.pin_memory = pin_memory
self.init_random_frames = init_random_frames
self.update_at_each_batch = update_at_each_batch
self.init_with_lag = init_with_lag
self.exploration_mode = exploration_mode
self.frames_per_worker = np.inf
self._run_processes()
self._exclude_private_keys = True
@property
def frames_per_batch_worker(self):
raise NotImplementedError
def update_policy_weights_(self) -> None:
for _device in self._policy_dict:
if self._get_weights_fn_dict[_device] is not None:
self._policy_dict[_device].load_state_dict(
self._get_weights_fn_dict[_device]()
)
@property
def _queue_len(self) -> int:
raise NotImplementedError
def _run_processes(self) -> None:
queue_out = mp.Queue(self._queue_len) # sends data from proc to main
self.procs = []
self.pipes = []
for i, (env_fun, env_fun_kwargs) in enumerate(
zip(self.create_env_fn, self.create_env_kwargs)
):
_device = self.devices[i]
_passing_device = self.passing_devices[i]
pipe_parent, pipe_child = mp.Pipe() # send messages to procs
if env_fun.__class__.__name__ != "EnvCreator" and not isinstance(
env_fun, _EnvClass
): # to avoid circular imports
env_fun = CloudpickleWrapper(env_fun)
kwargs = {
"pipe_parent": pipe_parent,
"pipe_child": pipe_child,
"queue_out": queue_out,
"create_env_fn": env_fun,
"create_env_kwargs": env_fun_kwargs,
"policy": self._policy_dict[_device],
"frames_per_worker": self.frames_per_worker,
"max_frames_per_traj": self.max_frames_per_traj,
"frames_per_batch": self.frames_per_batch_worker,
"reset_at_each_iter": self.reset_at_each_iter,
"device": _device,
"passing_device": _passing_device,
"seed": self.seed,
"pin_memory": self.pin_memory,
"init_with_lag": self.init_with_lag,
"exploration_mode": self.exploration_mode,
"idx": i,
}
proc = mp.Process(target=_main_async_collector, kwargs=kwargs)
# proc.daemon can't be set as daemonic processes may be launched by the process itself
proc.start()
pipe_child.close()
self.procs.append(proc)
self.pipes.append(pipe_parent)
self.queue_out = queue_out
self.closed = False
def __del__(self):
self.shutdown()
def shutdown(self) -> None:
"""Shuts down all processes. This operation is irreversible."""
self._shutdown_main()
def _shutdown_main(self) -> None:
if self.closed:
return
_check_for_faulty_process(self.procs)
self.closed = True
for idx in range(self.num_workers):
self.pipes[idx].send((None, "close"))
for idx in range(self.num_workers):
msg = self.pipes[idx].recv()
if msg != "closed":
raise RuntimeError(f"got {msg} but expected 'close'")
for proc in self.procs:
proc.join()
self.queue_out.close()
for pipe in self.pipes:
pipe.close()
def set_seed(self, seed: int) -> int:
"""Sets the seeds of the environments stored in the DataCollector.
Args:
seed: integer representing the seed to be used for the environment.
Returns:
Output seed. This is useful when more than one environment is
contained in the DataCollector, as the seed will be incremented for
each of these. The resulting seed is the seed of the last
environment.
Examples:
>>> env_fn = lambda: GymEnv("Pendulum-v0")
>>> env_fn_parallel = lambda: ParallelEnv(6, env_fn)
>>> collector = SyncDataCollector(env_fn_parallel)
>>> out_seed = collector.set_seed(1) # out_seed = 6
"""
_check_for_faulty_process(self.procs)
for idx in range(self.num_workers):
self.pipes[idx].send((seed, "seed"))
new_seed, msg = self.pipes[idx].recv()
if msg != "seeded":
raise RuntimeError(f"Expected msg='seeded', got {msg}")
seed = new_seed
self.reset()
return seed
def reset(self, reset_idx: Optional[Sequence[bool]] = None) -> None:
"""Resets the environments to a new initial state.
Args:
reset_idx: Optional. Sequence indicating which environments have
to be reset. If None, all environments are reset.
"""
_check_for_faulty_process(self.procs)
if reset_idx is None:
reset_idx = [True for _ in range(self.num_workers)]
for idx in range(self.num_workers):
if reset_idx[idx]:
self.pipes[idx].send((None, "reset"))
for idx in range(self.num_workers):
if reset_idx[idx]:
j, msg = self.pipes[idx].recv()
if msg != "reset":
raise RuntimeError(f"Expected msg='reset', got {msg}")
def state_dict(self) -> OrderedDict:
"""
Returns the state_dict of the data collector.
Each field represents a worker containing its own state_dict.
"""
for idx in range(self.num_workers):
self.pipes[idx].send((None, "state_dict"))
state_dict = OrderedDict()
for idx in range(self.num_workers):
_state_dict, msg = self.pipes[idx].recv()
if msg != "state_dict":
raise RuntimeError(f"Expected msg='state_dict', got {msg}")
state_dict[f"worker{idx}"] = _state_dict
return state_dict
def load_state_dict(self, state_dict: OrderedDict) -> None:
"""
Loads the state_dict on the workers.
Args:
state_dict (OrderedDict): state_dict of the form
``{"worker0": state_dict0, "worker1": state_dict1}``.
"""
for idx in range(self.num_workers):
self.pipes[idx].send((state_dict[f"worker{idx}"], "load_state_dict"))
for idx in range(self.num_workers):
_, msg = self.pipes[idx].recv()
if msg != "loaded":
raise RuntimeError(f"Expected msg='loaded', got {msg}")
class MultiSyncDataCollector(_MultiDataCollector):
"""Runs a given number of DataCollectors on separate processes
synchronously.
The collection starts when the next item of the collector is queried,
and no environment step is computed in between the reception of a batch of
trajectory and the start of the next collection.
This class can be safely used with online RL algorithms.
"""
__doc__ += _MultiDataCollector.__doc__
@property
def frames_per_batch_worker(self):
return -(-self.frames_per_batch // self.num_workers)
@property
def _queue_len(self) -> int:
return self.num_workers
def iterator(self) -> Iterator[_TensorDict]:
i = -1
frames = 0
out_tensordicts_shared = OrderedDict()
dones = [False for _ in range(self.num_workers)]
workers_frames = [0 for _ in range(self.num_workers)]
same_device = None
while not all(dones) and frames < self.total_frames:
_check_for_faulty_process(self.procs)
if self.update_at_each_batch:
self.update_policy_weights_()
for idx in range(self.num_workers):
if frames < self.init_random_frames:
msg = "continue_random"
else:
msg = "continue"
self.pipes[idx].send((None, msg))
i += 1
max_traj_idx = None
for k in range(self.num_workers):
new_data, j = self.queue_out.get()
if j == 0:
data, idx = new_data
out_tensordicts_shared[idx] = data
else:
idx = new_data
workers_frames[idx] = (
workers_frames[idx] + out_tensordicts_shared[idx].numel()
)
if workers_frames[idx] >= self.total_frames:
print(f"{idx} is done!")
dones[idx] = True
# we have to correct the traj_ids to make sure that they don't overlap
for idx in range(self.num_workers):
traj_ids = out_tensordicts_shared[idx].get("traj_ids")
if max_traj_idx is not None:
traj_ids += max_traj_idx
# out_tensordicts_shared[idx].set("traj_ids", traj_ids)
max_traj_idx = traj_ids.max().item() + 1
# out = out_tensordicts_shared[idx]
if same_device is None:
prev_device = None
same_device = True
for item in out_tensordicts_shared.values():
if prev_device is None:
prev_device = item.device
else:
same_device = same_device and (item.device == prev_device)
if same_device:
out = torch.cat([item for item in out_tensordicts_shared.values()], 0)
else:
out = torch.cat(
[item.cpu() for item in out_tensordicts_shared.values()], 0
)
if self.split_trajs:
out = split_trajectories(out)
frames += out.get("mask").sum()
else:
frames += prod(out.shape)
if self.postprocs:
self.postprocs = self.postprocs.to(out.device)
out = self.postprocs(out)
if self._exclude_private_keys:
excluded_keys = [key for key in out.keys() if key.startswith("_")]
out = out.exclude(*excluded_keys)
yield out
del out_tensordicts_shared
self._shutdown_main()
class MultiaSyncDataCollector(_MultiDataCollector):
"""Runs a given number of DataCollectors on separate processes
asynchronously.
The collection keeps on occuring on all processes even between the time