SoD2: Statically Optimizing Dynamic Deep Neural Network

Wei Niu, Gagan Agrawal, Bin Ren · DOI

Introduction

Deep learning compilers like XLA and TVM have traditionally excelled at optimizing static models where tensor shapes are fixed. However, modern AI increasingly relies on dynamic models—such as Transformers (BERT), adaptive vision models (YOLO-V6), and conditional networks—where shapes and execution paths change based on input. Current frameworks often struggle with this dynamism, resorting to slow runtime re-initialization, conservative memory allocation, or inefficient brute-force execution of all possible branches.

SoD² is a novel optimization framework designed to bring static compilation efficiency to these dynamic environments. By utilizing a specialized data-flow analysis called Rank and Dimension Propagation (RDP), SoD² enables sophisticated optimizations like operator fusion and memory planning even when exact dimensions are unknown until runtime.

Methods: Rank and Dimension Propagation (RDP)

The core of SoD² is the RDP framework, which operates through three primary stages to reason about symbolic shapes.

Operator Classification

The first step involves categorizing operators based on how their output shapes relate to their inputs. The authors analyzed over 150 ONNX operators, grouping them into four categories: Input Shape Determined Output (ISDO), Input Shape Determined Output Shape (ISDOS), Input Shape & Value Determined Output Shape (ISVDOS), and Execution-Determined.

Classification of operators based on shape and value dependencies.

RDP Data-Flow Analysis

RDP performs a static analysis that propagates symbolic information both forward and backward through the computational graph. It uses a lattice of values ranging from known constants to symbolic variables (e.g., "Dimension N") and "statically unknown" markers. This allows the compiler to determine if two tensors share the same shape symbolically, even if the numeric value is unknown.

Examples of forward and backward inference within the RDP framework.

As shown in the figure above, backward propagation is particularly powerful. For instance, by analyzing an Add operator (which follows broadcasting rules), the compiler can work backward to infer constraints on input tensors, filling gaps that a simple forward pass would miss.

RDP-Enabled Optimizations

With symbolic information established, SoD² performs several key optimizations:

  • Operator Fusion: Merging operations into a single kernel to reduce memory traffic by avoiding the materialization of intermediate results.
  • Static Execution Planning: Determining an optimal execution order to minimize peak memory usage.
  • Multi-Version Code Generation: Creating specialized kernel versions for different shape "types" (e.g., skinny vs. fat matrices) and selecting the best one at runtime.

Evaluation and Results

SoD² was evaluated using 10 dynamic DNN models, including StableDiffusion, YOLO-V6, and various conditional networks like SkipNet. Testing was conducted on mobile platforms (Snapdragon 888 and 835) using standard benchmarks like ImageNet and MS COCO.

  • Latency: SoD² outperformed production frameworks like ONNX Runtime (ORT), MNN, and TVM-Nimble by an average of 2.5× to 2.7× on CPUs and up to 3.9× on GPUs.
  • Memory Efficiency: The framework reduced peak memory consumption by 27% to 88% compared to competitors.
  • Fusion Gains: RDP-based fusion reduced the number of operators by an additional 16% to 46% beyond standard static fusion techniques.
  • Execution Stability: Unlike frameworks that suffer from high latency variance due to runtime re-initialization, SoD² provided consistent performance across varying input sizes.

Conclusion and Future Work

SoD² demonstrates that symbolic equality is often sufficient for high-level compiler optimizations in dynamic networks. While currently tied to the ONNX format, the underlying RDP logic is generalizable. Future work aims to extend this framework to native PyTorch/TensorFlow graphs, address the complexities of recursive models like Graph Neural Networks, and scale these techniques to the massive requirements of Large Language Models (LLMs).

Summary updated

BibTeX
@inproceedings{niu2024sod,
  author = {Wei Niu and Gagan Agrawal and Bin Ren},
  title = {{SoD2: Statically Optimizing Dynamic Deep Neural Network}},
  booktitle = {ASPLOS},
  year = {2024},
  doi = {10.1145/3617232.3624869},
}
Figure