Skip to content

Commit 80e938d

Browse files
committed
CMake module for ONNXRuntime and first inference benchmark with Linear_64.onnx
1 parent a585eb8 commit 80e938d

File tree

5 files changed

+231
-0
lines changed

5 files changed

+231
-0
lines changed

cmake/FindONNXRuntime.cmake

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#[============================================================================[
2+
3+
Authors: Federico Sossai (fsossai)
4+
Date: 2021/09/07
5+
Description: CMake script for finding the ONNXRuntime library.
6+
Usage: The user must provide the directory in which ONNXRuntime is installed
7+
together with the one in which it has been built by setting the
8+
following variables:
9+
ONNXRuntime_SRC (e.g. /home/repo/onnxruntime) [MADATORY]
10+
ONNXRuntime_BUILD (e.g. /home/repo/onnxruntime/build)
11+
If ONNXRuntime_BUILD is not set ${ONNXRuntime_SRC}/build is assumed
12+
as default value.
13+
14+
Result Variables
15+
^^^^^^^^^^^^^^^^
16+
17+
This module defines the following variables::
18+
19+
ONNXRuntime_FOUND - True if ONNXRuntime was found
20+
ONNXRuntime_INCLUDE_DIRS - include directories for ONNXRuntime
21+
ONNXRuntime_LIBRARIES - link against this library to use ONNXRuntime
22+
ONNXRuntime_VERSION_STRING - Full version of ONNXRuntime (e.g. 1.8.0)
23+
ONNXRuntime_VERSION_MAJOR - The major version of the ONNXRuntime implementation
24+
ONNXRuntime_VERSION_MINOR - The minor version of the ONNXRuntime implementation
25+
ONNXRuntime_VERSION_MINOR - The patch version of the ONNXRuntime implementation
26+
ONNXRuntime_BUILD_TYPE - Describes whether the current build is
27+
- Debug, MinSizeRel Release or RelWithDebInfo
28+
29+
30+
The module will also define two cache variables::
31+
32+
ONNXRuntime_OS - Operating system that the library has been built for
33+
ONNXRuntime_INCLUDE_DIR - Identical to ONNXRuntime_INCLUDE_DIRS
34+
ONNXRuntime_LIBRARY - Identical to ONNXRuntime_LIBRARIES
35+
36+
#]============================================================================]
37+
38+
set(ONNXRuntime_SRC /home/fsossai/repo/onnxruntime) # debug
39+
40+
if(NOT ONNXRuntime_SRC)
41+
message(FATAL_ERROR "ONNXRuntime: please set ONNXRuntime_SRC")
42+
endif()
43+
44+
if(NOT ONNXRuntime_BUILD)
45+
set(ONNXRuntime_BUILD ${ONNXRuntime_SRC}/build)
46+
message(STATUS "Assuming ${ONNXRuntime_BUILD} as the build directory for ONNXRuntime")
47+
endif()
48+
49+
# Setting ONNXRuntime_OS
50+
set(all_os "Linux" "Windows" "MacOS" "iOS" "Android")
51+
foreach(os ${all_os})
52+
if(IS_DIRECTORY ${ONNXRuntime_BUILD}/${os})
53+
set(ONNXRuntime_OS ${os})
54+
break()
55+
endif()
56+
endforeach()
57+
58+
if(NOT ONNXRuntime_OS)
59+
message(FATAL_ERROR "ONNXRuntime: no suitable operating system found in the build directory")
60+
endif()
61+
62+
# Setting ONNXRuntime_BUILD_TYPE
63+
set(all_types "Debug" "MinSizeRel" "Release" "RelWithDebInfo")
64+
foreach(type ${all_types})
65+
if(IS_DIRECTORY ${ONNXRuntime_BUILD}/${ONNXRuntime_OS}/${type})
66+
set(ONNXRuntime_BUILD_TYPE ${type})
67+
break()
68+
endif()
69+
endforeach()
70+
71+
# Setting ONNXRuntime_LIBRARIES
72+
if(EXISTS ${ONNXRuntime_BUILD}/${ONNXRuntime_OS}/${ONNXRuntime_BUILD_TYPE}/libonnxruntime.so)
73+
set(ONNXRuntime_LIBRARIES ${ONNXRuntime_BUILD}/${ONNXRuntime_OS}/${ONNXRuntime_BUILD_TYPE})
74+
set(ONNXRuntime_LIBRARY ${ONNXRuntime_LIBRARIES})
75+
76+
# Setting ONNXRuntime_VERSION_*
77+
file(GLOB match REGEX "${ONNXRuntime_LIBRARIES}/libonnxruntime.so.*")
78+
foreach(fname ${match})
79+
get_filename_component(fname ${fname} NAME)
80+
string(REGEX MATCH "[0-9][0-9.]+" ONNXRuntime_VERSION_STRING ${fname})
81+
string(REPLACE "." ";" version ${ONNXRuntime_VERSION_STRING})
82+
list(LENGTH version version_length)
83+
if(${version_length} GREATER 2)
84+
list(GET version 0 ONNXRuntime_VERSION_MAJOR)
85+
list(GET version 1 ONNXRuntime_VERSION_MINOR)
86+
list(GET version 2 ONNXRuntime_VERSION_PATCH)
87+
break()
88+
endif()
89+
endforeach()
90+
endif()
91+
92+
# Setting ONNXRuntime_INCLUDE_DIRS
93+
if(IS_DIRECTORY ${ONNXRuntime_SRC}/include)
94+
set(ONNXRuntime_INCLUDE_DIRS ${ONNXRuntime_SRC}/include)
95+
set(ONNXRuntime_INCLUDE_DIR ${ONNXRuntime_INCLUDE_DIRS})
96+
endif()
97+
98+
# Setting
99+
100+
find_package_handle_standard_args(
101+
ONNXRuntime
102+
FOUND_VAR ONNXRuntime_FOUND
103+
REQUIRED_VARS ONNXRuntime_LIBRARIES ONNXRuntime_INCLUDE_DIRS
104+
VERSION_VAR OpenCL_VERSION_STRING)

root/tmva/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
add_subdirectory(tmva)
2+
add_subdirectory(sofie)

root/tmva/sofie/CMakeLists.txt

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
if(ROOT_tmva_FOUND AND ROOT_tmva-sofie_FOUND)
2+
3+
# Looking for ONNXRuntime
4+
find_package(ONNXRuntime REQUIRED)
5+
if(ONNXRuntime_FOUND)
6+
message(STATUS "Found ONNXRuntime (build type: ${ONNXRuntime_BUILD_TYPE}, version: ${ONNXRuntime_VERSION_STRING})")
7+
endif()
8+
9+
#[======================================================================[
10+
11+
REQUIRED MODELS
12+
13+
The following models are required because are loaded by at least
14+
one or more benchmarks.
15+
The abscence of at least one of the following models from the
16+
${ONNX_MODELS_DIR} will cause CMake to throw a fatal error message.
17+
18+
#]======================================================================]
19+
set(REQUIRED_MODELS
20+
"Linear_64.onnx"
21+
)
22+
23+
# Checking that all required model exist
24+
if (NOT ONNX_MODELS_DIR)
25+
set(ONNX_MODELS_DIR input_models)
26+
endif()
27+
foreach(model ${REQUIRED_MODELS})
28+
if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${ONNX_MODELS_DIR}/${model})
29+
message(FATAL_ERROR "Missing a required ONNX input model. "
30+
"\"${model}\" is missing from ${CMAKE_CURRENT_SOURCE_DIR}/${ONNX_MODELS_DIR}")
31+
endif()
32+
endforeach()
33+
34+
# Copying every ONNX model in the input directory to the build directory.
35+
set(out_dir ${CMAKE_CURRENT_BINARY_DIR}/${ONNX_MODELS_DIR})
36+
file(MAKE_DIRECTORY ${out_dir})
37+
foreach(model ${REQUIRED_MODELS})
38+
configure_file(${ONNX_MODELS_DIR}/${model} ${out_dir}/${model} COPYONLY)
39+
endforeach()
40+
41+
# Creating an inference benchmark
42+
RB_ADD_GBENCHMARK(ONNXRuntimeInference
43+
ONNXRuntimeInference.cxx
44+
LABEL short
45+
LIBRARIES TMVA onnxruntime
46+
)
47+
target_link_directories(ONNXRuntimeInference PRIVATE ${ONNXRuntime_LIBRARIES})
48+
target_include_directories(ONNXRuntimeInference PRIVATE ${ONNXRuntime_INCLUDE_DIR})
49+
50+
endif()
51+
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include <benchmark/benchmark.h>
2+
#include <onnxruntime/core/session/onnxruntime_cxx_api.h>
3+
4+
#include <iostream>
5+
#include <vector>
6+
#include <numeric>
7+
8+
// Directory where ONNX models are placed, this must be consistent with
9+
// the directory specified in the CMakeLists.txt.
10+
#define INPUT_MODELS_DIR "input_models/"
11+
12+
static void BM_Onnxruntime_Inference(benchmark::State& state)
13+
{
14+
const std::string model_path = INPUT_MODELS_DIR "Linear_64.onnx";
15+
16+
Ort::Env env(ORT_LOGGING_LEVEL_WARNING, "benchmark");
17+
18+
Ort::SessionOptions session_options;
19+
session_options.SetIntraOpNumThreads(1);
20+
session_options.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_ENABLE_EXTENDED);
21+
22+
Ort::Session session(env, model_path.c_str(), session_options);
23+
24+
std::vector<const char*> input_node_names(1);
25+
std::vector<const char*> output_node_names(1);
26+
27+
Ort::AllocatorWithDefaultOptions allocator;
28+
input_node_names[0] = session.GetInputName(0, allocator);
29+
output_node_names[0] = session.GetOutputName(0, allocator);
30+
31+
// Getting the shapes
32+
33+
std::vector<int64_t> input_node_dims = session
34+
.GetInputTypeInfo(0).GetTensorTypeAndShapeInfo().GetShape();
35+
std::vector<int64_t> output_node_dims = session
36+
.GetOutputTypeInfo(0).GetTensorTypeAndShapeInfo().GetShape();
37+
38+
// Displaying input tensor shape
39+
40+
std::cout << "Input shape: [ ";
41+
for (auto& i: input_node_dims)
42+
std::cout << i << " ";
43+
std::cout << "]" << std::endl;
44+
45+
// Displaying output tensor shape
46+
47+
std::cout << "Output shape: [ ";
48+
for (auto& i: output_node_dims)
49+
std::cout << i << " ";
50+
std::cout << "]" << std::endl;
51+
52+
// Calculating the dimension of the input tensor
53+
54+
size_t input_tensor_size = std::accumulate(input_node_dims.begin(),
55+
input_node_dims.end(), 1, std::multiplies<int>());
56+
std::vector<float> input_tensor_values(input_tensor_size);
57+
58+
// Input tensor initialization
59+
std::fill_n(input_tensor_values.begin(), input_tensor_size, 1.0);
60+
61+
auto memory_info = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
62+
Ort::Value input_tensor = Ort::Value::CreateTensor<float>(memory_info,
63+
input_tensor_values.data(), input_tensor_size,
64+
input_node_dims.data(), input_node_dims.size());
65+
66+
// Running the model
67+
68+
for (auto _ : state) {
69+
session.Run(Ort::RunOptions{nullptr}, input_node_names.data(),
70+
&input_tensor, 1, output_node_names.data(), 1);
71+
}
72+
}
73+
BENCHMARK(BM_Onnxruntime_Inference);
74+
75+
BENCHMARK_MAIN();
103 KB
Binary file not shown.

0 commit comments

Comments
 (0)