// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #include #include #include #include #include #include #include "core/session/environment.h" #include #include #include #include #include #include #include #include #include #include "test/util/include/telemetry_test_environment.h" #include #include const OrtApi* g_ort = OrtGetApiBase()->GetApi(ORT_API_VERSION); OrtEnv* env = nullptr; using namespace onnxruntime; static void BM_CPUAllocator(benchmark::State& state) { AllocatorPtr cpu_allocator = CPUAllocator::DefaultInstance(); const size_t len = state.range(0); for (auto _ : state) { void* p = cpu_allocator->Alloc(len); cpu_allocator->Free(p); } } BENCHMARK(BM_CPUAllocator) ->Arg(4) ->Arg(sizeof(Tensor)); static void BM_ResolveGraph(benchmark::State& state) { constexpr const ORTCHAR_T* model_path = ORT_TSTR("transformers/test_data/models/gpt2_embedlayer_exp.onnx"); std::shared_ptr model_copy; auto logger = env->GetLoggingManager()->CreateLogger("test"); auto st = onnxruntime::Model::Load(model_path, model_copy, nullptr, *logger); if (!st.IsOK()) { ::std::cerr << "Parse model failed: " << st.ErrorMessage().c_str() << ::std::endl; abort(); } auto proto = model_copy->ToProto(); model_copy.reset(); for (auto _ : state) { state.PauseTiming(); std::shared_ptr model = std::make_shared(proto, nullptr, *logger); onnxruntime::Graph& graph = model->MainGraph(); state.ResumeTiming(); st = graph.Resolve(); if (!st.IsOK()) { ::std::cerr << "Resolve graph failed: " << st.ErrorMessage().c_str() << ::std::endl; abort(); } } } BENCHMARK(BM_ResolveGraph); #define ORT_ABORT_ON_ERROR(expr) \ do { \ OrtStatus* onnx_status = (expr); \ if (onnx_status != NULL) { \ const char* msg = g_ort->GetErrorMessage(onnx_status); \ ::std::cerr << msg << ::std::endl; \ g_ort->ReleaseStatus(onnx_status); \ abort(); \ } \ } while (0); int main(int argc, char** argv) { onnxruntime::test::SuppressTelemetryForTests(); ::benchmark::Initialize(&argc, argv); if (::benchmark::ReportUnrecognizedArguments(argc, argv)) return -1; ORT_ABORT_ON_ERROR(g_ort->CreateEnv(ORT_LOGGING_LEVEL_ERROR, "test", &env)); ::benchmark::RunSpecifiedBenchmarks(); g_ort->ReleaseEnv(env); return 0; }