Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limiting the number of threads used by XGBoost

I want to use XGBoost for online production purposes (Python 2.7 XGBoost API). In order to be able to do that I want to control and limit the number of threads used by XGBoost at the predict operation.

I'm using the sklearn compatible regressor offered by XGBoost (xgboost.XGBRegressor), and been trying to use the param nthread in the constructor of the regressor to limit the max threads used to 1.

Unfortunately, XGBoost keeps using multiple threads regardless of the value set in nthread.

Is there another way to limit XGBoost and force it to perform the predict operation using n=1 threads?

like image 934
ShaharA Avatar asked Dec 24 '22 11:12

ShaharA


1 Answers

I ran into the same issue and figured it out. The correct answer is to set system environment variables. For python scripts:

import os
os.environ['OMP_NUM_THREADS'] = "1"

would work.

Make sure to put these two lines before you import any other package, otherwise it might not work.

like image 93
Korin Du Avatar answered Dec 26 '22 11:12

Korin Du