From d965ba1e74568fac60fc8ea1c03feded836d8ab1 Mon Sep 17 00:00:00 2001 From: Alberto Ferreira Date: Sun, 29 Mar 2020 11:34:25 +0100 Subject: [PATCH] [profiling] Profile fast predict methods --- CMakeLists.txt | 7 ++++ profiling/profile_single_row_predict.cpp | 27 +++++++++++++++ profiling/profile_single_row_predict_fast.cpp | 33 +++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 profiling/profile_single_row_predict.cpp create mode 100644 profiling/profile_single_row_predict_fast.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 895f2dd26fce..ad5520e3b735 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,6 +15,7 @@ OPTION(USE_R35 "Set to ON if your R version is not earlier than 3.5" OFF) OPTION(USE_TIMETAG "Set to ON to output time costs" OFF) OPTION(USE_DEBUG "Set to ON for Debug mode" OFF) OPTION(BUILD_FOR_R "Set to ON if building lib_lightgbm for use with the R package" OFF) +OPTION(BUILD_PROFILING_TESTS "Set to ON to compile profiling executables for development and benchmarks." OFF) if(APPLE) OPTION(APPLE_OUTPUT_DYLIB "Output dylib shared library" OFF) @@ -234,6 +235,12 @@ file(GLOB SOURCES src/treelearner/*.cpp ) +if(BUILD_PROFILING_TESTS) + # For profiling builds (use -DDEBUG=ON) + add_executable(lightgbm_profile_single_row_predict profiling/profile_single_row_predict.cpp ${SOURCES} src/c_api.cpp) + add_executable(lightgbm_profile_single_row_predict_fast profiling/profile_single_row_predict_fast.cpp ${SOURCES} src/c_api.cpp) +endif(BUILD_PROFILING_TESTS) + add_executable(lightgbm src/main.cpp ${SOURCES}) add_library(_lightgbm SHARED src/c_api.cpp src/lightgbm_R.cpp ${SOURCES}) diff --git a/profiling/profile_single_row_predict.cpp b/profiling/profile_single_row_predict.cpp new file mode 100644 index 000000000000..606f3e309c14 --- /dev/null +++ b/profiling/profile_single_row_predict.cpp @@ -0,0 +1,27 @@ +#include +#include "LightGBM/c_api.h" + +using namespace std; + +int main() { + cout << "start\n"; + + BoosterHandle boosterHandle; + int num_iterations; + LGBM_BoosterCreateFromModelfile("./LightGBM_model.txt", &num_iterations, &boosterHandle); + cout << "Model iterations " << num_iterations<< "\n"; + + double vals[] = { 158.0, 311.4, 2.08207657E7, 10841.0, 0.0, 1.0, 2.0}; // 0.23829552970680268 + + int64_t dummy; + + double score[1]; + for (size_t i = 0; i < 1e5; ++i) { + LGBM_BoosterPredictForMatSingleRow(boosterHandle, vals, C_API_DTYPE_FLOAT64, 7, 1, C_API_PREDICT_NORMAL, num_iterations, "num_threads=1", &dummy, score); + } + cout << "len=" << dummy << endl; + + cout << "Score = " << score[0] << "\n"; + + cout << "end\n"; +} diff --git a/profiling/profile_single_row_predict_fast.cpp b/profiling/profile_single_row_predict_fast.cpp new file mode 100644 index 000000000000..df134b855464 --- /dev/null +++ b/profiling/profile_single_row_predict_fast.cpp @@ -0,0 +1,33 @@ +#include +#include "LightGBM/c_api.h" + +using namespace std; + +int main() { + cout << "start\n"; + + BoosterHandle boosterHandle; + int num_iterations; + LGBM_BoosterCreateFromModelfile("./LightGBM_model.txt", &num_iterations, &boosterHandle); + cout << "Model iterations " << num_iterations<< "\n"; + + double values[] = {158.0, 311.4, 2.08207657E7, 10841.0, 0.0, 1.0, 2.0}; // 0.23829552970680268 + + + FastConfigHandle fastConfigHandle; + LGBM_BoosterPredictForMatSingleRowFastInit(boosterHandle, values, C_API_DTYPE_FLOAT64, 7, "num_threads=1", &fastConfigHandle); + + int64_t dummy; + double score[1]; + for (size_t i = 0; i < 1e5; ++i) { + LGBM_BoosterPredictForMatSingleRowFast(fastConfigHandle, C_API_PREDICT_NORMAL, num_iterations, &dummy, score); + } + + LGBM_FastConfigFree(fastConfigHandle); + + cout << "len=" << dummy << endl; + + cout << "Score = " << score[0] << "\n"; + + cout << "end\n"; +}