Fix build errors in benchmark

This commit is contained in:
Changming Sun 2020-02-20 00:28:39 -08:00
parent 5306a1241b
commit 85c0989e6f
2 changed files with 58 additions and 35 deletions

View file

@ -5,6 +5,7 @@
#include <core/graph/onnx_protobuf.h>
#include <core/common/logging/logging.h>
#include <core/platform/env.h>
#include <core/platform/threadpool.h>
#include <core/providers/cpu/cpu_execution_provider.h>
#include "core/session/environment.h"
#include <core/common/logging/sinks/clog_sink.h>
@ -12,8 +13,14 @@
#include <core/graph/graph.h>
#include <core/framework/kernel_def_builder.h>
#include <core/session/onnxruntime_c_api.h>
#include <core/session/onnxruntime_cxx_api.h>
#include <core/session/ort_env.h>
#include <unordered_map>
const OrtApi* g_ort = OrtGetApiBase()->GetApi(ORT_API_VERSION);
OrtEnv* env = nullptr;
using namespace onnxruntime;
static void BM_CPUAllocator(benchmark::State& state) {
@ -28,7 +35,9 @@ BENCHMARK(BM_CPUAllocator)->Arg(4)->Arg(sizeof(Tensor));
static void BM_ResolveGraph(benchmark::State& state) {
std::shared_ptr<onnxruntime::Model> model_copy;
auto st = onnxruntime::Model::Load(ORT_TSTR("../models/opset8/test_tiny_yolov2/model.onnx"), model_copy);
auto logger = env->GetLoggingManager()->CreateLogger("test");
auto st =
onnxruntime::Model::Load(ORT_TSTR("../models/opset8/test_tiny_yolov2/model.onnx"), model_copy, nullptr, *logger);
if (!st.IsOK()) {
printf("Parse model failed: %s", st.ErrorMessage().c_str());
abort();
@ -37,7 +46,7 @@ static void BM_ResolveGraph(benchmark::State& state) {
model_copy.reset();
for (auto _ : state) {
state.PauseTiming();
std::shared_ptr<onnxruntime::Model> model = std::make_shared<onnxruntime::Model>(proto);
std::shared_ptr<onnxruntime::Model> model = std::make_shared<onnxruntime::Model>(proto, nullptr, *logger);
onnxruntime::Graph& graph = model->MainGraph();
state.ResumeTiming();
st = graph.Resolve();
@ -49,24 +58,31 @@ static void BM_ResolveGraph(benchmark::State& state) {
}
BENCHMARK(BM_ResolveGraph);
#define ORT_ABORT_ON_ERROR(expr) \
do { \
OrtStatus* onnx_status = (expr); \
if (onnx_status != NULL) { \
const char* msg = OrtGetErrorMessage(onnx_status); \
fprintf(stderr, "%s\n", msg); \
OrtReleaseStatus(onnx_status); \
abort(); \
} \
#define ORT_ABORT_ON_ERROR(expr) \
do { \
OrtStatus* onnx_status = (expr); \
if (onnx_status != NULL) { \
const char* msg = g_ort->GetErrorMessage(onnx_status); \
fprintf(stderr, "%s\n", msg); \
g_ort->ReleaseStatus(onnx_status); \
abort(); \
} \
} while (0);
OrtEnv* env = nullptr;
static void BM_CreateThreadPool(benchmark::State& state) {
concurrency::ThreadPool::ThreadEnvironment env;
for (auto _ : state) {
onnxruntime::concurrency::ThreadPool tp(48, true, env);
}
}
BENCHMARK(BM_CreateThreadPool)->UseRealTime()->Unit(benchmark::TimeUnit::kMillisecond);
int main(int argc, char** argv) {
::benchmark::Initialize(&argc, argv);
if (::benchmark::ReportUnrecognizedArguments(argc, argv)) return -1;
ORT_ABORT_ON_ERROR(OrtCreateEnv(ORT_LOGGING_LEVEL_WARNING, "test", &env));
if (::benchmark::ReportUnrecognizedArguments(argc, argv))
return -1;
ORT_ABORT_ON_ERROR(g_ort->CreateEnv(ORT_LOGGING_LEVEL_ERROR, "test", &env));
::benchmark::RunSpecifiedBenchmarks();
OrtReleaseEnv(env);
g_ort->ReleaseEnv(env);
return 0;
}

View file

@ -5,12 +5,20 @@
#include <core/graph/model.h>
#include <core/framework/path_lib.h>
#include <core/session/onnxruntime_c_api.h>
#include <core/session/onnxruntime_cxx_api.h>
#include <core/session/ort_env.h>
#include "providers.h"
extern OrtEnv* env;
extern const OrtApi* g_ort;
static void BM_LoadModel(benchmark::State& state) {
auto logger = env->GetLoggingManager()->CreateLogger("test");
for (auto _ : state) {
std::shared_ptr<onnxruntime::Model> yolomodel;
auto st = onnxruntime::Model::Load("../models/opset8/test_tiny_yolov2/model.onnx", yolomodel);
auto st =
onnxruntime::Model::Load(ORT_TSTR("../models/opset8/test_tiny_yolov2/model.onnx"), yolomodel, nullptr, *logger);
if (!st.IsOK()) {
state.SkipWithError(st.ErrorMessage().c_str());
break;
@ -20,30 +28,29 @@ static void BM_LoadModel(benchmark::State& state) {
BENCHMARK(BM_LoadModel);
extern OrtEnv* env;
#define ORT_BREAK_ON_ERROR(expr) \
do { \
OrtStatus* onnx_status = (expr); \
if (onnx_status != NULL) { \
state.SkipWithError(OrtGetErrorMessage(onnx_status)); \
OrtReleaseStatus(onnx_status); \
} \
#define ORT_BREAK_ON_ERROR(expr) \
do { \
OrtStatus* onnx_status = (expr); \
if (onnx_status != NULL) { \
state.SkipWithError(g_ort->GetErrorMessage(onnx_status)); \
g_ort->ReleaseStatus(onnx_status); \
} \
} while (0);
#ifdef USE_CUDA
static void BM_CreateSession_WithGPU(benchmark::State& state) {
const char* model_path = "../models/opset8/test_bvlc_alexnet/model.onnx";
OrtSessionOptions* session_option = OrtCreateSessionOptions();
ORT_BREAK_ON_ERROR(OrtSessionOptionsAppendExecutionProvider_CUDA(session_option, 0));
const ORTCHAR_T* model_path = ORT_TSTR("../models/opset8/test_bvlc_alexnet/model.onnx");
OrtSessionOptions* session_option;
ORT_BREAK_ON_ERROR(g_ort->CreateSessionOptions(&session_option));
ORT_BREAK_ON_ERROR(g_ort->SessionOptionsAppendExecutionProvider_CUDA(session_option, 0));
for (auto _ : state) {
OrtSession* session;
ORT_BREAK_ON_ERROR(OrtCreateSession(env, model_path, session_option, &session));
ORT_BREAK_ON_ERROR(g_ort->CreateSession(env, model_path, session_option, &session));
state.PauseTiming();
OrtReleaseSession(session);
g_ort->ReleaseSession(session);
state.ResumeTiming();
}
OrtReleaseSessionOptions(session_option);
g_ort->ReleaseSessionOptions(session_option);
}
BENCHMARK(BM_CreateSession_WithGPU);
#endif
@ -51,14 +58,14 @@ BENCHMARK(BM_CreateSession_WithGPU);
static void BM_CreateSession(benchmark::State& state) {
const ORTCHAR_T* model_path = ORT_TSTR("../models/opset8/test_bvlc_alexnet/model.onnx");
OrtSessionOptions* session_option;
ORT_BREAK_ON_ERROR(OrtCreateSessionOptions(&session_option));
ORT_BREAK_ON_ERROR(g_ort->CreateSessionOptions(&session_option));
for (auto _ : state) {
OrtSession* session;
ORT_BREAK_ON_ERROR(OrtCreateSession(env, model_path, session_option, &session));
ORT_BREAK_ON_ERROR(g_ort->CreateSession(env, model_path, session_option, &session));
state.PauseTiming();
OrtReleaseSession(session);
g_ort->ReleaseSession(session);
state.ResumeTiming();
}
OrtReleaseSessionOptions(session_option);
g_ort->ReleaseSessionOptions(session_option);
}
BENCHMARK(BM_CreateSession);