Skip to content

Commit 4f4eddb

Browse files
yanfeichLeo ZhaohabanachinaZhaiFeiYue1zhaify
authored
[Intel_HPU]Support intel hpu backend (#1418)
Co-authored-by: Leo Zhao <[email protected]> Co-authored-by: habanachina <[email protected]> Co-authored-by: Zhai Feiyue <[email protected]> Co-authored-by: Zhai Feiyue <[email protected]> Co-authored-by: Jinyan Chen <[email protected]> Co-authored-by: inkcherry <[email protected]> Co-authored-by: Jinyan Chen <[email protected]> Co-authored-by: zhaify <[email protected]>
1 parent 9844952 commit 4f4eddb

File tree

109 files changed

+15919
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+15919
-0
lines changed

backends/intel_hpu/CMakeLists.txt

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4+
# use this file except in compliance with the License. You may obtain a copy of
5+
# the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations under
13+
# the License
14+
15+
cmake_minimum_required(VERSION 3.10)
16+
17+
project(paddle-intel-hpu CXX C)
18+
19+
set(CMAKE_BUILD_TYPE "Debug")
20+
set(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g2 -ggdb")
21+
set(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")
22+
23+
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
24+
25+
option(WITH_TESTING "compile with unit testing" ON)
26+
option(ON_INFER "compile with inference c++ lib" OFF)
27+
option(WITH_MKL "compile with mkl support" ON)
28+
option(WITH_ARM "compile with arm support" OFF)
29+
30+
set(PLUGIN_NAME "paddle-intel-hpu")
31+
set(PLUGIN_VERSION "0.0.1")
32+
33+
include(paddle)
34+
include(generic)
35+
include(version)
36+
37+
include_directories(${PADDLE_INC_DIR} ${CMAKE_SOURCE_DIR}
38+
${CMAKE_SOURCE_DIR}/kernels /usr/include/habanalabs)
39+
link_directories(${PADDLE_LIB_DIR} /usr/lib/habanalabs)
40+
41+
add_definitions(-std=c++14)
42+
43+
file(
44+
GLOB_RECURSE PLUGIN_SRCS
45+
RELATIVE ${CMAKE_SOURCE_DIR}
46+
kernels/*.cc utils/*.cc)
47+
list(APPEND PLUGIN_SRCS runtime/runtime.cc)
48+
49+
# build shared library
50+
add_library(${PLUGIN_NAME} SHARED ${PLUGIN_SRCS})
51+
if(ON_INFER)
52+
target_link_directories(${PLUGIN_NAME} PRIVATE ${PADDLE_INFERENCE_LIB_DIR})
53+
target_link_libraries(${PLUGIN_NAME} PRIVATE paddle_inference glog Synapse)
54+
else()
55+
target_link_libraries(${PLUGIN_NAME} PRIVATE ${PADDLE_CORE_LIB} glog Synapse)
56+
endif()
57+
58+
include(third_party)
59+
add_dependencies(${PLUGIN_NAME} third_party)
60+
61+
# packing wheel package
62+
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/setup.py.in
63+
${CMAKE_CURRENT_BINARY_DIR}/setup.py)
64+
65+
add_custom_command(
66+
TARGET ${PLUGIN_NAME}
67+
POST_BUILD
68+
COMMAND ${CMAKE_COMMAND} -E remove -f ${CMAKE_CURRENT_BINARY_DIR}/python/
69+
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/python/
70+
COMMAND ${CMAKE_COMMAND} -E make_directory
71+
${CMAKE_CURRENT_BINARY_DIR}/python/paddle_custom_device/
72+
COMMAND
73+
${CMAKE_COMMAND} -E copy_if_different
74+
${CMAKE_CURRENT_BINARY_DIR}/lib${PLUGIN_NAME}.so
75+
${CMAKE_CURRENT_BINARY_DIR}/python/paddle_custom_device/
76+
COMMENT "Creating plugin dirrectories------>>>")
77+
78+
find_package(
79+
Python
80+
COMPONENTS Interpreter
81+
REQUIRED)
82+
83+
add_custom_command(
84+
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/python/.timestamp
85+
COMMAND ${Python_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/setup.py bdist_wheel
86+
DEPENDS ${PLUGIN_NAME}
87+
COMMENT "Packing whl packages------>>>")
88+
89+
add_custom_target(python_package ALL
90+
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/python/.timestamp)
91+
92+
if(WITH_TESTING)
93+
set(PYTHON_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../Paddle")
94+
enable_testing()
95+
add_subdirectory(tests)
96+
add_custom_command(
97+
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/tests/.timestamp
98+
COMMAND cp -r ${CMAKE_SOURCE_DIR}/tests ${CMAKE_CURRENT_BINARY_DIR})
99+
add_custom_target(python_tests ALL
100+
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/tests/.timestamp)
101+
endif()

backends/intel_hpu/README.md

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# PaddlePaddle Custom Device Implementaion for Custom CPU
2+
3+
English | [简体中文](./README_cn.md)
4+
5+
Please refer to the following steps to compile, install and verify the custom device implementaion for Habana HPU.
6+
7+
## Get Sources
8+
9+
```bash
10+
# clone source
11+
git clone --recursive https://github.com/PaddlePaddle/PaddleCustomDevice
12+
cd PaddleCustomDevice
13+
14+
# get the latest submodule source code
15+
git submodule sync
16+
git submodule update --remote --init --recursive
17+
```
18+
19+
## Compile and Install
20+
21+
```bash
22+
# navigate to implementaion for Habana HPU
23+
cd backends/intel_hpu
24+
25+
# before compiling, ensure that Paddle is installed, you can run the following command
26+
pip install paddlepaddle==0.0.0 -f https://www.paddlepaddle.org.cn/whl/linux/cpu-mkl/develop.html
27+
28+
# create the build directory and navigate in
29+
mkdir build && cd build
30+
31+
cmake ..
32+
make -j8
33+
34+
# using pip to install the output
35+
pip install dist/paddle_intel_hpu*.whl
36+
```
37+
38+
## Verification
39+
40+
```bash
41+
# list available hardware backends
42+
python -c "import paddle; print(paddle.device.get_all_custom_device_type())"
43+
44+
# expected output
45+
['intel_hpu']
46+
47+
# run a simple model
48+
python ../tests/test_MNIST_model.py
49+
50+
# expected similar output
51+
... ...
52+
Epoch 0 step 0, Loss = [2.2956038], Accuracy = 0.15625
53+
Epoch 0 step 100, Loss = [2.1552896], Accuracy = 0.3125
54+
Epoch 0 step 200, Loss = [2.1177733], Accuracy = 0.4375
55+
Epoch 0 step 300, Loss = [2.0089214], Accuracy = 0.53125
56+
Epoch 0 step 400, Loss = [2.0845466], Accuracy = 0.421875
57+
Epoch 0 step 500, Loss = [2.0473], Accuracy = 0.453125
58+
Epoch 0 step 600, Loss = [1.8561764], Accuracy = 0.71875
59+
Epoch 0 step 700, Loss = [1.9915285], Accuracy = 0.53125
60+
Epoch 0 step 800, Loss = [1.8925955], Accuracy = 0.640625
61+
Epoch 0 step 900, Loss = [1.8199624], Accuracy = 0.734375
62+
```
63+
64+
## Using PaddleInference
65+
66+
Re-compile plugin
67+
68+
```bash
69+
# Compile PaddleInference
70+
git clone https://github.com/PaddlePaddle/Paddle.git
71+
git clone https://github.com/ronny1996/Paddle-Inference-Demo.git
72+
73+
mkdir -p Paddle/build
74+
pushd Paddle/build
75+
76+
cmake .. -DPY_VERSION=3.7 -DWITH_GPU=OFF -DWITH_TESTING=ON -DCMAKE_BUILD_TYPE=Release -DON_INFER=ON -DWITH_MKL=ON -DWITH_CUSTOM_DEVICE=ON
77+
78+
make -j8
79+
80+
popd
81+
cp -R Paddle/build/paddle_inference_install_dir Paddle-Inference-Demo/c++/lib/paddle_inference
82+
export PADDLE_INFERENCE_LIB_DIR=$(realpath Paddle-Inference-Demo/c++/lib/paddle_inference/paddle/lib)
83+
84+
# Compile the plug-in
85+
mkdir -p PaddleCustomDevice/backends/intel_hpu/build
86+
pushd PaddleCustomDevice/backends/intel_hpu/build
87+
88+
cmake .. -DON_INFER=ON -DPADDLE_INFERENCE_LIB_DIR=${PADDLE_INFERENCE_LIB_DIR}
89+
make -j8
90+
91+
# Specify the plug-in directory
92+
export CUSTOM_DEVICE_ROOT=$PWD
93+
popd
94+
```
95+
96+
Using PaddleInference
97+
98+
```bash
99+
pushd Paddle-Inference-Demo/c++/resnet50
100+
101+
# Modify resnet50_test.cc, use config.EnableCustomDevice("intel_hpu", 0) to replace config.EnableUseGpu(100, 0)
102+
103+
bash run.sh
104+
```
105+
106+
Expected similar output
107+
108+
```bash
109+
I0713 09:02:38.808723 24792 resnet50_test.cc:74] run avg time is 297.75 ms
110+
I0713 09:02:38.808859 24792 resnet50_test.cc:89] 0 : 8.76192e-29
111+
I0713 09:02:38.808894 24792 resnet50_test.cc:89] 100 : 8.76192e-29
112+
I0713 09:02:38.808904 24792 resnet50_test.cc:89] 200 : 8.76192e-29
113+
I0713 09:02:38.808912 24792 resnet50_test.cc:89] 300 : 8.76192e-29
114+
I0713 09:02:38.808920 24792 resnet50_test.cc:89] 400 : 8.76192e-29
115+
I0713 09:02:38.808928 24792 resnet50_test.cc:89] 500 : 8.76192e-29
116+
I0713 09:02:38.808936 24792 resnet50_test.cc:89] 600 : 1.05766e-19
117+
I0713 09:02:38.808945 24792 resnet50_test.cc:89] 700 : 2.04093e-23
118+
I0713 09:02:38.808954 24792 resnet50_test.cc:89] 800 : 3.85255e-25
119+
I0713 09:02:38.808961 24792 resnet50_test.cc:89] 900 : 8.76192e-29
120+
```

backends/intel_hpu/README_cn.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# 飞桨自定义接入硬件后端(Custom CPU)
2+
3+
简体中文 | [English](./README.md)
4+
5+
请参考以下步骤进行硬件后端(Habana HPU)的编译安装与验证
6+
7+
## 一、源码同步
8+
9+
```bash
10+
# 克隆代码
11+
git clone --recursive https://github.com/PaddlePaddle/PaddleCustomDevice
12+
cd PaddleCustomDevice
13+
14+
# 请执行以下命令,以保证checkout最新的Paddle源码
15+
git submodule sync
16+
git submodule update --remote --init --recursive
17+
```
18+
19+
## 二、编译安装
20+
21+
```bash
22+
# 进入硬件后端(Habana HPU)目录
23+
cd backends/intel_hpu
24+
25+
# 编译之前需要先保证环境下装有Paddle WHL包,可以直接安装CPU版本
26+
pip install paddlepaddle==0.0.0 -f https://www.paddlepaddle.org.cn/whl/linux/cpu-mkl/develop.html
27+
28+
# 创建编译目录并编译
29+
mkdir build && cd build
30+
31+
cmake ..
32+
make -j8
33+
34+
# 编译产出在dist路径下,使用pip安装
35+
pip install dist/paddle_habana_hpu*.whl
36+
```
37+
38+
## 三、功能验证
39+
40+
```bash
41+
# 列出可用硬件后端
42+
python -c "import paddle; print(paddle.device.get_all_custom_device_type())"
43+
# 期待输出以下结果
44+
['intel_hpu']
45+
46+
# 运行简单模型
47+
python ../tests/test_MNIST_model.py
48+
# 期待输出以下类似结果
49+
... ...
50+
Epoch 0 step 0, Loss = [2.2956038], Accuracy = 0.15625
51+
Epoch 0 step 100, Loss = [2.1552896], Accuracy = 0.3125
52+
Epoch 0 step 200, Loss = [2.1177733], Accuracy = 0.4375
53+
Epoch 0 step 300, Loss = [2.0089214], Accuracy = 0.53125
54+
Epoch 0 step 400, Loss = [2.0845466], Accuracy = 0.421875
55+
Epoch 0 step 500, Loss = [2.0473], Accuracy = 0.453125
56+
Epoch 0 step 600, Loss = [1.8561764], Accuracy = 0.71875
57+
Epoch 0 step 700, Loss = [1.9915285], Accuracy = 0.53125
58+
Epoch 0 step 800, Loss = [1.8925955], Accuracy = 0.640625
59+
Epoch 0 step 900, Loss = [1.8199624], Accuracy = 0.734375
60+
```
61+
62+
## 四、使用PaddleInference
63+
64+
重新编译插件
65+
66+
```bash
67+
# 编译PaddleInference
68+
git clone https://github.com/PaddlePaddle/Paddle.git
69+
git clone https://github.com/ronny1996/Paddle-Inference-Demo.git
70+
71+
mkdir -p Paddle/build
72+
pushd Paddle/build
73+
74+
cmake .. -DPY_VERSION=3.7 -DWITH_GPU=OFF -DWITH_TESTING=ON -DCMAKE_BUILD_TYPE=Release -DON_INFER=ON -DWITH_MKL=ON -DWITH_CUSTOM_DEVICE=ON
75+
76+
make -j8
77+
78+
popd
79+
cp -R Paddle/build/paddle_inference_install_dir Paddle-Inference-Demo/c++/lib/paddle_inference
80+
export PADDLE_INFERENCE_LIB_DIR=$(realpath Paddle-Inference-Demo/c++/lib/paddle_inference/paddle/lib)
81+
82+
# 编译插件
83+
mkdir -p PaddleCustomDevice/backends/intel_hpu/build
84+
pushd PaddleCustomDevice/backends/intel_hpu/build
85+
86+
cmake .. -DON_INFER=ON -DPADDLE_INFERENCE_LIB_DIR=${PADDLE_INFERENCE_LIB_DIR}
87+
make -j8
88+
89+
# 指定插件路径
90+
export CUSTOM_DEVICE_ROOT=$PWD
91+
popd
92+
```
93+
94+
使用 PaddleInference
95+
96+
```bash
97+
pushd Paddle-Inference-Demo/c++/resnet50
98+
99+
# 修改 resnet50_test.cc,使用 config.EnableCustomDevice("intel_hpu", 0) 接口替换 config.EnableUseGpu(100, 0)
100+
101+
bash run.sh
102+
```
103+
104+
期待输出以下类似结果
105+
106+
```bash
107+
I0713 09:02:38.808723 24792 resnet50_test.cc:74] run avg time is 297.75 ms
108+
I0713 09:02:38.808859 24792 resnet50_test.cc:89] 0 : 8.76192e-29
109+
I0713 09:02:38.808894 24792 resnet50_test.cc:89] 100 : 8.76192e-29
110+
I0713 09:02:38.808904 24792 resnet50_test.cc:89] 200 : 8.76192e-29
111+
I0713 09:02:38.808912 24792 resnet50_test.cc:89] 300 : 8.76192e-29
112+
I0713 09:02:38.808920 24792 resnet50_test.cc:89] 400 : 8.76192e-29
113+
I0713 09:02:38.808928 24792 resnet50_test.cc:89] 500 : 8.76192e-29
114+
I0713 09:02:38.808936 24792 resnet50_test.cc:89] 600 : 1.05766e-19
115+
I0713 09:02:38.808945 24792 resnet50_test.cc:89] 700 : 2.04093e-23
116+
I0713 09:02:38.808954 24792 resnet50_test.cc:89] 800 : 3.85255e-25
117+
I0713 09:02:38.808961 24792 resnet50_test.cc:89] 900 : 8.76192e-29
118+
```
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../cmake/dummy.c.in
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../../cmake/external/gflags.cmake
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../../cmake/external/glog.cmake
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../../cmake/external/gtest.cmake
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../../cmake/external/onednn.cmake
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../../cmake/external/pybind11.cmake
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../cmake/generic.cmake

0 commit comments

Comments
 (0)