Skip to content

Commit 3b965a7

Browse files
committed
Use Google Benchmark for low-level benchmarks
1 parent be594c2 commit 3b965a7

File tree

6 files changed

+47
-42
lines changed

6 files changed

+47
-42
lines changed

benchmarks/c_bench/CMakeLists.txt

+8-4
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@ project(c_bench)
44
set(CMAKE_CXX_STANDARD 11)
55
set(MODEL $ENV{LLEAVES_BENCHMARK_MODEL}) # NYC_taxi / airline / mtpl2
66

7-
add_executable(benchmark c_bench.cpp)
7+
add_executable(c_bench c_bench.cpp)
88

9+
# remove the cached model file
910
file(REMOVE ${MODEL}.o)
11+
# generate new model file
12+
find_package(Python COMPONENTS Interpreter)
1013
add_custom_target(
1114
run ALL
12-
COMMAND python ${CMAKE_CURRENT_SOURCE_DIR}/gen_binary.py ${MODEL}
15+
COMMAND ${Python_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/gen_binary.py ${MODEL}
1316
BYPRODUCTS ${CMAKE_CURRENT_SOURCE_DIR}/${MODEL}.o
1417
COMMENT "Compiling model"
1518
)
16-
add_dependencies(benchmark run)
19+
add_dependencies(c_bench run)
20+
find_package(benchmark REQUIRED)
1721

18-
target_link_libraries(benchmark cnpy ${CMAKE_CURRENT_SOURCE_DIR}/${MODEL}.o)
22+
target_link_libraries(c_bench cnpy ${CMAKE_CURRENT_SOURCE_DIR}/${MODEL}.o benchmark::benchmark)

benchmarks/c_bench/README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
# Setting up C++ benchmark suite
1+
# Setting up Google Benchmark
22

33
To specify the model to be benchmarked
44
set the environment variable ``LLEAVES_BENCHMARK_MODEL`` to one of
55
`airline`, `NYC_taxi`, `mtpl2`.
66

7+
To download the data used for benchmarking there is a bash script `benchmarks/data/setup_data.sh`.
8+
79
```bash
810
mkdir build && cd build
911
export LLEAVES_BENCHMARK_MODEL="mtpl2"
1012
cmake .. && make
11-
./benchmark
13+
./c_bench
1214
```
1315

1416
There is a script to use [toplev](https://github.com/andikleen/pmu-tools) to generate

benchmarks/c_bench/c_bench.cpp

+10-25
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,27 @@
11
#include "c_bench.h"
2-
#include "cnpy.h"
2+
#include <cnpy.h>
3+
#include <benchmark/benchmark.h>
34
#include <algorithm>
45
#include <cstdlib>
56
#include <iostream>
67

7-
#define N_REPEAT 20
8-
9-
int main(int argc, char **argv) {
10-
(void)argc;
11-
(void)argv;
12-
8+
static void bm_lleaves(benchmark::State& state) {
139
char *model_name = std::getenv("LLEAVES_BENCHMARK_MODEL");
14-
std::cout << "Running model " << model_name << "\n";
1510

1611
std::ostringstream model_stream;
1712
model_stream << "../../data/" << model_name << ".npy";
1813
std::string model_file = model_stream.str();
1914
cnpy::NpyArray arr = cnpy::npy_load(model_file);
2015

21-
std::cout << "Batchsize: " << arr.shape[0] << "\n";
22-
2316
auto *loaded_data = arr.data<double>();
24-
ulong n_preds = arr.shape[0] / (ulong)6;
17+
ulong n_preds = arr.shape[0];
2518
auto *out = (double *)(malloc(n_preds * sizeof(double)));
2619

27-
std::array<double, N_REPEAT> timings{};
28-
clock_t start, end;
29-
std::cout << "starting...\n";
30-
for (size_t i = 0; i < N_REPEAT; ++i) {
31-
start = clock();
32-
forest_root(loaded_data, out, (int)0, (int)n_preds);
33-
end = clock();
34-
35-
timings[i] = (double)(end - start) / CLOCKS_PER_SEC;
20+
for (auto _ : state){
21+
// predict over the whole input array
22+
forest_root(loaded_data, out, (int)0, (int)n_preds);
3623
}
37-
std::cout << "...ending, took "
38-
<< std::accumulate(timings.begin(), timings.end(), 0.0) << "\n";
39-
40-
std::cout << "Runtime: " << *std::min_element(timings.begin(), timings.end())
41-
<< "\n";
4224
}
25+
26+
BENCHMARK(bm_lleaves);
27+
BENCHMARK_MAIN();

benchmarks/c_bench/gen_binary.py

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
11
import os
2+
import time
23

34
import lleaves
45

56
model = os.environ["LLEAVES_BENCHMARK_MODEL"]
7+
8+
fcodemodel = os.environ.get("LLEAVES_FCODEMODEL", "large")
9+
finline = os.environ.get("LLEAVES_FINLINE", "True")
10+
assert finline in (None, "True", "False")
11+
fblocksize = os.environ.get("LLEAVES_FBLOCKSIZE", 34)
12+
613
print(f"Generating {model}.o")
714

8-
llvm_model = lleaves.Model(model_file=f"../../../tests/models/{model}/model.txt")
9-
llvm_model.compile(cache=f"../{model}.o")
15+
llvm_model = lleaves.Model(
16+
model_file=f"../../../tests/models/{model}/model.txt",
17+
)
18+
start = time.time()
19+
llvm_model.compile(
20+
cache=f"../{model}.o",
21+
fblocksize=int(fblocksize) if fblocksize else None,
22+
fcodemodel=fcodemodel,
23+
finline=finline == "True",
24+
)
25+
print(f"Compiling took: {time.time() - start}")

benchmarks/data/gen_npy.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,13 @@
55
from benchmarks.train_NYC_model import feature_enginering
66

77
df = pd.read_csv("airline_data_factorized.csv")
8-
airline_X = df.to_numpy(np.float32)
9-
with open("airline.npy", "wb") as f:
10-
np.save(f, airline_X)
8+
airline_X = df.to_numpy(np.float64)
9+
np.save("airline.npy", airline_X)
1110

1211
df = pd.read_parquet("yellow_tripdata_2016-01.parquet", columns=NYC_used_columns)
13-
NYC_X = feature_enginering().fit_transform(df).astype(np.float32)
14-
with open("NYC_taxi.npy", "wb") as f:
15-
np.save(f, NYC_X)
12+
NYC_X = feature_enginering().fit_transform(df).astype(np.float64)
13+
np.save("NYC_taxi.npy", NYC_X)
1614

1715
df = pd.read_parquet("mtpl2.parquet")
18-
mtpl2_X = df.to_numpy(np.float32)
19-
with open("mtpl2.npy", "wb") as f:
20-
np.save(f, mtpl2_X)
16+
mtpl2_X = df.to_numpy(np.float64)
17+
np.save("mtpl2.npy", mtpl2_X)

environment.yml

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ dependencies:
2121
- sphinxcontrib-apidoc
2222
- setuptools-scm
2323
# benchmarks
24+
- benchmark
2425
- treelite
2526
- compilers
2627
- onnxruntime

0 commit comments

Comments
 (0)