multithreading in linux
Recently i use xgboost in linux. When i use multiple thread to invoke the predict function, the program get stuck. But i run the same code in windows, everything is ok. I wonder why would this happen.
It would be great if you can provide a snippet of code to reproduce the problem. I do not know what you mean by multi-threading here. The predict function in xgboost is internally multi-threaded.
If you mean call bst.predict from multiple thread, there will be indeed problem since predict function was not currently threadsafe. I think that will be changed in future, but for now I would recommend simply reply on xgboost's internal multi-threading to do the job
Tianqi
Thank you for your reply. My situation is just as you say. I've trained a model named bst using xgboost. Then multiple thread call bst.predict function.
I'm not clear how to "reply on xgboost's internal multi-threading" in my situation? Could you give some tips?
Do not use multiple thread to call bst.predict. bst.predict was internally multi-threaded
Ok, I got it. Could i duplicate multiple same models and use these models to support multiple thread ?
Yes you can. But the point is since bst.predict is multi-threaded internally, so you may not gain speedup by doing so.
Eventually bst.predict should be thread safe and I will post to let you know then
I have two question.
First, does "multi-threaded internally" means: if the argument of bst.predict is a list of multiple data, this function can process these data multi-threaded?
Second, My specific situation is: First i train a xgboost model, and save the model in the file. Next, i load the model from the file. Then multiple thread call the model's predict function. Each thread send a predict request at a time. And the thread must get the result at once. So I can't collect data from multiple thread and use the "multi-threaded internally". So what could i do?
Yes, bst.predict will launch multiple thread to predict the data. I understand your problem of online prediction. For now, you can use bst.copy() which is newly added feature to copy one booster for each thread
Ok, i got it. Thanks for your answer.
Sorry to bother you again.
Is bst.copy() supported in python? I couldn't use bst.copy() in python. When i use bst.copy(), it i get this "'Booster' object has no attribute 'copy'"
It is in latest version in github
I've download the latest version and i check the code. But i still didn't find the copy function. I check the file which you commit "add copy function", but there is only "import copy" and no other.
opps, just checked them in. Sorry about that
Sorry to bring this back up again, but I am doing a similar thing in Java. And I only use one instance of Booster to support multiple threads and it seems to be causing problems. Does this sound correct? I tried doing .copy(), but couldn't find it in the Java API.
I have the same issue. I use Java program which calls c_api.h functions via JNI. Trying to call XGBoosterPredict function from multiple Java threads causes program crash. But this is not stable and cannot be reproduced each time. Doing the same from one thread works perfectly. It's good that XGBoosterPredict is parallel inside itself. But my use case is different. I do not have one big matrix which is needed to be predicted. I have several threads which need to get the predictions each for one instance only. And these instances are not available in advance. Also c_api.h doesn't have booster copy method as I can see now.
Do you have any update on this issue? Or any plans to fix it in the next 2 days )
So what I ended up doing was creating a resource pool of Boosters in Java and using a queue to manage the threads. So when a new thread comes along it either gets an available Booster or waits for one to free up from the pool.
I'm thinking to do the same. How did you clone the boosters? Load from file several times?
The native code is not thread-safe, hence calling the booster via JNI by multiple threads can corrupt the internal state.
Although there's no explicit method for copying a booster, you can use saveModel and loadModel to instantiate a new booster object from an existing one.
I just loaded from file several times. I didn't realize you could load from an existing one.
meet the same problem, the model and the predictor can not be separated, when use on online, multi threads , need load the model multi time, this is cost memory, will this be solved in the future?
Met the same problem using c_api. Hope this can be solved. Mark.