@@ -427,6 +427,87 @@ etc.), but one can not use an arbitrary TorchRL environment, as it is possible w
427
427
ParallelEnv
428
428
EnvCreator
429
429
430
+ Async environments
431
+ ------------------
432
+
433
+ Asynchronous environments allow for parallel execution of multiple environments, which can significantly speed up the
434
+ data collection process in reinforcement learning.
435
+
436
+ The `AsyncEnvPool ` class and its subclasses provide a flexible interface for managing these environments using different
437
+ backends, such as threading and multiprocessing.
438
+
439
+ The `AsyncEnvPool ` class serves as a base class for asynchronous environment pools, providing a common interface for
440
+ managing multiple environments concurrently. It supports different backends for parallel execution, such as threading
441
+ and multiprocessing, and provides methods for asynchronous stepping and resetting of environments.
442
+
443
+ Contrary to :class: `~torchrl.envs.ParallelEnv `, :class: `~torchrl.envs.AsyncEnvPool ` and its subclasses permit the
444
+ execution of a given set of sub-environments while another task performed, allowing for complex asynchronous jobs to be
445
+ run at the same time. For instance, it is possible to execute some environments while the policy is running based on
446
+ the output of others.
447
+
448
+ This family of classes is particularly interesting when dealing with environments that have a high (and/or variable)
449
+ latency.
450
+
451
+ .. note :: This class and its subclasses should work when nested in with :class:`~torchrl.envs.TransformedEnv` and
452
+ batched environments, but users won't currently be able to use the async features of the base environment when
453
+ it's nested in these classes. One should prefer nested transformed envs within an `AsyncEnvPool ` instead.
454
+ If this is not possible, please raise an issue.
455
+
456
+ Classes
457
+ ~~~~~~~
458
+
459
+ - :class: `~torchrl.envs.AsyncEnvPool `: A base class for asynchronous environment pools. It determines the backend
460
+ implementation to use based on the provided arguments and manages the lifecycle of the environments.
461
+ - :class: `~torchrl.envs.ProcessorAsyncEnvPool `: An implementation of :class: `~torchrl.envs.AsyncEnvPool ` using
462
+ multiprocessing for parallel execution of environments. This class manages a pool of environments, each running in
463
+ its own process, and provides methods for asynchronous stepping and resetting of environments using inter-process
464
+ communication. It is automatically instantiated when `"multiprocessing" ` is passed as a backend during the
465
+ :class: `~torchrl.envs.AsyncEnvPool ` instantiation.
466
+ - :class: `~torchrl.envs.ThreadingAsyncEnvPool `: An implementation of :class: `~torchrl.envs.AsyncEnvPool ` using
467
+ threading for parallel execution of environments. This class manages a pool of environments, each running in its own
468
+ thread, and provides methods for asynchronous stepping and resetting of environments using a thread pool executor.
469
+ It is automatically instantiated when `"threading" ` is passed as a backend during the
470
+ :class: `~torchrl.envs.AsyncEnvPool ` instantiation.
471
+
472
+ Example
473
+ ~~~~~~~
474
+
475
+ >>> from functools import partial
476
+ >>> from torchrl.envs import AsyncEnvPool, GymEnv
477
+ >>> import torch
478
+ >>> # Choose backend
479
+ >>> backend = " threading"
480
+ >>> env = AsyncEnvPool(
481
+ >>> [partial(GymEnv, " Pendulum-v1" ), partial(GymEnv, " CartPole-v1" )],
482
+ >>> stack= " lazy" ,
483
+ >>> backend= backend
484
+ >>> )
485
+ >>> # Execute a synchronous reset
486
+ >>> reset = env.reset()
487
+ >>> print (reset)
488
+ >>> # Execute a synchronous step
489
+ >>> s = env.rand_step(reset)
490
+ >>> print (s)
491
+ >>> # Execute an asynchronous step in env 0
492
+ >>> s0 = s[0 ]
493
+ >>> s0[" action" ] = torch.randn(1 ).clamp(- 1 , 1 )
494
+ >>> s0[" env_index" ] = 0
495
+ >>> env.async_step_send(s0)
496
+ >>> # Receive data
497
+ >>> s0_result = env.async_step_recv()
498
+ >>> print (' result' , s0_result)
499
+ >>> # Close env
500
+ >>> env.close()
501
+
502
+
503
+ .. autosummary ::
504
+ :toctree: generated/
505
+ :template: rl_template.rst
506
+
507
+ AsyncEnvPool
508
+ ProcessorAsyncEnvPool
509
+ ThreadingAsyncEnvPool
510
+
430
511
431
512
Custom native TorchRL environments
432
513
----------------------------------
0 commit comments