From 591b957b96b15eb4d0cb1cd66da69ce79cfb0a14 Mon Sep 17 00:00:00 2001 From: masimeng1994 <1057010459@qq.com> Date: Thu, 30 Mar 2023 14:15:33 +0800 Subject: [PATCH] [runtime] fix linux && android cmake bug (#3112) * support vad ios compile * support onnx model recognize * add build ios sh --- runtime/CMakeLists.txt | 7 ++++-- runtime/build.sh | 6 ++++- runtime/build_android.sh | 4 ++-- runtime/cmake/glog.cmake | 2 +- runtime/engine/asr/nnet/u2_nnet.cc | 22 +++++++++--------- runtime/engine/cls/nnet/CMakeLists.txt | 2 +- runtime/engine/cls/nnet/panns_nnet.cc | 23 +++++++++---------- runtime/engine/cls/nnet/panns_nnet_main.cc | 4 ++-- runtime/engine/common/base/log_impl.h | 6 ++--- runtime/engine/common/frontend/CMakeLists.txt | 1 + runtime/engine/kaldi/base/kaldi-error.h | 2 +- runtime/engine/vad/interface/CMakeLists.txt | 2 +- runtime/engine/vad/nnet/CMakeLists.txt | 6 ++++- 13 files changed, 49 insertions(+), 38 deletions(-) diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 96f1e34f..cd836d7c 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -60,7 +60,10 @@ set(FETCHCONTENT_BASE_DIR ${fc_patch}) ############################################################################### # https://github.com/google/brotli/pull/655 option(BUILD_SHARED_LIBS "Build shared libraries" ON) -option(NDEBUG "debug option" OFF) +option(WITH_PPS_DEBUG "debug option" OFF) +if (WITH_PPS_DEBUG) + add_definitions("-DPPS_DEBUG") +endif() option(WITH_ASR "build asr" ON) option(WITH_CLS "build cls" ON) @@ -71,7 +74,7 @@ option(WITH_GPU "NNet using GPU." OFF) option(WITH_PROFILING "enable c++ profling" OFF) option(WITH_TESTING "unit test" ON) -option(WITH_ONNX "u2 support onnx runtime" ON) +option(WITH_ONNX "u2 support onnx runtime" OFF) ############################################################################### # Include Third Party diff --git a/runtime/build.sh b/runtime/build.sh index 12cd23ee..68889010 100755 --- a/runtime/build.sh +++ b/runtime/build.sh @@ -9,9 +9,11 @@ mkdir -p ${BUILD_DIR} BUILD_TYPE=Release #BUILD_TYPE=Debug BUILD_SO=OFF +BUILD_ONNX=ON BUILD_ASR=ON BUILD_CLS=ON BUILD_VAD=ON +PPS_DEBUG=OFF FASTDEPLOY_INSTALL_DIR="" # the build script had verified in the paddlepaddle docker image. @@ -21,9 +23,11 @@ FASTDEPLOY_INSTALL_DIR="" cmake -B ${BUILD_DIR} \ -DCMAKE_BUILD_TYPE=${BUILD_TYPE} \ -DBUILD_SHARED_LIBS=${BUILD_SO} \ + -DWITH_ONNX=${BUILD_ONNX} \ -DWITH_ASR=${BUILD_ASR} \ -DWITH_CLS=${BUILD_CLS} \ -DWITH_VAD=${BUILD_VAD} \ - -DFASTDEPLOY_INSTALL_DIR=${FASTDEPLOY_INSTALL_DIR} + -DFASTDEPLOY_INSTALL_DIR=${FASTDEPLOY_INSTALL_DIR} \ + -DWITH_PPS_DEBUG=${PPS_DEBUG} cmake --build ${BUILD_DIR} -j diff --git a/runtime/build_android.sh b/runtime/build_android.sh index ac3980a8..ce78e67c 100755 --- a/runtime/build_android.sh +++ b/runtime/build_android.sh @@ -2,7 +2,7 @@ set -ex -ANDROID_NDK=/workspace/zhanghui/android-sdk/android-ndk-r25c +ANDROID_NDK=/mnt/masimeng/workspace/software/android-ndk-r25b/ # Setting up Android toolchanin ANDROID_ABI=arm64-v8a # 'arm64-v8a', 'armeabi-v7a' @@ -14,7 +14,7 @@ TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake # Create build directory BUILD_ROOT=build/Android BUILD_DIR=${BUILD_ROOT}/${ANDROID_ABI}-api-21 -FASTDEPLOY_INSTALL_DIR="/workspace/zhanghui/paddle/FastDeploy/build/Android/arm64-v8a-api-21/install" +FASTDEPLOY_INSTALL_DIR="/mnt/masimeng/workspace/FastDeploy/build/Android/arm64-v8a-api-21/install" mkdir -p ${BUILD_DIR} cd ${BUILD_DIR} diff --git a/runtime/cmake/glog.cmake b/runtime/cmake/glog.cmake index 91341ca9..6c38963a 100644 --- a/runtime/cmake/glog.cmake +++ b/runtime/cmake/glog.cmake @@ -28,7 +28,7 @@ endif() if(ANDROID) add_library(extern_glog INTERFACE) - add_dependencies(glog gflags) + add_dependencies(extern_glog gflags) else() # UNIX add_library(extern_glog ALIAS glog) add_dependencies(glog gflags) diff --git a/runtime/engine/asr/nnet/u2_nnet.cc b/runtime/engine/asr/nnet/u2_nnet.cc index c03d2610..968b6cee 100644 --- a/runtime/engine/asr/nnet/u2_nnet.cc +++ b/runtime/engine/asr/nnet/u2_nnet.cc @@ -223,7 +223,7 @@ void U2Nnet::ForwardEncoderChunkImpl( VLOG(3) << "feats shape: " << feats.shape()[0] << ", " << feats.shape()[1] << ", " << feats.shape()[2]; -#ifndef NDEBUG +#ifdef PPS_DEBUG { std::stringstream path("feat", std::ios_base::app | std::ios_base::out); path << offset_; @@ -269,7 +269,7 @@ void U2Nnet::ForwardEncoderChunkImpl( cnn_cache_ = outputs[2]; #endif -#ifndef NDEBUG +#ifdef PPS_DEBUG { std::stringstream path("encoder_logits", std::ios_base::app | std::ios_base::out); @@ -299,7 +299,7 @@ void U2Nnet::ForwardEncoderChunkImpl( encoder_outs_.push_back(chunk_out); VLOG(2) << "encoder_outs_ size: " << encoder_outs_.size(); -#ifndef NDEBUG +#ifdef PPS_DEBUG { std::stringstream path("encoder_logits_list", std::ios_base::app | std::ios_base::out); @@ -332,7 +332,7 @@ void U2Nnet::ForwardEncoderChunkImpl( CHECK_EQ(outputs.size(), 1); paddle::Tensor ctc_log_probs = outputs[0]; -#ifndef NDEBUG +#ifdef PPS_DEBUG { std::stringstream path("encoder_logprob", std::ios_base::app | std::ios_base::out); @@ -371,7 +371,7 @@ void U2Nnet::ForwardEncoderChunkImpl( std::memcpy( out_prob->data(), ctc_log_probs_ptr, T * D * sizeof(kaldi::BaseFloat)); -#ifndef NDEBUG +#ifdef PPS_DEBUG { std::stringstream path("encoder_logits_list_ctc", std::ios_base::app | std::ios_base::out); @@ -462,7 +462,7 @@ void U2Nnet::AttentionRescoring(const std::vector>& hyps, } } -#ifndef NDEBUG +#ifdef PPS_DEBUG { std::stringstream path("encoder_logits_concat", std::ios_base::app | std::ios_base::out); @@ -486,7 +486,7 @@ void U2Nnet::AttentionRescoring(const std::vector>& hyps, paddle::Tensor encoder_out = paddle::concat(encoder_outs_, 1); VLOG(2) << "encoder_outs_ size: " << encoder_outs_.size(); -#ifndef NDEBUG +#ifdef PPS_DEBUG { std::stringstream path("encoder_out0", std::ios_base::app | std::ios_base::out); @@ -505,7 +505,7 @@ void U2Nnet::AttentionRescoring(const std::vector>& hyps, } #endif // end TEST_DEBUG -#ifndef NDEBUG +#ifdef PPS_DEBUG { std::stringstream path("encoder_out", std::ios_base::app | std::ios_base::out); @@ -536,7 +536,7 @@ void U2Nnet::AttentionRescoring(const std::vector>& hyps, CHECK_EQ(probs_shape[0], num_hyps); CHECK_EQ(probs_shape[1], max_hyps_len); -#ifndef NDEBUG +#ifdef PPS_DEBUG { std::stringstream path("decoder_logprob", std::ios_base::app | std::ios_base::out); @@ -554,7 +554,7 @@ void U2Nnet::AttentionRescoring(const std::vector>& hyps, } #endif // end TEST_DEBUG -#ifndef NDEBUG +#ifdef PPS_DEBUG { std::stringstream path("hyps_lens", std::ios_base::app | std::ios_base::out); @@ -570,7 +570,7 @@ void U2Nnet::AttentionRescoring(const std::vector>& hyps, } #endif // end TEST_DEBUG -#ifndef NDEBUG +#ifdef PPS_DEBUG { std::stringstream path("hyps_tensor", std::ios_base::app | std::ios_base::out); diff --git a/runtime/engine/cls/nnet/CMakeLists.txt b/runtime/engine/cls/nnet/CMakeLists.txt index 652e05b3..bb7f8eec 100644 --- a/runtime/engine/cls/nnet/CMakeLists.txt +++ b/runtime/engine/cls/nnet/CMakeLists.txt @@ -3,7 +3,7 @@ set(srcs panns_interface.cc ) -add_library(cls ${srcs}) +add_library(cls SHARED ${srcs}) target_link_libraries(cls PRIVATE ${FASTDEPLOY_LIBS} kaldi-matrix kaldi-base frontend utils ) set(bin_name panns_nnet_main) diff --git a/runtime/engine/cls/nnet/panns_nnet.cc b/runtime/engine/cls/nnet/panns_nnet.cc index 2edf71f2..6ab7f7bb 100644 --- a/runtime/engine/cls/nnet/panns_nnet.cc +++ b/runtime/engine/cls/nnet/panns_nnet.cc @@ -49,23 +49,22 @@ int ClsNnet::Init(const ClsNnetConf& conf) { // init model fastdeploy::RuntimeOption runtime_option; -#ifdef USE_ORT_BACKEND +#ifdef USE_PADDLE_INFERENCE_BACKEND + runtime_option.SetModelPath(conf.model_file_path_, + conf.param_file_path_, + fastdeploy::ModelFormat::PADDLE); + runtime_option.UsePaddleInferBackend(); +#elif defined(USE_ORT_BACKEND) runtime_option.SetModelPath( conf.model_file_path_, "", fastdeploy::ModelFormat::ONNX); // onnx runtime_option.UseOrtBackend(); // onnx -#endif -#ifdef USE_PADDLE_LITE_BACKEND +#elif defined(USE_PADDLE_LITE_BACKEND) runtime_option.SetModelPath(conf.model_file_path_, conf.param_file_path_, fastdeploy::ModelFormat::PADDLE); runtime_option.UseLiteBackend(); #endif -#ifdef USE_PADDLE_INFERENCE_BACKEND - runtime_option.SetModelPath(conf.model_file_path_, - conf.param_file_path_, - fastdeploy::ModelFormat::PADDLE); - runtime_option.UsePaddleInferBackend(); -#endif + runtime_option.SetCpuThreadNum(conf.num_cpu_thread_); // runtime_option.DeletePaddleBackendPass("simplify_with_basic_ops_pass"); runtime_ = std::unique_ptr(new fastdeploy::Runtime()); @@ -105,7 +104,7 @@ int ClsNnet::Forward(const char* wav_path, conf_.wav_normal_, conf_.wav_normal_type_, conf_.wav_norm_mul_factor_); -#ifndef NDEBUG +#ifdef PPS_DEBUG { std::ofstream fp("cls.wavform", std::ios::out); for (int i = 0; i < wavform.size(); ++i) { @@ -138,7 +137,7 @@ int ClsNnet::Forward(const char* wav_path, feats[i * feat_dim + j] = PowerTodb(feats[i * feat_dim + j]); } } -#ifndef NDEBUG +#ifdef PPS_DEBUG { std::ofstream fp("cls.feat", std::ios::out); for (int i = 0; i < num_frames; ++i) { @@ -162,7 +161,7 @@ int ClsNnet::Forward(const char* wav_path, #ifdef WITH_PROFILING printf("fast deploy infer consume: %fs\n", timer.Elapsed()); #endif -#ifndef NDEBUG +#ifdef PPS_DEBUG { std::ofstream fp("cls.logits", std::ios::out); for (int i = 0; i < model_out.size(); ++i) { diff --git a/runtime/engine/cls/nnet/panns_nnet_main.cc b/runtime/engine/cls/nnet/panns_nnet_main.cc index 44072082..20f3312c 100644 --- a/runtime/engine/cls/nnet/panns_nnet_main.cc +++ b/runtime/engine/cls/nnet/panns_nnet_main.cc @@ -15,8 +15,8 @@ #include #include -#include "base/flags.h" -#include "base/log.h" +#include "gflags/gflags.h" +#include "glog/logging.h" #include "cls/nnet/panns_interface.h" DEFINE_string(conf_path, "", "config path"); diff --git a/runtime/engine/common/base/log_impl.h b/runtime/engine/common/base/log_impl.h index 2cc96c45..fd6cce19 100644 --- a/runtime/engine/common/base/log_impl.h +++ b/runtime/engine/common/base/log_impl.h @@ -90,7 +90,7 @@ class LogMessage { } // namespace ppspeech -#ifdef NDEBUG +#ifndef PPS_DEBUG #define DLOG_INFO \ ppspeech::log::LogMessage(__FILE__, __LINE__, ppspeech::log::INFO, false) #define DLOG_WARNING \ @@ -145,7 +145,7 @@ class LogMessage { #define CHECK_LT(x, y) CHECK((x) < (y)) #define CHECK_GE(x, y) CHECK((x) >= (y)) #define CHECK_GT(x, y) CHECK((x) > (y)) -#ifndef NDEBUG +#ifdef PPS_DEBUG #define DCHECK(x) CHECK(x) #define DCHECK_EQ(x, y) CHECK_EQ(x, y) #define DCHECK_NE(x, y) CHECK_NE(x, y) @@ -153,7 +153,7 @@ class LogMessage { #define DCHECK_LT(x, y) CHECK_LT(x, y) #define DCHECK_GE(x, y) CHECK_GE(x, y) #define DCHECK_GT(x, y) CHECK_GT(x, y) -#else // NDEBUG +#else #define DCHECK(condition) \ while (false) CHECK(condition) #define DCHECK_EQ(val1, val2) \ diff --git a/runtime/engine/common/frontend/CMakeLists.txt b/runtime/engine/common/frontend/CMakeLists.txt index e63049d3..0b95b650 100644 --- a/runtime/engine/common/frontend/CMakeLists.txt +++ b/runtime/engine/common/frontend/CMakeLists.txt @@ -7,6 +7,7 @@ add_library(kaldi-native-fbank-core rfft.cc ) target_link_libraries(kaldi-native-fbank-core PUBLIC utils base) +target_compile_options(kaldi-native-fbank-core PUBLIC "-fPIC") add_library(frontend STATIC cmvn.cc diff --git a/runtime/engine/kaldi/base/kaldi-error.h b/runtime/engine/kaldi/base/kaldi-error.h index a9904a75..98bef74f 100644 --- a/runtime/engine/kaldi/base/kaldi-error.h +++ b/runtime/engine/kaldi/base/kaldi-error.h @@ -181,7 +181,7 @@ private: // Also see KALDI_COMPILE_TIME_ASSERT, defined in base/kaldi-utils.h, and // KALDI_ASSERT_IS_INTEGER_TYPE and KALDI_ASSERT_IS_FLOATING_TYPE, also defined // there. -#ifndef NDEBUG +#ifdef PPS_DEBUG #define KALDI_ASSERT(cond) \ do { \ if (cond) \ diff --git a/runtime/engine/vad/interface/CMakeLists.txt b/runtime/engine/vad/interface/CMakeLists.txt index 29c3f31a..e056ec39 100644 --- a/runtime/engine/vad/interface/CMakeLists.txt +++ b/runtime/engine/vad/interface/CMakeLists.txt @@ -2,7 +2,7 @@ set(srcs vad_interface.cc ) -add_library(pps_vad_interface ${srcs}) +add_library(pps_vad_interface SHARED ${srcs}) target_link_libraries(pps_vad_interface PUBLIC pps_vad extern_glog) diff --git a/runtime/engine/vad/nnet/CMakeLists.txt b/runtime/engine/vad/nnet/CMakeLists.txt index c1dec533..3ca951d9 100644 --- a/runtime/engine/vad/nnet/CMakeLists.txt +++ b/runtime/engine/vad/nnet/CMakeLists.txt @@ -12,4 +12,8 @@ target_link_libraries(${bin_name} pps_vad) file(RELATIVE_PATH DEST_DIR ${ENGINE_ROOT} ${CMAKE_CURRENT_SOURCE_DIR}) install(TARGETS pps_vad DESTINATION lib) -install(TARGETS glog DESTINATION lib) \ No newline at end of file +if(ANDROID) + install(TARGETS extern_glog DESTINATION lib) +else() # UNIX + install(TARGETS glog DESTINATION lib) +endif() -- GitLab