MNN: A Universal and Efficient Inference Engine

Xiaotang Jiang, Huan Wang, Yiliu Chen, Ziqi Wu, Lichuan Wang, Bin Zou, Yafeng Yang, Zongyang Cui, Yu Cai, Tianhang Yu, Chengfei Lyu, Zhihua WuMLSys 2020

Abstract

Deploying deep learning models on mobile devices draws more and more attention recently. However, designing an efficient inference engine on devices is under the great challenges of model compatibility, device diversity, and resource limitation. To deal with these challenges, we propose Mobile Neural Network (MNN), a universal and efficient inference engine tailored to mobile applications. In this paper, the contributions of MNN include: (1) presenting a mechanism called pre-inference that manages to conduct runtime optimization; (2) delivering thorough kernel optimization on operators to achieve optimal computation performance; (3) introducing backend abstraction module which enables hybrid scheduling and keeps the engine lightweight. Extensive benchmark experiments demonstrate that MNN performs favorably against other popular lightweight deep learning frameworks.

The Challenge of Mobile Inference

Bringing AI to edge devices like smartphones and IoT hardware offers significant benefits for latency and privacy. However, developers face a "three-headed monster" when building mobile inference engines: model compatibility across various training frameworks, extreme device diversity (fragmented hardware and APIs), and strict resource limitations regarding memory and power.

MNN architecture bridging diverse models and devices.

Existing solutions often force undesirable trade-offs. Heavy frameworks like TensorFlow Lite can bloat application sizes, while manually optimized engines like NCNN scale poorly to new operators. Conversely, model-specific compilers like TVM require re-releasing the entire app for every model update. MNN seeks a "Goldilocks" solution: a universal, lightweight engine that optimizes itself dynamically at runtime.

The MNN Framework Architecture

MNN introduces a "semi-automated search" architecture to bridge the gap between manual tuning and heavy ahead-of-time compilation. The workflow is split into two primary phases: offline conversion and on-device inference.

The MNN workflow: from offline model conversion to optimized on-device inference.

Offline Conversion

Models from frameworks like Caffe, TensorFlow, and PyTorch (via ONNX) are first passed through a Format Converter. The resulting MNN model undergoes offline graph optimization and optional model compression to produce an optimized binary ready for deployment.

On-Device Inference and Pre-inference

The core innovation of MNN is the Pre-inference stage. Because input sizes in mobile ML are usually fixed, MNN performs a "dry run" before actual execution. It evaluates the computational cost of different algorithms (e.g., standard sliding window vs. Winograd convolution) based on the specific hardware detected and dynamically selects the optimal scheme.

Key technical pillars include:

  • Preparation-Execution Decoupling: During pre-inference, MNN calculates the exact memory required for the entire computational graph, allocating a single pool upfront to eliminate runtime overhead. This choice alone has shown to drop inference time by 50–75% on GPUs.
  • Backend Abstraction: A unified Backend class separates math logic from hardware execution, enabling "hybrid scheduling" (e.g., running a convolution on CPU and activation on GPU) while keeping the engine lightweight.
  • Kernel Optimization: MNN utilizes the Strassen algorithm for matrix multiplications and a novel NC4HW4 data layout to maximize CPU SIMD efficiency.

Performance and Real-World Impact

Evaluated against standard mobile vision benchmarks (MobileNet, ResNet, Inception) on diverse hardware (iPhone X, Huawei Mate20, Xiaomi MI6), MNN demonstrated superior performance:

  • Speed: MNN outperforms competitors like TF-Lite and CoreML by 20% to 40% across most devices.
  • Efficiency: Using the Strassen algorithm for large matrix multiplications improved performance by 7.5% to 13.5% compared to direct multiplication.
  • Lightweight Footprint: Integrating MNN adds only 400–600 KB to a mobile application's binary size.

In production, Alibaba's e-commerce platform uses MNN for image-based commodity search, serving over 10 million daily users across 500+ device types with an average inference time of 90.2ms.

Conclusion

MNN provides a robust solution for the fragmented mobile AI landscape. By combining offline optimization with dynamic runtime pre-inference, it achieves high performance without sacrificing universality. Future work includes expanding auto-tuning capabilities and integrating on-the-fly model pruning to further enhance efficiency on constrained devices.

Summary updated

BibTeX
@inproceedings{jiang2020mnn,
  author = {Xiaotang Jiang and Huan Wang and Yiliu Chen and Ziqi Wu and Lichuan Wang and Bin Zou and Yafeng Yang and Zongyang Cui and Yu Cai and Tianhang Yu and Chengfei Lyu and Zhihua Wu},
  title = {{MNN: A Universal and Efficient Inference Engine}},
  booktitle = {MLSys},
  year = {2020},
  doi = {10.48550/arxiv.2002.12418},
}
Figure