Executive Summary
TorchRL is a new, highly modular reinforcement learning (RL) and decision-making library built natively for PyTorch. By introducing TensorDict—a flexible data carrier that standardizes communication between independent RL components—TorchRL allows researchers to easily mix and match environments, replay buffers, and models while achieving state-of-the-art computational efficiency and scaling.
The Modularity vs. Integration Trade-off
The reinforcement learning ecosystem is notoriously fragmented. Because RL pipelines require complex interactions between environments, data collectors, replay buffers, and neural networks, library developers usually face a strict trade-off: modularity vs. integration.
High-level libraries are highly integrated and easy to use, but their rigid structures make it difficult for researchers to prototype novel, out-of-the-box algorithms. Conversely, low-level libraries offer flexibility but require users to write massive amounts of complex boilerplate code. Furthermore, many existing tools struggle to support modern scaling demands, such as distributed training, offline RL, multi-agent setups, and GPU-accelerated vectorized environments. TorchRL bridges this gap by providing a comprehensive, scalable, and highly composable set of building blocks for PyTorch.
TorchRL Architecture and TensorDict
To solve the integration problem without sacrificing flexibility, the authors introduce a novel PyTorch primitive called TensorDict.
- The
TensorDictProtocol: Instead of hardcoding specific input/output tuples (likestate, action, reward, next_state), all TorchRL components communicate by passing aTensorDict. This dictionary-like object stores tensors but natively understands metadata likebatch_sizeanddevice. It allows for shape-based indexing, easy device casting (e.g.,.to("cuda")), and batched transformations. - Decoupled Components: Because every module expects and returns a
TensorDict, components become completely agnostic to each other. A replay buffer doesn't need to know the architecture of the policy, and a data collector doesn't need to know if the environment is single-agent or multi-agent. tensordict.nn: The library provides wrappers for standard PyTorch modules, allowing them to read and write directly to specific keys within aTensorDict. This supports functional programming, vectorized maps (vmap), and is fully compatible withtorch.compilefor speed.
By standardizing data rather than algorithms, TorchRL avoids the rigid class inheritance structures that plague other RL libraries. Users can build an offline Decision Transformer or a distributed IMPALA agent using the exact same underlying data structures.
Supported Environments and Tasks
The authors evaluated TorchRL across a wide variety of environments and datasets to prove its generality:
- Continuous Control: MuJoCo environments (HalfCheetah, Hopper, Walker2d, Ant).
- Discrete Control: Atari 2600 games (Pong, Freeway, Boxing, Breakout).
- Offline RL: D4RL datasets (e.g., hopper-medium, halfcheetah-medium).
- Multi-Agent RL (MARL): VMAS (Vectorized Multi-Agent Simulator) for tasks like Navigation, Balance, and Sampling.
- Supported Backends: Gym/Gymnasium, DeepMind Control, Brax, IsaacGym, and more.
Performance and Scaling Results
TorchRL successfully reproduces the original paper performance for a massive suite of algorithms, including A2C, PPO, DDPG, TD3, SAC, IMPALA, Decision Transformer, and Implicit Q-Learning (IQL).
Beyond accuracy, TorchRL excels in computational efficiency and data collection speed:
- Efficiency: TorchRL computes Generalized Advantage Estimation (GAE) in just 1.33ms on a GPU, outperforming Jax-based CleanRL (1.43ms) and vastly outperforming Numpy-based Stable-Baselines3 (14.1ms).
- Data Collection: In a 32-worker setup on HalfCheetah, TorchRL's async collector achieved ~32,800 frames per second (FPS), compared to Tianshou (~4,700 FPS) and RLlib (~1,800 FPS).
- Vectorized Scaling: Modern hardware accelerators thrive on parallelization. Because
TensorDictnatively handles multidimensional batch sizes, TorchRL seamlessly integrates with vectorized simulators. When tested on the VMAS multi-agent simulator with 20,000 vectorized environments, TorchRL achieved an astonishing ~538,000 FPS, whereas RLlib bottlenecked at around 1,500 FPS.
Limitations and Future Work
While highly optimized, the authors acknowledge that TensorDict introduces a slight computational overhead compared to native PyTree operations (e.g., a simple value increment took 1.05ms vs 0.91ms) due to the extra work of handling batch-size and device metadata. Additionally, because TorchRL prioritizes granular control, writing a training loop from scratch can be more verbose than using highly opinionated libraries.
Moving forward, the team is actively refactoring TensorDict functional calls to better support distributed and graph settings. They are also working on allowing more loss modules to be used without strictly requiring TensorDict instances, further freeing users from specific design choices.
Summary updated