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

多个模型串行共享一个runtime,内存没有复用反倒增加 #2760

Closed
486946 opened this issue Feb 20, 2024 · 16 comments
Closed

多个模型串行共享一个runtime,内存没有复用反倒增加 #2760

486946 opened this issue Feb 20, 2024 · 16 comments
Labels
stale User The user ask question about how to use. Or don't use MNN correctly and cause bug.

Comments

@486946
Copy link

486946 commented Feb 20, 2024

未使用runtime时,两个模型通过getsessioninfo查看内存信息分别为129.395508 MB和60.189285 MB
使用共享一个runtime时,两个模型通过getsessioninfo查看内存信息分别为129.395508 MB和189.584808 MB

共享runtime的作用应该为两者最大129.395508 MB才对,这怎么像是还累积了呢?

使用共享一个runtime方式如下:
ScheduleConfig config;
config.numberThread = 4;
auto runtimeInfo = Interpreter::createRuntime({config});

/创建第一个模型/
std::shared_ptr net1 = Interpreter::createFromFile("1.mnn");
auto session1 = net1->createSession(config, runtimeInfo);
//获取第一个模型内存信息
net1->getSessionInfo(session1, Interpreter::MEMORY, &memoryUsage1);

/创建第二个模型/
std::shared_ptr net2 = Interpreter::createFromFile("2.mnn");
auto session2 = net2->createSession(config, runtimeInfo);
//获取第二个模型内存信息
net2->getSessionInfo(session2, Interpreter::MEMORY, &memoryUsage2);

/使用/
/* 填充输入1..... */
net1->runSession(session1);

/* 读取输出1 填充输入2..... */
net2->runSession(session2);

@jxt1234
Copy link
Collaborator

jxt1234 commented Feb 20, 2024

  1. 模型中的静态内存(与模型大小相等权重)是无法复用的,只能累加
  2. 共享 runtime 是共享线程池与内存池,不是共享内存,各session所使用的内存还是需要分配的

@jxt1234 jxt1234 added the User The user ask question about how to use. Or don't use MNN correctly and cause bug. label Feb 20, 2024
@486946
Copy link
Author

486946 commented Feb 20, 2024

  1. 模型中的静态内存(与模型大小相等权重)是无法复用的,只能累加
  2. 共享 runtime 是共享线程池与内存池,不是共享内存,各session所使用的内存还是需要分配的

内存池不是提前优化分配好的内存吗?想串行的多个模型能够复用内存有什么办法(最好不增加耗时)?

@jxt1234
Copy link
Collaborator

jxt1234 commented Feb 20, 2024

内存池是用于分配内存的,但模型中的静态内存(卷积的权重)是不能共用的,这部分无法共用。动态部分可以共用,就是通过共享运行时方式

@jxt1234
Copy link
Collaborator

jxt1234 commented Feb 20, 2024

先看下你的模型有多大,是否量化过(量化过实际所需内存需要乘以 4)

@486946
Copy link
Author

486946 commented Feb 20, 2024

先看下你的模型有多大,是否量化过(量化过实际所需内存需要乘以 4)

两个模型都是大约14M ,都是没有量化过的浮点模型,

@jxt1234
Copy link
Collaborator

jxt1234 commented Feb 20, 2024

模型发一下吧

@jxt1234
Copy link
Collaborator

jxt1234 commented Feb 20, 2024

diff --git a/source/backend/cpu/CPUBackend.cpp b/source/backend/cpu/CPUBackend.cpp
index e96a2d10a..06df56dfb 100644
--- a/source/backend/cpu/CPUBackend.cpp
+++ b/source/backend/cpu/CPUBackend.cpp
@@ -273,6 +273,8 @@ bool CPUBackend::onSelectDynamicAllocator(int index, int maxIndex) {

ErrorCode CPUBackend::onResizeEnd() {
getCache()->release();

  • FUNC_PRINT((float)mStaticAllocator->totalSize() / 1024.0f / 1024.0f);
  • FUNC_PRINT((float)mDynamicAllocator->totalSize() / 1024.0f / 1024.0f);
    return mCurrentDynamicAllocator->compute();
    }

这么修改看一下 静态内存和动态内存大小

@486946
Copy link
Author

486946 commented Feb 20, 2024

模型发一下吧

请提供下邮箱?还是什么

@keke444
Copy link

keke444 commented Feb 21, 2024

Hi:
我们的两个模型见附件
按照 #2760 (comment)
添加之后查看静态内存和动态内存, 静态内存和getsessioninfo :memory 打印的数值一致 ,两个模型还是累加 ;动态内存,通过添加共享运行的runtime, 也没有什么变化,看不出是否共用
请帮忙查看一下,我们还是想串行的多个模型能够复用内存
谢谢
model1.zip
model2.zip

@jxt1234
Copy link
Collaborator

jxt1234 commented Feb 21, 2024

最新代码上两个模型 getSessionInfo memory 是多少,共享 runtime 之后 getSessionInfo memory 是多少?

@486946
Copy link
Author

486946 commented Feb 21, 2024

最新代码上两个模型 getSessionInfo memory 是多少,共享 runtime 之后 getSessionInfo memory 是多少?

分别是77M和43M,共享runtime之后分别是77M和120M

@jxt1234
Copy link
Collaborator

jxt1234 commented Feb 29, 2024

确实动态内存部分没有复用,还在解决中

@486946
Copy link
Author

486946 commented Feb 29, 2024

确实动态内存部分没有复用,还在解决中

期待解决方案

@wangzhaode
Copy link
Collaborator

确实动态内存部分没有复用,还在解决中

期待解决方案

分析了你的情况,已经讨论出解决方案了;后续需要时间实现和测试~

@486946
Copy link
Author

486946 commented Mar 25, 2024

请问是否有patch提前 发我们测试下

Copy link

Marking as stale. No activity in 60 days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale User The user ask question about how to use. Or don't use MNN correctly and cause bug.
Projects
None yet
Development

No branches or pull requests

4 participants