| 185 | } |
| 186 | |
| 187 | inline void initGetMaxValue(const std::string& maxValueFileName) { |
| 188 | initMaxValueFile(maxValueFileName); |
| 189 | |
| 190 | static bool registered = false; |
| 191 | if (!registered) { |
| 192 | std::atexit(cleanupAtExit); |
| 193 | registered = true; |
| 194 | } |
| 195 | |
| 196 | MNN::TensorCallBackWithInfo beforeCallBack = [](const std::vector<MNN::Tensor*>& ntensors, const MNN::OperatorInfo* info) { |
| 197 | auto opName = info->name(); |
| 198 | if (info->type() == "Copy") { |
| 199 | return true; |
| 200 | } |
| 201 | if (opName.find("Linear") == std::string::npos || opName.find("raster") != std::string::npos) { |
| 202 | return true; |
| 203 | } |
| 204 | |
| 205 | for (int i = 0; i < ntensors.size(); ++i) { |
| 206 | auto ntensor = ntensors[i]; |
| 207 | auto outDimType = ntensor->getDimensionType(); |
| 208 | std::shared_ptr<MNN::Tensor> expectTensor(new MNN::Tensor(ntensor, outDimType)); |
| 209 | bool res = ntensor->copyToHostTensor(expectTensor.get()); |
| 210 | if (res) { |
| 211 | ntensor = expectTensor.get(); |
| 212 | } |
| 213 | { |
| 214 | auto ninput = MNN::Express::Variable::create(MNN::Express::Expr::create(ntensor)); |
| 215 | if (nullptr == ninput->getInfo()) { |
| 216 | MNN_ERROR("Alloc memory or compute size error\n"); |
| 217 | return false; |
| 218 | } |
| 219 | ninput = MNN::Express::_Convert(ninput, MNN::Express::NHWC); |
| 220 | ninput = MNN::Express::_Abs(ninput); |
| 221 | ninput = MNN::Express::_Reshape(ninput, {-1}); |
| 222 | |
| 223 | auto kv = MNN::Express::_TopKV2(ninput, MNN::Express::_Scalar<int>(1)); |
| 224 | auto maxValues = kv[0]; |
| 225 | auto maxValueFloat = maxValues->readMap<float>()[0]; |
| 226 | |
| 227 | writeMaxValueRealtime(opName, maxValueFloat); |
| 228 | } |
| 229 | } |
| 230 | return true; |
| 231 | }; |
| 232 | |
| 233 | MNN::TensorCallBackWithInfo callBack = [](const std::vector<MNN::Tensor*>& ntensors, const MNN::OperatorInfo* info) { |
| 234 | return true; |
| 235 | }; |
| 236 | |
| 237 | MNN::Express::ExecutorScope::Current()->setCallBack(std::move(beforeCallBack), std::move(callBack)); |
| 238 | } |
| 239 | |
| 240 | inline void closeAllFiles() { |
| 241 | closeThresholdFile(); |
no test coverage detected