Skip to content

Commit 0e36759

Browse files
author
panhehe
committed
[runtime/xpu] Support the execution of non-streaming parsing on the Kunlun XPU card #1455
1 parent 89e8d0d commit 0e36759

32 files changed

+3693
-4
lines changed

runtime/core/cmake/xpu.cmake

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
if(NOT WIN32)
2+
string(ASCII 27 Esc)
3+
set(ColourReset "${Esc}[m")
4+
set(ColourBold "${Esc}[1m")
5+
set(Red "${Esc}[31m")
6+
set(Green "${Esc}[32m")
7+
set(Yellow "${Esc}[33m")
8+
set(Blue "${Esc}[34m")
9+
set(Magenta "${Esc}[35m")
10+
set(Cyan "${Esc}[36m")
11+
set(White "${Esc}[37m")
12+
set(BoldRed "${Esc}[1;31m")
13+
set(BoldGreen "${Esc}[1;32m")
14+
set(BoldYellow "${Esc}[1;33m")
15+
set(BoldBlue "${Esc}[1;34m")
16+
set(BoldMagenta "${Esc}[1;35m")
17+
set(BoldCyan "${Esc}[1;36m")
18+
set(BoldWhite "${Esc}[1;37m")
19+
endif()
20+
21+
if(XPU)
22+
set(RUNTIME_XPU_PATH ${CMAKE_CURRENT_SOURCE_DIR})
23+
message(STATUS "RUNTIME_XPU_PATH is ${RUNTIME_XPU_PATH} .\n")
24+
set(XPU_KUNLUN_PATH ${RUNTIME_XPU_PATH}/decoder/xpu_kunlun)
25+
if(NOT DEFINED ENV{XPU_API_PATH})
26+
message(FATAL_ERROR "${BoldRed}NO ENV{XPU_API_PATH} in your env. Please set XPU_API_PATH.${ColourReset}\n")
27+
else()
28+
set(XPU_API_PATH $ENV{XPU_API_PATH})
29+
message("set XPU_API_PATH from env_var. Val is $ENV{XPU_API_PATH}.")
30+
endif()
31+
32+
include_directories(${XPU_KUNLUN_PATH}/
33+
${XPU_API_PATH}/output/include ${XPU_API_PATH}/../runtime/include)
34+
link_directories(${XPU_API_PATH}/output/so/ ${XPU_API_PATH}/../runtime/output/so/)
35+
36+
add_definitions(-DUSE_XPU)
37+
endif()

runtime/core/decoder/CMakeLists.txt

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ set(decoder_srcs
77
ctc_endpoint.cc
88
)
99

10-
if(NOT TORCH AND NOT ONNX)
11-
message(FATAL_ERROR "Please build with TORCH or ONNX!!!")
10+
if(NOT TORCH AND NOT ONNX AND NOT XPU)
11+
message(FATAL_ERROR "Please build with TORCH or ONNX or XPU!!!")
1212
endif()
1313
if(TORCH)
1414
list(APPEND decoder_srcs torch_asr_model.cc)
@@ -17,8 +17,23 @@ if(ONNX)
1717
list(APPEND decoder_srcs onnx_asr_model.cc)
1818
endif()
1919

20+
if(XPU)
21+
list(APPEND decoder_srcs xpu_asr_model.cc)
22+
list(APPEND decoder_srcs ./xpu_kunlun/xpu_conformer.cpp)
23+
list(APPEND decoder_srcs ./xpu_kunlun/xpu_util.cpp)
24+
message(STATUS "xpu decoder_srcs is :: ${decoder_srcs} \n")
25+
# compile conformer_test
26+
add_subdirectory(xpu_kunlun)
27+
endif()
28+
2029
add_library(decoder STATIC ${decoder_srcs})
21-
target_link_libraries(decoder PUBLIC kaldi-decoder frontend post_processor utils)
30+
if(XPU)
31+
target_link_libraries(decoder PUBLIC kaldi-decoder frontend
32+
post_processor utils xpuapi xpurt)
33+
else()
34+
target_link_libraries(decoder PUBLIC kaldi-decoder frontend
35+
post_processor utils)
36+
endif()
2237

2338
if(ANDROID)
2439
target_link_libraries(decoder PUBLIC ${PYTORCH_LIBRARY} ${FBJNI_LIBRARY})

runtime/core/decoder/params.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,23 @@
2929
#ifdef USE_TORCH
3030
#include "decoder/torch_asr_model.h"
3131
#endif
32+
#ifdef USE_XPU
33+
#include "decoder/xpu_asr_model.h"
34+
#endif
3235
#include "frontend/feature_pipeline.h"
3336
#include "post_processor/post_processor.h"
3437
#include "utils/flags.h"
3538
#include "utils/string.h"
3639

3740
DEFINE_int32(num_threads, 1, "num threads for ASR model");
41+
DEFINE_int32(device_id, 0, "set XPU DeviceID for ASR model");
3842

3943
// TorchAsrModel flags
4044
DEFINE_string(model_path, "", "pytorch exported model path");
4145
// OnnxAsrModel flags
4246
DEFINE_string(onnx_dir, "", "directory where the onnx model is saved");
47+
// XPUAsrModel flags
48+
DEFINE_string(xpu_model_dir, "", "directory where the XPU model and weights is saved");
4349

4450
// FeaturePipelineConfig flags
4551
DEFINE_int32(num_bins, 80, "num mel bins for fbank feature");
@@ -130,7 +136,7 @@ std::shared_ptr<DecodeResource> InitDecodeResourceFromFlags() {
130136
#else
131137
LOG(FATAL) << "Please rebuild with cmake options '-DONNX=ON'.";
132138
#endif
133-
} else {
139+
} else if (!FLAGS_model_path.empty()) {
134140
#ifdef USE_TORCH
135141
LOG(INFO) << "Reading torch model " << FLAGS_model_path;
136142
TorchAsrModel::InitEngineThreads(FLAGS_num_threads);
@@ -140,6 +146,19 @@ std::shared_ptr<DecodeResource> InitDecodeResourceFromFlags() {
140146
#else
141147
LOG(FATAL) << "Please rebuild with cmake options '-DTORCH=ON'.";
142148
#endif
149+
} else if(!FLAGS_xpu_model_dir.empty()) {
150+
#ifdef USE_XPU
151+
LOG(INFO) << "Reading XPU WeNet model weight from " << FLAGS_xpu_model_dir;
152+
auto model = std::make_shared<XPUAsrModel>();
153+
model->SetEngineThreads(FLAGS_num_threads);
154+
model->SetDeviceId(FLAGS_device_id);
155+
model->Read(FLAGS_xpu_model_dir);
156+
resource->model = model;
157+
#else
158+
LOG(FATAL) << "Please rebuild with cmake options '-DXPU=ON'.";
159+
#endif
160+
} else {
161+
LOG(FATAL) << "Please set ONNX, TORCH or XPU model path!!!";
143162
}
144163

145164
LOG(INFO) << "Reading unit table " << FLAGS_unit_path;
@@ -186,6 +205,7 @@ std::shared_ptr<DecodeResource> InitDecodeResourceFromFlags() {
186205
post_process_opts.lowercase = FLAGS_lowercase;
187206
resource->post_processor =
188207
std::make_shared<PostProcessor>(std::move(post_process_opts));
208+
LOG(INFO) << "Finish set PostProcessOptions. \n";
189209
return resource;
190210
}
191211

0 commit comments

Comments
 (0)