Open-source speech recognition toolkit for training, inference, streaming ASR, VAD, punctuation, speaker diarization pipelines, and OpenAI-compatible/MCP serving.
Python asraudiochineseemotion-recognitionfunasrmcp-servermultilingual-asropenai-compatible-apiparaformerpunctuationpytorchreal-time-asrspeaker-diarizationspeech-recognitionspeech-to-textstreaming-asrtranscriptionvllmvoice-activity-detectionwhisper-alternative
fix onnxruntime memoryleak when load model (#2108)
locasxec494d8fd18c2
diff --git a/runtime/onnxruntime/src/fsmn-vad.cpp b/runtime/onnxruntime/src/fsmn-vad.cppindex 42ce83b45..e34767009 100644--- a/runtime/onnxruntime/src/fsmn-vad.cpp+++ b/runtime/onnxruntime/src/fsmn-vad.cpp@@ -60,18 +60,19 @@ void FsmnVad::ReadModel(const char* vad_model) {LOG(ERROR) << "Error when load vad onnx model: " << e.what();exit(-1);}- GetInputOutputInfo(vad_session_, &vad_in_names_, &vad_out_names_);+ GetInputOutputInfo(vad_session_, &vad_in_names_, &vad_out_names_, &vad_allocator);}void FsmnVad::GetInputOutputInfo(const std::shared_ptr<Ort::Session> &session,- std::vector<const char *> *in_names, std::vector<const char *> *out_names) {+ std::vector<const char *> *in_names, std::vector<const char *> *out_names,+ Ort::AllocatorWithDefaultOptions *allocator) {Ort::AllocatorWithDefaultOptions allocator;// Input infoint num_nodes = session->GetInputCount();in_names->resize(num_nodes);for (int i = 0; i < num_nodes; ++i) {- std::unique_ptr<char, Ort::detail::AllocatedFree> name = session->GetInputNameAllocated(i, allocator);+ std::unique_ptr<char, Ort::detail::AllocatedFree> name = session->GetInputNameAllocated(i, *allocator);Ort::TypeInfo type_info = session->GetInputTypeInfo(i);auto tensor_info = type_info.GetTensorTypeAndShapeInfo();ONNXTensorElementDataType type = tensor_info.GetElementType();@@ -90,7 +91,7 @@ void FsmnVad::GetInputOutputInfo(num_nodes = session->GetOutputCount();out_names->resize(num_nodes);for (int i = 0; i < num_nodes; ++i) {- std::unique_ptr<char, Ort::detail::AllocatedFree> name = session->GetOutputNameAllocated(i, allocator);+ std::unique_ptr<char, Ort::detail::AllocatedFree> name = session->GetOutputNameAllocated(i, *allocator);Ort::TypeInfo type_info = session->GetOutputTypeInfo(i);auto tensor_info = type_info.GetTensorTypeAndShapeInfo();ONNXTensorElementDataType type = tensor_info.GetElementType();@@ -310,6 +311,8 @@ void FsmnVad::Test() {}FsmnVad::~FsmnVad() {+ for (auto vad_in_name_item : vad_in_names_) vad_allocator.Free((void*)vad_in_name_item);+ for (auto vad_out_name_item : vad_out_names_) vad_allocator.Free((void*)vad_out_name_item);}FsmnVad::FsmnVad():env_(ORT_LOGGING_LEVEL_ERROR, ""),session_options_{} {diff --git a/runtime/onnxruntime/src/fsmn-vad.h b/runtime/onnxruntime/src/fsmn-vad.hindex f06a9651b..5443a3ba4 100644--- a/runtime/onnxruntime/src/fsmn-vad.h+++ b/runtime/onnxruntime/src/fsmn-vad.h@@ -34,6 +34,7 @@ public:std::shared_ptr<Ort::Session> vad_session_ = nullptr;Ort::Env env_;Ort::SessionOptions session_options_;+ Ort::AllocatorWithDefaultOptions vad_allocator;std::vector<const char *> vad_in_names_;std::vector<const char *> vad_out_names_;std::vector<std::vector<float>> in_cache_;@@ -56,7 +57,8 @@ private:static void GetInputOutputInfo(const std::shared_ptr<Ort::Session> &session,- std::vector<const char *> *in_names, std::vector<const char *> *out_names);+ std::vector<const char *> *in_names, std::vector<const char *> *out_names,+ Ort::AllocatorWithDefaultOptions *allocator);void FbankKaldi(float sample_rate, std::vector<std::vector<float>> &vad_feats,std::vector<float> &waves);