Just-in-Time Dynamic-Batching

Sheng Zha, Ziheng Jiang, Haibin Lin, Zhi ZhangarXiv 2019

Abstract

Modern deep learning models often utilize dynamic computation graphs to process structured data like trees and graphs. However, these irregular structures hinder efficient batching, a cornerstone of hardware acceleration. This paper presents a Just-in-Time (JIT) dynamic batching system for the MXNet Gluon framework. By automatically identifying and grouping isomorphic subgraphs at an optimal level of granularity, the proposed method achieves significant speedups—up to 6.25x—on complex NLP tasks without requiring manual data preprocessing.

Introduction

In traditional deep learning, "static" computation graphs are the norm. In a standard image classifier, every input passes through the exact same sequence of layers, making it straightforward to batch multiple samples together for efficient processing on GPUs and CPUs. However, many modern AI challenges, particularly in Natural Language Processing (NLP), involve "dynamic" structures where the computation graph changes for every sample. For instance, a sentence might be represented as a unique parse tree.

Historically, researchers dealing with dynamic models faced a difficult choice: process samples one by one, which is slow and fails to utilize hardware parallelism, or manually "pad" and "bucket" data, which is complex and often inefficient. This paper introduces a system to automatically and efficiently batch these irregular computation graphs on the fly.

Methodology: JIT Dynamic Batching

The authors propose a JIT dynamic batching system integrated into the MXNet Gluon framework. Their approach relies on three core innovations designed to balance analysis overhead with execution speed.

Granularity Selection

A critical challenge in automatic batching is determining the right level of abstraction. Analyzing the graph at the individual operator level (e.g., every single addition) is too slow, while batching at the whole-graph level misses many opportunities. The authors leverage MXNet’s HybridBlock to find a "sweet spot" at the subgraph level.

Figure 1: Batching granularity across different levels of the computation graph, from fine-grained kernels to coarse-grained graphs.

Lazy Execution via NDArrayFuture

Standard imperative programming executes operations immediately. To enable batching, the authors introduced a "lazy" interface using NDArrayFuture. When a function is called, it returns a "future" and records the operation's intent rather than running it. This allows the system to look ahead, accumulate a pool of pending operations, and identify batching opportunities before execution.

Isomorphism Matching

The system organizes computation nodes into a lookup table based on their depth in the graph. It then identifies "isomorphic" nodes—those performing the exact same operation with identical settings—and stacks them into a single batched operation. By performing this as part of a JIT compilation process, the framework can cache rewritten graphs, accelerating subsequent passes.

Experimental Evaluation

The researchers evaluated their method using the SICK (Sentences Involving Compositional Knowledge) dataset for semantic relatedness tasks. They employed a Tree-LSTM model, which processes sentences based on their grammatical parse trees. This model serves as an ideal stress test because the trees vary significantly, with nodes having anywhere from 0 to 9 children, making manual batching extremely difficult.

Key Results

Benchmarks were conducted on Amazon EC2 c4.8xlarge instances powered by Intel CPUs. The JIT dynamic-batching method demonstrated substantial performance gains over the standard per-instance processing approach.

Table 1: Performance comparison between per-instance processing and JIT dynamic-batching showing significant throughput improvements.

As shown in the results, the system achieved a 5.96x speedup in training and a 6.25x speedup in inference. These gains effectively close the performance gap between flexible dynamic models and rigid static models, allowing researchers to use complex structures without a massive computational penalty.

Conclusion and Future Work

JIT dynamic batching provides a user-friendly way to optimize dynamic models, requiring as little as one line of code (e.g., wrapping a loop in a with mx.batching(): scope). While the current work focuses on CPU performance and subgraph-level batching, future iterations could explore combining this technique with operator fusion—merging multiple math operations into a single kernel—to unlock even greater performance. Additionally, developing more sophisticated heuristics for "waiting" for batches during real-time inference remains an open area for optimization.

Summary updated

BibTeX
@misc{zha2019just,
  author = {Sheng Zha and Ziheng Jiang and Haibin Lin and Zhi Zhang},
  title = {{Just-in-Time Dynamic-Batching}},
  year = {2019},
  eprint = {1904.07421},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/1904.07421},
}
Figure