Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

基于onnxruntime的模型加载内存泄漏 #1964

Closed
lvchigo opened this issue Aug 25, 2023 · 10 comments
Closed

基于onnxruntime的模型加载内存泄漏 #1964

lvchigo opened this issue Aug 25, 2023 · 10 comments
Assignees
Labels

Comments

@lvchigo
Copy link

lvchigo commented Aug 25, 2023

Describe the bug

  • 使用wenet/runtime/onnxruntime流程,编译流式wenet库,并参考wenet_api接口(wenet/runtime/core/api/wenet_api.h)与demo(wenet/runtime/core/bin/api_main.cc),同时使用valgrind进行内存泄漏检测,发现在onnxruntime的模型加载时(wenet::OnnxAsrModel::Read),出现以下内存泄漏:
    image
  • 且泄漏接口包含三处接口:Ort::ModelMetadata::LookupCustomMetadataMap、Ort::Session::GetInputName、Ort::Session::GetOutputName:
    image
  • 但,除去模型初始化时内存泄漏问题,整体WER验证c++端与python端一致,推理过程中无内存泄漏情况发生;
  • 同时,查看onnxruntime v1.12.0源码,发现LookupCustomMetadataMap接口,有提示内存泄漏风险:
    image
  • 推荐解决方案:使用LookupCustomMetadataMapAllocated、GetInputNameAllocated、GetOutputNameAllocated替换原有接口即可(LookupCustomMetadataMap、GetInputName、GetOutputName):
    image

编译环境

  • GCC:7.5.0;
  • onnxruntime:1.12.0、CPU版;

联系方式:

@xingchensong
Copy link
Member

xingchensong commented Aug 28, 2023

谢谢,可以直接提个PR修复嘛,非常感谢! @lvchigo

@xingchensong
Copy link
Member

Update: 尝试了更换三个接口,程序无法运行成功(如下图所示),原因可能是session->run函数需要传入节点的name,这些name是const char*,在替换了接口后,这些pointer在出了初始化函数后被释放了。

image

image

@lvchigo
Copy link
Author

lvchigo commented Aug 29, 2023

  • 目前,在仅使用LookupCustomMetadataMapAllocated替换LookupCustomMetadataMap后,ASR结果正常,且该接口内存泄漏问题修复;
  • 在使用GetInputNameAllocated、GetOutputNameAllocated替换原有接口(GetInputName、GetOutputName)后,ASR结果异常;
  • 遇到问题与 @xingchensong 大佬问题一致,使用GetInputNameAllocated与GetOutputNameAllocated后,指针被释放;

@xingchensong
Copy link
Member

https://bbs.huaweicloud.com/blogs/357047 @lvchigo 或许这篇blog的方法可以借鉴下

@lvchigo
Copy link
Author

lvchigo commented Aug 31, 2023

@xingchensong
Copy link
Member

@lvchigo onnxruntime最新版应该在API上有不少改动,可以试试最新版的onnxruntime 在run这个api上是否还需要以来const char*的输入?

Copy link

github-actions bot commented Dec 2, 2023

This issue has been automatically closed due to inactivity.

@github-actions github-actions bot added the Stale label Dec 2, 2023
Copy link

github-actions bot commented Dec 9, 2023

This issue was closed because it has been inactive for 7 days since being marked as stale. Please reopen if you'd like to work on this further.

@github-actions github-actions bot closed this as completed Dec 9, 2023
@zhuanaa
Copy link

zhuanaa commented Jan 10, 2024

@lvchigo
参照此可解决:microsoft/onnxruntime#14157
onnxruntime :1.13.1

@xingchensong
Copy link
Member

@lvchigo 参照此可解决:microsoft/onnxruntime#14157 onnxruntime :1.13.1

可以提个pr吗

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants