Skip to content

Commit

Permalink
[Other] Support Ascend deployment on X86 platform. (PaddlePaddle#1067)
Browse files Browse the repository at this point in the history
* Add Huawei Ascend NPU deploy through PaddleLite CANN

* Add NNAdapter interface for paddlelite

* Modify Huawei Ascend Cmake

* Update way for compiling Huawei Ascend NPU deployment

* remove UseLiteBackend in UseCANN

* Support compile python whlee

* Change names of nnadapter API

* Add nnadapter pybind and remove useless API

* Support Python deployment on Huawei Ascend NPU

* Add models suppor for ascend

* Add PPOCR rec reszie for ascend

* fix conflict for ascend

* Rename CANN to Ascend

* Rename CANN to Ascend

* Improve ascend

* fix ascend bug

* improve ascend docs

* improve ascend docs

* improve ascend docs

* Improve Ascend

* Improve Ascend

* Move ascend python demo

* Imporve ascend

* Improve ascend

* Improve ascend

* Improve ascend

* Improve ascend

* Imporve ascend

* Imporve ascend

* Improve ascend

* acc eval script

* acc eval

* remove acc_eval from branch huawei

* Add detection and segmentation examples for Ascend deployment

* Add detection and segmentation examples for Ascend deployment

* Add PPOCR example for ascend deploy

* Imporve paddle lite compiliation

* Add FlyCV doc

* Add FlyCV doc

* Add FlyCV doc

* Imporve Ascend docs

* Imporve Ascend docs

* Improve PPOCR example

* Support Ascend deployment on X86 platform

* Improve Ascend docs

* Improve ascend

* Improve ascend

* Change Paddle Lite Ascend URL
  • Loading branch information
yunyaoXYY authored Jan 9, 2023
1 parent 4aa4ebd commit 43f22f4
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 26 deletions.
43 changes: 28 additions & 15 deletions cmake/ascend.cmake
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
if(NOT CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "aarch64")
message(FATAL_ERROR "Huawei Ascend NPU is supported on Linux aarch64 platform for now.")
endif()

if(NOT ${ENABLE_LITE_BACKEND})
set(ENABLE_LITE_BACKEND ON)
endif()

if (NOT BUILD_FASTDEPLOY_PYTHON)
message(STATUS "Build FastDeploy Ascend C++ library.")
if(NOT PADDLELITE_URL)
set(PADDLELITE_URL "https://bj.bcebos.com/fastdeploy/test/lite-linux_arm64_huawei_ascend_npu_1121.tgz")
endif()
else ()
message(STATUS "Build FastDeploy Ascend Python library.")
if(NOT PADDLELITE_URL)
set(PADDLELITE_URL "https://bj.bcebos.com/fastdeploy/test/lite-linux_arm64_huawei_ascend_npu_python_1207.tgz")
endif()
endif()
if(NOT CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "aarch64")
if (NOT BUILD_FASTDEPLOY_PYTHON)
message(STATUS "Build FastDeploy Ascend C++ library on X86 platform.")
if(NOT PADDLELITE_URL)
set(PADDLELITE_URL "https://bj.bcebos.com/fastdeploy/third_libs/lite-linux_x86_huawei_ascend_npu_0105.tgz")
endif()
else ()
message(STATUS "Build FastDeploy Ascend Python library on X86 platform.")
if(NOT PADDLELITE_URL)
set(PADDLELITE_URL "https://bj.bcebos.com/fastdeploy/third_libs/lite-linux_x86_huawei_ascend_npu_python_0105.tgz")
endif()
endif()
endif()


if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "aarch64")
if (NOT BUILD_FASTDEPLOY_PYTHON)
message(STATUS "Build FastDeploy Ascend C++ library on aarch64 platform.")
if(NOT PADDLELITE_URL)
set(PADDLELITE_URL "https://bj.bcebos.com/fastdeploy/third_libs/lite-linux_arm64_huawei_ascend_npu_1121.tgz")
endif()
else ()
message(STATUS "Build FastDeploy Ascend Python library on aarch64 platform.")
if(NOT PADDLELITE_URL)
set(PADDLELITE_URL "https://bj.bcebos.com/fastdeploy/third_libs/lite-linux_arm64_huawei_ascend_npu_python_1207.tgz")
endif()
endif()
endif()
27 changes: 22 additions & 5 deletions docs/cn/build_and_install/huawei_ascend.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
FastDeploy基于 Paddle-Lite 后端, 支持在华为昇腾NPU上进行部署推理。
更多详细的信息请参考:[Paddle Lite部署示例](https://github.com/PaddlePaddle/Paddle-Lite/blob/develop/docs/demo_guides/huawei_ascend_npu.md)

本文档介绍如何在ARM Linux操作系统环境下, 编译基于 Paddle Lite 的 C++ 与 Python 的FastDeploy源码, 生成目标硬件为华为昇腾NPU的预测库。
本文档介绍如何在ARM/X86_64的Linux操作系统环境下, 编译基于 Paddle Lite 的 C++ 与 Python 的FastDeploy源码, 生成目标硬件为华为昇腾NPU的预测库。

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

Expand All @@ -14,7 +14,7 @@ FastDeploy基于 Paddle-Lite 后端, 支持在华为昇腾NPU上进行部署推
- 配套驱动和固件包下载:
- https://www.hiascend.com/hardware/firmware-drivers?tag=community(社区版)
- https://www.hiascend.com/hardware/firmware-drivers?tag=commercial(商业版)
- 驱动:Atlas-300i-pro-npu-driver_5.1.rc2_linux-aarch64.run
- 驱动:Atlas-300i-pro-npu-driver_5.1.rc2_linux-aarch64.run (这里以aarch64平台为例)
- 固件:Atlas-300i-pro-npu-firmware_5.1.rc2.run
- 安装驱动和固件包:

Expand All @@ -35,13 +35,14 @@ $ npu-smi info
## 二.编译环境搭建

### 宿主机环境需求
- os:ARM-Linux
- os:ARM-Linux, X86_64-Linux
- gcc、g++、git、make、wget、python、pip、python-dev、patchelf
- cmake(建议使用 3.10 或以上版本)

### 使用Docker开发环境
为了保证和FastDeploy验证过的编译环境一致,建议使用Docker开发环境进行配置.

aarch64平台示例
```shell
# 下载 Dockerfile
$ wget https://bj.bcebos.com/fastdeploy/test/Ascend_ubuntu18.04_aarch64_5.1.rc2.Dockerfile
Expand All @@ -54,11 +55,27 @@ $ docker exec -it ascend-aarch64 /bin/bash
# 确认容器的 Ascend 环境是否创建成功
$ npu-smi info
```

x86_64平台示例
```shell
# 下载 Dockerfile
$ wget https://paddlelite-demo.bj.bcebos.com/devices/huawei/ascend/intel_x86/Ascend_ubuntu18.04_x86_5.1.rc1.alpha001.Dockerfile
# 通过 Dockerfile 生成镜像
$ docker build --network=host -f Ascend_ubuntu18.04_x86_5.1.rc1.alpha001.Dockerfile -t paddlelite/ascend_x86:cann_5.1.rc1.alpha001 .
# 创建容器
$ docker run -itd --privileged --name=ascend-x86 --net=host -v $PWD:/Work -w /Work --device=/dev/davinci0 --device=/dev/davinci_manager --device=/dev/hisi_hdc --device /dev/devmm_svm -v /usr/local/bin/npu-smi:/usr/local/bin/npu-smi -v /usr/local/Ascend/driver/:/usr/local/Ascend/driver/ paddlelite/ascend_x86:cann_5.1.1.alpha001 /bin/bash
# 进入容器
$ docker exec -it ascend-x86 /bin/bash
# 确认容器的 Ascend 环境是否创建成功
$ npu-smi info
```

以上步骤成功后,用户可以直接在docker内部开始FastDeploy的编译.

注意:
- 如果用户在Docker内想使用其他的CANN版本,请自行更新 Dockerfile 文件内的 CANN 下载路径, 同时更新相应的驱动和固件. 当前Dockerfile内默认为[CANN 5.1.RC2](https://ascend-repo.obs.cn-east-2.myhuaweicloud.com/CANN/CANN%205.1.RC2/Ascend-cann-toolkit_5.1.RC2_linux-aarch64.run).
- 如果用户不想使用docker,可以参考由Paddle Lite提供的[ARM Linux环境下的编译环境准备](https://github.com/PaddlePaddle/Paddle-Lite/blob/develop/docs/source_compile/arm_linux_compile_arm_linux.rst)自行配置编译环境, 之后再自行下载并安装相应的CANN软件包来完成配置.
- 如果用户在Docker内想使用其他的CANN版本,请自行更新 Dockerfile 文件内的 CANN 下载路径, 同时更新相应的驱动和固件. 当前示例中, aarch64平台的Dockerfile内默认为[CANN 5.1.RC2](https://ascend-repo.obs.cn-east-2.myhuaweicloud.com/CANN/CANN%205.1.RC2/Ascend-cann-toolkit_5.1.RC2_linux-aarch64.run), x86_64平台的Dockerfile内默认为[CANN 5.1.RC1](https://ascend-repo.obs.cn-east-2.myhuaweicloud.com/CANN/5.1.RC1.alpha001/Ascend-cann-toolkit_5.1.RC1.alpha001_linux-x86_64.run).

- 如果用户不想使用docker,可以参考由Paddle Lite提供的[ARM Linux环境下的编译环境准备](https://github.com/PaddlePaddle/Paddle-Lite/blob/develop/docs/source_compile/arm_linux_compile_arm_linux.rst)或者[X86 Linux环境下的编译环境准备](https://github.com/PaddlePaddle/Paddle-Lite/blob/develop/docs/source_compile/linux_x86_compile_linux_x86.rst)自行配置编译环境, 之后再自行下载并安装相应的CANN软件包来完成配置.

## 三.基于 Paddle Lite 的 C++ FastDeploy 库编译
搭建好编译环境之后,编译命令如下:
Expand Down
26 changes: 21 additions & 5 deletions docs/en/build_and_install/huawei_ascend.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Based on the Paddle-Lite backend, FastDeploy supports model inference on Huawei's Ascend NPU.
For more detailed information, please refer to: [Paddle Lite Deployment Example](https://github.com/PaddlePaddle/Paddle-Lite/blob/develop/docs/demo_guides/huawei_ascend_npu.md).

This document describes how to compile C++ and Python FastDeploy source code under ARM Linux OS environment to generate prediction libraries for Huawei Sunrise NPU as the target hardware.
This document describes how to compile C++ and Python FastDeploy source code under ARM/X86_64 Linux OS environment to generate prediction libraries for Huawei Sunrise NPU as the target hardware.

For more compilation options, please refer to the [FastDeploy compilation options description](./README.md)

Expand All @@ -13,7 +13,7 @@ For more compilation options, please refer to the [FastDeploy compilation option
- Download the matching driver and firmware package at:
- https://www.hiascend.com/hardware/firmware-drivers?tag=community(Community Edition)
- https://www.hiascend.com/hardware/firmware-drivers?tag=commercial(Commercial version)
- driver:Atlas-300i-pro-npu-driver_5.1.rc2_linux-aarch64.run
- driver:Atlas-300i-pro-npu-driver_5.1.rc2_linux-aarch64.run (aarch64 as example)
- firmware:Atlas-300i-pro-npu-firmware_5.1.rc2.run
- Installing drivers and firmware packages:

Expand All @@ -32,13 +32,14 @@ $ npu-smi info
## Compilation environment construction

### Host environment requirements
- os: ARM-Linux
- os: ARM-Linux, X86_64-Linux
- gcc, g++, git, make, wget, python, pip, python-dev, patchelf
- cmake (version 3.10 or above recommended)

### Using Docker development environment
In order to ensure consistency with the FastDeploy verified build environment, it is recommended to use the Docker development environment for configuration.

On aarch64 platform,
```shell
# Download Dockerfile
$ wget https://bj.bcebos.com/fastdeploy/test/Ascend_ubuntu18.04_aarch64_5.1.rc2.Dockerfile
Expand All @@ -51,11 +52,26 @@ $ docker exec -it ascend-aarch64 /bin/bash
# Verify that the Ascend environment for the container is created successfully
$ npu-smi info
```
On X86_64 platform,
```shell
# Download Dockerfile
$ wget https://paddlelite-demo.bj.bcebos.com/devices/huawei/ascend/intel_x86/Ascend_ubuntu18.04_x86_5.1.rc1.alpha001.Dockerfile
# Create docker images
$ docker build --network=host -f Ascend_ubuntu18.04_x86_5.1.rc1.alpha001.Dockerfile -t paddlelite/ascend_x86:cann_5.1.rc1.alpha001 .
# Create container
$ docker run -itd --privileged --name=ascend-x86 --net=host -v $PWD:/Work -w /Work --device=/dev/davinci0 --device=/dev/davinci_manager --device=/dev/hisi_hdc --device /dev/devmm_svm -v /usr/local/bin/npu-smi:/usr/local/bin/npu-smi -v /usr/local/Ascend/driver/:/usr/local/Ascend/driver/ paddlelite/ascend_x86:cann_5.1.1.alpha001 /bin/bash
# Enter the container
$ docker exec -it ascend-x86 /bin/bash
# Verify that the Ascend environment for the container is created successfully
$ npu-smi info
```

Once the above steps are successful, the user can start compiling FastDeploy directly from within docker.

Note:
- If you want to use another CANN version in Docker, please update the CANN download path in the Dockerfile file, and update the corresponding driver and firmware. The current default in Dockerfile is [CANN 5.1.RC2](https://ascend-repo.obs.cn-east-2.myhuaweicloud.com/CANN/CANN%205.1.RC2/Ascend-cann-toolkit_5.1.RC2_linux-aarch64.run).
- If users do not want to use docker, you can refer to [Compile Environment Preparation for ARM Linux Environments](https://github.com/PaddlePaddle/Paddle-Lite/blob/develop/docs/source_compile/arm_linux_compile_arm_linux.rst) provided by Paddle Lite and configure your own compilation environment, and then download and install the proper CANN packages to complete the configuration.
- If you want to use another CANN version in Docker, please update the CANN download path in the Dockerfile file, and update the corresponding driver and firmware. The current default in aarch64 Dockerfile is [CANN 5.1.RC2](https://ascend-repo.obs.cn-east-2.myhuaweicloud.com/CANN/CANN%205.1.RC2/Ascend-cann-toolkit_5.1.RC2_linux-aarch64.run), in x86_64 is [CANN 5.1.RC1](https://ascend-repo.obs.cn-east-2.myhuaweicloud.com/CANN/5.1.RC1.alpha001/Ascend-cann-toolkit_5.1.RC1.alpha001_linux-x86_64.run).

- If users do not want to use docker, you can refer to [Compile Environment Preparation for ARM Linux Environments](https://github.com/PaddlePaddle/Paddle-Lite/blob/develop/docs/source_compile/arm_linux_compile_arm_linux.rst) or [Compile Environment Preparation for X86 Linux Environments](https://github.com/PaddlePaddle/Paddle-Lite/blob/develop/docs/source_compile/linux_x86_compile_linux_x86.rst) provided by Paddle Lite and configure your own compilation environment, and then download and install the proper CANN packages to complete the configuration.

## C++ FastDeploy library compilation based on Paddle Lite
After setting up the compilation environment, the compilation command is as follows.
Expand Down
25 changes: 24 additions & 1 deletion scripts/patch_paddle_lite.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
def process_paddle_lite(paddle_lite_so_path):
if platform.system().lower() != "linux":
return
rpaths = ["$ORIGIN"]
rpaths = ["$ORIGIN", "$ORIGIN/mklml/lib/"]
patchelf_exe = os.getenv("PATCHELF_EXE", "patchelf")

for paddle_lite_so_file in os.listdir(paddle_lite_so_path):
paddle_lite_so_file = os.path.join(paddle_lite_so_path,
paddle_lite_so_file)

# Patch /paddlelite/lib/*.so
if '.so' in paddle_lite_so_file:
command = "{} --set-rpath '{}' {}".format(
patchelf_exe, ":".join(rpaths), paddle_lite_so_file)
Expand All @@ -38,6 +40,27 @@ def process_paddle_lite(paddle_lite_so_path):
command) == 0, "patchelf {} failed, the command: {}".format(
paddle_lite_so_file, command)

# Patch /paddlelite/lib/mklml/lib/*.so
if 'mklml' in paddle_lite_so_file:
paddle_lite_mklml_lib_path = os.path.join(
paddle_lite_so_path, paddle_lite_so_file, 'lib')

for paddle_lite_mklml_so_file in os.listdir(
paddle_lite_mklml_lib_path):
paddle_lite_mklml_so_file = os.path.join(
paddle_lite_mklml_lib_path, paddle_lite_mklml_so_file)

if '.so' in paddle_lite_mklml_so_file:
command = "{} --set-rpath '{}' {}".format(
patchelf_exe, ":".join(rpaths),
paddle_lite_mklml_so_file)
if platform.machine() != 'sw_64' and platform.machine(
) != 'mips64':
assert os.system(
command
) == 0, "patchelf {} failed, the command: {}".format(
paddle_lite_mklml_so_file, command)


if __name__ == "__main__":
process_paddle_lite(sys.argv[1])

0 comments on commit 43f22f4

Please sign in to comment.