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

Function doBench

benchmark/benchmark.cpp:119–182  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

117}
118
119std::vector<float> doBench(Model& model, int loop, int warmup = 10, int forward = MNN_FORWARD_CPU, bool only_inference = true,
120 int numberThread = 4, int precision = 2, float sparsity = 0.0f, int sparseBlockOC = 1, bool testQuantModel=false, bool enableKleidiAI=false) {
121 auto revertor = std::unique_ptr<Revert>(new Revert(model.model_file.c_str()));
122 if (testQuantModel) {
123 revertor->initialize(0, sparseBlockOC, false, true);
124 } else {
125 revertor->initialize(sparsity, sparseBlockOC);
126 }
127
128 auto modelBuffer = revertor->getBuffer();
129 const auto bufferSize = revertor->getBufferSize();
130 auto net = std::shared_ptr<MNN::Interpreter>(MNN::Interpreter::createFromBuffer(modelBuffer, bufferSize), MNN::Interpreter::destroy);
131 revertor.reset();
132 net->setSessionMode(MNN::Interpreter::Session_Release);
133 net->setSessionHint(MNN::Interpreter::HintMode::CPU_ENABLE_KLEIDIAI, enableKleidiAI);
134 MNN::ScheduleConfig config;
135 config.numThread = numberThread;
136 config.type = static_cast<MNNForwardType>(forward);
137 MNN::BackendConfig backendConfig;
138 backendConfig.precision = (MNN::BackendConfig::PrecisionMode)precision;
139 backendConfig.power = MNN::BackendConfig::Power_High;
140 config.backendConfig = &backendConfig;
141
142 std::vector<float> costs;
143 MNN::Session* session = net->createSession(config);
144
145 MNN::Tensor* input = net->getSessionInput(session, NULL);
146
147 // if the model has not the input dimension, umcomment the below code to set the input dims
148 // std::vector<int> dims{1, 3, 224, 224};
149 // net->resizeTensor(input, dims);
150 // net->resizeSession(session);
151
152 net->releaseModel();
153
154 const MNN::Backend* inBackend = net->getBackend(session, input);
155
156 std::shared_ptr<MNN::Tensor> givenTensor(MNN::Tensor::createHostTensorFromDevice(input, false));
157
158 auto outputTensor = net->getSessionOutput(session, NULL);
159 std::shared_ptr<MNN::Tensor> expectTensor(MNN::Tensor::createHostTensorFromDevice(outputTensor, false));
160 // Warming up...
161 for (int i = 0; i < warmup; ++i) {
162 void* host = input->map(MNN::Tensor::MAP_TENSOR_WRITE, input->getDimensionType());
163 input->unmap(MNN::Tensor::MAP_TENSOR_WRITE, input->getDimensionType(), host);
164
165 net->runSession(session);
166
167 host = outputTensor->map(MNN::Tensor::MAP_TENSOR_READ, outputTensor->getDimensionType());
168 outputTensor->unmap(MNN::Tensor::MAP_TENSOR_READ, outputTensor->getDimensionType(), host);
169 }
170
171 for (int round = 0; round < loop; round++) {
172 MNN::Timer _t;
173 void* host = input->map(MNN::Tensor::MAP_TENSOR_WRITE, input->getDimensionType());
174 input->unmap(MNN::Tensor::MAP_TENSOR_WRITE, input->getDimensionType(), host);
175 net->runSession(session);
176 host = outputTensor->map(MNN::Tensor::MAP_TENSOR_READ, outputTensor->getDimensionType());

Callers 2

iosBenchAllFunction · 0.85
mainFunction · 0.85

Calls 15

getBufferSizeMethod · 0.80
setSessionModeMethod · 0.80
setSessionHintMethod · 0.80
getSessionInputMethod · 0.80
releaseModelMethod · 0.80
getSessionOutputMethod · 0.80
getDimensionTypeMethod · 0.80
runSessionMethod · 0.80
durationInUsMethod · 0.80
c_strMethod · 0.45
initializeMethod · 0.45
getBufferMethod · 0.45

Tested by

no test coverage detected