Skip to content

Commit

Permalink
Fix build errors in benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
snnn committed Feb 20, 2020
1 parent 5306a12 commit 85c0989
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 35 deletions.
46 changes: 31 additions & 15 deletions onnxruntime/test/onnx/microbenchmark/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@
#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>
#include <core/graph/model.h>
#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) {
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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;
}
47 changes: 27 additions & 20 deletions onnxruntime/test/onnx/microbenchmark/modeltest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,45 +28,44 @@ 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

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);

0 comments on commit 85c0989

Please sign in to comment.