forked from ami-iit/onnx-cpp-benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
onnx-cpp-benchmark.cpp
391 lines (334 loc) · 17.1 KB
/
onnx-cpp-benchmark.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#include <cstdint>
#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <random>
#include <chrono>
#include <CLI/CLI.hpp>
#include <onnxruntime_cxx_api.h>
std::unique_ptr<Ort::Session> g_ortSession;
std::string convertEnumToString(const ONNXTensorElementDataType value) {
switch (value) {
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_UNDEFINED:
return "UNDEFINED";
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT:
return "FLOAT";
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT8:
return "UINT8";
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_INT8:
return "INT8";
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT16:
return "UINT16";
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_INT16:
return "INT16";
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_INT32:
return "INT32";
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_INT64:
return "INT64";
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING:
return "STRING";
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_BOOL:
return "BOOL";
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT16:
return "FLOAT16";
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_DOUBLE:
return "DOUBLE";
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT32:
return "UINT32";
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT64:
return "UINT64";
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_COMPLEX64:
return "COMPLEX64";
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_COMPLEX128:
return "COMPLEX128";
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_BFLOAT16:
return "BFLOAT16";
default:
return "UNKNOWN";
}
}
Ort::Value CreateTensor(const ONNXTensorElementDataType orttype, const std::vector<int64_t>& realShape)
{
Ort::AllocatorWithDefaultOptions allocator;
switch (orttype) {
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT:
return Ort::Value::CreateTensor<float>(allocator, realShape.data(), realShape.size());
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT8:
return Ort::Value::CreateTensor<uint8_t>(allocator, realShape.data(), realShape.size());
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_INT8:
return Ort::Value::CreateTensor<int8_t>(allocator, realShape.data(), realShape.size());
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT16:
return Ort::Value::CreateTensor<uint16_t>(allocator, realShape.data(), realShape.size());
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_INT16:
return Ort::Value::CreateTensor<int16_t>(allocator, realShape.data(), realShape.size());
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_INT32:
return Ort::Value::CreateTensor<int32_t>(allocator, realShape.data(), realShape.size());
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_INT64:
return Ort::Value::CreateTensor<int64_t>(allocator, realShape.data(), realShape.size());
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_BOOL:
return Ort::Value::CreateTensor<bool>(allocator, realShape.data(), realShape.size());
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_DOUBLE:
return Ort::Value::CreateTensor<double>(allocator, realShape.data(), realShape.size());
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT32:
return Ort::Value::CreateTensor<uint32_t>(allocator, realShape.data(), realShape.size());
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT64:
return Ort::Value::CreateTensor<uint64_t>(allocator, realShape.data(), realShape.size());
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING:
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_COMPLEX64:
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_COMPLEX128:
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_BFLOAT16:
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_UNDEFINED:
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT16:
default:
std::cerr << "onnx-cpp-benchmark: Error in CreateTensor, unable to create tensor of type "
<< convertEnumToString(orttype) << std::endl;
std::exit(1);
return Ort::Value(nullptr);
}
}
template <typename T>
void FillTensorWithRandomDataHelper(Ort::Value& tensor, std::vector<int64_t>& realShape) {
std::default_random_engine generator;
generator.seed(0);
std::uniform_real_distribution<float> distribution(-100.0,
100.0);
int tensor_size = 1;
for(auto i: realShape)
{
tensor_size = tensor_size*i;
}
T* tensor_data = tensor.GetTensorMutableData<T>();
for (int i = 0; i < tensor_size; ++i) {
float random_value = distribution(generator);
tensor_data[i] = random_value;
}
return;
}
void FillTensorWithRandomData(Ort::Value& tensor)
{
ONNXTensorElementDataType orttype = tensor.GetTypeInfo().GetTensorTypeAndShapeInfo().GetElementType();
std::vector<int64_t> realShape = tensor.GetTypeInfo().GetTensorTypeAndShapeInfo().GetShape();
switch (orttype) {
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT:
return FillTensorWithRandomDataHelper<float>(tensor, realShape);
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT8:
return FillTensorWithRandomDataHelper<float>(tensor, realShape);
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_INT8:
return FillTensorWithRandomDataHelper<float>(tensor, realShape);
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT16:
return FillTensorWithRandomDataHelper<float>(tensor, realShape);
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_INT16:
return FillTensorWithRandomDataHelper<float>(tensor, realShape);
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_INT32:
return FillTensorWithRandomDataHelper<float>(tensor, realShape);
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_INT64:
return FillTensorWithRandomDataHelper<float>(tensor, realShape);
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_BOOL:
return FillTensorWithRandomDataHelper<float>(tensor, realShape);
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_DOUBLE:
return FillTensorWithRandomDataHelper<float>(tensor, realShape);
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT32:
return FillTensorWithRandomDataHelper<float>(tensor, realShape);
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT64:
return FillTensorWithRandomDataHelper<uint64_t>(tensor, realShape);
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING:
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_COMPLEX64:
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_COMPLEX128:
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_BFLOAT16:
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_UNDEFINED:
case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT16:
default:
std::cerr << "onnx-cpp-benchmark: Error in FillTensorWithRandomData, unable to create tensor of type "
<< convertEnumToString(orttype) << std::endl;
std::exit(1);
return;
}
}
int main(int argc, char* argv[])
{
CLI::App app{"onnx-cpp-benchmark: Simple tool to profile onnx inference with C++ APis."};
// Read the parameters from the command line arguments
std::string onnxfilepath;
// Number of inputs fed to the network fed to the network in one inference run
int64_t nrOfParallelInputsInOneIteration = 10;
// Number of iterations
int64_t numberOfIterations = 1000;
// Backend
std::string backend = "onnxruntimecpu";
app.add_option("onnxfilepath", onnxfilepath, "The name of the onnx file to benchmark")
->required()
->check(CLI::ExistingFile);
app.add_option("--batch_size", nrOfParallelInputsInOneIteration, "Number of parallel inputs in one iteration/inference run");
app.add_option("--iterations", numberOfIterations, "Number of iterations (inference runs) to use for benchmark");
app.add_option("--backend", backend, "Backend to use, one of : onnxruntimecpu, onnxruntimecuda");
CLI11_PARSE(app, argc, argv);
if (backend != "onnxruntimecpu" && backend != "onnxruntimecuda")
{
std::cerr << "ERROR: Unsupported backend " << backend << std::endl;
return 1;
}
OrtSessionOptions* sessionOptionsCPtr;
OrtStatus* statusCreateSessionPtr = Ort::GetApi().CreateSessionOptions(&sessionOptionsCPtr);
// TODO handle statusCreateSessionPtr
// Enable CUDA if requested
OrtCUDAProviderOptionsV2* cuda_options = nullptr;
if (backend == "onnxruntimecuda")
{
// Check that onnxruntime is compiled with GPU support
const auto providers = Ort::GetAvailableProviders();
if (std::find(providers.begin(), providers.end(), "CUDAExecutionProvider") == providers.end())
{
std::cerr << "onnxruntime is not compiled with the cuda provide, impossible to use onnxruntimecuda backend" << std::endl;
std::cerr << "Available onnxruntime provides: " << std::endl;
for (const auto& provider : providers) {
std::cerr << provider << '\n';
}
return 1;
}
Ort::GetApi().CreateCUDAProviderOptions(&cuda_options);
OrtStatus* statusPtr = Ort::GetApi().SessionOptionsAppendExecutionProvider_CUDA_V2(sessionOptionsCPtr, cuda_options);
Ort::Status statusCxx(statusPtr);
if (!statusCxx.IsOK())
{
std::cerr << "SessionOptionsAppendExecutionProvider_CUDA_V2 returned error " << statusCxx.GetErrorMessage() << std::endl;
return 1;
}
}
Ort::SessionOptions sessionOptions{sessionOptionsCPtr};
// Ort::Session's constructor is OS-dependent, wants wchar_t* on Windows and char* on other OSs
// Note: this only works with single-byte characters, such as ASCII or ISO-8859-1,
// see https://stackoverflow.com/questions/2573834/c-convert-string-or-char-to-wstring-or-wchar-t
std::basic_string<ORTCHAR_T> networkModelPathAsOrtString(onnxfilepath.begin(), onnxfilepath.end());
Ort::Env env(OrtLoggingLevel::ORT_LOGGING_LEVEL_WARNING);
Ort::AllocatorWithDefaultOptions allocator;
g_ortSession = std::make_unique<Ort::Session>(env, networkModelPathAsOrtString.c_str(),
sessionOptions);
if (g_ortSession == nullptr)
{
std::cerr << "Unable to instantiate the model in: " << onnxfilepath << std::endl;
return 1;
}
// Get size of inputs
std::vector<Ort::TypeInfo> inputsInfo;
std::vector<std::string> inputNames;
for(size_t inputIdx=0; inputIdx < g_ortSession->GetInputCount(); inputIdx++)
{
inputNames.emplace_back(g_ortSession->GetInputNameAllocated(inputIdx, allocator).get());
inputsInfo.push_back(g_ortSession->GetInputTypeInfo(inputIdx));
}
// Get size of outputs
std::vector<Ort::TypeInfo> outputsInfo;
std::vector<std::string> outputNames;
for(size_t outputIdx=0; outputIdx < g_ortSession->GetOutputCount(); outputIdx++)
{
outputNames.emplace_back(g_ortSession->GetOutputNameAllocated(outputIdx, allocator).get());
outputsInfo.push_back(g_ortSession->GetOutputTypeInfo(outputIdx));
}
bool verbose=true;
if (verbose)
{
std::cout << "Model " << onnxfilepath << " has " << inputsInfo.size() << " inputs and " << outputsInfo.size() << " outputs." << std::endl;
for(size_t inputIdx=0; inputIdx < g_ortSession->GetInputCount(); inputIdx++)
{
std::cout << " Input " << inputIdx << " name: " << inputNames[inputIdx]
<< " type: " << convertEnumToString(inputsInfo[inputIdx].GetTensorTypeAndShapeInfo().GetElementType())
//<< " elementCount: " << (inputsInfo[inputIdx].GetTensorTypeAndShapeInfo().GetElementCount())
<< " shape: ";
auto shape = inputsInfo[inputIdx].GetTensorTypeAndShapeInfo().GetShape();
for(auto i: shape)
{
std::cout << i << ",";
}
std::cout << std::endl;
}
for(size_t outputIdx=0; outputIdx < g_ortSession->GetOutputCount(); outputIdx++)
{
std::cout << " Output " << outputIdx << " name: " << outputNames[outputIdx]
<< " type: " << convertEnumToString(outputsInfo[outputIdx].GetTensorTypeAndShapeInfo().GetElementType())
//<< " elementCount: " << outputsInfo[outputIdx].GetTensorTypeAndShapeInfo().GetElementCount()
<< " shape: ";
auto shape = outputsInfo[outputIdx].GetTensorTypeAndShapeInfo().GetShape();
for(auto i: shape)
{
std::cout << i << ",";
}
std::cout << std::endl;
}
}
std::vector<Ort::Value> inputTensors;
for(size_t inputIdx=0; inputIdx < g_ortSession->GetInputCount(); inputIdx++)
{
ONNXTensorElementDataType orttype = inputsInfo[inputIdx].GetTensorTypeAndShapeInfo().GetElementType();
std::vector<int64_t> shape = inputsInfo[inputIdx].GetTensorTypeAndShapeInfo().GetShape();
std::vector<int64_t> realShape = shape;
std::replace(realShape.begin(), realShape.end(), static_cast<int64_t>(-1), nrOfParallelInputsInOneIteration);
inputTensors.emplace_back(CreateTensor(orttype, realShape));
}
std::vector<Ort::Value> outputTensors;
for(size_t outputIdx=0; outputIdx < g_ortSession->GetOutputCount(); outputIdx++)
{
ONNXTensorElementDataType orttype = outputsInfo[outputIdx].GetTensorTypeAndShapeInfo().GetElementType();
auto shape = outputsInfo[outputIdx].GetTensorTypeAndShapeInfo().GetShape();
std::vector<int64_t> realShape = shape;
std::replace(realShape.begin(), realShape.end(), static_cast<int64_t>(-1), nrOfParallelInputsInOneIteration);
outputTensors.emplace_back(CreateTensor(orttype, realShape));
}
std::vector<const char*> inputNames_cstr;
for (const std::string& name : inputNames) {
inputNames_cstr.push_back(name.c_str());
}
std::vector<const char*> outputNames_cstr;
for (const std::string& name : outputNames) {
outputNames_cstr.push_back(name.c_str());
}
std::vector<Ort::Value*> inputTensor_ptrs;
for(size_t inputIdx=0; inputIdx < g_ortSession->GetInputCount(); inputIdx++)
{
inputTensor_ptrs.push_back(&(inputTensors[inputIdx]));
}
std::vector<Ort::Value*> outputTensor_ptrs;
for(size_t outputIdx=0; outputIdx < g_ortSession->GetOutputCount(); outputIdx++)
{
outputTensor_ptrs.push_back(&(outputTensors[outputIdx]));
}
std::cout << "iterations (i.e. number of inference run used for benchmark): " << numberOfIterations << std::endl;
std::cout << "batch_size (i.e. number of parallel inputs fed to network in one run/iteration): " << nrOfParallelInputsInOneIteration << std::endl;
std::vector<double> durations;
for(size_t iter=0; iter < numberOfIterations; iter++)
{
for(size_t inputIdx=0; inputIdx < g_ortSession->GetInputCount(); inputIdx++)
{
FillTensorWithRandomData(inputTensors[inputIdx]);
}
auto start_time = std::chrono::high_resolution_clock::now();
g_ortSession->Run(Ort::RunOptions(),
inputNames_cstr.data(),
inputTensors.data(),
inputNames_cstr.size(),
outputNames_cstr.data(),
outputTensors.data(),
outputNames_cstr.size());
auto end_time = std::chrono::high_resolution_clock::now();
auto duration = end_time - start_time;
double duration_s = duration.count() / 1000000.0;
durations.push_back(duration_s);
}
double sum = std::accumulate(std::begin(durations), std::end(durations), 0.0);
double m = sum / durations.size();
double accum = 0.0;
std::for_each (std::begin(durations), std::end(durations), [&](const double d) {
accum += (d - m) * (d - m);
});
double stdev = sqrt(accum / (durations.size()-1));
std::cerr << "=======================" << std::endl;
std::cerr << "======= Results =======" << std::endl;
std::cerr << "=======================" << std::endl;
std::cerr << "Time (in seconds) for inference: mean: " << m << " stddev:" << stdev << std::endl;
std::cerr << "Time (in seconds) for input: mean: " << m/nrOfParallelInputsInOneIteration << std::endl;
if (backend == "onnxruntimecuda")
{
Ort::GetApi().ReleaseCUDAProviderOptions(cuda_options);
}
return 0;
}