[runtime] add logging module, build on linux and android, normalize option name (#2986)

* rename option with WITH_; add logging module to replace with glog

* refactor logging module; build pass on linux and andorid
pull/2993/head
Hui Zhang 1 year ago committed by GitHub
parent b35fc01a3a
commit e9da7e0e07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -4,3 +4,4 @@ engine/common/base/log.h
tools/valgrind*
*log
fc_patch/*
test

@ -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,11 +53,10 @@ 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
@ -70,7 +70,6 @@ if(WITH_TESTING)
include(gtest) # download, build, install gtest
endif()
# fastdeploy
include(fastdeploy)
@ -161,15 +160,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)

@ -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

@ -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}")

@ -1,8 +1,21 @@
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(
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)
endif()
if(ANDROID)
add_library(extern_glog INTERFACE)
else() # UNIX
add_dependencies(glog gflags)
add_library(extern_glog ALIAS glog)
endif()

@ -1,14 +1,26 @@
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(
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)
endif()
if(ANDROID)
add_library(extern_gtest INTERFACE)
else() # UNIX
add_dependencies(gtest gflags gflog)
add_library(extern_gtest ALIAS gtest)
endif()
if(WITH_TESTING)
enable_testing()

@ -0,0 +1,58 @@
# 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 " UNIX : ${UNIX}")
message(STATUS " ANDROID : ${ANDROID}")
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()

@ -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<std::vector<kaldi::BaseFloat>>& logp) {
#ifdef USE_PROFILING
#ifdef WITH_PROFILING
RecordEvent event("CtcPrefixBeamSearch::AdvanceDecoding",
TracerEventType::UserDefined,
1);

@ -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<kaldi::BaseFloat>* 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<paddle::Tensor> 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<int64_t> 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<std::vector<int>>& hyps,
float reverse_weight,
std::vector<float>* 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<std::vector<int>>& 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<std::vector<int>>& 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<std::vector<int>>& 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<std::vector<int>>& 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<std::vector<int>>& 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<std::vector<int>>& hyps,
}
#endif // end TEST_DEBUG
#ifdef TEST_DEBUG
#ifndef NDEBUG
{
std::stringstream path("hyps_tensor",
std::ios_base::app | std::ios_base::out);

@ -1,7 +1 @@
project(cls)
include(fastdeploy)
# add_definitions("-DTEST_DEBUG")
# add_definitions("-DPRINT_TIME")
add_subdirectory(nnet)

@ -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)
target_link_libraries(${bin_name} -static-libstdc++;-Wl,-Bsymbolic gflags glog)

@ -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<float> 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) {

@ -1,3 +1,6 @@
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
add_subdirectory(glog)
if(ANDROID)
else() #Unix
add_subdirectory(glog)
endif()

@ -1,8 +1,8 @@
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
add_executable(glog_main ${CMAKE_CURRENT_SOURCE_DIR}/glog_main.cc)
target_link_libraries(glog_main glog)
target_link_libraries(glog_main extern_glog)
add_executable(glog_logtostderr_main ${CMAKE_CURRENT_SOURCE_DIR}/glog_logtostderr_main.cc)
target_link_libraries(glog_logtostderr_main glog)
target_link_libraries(glog_logtostderr_main extern_glog)

@ -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)

@ -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")
message(STATUS "Generated ${CMAKE_CURRENT_SOURCE_DIR}/log.h")
if(ANDROID)
set(csrc
log_impl.cc
glog_utils.cc
)
add_library(base ${csrc})
else() # UNIX
set(csrc)
add_library(base INTERFACE)
endif()

@ -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"

@ -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

@ -0,0 +1,9 @@
#pragma once
#include "base/common.h"
namespace google {
void InitGoogleLogging(const char* name);
void InstallFailureSignalHandler();
} // namespace google

@ -0,0 +1,105 @@
#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::ostream>(&std::cout);
} else if (FLAGS_logtostderr == 1) {
stream_ = std::shared_ptr<std::ostream>(&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<std::ofstream>(
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<std::ofstream>(
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<std::ofstream>(
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<std::ofstream>(
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<std::ofstream>(
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

@ -0,0 +1,156 @@
// 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 <fstream>
#include <iostream>
#include <mutex>
#include <sstream>
#include <string>
#include <thread>
#include <stdlib.h>
#include <unistd.h>
#include "base/common.h"
#include "base/macros.h"
#ifndef WITH_GLOG
#include "base/glog_utils.h"
#endif
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<std::ostream> 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

@ -17,14 +17,14 @@
#include <limits>
#include <string>
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";

@ -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)
endforeach()
target_link_libraries(${bin_name} PUBLIC frontend base utils kaldi-util gflags extern_glog)
endforeach()

@ -584,7 +584,7 @@ template void VectorBase<double>::CopyColFromMat(const MatrixBase<double> &mat,
// Real *data = data_;
//// implement the function according to a dimension cutoff for computation
///efficiency
/// efficiency
// if (num_rows <= 64) {
// cblas_Xscal(dim, beta, data, 1);
// const Real *m_data = M.Data();
@ -605,7 +605,7 @@ template void VectorBase<double>::CopyColFromMat(const MatrixBase<double> &mat,
// MatrixIndexT num_cols = M.NumCols();
//// implement the function according to a dimension cutoff for computation
///efficiency
/// efficiency
// if (num_cols <= 64) {
// for (MatrixIndexT i = 0; i < dim_; i++) {
// double sum = 0.0;
@ -1224,7 +1224,7 @@ void Vector<Real>::Swap(Vector<Real> *other) {
// for (MatrixIndexT i = 0; i < dim; i++, Mdata += M_row_stride, Ndata +=
// N_col_stride, data++) {
//*data = beta * *data + alpha * cblas_Xdot(M_col_dim, Mdata, M_col_stride,
//Ndata, N_row_stride);
// Ndata, N_row_stride);
//}
//}

@ -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()

@ -1,18 +1,5 @@
# 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})
set(bin_name silero_vad_main)
add_executable(${bin_name} ${CMAKE_CURRENT_SOURCE_DIR}/${bin_name}.cc vad.cc)
target_link_libraries(${bin_name} ${FASTDEPLOY_LIBS} gflags extern_glog)

@ -0,0 +1,14 @@
ANDROID_NDK=/workspace/zhanghui/android-sdk/android-ndk-r25c
ANDROID_TOOLCHAIN=clang
FASTDEPLOY_INSTALL_DIR=./fdlib/fastdeploy-android-1.0.3-shared/
TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake
cmake -B build -DCMAKE_TOOLCHAIN_FILE=${TOOLCHAIN_FILE} \
-DCMAKE_BUILD_TYPE=Release \
-DANDROID_ABI="arm64-v8a" \
-DANDROID_NDK=${ANDROID_NDK} \
-DANDROID_PLATFORM="android-21" \
-DANDROID_STL=c++_shared \
-DANDROID_TOOLCHAIN=${ANDROID_TOOLCHAIN} \
-DFASTDEPLOY_INSTALL_DIR=${FASTDEPLOY_INSTALL_DIR} \
-Wno-dev

@ -0,0 +1,31 @@
ANDROID_NDK=/workspace/zhanghui/android-sdk/android-ndk-r25c
FASTDEPLOY_INSTALL_DIR=./fdlib/fastdeploy-android-1.0.4-shared/
# 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} \
-DFASTDEPLOY_INSTALL_DIR=${FASTDEPLOY_INSTALL_DIR} \
-Wno-dev ../../..
# Build FastDeploy Android C++ SDK
make -j8

@ -0,0 +1,10 @@
#!/bin/bash
mkdir -p data
cd data
wget -c https://bj.bcebos.com/paddlehub/fastdeploy/silero_vad.tgz
test -e silero_vad || tar zxvf silero_vad.tgz
wget -c https://bj.bcebos.com/paddlehub/fastdeploy/silero_vad_sample.wav

@ -0,0 +1,18 @@
# This contains the locations of binarys build required for running the examples.
unset GREP_OPTIONS
ENGINE_ROOT=$PWD/../../../
ENGINE_BUILD=$ENGINE_ROOT/build/engine/asr
ENGINE_TOOLS=$ENGINE_ROOT/tools
TOOLS_BIN=$ENGINE_TOOLS/valgrind/install/bin
[ -d $ENGINE_BUILD ] || { echo "Error: 'build/runtime' directory not found. please ensure that the project build successfully"; }
export LC_AL=C
export PATH=$PATH:$TOOLS_BIN:$ENGINE_BUILD/nnet:$ENGINE_BUILD/decoder:$ENGINE_BUILD/../common/frontend/audio:$ENGINE_BUILD/recognizer
#PADDLE_LIB_PATH=$(python -c "import os; import paddle; include_dir=paddle.sysconfig.get_include(); paddle_dir=os.path.split(include_dir)[0]; libs_dir=os.path.join(paddle_dir, 'libs'); fluid_dir=os.path.join(paddle_dir, 'fluid'); out=':'.join([libs_dir, fluid_dir]); print(out);")
export LD_LIBRARY_PATH=$PADDLE_LIB_PATH:$LD_LIBRARY_PATH

@ -0,0 +1,38 @@
#!/bin/bash
set -e
. path.sh
nj=40
stage=-1
stop_stage=100
. utils/parse_options.sh
# input
data=data
exp=exp
mkdir -p $exp $data
# 1. compile
if [ ! -d ${SPEECHX_BUILD} ]; then
pushd ${SPEECHX_ROOT}
bash build.sh
# build for android armv8/armv7
# bash build_android.sh
popd
fi
ckpt_dir=$data/silero_vad
wav=$data/silero_vad_sample.wav
if [ ${stage} -le 0 ] && [ ${stop_stage} -ge 0 ];then
./local/download.sh
fi
if [ ${stage} -le 1 ] && [ ${stop_stage} -ge 1 ]; then
./local/decode.sh
fi
Loading…
Cancel
Save