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

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

TL;DR

This paper introduces ED-Batch, a novel framework that uses learned Finite State Machines (FSMs) to automatically discover optimal batching policies for dynamic neural networks (DNNs). By combining this with a PQ-tree-based memory planning algorithm, ED-Batch significantly reduces data movement overheads and achieves substantial speedups over existing methods for various dynamic DNN architectures.

What Problem Does This Paper Address?

Deep neural networks (DNNs) often benefit from "batching," where multiple inputs are processed together to improve hardware utilization and reduce overhead. However, this becomes a significant challenge for dynamic DNNs, whose computational graphs change based on the input (e.g., in natural language processing or speech recognition tasks involving variable-length sequences or tree structures). Existing approaches for dynamic DNNs often rely on simple heuristics for batching, leading to suboptimal decisions and increased memory movement costs due to strict memory layout requirements for batched operations. This paper aims to address these limitations by developing an efficient, automated batching solution for dynamic DNNs that optimizes both the batching policy and memory management.

Methods

ED-Batch tackles the problem in two main ways:

  1. Learned Finite State Machines (FSMs) for Dynamic Batching: Instead of relying on hand-crafted heuristics, ED-Batch uses a reinforcement learning (RL) algorithm to train a Finite State Machine (FSM). This FSM learns an optimal batching policy tailored to the specific structure of a dynamic DNN. The FSM's states represent the current frontier of the dataflow graph, and its transitions dictate which operation types should be batched next. This approach allows for more sophisticated and adaptive batching decisions compared to previous methods.
  2. PQ-Tree-Based Memory Planning: To mitigate the memory overhead associated with batching, ED-Batch introduces a novel PQ-tree-based algorithm. This algorithm plans inter-tensor memory layouts after batching to ensure that operands for batched operations are contiguous in memory, as required by vendor libraries. This avoids costly memory gather/scatter operations and allows for the use of highly optimized vendor kernels.

Datasets

The paper evaluates ED-Batch on eight different workloads, which are described as following an increase in dynamism from chains to trees and graphs. These include:

  • BiLSTM-Tagger (chain-based)
  • LSTM-NMT (chain-based)
  • TreeLSTM (tree-based)
  • TreeGRU (tree-based)
  • MVRNN (tree-based)
  • LatticeLSTM (lattice-based)
  • LatticeGRU (lattice-based)
  • TreeLSTM-2Type (a variation of TreeLSTM)

These workloads are noted to have appeared as benchmarks in previous works. No new benchmarks were introduced.

Key Insights and Ideas

  • FSMs as an optimal batching policy representation: The authors found that an FSM can effectively capture complex batching policies for dynamic DNNs, striking a balance between expressiveness and efficiency.
  • Reinforcement learning for policy discovery: Using RL to learn the FSM allows for automatic discovery of specialized batching policies, eliminating the need for manual tuning or heuristic design.
  • Batching-aware memory planning is crucial: Optimizing memory layouts after batching, rather than just before, can significantly reduce data movement overheads, especially for static subgraphs.
  • PQ-trees are well-suited for memory layout optimization: The PQ-tree data structure provides an efficient way to find memory layouts that satisfy adjacency and alignment constraints for batched operations.
  • Dynamic DNN batching is inherently NP-hard: The paper formally proves that the batching problem for dynamic DNNs is NP-hard, underscoring the need for efficient approximation algorithms like the one proposed.

Key Results

ED-Batch demonstrates significant performance improvements across various dynamic DNN architectures:

  • Overall Speedup: On average, ED-Batch achieves 1.15x, 1.39x, and 2.45x speedups for chain-based, tree-based, and lattice-based DNNs, respectively, compared to state-of-the-art frameworks like DyNet and Cavs (referred to as Cavs-DyNet).
  • Reduced Batch Count: For tree-based models, ED-Batch reduces the number of batches by up to 37% compared to heuristic-based methods.
  • Memory Optimization: The PQ-tree-based memory layout optimization reduces static subgraph latency by up to 1.6x, memory kernels by up to 4x, and memory transfer amount by up to 66x.
  • Comparison with Cortex: ED-Batch outperforms Cortex, a highly specialized framework, by up to 3.98x on TreeLSTM and TreeGRU models.
  • Training Efficiency: The RL training for the FSM policy is relatively fast, taking tens of milliseconds to optimize static subgraphs and up to 22 seconds to learn the batching policy for tested workloads.

Limitations and Future Work

  • FSM Coverage: The paper acknowledges that there are cases where the learned FSM might not cover all scenarios, particularly when there are complex dependencies or swapped operation types within subgraphs. Future work could involve incorporating phase information or other contextual cues into the FSM's state encoding to address this.
  • Specialized Frameworks: While ED-Batch offers a general and automated solution, highly specialized frameworks like Cortex can achieve even greater performance gains for specific network classes by leveraging manual, low-level optimizations. The authors suggest that ED-Batch's approach is more accessible for general users.
  • Future Work: The authors mention that the algorithm itself and the idea of better memory planning are applicable to any batching problem, suggesting potential extensions to other domains. The framework is implemented as a runtime extension to DyNet, indicating a path for integration with other deep learning frameworks.

Summary updated

BibTeX
@inproceedings{chen2023ed,
  author = {Siyuan Chen and Pratik 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