TensorFlow Agents: Efficient Batched Reinforcement Learning in TensorFlow

Danijar Hafner, James Davidson, Vincent VanhouckearXiv 2017

Abstract

TensorFlow Agents (TF Agents) is an open-source framework designed to accelerate reinforcement learning (RL) by parallelizing environment simulation and batching neural network inference. By bypassing Python’s Global Interpreter Lock (GIL) and integrating environments directly into the TensorFlow graph, it enables high-performance training on a single machine without the complexity of asynchronous distributed systems.

Introduction

Reinforcement Learning (RL) is notoriously computationally expensive. Two primary bottlenecks typically slow down training: neural network inference and environment stepping. Standard RL loops often process one observation at a time, failing to leverage the parallel processing power of GPUs or multi-core CPUs. Furthermore, many RL environments are written in Python, which cannot execute multiple environments in parallel on different CPU cores within a single process due to the Global Interpreter Lock (GIL).

While previous solutions used asynchronous "workers" to collect data, these often introduced significant communication overhead and complex synchronization requirements. TF Agents aims to provide a simpler, synchronous, and more efficient infrastructure for batched RL.

Methodology

The framework introduces several key architectural innovations to streamline the RL loop:

  • Batched Inference: The agent is designed to process a batch of observations from multiple environments simultaneously. This allows TensorFlow to utilize its internal thread pools or hardware accelerators (GPUs/TPUs) effectively.
  • Process-Level Parallelism: To circumvent the GIL, TF Agents spawns each environment in its own separate Python process. A wrapper (ExternalProcess) handles the communication between the main training process and these sub-processes.
  • In-Graph Integration: The framework provides an InGraphBatchEnv class that allows the environment to exist as a node within the TensorFlow computation graph. This "fuses" the environment and the algorithm, allowing the entire training loop to be executed with a single session.run() call, minimizing the overhead of switching between Python and C++.
  • BatchPPO: As a proof of concept, the authors implemented an optimized version of Proximal Policy Optimization (PPO) that uses a KL-divergence penalty to keep policy updates stable.

Experimental Evaluation

The authors evaluated their framework using the MuJoCo (Multi-Joint dynamics with Contact) physics engine, a standard benchmark for continuous control tasks. Specifically, they tested on HalfCheetah-v1, Walker2d-v1, and Humanoid-v1.

BatchPPO performance across different MuJoCo environments plotted against environment steps. The blue line (mean action) consistently outperforms the red line (sampling).

As shown in Figure 1, the agent successfully learns complex locomotion tasks. The synchronous approach makes the implementation conceptually simpler and easier to debug while still providing massive speedups through batching. By putting the environment in the graph, the training loop begins to resemble standard supervised learning: providing a "batch" of data and performing an update.

Training time in hours for BatchPPO on 6 CPU cores. The implementation demonstrates the ability to solve locomotion tasks on a single machine relatively quickly.

Efficiency is a core strength of the framework. Using only 6 CPU cores, the system can solve challenging tasks like Humanoid-v1 in a fraction of the time required by non-batched implementations. Figure 2 illustrates the wall-clock time required to reach high performance levels across the benchmark suite.

Discussion and Future Work

While TF Agents significantly improves performance, some limitations remain. Inter-process communication (IPC) overhead still exists when sending data between the main process and environment sub-processes. Additionally, hardware utilization gaps occur when the system alternates between loading CPUs (to step environments) and the GPU (for inference).

Future work may explore "action lag"—allowing the environment to step while the GPU is computing—to further optimize hardware utilization. The authors also note that the ultimate performance gain would come from environments implemented natively in a vectorized way (e.g., directly in TensorFlow or C++), which would eliminate the need for IPC entirely.

Summary updated

BibTeX
@misc{hafner2017tensorflow,
  author = {Danijar Hafner and James Davidson and Vincent Vanhoucke},
  title = {{TensorFlow Agents: Efficient Batched Reinforcement Learning in TensorFlow}},
  year = {2017},
  eprint = {1709.02878},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/1709.02878},
}
Figure