ED-Batch: Efficient Automatic Batching of Dynamic Neural Networks via Learned Finite State Machines

Siyuan Chen, Pratik Pramod Fegade, Tianqi Chen, Phillip B. Gibbons, Todd C. Mowry

Abstract

Dynamic neural networks, such as those utilizing tree or lattice structures, are essential for modern natural language processing and speech recognition. However, their input-dependent architectures make efficient batching—a prerequisite for high-performance GPU execution—notoriously difficult. Traditional frameworks rely on greedy heuristics that often lead to suboptimal kernel launches and significant memory overhead from gather/scatter operations. We present ED-Batch, a framework that automates batching by representing policies as Finite State Machines (FSMs) learned via Reinforcement Learning. Combined with a novel PQ tree-based memory planning algorithm, ED-Batch significantly reduces data movement and kernel overhead, achieving throughput speedups of up to 2.45x over state-of-the-art dynamic batching frameworks.

Introduction

In deep learning, batching is the primary mechanism for maximizing hardware utilization. While static networks like CNNs are easily batched, dynamic neural networks present a moving target where the computation graph changes for every input. Current approaches, such as those found in DyNet or TensorFlow Fold, typically employ runtime heuristics to group operations. These heuristics often fail to find the global optimum for batching, resulting in a high number of GPU kernel launches. Furthermore, the requirement for contiguous memory in batched operations necessitates expensive data reorganization at runtime, creating a bottleneck that can negate the benefits of batching.

Methodology

ED-Batch addresses these inefficiencies through two primary innovations: FSM-based batching policies and PQ tree-based memory planning.

FSM-based Dynamic Batching

Rather than relying on fixed rules (e.g., depth-based batching), ED-Batch treats the batching process as a sequential decision-making problem. The framework defines the "state" of the system by the frontier of ready operations in the computation graph. To navigate this state space efficiently, the authors utilize Reinforcement Learning (Q-learning) to train an agent. This agent learns to select operation types that minimize the total number of batches required for a given workload.

The resulting policy is distilled into a Finite State Machine (FSM). This representation offers a "sweet spot" for dynamic execution: it is expressive enough to capture the structural regularities of the network topology while remaining computationally inexpensive to query at runtime. The RL training process essentially converts a complex optimization problem into a constant-time lookup table.

PQ Tree-based Memory Planning

Batching efficiency is fundamentally tied to memory layout. To minimize the overhead of moving data, ED-Batch introduces a memory allocator based on PQ trees—a data structure typically used to solve the "consecutive ones property." This algorithm plans memory layouts during compilation to satisfy two critical constraints:

  1. Adjacency: Ensuring that operands within a batch are stored contiguously.
  2. Alignment: Ensuring the order of operands matches the order of the resulting outputs.

By optimizing for these constraints, ED-Batch can often perform batched operations "in-place," allowing the system to utilize highly optimized vendor libraries like cuDNN without the need for intermediate gather/scatter steps.

Experimental Evaluation

The framework was evaluated across eight diverse dynamic workloads, including:

  • Chain-based: BiLSTM Tagger (WikiNER) and LSTM-NMT (IWSLT 2015).
  • Tree-based: TreeLSTM, TreeGRU (Penn Treebank), MV-RNN, and TreeLSTM-2Type.
  • Lattice-based: LatticeLSTM (Chinese Weibo Dataset) and LatticeGRU.

Key Results

ED-Batch demonstrates substantial performance improvements over existing frameworks like DyNet, Cavs, and the specialized compiler Cortex:

  • Throughput Gains: The framework achieved average speedups of 1.15x for chain-based models, 1.39x for tree-based models, and 2.45x for complex lattice-based models.
  • Batch Optimization: For lattice-based networks, the learned FSM policy reduced the total number of executed batches by up to 3.27x compared to standard heuristics.
  • Memory Efficiency: The PQ tree optimization slashed memory transfer amounts by up to 66x and improved the latency of static subgraphs (such as LSTM cells) by up to 1.6x.
  • Comparison to Specialized Compilers: ED-Batch outperformed Cortex by up to 3.98x, demonstrating that automated FSM-based policies can exceed the performance of manual expert tuning.

Limitations and Future Work

While ED-Batch represents a significant step forward, certain challenges remain. The current FSM state encoding may struggle with rare network structures that require "phase" information to distinguish between identical operation frontiers. Additionally, while memory planning is highly effective for static subgraphs, extending this optimization to fully dynamic components at runtime remains difficult due to strict execution time constraints. Future research will focus on scaling these RL-based approaches to even larger, more heterogeneous graph structures and refining the state representation for broader coverage.

Summary updated

BibTeX
@inproceedings{chen2023ed,
  author = {Siyuan Chen and Pratik Pramod Fegade and Tianqi Chen and Phillip B. Gibbons and Todd C. Mowry},
  title = {{ED-Batch: Efficient Automatic Batching of Dynamic Neural Networks via Learned Finite State Machines}},
  booktitle = {International Conference on Machine Learning},
  year = {2023},
  url = {https://openreview.net/forum?id=0S6qKsGk1KE},
}
Figure