| 1056 | } |
| 1057 | |
| 1058 | static std::unique_ptr<MNN::OpT> _compileSubModule(const SubModuleIO& io, SubModuleInfo& info, const void* buffer, size_t bufferSize, const std::string& path, std::string srcpath, const std::string& targetNpuPath, float& cpuTotal, float& npuTotal, int shapeIndex, std::string graphicName) { |
| 1059 | std::vector<std::string> inputNames(info.inputs.size()); |
| 1060 | std::vector<std::string> outputNames(info.outputs.size()); |
| 1061 | auto net = flatbuffers::GetRoot<Net>(buffer); |
| 1062 | for (int i=0; i<info.inputs.size(); ++i) { |
| 1063 | auto index = info.inputs[i]; |
| 1064 | inputNames[i] = net->tensorName()->GetAsString(index)->str(); |
| 1065 | } |
| 1066 | for (int i=0; i<info.outputs.size(); ++i) { |
| 1067 | auto index = info.outputs[i]; |
| 1068 | outputNames[i] = net->tensorName()->GetAsString(index)->str(); |
| 1069 | } |
| 1070 | /** Get Output shapes*/ |
| 1071 | std::vector<MNN::Express::Variable::Info> outputInfos(io.outputs.size()); |
| 1072 | for (int i=0; i<outputInfos.size(); ++i) { |
| 1073 | outputInfos[i] = *io.outputs[i]->getInfo(); |
| 1074 | } |
| 1075 | |
| 1076 | /** Make ML Model*/ |
| 1077 | do { |
| 1078 | MNN::ScheduleConfig config; |
| 1079 | config.type = gNPUType; |
| 1080 | std::shared_ptr<MNN::Express::Executor::RuntimeManager> rtmgr(MNN::Express::Executor::RuntimeManager::createRuntimeManager(config)); |
| 1081 | rtmgr->setExternalFile((srcpath + ".weight").c_str()); |
| 1082 | rtmgr->setCache(path.c_str()); |
| 1083 | rtmgr->setHint(MNN::Interpreter::KVCACHE_SIZE_LIMIT, gMaxKVSize); |
| 1084 | MNN::Express::Module::Config mdconfig; |
| 1085 | mdconfig.shapeMutable = false; |
| 1086 | std::shared_ptr<MNN::Express::Module> m(MNN::Express::Module::load(inputNames, outputNames, (const uint8_t*)buffer, bufferSize, rtmgr, &mdconfig), MNN::Express::Module::destroy); |
| 1087 | auto predict = m->onForward(io.inputs); |
| 1088 | if (gNeedOffline) { |
| 1089 | break; |
| 1090 | } |
| 1091 | if (predict.size() != io.outputs.size()) { |
| 1092 | MNN_ERROR("Failed to compile: %s\n", path.c_str()); |
| 1093 | info.isBreak = true; |
| 1094 | return nullptr; |
| 1095 | } |
| 1096 | for (int i=0; i<predict.size(); ++i) { |
| 1097 | auto error = io.outputs[i]-predict[i]; |
| 1098 | error = MNN::Express::_ReduceMax(MNN::Express::_Abs(MNN::Express::_Cast<float>(error))); |
| 1099 | auto maxValue = MNN::Express::_ReduceMax(MNN::Express::_Abs(MNN::Express::_Cast<float>(io.outputs[i])))->readMap<float>()[0]; |
| 1100 | if (maxValue < 0.01f) { |
| 1101 | maxValue = 0.01f; |
| 1102 | } |
| 1103 | auto errorf = error->readMap<float>()[0]; |
| 1104 | if (errorf / maxValue > 0.1f) { |
| 1105 | MNN_ERROR("error = %f, max = %f for %s\n", errorf, maxValue, path.c_str()); |
| 1106 | info.isBreak = true; |
| 1107 | return nullptr; |
| 1108 | } |
| 1109 | } |
| 1110 | // Compare Speed |
| 1111 | int testTime = 20; |
| 1112 | MNN_PRINT("Start to Test speed for %d times\n", testTime); |
| 1113 | MNN::Timer timer; |
| 1114 | for (int i=0; i<testTime; ++i) { |
| 1115 | predict = m->onForward(io.inputs); |
no test coverage detected