Unverified Commit 4ae4abbe authored by Alberto Ferreira's avatar Alberto Ferreira Committed by GitHub
Browse files

Fix thread-safety in C API's PredictSingleRow (#3771)

By using a unique lock instead of the shared lock the timings are very similar,
but predictions are correct.

Even so, by designing a small C++ benchmark with a very simple LGBM model,more threads on a simple model are slower than
the single-thread case. This is probably due to very small work units,
the lock contention overhead increases.

We should in the future benchmark with more complex models to see if supporting
threading on these calls is worth it in performance gains.

If not, then we could choose to not to provide thread-safety and remove
the locks altogether for maximal throughput.

See https://github.com/microsoft/LightGBM/issues/3751 for timings.

See gist for benchmark code:

  https://gist.github.com/AlbertoEAF/5972db15a27c294bab65b97e1bc4c315
parent ac33a483
...@@ -389,7 +389,7 @@ class Booster { ...@@ -389,7 +389,7 @@ class Booster {
Log::Fatal("The number of features in data (%d) is not the same as it was in training data (%d).\n"\ Log::Fatal("The number of features in data (%d) is not the same as it was in training data (%d).\n"\
"You can set ``predict_disable_shape_check=true`` to discard this error, but please be aware what you are doing.", ncol, boosting_->MaxFeatureIdx() + 1); "You can set ``predict_disable_shape_check=true`` to discard this error, but please be aware what you are doing.", ncol, boosting_->MaxFeatureIdx() + 1);
} }
SHARED_LOCK(mutex_) UNIQUE_LOCK(mutex_)
const auto& single_row_predictor = single_row_predictor_[predict_type]; const auto& single_row_predictor = single_row_predictor_[predict_type];
auto one_row = get_row_fun(0); auto one_row = get_row_fun(0);
auto pred_wrt_ptr = out_result; auto pred_wrt_ptr = out_result;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment