On-the-fly Operation Batching in Dynamic Computation Graphs

Graham Neubig, Yoav Goldberg, Chris Dyer

Abstract

Dynamic neural network frameworks offer flexibility for handling variable-structured data but often require manual, error-prone batching logic to achieve high performance. This paper introduces an automatic batching algorithm that separates graph construction from execution. By employing node signatures and an agenda-based scheduling heuristic, the framework automatically groups operations into efficient batches on the fly. Experimental results demonstrate speedups of up to 11x on standard architectures and significant performance gains on complex structures like tree-LSTMs and transition-based parsers, often rivaling or exceeding specialized manual implementations.

Introduction

Modern deep learning hardware, particularly GPUs, relies on parallelism to achieve high throughput. Traditionally, this is achieved through "batching," where multiple data inputs are grouped to perform large-scale matrix operations. In static frameworks, this process is rigid. In dynamic frameworks like PyTorch or DyNet, while handling variable-sized data is more intuitive, the burden of batching logic—including complex padding, masking, and tensor packing—still falls on the developer.

For complex architectures such as recursive neural networks or transition-based parsers, manual batching is notoriously difficult. Consequently, many researchers resort to training on single instances, failing to utilize the full potential of their hardware. This paper addresses this gap by proposing a system that automates the batching process without requiring the user to modify their conceptual, instance-level code.

Methodology: On-the-fly Batching

The proposed system decouples the definition of the computation graph from its execution through three primary mechanisms:

  1. Lazy Evaluation: Operations are not executed immediately. Instead, they are recorded into a symbolic representation known as a Wengert list.
  2. Node Signatures: Every operation is assigned a signature based on its type, dimensions, and shared parameters. Nodes with identical signatures are candidates for batching.
  3. Agenda-based Scheduling: Since finding the optimal batching strategy is NP-hard, the authors use a heuristic. An "agenda" tracks nodes whose dependencies are satisfied. The scheduler groups nodes with the same signature, prioritizing them based on their average depth in the graph to maximize batching opportunities before final operations like loss calculation.
Comparison between individual dynamic computation graphs (left) and their batched representation with padding and masking (right).

This approach allows the framework to identify shared parameter optimizations automatically, such as converting multiple matrix-vector products into a single matrix-matrix product, without explicit user definition of a batch dimension.

Experimental Evaluation

The algorithm was evaluated across several tasks of increasing structural complexity:

  • BiLSTM Sequence Labeling: Tested on both synthetic fixed-length data and the WikiNER English corpus (variable-length).
  • Tree-structured LSTMs: Evaluated on the Stanford Sentiment Treebank (SST), where sentence-specific tree structures make manual batching difficult.
  • Transition-based Dependency Parsing: Tested on the English Universal Dependencies Treebank, a task where future actions depend on previous model predictions, making traditional batching nearly impossible.

Results and Discussion

The automatic batching system achieved massive performance gains. On standard BiLSTMs, it was 11x faster than single-instance training on both CPU and GPU. Even compared to hand-optimized manual batching, the automatic approach maintained 70-80% of the throughput.

Performance breakdown on CPU and GPU comparing different batching strategies and manual optimization.

Notably, on TreeLSTMs, the system outperformed specialized tools like TensorFlow Fold. For transition-based parsing, it provided a 2.7x to 3.6x speedup, enabling efficient training for models that were previously computationally prohibitive to batch.

The "By Agenda" scheduling strategy proved superior to simpler depth-based approaches, particularly when handling batches with highly varied computational depths (e.g., sentences of significantly different lengths).

Conclusion and Future Work

On-the-fly operation batching bridges the gap between conceptual code and hardware efficiency. While the system introduces some overhead for graph analysis and memory management (e.g., copying data into contiguous blocks), the net gains in developer productivity and execution speed are substantial. Future work will focus on further reducing scheduling latency and optimizing memory movement to bring automatic batching even closer to the performance of hand-tuned static graphs.

Summary updated

BibTeX
@inproceedings{neubig2017fly,
  author = {Graham Neubig and Yoav Goldberg and Chris Dyer},
  title = {{On-the-fly Operation Batching in Dynamic Computation Graphs}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2017},
  url = {https://openreview.net/forum?id=SkWujPb_-H},
}
Figure