Skip to content

Commit 210d497

Browse files
Ashafixmfeurer
andcommitted
fix for n_jobs=-1, resolves #692 (#733)
* fix for n_jobs=-1 * using joblib.cpu_count() instead of custom function * trying to appease flake8 * PEP8 Co-authored-by: Matthias Feurer <[email protected]>
1 parent 275bf18 commit 210d497

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

autosklearn/estimators.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import numpy as np
77
from sklearn.base import BaseEstimator
88
from sklearn.utils.multiclass import type_of_target
9+
import joblib
910

1011
from autosklearn.automl import AutoMLClassifier, AutoMLRegressor, BaseAutoML
1112
from autosklearn.util.backend import create, get_randomized_directory_names
@@ -341,7 +342,11 @@ def fit(self, **kwargs):
341342
output_directory=self.output_folder,
342343
)
343344

344-
self._n_jobs = self.n_jobs
345+
if self.n_jobs == -1:
346+
self._n_jobs = joblib.cpu_count()
347+
else:
348+
self._n_jobs = self.n_jobs
349+
345350
shared_mode = True
346351
seeds = set()
347352
for i in range(self._n_jobs):
@@ -508,7 +513,6 @@ def refit(self, X, y):
508513
self._automl[0].refit(X, y)
509514
return self
510515

511-
512516
def predict(self, X, batch_size=None, n_jobs=1):
513517
return self._automl[0].predict(X, batch_size=batch_size, n_jobs=n_jobs)
514518

test/test_automl/test_estimators.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,20 @@
99
import numpy as np
1010
import numpy.ma as npma
1111

12+
from joblib import cpu_count
13+
1214
import autosklearn.pipeline.util as putil
15+
import autosklearn.estimators # noqa F401
1316
from autosklearn.estimators import AutoSklearnEstimator
1417
from autosklearn.classification import AutoSklearnClassifier
1518
from autosklearn.regression import AutoSklearnRegressor
1619
from autosklearn.metrics import accuracy, f1_macro, mean_squared_error
1720
from autosklearn.automl import AutoMLClassifier, AutoML
1821
from autosklearn.util.backend import Backend, BackendContext
1922
from autosklearn.constants import BINARY_CLASSIFICATION
20-
sys.path.append(os.path.dirname(__file__))
2123
from base import Base
2224

25+
sys.path.append(os.path.dirname(__file__))
2326

2427
class ArrayReturningDummyPredictor(object):
2528
def __init__(self, test):
@@ -479,6 +482,17 @@ def test_fit_n_jobs_2(self):
479482

480483
self.assertEqual(len(seeds), 1)
481484

485+
@unittest.mock.patch('autosklearn.estimators.AutoSklearnEstimator.build_automl')
486+
def test_fit_n_jobs_negative(self, build_automl_patch):
487+
n_cores = cpu_count()
488+
cls = AutoSklearnEstimator(n_jobs=-1)
489+
cls.fit()
490+
self.assertEqual(len(cls._automl), n_cores)
491+
492+
def test_get_number_of_available_cores(self):
493+
n_cores = cpu_count()
494+
self.assertGreaterEqual(n_cores, 1)
495+
482496

483497
class AutoMLClassifierTest(Base, unittest.TestCase):
484498
@unittest.mock.patch('autosklearn.automl.AutoML.predict')

0 commit comments

Comments
 (0)