DyCL: Dynamic Neural Network Compilation Via Program Rewriting and Graph Optimization

Simin Chen, Shiyi Wei, Cong Liu, Wei Yang

TL;DR

This paper introduces DyCL, a novel approach to compile dynamic neural networks (DyNNs) that traditional deep learning compilers struggle with. DyCL rewrites DyNNs to separate their dynamic control flow from static computation, allowing existing compilers to efficiently compile the computation parts and a generated host program to manage the dynamic logic, achieving correct compilation and significant speedups.

What Problem Does This Paper Address?

Deep learning (DL) compilers are crucial for deploying neural networks on various hardware. They translate high-level DL models (like those in PyTorch or TensorFlow) into optimized, portable executables. However, a major challenge arises with dynamic neural networks (DyNNs). These networks have execution paths that change based on the input data, unlike traditional "static" networks with fixed computational graphs. Existing DL compilers typically rely on a "tracing" mechanism, where they feed a sample input to the network and record the execution path to build a static computational graph. This approach fails for DyNNs because the traced path might not represent all possible execution paths, leading to incorrect compilation and performance issues. The paper aims to bridge this gap by enabling existing DL compilers to correctly and efficiently compile DyNNs.

Methods

DyCL tackles the DyNN compilation problem through a multi-step process:

  1. Program Rewriting: DyCL first analyzes the DyNN's source code. It performs loop unrolling and constant propagation to eliminate input-dependent loops and make variables constant where possible. This step aims to simplify the code and make sub-parts more amenable to static analysis.
  2. Heterogeneous Control Flow Graph (HCFG) Construction: The rewritten program is then transformed into an HCFG. This graph explicitly represents the dynamic behavior of the DyNN by separating conditional logic nodes from computation nodes. Each computation node effectively becomes a "sub-DNN."
  3. Sub-DNN Optimization: For each identified sub-DNN, DyCL applies graph optimization strategies. These strategies aim to reduce unnecessary data transfers between the host program and the accelerator by moving computation-free operations (like identity tensor copies) to the host.
  4. Sub-DNN Compilation: Each sub-DNN, now devoid of dynamic control flow, is compiled independently using existing DL compilers (like TVM or ONNX Runtime). DyCL automatically determines the necessary input shapes for each sub-DNN by traversing the HCFG.
  5. Host API Generation: Finally, DyCL synthesizes a host program. This host program encapsulates the dynamic control flow logic (the conditional statements) and orchestrates the execution of the compiled sub-DNNs. The rewritten DyNN's abstract syntax tree (AST) is modified to replace original tensor computation statements with calls to the compiled sub-DNNs.

Datasets

The paper does not mention specific datasets used for training the DyNN models. However, for evaluation, they used nine publicly available DyNN models (including Shallow-Deep, SkipNet, AttentionNet, En-Decoder, MobileNet, VGG19, ResNet50, WideResNet, ResNet38+RNN, ResNet38+Dense, ResNetX+LSTM, GoogLeNet+LSTM, and FlatResNet32) and generated random inputs for testing.

Key Insights and Ideas

  • Separation of Concerns: The core insight is that the dynamism in DyNNs primarily stems from conditional statements. By separating these control flow elements from the static tensor computations, DyCL can leverage existing DL compilers for the latter.
  • Host Program as Dynamic Orchestrator: The host program, often written in high-level languages with robust compilers, is a natural place to manage the dynamic execution logic of DyNNs.
  • HCFG for Explicit Dynamism: The Heterogeneous Control Flow Graph (HCFG) is a novel representation that effectively models the mixed control and data flow of DyNNs, enabling systematic decomposition into sub-DNNs.
  • Contextual Compilation of Sub-DNNs: DyCL's ability to automatically determine the correct input shapes and contexts for compiling each sub-DNN is crucial for accurate compilation.

Key Results

  • 100% Compilation Success Rate: DyCL successfully compiled all tested dynamic neural networks, achieving semantic equivalence with the original models.
  • Significant Performance Improvement: Compiled DyNNs using DyCL achieved substantial speedups, ranging from 1.12x to 20.21x faster than the original DyNNs running on general-purpose DL frameworks.
  • Minimal Numeric Error: The maximum numeric error between DyCL-compiled DyNNs and original DyNNs was very small (within 10⁻⁴.⁷², well within acceptable limits), confirming correctness.
  • Graph Optimization Benefits: The proposed graph optimization module in DyCL provided an average acceleration of 4% to 9% compared to compilation without it.
  • Low Compilation Overhead: DyCL's compilation process itself introduced a low overhead, accounting for only 3.23% to 9.90% of the total compilation time when compared to the time taken by existing DL compilers to compile the sub-DNNs.

Limitations and Future Work

  • Input-Dependent Loops: The current DyCL approach focuses on DyNNs that do not contain input-dependent loops to avoid potential infinite loop scenarios. Handling such loops would be a future direction.
  • External Validity: The study used a specific set of popular DyNN models. While efforts were made to ensure diversity, the conclusions might be further strengthened by evaluating on a broader range of DyNN architectures and applications.
  • Noisy Latency Measurements: The authors acknowledge the potential for noisy latency measurements but state that multiple measurements and variance analysis minimized this impact.

Future work could involve extending DyCL to handle input-dependent loops and exploring its application to an even wider array of dynamic neural network architectures.

Summary updated

BibTeX
@inproceedings{chen2023dycl,
  author = {Simin Chen and Shiyi Wei and Cong Liu and Wei Yang},
  title = {{DyCL: Dynamic Neural Network Compilation Via Program Rewriting and Graph Optimization}},
  booktitle = {ISSTA},
  year = {2023},
  url = {https://openreview.net/forum?id=sK6cL9ISYO},
}

Loading related content...

Figure