| 1319 | } |
| 1320 | |
| 1321 | void Calibration::_quantizeModelEMA() { |
| 1322 | auto varMap = Variable::loadMap(_originalModelFile.c_str()); |
| 1323 | if (varMap.empty()) { |
| 1324 | MNN_ERROR("Can not load model %s\n", _originalModelFile.c_str()); |
| 1325 | return; |
| 1326 | } |
| 1327 | |
| 1328 | auto inputOutputs = Variable::getInputAndOutput(varMap); |
| 1329 | auto varInputs = Variable::mapToSequence(inputOutputs.first); |
| 1330 | auto varOutputs = Variable::mapToSequence(inputOutputs.second); |
| 1331 | auto originInfo = varInputs[0]->getInfo(); |
| 1332 | auto originFormat = NC4HW4; |
| 1333 | auto originType = halide_type_of<float>(); |
| 1334 | std::vector<int> originDims; |
| 1335 | if (nullptr != originInfo) { |
| 1336 | originFormat = originInfo->order; |
| 1337 | originDims = originInfo->dim; |
| 1338 | originType = originInfo->type; |
| 1339 | } |
| 1340 | if (!_batchSetByUser && !originDims.empty() && originDims[0] > 0) { |
| 1341 | if (_batch != originDims[0]) { |
| 1342 | DLOG(INFO) << "batch_size not set, use model batch: " << originDims[0]; |
| 1343 | _batch = originDims[0]; |
| 1344 | } |
| 1345 | } |
| 1346 | _module.reset(NN::extract(varInputs, varOutputs, true), Module::destroy); |
| 1347 | NN::turnQuantize(_module.get(), _quant_bits, NN::PerTensor, NN::MovingAverage, _winogradOpt); |
| 1348 | |
| 1349 | auto exe = Executor::getGlobalExecutor(); |
| 1350 | BackendConfig config; |
| 1351 | exe->setGlobalExecutorConfig(MNN_FORWARD_CPU, config, 1); |
| 1352 | |
| 1353 | std::shared_ptr<SGD> solver(new SGD(_module)); |
| 1354 | solver->setLearningRate(1e-5); |
| 1355 | solver->setMomentum(0.9f); |
| 1356 | solver->setWeightDecay(0.00004f); |
| 1357 | |
| 1358 | DLOG(INFO) << "batch size: " << _batch; |
| 1359 | DLOG(INFO) << "quant bits: " << _quant_bits; |
| 1360 | if (_calibrationFileNum < _batch) { |
| 1361 | MNN_ERROR("_calibrationFileNum %d < batch size %d, set batch size as %d\n", _calibrationFileNum, _batch, _calibrationFileNum); |
| 1362 | _batch = _calibrationFileNum; |
| 1363 | if (_inputType == Helper::SEQUENCE) { |
| 1364 | _batch = 1; |
| 1365 | } |
| 1366 | } |
| 1367 | |
| 1368 | const int trainIterations = _calibrationFileNum / _batch; |
| 1369 | _module->clearCache(); |
| 1370 | exe->gc(Executor::FULL); |
| 1371 | |
| 1372 | _module->setIsTraining(true); |
| 1373 | for (int it = 0; it < trainIterations; it++) { |
| 1374 | std::vector<VARP> inputs(varInputs.size()); // inputs[i].dim=[batch, c, h, w] |
| 1375 | int indicesStart = it * _batch; |
| 1376 | int indicesEnd = indicesStart + _batch; |
| 1377 | // Init batch size inputs |
| 1378 | if (_inputType == Helper::IMAGE) { |
nothing calls this directly
no test coverage detected