Paddle Lite characteristic full interpretation, and more hardware support, lightweight deployment

AI fast track Paddle Lite special presentation material paper consists of 21 September in the Baidu Technology Park held from finishing, we introduced the Paddle Lite performance characteristics, usage, architecture and design, and provides a complete use case, for developers to rapidly launch applications.

In the wave of technological innovation, intelligent hardware combined with artificial intelligence more widely to enter our lives. Small to smart phones, watches, large intelligent transportation systems, industrial automatic detection platform, all permeated with the power of artificial intelligence. In addition, the artificial intelligence to learn the depth of customized hardware in recent years has the potential blowout. Artificial intelligence has been more and more applications in a variety of service platform, a variety of hardware. Application here generally refers to the depth of reasoning learning model calculation in the actual scene. Although the rapid development of hardware brings performance improvements, but a variety of hardware platforms to the application development challenges. At the same time, the depth of learning tasks, considered the practical application of force and memory limitations still very harsh.

Hardware for multi-platform multi-scene, flying paddle (PaddlePaddle) in August this year to upgrade released Paddle Lite inference engine. It supports a variety of hardware, a variety of platforms, also has important features lightweight deployment, high-performance realization.

Baidu work in depth learning inference engine field for many years, beginning in 2017, the company had more research since the inference engine and have their own strengths, such as some flagship mobile terminal deployment, while others focus on supporting multiple hardware platforms and more. 2019, with the overall ecological construction of fly paddle, we will accumulate the inference engine technology was integrated Paddle Mobile brand will be upgraded to Paddle Lite, designed to support a wider range of hardware platforms, the architecture and performance of public companies, Part One long, and it incorporates the latest thinking on hardware and platform support. August we released the Beta version of Lite Paddle, and on September 22 released the RC version. Paddle Lite is an open source project, code maintenance in all of its projects on Github:

Warehouse Address: https: //github.com/PaddlePaddle/Paddle-Lite
latest documentation address: https: //paddlepaddle.github.io/Paddle-Lite

1. Features & capabilities

Paddle Lite supports the following features: ① support multiple hardware platforms; ② lightweight deployment; ③ high-performance implementation; ④ quantify computing support. Here we introduced one by one.

1.1 supports multiple hardware platforms

Currently supports the following seven kinds of hardware:
ARM the CPU V7, V8
Mali GPU, Adreno GPU, GPU Metal
Huawei NPU
FPGA

Here Insert Picture Description
Paddle Lite supports two levels of top-down multi-platform support, a variety of training is the upper platform support, in addition to the native model flying training paddle out, you can also X2Paddle tool Caffe, TensorFlow, ONNX and other third-party platform PaddlePaddle model into a model format, which is loaded into the Paddle Lite deployment. X2Paddle is very simple to use, with reference to the above command to complete conversion of the corresponding models. Paddle Lite official test validated the model list below.

Here Insert Picture DescriptionIn addition to a variety of training platforms this level, but at the bottom, Paddle Lite also supports the prediction deployment server and mobile end both platforms.

1.2. Lightweight deployment

Perfect support multiple hardware platforms must have at least the following support, such as a full back-end support for all kinds of hardware to achieve multiple hardware-related operators to calculate detailed analysis and optimization capabilities map. These capabilities support will inevitably bring about the eventual deployment of volume expansion of the library, it is difficult lightweight deployment.

Paddle Lite To address this issue, in the framework to do the appropriate design:

Here Insert Picture DescriptionIn the specific framework of the implementation of the order by the model loads, analysis and implementation of strict split into two phases. Analysis phase has the ability to complete optimization analysis computation graph (such as operator fusion, memory optimization, etc.), as more strategies, larger; execution stage includes only the operator, simple function, but the volume is very small.

在体积敏感的场景下,Paddle Lite 可以选择轻量化部署方案,即可以只部署执行阶段,而复杂的分析优化则封装到了离线工具中。当然,在体积不敏感的场景下,比如服务器端推理,Paddle Lite 也支持两个阶段合并在一起部署,以支持一些预测前需要针对具体硬件和上下文信息调整动态图优化的能力。

Here Insert Picture DescriptionPaddle Lite 核心库(ARM V8)是 1.4M,在主流实现中较有优势。

1.3. 高性能实现

Here Insert Picture DescriptionPaddle Lite 的实现也是比较有优势的,比如在端侧推理非常主流的硬件 ARM CPU 上,MobileNet V1 的性能如上,也是比较有优势的。

1.4. 量化计算支持

量化计算是神经网络计算的趋势。神经网络有信息冗余的特点,即使使用低精度计算也能保证效果,基于这个优点,各硬件平台都通过量化计算来提供更快,功耗体积更有优势的硬件。Paddle Lite 在这方面也有很好的支持。

Here Insert Picture Description基于 PaddleSlim 的量化训练出的模型,结合 Paddle Lite 的 Int8 量化预测能力,可以得到很有竞争力的 Int8 量化性能:

PaddleSlim 项目地址:
https://github.com/PaddlePaddle/FluidDoc/tree/develop/doc/fluid/advanced_usage/paddle_slim

Here Insert Picture Description
Paddle Lite 在架构的设计中,重点考虑了相对广泛的多硬件平台支持的能力。首先在 Kernel 层,一种算法可以为多个硬件或者实现方法注册多个实现。

Here Insert Picture Description比如上图,Operator1 代表的算法有三种硬件,6 种实现。在具体的硬件平台上,可能会同时存在多种硬件可用,比如在安卓手机上,除了 ARM CPU 之外,可能还有 Mali GPU 可用,这时就需要混合调度多种硬件的 Kernel,中间涉及的跨设备拷贝,不同的数据排布等自动转化会比较复杂。为了较完备地支持这些场景,Paddle Lite 在架构中集成了 TypeSystem 的设计思路,将架构上支持任意硬件、任意量化精度、任意数据排布的 Kernel 实现的混合调度转化为类型推导的过程。

具体地,每个 Kernel 在注册时,会同时注册其自身,以及输入输出的类型信息,比如一个矩阵乘的 Kernel 会有如下注册逻辑。

Here Insert Picture Description
其输入是一个 ARM 上的 Tensor type。

一个复杂的多种不同 Kernel 的调度转为计算图中类型推导和对应做类型转换(type cast)的过程,比如下图,两个 Kernel 有不同类型的形参 a, b(红色和蓝色各代表一种类型)。

Here Insert Picture Description在第一步类型推导后,我们发现类型不匹配的点(红色叉处)。

Paddle Lite 的图分析优化模块(MIR)会自动插入相应种类的 typecast kernel 完成类型的转化,从而完成不同计算的混合调度。

除了调度之外,为了支持相对广泛的硬件,Paddle Lite 需要对硬件进行合理的抽象,抽象不能过浅,否则框架会与具体的硬件耦合,影响更多硬件的扩展;抽象也不能过高,否则不同硬件的必要支持能力会被削弱。

Paddle Lite 在 Kernel 层和 MIR(图分析优化模块)均引入了适度的硬件支持,以支持硬件的针对优化,但通过通用的高层 API 来避免框架与具体硬件耦合。要扩展新的硬件,只需要扩展 Kernel 层(编写对应的 Kernel)和 MIR 层(编写对应的 IR Pass)便可轻松实现。

3. 应用案例

3.1. 使用方法

Paddle Lite 的使用有如下几个步骤:

(1) 获取 PaddlePaddle 模型

用 PaddlePaddle 训练
可选通过 PaddleSlim 做模型压缩
或用 X2Paddle 转换 TF 等第三方模型

(2) model_optimize_tool 优化模型

(3) 编写预测代码,集成到服务中

C++ 接口
Java 接口

获取模型的方法以下几种:

参考 PaddlePaddle 文档训练新的模型
直接使用 Paddle/models 标准模型库中提供的模型
X2Paddle 模型转换工具转化第三方模型
PaddleSlim 模型压缩工具集二次压缩 PaddlePaddle 模型

之后是使用模型压缩工具优化模型,使用方法可以参考如下命令,完整教程可以参考文档。

地址:
https://paddlepaddle.github.io/Paddle-Lite/develop/model_optimize_tool/

使用代码大体如下:

Here Insert Picture Description

3.2. 实验一:手机端 Demo

Paddle Lite 官方提供了 Android 和 iOS 的演示性 Demo,可以帮助用户快速的集成和验证模型的性能和效果。

Paddle-Lite-Demo 地址:https://github.com/PaddlePaddle/Paddle-Lite-Demo

3.2.1. 试验设备

iOS 用户需要 MacOS 系统,安装 xcode10.1(或以上版本),并登录 iCloud 账户
Android 用户可以使用 MacOS,Ubuntu 或 Centos 系统,安装 Android Studio3.4
iPhone 手机需要登录 iCloud 账户
Android 手机需要打开手机的「调试模式」并开启「USB 安装」
配置 MacOS 或 Ubuntu 系统的开发环境。具体请参考:https://github.io/Paddle-Lite/develop/source_compile/

3.2.2. 预测库编译

手机端 Demo 采用的是无第三方依赖的「tiny_publish」模式。

具体编译参数可以参考:
https://paddlepaddle.github.io/Paddle-Lite/develop/source_compile/#%E4%BA%8C%E7%BC%96%E8%AF%91paddlelite

下载代码库:

git clone https://github.com/PaddlePaddle/Paddle-Lite.git 
cd Paddle-Lite 
git checkout v2.0.0-rc
  • Android tiny_publish 编译(gcc,armv8):

./lite/tools/build.sh --arm_os=android --arm_abi=armv8 \
--arm_lang=gcc --android_stl=c++_static tiny_publish
  • iOS tiny_publish 编译(armv8, ios64):
./lite/tools/build.sh --arm_os=ios64 --arm_abi=armv8 ios

3.2.3. 模型转换

为了满足手机端轻量化部署的需求,我们会在离线阶段进行模型结构的分析和优化,并将优化后的模型序列化到 Naive Buffer 的格式,从而减少部署包的体积,并实现无第三方依赖。因此在手机端上部署之前我们需要通过离线优化工具「model_optimize_tool」进行模型的转换。

Here Insert Picture Description

  • 源码编译:

./lite/tools/build.sh build_optimize_tool

编译完成后,离线优化工具位于
「build.model_optimize_tool/lite/api/model_optimize_tool」

下载编译好的执行文件:

地址:https://github.com/PaddlePaddle/Paddle-Lite/releases/download/v2.0.0-rc/model_optimize_tool.gz

模型转换
详情请参考:https://paddlepaddle.github.io/Paddle-Lite/develop/model_optimize_tool/

3.2.4. APP 集成开发

首先下载 Paddle-Lite-Demo 仓库。在仓库中包含 3 个项目:
Here Insert Picture Description

  • PaddleLite-android-demo:Android Demo,包含了目标识别和目标检测
  • ios-classification_demo:iOS Demo,用于目标识别
  • ios-detection_demo:iOS Demo,用于目标检测;

对于 Android 用户,使用 Android Studio 打开 Paddle-Lite-android-demo,插入手机,在 Android studio 上可以编译并在手机上安装对应的 APP,可以在手机上直接打开使用;

对于 iOS 用户,使用 xCode 打开 ios-classification_demo 或者 ios-detection_demo,在 xcode 中选择插入的真机(iphone),点击编译运行即可在手机上安装对应的 APP,用户可以直接在手机上体验视频流的目标识别和检测。

Here Insert Picture Description用户可以替换 Demo 中的预测库到最新的版本或者更换 Demo 中的模型为自己训练的模型,具体操作请参考文档。

3.3. 实验二:基于 FPGA 的蔬菜识别应用

3.3.1. 百度大脑 EdgeBoard 嵌入式 AI 解决方案介绍

EdgeBoard 是基于 FPGA 打造的嵌入式 AI 解决方案及基于此方案实现的系列硬件,与 AI Studio/EasyDL 等模型定制平台深度打通,具有高性能、高通用、低成本、易开发等四大优点,适用于开发验证、产品集成、科研教学、项目落地等应用方向,以及安防监控、工业质检、医疗诊断、农作物生长监控、无人驾驶、无人零售等应用场景,本小节主要使用 EdgeBoard 深度学习计算卡进行案例打造。

Here Insert Picture Description
Edgeboard 可以支持多种数据源输入,可以支持 Float 模型和 INT8 量化模型,可以支持 CPU+FPGA OP 混合调度,可以支持自动融合 OP,FPGA 流水操作。

Here Insert Picture Description

Edgeboard 使用流程包含:模型获取、接入数据源、部署模型、结果输出等四个步骤,就是个简单的应用开发,不具备深度学习的基础的同学也能很容易上手。

3.3.2. 蔬菜识别模型训练

本课程采用了百度 Ai Studio 的公开蔬菜识别项目,首先我们训练一个 Float 模型,并在 AI Studio 平台上使用 GPU 评估下效果。

项目地址:
https://aistudio.baidu.com/aistudio/projectdetail/131380

为了进一步提高识别速度和效率,Edgeboard 会在运行时进行动态量化,这样会有一定的精度损失。我们会基于此模型在 AI Studio 上进行 INT8 量化训练,训练一个 INT8 的模型,来弥补精度损失。

3.3.3. 模型的布署与测试

(1)Paddle Lite 支持 Fpga 硬件原理

Paddle Lite 完整支持从 Mobile 到 Server 多种硬件,这主要得益于对不同硬件的统一抽象,不同的硬件提供统一的接口给上层框架,形成了硬件的可插拔,极大方便了底层硬件的扩展与支持。Fpga 作为 Paddle Lite 的 Backends 之一,将自身硬件相关的 kernel、驱动及内核进行了包装并向 Paddle Lite 提供了统一的 op 调用接口,使得 Paddle Lite 能很方便的集成 Fpga 的计算能力。同时通过 Paddle Lite 上层框架对模型的优化能力,包括各种 op 融合、计算剪枝、存储复用、量化融合的优化 pass, 以及 kernel 的最优调度、混合布署等功能,进一步实现了模型加速。

(2)EdgeBoard 模型布署

利用 Paddle Lite 在 EdgeBoard 上进行模型布署很简单,只需将 include 下头文件包含到调用 Paddle Lite 的代码文件中,将静态库文件与工程文件联编成可执行程序,替换模型即可运行。训练模型效果对比:

Here Insert Picture Description

3.3.4. 实验总结

本课程利用 Paddle Lite 推理引擎在 EdgeBoard 计算卡上布署 Inceptionv4 蔬菜识别模型,实现了边缘端智能识别蔬菜品类的功能,可用于多种场景。整个应用借助 Paddle Lite 的上层框架优化能力与 Fpga 的底层计算加速能力,每秒可识别 30 帧,真正实现实时高效的蔬菜识别。

本课程具有极高的拓展性,由于 Paddle Lite 可以和 PaddlePaddle 模型无缝对接,加上 EdgeBoard 计算卡的软件可持续迭代、op 持续更新等特点,在模型支持上的数量上和速度上均会有很大优势,如本文所用模型也可以更换的分类模型或检测模型。

4. Paddle Lite 项目未来计划

Paddle Lite 未来计划如下,10 月发布正式版,每个月一个版本高效迭代:

Framework to strengthen capabilities and functions, such as platform support for expansion, improve ease of use, encryption, memory optimization
hardware support range continues to expand, new hardware support capabilities to enhance the evaluation of
overall performance optimization, to improve and increase quantify deployment model coverage
document continued optimization: more demo, sample code readability strengthen

Published 116 original articles · won praise 1 · views 4583

Guess you like

Origin blog.csdn.net/PaddleLover/article/details/103737148