MNN icon indicating copy to clipboard operation
MNN copied to clipboard

260M的MNN超分模型,resizeSession()的耗时太长

Open CloudGuardian opened this issue 6 months ago • 3 comments

我的模型:

Onnx转MNN:MNNConvert -f ONNX --modelFile XXX.onnx --MNNModel XXX.mnn --bizCode biz --optimizeLevel 2 --fp16 MNN文件大小:260M Config:

ScheduleConfig netConfig;
netConfig.type = MNN_FORWARD_CPU; // MNN_FORWARD_OPENCL; //  MNN_FORWARD_VULKAN; // MNN_FORWARD_CPU
netConfig.numThread = 1;

BackendConfig backendConfig;
backendConfig.precision = BackendConfig::Precision_Low;
backendConfig.power = BackendConfig::Power_Normal;
netConfig.backendConfig = &backendConfig;

耗时log: create model time: 777ms CPU Group: [ 0 1 2 3 4 5 ], 500000 - 2000000 CPU Group: [ 6 7 ], 725000 - 2200000 The device supports: i8sdot:1, fp16:1, i8mm: 0, sve2: 0 MNN session created successfully. create session time: 2ms get input time: 0ms resize tensor time: 0ms resize session time: 16787ms Input shape: 1 3 512 512 Output shape: 1 3 1024 1024

主要问题是resizeSession()的耗时实在太长了,有什么办法优化这部分的耗时吗?🥳🥳🥳

CloudGuardian avatar Jun 06 '25 08:06 CloudGuardian

  1. 加载时会做权重重排,后续的 resizeSession 时间就少了。
  2. fp16 的模型目前需要先 fp16 -> fp32 再重排,如果开了 arm82 还需要再转 fp16 ,是当前加载最慢的。
  3. 可以换用量化模型 (模型转换时加上 --weightQuantBits=8 --weightQuantBlock=64),并开启动态量化 (设置 memory = low),加载速度有优化,性能也可以提升

https://mnn-docs.readthedocs.io/en/latest/tools/compress.html

jxt1234 avatar Jun 09 '25 01:06 jxt1234

  1. 加载时会做权重重排,后续的 resizeSession 时间就少了。
  2. fp16 的模型目前需要先 fp16 -> fp32 再重排,如果开了 arm82 还需要再转 fp16 ,是当前加载最慢的。
  3. 可以换用量化模型 (模型转换时加上 --weightQuantBits=8 --weightQuantBlock=64),并开启动态量化 (设置 memory = low),加载速度有优化,性能也可以提升

https://mnn-docs.readthedocs.io/en/latest/tools/compress.html

关于第2点,意思是fp16的输入模型的初始化时间反而会比fp32的更长吗?

CloudGuardian avatar Jun 09 '25 03:06 CloudGuardian

  1. 加载时会做权重重排,后续的 resizeSession 时间就少了。
  2. fp16 的模型目前需要先 fp16 -> fp32 再重排,如果开了 arm82 还需要再转 fp16 ,是当前加载最慢的。
  3. 可以换用量化模型 (模型转换时加上 --weightQuantBits=8 --weightQuantBlock=64),并开启动态量化 (设置 memory = low),加载速度有优化,性能也可以提升

https://mnn-docs.readthedocs.io/en/latest/tools/compress.html

关于第3点,我的模型做动态量化后,fp16精度推理耗时比非动态量化的模型多了50%,这是正常的吗?🤣🤣🤣

CloudGuardian avatar Jun 10 '25 01:06 CloudGuardian

Marking as stale. No activity in 60 days.

github-actions[bot] avatar Aug 09 '25 09:08 github-actions[bot]