| 294 | } |
| 295 | |
| 296 | void converToStaticModel(const Net* net, std::map<std::string,std::vector<int>>& inputConfig, std::string mnnFile) { |
| 297 | // set a backend and context to run resize |
| 298 | ScheduleConfig config; |
| 299 | config.type = MNN_FORWARD_CPU; |
| 300 | BackendConfig backendConfig; |
| 301 | backendConfig.precision = BackendConfig::Precision_High; |
| 302 | config.backendConfig = &backendConfig; |
| 303 | Backend::Info compute; |
| 304 | compute.type = config.type; |
| 305 | compute.numThread = config.numThread; |
| 306 | compute.user = config.backendConfig; |
| 307 | const RuntimeCreator* runtimeCreator(MNNGetExtraRuntimeCreator(compute.type)); |
| 308 | std::unique_ptr<Runtime> runtime(runtimeCreator->onCreate(compute)); |
| 309 | std::shared_ptr<Backend> backend(runtime->onCreate()); |
| 310 | BackendConfig defaultConfig; |
| 311 | defaultConfig.flags = 4; |
| 312 | std::shared_ptr<Backend> defaultBackend(runtime->onCreate(&defaultConfig)); |
| 313 | std::vector<std::shared_ptr<Tensor>> allTensors; |
| 314 | allTensors.resize(net->tensorName()->size()); |
| 315 | ErrorCode code = NO_ERROR; |
| 316 | initConstTensors(allTensors, net, defaultBackend.get(), code, nullptr); |
| 317 | if (NO_ERROR != code) { |
| 318 | MNN_ERROR("Init tensor error code = %d\n", code); |
| 319 | return; |
| 320 | } |
| 321 | bool valid = initTensors(allTensors, net); |
| 322 | // set tensors' shape by inputConfig |
| 323 | for (int i = 0; i < allTensors.size(); i++) { |
| 324 | auto name = net->tensorName()->GetAsString(i)->str(); |
| 325 | if (inputConfig.find(name) != inputConfig.end()) { |
| 326 | auto& dims = inputConfig[name]; |
| 327 | allTensors[i]->buffer().dimensions = dims.size(); |
| 328 | for (int j = 0; j < dims.size(); j++) { |
| 329 | allTensors[i]->setLength(j, dims[j]); |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | std::vector<Schedule::OpCacheInfo> infos; |
| 334 | { |
| 335 | std::vector<const Op*> ops; |
| 336 | for (int i = 0; i < net->oplists()->size(); i++) { |
| 337 | auto op = net->oplists()->GetAs<Op>(i); |
| 338 | if (needComputeOp(op)) { |
| 339 | ops.push_back(op); |
| 340 | } |
| 341 | } |
| 342 | initPipelineInfosFromOps(infos, ops, allTensors); |
| 343 | setInputOutputForOps(allTensors, ops); |
| 344 | } |
| 345 | GeometryComputer::Context ctx(Interpreter::GeometryComputeMask::GEOMETRCOMPUTEMASK_ALL, defaultBackend); |
| 346 | // resize the session's info and store to buffer |
| 347 | std::vector<Tensor*> constTensors; |
| 348 | GeometryComputerUtils::buildConstantTensors(infos); |
| 349 | GeometryComputerUtils::shapeComputeAndGeometryTransform(runtime.get(), nullptr, infos, ctx, defaultBackend, runtime->onGetCompilerType()); |
| 350 | std::map<Tensor*, std::pair<std::string, int>> tensorName; |
| 351 | for (int i = 0; i < net->tensorName()->size(); i++) { |
| 352 | tensorName[allTensors[i].get()] = std::make_pair(net->tensorName()->GetAsString(i)->str(), i); |
| 353 | } |
no test coverage detected