| 101 | } |
| 102 | } |
| 103 | static void compareForwadType(OUTPUTCONFIG outputNames, Interpreter* net, MNNForwardType expectType, MNNForwardType compareType, float tolerance, |
| 104 | const std::map<std::string, std::shared_ptr<Tensor>>& inputs, const std::string& stopOp, BackendConfig::PrecisionMode precision, int modeNum) { |
| 105 | auto inputNames = _getAllInputs(MNN::GetNet(net->getModelBuffer().first)); |
| 106 | for (int v=0; v<outputNames.size(); ++v) { |
| 107 | auto outputName = outputNames[v].second; |
| 108 | auto opName = outputNames[v].first; |
| 109 | MNN::ScheduleConfig expectConfig, compareConfig; |
| 110 | BackendConfig backendConfig; |
| 111 | backendConfig.precision = precision; |
| 112 | expectConfig.type = expectType; |
| 113 | expectConfig.path.inputs = inputNames; |
| 114 | expectConfig.path.outputs = outputName; |
| 115 | expectConfig.saveTensors = outputName; |
| 116 | expectConfig.path.mode = MNN::ScheduleConfig::Path::Tensor; |
| 117 | |
| 118 | compareConfig.type = compareType; |
| 119 | compareConfig.backendConfig = &backendConfig; |
| 120 | compareConfig.mode = modeNum; |
| 121 | compareConfig.path.inputs = inputNames; |
| 122 | compareConfig.path.outputs = outputName; |
| 123 | compareConfig.saveTensors = outputName; |
| 124 | compareConfig.path.mode = MNN::ScheduleConfig::Path::Tensor; |
| 125 | auto expectSession = net->createSession(expectConfig); |
| 126 | auto compareSession = net->createSession(compareConfig); |
| 127 | auto realInputs = net->getSessionInputAll(expectSession); |
| 128 | _zeroInputs(net, expectSession); |
| 129 | _zeroInputs(net, compareSession); |
| 130 | for (auto& iter : inputs) { |
| 131 | if (realInputs.find(iter.first) == realInputs.end()) { |
| 132 | continue; |
| 133 | } |
| 134 | Tensor* expectInput = net->getSessionInput(expectSession, iter.first.empty() ? NULL : iter.first.c_str()); |
| 135 | expectInput->copyFromHostTensor(iter.second.get()); |
| 136 | Tensor* compareInput = net->getSessionInput(compareSession, iter.first.empty() ? NULL : iter.first.c_str()); |
| 137 | compareInput->copyFromHostTensor(iter.second.get()); |
| 138 | } |
| 139 | net->runSession(expectSession); |
| 140 | net->runSession(compareSession); |
| 141 | bool allCorrect = true; |
| 142 | bool outputValid = false; |
| 143 | auto compare = [&]() { |
| 144 | for(auto name : outputName) { |
| 145 | auto expectTensor = net->getSessionOutput(expectSession, name.c_str()); |
| 146 | if (nullptr == expectTensor || expectTensor->host<void>() == nullptr) { |
| 147 | MNN_ERROR("Can't compare tensor: %s\n", name.c_str()); |
| 148 | continue; |
| 149 | } |
| 150 | outputValid = true; |
| 151 | auto compareTensor = net->getSessionOutput(compareSession, name.c_str()); |
| 152 | if (nullptr == compareTensor) { |
| 153 | MNN_ERROR("%d [%s] Tensor %s invalid\n", v, opName.c_str(), name.c_str()); |
| 154 | allCorrect = false; |
| 155 | break; |
| 156 | } |
| 157 | auto correct = TensorUtils::compareTensors(compareTensor, expectTensor, tolerance, true); |
| 158 | if (!correct) { |
| 159 | MNN_PRINT("%d [%s] Op outputs %s is error\n", v, opName.c_str(), name.c_str()); |
| 160 | allCorrect = false; |
no test coverage detected