| 25 | return -1; |
| 26 | } |
| 27 | WhileModule* WhileModule::create(const Op* op, const std::map<std::string, SubGraph>& subGraph) { |
| 28 | auto module = new WhileModule; |
| 29 | module->setType("WhileModule"); |
| 30 | std::shared_ptr<WhileModule::Info> info(new WhileModule::Info); |
| 31 | module->mInfo = info; |
| 32 | if (nullptr != op->name()) { |
| 33 | module->setName(op->name()->str()); |
| 34 | } |
| 35 | auto whileParam = op->main_as_WhileParam(); |
| 36 | auto bodyIter = subGraph.find(whileParam->body_graph()->str()); |
| 37 | if (bodyIter == subGraph.end()) { |
| 38 | MNN_ERROR("Can't find subgraph: %s, maybe the model is breaked\n", whileParam->body_graph()->c_str()); |
| 39 | return nullptr; |
| 40 | } |
| 41 | auto& body = bodyIter->second; |
| 42 | module->mBody = body.m; |
| 43 | module->registerModel({body.m}); |
| 44 | if (whileParam->cond_graph() == nullptr) { |
| 45 | // From onnx's loop, use easy way to init |
| 46 | info->mOutputNumber = op->outputIndexes()->size(); |
| 47 | info->mBodyInputNumber = op->inputIndexes()->size(); |
| 48 | return module; |
| 49 | } |
| 50 | auto& cond = subGraph.find(whileParam->cond_graph()->str())->second; |
| 51 | module->mCond = cond.m; |
| 52 | module->registerModel({cond.m}); |
| 53 | /** Compute map index |
| 54 | int mCondInputNumber; |
| 55 | int mBodyInputNumber; |
| 56 | |
| 57 | // First mCondInputs' index, Second: inputs's index |
| 58 | std::vector<std::pair<int, int>> mInputForCond; |
| 59 | |
| 60 | // First mBodyInputs' index, Second: inputs's index |
| 61 | std::vector<std::pair<int, int>> mInputForBody; |
| 62 | std::vector<int> mOutputFromBody; |
| 63 | std::vector<std::pair<int, int>> mUpdateForCond; |
| 64 | std::vector<std::pair<int, int>> mUpdateForBody; |
| 65 | std::vector<std::pair<int, int>> mCondUpdateForCond; |
| 66 | std::vector<std::pair<int, int>> mCondUpdateForBody; |
| 67 | */ |
| 68 | // Map Inputs |
| 69 | info->mBodyInputNumber = body.inputs.size(); |
| 70 | info->mCondInputNumber = cond.inputs.size(); |
| 71 | for (int i=0; i<whileParam->aliases_inputs()->size(); ++i) { |
| 72 | auto index = i; |
| 73 | auto data = whileParam->aliases_inputs()->GetAs<StringVec>(i); |
| 74 | for (int s=0; s<data->data()->size(); ++s) { |
| 75 | auto name = data->data()->GetAsString(s)->str(); |
| 76 | auto bodyInputPos = _findPos(body.inputs, name); |
| 77 | if (bodyInputPos >= 0) { |
| 78 | info->mInputForBody.emplace_back(std::make_pair(bodyInputPos, i)); |
| 79 | } |
| 80 | auto condInputPos = _findPos(cond.inputs, name); |
| 81 | if (condInputPos >= 0) { |
| 82 | info->mInputForCond.emplace_back(std::make_pair(condInputPos, i)); |
| 83 | } |
| 84 | // if (bodyInputPos < 0 && condInputPos < 0) { |
nothing calls this directly
no test coverage detected