Skip to content

Commit

Permalink
[performance] Revert FastConfig improvement to pass data at init
Browse files Browse the repository at this point in the history
This reduces optimization by 5% / 30% with this branch but makes it so it can be used for higher level wrappers in MMLSpark.
And outside it as well.
  • Loading branch information
AlbertoEAF committed May 15, 2020
1 parent 4f79d01 commit a9eae2b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 26 deletions.
4 changes: 2 additions & 2 deletions include/LightGBM/c_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -898,15 +898,13 @@ LIGHTGBM_C_EXPORT int LGBM_FastConfigFree(FastConfigHandle fastConfig);
* Release the `FastConfig` by passing its handle to `LGBM_FastConfigFree` when no longer needed.
*
* \param handle Booster handle
* \param data Single-row array data (no other way than row-major form).
* \param data_type Type of ``data`` pointer, can be ``C_API_DTYPE_FLOAT32`` or ``C_API_DTYPE_FLOAT64``
* \param ncol Number of columns
* \param parameter Other parameters for prediction, e.g. early stopping for prediction
* \param[out] out_fastConfig FastConfig object with which you can call `LGBM_BoosterPredictForMatSingleRowFast`
* \return 0 when it succeeds, -1 when failure happens
*/
LIGHTGBM_C_EXPORT int LGBM_BoosterPredictForMatSingleRowFastInit(BoosterHandle handle,
const void* data,
int data_type,
int32_t ncol,
const char* parameter,
Expand All @@ -916,6 +914,7 @@ LIGHTGBM_C_EXPORT int LGBM_BoosterPredictForMatSingleRowFastInit(BoosterHandle h
* \brief Score a single row after setup with `LGBM_BoosterPredictForMatSingleRowFastInit`.
*
* \param fastConfig_handle FastConfig object handle returned by `LGBM_BoosterPredictForMatSingleRowFastInit`
* \param data Single-row array data (no other way than row-major form).
* \param predict_type What should be predicted
* - ``C_API_PREDICT_NORMAL``: normal prediction, with transform (if needed);
* - ``C_API_PREDICT_RAW_SCORE``: raw score;
Expand All @@ -927,6 +926,7 @@ LIGHTGBM_C_EXPORT int LGBM_BoosterPredictForMatSingleRowFastInit(BoosterHandle h
* \return 0 when it succeeds, -1 when failure happens
*/
LIGHTGBM_C_EXPORT int LGBM_BoosterPredictForMatSingleRowFast(FastConfigHandle fastConfig_handle,
const void* data,
int predict_type,
int num_iteration,
int64_t* out_len,
Expand Down
49 changes: 25 additions & 24 deletions src/c_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1654,31 +1654,31 @@ int LGBM_BoosterPredictForMatSingleRow(BoosterHandle handle,
*/
struct FastConfig {
public:
FastConfig(Booster *booster_ptr,
FastConfig(Booster *const booster_ptr,
const char *parameter,
std::function<std::vector<std::pair<int, double>>(int row_idx)> row_function,
const int32_t num_cols) : booster(booster_ptr), get_row_function(row_function), ncol(num_cols) {
config.Set(Config::Str2Map(parameter));
const int data_type,
const int32_t num_cols) : _booster(booster_ptr), _data_type(data_type), _ncol(num_cols) {
_config.Set(Config::Str2Map(parameter));
}

friend int LGBM_BoosterPredictForMatSingleRowFastInit(BoosterHandle handle,
const void* data,
int data_type,
int32_t ncol,
const int data_type,
const int32_t ncol,
const char* parameter,
FastConfigHandle *out_fastConfig);

friend int LGBM_BoosterPredictForMatSingleRowFast(FastConfigHandle fast_config_handle,
int predict_type,
int num_iteration,
const void* data,
const int predict_type,
const int num_iteration,
int64_t* out_len,
double* out_result);

private:
Booster* booster;
Config config;
std::function<std::vector<std::pair<int, double>>(int row_idx)> get_row_function;
int32_t ncol;
Booster* const _booster;
Config _config;
const int _data_type;
const int32_t _ncol;
};

int LGBM_FastConfigFree(FastConfigHandle fastConfig) {
Expand All @@ -1688,36 +1688,37 @@ int LGBM_FastConfigFree(FastConfigHandle fastConfig) {
}

int LGBM_BoosterPredictForMatSingleRowFastInit(BoosterHandle handle,
const void* data,
int data_type,
int32_t ncol,
const int data_type,
const int32_t ncol,
const char* parameter,
FastConfigHandle *out_fastConfig) {
API_BEGIN();
auto fastConfig_ptr = std::unique_ptr<FastConfig>(new FastConfig(
reinterpret_cast<Booster*>(handle),
parameter,
RowPairFunctionFromDenseMatric(data, 1, ncol, data_type, 1), // Single row in row-major format.
data_type,
ncol));

if (fastConfig_ptr->config.num_threads > 0) {
omp_set_num_threads(fastConfig_ptr->config.num_threads);
if (fastConfig_ptr->_config.num_threads > 0) {
omp_set_num_threads(fastConfig_ptr->_config.num_threads);
}

*out_fastConfig = fastConfig_ptr.release();
API_END();
}

int LGBM_BoosterPredictForMatSingleRowFast(FastConfigHandle fastConfig_handle,
int predict_type,
int num_iteration,
const void* data,
const int predict_type,
const int num_iteration,
int64_t* out_len,
double* out_result) {
API_BEGIN();
FastConfig *fastConfig = reinterpret_cast<FastConfig*>(fastConfig_handle);
fastConfig->booster->PredictSingleRow(num_iteration, predict_type,
fastConfig->ncol, fastConfig->get_row_function, fastConfig->config,
out_result, out_len);
auto get_row_fun = RowPairFunctionFromDenseMatric(data, 1, fastConfig->_ncol, fastConfig->_data_type, 1); // Single row in row-major format.
fastConfig->_booster->PredictSingleRow(num_iteration, predict_type,
fastConfig->_ncol, get_row_fun, fastConfig->_config,
out_result, out_len);
API_END();
}

Expand Down

0 comments on commit a9eae2b

Please sign in to comment.