-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Passing default num_threads to booster.predict not working #4607
Comments
@yinweisu Thanks for raising this issue! |
@StrikerRUS Thanks for confirming the issue! |
Some updates about this issue: I noticed that setting num_threads equal to a very large number, i.e. 999, would cause mem leak |
Hmm, I guess this can be a separate bug. How did you notice memory leaks? We'd really appreciate a new issue with small reproducible example like one you've used in your original comment here. |
The problem is rooted here, Lines 1807 to 1821 in a77260f
when num_threads <= 0 , it is being ignored and the current setting of number of threads in OpenMP is kept.
Just wondering is there a way to find the original default number of threads used by OpenMP, except query the environment variable |
I did some investigation on this topic. OpenMP has the following 4 ways to determine the number of the thread it uses:
The priority of these ways is 1 > 2 > 3 > 4. As for this issue, the main cause is: We first call It can be also reproduced by: 1. set I submit a PR #4704 which records the default thread setting and reset the thread number back when |
@hzy46 Great, I think this is a satisfactory solution. Thanks! Let's set aside the change of |
I believe we should at least document this clearly. scikit-learn suggests this way of changing number of threads in their docs. So I think users might expect the same (default) behavior from LightGBM as well. |
Cross-linking the following issue here: #4705. |
There's actually more issues coming from this OMP usage. Ideally, the lightgbm functions should never set the number of global threads through The official scikit-learn docs say that, for scikit-learn estimators, OMP-based parallelization should follow the environment variable for getting the number of threads, but I think such recommendation is counterintuive and unergonomic (i.e. most users would expect to be able to control the number of threads from It would be better to have the number of threads set for each OMP pragma separately:
(this would require changing many function signatures though) However, that will not ignore invalid values such as 0 - these have to be manually checked beforehand and set to a minimum of 1 or so, or it could alternatively be made throw an exception or a warning in such cases. In the python package, additionally, I think it would be expected that negative numbers of threads follow the joblib formula:
(with an additional check that the number that's obtained from that is still valid) From a design POV, it'd additional be helpful to be able to set the number of threads in a fitted model object. (And BTW while you're at it, please don't use |
@david-cortes Thanks. I agree. Calling |
This issue has been automatically locked since there has not been any recent activity since it was closed. To start a new related discussion, open a new issue at https://github.com/microsoft/LightGBM/issues including a reference to this. |
Description
Passing num_threads=-1, or 0 or None to booster.predict method will not take effect. Instead, it will continue to use num_threads specified by the train method. Currently, the workaround I found is passing a huge value, i.e. 999.
One can pass the number of cores as an argument themselves of course. However, user should be able to provide a default value and lgb should auto use all the cores
Reproducible example
Environment info
LightGBM version or commit hash: 3.2.1
Command(s) you used to install LightGBM
Additional Comments
The text was updated successfully, but these errors were encountered: