Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【Hackthon_4th 180】Support HORIZON BPU Backend for FastDeploy #1822

Merged
merged 17 commits into from
May 6, 2023

Conversation

seyosum
Copy link
Contributor

@seyosum seyosum commented Apr 14, 2023

PR types(PR类型)

Add Horizon backend

Description

1、Add Horizon backend
2、Clas model inference
3、PPYOLOE inference

@seyosum
Copy link
Contributor Author

seyosum commented Apr 17, 2023

@DefTruth 有空可以帮review下嘛

@DefTruth
Copy link
Collaborator

@DefTruth 有空可以帮review下嘛

好的,这两天会整体review一下哈,感谢您的贡献~

@@ -167,6 +168,23 @@ if(ENABLE_RKNPU2_BACKEND)
list(APPEND FASTDEPLOY_LIBS ${RKNPU2_LIB})
endif()

if(ENABLE_HORIZON_BACKEND)
set(DNN_PATH "~/.horizon/ddk/xj3_aarch64/dnn/")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个路径不能是绝对路径,可以参考paddlelite.cmake的处理。不属于系统so的,需要从云端下载,并安装到用户的编译环境。可以打包一下成tgz提供给我们哈

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

系统的package和编译器都打包好了,我怎么发你呢

Copy link
Collaborator

@DefTruth DefTruth Apr 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@seyosum 贴个下载链接到这个评论即可

@@ -167,6 +168,23 @@ if(ENABLE_RKNPU2_BACKEND)
list(APPEND FASTDEPLOY_LIBS ${RKNPU2_LIB})
endif()

if(ENABLE_HORIZON_BACKEND)
set(DNN_PATH "~/.horizon/ddk/xj3_aarch64/dnn/")
set(APPSDK_PATH "~/.horizon/ddk/xj3_aarch64/appsdk/appuser/")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上

@@ -0,0 +1,12 @@
set(DNN_PATH "~/.horizon/ddk/xj3_aarch64/dnn/")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上,不能是绝对路径,请参考paddlelite.cmake的处理

set(APPSDK_LIB_PATH ${APPSDK_PATH}/lib/hbbpu)
set(BPU_libs dnn cnn_intf hbrt_bernoulli_aarch64)

include_directories(${DNN_PATH}/include
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里需要使用 ExternalProject_Add 的方式进行下载和配置

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这块目前我还没搞清楚ExternalProject_Add这个的用法,之前没用过😂😂😂,我了解下之后更新吧

set(CMAKE_CXX_STANDARD 14)

# 指定下载解压后的fastdeploy库路径
set(FASTDEPLOY_INSTALL_DIR "/home/xuhao/Hackathon/FastDeploy/build/fastdeploy-0.0.0")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个路径不能hardcode,参考其他examples的写法,可以让用户设置

@@ -110,6 +118,9 @@ std::vector<Backend> GetAvailableBackends() {
#ifdef ENABLE_RKNPU2_BACKEND
backends.push_back(Backend::RKNPU2);
#endif
#ifdef ENABLE_HORIZON_BACKEND
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个宏也需要在fastdeploy/core/config.h.in中增加下,可参考已有写法。

}
bool HorizonBackend::Init(const RuntimeOption& runtime_option){
// Init model from file
if (!this->LoadModel((char*)runtime_option.model_file.data())) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FastDeploy统一不使用this关键字

}

int ret = -1;
if(!this->infer_init){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

类私有成员使用下划线结尾

input_mems_ = (hbDNNTensor*)malloc(sizeof(hbDNNTensor) * input_count);
output_mems_ = (hbDNNTensor*)malloc(sizeof(hbDNNTensor) * output_count);

for(uint32_t i = 0; i < input_count; i++){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以参考Paddle Lite后端中的写法,增加推理benchmark宏,以支持benchmark功能,对推理块进行包裹。

RUNTIME_PROFILE_LOOP_BEGIN(1)

更详细的说明,还可以参考这个PR:

编译带benchmark功能的SDK,需要-DENABLE_BENCHMARK=ON

@@ -65,6 +65,10 @@ void RuntimeOption::UseRKNPU2(fastdeploy::rknpu2::CpuName rknpu2_name,
device = Device::RKNPU;
}

void RuntimeOption::UseHorizon(){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UseHorizon()是指硬件的含义?是否应该提供UseHorizonNPUBackend() API设置backend=Backend::HORIZONNPU?可以参考Sophgo/RKNPU2的写法。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UseHorizon()这块指的是使用地平线backends,我理解之后会有不同的芯片,目前还没有改这部分,之后如果有新增的话可以考虑加参数。

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果是使用具体的推理后端,需要提供类似UseHorizonNPUBackend()来明确语义,Backend强调的是推理后端,比如UseCpu(), UseGpu(), UseKunlunXin()这些则是强调硬件(CPU/GPU/昆仑芯XPU等),强调硬件的接口会执行一些和硬件相关的初始化配置,比如UseGpu(), UseKunlunXin()需要指定device_id,UseKunlunXin()还需要指定L3 Cache size等。带Backend后缀的接口,则强调的是使用哪个推理后端,比如UsePaddleInferBackend(), UsePaddleLiteBackend()等。硬件配置 + 后端配置,形成一个完整的推理条件组合。举个例子:

// CPU + Paddle Inference
option.UseCpu();
option.UsePaddleInferBackend();
// CPU + OpenVINO
option.UseCpu();
option.UseOpenVINOBackend();
// GPU + Paddle Inference
option.UseGpu(); // device_id默认为0
option.UsePaddleInferBackend();

因此建议把硬件API和推理后端API区分开来,在地平线:

option.UseHorizon();  // 一般理解这个指定是硬件
option.UseHorizonNPUBackend(); // 指的是使用哪个推理后端

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这块我大致理解了,增加了这部分内容,infer demo也同步改啦

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@seyosum LGTM ~

set(APPSDK_LIB_PATH ${APPSDK_PATH}/lib/hbbpu)
set(BPU_libs dnn cnn_intf hbrt_bernoulli_aarch64)

include_directories(${DNN_PATH}/include
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个include路径,在编译FD SDK后使用,是必须的吗?如果是必须的需要添加到FASTDEPLOY_INCS中,不是单独include_directories,参考:

list(APPEND FASTDEPLOY_INCS ${LITE_DIR}/include)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个是非必须的,我改下这块

model.GetPreprocessor().DisableNormalize();



Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

代码风格,需要先安装pre-commit check一下。

cd FastDeploy  
sh ./commit-prepare.sh 
# then commit your codes

return;
}
// print res
// std::cout << res.Str() << std::endl;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

删除无用代码

model.GetPostprocessor().ApplyNMS();
fastdeploy::vision::DetectionResult res;
auto im = cv::imread(image_file);
for(int i = 0; i < 100; i++){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

案例中不需要保留测速代码

} else if (runtime_option.device == Device::TIMVX) {
} else if (runtime_option.device == Device::SUNRISENPU){
return CreateHorizonBackend();
}else if (runtime_option.device == Device::TIMVX) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

代码风格,else if前需要空格

@DefTruth DefTruth changed the title 【Hackthon_4th 180】在FastDeploy中集成地平线推理引擎 【Hackthon_4th 180】Support BPU Backend for FastDeploy May 6, 2023
@DefTruth DefTruth changed the title 【Hackthon_4th 180】Support BPU Backend for FastDeploy 【Hackthon_4th 180】Support HORIZON BPU Backend for FastDeploy May 6, 2023
@DefTruth DefTruth merged commit df8dd3e into PaddlePaddle:develop May 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants