1.1.0

alibaba/MNN1.1.0Nov 5, 2020by hush-alibaba

AI Summary

MNN 1.1.0 is a major release featuring geometric computing - a framework refactoring that separates shape/geometric computation from hardware backends, enabling ~20% GPU performance gains. The release adds TensorRT and CUDA backends, introduces weight-only quantization (4x model size reduction at 8-bit), and adds support for Transformer-based ASR models with Control Flow, Dynamic Shape, and Zero Shape features. Performance optimizations across ARM (10-20% improvement), OpenCL (20-100% improvement), and x86 (on par with OpenVINO) backends are also included.

Key Highlights

  • Geometric Computing framework refactoring separates backend-agnostic computation, enabling ~20% GPU performance gains
  • Added TensorRT and CUDA backends supporting common CV and RNN models
  • Weight-only quantization reduces model size by 4x at 8-bit with near-lossless accuracy
  • x86 floating-point performance now matches OpenVINO benchmark
  • Added Interpreter::setCacheFile API for caching GPU compiled models

New Features

  • Geometric Computing - major framework refactoring for heterogeneous backend support
  • TensorRT backend for GPU inference
  • CUDA backend for GPU inference
  • ASR model support with Transformer architecture (Control Flow, Dynamic Shape, Zero Shape)
  • Weight-only quantization (MNNConvert --weightQuantBits) for 2-8 bit compression
  • Dynamic Shape support with subgraph partitioning and nested Module architecture
  • Zero Shape tensor support for while-loop initialization
  • OpenCL AutoTuning optimization (20-100% performance gain)
  • ARM CPU performance improvements (10-20% gain)
  • x86 backend optimization on par with OpenVINO
  • Interpreter::setCacheFile API for GPU model caching
  • SpaceToBatchND/BatchToSpaceND support for unknown input shapes
  • DepthToSpace and SpaceToDepth with pixelshuffle support
  • 1x1 convolution performance fix for large batch/small spatial dimensions
  • Onnx ConvTranspose output padding fix
  • Onnx Resize fix for variable input counts

Full Release Notes

## 一、框架通用性
### 1.1 几何计算
几何计算是本次发布中大规模的框架重构。它将大部分算子的计算过程中与硬件后端无关部分(形状计算和几何计算)剥离出来,极大地降低了异构后端算子实现的成本。基于几何计算,MNN重写了目前所有的硬件后端。由于引入几何计算之后GPU后端算子的覆盖率的增加,在阿里巴巴内部的业务模型中,MNN GPU后端性能普遍获得约20%提升。

### 1.2 新增后端
基于几何计算机制,MNN新增了TensorRT和CUDA后端。目前已经支持常用CV模型与RNN模型。

### 1.3 ASR模型支持
除了业务应用广泛的CV模型,MNN在这次发布中添加了对基于Transformer结构的ASR模型的支持。这类模型结构要求推理引擎支持Control Flow、Dynamic Shape和Zero Shape等特性。MNN在框架层面对这些特性进行了支持和完善:
• 重构Control Flow支持方案,提供用户透明的functional control flow实现,并支持TF1.x的控制流模型转换。
• 添加Dynamic Shape的支持,MNN将整图按照动态形状算子划分为多个分段子图。在代码层面,一个子图对应一个Module,Module支持嵌套,即整图被表达为一个由Module组成的调用树,树的叶子节点可以使用Session来执行,Session每次执行前Resize,重新进行形状推理和预分配内存。
• Zero Shape指的是模型中某些Tensor的shape存在0值,比如 (1, 0, 256),这种情况大多是为了给while-loop中某些循环变量提供初始值而引入的。MNN在对形状推理和执行逻辑上对Zero Shape进行了支持。

## 二、模型压缩
新添模型压缩的[仅权值量化](https://www.yuque.com/mnn/cn/model_convert)(MNNConvert --weightQuantBits)。此功能仅对conv/matmul/LSTM的float32权值进行量化,仅优化模型大小,加载模型后会解码为float32,量化位宽可选2~8,运行速度和float32模型一致。经内部测试8bit时精度基本无损,模型大小减小4倍。

## 三、性能优化
### ARM后端
在今年5月,MNN在ARM CPU上的性能已立于业界前列(https://zhuanlan.zhihu.com/p/151666822)。在此之后,MNN持续投入ARM CPU性能优化,在各模型和芯片上又获得了10%~20%的性能提升。性能提升之路永无止境。
![1](https://user-images.githubusercontent.com/56632408/98210106-04637f00-1f7b-11eb-9df5-8c31ecd11035.png)
![2](https://user-images.githubusercontent.com/56632408/98210183-1cd39980-1f7b-11eb-8f09-cd29cd62ebef.png)

### OpenCL后端
开启AutoTuning等一系列优化后,MNN在1.0.0的基础上,普遍有20%~100%的性能提升。具体性能数据如下:
![3](https://user-images.githubusercontent.com/56632408/98210252-3674e100-1f7b-11eb-8a57-196907dec310.png)
![4](https://user-images.githubusercontent.com/56632408/98210273-3ecd1c00-1f7b-11eb-81b2-373dc6b1fc90.png)

### x86后端
5月以来,MNN团队持续投入x86后端的优化,目前浮点单线程性能与行业标杆OpenVINO基本持平,部分情况 (Squeezenet v1.0) 超越。
![5](https://user-images.githubusercontent.com/56632408/98210339-60c69e80-1f7b-11eb-9c77-5dffefd688fc.png)

## 四、框架易用性
Interpreter::setCacheFile API
由于OpenCL新增的AutoTuning机制、TensorRT后端初次推理的耗时较高,MNN在Interpreter上增加setCacheFile API,用于缓存GPU后端的编译优化之后的模型。用法如下:
```
    std::shared_ptr<MNN::Interpreter> net =
        std::shared_ptr<MNN::Interpreter>(MNN::Interpreter::createFromFile(fileName));
    if (nullptr == net) {
        return 0;
    }
    // Must call it before createSession.
    // If ".tempcache" file does not exist, Interpreter will go through the 
    // regular initialization procedure, after which the compiled model files 
    // will be written  to ".tempcache".
    // If ".tempcache" file exists, the Interpreter will be created from the
    // cache.
    net->setCacheFile(".tempcache");
    
    MNN::ScheduleConfig config;
    // Creates the session after you've called setCacheFile.
    MNN::Session* session = net->createSession(config);
```

## 五、Bugfixes
修正但不限于如下 Bug:
1. SpaceToBatchND , BatchToSpaceND 支持 block size / padding 作为输入(支持在输入shape 未知情况下的 Tensorflow 空洞卷积)
2. 修正 depthToSpace 和 spaceToDepth ,支持 pixelshuffle
3. 修正 1x1 卷积对于 batch 较大,width / height 较小时,性能不好的问题
4. 修正 Onnx 的 ConvTranspose output padding 支持问题
5. 修正 Onnx 的 Resize 在某些输入个数下不支持的问题

## 六、One More Thing
MNN有自己的官网啦![官网](https://www.mnn.zone/)上还可以下载MNN团队全新力作「MNN工作台」,涵盖开箱即用模型、可视化训练等工具,更可以一键部署到多端设备。
![6](https://user-images.githubusercontent.com/56632408/98215708-121d0280-1f83-11eb-89e8-c2a27d2516fd.png)