MCPcopy Create free account
hub / github.com/alibaba/MNN / initGetThreshold

Function initGetThreshold

tools/cpp/getLinearInput.hpp:130–185  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

128
129
130inline 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
187inline void initGetMaxValue(const std::string& maxValueFileName) {

Callers 1

Calls 15

initThresholdFileFunction · 0.85
_ConvertFunction · 0.85
_AbsFunction · 0.85
_ReshapeFunction · 0.85
_TopKV2Function · 0.85
_GatherFunction · 0.85
writeThresholdRealtimeFunction · 0.85
getDimensionTypeMethod · 0.80
copyToHostTensorMethod · 0.80
setCallBackMethod · 0.80
createFunction · 0.50
nameMethod · 0.45

Tested by

no test coverage detected