diff --git a/speechx/cmake/paddleinference.cmake b/speechx/cmake/paddleinference.cmake index d1f71f00fe47114b573e33eca1ce8769f5482594..957e423c267e2c6176a4c0fcb4623c8752097e46 100644 --- a/speechx/cmake/paddleinference.cmake +++ b/speechx/cmake/paddleinference.cmake @@ -22,6 +22,7 @@ link_directories("${PADDLE_LIB_THIRD_PARTY_PATH}xxhash/lib") link_directories("${PADDLE_LIB_THIRD_PARTY_PATH}cryptopp/lib") link_directories("${PADDLE_LIB}/paddle/lib") link_directories("${PADDLE_LIB_THIRD_PARTY_PATH}mklml/lib") +link_directories("${PADDLE_LIB_THIRD_PARTY_PATH}mkldnn/lib") ##paddle with mkl set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp") @@ -29,14 +30,16 @@ set(MATH_LIB_PATH "${PADDLE_LIB_THIRD_PARTY_PATH}mklml") include_directories("${MATH_LIB_PATH}/include") set(MATH_LIB ${MATH_LIB_PATH}/lib/libmklml_intel${CMAKE_SHARED_LIBRARY_SUFFIX} ${MATH_LIB_PATH}/lib/libiomp5${CMAKE_SHARED_LIBRARY_SUFFIX}) + set(MKLDNN_PATH "${PADDLE_LIB_THIRD_PARTY_PATH}mkldnn") include_directories("${MKLDNN_PATH}/include") set(MKLDNN_LIB ${MKLDNN_PATH}/lib/libmkldnn.so.0) set(EXTERNAL_LIB "-lrt -ldl -lpthread") # global vars -set(DEPS ${PADDLE_LIB}/paddle/lib/libpaddle_inference${CMAKE_SHARED_LIBRARY_SUFFIX} CACHE FORCE "DEPS") +set(DEPS ${PADDLE_LIB}/paddle/lib/libpaddle_inference${CMAKE_SHARED_LIBRARY_SUFFIX} CACHE INTERNAL "deps") set(DEPS ${DEPS} ${MATH_LIB} ${MKLDNN_LIB} glog gflags protobuf xxhash cryptopp - ${EXTERNAL_LIB} CACHE FORCE "DEPS") + ${EXTERNAL_LIB} CACHE INTERNAL "deps") +message(STATUS "Deps libraries: ${DEPS}") diff --git a/speechx/speechx/base/common.h b/speechx/speechx/base/common.h index a9303cbbc41f8f0cc61ef3a81a524c0ca63c193b..778c06d76194bf136f01f2097a5f9b1cf3703b9c 100644 --- a/speechx/speechx/base/common.h +++ b/speechx/speechx/base/common.h @@ -38,3 +38,6 @@ #include "base/flags.h" #include "base/log.h" #include "base/macros.h" + +#include "utils/file_utils.h" +#include "utils/math.h" \ No newline at end of file diff --git a/speechx/speechx/base/macros.h b/speechx/speechx/base/macros.h index d7d5a78d1024b83da3fdc310a3b6383e952c9aef..14332a8065368462a7aac3881137511c7fc8274b 100644 --- a/speechx/speechx/base/macros.h +++ b/speechx/speechx/base/macros.h @@ -14,6 +14,9 @@ #pragma once +#include +#include + namespace ppspeech { #ifndef DISALLOW_COPY_AND_ASSIGN @@ -22,4 +25,8 @@ namespace ppspeech { void operator=(const TypeName&) = delete #endif -} // namespace pp_speech \ No newline at end of file +constexpr float kFloatMax = std::numeric_limits::max(); + +const std::string kSpaceSymbol = "\xe2\x96\x81"; + +} // namespace ppspeech diff --git a/speechx/speechx/decoder/CMakeLists.txt b/speechx/speechx/decoder/CMakeLists.txt index 1df935112e641abe6deaafb8f57b2cb635075c00..0383c3ea0d64ee595d0a3b5795725366565b8f5e 100644 --- a/speechx/speechx/decoder/CMakeLists.txt +++ b/speechx/speechx/decoder/CMakeLists.txt @@ -18,6 +18,7 @@ set(BINS tlg_decoder_main ) +message(STATUS "xxxxxxxxxx: " ${DEPS}) 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) diff --git a/speechx/speechx/decoder/param.h b/speechx/speechx/decoder/param.h index d6ee2705894bac7d396c1e4228adb9fae421656a..ed895aedafe20e0a5e1c8c67a1d5ebde6004cd6b 100644 --- a/speechx/speechx/decoder/param.h +++ b/speechx/speechx/decoder/param.h @@ -56,7 +56,9 @@ DEFINE_int32(max_active, 7500, "max active"); DEFINE_double(beam, 15.0, "decoder beam"); DEFINE_double(lattice_beam, 7.5, "decoder beam"); + namespace ppspeech { + // todo refactor later FeaturePipelineOptions InitFeaturePipelineOptions() { FeaturePipelineOptions opts; @@ -115,4 +117,5 @@ RecognizerResource InitRecognizerResoure() { resource.tlg_opts = InitDecoderOptions(); return resource; } -} + +} // namespace ppspeech diff --git a/speechx/speechx/utils/math.cc b/speechx/speechx/utils/math.cc new file mode 100644 index 0000000000000000000000000000000000000000..fe5c7118958ec7ee92e5110e5fc1777c73285223 --- /dev/null +++ b/speechx/speechx/utils/math.cc @@ -0,0 +1,82 @@ + +// 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. + +#include "utils/math.h" + +#include "base/common.h" + +#include +#include +#include +#include + + +namespace ppspeech { + +// Sum in log scale +float LogSumExp(float x, float y) { + if (x <= -kFloatMax) return y; + if (y <= -kFloatMax) return x; + float max = std::max(x, y); + return max + std::log(std::exp(x - max) + std::exp(y - max)); +} + +// greater compare for smallest priority_queue +template +struct ValGreaterComp { + bool operator()(const std::pair& lhs, const std::pair& rhs) const { + return lhs.first > rhs.first || (lhs.first == rhs.first && lhs.second < rhs.second); + } +} + +template +void TopK(const std::vector& data, int32_t k, std::vector* values, std::vector* indices) { + int n = data.size(); + int min_k_n = std::min(k, n); + + // smallest heap, (val, idx) + std::vector> smallest_heap; + for (int i = 0; i < min_k_n; i++){ + smallest_heap.emplace_back(data[i], i); + } + + // smallest priority_queue + std::priority_queue, std::vector>, ValGreaterComp> pq(ValGreaterComp(), std::move(smallest_heap)); + + // top k + for (int i = k ; i < n; i++){ + if (pq.top().first < data[i]){ + pq.pop(); + pq.emplace_back(data[i], i); + } + } + + values->resize(min_k_n); + indices->resize(min_k_n); + + // from largest to samllest + int cur = values->size() - 1; + while(!pq.empty()){ + const auto& item = pq.top(); + pq.pop(); + + (*values)[cur] = item.first; + (*indices)[cur] = item.second; + + cur--; + } +} + +} // namespace ppspeech \ No newline at end of file diff --git a/speechx/speechx/utils/math.h b/speechx/speechx/utils/math.h new file mode 100644 index 0000000000000000000000000000000000000000..452bf089ce3e660b2f2e9f924d520c9e0a3d9ab5 --- /dev/null +++ b/speechx/speechx/utils/math.h @@ -0,0 +1,28 @@ +// 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. + +#pragma once + +#include +#include + +namespace ppspeech { + +// Sum in log scale +float LogSumExp(float x, float y); + +template +void TopK(const std::vector& data, int32_t k, std::vector* values, std::vector* indices); + +} // namespace ppspeech \ No newline at end of file