Skip to content

Commit

Permalink
[Backend] A311D support (PaddlePaddle#825)
Browse files Browse the repository at this point in the history
* add A311D support

* update code

* update toolchain

* update opencv_armhf lib

* update libs

* update code

* add install script

* update bos link

* update toolchain
  • Loading branch information
yeliang2258 authored Dec 13, 2022
1 parent f6e8fe7 commit 6a1a3d0
Show file tree
Hide file tree
Showing 36 changed files with 1,094 additions and 86 deletions.
22 changes: 10 additions & 12 deletions cmake/opencv.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ elseif(IOS)
else()
if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "aarch64")
set(OPENCV_FILENAME "opencv-linux-aarch64-3.4.14")
else()
if(ENABLE_TIMVX)
set(OPENCV_FILENAME "opencv-armv7hf")
else()
set(OPENCV_FILENAME "opencv-linux-x64-3.4.16")
endif()
endif()
if(ENABLE_OPENCV_CUDA)
if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "aarch64")
Expand All @@ -56,15 +50,20 @@ else()
endif()
endif()

if(NOT OPENCV_FILENAME)
set(OPENCV_FILENAME "opencv-linux-x64-3.4.16")
endif()

set(OPENCV_INSTALL_DIR ${THIRD_PARTY_PATH}/install/)
if(ANDROID)
set(OPENCV_URL_PREFIX "https://bj.bcebos.com/fastdeploy/third_libs")
elseif(ENABLE_TIMVX)
set(OPENCV_URL_PREFIX "https://bj.bcebos.com/fastdeploy/test")
else() # TODO: use fastdeploy/third_libs instead.
set(OPENCV_URL_PREFIX "https://bj.bcebos.com/paddle2onnx/libs")
endif()
set(OPENCV_URL ${OPENCV_URL_PREFIX}/${OPENCV_FILENAME}${COMPRESSED_SUFFIX})
if(NOT OPENCV_URL)
set(OPENCV_URL ${OPENCV_URL_PREFIX}/${OPENCV_FILENAME}${COMPRESSED_SUFFIX})
endif()


if(BUILD_ON_JETSON)
if(EXISTS /usr/lib/aarch64-linux-gnu/cmake/opencv4/)
Expand Down Expand Up @@ -186,9 +185,8 @@ else()
endif()
file(RENAME ${THIRD_PARTY_PATH}/install/${OPENCV_FILENAME}/ ${THIRD_PARTY_PATH}/install/opencv)
set(OPENCV_FILENAME opencv)
set(OpenCV_DIR ${THIRD_PARTY_PATH}/install/${OPENCV_FILENAME})
if(ENABLE_TIMVX)
set(OpenCV_DIR ${OpenCV_DIR}/lib/cmake/opencv4)
if(NOT OpenCV_DIR)
set(OpenCV_DIR ${THIRD_PARTY_PATH}/install/${OPENCV_FILENAME})
endif()
if (WIN32)
set(OpenCV_DIR ${OpenCV_DIR}/build)
Expand Down
28 changes: 13 additions & 15 deletions cmake/paddlelite.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,20 @@ if(ANDROID)
endif()
endif()

if(WIN32 OR APPLE OR IOS)
message(FATAL_ERROR "Doesn't support windows/mac/ios platform with backend Paddle Lite now.")
elseif(ANDROID)
set(PADDLELITE_URL "${PADDLELITE_URL_PREFIX}/lite-android-${ANDROID_ABI}-latest-dev.tgz")
if(ANDROID_ABI MATCHES "arm64-v8a")
set(PADDLELITE_URL "${PADDLELITE_URL_PREFIX}/lite-android-${ANDROID_ABI}-fp16-latest-dev.tgz")
endif()
else() # Linux
if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "aarch64")
set(PADDLELITE_URL "${PADDLELITE_URL_PREFIX}/lite-linux-arm64-20221209.tgz")
else()
if(ENABLE_TIMVX)
set(PADDLELITE_URL "https://bj.bcebos.com/fastdeploy/test/lite-linux_armhf_1130.tgz")
if(NOT PADDLELITE_URL)
if(WIN32 OR APPLE OR IOS)
message(FATAL_ERROR "Doesn't support windows/mac/ios platform with backend Paddle Lite now.")
elseif(ANDROID)
set(PADDLELITE_URL "${PADDLELITE_URL_PREFIX}/lite-android-${ANDROID_ABI}-latest-dev.tgz")
if(ANDROID_ABI MATCHES "arm64-v8a")
set(PADDLELITE_URL "${PADDLELITE_URL_PREFIX}/lite-android-${ANDROID_ABI}-fp16-latest-dev.tgz")
endif()
else() # Linux
if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "aarch64")
set(PADDLELITE_URL "${PADDLELITE_URL_PREFIX}/lite-linux-arm64-20221209.tgz")
else()
message(FATAL_ERROR "Only support Linux aarch64 or ENABLE_TIMVX now, x64 is not supported with backend Paddle Lite.")
endif()
message(FATAL_ERROR "Only support Linux aarch64 now, x64 is not supported with backend Paddle Lite.")
endif()
endif()
endif()

Expand Down
87 changes: 39 additions & 48 deletions cmake/timvx.cmake
Original file line number Diff line number Diff line change
@@ -1,54 +1,45 @@
if (NOT DEFINED CMAKE_SYSTEM_PROCESSOR)
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(CMAKE_C_COMPILER "arm-linux-gnueabihf-gcc")
set(CMAKE_CXX_COMPILER "arm-linux-gnueabihf-g++")
set(CMAKE_CXX_FLAGS "-march=armv7-a -mfloat-abi=hard -mfpu=neon-vfpv4 ${CMAKE_CXX_FLAGS}")
set(CMAKE_C_FLAGS "-march=armv7-a -mfloat-abi=hard -mfpu=neon-vfpv4 ${CMAKE_C_FLAGS}" )
set(CMAKE_BUILD_TYPE MinSizeRel)
else()
if(NOT ${ENABLE_LITE_BACKEND})
message(WARNING "While compiling with -DWITH_TIMVX=ON, will force to set -DENABLE_LITE_BACKEND=ON")
set(ENABLE_LITE_BACKEND ON)
endif()
if(${ENABLE_PADDLE_FRONTEND})
message(WARNING "While compiling with -DWITH_TIMVX=ON, will force to set -DENABLE_PADDLE_FRONTEND=OFF")
set(ENABLE_PADDLE_FRONTEND OFF)
endif()
if(${ENABLE_ORT_BACKEND})
message(WARNING "While compiling with -DWITH_TIMVX=ON, will force to set -DENABLE_ORT_BACKEND=OFF")
set(ENABLE_ORT_BACKEND OFF)
endif()
if(${ENABLE_PADDLE_BACKEND})
message(WARNING "While compiling with -DWITH_TIMVX=ON, will force to set -DENABLE_PADDLE_BACKEND=OFF")
set(ENABLE_PADDLE_BACKEND OFF)
endif()
if(${ENABLE_OPENVINO_BACKEND})
message(WARNING "While compiling with -DWITH_TIMVX=ON, will force to set -DENABLE_OPENVINO_BACKEND=OFF")
set(ENABLE_OPENVINO_BACKEND OFF)
endif()
if(${ENABLE_TRT_BACKEND})
message(WARNING "While compiling with -DWITH_TIMVX=ON, will force to set -DENABLE_TRT_BACKEND=OFF")
set(ENABLE_TRT_BACKEND OFF)
endif()

if(${WITH_GPU})
message(WARNING "While compiling with -DWITH_TIMVX=ON, will force to set -DWITH_GPU=OFF")
set(WITH_GPU OFF)
endif()
if(NOT ${ENABLE_LITE_BACKEND})
message(WARNING "While compiling with -DWITH_TIMVX=ON, will force to set -DENABLE_LITE_BACKEND=ON")
set(ENABLE_LITE_BACKEND ON)
endif()
if(${ENABLE_PADDLE_FRONTEND})
message(WARNING "While compiling with -DWITH_TIMVX=ON, will force to set -DENABLE_PADDLE_FRONTEND=OFF")
set(ENABLE_PADDLE_FRONTEND OFF)
endif()
if(${ENABLE_ORT_BACKEND})
message(WARNING "While compiling with -DWITH_TIMVX=ON, will force to set -DENABLE_ORT_BACKEND=OFF")
set(ENABLE_ORT_BACKEND OFF)
endif()
if(${ENABLE_PADDLE_BACKEND})
message(WARNING "While compiling with -DWITH_TIMVX=ON, will force to set -DENABLE_PADDLE_BACKEND=OFF")
set(ENABLE_PADDLE_BACKEND OFF)
endif()
if(${ENABLE_OPENVINO_BACKEND})
message(WARNING "While compiling with -DWITH_TIMVX=ON, will force to set -DENABLE_OPENVINO_BACKEND=OFF")
set(ENABLE_OPENVINO_BACKEND OFF)
endif()
if(${ENABLE_TRT_BACKEND})
message(WARNING "While compiling with -DWITH_TIMVX=ON, will force to set -DENABLE_TRT_BACKEND=OFF")
set(ENABLE_TRT_BACKEND OFF)
endif()

if(${ENABLE_OPENCV_CUDA})
message(WARNING "While compiling with -DWITH_TIMVX=ON, will force to set -DENABLE_OPENCV_CUDA=OFF")
set(ENABLE_OPENCV_CUDA OFF)
endif()
if(${WITH_GPU})
message(WARNING "While compiling with -DWITH_TIMVX=ON, will force to set -DWITH_GPU=OFF")
set(WITH_GPU OFF)
endif()

if(${ENABLE_TEXT})
set(ENABLE_TEXT OFF CACHE BOOL "Force ENABLE_TEXT OFF" FORCE)
message(STATUS "While compiling with -DWITH_TIMVX=ON, will force to set -DENABLE_TEXT=OFF")
endif()
if (DEFINED CMAKE_INSTALL_PREFIX)
install(FILES ${PROJECT_SOURCE_DIR}/cmake/timvx.cmake DESTINATION ${CMAKE_INSTALL_PREFIX})
endif()
if(${ENABLE_OPENCV_CUDA})
message(WARNING "While compiling with -DWITH_TIMVX=ON, will force to set -DENABLE_OPENCV_CUDA=OFF")
set(ENABLE_OPENCV_CUDA OFF)
endif()

if(${ENABLE_TEXT})
set(ENABLE_TEXT OFF CACHE BOOL "Force ENABLE_TEXT OFF" FORCE)
message(STATUS "While compiling with -DWITH_TIMVX=ON, will force to set -DENABLE_TEXT=OFF")
endif()

install(FILES ${PROJECT_SOURCE_DIR}/cmake/timvx.cmake DESTINATION ${CMAKE_INSTALL_PREFIX})
install(FILES ${PROJECT_SOURCE_DIR}/cmake/toolchain.cmake DESTINATION ${CMAKE_INSTALL_PREFIX})


38 changes: 38 additions & 0 deletions cmake/toolchain.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
if (DEFINED TARGET_ABI)
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_BUILD_TYPE MinSizeRel)
if(${TARGET_ABI} MATCHES "armhf")
set(CMAKE_SYSTEM_PROCESSOR arm)
set(CMAKE_C_COMPILER "arm-linux-gnueabihf-gcc")
set(CMAKE_CXX_COMPILER "arm-linux-gnueabihf-g++")
set(CMAKE_CXX_FLAGS "-march=armv7-a -mfloat-abi=hard -mfpu=neon-vfpv4 ${CMAKE_CXX_FLAGS}")
set(CMAKE_C_FLAGS "-march=armv7-a -mfloat-abi=hard -mfpu=neon-vfpv4 ${CMAKE_C_FLAGS}" )
set(OPENCV_URL "https://bj.bcebos.com/fastdeploy/third_libs/opencv-linux-armv7hf-4.6.0.tgz")
set(OPENCV_FILENAME "opencv-linux-armv7hf-4.6.0")
if(WITH_TIMVX)
set(PADDLELITE_URL "https://bj.bcebos.com/fastdeploy/third_libs/lite-linux-armhf-timvx-1130.tgz")
else()
message(STATUS "PADDLELITE_URL will be configured if WITH_TIMVX=ON.")
endif()
set(THIRD_PARTY_PATH ${CMAKE_CURRENT_BINARY_DIR}/third_libs)
set(OpenCV_DIR ${THIRD_PARTY_PATH}/install/opencv/lib/cmake/opencv4)
elseif(${TARGET_ABI} MATCHES "arm64")
set(CMAKE_SYSTEM_PROCESSOR aarch64)
set(CMAKE_C_COMPILER "aarch64-linux-gnu-gcc")
set(CMAKE_CXX_COMPILER "aarch64-linux-gnu-g++")
set(CMAKE_CXX_FLAGS "-march=armv8-a ${CMAKE_CXX_FLAGS}")
set(CMAKE_C_FLAGS "-march=armv8-a ${CMAKE_C_FLAGS}")
set(OPENCV_URL "https://bj.bcebos.com/fastdeploy/third_libs/opencv-linux-aarch64-4.6.0.tgz")
set(OPENCV_FILENAME "opencv-linux-aarch64-4.6.0")
if(WITH_TIMVX)
set(PADDLELITE_URL "https://bj.bcebos.com/fastdeploy/third_libs/lite-linux-aarch64-timvx-20221209.tgz")
else()
set(PADDLELITE_URL "https://bj.bcebos.com/fastdeploy/third_libs/lite-linux-arm64-20221209.tgz")
endif()
set(THIRD_PARTY_PATH ${CMAKE_CURRENT_BINARY_DIR}/third_libs)
set(OpenCV_DIR ${THIRD_PARTY_PATH}/install/opencv/lib/cmake/opencv4)
else()
message(FATAL_ERROR "When cross-compiling, please set the -DTARGET_ABI to arm64 or armhf.")
endif()
endif()

3 changes: 2 additions & 1 deletion docs/cn/build_and_install/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- [Jetson部署环境](jetson.md)
- [Android平台部署环境](android.md)
- [瑞芯微RV1126部署环境](rv1126.md)
- [晶晨A311D部署环境](a311d.md)


## FastDeploy编译选项说明
Expand All @@ -22,7 +23,7 @@
| ENABLE_PADDLE_BACKEND | 默认OFF,是否编译集成Paddle Inference后端(CPU/GPU上推荐打开) |
| ENABLE_LITE_BACKEND | 默认OFF,是否编译集成Paddle Lite后端(编译Android库时需要设置为ON) |
| ENABLE_RKNPU2_BACKEND | 默认OFF,是否编译集成RKNPU2后端(RK3588/RK3568/RK3566上推荐打开) |
| ENABLE_TIMVX | 默认OFF,需要在RV1126/RV1109上部署时,需设置为ON |
| WITH_TIMVX | 默认OFF,需要在RV1126/RV1109/A311D上部署时,需设置为ON |
| ENABLE_TRT_BACKEND | 默认OFF,是否编译集成TensorRT后端(GPU上推荐打开) |
| ENABLE_OPENVINO_BACKEND | 默认OFF,是否编译集成OpenVINO后端(CPU上推荐打开) |
| ENABLE_VISION | 默认OFF,是否编译集成视觉模型的部署模块 |
Expand Down
107 changes: 107 additions & 0 deletions docs/cn/build_and_install/a311d.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# 晶晨 A311D 部署环境编译安装

FastDeploy 基于 Paddle-Lite 后端支持在晶晨 NPU 上进行部署推理。
更多详细的信息请参考:[PaddleLite部署示例](https://www.paddlepaddle.org.cn/lite/develop/demo_guides/verisilicon_timvx.html)

本文档介绍如何编译基于 PaddleLite 的 C++ FastDeploy 交叉编译库。

相关编译选项说明如下:
|编译选项|默认值|说明|备注|
|:---|:---|:---|:---|
|ENABLE_LITE_BACKEND|OFF|编译A311D部署库时需要设置为ON| - |
|WITH_TIMVX|OFF|编译A311D部署库时需要设置为ON| - |

更多编译选项请参考[FastDeploy编译选项说明](./README.md)

## 交叉编译环境搭建

### 宿主机环境需求
- os:Ubuntu == 16.04
- cmake: version >= 3.10.0

### 环境搭建
可以进入 FastDeploy/tools/timvx 目录,使用如下命令一键安装:
```bash
cd FastDeploy/tools/timvx
bash install.sh
```
也可以按照如下命令安装:
```bash
# 1. Install basic software
apt update
apt-get install -y --no-install-recommends \
gcc g++ git make wget python unzip

# 2. Install arm gcc toolchains
apt-get install -y --no-install-recommends \
g++-arm-linux-gnueabi gcc-arm-linux-gnueabi \
g++-arm-linux-gnueabihf gcc-arm-linux-gnueabihf \
gcc-aarch64-linux-gnu g++-aarch64-linux-gnu

# 3. Install cmake 3.10 or above
wget -c https://mms-res.cdn.bcebos.com/cmake-3.10.3-Linux-x86_64.tar.gz && \
tar xzf cmake-3.10.3-Linux-x86_64.tar.gz && \
mv cmake-3.10.3-Linux-x86_64 /opt/cmake-3.10 && \
ln -s /opt/cmake-3.10/bin/cmake /usr/bin/cmake && \
ln -s /opt/cmake-3.10/bin/ccmake /usr/bin/ccmake
```

## 基于 PaddleLite 的 FastDeploy 交叉编译库编译
搭建好交叉编译环境之后,编译命令如下:
```bash
# Download the latest source code
git clone https://github.com/PaddlePaddle/FastDeploy.git
cd FastDeploy
mkdir build && cd build

# CMake configuration with A311D toolchain
cmake -DCMAKE_TOOLCHAIN_FILE=./../cmake/toolchain.cmake \
-DWITH_TIMVX=ON \
-DTARGET_ABI=arm64 \
-DCMAKE_INSTALL_PREFIX=fastdeploy-tmivx \
-DENABLE_VISION=ON \ # 是否编译集成视觉模型的部署模块,可选择开启
-Wno-dev ..

# Build FastDeploy A311D C++ SDK
make -j8
make install
```
编译完成之后,会生成 fastdeploy-tmivx 目录,表示基于 PadddleLite TIM-VX 的 FastDeploy 库编译完成。

## 准备设备运行环境
部署前要保证晶晨 Linux Kernel NPU 驱动 galcore.so 版本及所适用的芯片型号与依赖库保持一致,在部署前,请登录开发板,并通过命令行输入以下命令查询 NPU 驱动版本,晶晨建议的驱动版本为:6.4.4.3
```bash
dmesg | grep Galcore
```

如果当前版本不符合上述,请用户仔细阅读以下内容,以保证底层 NPU 驱动环境正确。

有两种方式可以修改当前的 NPU 驱动版本:
1. 手动替换 NPU 驱动版本。(推荐)
2. 刷机,刷取 NPU 驱动版本符合要求的固件。

### 手动替换 NPU 驱动版本
1. 使用如下命令下载解压 PaddleLite demo,其中提供了现成的驱动文件
```bash
wget https://paddlelite-demo.bj.bcebos.com/devices/generic/PaddleLite-generic-demo.tar.gz
tar -xf PaddleLite-generic-demo.tar.gz
```
2. 使用 `uname -a` 查看 `Linux Kernel` 版本,确定为 `Linux` 系统 4.19.111 版本,
3.`PaddleLite-generic-demo/libs/PaddleLite/linux/arm64/lib/verisilicon_timvx/viv_sdk_6_4_4_3/lib/a311d/4.9.113` 路径下的 `galcore.ko` 上传至开发板。

4. 登录开发板,命令行输入 `sudo rmmod galcore` 来卸载原始驱动,输入 `sudo insmod galcore.ko` 来加载传上设备的驱动。(是否需要 sudo 根据开发板实际情况,部分 adb 链接的设备请提前 adb root)。此步骤如果操作失败,请跳转至方法 2。
5. 在开发板中输入 `dmesg | grep Galcore` 查询 NPU 驱动版本,确定为:6.4.4.3

### 刷机
根据具体的开发板型号,向开发板卖家或官网客服索要 6.4.4.3 版本 NPU 驱动对应的固件和刷机方法。

更多细节请参考:[PaddleLite准备设备环境](https://www.paddlepaddle.org.cn/lite/develop/demo_guides/verisilicon_timvx.html#zhunbeishebeihuanjing)

## 基于 FastDeploy 在 A311D 上的部署示例
1. A311D 上部署 PaddleClas 分类模型请参考:[PaddleClas 分类模型在 A311D 上的 C++ 部署示例](../../../examples/vision/classification/paddleclas/a311d/README.md)

2. A311D 上部署 PPYOLOE 检测模型请参考:[PPYOLOE 检测模型在 A311D 上的 C++ 部署示例](../../../examples/vision/detection/paddledetection/a311d/README.md)

3. A311D 上部署 YOLOv5 检测模型请参考:[YOLOv5 检测模型在 A311D 上的 C++ 部署示例](../../../examples/vision/detection/yolov5/a311d/README.md)

4. A311D 上部署 PP-LiteSeg 分割模型请参考:[PP-LiteSeg 分割模型在 A311D 上的 C++ 部署示例](../../../examples/vision/segmentation/paddleseg/a311d/README.md)
13 changes: 10 additions & 3 deletions docs/cn/build_and_install/rv1126.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ FastDeploy基于 Paddle-Lite 后端支持在瑞芯微(Rockchip)Soc 上进行
|编译选项|默认值|说明|备注|
|:---|:---|:---|:---|
|ENABLE_LITE_BACKEND|OFF|编译RK库时需要设置为ON| - |
|ENABLE_TIMVX|OFF|编译RK库时需要设置为ON| - |
|WITH_TIMVX|OFF|编译RK库时需要设置为ON| - |

更多编译选项请参考[FastDeploy编译选项说明](./README.md)

Expand All @@ -20,6 +20,12 @@ FastDeploy基于 Paddle-Lite 后端支持在瑞芯微(Rockchip)Soc 上进行
- cmake: version >= 3.10.0

### 环境搭建
可以进入 FastDeploy/tools/timvx 目录,使用如下命令一键安装:
```bash
cd FastDeploy/tools/timvx
bash install.sh
```
也可以按照如下命令安装:
```bash
# 1. Install basic software
apt update
Expand Down Expand Up @@ -49,8 +55,9 @@ cd FastDeploy
mkdir build && cd build

# CMake configuration with RK toolchain
cmake -DCMAKE_TOOLCHAIN_FILE=./../cmake/timvx.cmake \
-DENABLE_TIMVX=ON \
cmake -DCMAKE_TOOLCHAIN_FILE=./../cmake/toolchain.cmake \
-DWITH_TIMVX=ON \
-DTARGET_ABI=armhf \
-DCMAKE_INSTALL_PREFIX=fastdeploy-tmivx \
-DENABLE_VISION=ON \ # 是否编译集成视觉模型的部署模块,可选择开启
-Wno-dev ..
Expand Down
11 changes: 11 additions & 0 deletions examples/vision/classification/paddleclas/a311d/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# PaddleClas 量化模型在 A311D 上的部署
目前 FastDeploy 已经支持基于 PaddleLite 部署 PaddleClas 量化模型到 A311D 上。

模型的量化和量化模型的下载请参考:[模型量化](../quantize/README.md)


## 详细部署文档

在 A311D 上只支持 C++ 的部署。

- [C++部署](cpp)
Loading

0 comments on commit 6a1a3d0

Please sign in to comment.