Optimizing Dynamic Neural Networks with Brainstorm

Weihao Cui, Zhenhua Han, Lingji Ouyang, Yichuan Wang, Ningxin Zheng, Lingxiao Ma, Yuqing Yang, Fan Yang, Jilong Xue, Lili Qiu, Lidong Zhou, Quan Chen, Haisheng Tan, Minyi GuoOSDI 2023

TL;DR

This paper introduces Brainstorm, a novel deep learning framework designed to optimize dynamic neural networks (NNs). By introducing "Cell" and "Router" abstractions, Brainstorm enables fine-grained tracing of dynamic execution paths, unlocking new optimization opportunities that traditional static frameworks miss. This leads to significant speedups and reduced memory consumption for dynamic NNs.

What Problem Does This Paper Address?

Modern deep neural networks are becoming increasingly complex, demanding substantial computational resources. Dynamic neural networks offer a solution by adapting their structure or computation based on input, leading to improved efficiency and accuracy. However, existing deep learning frameworks primarily focus on optimizing static NNs with deterministic execution. This leaves a significant gap in optimizing dynamic NNs, as their non-uniform, input-dependent execution paths are difficult to trace and optimize with traditional tensor-centric approaches. The core challenge lies in the mismatch between the tensor-level granularity of current frameworks and the sub-tensor level (e.g., token, patch) where dynamism often occurs in dynamic NNs.

Methods

Brainstorm introduces two key abstractions:

  • Cell: This abstraction allows developers to define the granularity of dynamism within a tensor. For example, a "Cell" could represent a token in a sentence, a patch in an image, or a pixel in a video frame.
  • Router: This component provides a unified interface for developers to express how "Cells" should be dynamically dispatched to different branches of the network. Brainstorm's JIT profiler collects runtime profiles of these routing decisions with minimal overhead.

Leveraging these profiles, Brainstorm implements four dynamic optimization strategies:

  1. Dynamic Horizontal Fusion: Fuses parallel branches into a single GPU kernel, optimizing for frequently encountered Cell loads and reducing kernel launch overhead.
  2. Profile-Guided Model Placement: Analyzes multi-layer correlations to co-locate highly correlated sub-networks on the same GPU, minimizing inter-GPU communication.
  3. Speculative Routing: Predicts routing decisions based on historical profiles and speculatively launches branches, hiding routing overhead. If a prediction is wrong, it falls back to correct execution.
  4. Speculative Weight Preloading: Predicts which branches are likely to be activated and speculatively preloads their weights to hide memory migration latency, crucial for large models on limited GPU memory.

Brainstorm also includes static Cell-level dataflow analysis to understand Cell relationships across static operators, complementing the dynamic analysis.

Datasets

The paper evaluates Brainstorm on six representative dynamic neural networks: SwitchTransformer, TaskMoE, SwinV2-MoE, LiveSR, DynamicRouting, and MSDNet. These models are trained and evaluated on various datasets, including MNLI, ImageNet22k, Iowa-DOT, and Cityscapes, as well as synthetic datasets.

Key Insights and Ideas

  • The "Cell" Abstraction is Crucial: Traditional tensor-level analysis is insufficient for dynamic NNs. Defining dynamism at a finer granularity (Cell) is essential for effective optimization.
  • Runtime Profiling Unlocks New Optimizations: By analyzing the statistical distribution of dynamic execution paths (e.g., how many Cells go to each branch), Brainstorm can apply optimizations that are impossible with static analysis alone.
  • Speculation is Key for Latency Hiding: Speculative routing and weight preloading significantly reduce inference latency by anticipating future execution paths and proactively loading resources.
  • Dynamic NNs Benefit from Multiple Optimizations: The paper demonstrates that combining different optimization strategies (e.g., horizontal fusion and speculative routing) can yield synergistic performance gains.
  • Framework Design Matters: Brainstorm's decoupled approach to routing logic and execution, along with its specialized sparse communication primitives, are critical for its efficiency.

Key Results

Brainstorm achieves significant performance improvements across various dynamic NNs:

  • Speedup: Up to 11.7x speedup ( 3.29x on average) compared to baselines.
  • Memory Reduction: Up to 42% reduction in GPU memory consumption.
  • Specific Model Improvements:
    • SwitchTransformer: 3.63x speedup over PyTorch and 3.33x over Tutel.
  • LiveSR: Up to 8.62x speedup with dynamic horizontal fusion.
  • TaskMoE: Up to 1.34x speedup with optimized placement.
  • MSDNet: Up to 11.7x speedup with speculative routing and dynamic horizontal fusion.
  • DynamicRouting: Up to 1.97x speedup with speculative weight preloading.

Limitations and Future Work

  • Distribution Drift: The performance of Brainstorm's optimizations relies on profiling data being statistically representative. If the runtime distribution drifts significantly from the training profiles, performance gains can diminish or even become negative. The authors suggest continuous monitoring and re-optimization as a future direction.
  • Training Support: While Brainstorm focuses on inference optimization, extending it to support training (e.g., backward propagation) and handling operators that behave differently during training requires further work.
  • Operator Invalidation: Certain operators might invalidate Brainstorm's tracing for dynamic optimization, necessitating manual specification or further research.
  • Robustness to Drifts: The authors acknowledge that enabling dynamic optimizations during cold-start or re-optimization periods is an area for future exploration to ensure robustness against distribution drifts.
  • More Dynamic Optimization Opportunities: The paper suggests that Brainstorm's framework can be extended to exploit dynamism in areas like Neural Architecture Search and dynamic sparsity.

Summary updated

BibTeX
@inproceedings{cui2023optimizing,
  author = {Weihao Cui and Zhenhua Han and Lingji Ouyang and Yichuan Wang and Ningxin Zheng and Lingxiao Ma and Yuqing Yang and Fan Yang and Jilong Xue and Lili Qiu and Lidong Zhou and Quan Chen and Haisheng Tan and Minyi Guo},
  title = {{Optimizing Dynamic Neural Networks with Brainstorm}},
  booktitle = {OSDI},
  year = {2023},
  url = {https://openreview.net/forum?id=3vGuwzyGXS},
}
Figure