forked from pytorch/rl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batched_envs.py
1137 lines (1005 loc) · 43.5 KB
/
batched_envs.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.
from __future__ import annotations
import os
from collections import OrderedDict
from copy import deepcopy
from functools import wraps
from multiprocessing import connection
from multiprocessing.synchronize import Lock as MpLock
from typing import Any, Callable, Dict, List, Optional, Sequence, Union
from warnings import warn
import torch
from tensordict import TensorDict
from tensordict._tensordict import _unravel_key_to_tuple, unravel_keys
from tensordict.tensordict import LazyStackedTensorDict, TensorDictBase
from torch import multiprocessing as mp
from torchrl._utils import _check_for_faulty_process, _ProcessNoWarn, VERBOSE
from torchrl.data.utils import CloudpickleWrapper, contains_lazy_spec, DEVICE_TYPING
from torchrl.envs.common import EnvBase
from torchrl.envs.env_creator import get_env_metadata
from torchrl.envs.utils import (
_aggregate_resets,
_set_single_key,
_sort_keys,
clear_mpi_env_vars,
)
# legacy
from .libs.envpool import MultiThreadedEnv, MultiThreadedEnvWrapper # noqa: F401
def _check_start(fun):
def decorated_fun(self: _BatchedEnv, *args, **kwargs):
if self.is_closed:
self._create_td()
self._start_workers()
else:
if isinstance(self, ParallelEnv):
_check_for_faulty_process(self._workers)
return fun(self, *args, **kwargs)
return decorated_fun
class _dispatch_caller_parallel:
def __init__(self, attr, parallel_env):
self.attr = attr
self.parallel_env = parallel_env
def __call__(self, *args, **kwargs):
# remove self from args
args = [_arg if _arg is not self.parallel_env else "_self" for _arg in args]
for channel in self.parallel_env.parent_channels:
channel.send((self.attr, (args, kwargs)))
results = []
for channel in self.parallel_env.parent_channels:
msg, result = channel.recv()
results.append(result)
return results
def __iter__(self):
# if the object returned is not a callable
return iter(self.__call__())
class _dispatch_caller_serial:
def __init__(self, list_callable: List[Callable, Any]):
self.list_callable = list_callable
def __call__(self, *args, **kwargs):
return [_callable(*args, **kwargs) for _callable in self.list_callable]
def lazy_property(prop: property):
"""Converts a property in a lazy property, that will call _set_properties when queried the first time."""
return property(fget=lazy(prop.fget), fset=prop.fset)
def lazy(fun):
"""Converts a fun in a lazy fun, that will call _set_properties when queried the first time."""
@wraps(fun)
def new_fun(self, *args, **kwargs):
if not self._properties_set:
self._set_properties()
return fun(self, *args, **kwargs)
return new_fun
class _BatchedEnv(EnvBase):
"""Batched environments allow the user to query an arbitrary method / attribute of the environment running remotely.
Those queries will return a list of length equal to the number of workers containing the
values resulting from those queries.
>>> env = ParallelEnv(3, my_env_fun)
>>> custom_attribute_list = env.custom_attribute
>>> custom_method_list = env.custom_method(*args)
Args:
num_workers: number of workers (i.e. env instances) to be deployed simultaneously;
create_env_fn (callable or list of callables): function (or list of functions) to be used for the environment
creation.
If a single task is used, a callable should be used and not a list of identical callables:
if a list of callable is provided, the environment will be executed as if multiple, diverse tasks were
needed, which comes with a slight compute overhead;
create_env_kwargs (dict or list of dicts, optional): kwargs to be used with the environments being created;
share_individual_td (bool, optional): if ``True``, a different tensordict is created for every process/worker and a lazy
stack is returned.
default = None (False if single task);
shared_memory (bool): whether or not the returned tensordict will be placed in shared memory;
memmap (bool): whether or not the returned tensordict will be placed in memory map.
policy_proof (callable, optional): if provided, it'll be used to get the list of
tensors to return through the :obj:`step()` and :obj:`reset()` methods, such as :obj:`"hidden"` etc.
device (str, int, torch.device): for consistency, this argument is kept. However this
argument should not be passed, as the device will be inferred from the environments.
It is assumed that all environments will run on the same device as a common shared
tensordict will be used to pass data from process to process. The device can be
changed after instantiation using :obj:`env.to(device)`.
num_threads (int, optional): number of threads for this process.
Defaults to the number of workers.
This parameter has no effect for the :class:`~SerialEnv` class.
num_sub_threads (int, optional): number of threads of the subprocesses.
Should be equal to one plus the number of processes launched within
each subprocess (or one if a single process is launched).
Defaults to 1 for safety: if none is indicated, launching multiple
workers may charge the cpu load too much and harm performance.
This parameter has no effect for the :class:`~SerialEnv` class.
"""
_verbose: bool = VERBOSE
_excluded_wrapped_keys = [
"is_closed",
"parent_channels",
"batch_size",
"_dummy_env_str",
]
def __init__(
self,
num_workers: int,
create_env_fn: Union[Callable[[], EnvBase], Sequence[Callable[[], EnvBase]]],
create_env_kwargs: Union[dict, Sequence[dict]] = None,
pin_memory: bool = False,
share_individual_td: Optional[bool] = None,
shared_memory: bool = True,
memmap: bool = False,
policy_proof: Optional[Callable] = None,
device: Optional[DEVICE_TYPING] = None,
allow_step_when_done: bool = False,
num_threads: int = None,
num_sub_threads: int = 1,
):
if device is not None:
raise ValueError(
"Device setting for batched environment can't be done at initialization. "
"The device will be inferred from the constructed environment. "
"It can be set through the `to(device)` method."
)
super().__init__(device=None)
self.is_closed = True
if num_threads is None:
num_threads = num_workers + 1 # 1 more thread for this proc
self.num_sub_threads = num_sub_threads
self.num_threads = num_threads
self._cache_in_keys = None
self._single_task = callable(create_env_fn) or (len(set(create_env_fn)) == 1)
if callable(create_env_fn):
create_env_fn = [create_env_fn for _ in range(num_workers)]
else:
if len(create_env_fn) != num_workers:
raise RuntimeError(
f"num_workers and len(create_env_fn) mismatch, "
f"got {len(create_env_fn)} and {num_workers}"
)
if (
share_individual_td is False and not self._single_task
): # then it has been explicitly set by the user
raise ValueError(
"share_individual_td must be set to None or True when using multi-task batched environments"
)
share_individual_td = True
create_env_kwargs = {} if create_env_kwargs is None else create_env_kwargs
if isinstance(create_env_kwargs, dict):
create_env_kwargs = [
deepcopy(create_env_kwargs) for _ in range(num_workers)
]
self.policy_proof = policy_proof
self.num_workers = num_workers
self.create_env_fn = create_env_fn
self.create_env_kwargs = create_env_kwargs
self.pin_memory = pin_memory
if pin_memory:
raise ValueError("pin_memory for batched envs is deprecated")
self.share_individual_td = bool(share_individual_td)
self._share_memory = shared_memory
self._memmap = memmap
self.allow_step_when_done = allow_step_when_done
if allow_step_when_done:
raise ValueError("allow_step_when_done is deprecated")
if self._share_memory and self._memmap:
raise RuntimeError(
"memmap and shared memory are mutually exclusive features."
)
self._batch_size = None
self._device = None
self._dummy_env_str = None
self._seeds = None
self.__dict__["_input_spec"] = None
self.__dict__["_output_spec"] = None
# self._prepare_dummy_env(create_env_fn, create_env_kwargs)
self._properties_set = False
self._get_metadata(create_env_fn, create_env_kwargs)
def _get_metadata(
self, create_env_fn: List[Callable], create_env_kwargs: List[Dict]
):
if self._single_task:
# if EnvCreator, the metadata are already there
meta_data = get_env_metadata(create_env_fn[0], create_env_kwargs[0])
self.meta_data = meta_data.expand(
*(self.num_workers, *meta_data.batch_size)
)
else:
n_tasks = len(create_env_fn)
self.meta_data = []
for i in range(n_tasks):
self.meta_data.append(
get_env_metadata(create_env_fn[i], create_env_kwargs[i]).clone()
)
self._set_properties()
def update_kwargs(self, kwargs: Union[dict, List[dict]]) -> None:
"""Updates the kwargs of each environment given a dictionary or a list of dictionaries.
Args:
kwargs (dict or list of dict): new kwargs to use with the environments
"""
if isinstance(kwargs, dict):
for _kwargs in self.create_env_kwargs:
_kwargs.update(kwargs)
else:
for _kwargs, _new_kwargs in zip(self.create_env_kwargs, kwargs):
_kwargs.update(_new_kwargs)
def _get_in_keys_to_exclude(self, tensordict):
if self._cache_in_keys is None:
self._cache_in_keys = list(
set(self.input_spec.keys(True)).intersection(
tensordict.keys(True, True)
)
)
return self._cache_in_keys
def _set_properties(self):
meta_data = self.meta_data
self._properties_set = True
if self._single_task:
self._batch_size = meta_data.batch_size
device = self._device = meta_data.device
input_spec = meta_data.specs["input_spec"].to(device)
output_spec = meta_data.specs["output_spec"].to(device)
self.action_spec = input_spec["full_action_spec"]
self.state_spec = input_spec["full_state_spec"]
self.observation_spec = output_spec["full_observation_spec"]
self.reward_spec = output_spec["full_reward_spec"]
self.done_spec = output_spec["full_done_spec"]
self._dummy_env_str = meta_data.env_str
self._env_tensordict = meta_data.tensordict
self._batch_locked = meta_data.batch_locked
else:
self._batch_size = torch.Size([self.num_workers, *meta_data[0].batch_size])
device = self._device = meta_data[0].device
# TODO: check that all action_spec and reward spec match (issue #351)
input_spec = []
for md in meta_data:
input_spec.append(md.specs["input_spec"])
input_spec = torch.stack(input_spec, 0)
output_spec = []
for md in meta_data:
output_spec.append(md.specs["output_spec"])
output_spec = torch.stack(output_spec, 0)
self.action_spec = input_spec["full_action_spec"]
self.state_spec = input_spec["full_state_spec"]
self.observation_spec = output_spec["full_observation_spec"]
self.reward_spec = output_spec["full_reward_spec"]
self.done_spec = output_spec["full_done_spec"]
self._dummy_env_str = str(meta_data[0])
self._env_tensordict = torch.stack(
[meta_data.tensordict for meta_data in meta_data], 0
)
self._batch_locked = meta_data[0].batch_locked
self.has_lazy_inputs = contains_lazy_spec(self.input_spec)
def state_dict(self) -> OrderedDict:
raise NotImplementedError
def load_state_dict(self, state_dict: OrderedDict) -> None:
raise NotImplementedError
batch_size = lazy_property(EnvBase.batch_size)
device = lazy_property(EnvBase.device)
input_spec = lazy_property(EnvBase.input_spec)
output_spec = lazy_property(EnvBase.output_spec)
def _create_td(self) -> None:
"""Creates self.shared_tensordict_parent, a TensorDict used to store the most recent observations."""
if self._single_task:
shared_tensordict_parent = self._env_tensordict.clone()
if not self._env_tensordict.shape[0] == self.num_workers:
raise RuntimeError(
"batched environment base tensordict has the wrong shape"
)
else:
shared_tensordict_parent = self._env_tensordict.clone()
if self._single_task:
self._env_input_keys = sorted(
list(self.input_spec["full_action_spec"].keys(True, True))
+ list(self.state_spec.keys(True, True)),
key=_sort_keys,
)
self._env_output_keys = []
self._env_obs_keys = []
for key in self.output_spec["full_observation_spec"].keys(True, True):
self._env_output_keys.append(key)
self._env_obs_keys.append(key)
self._env_output_keys += self.reward_keys + self.done_keys
else:
env_input_keys = set()
for meta_data in self.meta_data:
if meta_data.specs["input_spec", "full_state_spec"] is not None:
env_input_keys = env_input_keys.union(
meta_data.specs["input_spec", "full_state_spec"].keys(
True, True
)
)
env_input_keys = env_input_keys.union(
meta_data.specs["input_spec", "full_action_spec"].keys(True, True)
)
env_output_keys = set()
env_obs_keys = set()
for meta_data in self.meta_data:
env_obs_keys = env_obs_keys.union(
key
for key in meta_data.specs["output_spec"][
"full_observation_spec"
].keys(True, True)
)
env_output_keys = env_output_keys.union(
meta_data.specs["output_spec"]["full_observation_spec"].keys(
True, True
)
)
env_output_keys = env_output_keys.union(self.reward_keys + self.done_keys)
self._env_obs_keys = sorted(env_obs_keys, key=_sort_keys)
self._env_input_keys = sorted(env_input_keys, key=_sort_keys)
self._env_output_keys = sorted(env_output_keys, key=_sort_keys)
reset_keys = self.reset_keys
self._selected_keys = (
set(self._env_output_keys)
.union(self._env_input_keys)
.union(self._env_obs_keys)
.union(set(self.done_keys))
)
self._selected_keys = self._selected_keys.union(reset_keys)
# input keys
self._selected_input_keys = {
_unravel_key_to_tuple(key) for key in self._env_input_keys
}
# output keys after reset
self._selected_reset_keys = {
_unravel_key_to_tuple(key)
for key in self._env_obs_keys + self.done_keys + reset_keys
}
# output keys after reset, filtered
self._selected_reset_keys_filt = {
unravel_keys(key) for key in self._env_obs_keys + self.done_keys
}
# output keys after step
self._selected_step_keys = {
_unravel_key_to_tuple(key) for key in self._env_output_keys
}
if self._single_task:
shared_tensordict_parent = shared_tensordict_parent.select(
*self._selected_keys,
"next",
strict=False,
)
self.shared_tensordict_parent = shared_tensordict_parent.to(self.device)
else:
# Multi-task: we share tensordict that *may* have different keys
shared_tensordict_parent = [
tensordict.select(*self._selected_keys, "next", strict=False).to(
self.device
)
for tensordict in shared_tensordict_parent
]
shared_tensordict_parent = torch.stack(
shared_tensordict_parent,
0,
)
self.shared_tensordict_parent = shared_tensordict_parent
if self.share_individual_td:
if not isinstance(self.shared_tensordict_parent, LazyStackedTensorDict):
self.shared_tensordicts = [
td.clone() for td in self.shared_tensordict_parent.unbind(0)
]
self.shared_tensordict_parent = torch.stack(self.shared_tensordicts, 0)
else:
# Multi-task: we share tensordict that *may* have different keys
# LazyStacked already stores this so we don't need to do anything
self.shared_tensordicts = self.shared_tensordict_parent
if self.device.type == "cpu":
if self._share_memory:
for td in self.shared_tensordicts:
td.share_memory_()
elif self._memmap:
for td in self.shared_tensordicts:
td.memmap_()
else:
if self._share_memory:
self.shared_tensordict_parent.share_memory_()
if not self.shared_tensordict_parent.is_shared():
raise RuntimeError("share_memory_() failed")
elif self._memmap:
self.shared_tensordict_parent.memmap_()
if not self.shared_tensordict_parent.is_memmap():
raise RuntimeError("memmap_() failed")
self.shared_tensordicts = self.shared_tensordict_parent.unbind(0)
def _start_workers(self) -> None:
"""Starts the various envs."""
raise NotImplementedError
def __repr__(self) -> str:
if self._dummy_env_str is None:
self._dummy_env_str = self._set_properties()
return (
f"{self.__class__.__name__}("
f"\n\tenv={self._dummy_env_str}, "
f"\n\tbatch_size={self.batch_size})"
)
def close(self) -> None:
if self.is_closed:
raise RuntimeError("trying to close a closed environment")
if self._verbose:
print(f"closing {self.__class__.__name__}")
self.__dict__["_input_spec"] = None
self.__dict__["_output_spec"] = None
self._properties_set = False
self.event = None
self._shutdown_workers()
self.is_closed = True
def _shutdown_workers(self) -> None:
raise NotImplementedError
def _set_seed(self, seed: Optional[int]):
"""This method is not used in batched envs."""
pass
@lazy
def start(self) -> None:
if not self.is_closed:
raise RuntimeError("trying to start a environment that is not closed.")
self._create_td()
self._start_workers()
def to(self, device: DEVICE_TYPING):
device = torch.device(device)
if device == self.device:
return self
self._device = device
self.meta_data = (
self.meta_data.to(device)
if self._single_task
else [meta_data.to(device) for meta_data in self.meta_data]
)
if not self.is_closed:
warn(
"Casting an open environment to another device requires closing and re-opening it. "
"This may have unexpected and unwanted effects (e.g. on seeding etc.)"
)
# the tensordicts must be re-created on device
super().to(device)
self.close()
self.start()
else:
if self.__dict__["_input_spec"] is not None:
self.__dict__["_input_spec"] = self.__dict__["_input_spec"].to(device)
if self.__dict__["_output_spec"] is not None:
self.__dict__["_output_spec"] = self.__dict__["_output_spec"].to(device)
return self
class SerialEnv(_BatchedEnv):
"""Creates a series of environments in the same process."""
__doc__ += _BatchedEnv.__doc__
_share_memory = False
def _start_workers(self) -> None:
_num_workers = self.num_workers
self._envs = []
for idx in range(_num_workers):
env = self.create_env_fn[idx](**self.create_env_kwargs[idx])
self._envs.append(env.to(self.device))
self.is_closed = False
@_check_start
def state_dict(self) -> OrderedDict:
state_dict = OrderedDict()
for idx, env in enumerate(self._envs):
state_dict[f"worker{idx}"] = env.state_dict()
return state_dict
@_check_start
def load_state_dict(self, state_dict: OrderedDict) -> None:
if "worker0" not in state_dict:
state_dict = OrderedDict(
**{f"worker{idx}": state_dict for idx in range(self.num_workers)}
)
for idx, env in enumerate(self._envs):
env.load_state_dict(state_dict[f"worker{idx}"])
@_check_start
def _step(
self,
tensordict: TensorDict,
) -> TensorDict:
tensordict_in = tensordict.clone(False)
next_td = self.shared_tensordict_parent.get("next")
for i in range(self.num_workers):
# shared_tensordicts are locked, and we need to select the keys since we update in-place.
# There may be unexpected keys, such as "_reset", that we should comfortably ignore here.
out_td = self._envs[i]._step(tensordict_in[i])
next_td[i].update_(out_td.select(*self._env_output_keys, strict=False))
# We must pass a clone of the tensordict, as the values of this tensordict
# will be modified in-place at further steps
if self._single_task:
out = TensorDict(
{}, batch_size=self.shared_tensordict_parent.shape, device=self.device
)
for key in self._selected_step_keys:
_set_single_key(next_td, out, key, clone=True)
else:
# strict=False ensures that non-homogeneous keys are still there
out = next_td.select(*self._selected_step_keys, strict=False).clone()
return out
def _shutdown_workers(self) -> None:
if not self.is_closed:
for env in self._envs:
env.close()
del self._envs
@_check_start
def set_seed(
self, seed: Optional[int] = None, static_seed: bool = False
) -> Optional[int]:
for env in self._envs:
new_seed = env.set_seed(seed, static_seed=static_seed)
seed = new_seed
return seed
@_check_start
def _reset(self, tensordict: TensorDictBase, **kwargs) -> TensorDictBase:
if tensordict is not None:
needs_resetting = _aggregate_resets(tensordict, reset_keys=self.reset_keys)
if needs_resetting.ndim > 2:
needs_resetting = needs_resetting.flatten(1, needs_resetting.ndim - 1)
if needs_resetting.ndim > 1:
needs_resetting = needs_resetting.any(-1)
elif not needs_resetting.ndim:
needs_resetting = needs_resetting.expand((self.num_workers,))
else:
needs_resetting = torch.ones(
(self.num_workers,), device=self.device, dtype=torch.bool
)
for i, _env in enumerate(self._envs):
if tensordict is not None:
tensordict_ = tensordict[i]
if tensordict_.is_empty():
tensordict_ = None
else:
tensordict_ = None
if not needs_resetting[i]:
# We update the stored tensordict with the value of the "next"
# key as one may be surprised to receive data that is not up-to-date
# If we don't do this, the result of calling reset and skipping one env
# will be that the env will have the data from the previous
# step at the root (since the shared_tensordict did not go through
# step_mdp).
self.shared_tensordicts[i].update_(
self.shared_tensordicts[i]
.get("next")
.select(*self._selected_reset_keys, strict=False)
)
if tensordict_ is not None:
self.shared_tensordicts[i].update_(
tensordict_.select(*self._selected_reset_keys, strict=False)
)
continue
_td = _env._reset(tensordict=tensordict_, **kwargs)
self.shared_tensordicts[i].update_(
_td.select(*self._selected_reset_keys, strict=False)
)
selected_output_keys = self._selected_reset_keys_filt
if self._single_task:
# select + clone creates 2 tds, but we can create one only
out = TensorDict(
{}, batch_size=self.shared_tensordict_parent.shape, device=self.device
)
for key in selected_output_keys:
_set_single_key(self.shared_tensordict_parent, out, key, clone=True)
return out
else:
return self.shared_tensordict_parent.select(
*selected_output_keys,
strict=False,
).clone()
def __getattr__(self, attr: str) -> Any:
if attr in self.__dir__():
return super().__getattr__(
attr
) # make sure that appropriate exceptions are raised
elif attr.startswith("__"):
raise AttributeError(
"dispatching built-in private methods is "
f"not permitted with type {type(self)}. "
f"Got attribute {attr}."
)
else:
if attr in self._excluded_wrapped_keys:
raise AttributeError(f"Getting {attr} resulted in an exception")
try:
# determine if attr is a callable
list_attr = [getattr(env, attr) for env in self._envs]
callable_attr = callable(list_attr[0])
if callable_attr:
if self.is_closed:
raise RuntimeError(
"Trying to access attributes of closed/non started "
"environments. Check that the batched environment "
"has been started (e.g. by calling env.reset)"
)
return _dispatch_caller_serial(list_attr)
else:
return list_attr
except AttributeError:
raise AttributeError(
f"attribute {attr} not found in " f"{self._dummy_env_str}"
)
def to(self, device: DEVICE_TYPING):
device = torch.device(device)
if device == self.device:
return self
super().to(device)
if not self.is_closed:
for env in self._envs:
env.to(device)
return self
class ParallelEnv(_BatchedEnv):
"""Creates one environment per process.
TensorDicts are passed via shared memory or memory map.
"""
__doc__ += _BatchedEnv.__doc__
def _start_workers(self) -> None:
from torchrl.envs.env_creator import EnvCreator
torch.set_num_threads(self.num_threads)
ctx = mp.get_context("spawn")
_num_workers = self.num_workers
self.parent_channels = []
self._workers = []
self._events = []
if self.device.type == "cuda":
self.event = torch.cuda.Event()
else:
self.event = None
with clear_mpi_env_vars():
for idx in range(_num_workers):
if self._verbose:
print(f"initiating worker {idx}")
# No certainty which module multiprocessing_context is
parent_pipe, child_pipe = ctx.Pipe()
event = ctx.Event()
self._events.append(event)
env_fun = self.create_env_fn[idx]
if not isinstance(env_fun, EnvCreator):
env_fun = CloudpickleWrapper(env_fun)
process = _ProcessNoWarn(
target=_run_worker_pipe_shared_mem,
num_threads=self.num_sub_threads,
args=(
parent_pipe,
child_pipe,
env_fun,
self.create_env_kwargs[idx],
self.device,
event,
self.shared_tensordicts[idx],
self._selected_input_keys,
self._selected_reset_keys,
self._selected_step_keys,
self.has_lazy_inputs,
),
)
process.daemon = True
process.start()
child_pipe.close()
self.parent_channels.append(parent_pipe)
self._workers.append(process)
for parent_pipe in self.parent_channels:
msg = parent_pipe.recv()
assert msg == "started"
# send shared tensordict to workers
for channel in self.parent_channels:
channel.send(("init", None))
self.is_closed = False
@_check_start
def state_dict(self) -> OrderedDict:
state_dict = OrderedDict()
for channel in self.parent_channels:
channel.send(("state_dict", None))
for idx, channel in enumerate(self.parent_channels):
msg, _state_dict = channel.recv()
if msg != "state_dict":
raise RuntimeError(f"Expected 'state_dict' but received {msg}")
state_dict[f"worker{idx}"] = _state_dict
return state_dict
@_check_start
def load_state_dict(self, state_dict: OrderedDict) -> None:
if "worker0" not in state_dict:
state_dict = OrderedDict(
**{f"worker{idx}": state_dict for idx in range(self.num_workers)}
)
for i, channel in enumerate(self.parent_channels):
channel.send(("load_state_dict", state_dict[f"worker{i}"]))
for event in self._events:
event.wait()
event.clear()
@_check_start
def _step(self, tensordict: TensorDictBase) -> TensorDictBase:
if self._single_task and not self.has_lazy_inputs:
# this is faster than update_ but won't work for lazy stacks
for key in self._env_input_keys:
key = _unravel_key_to_tuple(key)
self.shared_tensordict_parent._set_tuple(
key,
tensordict._get_tuple(key, None),
inplace=True,
validated=True,
)
else:
self.shared_tensordict_parent.update_(
tensordict.select(*self._env_input_keys, strict=False)
)
if self.event is not None:
self.event.record()
self.event.synchronize()
for i in range(self.num_workers):
self.parent_channels[i].send(("step", None))
for i in range(self.num_workers):
event = self._events[i]
event.wait()
event.clear()
# We must pass a clone of the tensordict, as the values of this tensordict
# will be modified in-place at further steps
next_td = self.shared_tensordict_parent.get("next")
if self._single_task:
out = TensorDict(
{}, batch_size=self.shared_tensordict_parent.shape, device=self.device
)
for key in self._selected_step_keys:
_set_single_key(next_td, out, key, clone=True)
else:
# strict=False ensures that non-homogeneous keys are still there
out = next_td.select(*self._selected_step_keys, strict=False).clone()
return out
@_check_start
def _reset(self, tensordict: TensorDictBase, **kwargs) -> TensorDictBase:
if tensordict is not None:
needs_resetting = _aggregate_resets(tensordict, reset_keys=self.reset_keys)
if needs_resetting.ndim > 2:
needs_resetting = needs_resetting.flatten(1, needs_resetting.ndim - 1)
if needs_resetting.ndim > 1:
needs_resetting = needs_resetting.any(-1)
elif not needs_resetting.ndim:
needs_resetting = needs_resetting.expand((self.num_workers,))
else:
needs_resetting = torch.ones(
(self.num_workers,), device=self.device, dtype=torch.bool
)
workers = []
for i, channel in enumerate(self.parent_channels):
if tensordict is not None:
tensordict_ = tensordict[i]
if tensordict_.is_empty():
tensordict_ = None
else:
tensordict_ = None
if not needs_resetting[i]:
# We update the stored tensordict with the value of the "next"
# key as one may be surprised to receive data that is not up-to-date
# If we don't do this, the result of calling reset and skipping one env
# will be that the env will have the data from the previous
# step at the root (since the shared_tensordict did not go through
# step_mdp).
self.shared_tensordicts[i].update_(
self.shared_tensordicts[i]
.get("next")
.select(*self._selected_reset_keys, strict=False)
)
if tensordict_ is not None:
self.shared_tensordicts[i].update_(
tensordict_.select(*self._selected_reset_keys, strict=False)
)
continue
out = ("reset", tensordict_)
channel.send(out)
workers.append(i)
for i in workers:
event = self._events[i]
event.wait()
event.clear()
selected_output_keys = self._selected_reset_keys_filt
if self._single_task:
# select + clone creates 2 tds, but we can create one only
out = TensorDict(
{}, batch_size=self.shared_tensordict_parent.shape, device=self.device
)
for key in selected_output_keys:
_set_single_key(self.shared_tensordict_parent, out, key, clone=True)
return out
else:
return self.shared_tensordict_parent.select(
*selected_output_keys,
strict=False,
).clone()
@_check_start
def _shutdown_workers(self) -> None:
if self.is_closed:
raise RuntimeError(
"calling {self.__class__.__name__}._shutdown_workers only allowed when env.is_closed = False"
)
for i, channel in enumerate(self.parent_channels):
if self._verbose:
print(f"closing {i}")
channel.send(("close", None))
self._events[i].wait()
self._events[i].clear()
del self.shared_tensordicts, self.shared_tensordict_parent
for channel in self.parent_channels:
channel.close()
for proc in self._workers:
proc.join()
del self._workers
del self.parent_channels
@_check_start
def set_seed(
self, seed: Optional[int] = None, static_seed: bool = False
) -> Optional[int]:
self._seeds = []
for channel in self.parent_channels:
channel.send(("seed", (seed, static_seed)))
self._seeds.append(seed)
msg, new_seed = channel.recv()
if msg != "seeded":
raise RuntimeError(f"Expected 'seeded' but received {msg}")
seed = new_seed
return seed
def __reduce__(self):
if not self.is_closed:
# ParallelEnv contains non-instantiated envs, thus it can be
# closed and serialized if the environment building functions
# permit it
self.close()
return super().__reduce__()
def __getattr__(self, attr: str) -> Any:
if attr in self.__dir__():
return super().__getattr__(
attr
) # make sure that appropriate exceptions are raised
elif attr.startswith("__"):
raise AttributeError(
"dispatching built-in private methods is not permitted."
)
else:
if attr in self._excluded_wrapped_keys:
raise AttributeError(f"Getting {attr} resulted in an exception")
try:
# _ = getattr(self._dummy_env, attr)
if self.is_closed:
raise RuntimeError(
"Trying to access attributes of closed/non started "
"environments. Check that the batched environment "
"has been started (e.g. by calling env.reset)"
)
# dispatch to workers
return _dispatch_caller_parallel(attr, self)
except AttributeError:
raise AttributeError(
f"attribute {attr} not found in " f"{self._dummy_env_str}"
)
def to(self, device: DEVICE_TYPING):
device = torch.device(device)
if device == self.device:
return self
super().to(device)
if self._seeds is not None:
warn(
"Sending a seeded ParallelEnv to another device requires "
f"re-seeding it. Re-seeding envs to {self._seeds}."
)
self.set_seed(self._seeds[0])
return self
def _recursively_strip_locks_from_state_dict(state_dict: OrderedDict) -> OrderedDict:
return OrderedDict(
**{
k: _recursively_strip_locks_from_state_dict(item)
if isinstance(item, OrderedDict)
else None
if isinstance(item, MpLock)
else item
for k, item in state_dict.items()
}
)