diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 015a1088d..5d8469fbd 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -16,11 +16,14 @@ if(NOT CMAKE_BUILD_TYPE) FORCE) endif() -project(paddlespeech VERSION 0.1) - -set(CMAKE_VERBOSE_MAKEFILE on) +project(paddlespeech VERSION 0.1) +# if(ANDROID) +# # when cross compile with ndk under linux, +# # UNIX and ANROID are all True +# set(UNIX) +# endif() include(FetchContent) include(ExternalProject) @@ -30,12 +33,12 @@ set(FETCHCONTENT_QUIET off) get_filename_component(fc_patch "fc_patch" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}") set(FETCHCONTENT_BASE_DIR ${fc_patch}) -set(CMAKE_CXX_FLAGS) -set(CMAKE_CXX_FLAGS_DEBUG) -set(CMAKE_CXX_FLAGS_RELEASE) +set(CMAKE_VERBOSE_MAKEFILE ON) +set(PPS_CXX_STANDARD 14) # set std-14 -set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD ${PPS_CXX_STANDARD}) +add_compile_options(-fPIC) # compiler option # Keep the same with openfst, -fPIC or -fpic @@ -43,8 +46,6 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --std=c++14 -pthread -fPIC -O0 -Wall -g SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} --std=c++14 -pthread -fPIC -O0 -Wall -g -ggdb") SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} --std=c++14 -pthread -fPIC -O3 -Wall") - -add_compile_options(-fPIC) ############################################################################### # Option Configurations ############################################################################### @@ -52,25 +53,25 @@ option(WITH_ASR "build asr" ON) option(WITH_CLS "build cls" ON) option(WITH_VAD "build vad" ON) -option(TEST_DEBUG "option for debug" OFF) -option(USE_PROFILING "enable c++ profling" OFF) -option(WITH_TESTING "unit test" ON) +option(WITH_GPU "NNet using GPU." OFF) -option(USING_GPU "u2 compute on GPU." OFF) +option(WITH_PROFILING "enable c++ profling" OFF) +option(WITH_TESTING "unit test" ON) ############################################################################### # Include Third Party ############################################################################### include(gflags) + include(glog) +add_dependencies(glog gflags) # gtest if(WITH_TESTING) include(gtest) # download, build, install gtest endif() - # fastdeploy include(fastdeploy) @@ -161,15 +162,11 @@ if(WITH_ASR) message(STATUS PADDLE_LIB_DIRS= ${PADDLE_LIB_DIRS}) endif() +include(summary) ############################################################################### # Add local library ############################################################################### set(ENGINE_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/engine) -message(STATUS "CMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}") -message(STATUS "CMAKE_CXX_FLAGS_DEBUG=${CMAKE_CXX_FLAGS_DEBUG}") -message(STATUS "CMAKE_CXX_FLAGS_RELEASE=${CMAKE_CXX_FLAGS_RELEASE}") - - add_subdirectory(engine) diff --git a/runtime/build_android.sh b/runtime/build_android.sh new file mode 100755 index 000000000..64a337627 --- /dev/null +++ b/runtime/build_android.sh @@ -0,0 +1,36 @@ +#!/bin/bash + +set -ex + +ANDROID_NDK=/workspace/zhanghui/android-sdk/android-ndk-r25c + +# Setting up Android toolchanin +ANDROID_ABI=arm64-v8a # 'arm64-v8a', 'armeabi-v7a' +ANDROID_PLATFORM="android-21" # API >= 21 +ANDROID_STL=c++_shared # 'c++_shared', 'c++_static' +ANDROID_TOOLCHAIN=clang # 'clang' only +TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake + +# Create build directory +BUILD_ROOT=build/Android +BUILD_DIR=${BUILD_ROOT}/${ANDROID_ABI}-api-21 +#FASDEPLOY_INSTALL_DIR="${BUILD_DIR}/install" +#mkdir build && mkdir ${BUILD_ROOT} && mkdir ${BUILD_DIR} +mkdir -p ${BUILD_DIR} +cd ${BUILD_DIR} + +# CMake configuration with Android toolchain +cmake -DCMAKE_TOOLCHAIN_FILE=${TOOLCHAIN_FILE} \ + -DCMAKE_BUILD_TYPE=MinSizeRel \ + -DANDROID_ABI=${ANDROID_ABI} \ + -DANDROID_NDK=${ANDROID_NDK} \ + -DANDROID_PLATFORM=${ANDROID_PLATFORM} \ + -DANDROID_STL=${ANDROID_STL} \ + -DANDROID_TOOLCHAIN=${ANDROID_TOOLCHAIN} \ + -DWITH_ASR=OFF \ + -DWITH_CLS=OFF \ + -Wno-dev ../../.. + #-DFASTDEPLOY_INSTALL_DIR=${FASTDEPLOY_INSTALL_DIR} \ + +# Build FastDeploy Android C++ SDK +make diff --git a/runtime/cmake/fastdeploy.cmake b/runtime/cmake/fastdeploy.cmake index cb9ceacda..463a8e8e8 100644 --- a/runtime/cmake/fastdeploy.cmake +++ b/runtime/cmake/fastdeploy.cmake @@ -1,44 +1,42 @@ -cmake_minimum_required(VERSION 3.14 FATAL_ERROR) - set(ARCH "mserver_x86_64" CACHE STRING "Target Architecture: android_arm, android_armv7, android_armv8, android_x86, android_x86_64, mserver_x86_64, ubuntu_x86_64, ios_armv7, ios_armv7s, ios_armv8, ios_x86_64, ios_x86, windows_x86") -set(CMAKE_VERBOSE_MAKEFILE ON) - set(FASTDEPLOY_DIR ${CMAKE_SOURCE_DIR}/fc_patch/fastdeploy) if(NOT EXISTS ${FASTDEPLOY_DIR}/fastdeploy-linux-x64-1.0.4.tgz) exec_program("mkdir -p ${FASTDEPLOY_DIR} && - wget https://bj.bcebos.com/fastdeploy/release/cpp/fastdeploy-linux-x64-1.0.4.tgz -P ${FASTDEPLOY_DIR} && + wget -c https://bj.bcebos.com/fastdeploy/release/cpp/fastdeploy-linux-x64-1.0.4.tgz -P ${FASTDEPLOY_DIR} && tar xzvf ${FASTDEPLOY_DIR}/fastdeploy-linux-x64-1.0.4.tgz -C ${FASTDEPLOY_DIR} && mv ${FASTDEPLOY_DIR}/fastdeploy-linux-x64-1.0.4 ${FASTDEPLOY_DIR}/linux-x64") endif() -if(NOT EXISTS ${FASTDEPLOY_DIR}/fastdeploy-android-1.0.0-shared.tgz) +if(NOT EXISTS ${FASTDEPLOY_DIR}/fastdeploy-android-1.0.4-shared.tgz) exec_program("mkdir -p ${FASTDEPLOY_DIR} && - wget https://bj.bcebos.com/fastdeploy/release/android/fastdeploy-android-1.0.0-shared.tgz -P ${FASTDEPLOY_DIR} && - tar xzvf ${FASTDEPLOY_DIR}/fastdeploy-android-1.0.0-shared.tgz -C ${FASTDEPLOY_DIR} && - mv ${FASTDEPLOY_DIR}/fastdeploy-android-1.0.0-shared ${FASTDEPLOY_DIR}/android-armv7v8") + wget -c https://bj.bcebos.com/fastdeploy/release/android/fastdeploy-android-1.0.4-shared.tgz -P ${FASTDEPLOY_DIR} && + tar xzvf ${FASTDEPLOY_DIR}/fastdeploy-android-1.0.4-shared.tgz -C ${FASTDEPLOY_DIR} && + mv ${FASTDEPLOY_DIR}/fastdeploy-android-1.0.4-shared ${FASTDEPLOY_DIR}/android-armv7v8") endif() -if (ARCH STREQUAL "mserver_x86_64") + +if(ANDROID) + set(FASTDEPLOY_INSTALL_DIR ${FASTDEPLOY_DIR}/android-armv7v8) + add_definitions("-DUSE_PADDLE_LITE_BAKEND") + set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -mfloat-abi=softfp -mfpu=vfpv3 -mfpu=neon -fPIC -pie -fPIE") + set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -g0 -O3 -mfloat-abi=softfp -mfpu=vfpv3 -mfpu=neon -fPIC -pie -fPIE") +elseif(UNIX) set(FASTDEPLOY_INSTALL_DIR ${FASTDEPLOY_DIR}/linux-x64) add_definitions("-DUSE_PADDLE_INFERENCE_BACKEND") # add_definitions("-DUSE_ORT_BACKEND") set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -msse -msse2") set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -msse -msse2 -mavx -O3") -elseif (ARCH STREQUAL "android_armv7") - set(FASTDEPLOY_INSTALL_DIR ${FASTDEPLOY_DIR}/android-armv7v8) - add_definitions("-DUSE_PADDLE_LITE_BAKEND") - set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -mfloat-abi=softfp -mfpu=vfpv3 -mfpu=neon -fPIC -pie -fPIE") - set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -g0 -O3 -mfloat-abi=softfp -mfpu=vfpv3 -mfpu=neon -fPIC -pie -fPIE") endif() +message(STATUS "FASTDEPLOY_INSTALL_DIR=${FASTDEPLOY_INSTALL_DIR} ${UNIX}") include(${FASTDEPLOY_INSTALL_DIR}/FastDeploy.cmake) # fix compiler flags conflict, since fastdeploy using c++11 for project -set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD ${PPS_CXX_STANDARD}) include_directories(${FASTDEPLOY_INCS}) message(STATUS "FASTDEPLOY_INCS=${FASTDEPLOY_INCS}") \ No newline at end of file diff --git a/runtime/cmake/glog.cmake b/runtime/cmake/glog.cmake index 8cc9999b1..d45226181 100644 --- a/runtime/cmake/glog.cmake +++ b/runtime/cmake/glog.cmake @@ -1,8 +1,20 @@ include(FetchContent) -FetchContent_Declare( - glog - URL https://paddleaudio.bj.bcebos.com/build/glog-0.4.0.zip - URL_HASH SHA256=9e1b54eb2782f53cd8af107ecf08d2ab64b8d0dc2b7f5594472f3bd63ca85cdc -) -FetchContent_MakeAvailable(glog) -include_directories(${glog_BINARY_DIR} ${glog_SOURCE_DIR}/src) + +if(ANDROID) +else() # UNIX + add_definitions(-DWITH_GLOG) + FetchContent_Declare( + extern_glog + URL https://paddleaudio.bj.bcebos.com/build/glog-0.4.0.zip + URL_HASH SHA256=9e1b54eb2782f53cd8af107ecf08d2ab64b8d0dc2b7f5594472f3bd63ca85cdc + ) + FetchContent_MakeAvailable(extern_glog) + include_directories(${glog_BINARY_DIR} ${glog_SOURCE_DIR}/src) +endif() + +add_library(glog INTERFACE) + +if(ANDROID) +else() # UNIX + add_dependencies(gtest extern_glog) +endif() \ No newline at end of file diff --git a/runtime/cmake/gtest.cmake b/runtime/cmake/gtest.cmake index f3e72d266..53336f101 100644 --- a/runtime/cmake/gtest.cmake +++ b/runtime/cmake/gtest.cmake @@ -1,14 +1,24 @@ include(FetchContent) -FetchContent_Declare( - gtest - URL https://paddleaudio.bj.bcebos.com/build/gtest-release-1.11.0.zip - URL_HASH SHA256=353571c2440176ded91c2de6d6cd88ddd41401d14692ec1f99e35d013feda55a -) -FetchContent_MakeAvailable(gtest) -include_directories(${gtest_BINARY_DIR} ${gtest_SOURCE_DIR}/src) +if(ANDROID) +else() # UNIX + FetchContent_Declare( + extern_gtest + URL https://paddleaudio.bj.bcebos.com/build/gtest-release-1.11.0.zip + URL_HASH SHA256=353571c2440176ded91c2de6d6cd88ddd41401d14692ec1f99e35d013feda55a + ) + FetchContent_MakeAvailable(extern_gtest) + include_directories(${gtest_BINARY_DIR} ${gtest_SOURCE_DIR}/src) +endif() + +add_library(gtest INTERFACE) + +if(ANDROID) +else() # UNIX + add_dependencies(gtest extern_gtest) +endif() if(WITH_TESTING) enable_testing() diff --git a/runtime/cmake/summary.cmake b/runtime/cmake/summary.cmake new file mode 100644 index 000000000..511c83974 --- /dev/null +++ b/runtime/cmake/summary.cmake @@ -0,0 +1,56 @@ +# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +function(pps_summary) + message(STATUS "") + message(STATUS "*************PaddleSpeech Building Summary**********") + message(STATUS " CMake version : ${CMAKE_VERSION}") + message(STATUS " CMake command : ${CMAKE_COMMAND}") + message(STATUS " System : ${CMAKE_SYSTEM_NAME}") + message(STATUS " C++ compiler : ${CMAKE_CXX_COMPILER}") + message(STATUS " C++ compiler version : ${CMAKE_CXX_COMPILER_VERSION}") + message(STATUS " CXX flags : ${CMAKE_CXX_FLAGS}") + message(STATUS " Build type : ${CMAKE_BUILD_TYPE}") + get_directory_property(tmp DIRECTORY ${PROJECT_SOURCE_DIR} COMPILE_DEFINITIONS) + message(STATUS " Compile definitions : ${tmp}") + message(STATUS " CMAKE_PREFIX_PATH : ${CMAKE_PREFIX_PATH}") + message(STATUS " CMAKE_INSTALL_PREFIX : ${CMAKE_INSTALL_PREFIX}") + message(STATUS " CMAKE_MODULE_PATH : ${CMAKE_MODULE_PATH}") + message(STATUS " CMAKE_SYSTEM_NAME : ${CMAKE_SYSTEM_NAME}") + message(STATUS "") + + message(STATUS " WITH_ASR : ${WITH_ASR}") + message(STATUS " WITH_CLS : ${WITH_CLS}") + message(STATUS " WITH_VAD : ${WITH_VAD}") + message(STATUS " WITH_GPU : ${WITH_GPU}") + message(STATUS " WITH_TESTING : ${WITH_TESTING}") + message(STATUS " WITH_PROFILING : ${WITH_PROFILING}") + message(STATUS " FASTDEPLOY_INSTALL_DIR : ${FASTDEPLOY_INSTALL_DIR}") + if(WITH_GPU) + message(STATUS " CUDA_DIRECTORY : ${CUDA_DIRECTORY}") + endif() + + if(ANDROID) + message(STATUS " ANDROID_ABI : ${ANDROID_ABI}") + message(STATUS " ANDROID_PLATFORM : ${ANDROID_PLATFORM}") + message(STATUS " ANDROID_NDK : ${ANDROID_NDK}") + message(STATUS " ANDROID_NDK_VERSION : ${CMAKE_ANDROID_NDK_VERSION}") + endif() + if (WITH_ASR) + message(STATUS " Python executable : ${PYTHON_EXECUTABLE}") + message(STATUS " Python includes : ${PYTHON_INCLUDE_DIR}") + endif() +endfunction() + +pps_summary() \ No newline at end of file diff --git a/runtime/engine/asr/decoder/ctc_prefix_beam_search_decoder.cc b/runtime/engine/asr/decoder/ctc_prefix_beam_search_decoder.cc index 3e3ca2c20..fda8aab0e 100644 --- a/runtime/engine/asr/decoder/ctc_prefix_beam_search_decoder.cc +++ b/runtime/engine/asr/decoder/ctc_prefix_beam_search_decoder.cc @@ -22,7 +22,7 @@ #include "decoder/ctc_prefix_beam_search_score.h" #include "utils/math.h" -#ifdef USE_PROFILING +#ifdef WITH_PROFILING #include "paddle/fluid/platform/profiler.h" using paddle::platform::RecordEvent; using paddle::platform::TracerEventType; @@ -103,7 +103,7 @@ static bool PrefixScoreCompare( void CTCPrefixBeamSearch::AdvanceDecoding( const std::vector>& logp) { -#ifdef USE_PROFILING +#ifdef WITH_PROFILING RecordEvent event("CtcPrefixBeamSearch::AdvanceDecoding", TracerEventType::UserDefined, 1); diff --git a/runtime/engine/asr/nnet/u2_nnet.cc b/runtime/engine/asr/nnet/u2_nnet.cc index 0795c836a..c2062033d 100644 --- a/runtime/engine/asr/nnet/u2_nnet.cc +++ b/runtime/engine/asr/nnet/u2_nnet.cc @@ -18,11 +18,11 @@ #include "nnet/u2_nnet.h" -#ifdef USE_PROFILING +#ifdef WITH_PROFILING #include "paddle/fluid/platform/profiler.h" using paddle::platform::RecordEvent; using paddle::platform::TracerEventType; -#endif // end USE_PROFILING +#endif // end WITH_PROFILING namespace ppspeech { @@ -30,7 +30,7 @@ namespace ppspeech { void U2Nnet::LoadModel(const std::string& model_path_w_prefix) { paddle::jit::utils::InitKernelSignatureMap(); -#ifdef USE_GPU +#ifdef WITH_GPU dev_ = phi::GPUPlace(); #else dev_ = phi::CPUPlace(); @@ -62,12 +62,12 @@ void U2Nnet::LoadModel(const std::string& model_path_w_prefix) { } void U2Nnet::Warmup() { -#ifdef USE_PROFILING +#ifdef WITH_PROFILING RecordEvent event("warmup", TracerEventType::UserDefined, 1); #endif { -#ifdef USE_PROFILING +#ifdef WITH_PROFILING RecordEvent event( "warmup-encoder-ctc", TracerEventType::UserDefined, 1); #endif @@ -91,7 +91,7 @@ void U2Nnet::Warmup() { } { -#ifdef USE_PROFILING +#ifdef WITH_PROFILING RecordEvent event("warmup-decoder", TracerEventType::UserDefined, 1); #endif auto hyps = @@ -194,7 +194,7 @@ void U2Nnet::ForwardEncoderChunkImpl( const int32& feat_dim, std::vector* out_prob, int32* vocab_dim) { -#ifdef USE_PROFILING +#ifdef WITH_PROFILING RecordEvent event( "ForwardEncoderChunkImpl", TracerEventType::UserDefined, 1); #endif @@ -222,7 +222,7 @@ void U2Nnet::ForwardEncoderChunkImpl( VLOG(3) << "feats shape: " << feats.shape()[0] << ", " << feats.shape()[1] << ", " << feats.shape()[2]; -#ifdef TEST_DEBUG +#ifndef NDEBUG { std::stringstream path("feat", std::ios_base::app | std::ios_base::out); path << offset_; @@ -241,7 +241,7 @@ void U2Nnet::ForwardEncoderChunkImpl( #endif // Endocer chunk forward -#ifdef USE_GPU +#ifdef WITH_GPU feats = feats.copy_to(paddle::GPUPlace(), /*blocking*/ false); att_cache_ = att_cache_.copy_to(paddle::GPUPlace()), /*blocking*/ false; cnn_cache_ = cnn_cache_.copy_to(Paddle::GPUPlace(), /*blocking*/ false); @@ -258,7 +258,7 @@ void U2Nnet::ForwardEncoderChunkImpl( std::vector outputs = forward_encoder_chunk_(inputs); CHECK_EQ(outputs.size(), 3); -#ifdef USE_GPU +#ifdef WITH_GPU paddle::Tensor chunk_out = outputs[0].copy_to(paddle::CPUPlace()); att_cache_ = outputs[1].copy_to(paddle::CPUPlace()); cnn_cache_ = outputs[2].copy_to(paddle::CPUPlace()); @@ -268,7 +268,7 @@ void U2Nnet::ForwardEncoderChunkImpl( cnn_cache_ = outputs[2]; #endif -#ifdef TEST_DEBUG +#ifndef NDEBUG { std::stringstream path("encoder_logits", std::ios_base::app | std::ios_base::out); @@ -298,7 +298,7 @@ void U2Nnet::ForwardEncoderChunkImpl( encoder_outs_.push_back(chunk_out); VLOG(2) << "encoder_outs_ size: " << encoder_outs_.size(); -#ifdef TEST_DEBUG +#ifndef NDEBUG { std::stringstream path("encoder_logits_list", std::ios_base::app | std::ios_base::out); @@ -317,7 +317,7 @@ void U2Nnet::ForwardEncoderChunkImpl( } #endif // end TEST_DEBUG -#ifdef USE_GPU +#ifdef WITH_GPU #error "Not implementation." @@ -331,7 +331,7 @@ void U2Nnet::ForwardEncoderChunkImpl( CHECK_EQ(outputs.size(), 1); paddle::Tensor ctc_log_probs = outputs[0]; -#ifdef TEST_DEBUG +#ifndef NDEBUG { std::stringstream path("encoder_logprob", std::ios_base::app | std::ios_base::out); @@ -353,7 +353,7 @@ void U2Nnet::ForwardEncoderChunkImpl( } #endif // end TEST_DEBUG -#endif // end USE_GPU +#endif // end WITH_GPU // Copy to output, (B=1,T,D) std::vector ctc_log_probs_shape = ctc_log_probs.shape(); @@ -370,7 +370,7 @@ void U2Nnet::ForwardEncoderChunkImpl( std::memcpy( out_prob->data(), ctc_log_probs_ptr, T * D * sizeof(kaldi::BaseFloat)); -#ifdef TEST_DEBUG +#ifndef NDEBUG { std::stringstream path("encoder_logits_list_ctc", std::ios_base::app | std::ios_base::out); @@ -419,7 +419,7 @@ float U2Nnet::ComputePathScore(const paddle::Tensor& prob, void U2Nnet::AttentionRescoring(const std::vector>& hyps, float reverse_weight, std::vector* rescoring_score) { -#ifdef USE_PROFILING +#ifdef WITH_PROFILING RecordEvent event("AttentionRescoring", TracerEventType::UserDefined, 1); #endif CHECK(rescoring_score != nullptr); @@ -461,7 +461,7 @@ void U2Nnet::AttentionRescoring(const std::vector>& hyps, } } -#ifdef TEST_DEBUG +#ifndef NDEBUG { std::stringstream path("encoder_logits_concat", std::ios_base::app | std::ios_base::out); @@ -485,7 +485,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(); -#ifdef TEST_DEBUG +#ifndef NDEBUG { std::stringstream path("encoder_out0", std::ios_base::app | std::ios_base::out); @@ -504,7 +504,7 @@ void U2Nnet::AttentionRescoring(const std::vector>& hyps, } #endif // end TEST_DEBUG -#ifdef TEST_DEBUG +#ifndef NDEBUG { std::stringstream path("encoder_out", std::ios_base::app | std::ios_base::out); @@ -535,7 +535,7 @@ void U2Nnet::AttentionRescoring(const std::vector>& hyps, CHECK_EQ(probs_shape[0], num_hyps); CHECK_EQ(probs_shape[1], max_hyps_len); -#ifdef TEST_DEBUG +#ifndef NDEBUG { std::stringstream path("decoder_logprob", std::ios_base::app | std::ios_base::out); @@ -553,7 +553,7 @@ void U2Nnet::AttentionRescoring(const std::vector>& hyps, } #endif // end TEST_DEBUG -#ifdef TEST_DEBUG +#ifndef NDEBUG { std::stringstream path("hyps_lens", std::ios_base::app | std::ios_base::out); @@ -569,7 +569,7 @@ void U2Nnet::AttentionRescoring(const std::vector>& hyps, } #endif // end TEST_DEBUG -#ifdef TEST_DEBUG +#ifndef NDEBUG { std::stringstream path("hyps_tensor", std::ios_base::app | std::ios_base::out); diff --git a/runtime/engine/cls/CMakeLists.txt b/runtime/engine/cls/CMakeLists.txt index 4d5e0cff6..2f3fd22a4 100644 --- a/runtime/engine/cls/CMakeLists.txt +++ b/runtime/engine/cls/CMakeLists.txt @@ -1,7 +1 @@ -project(cls) - -include(fastdeploy) -# add_definitions("-DTEST_DEBUG") -# add_definitions("-DPRINT_TIME") - add_subdirectory(nnet) \ No newline at end of file diff --git a/runtime/engine/cls/nnet/CMakeLists.txt b/runtime/engine/cls/nnet/CMakeLists.txt index b4b761209..27f244345 100644 --- a/runtime/engine/cls/nnet/CMakeLists.txt +++ b/runtime/engine/cls/nnet/CMakeLists.txt @@ -1,8 +1,11 @@ -set(srcs panns_nnet.cc panns_interface.cc) +set(srcs + panns_nnet.cc + panns_interface.cc +) add_library(cls SHARED ${srcs}) -target_link_libraries(cls -static-libstdc++;-Wl,-Bsymbolic ${FASTDEPLOY_LIBS} kaldi-matrix kaldi-base frontend utils) +target_link_libraries(cls INTERFACE -static-libstdc++;-Wl,-Bsymbolic ${FASTDEPLOY_LIBS} kaldi-matrix kaldi-base frontend utils ) set(bin_name panns_nnet_main) add_executable(${bin_name} ${CMAKE_CURRENT_SOURCE_DIR}/${bin_name}.cc) -target_link_libraries(${bin_name} -static-libstdc++;-Wl,-Bsymbolic cls gflags glog) \ No newline at end of file +target_link_libraries(${bin_name} -static-libstdc++;-Wl,-Bsymbolic gflags glog) diff --git a/runtime/engine/cls/nnet/panns_nnet.cc b/runtime/engine/cls/nnet/panns_nnet.cc index 6b8213f63..bd2265d42 100644 --- a/runtime/engine/cls/nnet/panns_nnet.cc +++ b/runtime/engine/cls/nnet/panns_nnet.cc @@ -13,7 +13,7 @@ // limitations under the License. #include "cls/nnet/panns_nnet.h" -#ifdef PRINT_TIME +#ifdef WITH_PROFILING #include "kaldi/base/timer.h" #endif @@ -86,7 +86,7 @@ int ClsNnet::Forward(const char* wav_path, int topk, char* result, int result_max_len) { -#ifdef PRINT_TIME +#ifdef WITH_PROFILING kaldi::Timer timer; timer.Reset(); #endif @@ -105,7 +105,7 @@ int ClsNnet::Forward(const char* wav_path, conf_.wav_normal_, conf_.wav_normal_type_, conf_.wav_norm_mul_factor_); -#ifdef TEST_DEBUG +#ifndef NDEBUG { std::ofstream fp("cls.wavform", std::ios::out); for (int i = 0; i < wavform.size(); ++i) { @@ -114,11 +114,11 @@ int ClsNnet::Forward(const char* wav_path, fp << "\n"; } #endif -#ifdef PRINT_TIME +#ifdef WITH_PROFILING printf("wav read consume: %fs\n", timer.Elapsed()); #endif -#ifdef PRINT_TIME +#ifdef WITH_PROFILING timer.Reset(); #endif @@ -138,7 +138,7 @@ int ClsNnet::Forward(const char* wav_path, feats[i * feat_dim + j] = PowerTodb(feats[i * feat_dim + j]); } } -#ifdef TEST_DEBUG +#ifndef NDEBUG { std::ofstream fp("cls.feat", std::ios::out); for (int i = 0; i < num_frames; ++i) { @@ -149,20 +149,20 @@ int ClsNnet::Forward(const char* wav_path, } } #endif -#ifdef PRINT_TIME +#ifdef WITH_PROFILING printf("extract fbank consume: %fs\n", timer.Elapsed()); #endif // infer std::vector model_out; -#ifdef PRINT_TIME +#ifdef WITH_PROFILING timer.Reset(); #endif ModelForward(feats.data(), num_frames, feat_dim, &model_out); -#ifdef PRINT_TIME +#ifdef WITH_PROFILING printf("fast deploy infer consume: %fs\n", timer.Elapsed()); #endif -#ifdef TEST_DEBUG +#ifndef NDEBUG { std::ofstream fp("cls.logits", std::ios::out); for (int i = 0; i < model_out.size(); ++i) { diff --git a/runtime/engine/codelab/CMakeLists.txt b/runtime/engine/codelab/CMakeLists.txt index c8445fb82..13aa5efb0 100644 --- a/runtime/engine/codelab/CMakeLists.txt +++ b/runtime/engine/codelab/CMakeLists.txt @@ -1,3 +1,6 @@ cmake_minimum_required(VERSION 3.14 FATAL_ERROR) -add_subdirectory(glog) +if(ANDROID) +else() #Unix + add_subdirectory(glog) +endif() \ No newline at end of file diff --git a/runtime/engine/common/CMakeLists.txt b/runtime/engine/common/CMakeLists.txt index 4f399eea8..a2f56f7ff 100644 --- a/runtime/engine/common/CMakeLists.txt +++ b/runtime/engine/common/CMakeLists.txt @@ -2,11 +2,14 @@ include_directories( ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/../ ) -add_subdirectory(utils) add_subdirectory(base) +add_subdirectory(utils) add_subdirectory(matrix) include_directories( ${CMAKE_CURRENT_SOURCE_DIR}/frontend ) add_subdirectory(frontend) + +add_library(common INTERFACE) +add_definitions(common base utils kaldi-matrix frontend) \ No newline at end of file diff --git a/runtime/engine/common/base/CMakeLists.txt b/runtime/engine/common/base/CMakeLists.txt index ab7108748..fce31c72c 100644 --- a/runtime/engine/common/base/CMakeLists.txt +++ b/runtime/engine/common/base/CMakeLists.txt @@ -1,10 +1,20 @@ + + if(WITH_ASR) add_compile_options(-DWITH_ASR) set(PPS_FLAGS_LIB "fst/flags.h") - set(PPS_GLOB_LIB "fst/log.h") else() set(PPS_FLAGS_LIB "gflags/gflags.h") - set(PPS_GLOB_LIB "glog/logging.h") +endif() + +if(ANDROID) + set(PPS_GLOB_LIB "base/log_impl.h") +else() #UNIX + if(WITH_ASR) + set(PPS_GLOB_LIB "fst/log.h") + else() + set(PPS_GLOB_LIB "glog/logging.h") + endif() endif() configure_file( @@ -17,4 +27,16 @@ configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/log.h.in ${CMAKE_CURRENT_SOURCE_DIR}/log.h @ONLY ) -message(STATUS "Generated ${CMAKE_CURRENT_SOURCE_DIR}/log.h") \ No newline at end of file +message(STATUS "Generated ${CMAKE_CURRENT_SOURCE_DIR}/log.h") + + +if(ANDROID) +set(csrc + log_impl.cc + glog_utils.cc +) +else() # UNIX + set(csrc) +endif() + +add_library(base ${csrc}) \ No newline at end of file diff --git a/runtime/engine/common/base/common.h b/runtime/engine/common/base/common.h index d94dc8a89..17560102e 100644 --- a/runtime/engine/common/base/common.h +++ b/runtime/engine/common/base/common.h @@ -50,4 +50,4 @@ #include "base/log.h" #include "base/macros.h" #include "utils/file_utils.h" -#include "utils/math.h" +#include "utils/math.h" \ No newline at end of file diff --git a/runtime/engine/common/base/glog_utils.cc b/runtime/engine/common/base/glog_utils.cc new file mode 100644 index 000000000..bd4f7f9cb --- /dev/null +++ b/runtime/engine/common/base/glog_utils.cc @@ -0,0 +1,12 @@ + +#include "base/glog_utils.h" + +namespace google { + void InitGoogleLogging(const char* name) { + LOG(INFO) << "dummpy InitGoogleLogging."; + } + + void InstallFailureSignalHandler(){ + LOG(INFO) << "dummpy InstallFailureSignalHandler."; + } +} // namespace google diff --git a/runtime/engine/common/base/glog_utils.h b/runtime/engine/common/base/glog_utils.h new file mode 100644 index 000000000..17aebded4 --- /dev/null +++ b/runtime/engine/common/base/glog_utils.h @@ -0,0 +1,9 @@ +#pragma once + +#include "base/common.h" + +namespace google { + void InitGoogleLogging(const char* name); + + void InstallFailureSignalHandler(); +} // namespace google \ No newline at end of file diff --git a/runtime/engine/common/base/log_impl.cc b/runtime/engine/common/base/log_impl.cc new file mode 100644 index 000000000..f7942554c --- /dev/null +++ b/runtime/engine/common/base/log_impl.cc @@ -0,0 +1,91 @@ +#include "base/log.h" + +DEFINE_int32(logtostderr, 0, "logging to stderr"); + +namespace ppspeech { + +static char __progname[] = "paddlespeech"; + +namespace log { + +std::mutex LogMessage::lock_; +std::string LogMessage::s_debug_logfile_(""); +std::string LogMessage::s_info_logfile_(""); +std::string LogMessage::s_warning_logfile_(""); +std::string LogMessage::s_error_logfile_(""); +std::string LogMessage::s_fatal_logfile_(""); + + void LogMessage::get_curr_proc_info(std::string* pid, std::string* proc_name) { + std::stringstream ss; + ss << getpid(); + ss >> *pid; + *proc_name = ::ppspeech::__progname; +} + + LogMessage::LogMessage(const char* file, int line, Severity level, bool verbose, bool out_to_file /* = false */) : level_(level), verbose_(verbose), out_to_file_(out_to_file) { + + if (FLAGS_logtostderr == 0){ + stream_ = std::shared_ptr(&std::cout); + } else if (FLAGS_logtostderr == 1){ + stream_ = std::shared_ptr(&std::cerr); + }else if (out_to_file_){ + // logfile + lock_.lock(); + init(file, line); + } + } + + LogMessage:: ~LogMessage(){ + stream() << std::endl; + + if(out_to_file_){ + lock_.unlock(); + } + + if (level_ == FATAL){ + std::abort(); + } + } + + void LogMessage::init(const char* file, int line) { + time_t t = time(0); + char tmp[100]; + strftime(tmp, sizeof(tmp), "%Y%m%d-%H%M%S", localtime(&t)); + + if (s_info_logfile_.empty()) { + std::string pid; + std::string proc_name; + get_curr_proc_info(&pid, &proc_name); + + s_debug_logfile_ = std::string("log." + proc_name + ".log.DEBUG." + tmp + "." + pid); + s_info_logfile_ = std::string("log." + proc_name + ".log.INFO." + tmp + "." + pid); + s_warning_logfile_ = std::string("log." + proc_name + ".log.WARNING." + tmp + "." + pid); + s_error_logfile_ = std::string("log." + proc_name + ".log.ERROR." + tmp + "." + pid); + s_fatal_logfile_ = std::string("log." + proc_name + ".log.FATAL." + tmp + "." + pid); + } + + std::ofstream ofs; + if (level_ == DEBUG) { + stream_ = std::make_shared(s_debug_logfile_.c_str(), std::ios::out | std::ios::app); + // ofs.open(s_debug_logfile_.c_str(), std::ios::out | std::ios::app); + } else if (level_ == INFO) { + // ofs.open(s_warning_logfile_.c_str(), std::ios::out | std::ios::app); + stream_ = std::make_shared(s_warning_logfile_.c_str(), std::ios::out | std::ios::app); + } else if (level_ == WARNING) { + // ofs.open(s_warning_logfile_.c_str(), std::ios::out | std::ios::app); + stream_ = std::make_shared(s_warning_logfile_.c_str(), std::ios::out | std::ios::app); + } else if (level_ == ERROR) { + // ofs.open(s_error_logfile_.c_str(), std::ios::out | std::ios::app); + stream_ = std::make_shared(s_error_logfile_.c_str(), std::ios::out | std::ios::app); + } else { + // ofs.open(s_fatal_logfile_.c_str(), std::ios::out | std::ios::app); + stream_ = std::make_shared(s_fatal_logfile_.c_str(), std::ios::out | std::ios::app); + } + + // stream_ = &ofs; + + stream() << tmp << " " << file << " line " << line << "; "; + stream() << std::flush; + } +} // namespace log +} // namespace ppspeech \ No newline at end of file diff --git a/runtime/engine/common/base/log_impl.h b/runtime/engine/common/base/log_impl.h new file mode 100644 index 000000000..15315c6ed --- /dev/null +++ b/runtime/engine/common/base/log_impl.h @@ -0,0 +1,155 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// modified from https://github.com/Dounm/dlog +// modified form https://android.googlesource.com/platform/art/+/806defa/src/logging.h + +#pragma once + +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "base/common.h" +#include "base/macros.h" +#include "base/glog_utils.h" + +DECLARE_int32(logtostderr); + +namespace ppspeech{ + +namespace log { + +enum Severity { + DEBUG, + INFO, + WARNING, + ERROR, + FATAL, + NUM_SEVERITIES, +}; + +class LogMessage { + public: + static void get_curr_proc_info(std::string* pid, std::string* proc_name); + + LogMessage(const char* file, int line, Severity level, bool verbose, bool out_to_file = false); + + ~LogMessage(); + + std::ostream& stream() { return *stream_; } + + private: + void init(const char* file, int line); + + private: + std::shared_ptr stream_; + Severity level_; + bool verbose_; + bool out_to_file_; + + static std::mutex lock_; // stream write lock + static std::string s_debug_logfile_; + static std::string s_info_logfile_; + static std::string s_warning_logfile_; + static std::string s_error_logfile_; + static std::string s_fatal_logfile_; + + DISALLOW_COPY_AND_ASSIGN(LogMessage); +}; + + +} //namespace log + +} //namespace ppspeech + + + +#ifndef NDEBUG + #define DLOG_DEBUG ppspeech::log::LogMessage \ + (__FILE__, __LINE__, ppspeech::log::DEBUG, false) +#else + #define DLOG_DEBUG ppspeech::log::LogMessage \ + (__FILE__, __LINE__, ppspeech::log::DEBUG, true) +#endif + +#define DLOG_INFO ppspeech::log::LogMessage \ + (__FILE__, __LINE__, ppspeech::log::INFO, true) +#define DLOG_WARNING ppspeech::log::LogMessage \ + (__FILE__, __LINE__, ppspeech::log::WARNING, true) +#define DLOG_ERROR ppspeech::log::LogMessage \ + (__FILE__, __LINE__, ppspeech::log::ERROR, true) +#define DLOG_FATAL ppspeech::log::LogMessage \ + (__FILE__, __LINE__, ppspeech::log::FATAL, true) + +#define DLOG_0 DLOG_DEBUG +#define DLOG_1 DLOG_INFO +#define DLOG_2 DLOG_WARNING +#define DLOG_3 DLOG_ERROR +#define DLOG_4 DLOG_FATAL + +#define LOG(level) DLOG_##level.stream() + +#define VLOG(verboselevel) LOG(verboselevel) + + +#define CHECK(exp) ppspeech::log::LogMessage \ + (__FILE__, __LINE__, ppspeech::log::FATAL, !(exp)).stream() << "Check Failed: " #exp + +#define CHECK_EQ(x, y) CHECK((x) == (y)) +#define CHECK_NE(x, y) CHECK((x) != (y)) +#define CHECK_LE(x, y) CHECK((x) <= (y)) +#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 +#define DCHECK(x) CHECK(x) +#define DCHECK_EQ(x, y) CHECK_EQ(x, y) +#define DCHECK_NE(x, y) CHECK_NE(x, y) +#define DCHECK_LE(x, y) CHECK_LE(x, y) +#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 +#define DCHECK(condition) \ + while (false) \ + CHECK(condition) +#define DCHECK_EQ(val1, val2) \ + while (false) \ + CHECK_EQ(val1, val2) +#define DCHECK_NE(val1, val2) \ + while (false) \ + CHECK_NE(val1, val2) +#define DCHECK_LE(val1, val2) \ + while (false) \ + CHECK_LE(val1, val2) +#define DCHECK_LT(val1, val2) \ + while (false) \ + CHECK_LT(val1, val2) +#define DCHECK_GE(val1, val2) \ + while (false) \ + CHECK_GE(val1, val2) +#define DCHECK_GT(val1, val2) \ + while (false) \ + CHECK_GT(val1, val2) +#define DCHECK_STREQ(str1, str2) \ + while (false) \ + CHECK_STREQ(str1, str2) +#endif \ No newline at end of file diff --git a/runtime/engine/common/base/macros.h b/runtime/engine/common/base/macros.h index db9898123..860e354e6 100644 --- a/runtime/engine/common/base/macros.h +++ b/runtime/engine/common/base/macros.h @@ -17,14 +17,14 @@ #include #include -namespace ppspeech { - #ifndef DISALLOW_COPY_AND_ASSIGN #define DISALLOW_COPY_AND_ASSIGN(TypeName) \ TypeName(const TypeName&) = delete; \ void operator=(const TypeName&) = delete #endif +namespace ppspeech { + // kSpaceSymbol in UTF-8 is: ▁ const char kSpaceSymbo[] = "\xe2\x96\x81"; diff --git a/runtime/engine/common/frontend/CMakeLists.txt b/runtime/engine/common/frontend/CMakeLists.txt index 617c35e16..f73588af6 100644 --- a/runtime/engine/common/frontend/CMakeLists.txt +++ b/runtime/engine/common/frontend/CMakeLists.txt @@ -24,5 +24,5 @@ set(BINS foreach(bin_name IN LISTS BINS) add_executable(${bin_name} ${CMAKE_CURRENT_SOURCE_DIR}/${bin_name}.cc) target_include_directories(${bin_name} PRIVATE ${SPEECHX_ROOT} ${SPEECHX_ROOT}/kaldi) - target_link_libraries(${bin_name} PUBLIC frontend utils kaldi-util gflags glog) + target_link_libraries(${bin_name} PUBLIC frontend base utils kaldi-util gflags glog) endforeach() diff --git a/runtime/engine/common/frontend/compute_fbank_main.cc b/runtime/engine/common/frontend/compute_fbank_main.cc index e022207d9..354283341 100644 --- a/runtime/engine/common/frontend/compute_fbank_main.cc +++ b/runtime/engine/common/frontend/compute_fbank_main.cc @@ -16,6 +16,7 @@ #include "base/flags.h" #include "base/log.h" +#include "base/log_impl.h" #include "frontend/audio_cache.h" #include "frontend/data_cache.h" #include "frontend/fbank.h" diff --git a/runtime/engine/common/utils/CMakeLists.txt b/runtime/engine/common/utils/CMakeLists.txt index 8589b19a5..eb3c71979 100644 --- a/runtime/engine/common/utils/CMakeLists.txt +++ b/runtime/engine/common/utils/CMakeLists.txt @@ -1,20 +1,27 @@ -add_library(utils + +set(csrc file_utils.cc math.cc strings.cc audio_process.cc ) +add_library(utils ${csrc}) if(WITH_TESTING) enable_testing() - link_libraries(gtest_main gmock) + + if(ANDROID) + else() # UNIX + link_libraries(gtest_main gmock) + + add_executable(strings_test strings_test.cc) + target_link_libraries(strings_test PUBLIC utils) + add_test( + NAME strings_test + COMMAND strings_test + ) + endif() +endif() - add_executable(strings_test strings_test.cc) - target_link_libraries(strings_test PUBLIC utils) - add_test( - NAME strings_test - COMMAND strings_test - ) -endif() \ No newline at end of file diff --git a/runtime/engine/vad/CMakeLists.txt b/runtime/engine/vad/CMakeLists.txt index d13cc407c..75f6200d2 100644 --- a/runtime/engine/vad/CMakeLists.txt +++ b/runtime/engine/vad/CMakeLists.txt @@ -1,18 +1,4 @@ -# set(CMAKE_CXX_STANDARD 11) - -# # 指定下载解压后的fastdeploy库路径 -# set(FASTDEPLOY_INSTALL_DIR "fdlib/fastdeploy-linux-x64-1.0.4" CACHE STRING force) - -# if(NOT EXISTS ${FASTDEPLOY_INSTALL_DIR}) -# message(FATAL_ERROR "Please using cmake -B build -DFASTDEPLOY_INSTALL_DIR=${FASTDEPLOY_INSTALL_DIR}") -# endif() - -# include(${FASTDEPLOY_INSTALL_DIR}/FastDeploy.cmake) - -# # 添加FastDeploy依赖头文件 -# include_directories(${FASTDEPLOY_INCS}) - add_executable(infer_onnx_silero_vad ${CMAKE_CURRENT_SOURCE_DIR}/infer_onnx_silero_vad.cc wav.h vad.cc vad.h) # 添加FastDeploy库依赖 -target_link_libraries(infer_onnx_silero_vad ${FASTDEPLOY_LIBS}) +target_link_libraries(infer_onnx_silero_vad ${FASTDEPLOY_LIBS} gflags glog)