Recurrent Reinforcement Learning with Memoroids

Steven Morad, Chris Lu, Ryan Kortvelesy, Stephan Liwicki, Jakob Nicolaus Foerster, Amanda Prorok

TL;DR

Researchers have introduced memoroids, a mathematical framework that reformulates efficient sequence models (like Linear Recurrent Units) as associative operations. By leveraging this framework, they developed Tape-Based Batching (TBB), a method that allows reinforcement learning (RL) agents to train on entire, variable-length episodes. This eliminates the efficiency-killing padding and performance-degrading gradient truncation found in standard RL libraries.

The Problem with Segment-Based Batching

In Reinforcement Learning, agents often face "Partially Observable" environments where they cannot see the full state of the world (e.g., a robot navigating a house based solely on a localized camera feed). To succeed, these agents require memory. Traditionally, Recurrent Neural Networks (RNNs) or Transformers are used, but they scale poorly to long sequences.

To make training feasible, most RL libraries rely on Segment-Based Batching (SBB). They chop long episodes into fixed-length "segments" (e.g., 64 steps) and pad shorter ones with zeros to fit a rectangular tensor.

Segment-Based Batching (SBB) chops rollouts into fixed segments, padding shorter ones with zeros to fit a rectangular tensor.

This approach introduces three major flaws:

  1. Waste: Zero-padding consumes memory and computation for no actual gain.
  2. Bias: Padding interferes with normalization layers like BatchNorm.
  3. Truncation: Because gradients do not flow between segments (due to Truncated Backpropagation Through Time), the agent literally cannot learn dependencies longer than the segment length.

Methods: Memoroids and Tape-Based Batching

The authors propose a two-part solution to overcome the limitations of SBB:

  1. Memoroids: The authors observe that a new class of "Linear Recurrent Models" (like S5 or LRU) relies on associative updates. In mathematical terms, these are monoids. They extend this into a "memoroid" framework that decouples the input, the hidden memory state, and the output. Because memoroids are associative, they can be computed in parallel across the time dimension using a "parallel scan" (specifically the Blelloch scan), making them incredibly fast on GPUs.
  2. Tape-Based Batching (TBB): Instead of a 2D grid of segments ($Batch \times Time$), TBB treats the replay buffer as a single long "tape" of concatenated episodes. To prevent information from "leaking" from the end of one episode into the start of the next during the parallel scan, they introduce a resettable monoid transformation. This allows the model to process a massive string of different episodes in one parallel pass while mathematically "resetting" the memory at every episode boundary.

Datasets

The authors evaluated their approach on:

  • POPGym: A benchmark specifically designed to test long-term memory in RL (featuring tasks like Count Recall, Repeat Previous, and Minesweeper).
  • Arcade Learning Environment (ALE): Standard Atari 2600 benchmarks (e.g., Asteroids, Pong, Frostbite) to demonstrate that the method works in complex visual environments.

Key Insights and Ideas

  • Truncation is worse than we thought: The authors discovered that truncated gradients don't just limit memory—they actively degrade the agent's ability to estimate "Value" (how good a state is). Even if a task only requires 10 steps of memory to predict a reward, the value function often needs to look back across the entire episode to be accurate. SBB prevents this, while TBB enables it.
Comparison of gradient flow between SBB (left) and TBB (right). SBB artificially truncates gradients, whereas TBB allows them to flow across the entire episode.
  • Linear Recurrent Models are Monoids: By framing modern sequence models as category-theory objects (monoids), we unlock highly optimized parallel algorithms that were previously reserved for simple operations like prefix sums.
  • Simplifying the Codebase: Because TBB handles the recurrence via a pre-computed "scan," the actual RL loss functions (like Q-learning or PPO) can be written as if they were non-recurrent. This removes the need for complex masking and padding logic in RL implementations.

Key Results

  • Sample Efficiency: Agents trained with TBB reached higher scores much faster than those using SBB. In tasks like Count Recall, TBB succeeded where SBB failed entirely.
  • Speed: Using the memoroid framework to compute RL targets (like Discounted Return or Generalized Advantage Estimation) was three orders of magnitude faster than standard iterative implementations.
  • Wall-Clock Parity: Despite the theoretical overhead of processing longer sequences, TBB's training time was nearly identical to SBB because it avoids the expensive "split-and-pad" operations that slow down standard data pipelines.

Limitations and Future Work

  • Scaling Costs: TBB has a slightly higher computational complexity ($O(\log B)$ where $B$ is the total transitions in a batch) compared to SBB’s $O(\log L)$ (where $L$ is segment length). While not noticeable in these experiments, it might become a factor for extremely long-context "In-Context RL."
  • Model Constraints: This approach specifically requires models that fit the memoroid/linear recurrent framework. It wouldn't directly apply to standard "Softmax" Transformers or traditional LSTMs without modification.
  • Scope of Tasks: The benefits are most pronounced in memory-heavy tasks. The authors note it is still unclear how much this helps in environments like Atari, where the "memory" requirements are often quite short.

Summary updated

BibTeX
@inproceedings{morad2024recurrent,
  author = {Steven Morad and Chris Lu and Ryan Kortvelesy and Stephan Liwicki and Jakob Nicolaus Foerster and Amanda Prorok},
  title = {{Recurrent Reinforcement Learning with Memoroids}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=nA4Q983a1v},
}
Figure