| 128 | |
| 129 | |
| 130 | inline void initGetThreshold(const std::string& thresholdFileName, float targetSparsity) { |
| 131 | initThresholdFile(thresholdFileName); |
| 132 | |
| 133 | static bool registered = false; |
| 134 | if (!registered) { |
| 135 | std::atexit(cleanupAtExit); |
| 136 | registered = true; |
| 137 | } |
| 138 | |
| 139 | MNN::TensorCallBackWithInfo beforeCallBack = [targetSparsity](const std::vector<MNN::Tensor*>& ntensors, const MNN::OperatorInfo* info) { |
| 140 | auto opName = info->name(); |
| 141 | if (info->type() == "Copy") { |
| 142 | return true; |
| 143 | } |
| 144 | if (opName.find("Linear") == std::string::npos || opName.find("raster") != std::string::npos) { |
| 145 | return true; |
| 146 | } |
| 147 | for (int i = 0; i < ntensors.size(); ++i) { |
| 148 | auto ntensor = ntensors[i]; |
| 149 | auto outDimType = ntensor->getDimensionType(); |
| 150 | std::shared_ptr<MNN::Tensor> expectTensor(new MNN::Tensor(ntensor, outDimType)); |
| 151 | bool res = ntensor->copyToHostTensor(expectTensor.get()); |
| 152 | if (res) { |
| 153 | ntensor = expectTensor.get(); |
| 154 | } |
| 155 | { |
| 156 | auto ninput = MNN::Express::Variable::create(MNN::Express::Expr::create(ntensor)); |
| 157 | if (nullptr == ninput->getInfo()) { |
| 158 | MNN_ERROR("Alloc memory or compute size error\n"); |
| 159 | return false; |
| 160 | } |
| 161 | ninput = MNN::Express::_Convert(ninput, MNN::Express::NHWC); |
| 162 | ninput = MNN::Express::_Abs(ninput); |
| 163 | ninput = MNN::Express::_Reshape(ninput, {-1}); |
| 164 | |
| 165 | auto totalNum = ninput->getInfo()->dim[0]; |
| 166 | int keepNum = totalNum * (1 - targetSparsity); |
| 167 | |
| 168 | auto kv = MNN::Express::_TopKV2(ninput, MNN::Express::_Scalar<int>(keepNum)); |
| 169 | auto values = kv[0]; |
| 170 | |
| 171 | auto threshold = MNN::Express::_Gather(values, MNN::Express::_Scalar<int>(keepNum - 1)); |
| 172 | auto thresholdValue = threshold->readMap<float>()[0]; |
| 173 | |
| 174 | writeThresholdRealtime(opName, thresholdValue); |
| 175 | } |
| 176 | } |
| 177 | return true; |
| 178 | }; |
| 179 | |
| 180 | MNN::TensorCallBackWithInfo callBack = [](const std::vector<MNN::Tensor*>& ntensors, const MNN::OperatorInfo* info) { |
| 181 | return true; |
| 182 | }; |
| 183 | |
| 184 | MNN::Express::ExecutorScope::Current()->setCallBack(std::move(beforeCallBack), std::move(callBack)); |
| 185 | } |
| 186 | |
| 187 | inline void initGetMaxValue(const std::string& maxValueFileName) { |
no test coverage detected