Skip to content

Commit 2fbfa2f

Browse files
committed
GBenchmarks generated automatically from ONNX models
1 parent a563d26 commit 2fbfa2f

13 files changed

+149
-106
lines changed

cmake/FindONNXRuntime.cmake

Lines changed: 50 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#[============================================================================[
22
3-
Authors: Federico Sossai (fsossai)
4-
Date: 2021/09/07
3+
Author: Federico Sossai (fsossai), 2021
54
Description: CMake script for finding the ONNXRuntime library.
65
Usage: The user must provide the directory in which ONNXRuntime is installed
76
together with the one in which it has been built by setting the
@@ -36,68 +35,67 @@ The module will also define two cache variables::
3635
#]============================================================================]
3736

3837

39-
if(NOT ONNXRuntime_SRC)
40-
message(FATAL_ERROR "ONNXRuntime: please set ONNXRuntime_SRC")
41-
endif()
42-
43-
if(NOT ONNXRuntime_BUILD)
44-
set(ONNXRuntime_BUILD ${ONNXRuntime_SRC}/build)
45-
message(STATUS "Assuming ${ONNXRuntime_BUILD} as the build directory for ONNXRuntime")
46-
endif()
38+
if(ONNXRuntime_SRC)
4739

48-
# Setting ONNXRuntime_OS
49-
set(all_os "Linux" "Windows" "MacOS" "iOS" "Android")
50-
foreach(os ${all_os})
51-
if(IS_DIRECTORY ${ONNXRuntime_BUILD}/${os})
52-
set(ONNXRuntime_OS ${os})
53-
break()
40+
if(NOT ONNXRuntime_BUILD)
41+
set(ONNXRuntime_BUILD ${ONNXRuntime_SRC}/build)
42+
message(STATUS "Assuming ${ONNXRuntime_BUILD} as the build directory for ONNXRuntime")
5443
endif()
55-
endforeach()
5644

57-
if(NOT ONNXRuntime_OS)
58-
message(FATAL_ERROR "ONNXRuntime: no suitable operating system found in the build directory")
59-
endif()
45+
# Setting ONNXRuntime_OS
46+
set(all_os "Linux" "Windows" "MacOS" "iOS" "Android")
47+
foreach(os ${all_os})
48+
if(IS_DIRECTORY ${ONNXRuntime_BUILD}/${os})
49+
set(ONNXRuntime_OS ${os})
50+
break()
51+
endif()
52+
endforeach()
6053

61-
# Setting ONNXRuntime_BUILD_TYPE
62-
set(all_types "Debug" "MinSizeRel" "Release" "RelWithDebInfo")
63-
foreach(type ${all_types})
64-
if(IS_DIRECTORY ${ONNXRuntime_BUILD}/${ONNXRuntime_OS}/${type})
65-
set(ONNXRuntime_BUILD_TYPE ${type})
66-
break()
54+
if(NOT ONNXRuntime_OS)
55+
message(FATAL_ERROR "ONNXRuntime: no suitable operating system found in the build directory")
6756
endif()
68-
endforeach()
69-
70-
# Setting ONNXRuntime_LIBRARIES
71-
if(EXISTS ${ONNXRuntime_BUILD}/${ONNXRuntime_OS}/${ONNXRuntime_BUILD_TYPE}/libonnxruntime.so)
72-
set(ONNXRuntime_LIBRARIES ${ONNXRuntime_BUILD}/${ONNXRuntime_OS}/${ONNXRuntime_BUILD_TYPE})
73-
set(ONNXRuntime_LIBRARY ${ONNXRuntime_LIBRARIES})
74-
75-
# Setting ONNXRuntime_VERSION_*
76-
file(GLOB match REGEX "${ONNXRuntime_LIBRARIES}/libonnxruntime.so.*")
77-
foreach(fname ${match})
78-
get_filename_component(fname ${fname} NAME)
79-
string(REGEX MATCH "[0-9][0-9.]+" ONNXRuntime_VERSION_STRING ${fname})
80-
string(REPLACE "." ";" version ${ONNXRuntime_VERSION_STRING})
81-
list(LENGTH version version_length)
82-
if(${version_length} GREATER 2)
83-
list(GET version 0 ONNXRuntime_VERSION_MAJOR)
84-
list(GET version 1 ONNXRuntime_VERSION_MINOR)
85-
list(GET version 2 ONNXRuntime_VERSION_PATCH)
57+
58+
# Setting ONNXRuntime_BUILD_TYPE
59+
set(all_types "Debug" "MinSizeRel" "Release" "RelWithDebInfo")
60+
foreach(type ${all_types})
61+
if(IS_DIRECTORY ${ONNXRuntime_BUILD}/${ONNXRuntime_OS}/${type})
62+
set(ONNXRuntime_BUILD_TYPE ${type})
8663
break()
8764
endif()
8865
endforeach()
89-
endif()
9066

91-
# Setting ONNXRuntime_INCLUDE_DIRS
92-
if(IS_DIRECTORY ${ONNXRuntime_SRC}/include)
93-
set(ONNXRuntime_INCLUDE_DIRS ${ONNXRuntime_SRC}/include)
94-
set(ONNXRuntime_INCLUDE_DIR ${ONNXRuntime_INCLUDE_DIRS})
95-
endif()
67+
# Setting ONNXRuntime_LIBRARIES
68+
if(EXISTS ${ONNXRuntime_BUILD}/${ONNXRuntime_OS}/${ONNXRuntime_BUILD_TYPE}/libonnxruntime.so)
69+
set(ONNXRuntime_LIBRARIES ${ONNXRuntime_BUILD}/${ONNXRuntime_OS}/${ONNXRuntime_BUILD_TYPE})
70+
set(ONNXRuntime_LIBRARY ${ONNXRuntime_LIBRARIES})
71+
72+
# Setting ONNXRuntime_VERSION_*
73+
file(GLOB match REGEX "${ONNXRuntime_LIBRARIES}/libonnxruntime.so.*")
74+
foreach(fname ${match})
75+
get_filename_component(fname ${fname} NAME)
76+
string(REGEX MATCH "[0-9][0-9.]+" ONNXRuntime_VERSION_STRING ${fname})
77+
string(REPLACE "." ";" version ${ONNXRuntime_VERSION_STRING})
78+
list(LENGTH version version_length)
79+
if(${version_length} GREATER 2)
80+
list(GET version 0 ONNXRuntime_VERSION_MAJOR)
81+
list(GET version 1 ONNXRuntime_VERSION_MINOR)
82+
list(GET version 2 ONNXRuntime_VERSION_PATCH)
83+
break()
84+
endif()
85+
endforeach()
86+
endif()
9687

97-
# Setting
88+
# Setting ONNXRuntime_INCLUDE_DIRS
89+
if(IS_DIRECTORY ${ONNXRuntime_SRC}/include)
90+
set(ONNXRuntime_INCLUDE_DIRS ${ONNXRuntime_SRC}/include)
91+
set(ONNXRuntime_INCLUDE_DIR ${ONNXRuntime_INCLUDE_DIRS})
92+
endif()
93+
94+
endif()
9895

9996
find_package_handle_standard_args(
10097
ONNXRuntime
10198
FOUND_VAR ONNXRuntime_FOUND
10299
REQUIRED_VARS ONNXRuntime_LIBRARIES ONNXRuntime_INCLUDE_DIRS
103-
VERSION_VAR OpenCL_VERSION_STRING)
100+
VERSION_VAR ONNXRuntime_VERSION_STRING
101+
)

root/tmva/sofie/CMakeLists.txt

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,53 @@
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()
1+
# @author Federico Sossai (fsossai)
82

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.
3+
if(ROOT_tmva_FOUND AND ROOT_tmva-sofie_FOUND)
174

18-
#]======================================================================]
19-
set(REQUIRED_MODELS
20-
"Linear_64.onnx"
21-
)
22-
235
# Checking that all required model exist
246
if (NOT ONNX_MODELS_DIR)
257
set(ONNX_MODELS_DIR input_models)
268
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()
9+
file(GLOB ONNX_MODELS "${ONNX_MODELS_DIR}/*.onnx")
3310

3411
# Copying every ONNX model in the input directory to the build directory.
3512
set(out_dir ${CMAKE_CURRENT_BINARY_DIR}/${ONNX_MODELS_DIR})
3613
file(MAKE_DIRECTORY ${out_dir})
37-
foreach(model ${REQUIRED_MODELS})
38-
configure_file(${ONNX_MODELS_DIR}/${model} ${out_dir}/${model} COPYONLY)
14+
foreach(model ${ONNX_MODELS})
15+
get_filename_component(fname ${model} NAME)
16+
configure_file(${model} ${out_dir}/${fname} COPYONLY)
3917
endforeach()
4018

41-
# Creating an inference benchmark
42-
RB_ADD_GBENCHMARK(ONNXRuntimeInference
43-
ONNXRuntimeInference.cxx
19+
# Looking for ONNXRuntime
20+
find_package(ONNXRuntime QUIET)
21+
if(ONNXRuntime_FOUND)
22+
message(STATUS "Found ONNXRuntime (build type: ${ONNXRuntime_BUILD_TYPE}, version: ${ONNXRuntime_VERSION_STRING})")
23+
24+
# Creating an ONNXRuntime inference benchmark for every model
25+
foreach(fname ${ONNX_MODELS})
26+
get_filename_component(fname ${fname} NAME_WE)
27+
set(target_name ONNXRuntime_${fname})
28+
configure_file(ONNXRuntimeInference.cxx.in ${target_name}.cxx)
29+
30+
RB_ADD_GBENCHMARK(${target_name}
31+
${target_name}.cxx
32+
LABEL short
33+
LIBRARIES TMVA onnxruntime
34+
)
35+
target_link_directories(${target_name} PRIVATE ${ONNXRuntime_LIBRARIES})
36+
target_include_directories(${target_name} PRIVATE ${ONNXRuntime_INCLUDE_DIR})
37+
endforeach()
38+
endif()
39+
40+
# Checking the SOFIE compiler
41+
if(NOT EXISTS ${ROOTSYS}/tmva/sofie/test/emitFromONNX)
42+
message(FATAL_ERROR "SOFIE compiler not found")
43+
endif()
44+
45+
# Creating an inference benchmark for SOFIE
46+
RB_ADD_GBENCHMARK(SOFIEInference
47+
SOFIEInference.cxx
4448
LABEL short
45-
LIBRARIES TMVA onnxruntime
49+
LIBRARIES TMVA blas
4650
)
47-
target_link_directories(ONNXRuntimeInference PRIVATE ${ONNXRuntime_LIBRARIES})
48-
target_include_directories(ONNXRuntimeInference PRIVATE ${ONNXRuntime_INCLUDE_DIR})
4951

5052
endif()
5153

root/tmva/sofie/EmitFromONNX.cxx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Author: Federico Sossai, 2021
2+
// Description:
3+
// SOFIE command line compiler.
4+
// Usage example: $./EmitFromONNX in_dir/mymodel.onnx out_dir/mymodel
5+
// Will produce: out_dir/mymodel_FromONNX.hxx
6+
7+
#include <iostream>
8+
9+
#include "TMVA/RModel.hxx"
10+
#include "TMVA/RModelParser_ONNX.hxx"
11+
12+
using namespace TMVA::Experimental::SOFIE;
13+
14+
int main(int argc, char *argv[])
15+
{
16+
if (argc < 2) {
17+
std::cerr << "ERROR: missing input file\n";
18+
return -1;
19+
}
20+
21+
std::string outname = argv[2];
22+
RModelParser_ONNX parser;
23+
RModel model = parser.Parse(argv[1]);
24+
model.Generate();
25+
model.OutputGenerated(outname + "_FromONNX.hxx");
26+
27+
return 0;
28+
}
Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1+
// Author: Federico Sossai (fsossai), 2021
2+
13
#include <benchmark/benchmark.h>
24
#include <onnxruntime/core/session/onnxruntime_cxx_api.h>
35

46
#include <iostream>
57
#include <vector>
68
#include <numeric>
79

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/"
10+
using namespace std;
1111

12-
static void BM_Onnxruntime_Inference(benchmark::State& state)
12+
static void BM_ONNXRuntime_${fname}(benchmark::State& state)
1313
{
14-
const std::string model_path = INPUT_MODELS_DIR "Linear_64.onnx";
14+
const string model_path = "${ONNX_MODELS_DIR}/${fname}.onnx";
1515

1616
Ort::Env env(ORT_LOGGING_LEVEL_WARNING, "benchmark");
1717

@@ -21,42 +21,42 @@ static void BM_Onnxruntime_Inference(benchmark::State& state)
2121

2222
Ort::Session session(env, model_path.c_str(), session_options);
2323

24-
std::vector<const char*> input_node_names(1);
25-
std::vector<const char*> output_node_names(1);
24+
vector<const char*> input_node_names(1);
25+
vector<const char*> output_node_names(1);
2626

2727
Ort::AllocatorWithDefaultOptions allocator;
2828
input_node_names[0] = session.GetInputName(0, allocator);
2929
output_node_names[0] = session.GetOutputName(0, allocator);
3030

3131
// Getting the shapes
3232

33-
std::vector<int64_t> input_node_dims = session
33+
vector<int64_t> input_node_dims = session
3434
.GetInputTypeInfo(0).GetTensorTypeAndShapeInfo().GetShape();
35-
std::vector<int64_t> output_node_dims = session
35+
vector<int64_t> output_node_dims = session
3636
.GetOutputTypeInfo(0).GetTensorTypeAndShapeInfo().GetShape();
3737

3838
// Displaying input tensor shape
3939

40-
std::cout << "Input shape: [ ";
40+
cout << "Input shape: [ ";
4141
for (auto& i: input_node_dims)
42-
std::cout << i << " ";
43-
std::cout << "]" << std::endl;
42+
cout << i << " ";
43+
cout << "]" << endl;
4444

4545
// Displaying output tensor shape
4646

47-
std::cout << "Output shape: [ ";
47+
cout << "Output shape: [ ";
4848
for (auto& i: output_node_dims)
49-
std::cout << i << " ";
50-
std::cout << "]" << std::endl;
49+
cout << i << " ";
50+
cout << "]" << endl;
5151

5252
// Calculating the dimension of the input tensor
5353

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);
54+
size_t input_tensor_size = accumulate(input_node_dims.begin(),
55+
input_node_dims.end(), 1, multiplies<int>());
56+
vector<float> input_tensor_values(input_tensor_size);
5757

5858
// Input tensor initialization
59-
std::fill_n(input_tensor_values.begin(), input_tensor_size, 1.0);
59+
fill_n(input_tensor_values.begin(), input_tensor_size, 1.0);
6060

6161
auto memory_info = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
6262
Ort::Value input_tensor = Ort::Value::CreateTensor<float>(memory_info,
@@ -70,6 +70,6 @@ static void BM_Onnxruntime_Inference(benchmark::State& state)
7070
&input_tensor, 1, output_node_names.data(), 1);
7171
}
7272
}
73-
BENCHMARK(BM_Onnxruntime_Inference);
73+
BENCHMARK(BM_ONNXRuntime_${fname});
7474

7575
BENCHMARK_MAIN();

root/tmva/sofie/SOFIEInference.cxx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Author: Federico Sossai (fsossai), 2021
2+
3+
#include <benchmark/benchmark.h>
4+
5+
#include <iostream>
6+
7+
static void BM_SOFIE_Inference(benchmark::State& state)
8+
{
9+
for (auto _ : state) {
10+
;
11+
}
12+
}
13+
BENCHMARK(BM_SOFIE_Inference);
14+
15+
BENCHMARK_MAIN();
268 Bytes
Binary file not shown.
275 Bytes
Binary file not shown.
240 Bytes
Binary file not shown.
267 Bytes
Binary file not shown.
265 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)