Skip to content

Commit

Permalink
[profiling] Profile fast predict methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Alberto Ferreira committed Apr 11, 2020
1 parent ce384e9 commit d965ba1
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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})

Expand Down
27 changes: 27 additions & 0 deletions profiling/profile_single_row_predict.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <iostream>
#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";
}
33 changes: 33 additions & 0 deletions profiling/profile_single_row_predict_fast.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <iostream>
#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";
}

0 comments on commit d965ba1

Please sign in to comment.