Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

FIX Deep copy criterion in trees to fix concurrency bug #19580

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

Merged
merged 1 commit into from
Mar 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions 10 doc/whats_new/v0.24.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ Changelog
:class:`~sklearn.semi_supervised.LabelPropagation`.
:pr:`19271` by :user:`Zhaowei Wang <ThuWangzw>`.

:mod:`sklearn.tree`
.......................
samdbrice marked this conversation as resolved.
Show resolved Hide resolved

- |Fix| Fix a bug in `fit` of :class:`tree.BaseDecisionTree` that caused
segmentation faults under certain conditions. `fit` now deep copies the
`Criterion` object to prevent shared concurrent accesses.
:pr:`19580` by :user:`Samuel Brice <samdbrice>` and
:user:`Alex Adamson <aadamson>` and
:user:`Wil Yegelwel <wyegelwel>`.

:mod:`sklearn.utils`
....................

Expand Down
18 changes: 18 additions & 0 deletions 18 sklearn/ensemble/tests/test_forest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1494,3 +1494,21 @@ def test_n_features_deprecation(Estimator):

with pytest.warns(FutureWarning, match="n_features_ was deprecated"):
est.n_features_


@pytest.mark.parametrize('Forest', FOREST_REGRESSORS)
def test_mse_criterion_object_segfault_smoke_test(Forest):
# This is a smoke test to ensure that passing a mutable criterion
# does not cause a segfault when fitting with concurrent threads.
# Non-regression test for:
# https://github.com/scikit-learn/scikit-learn/issues/12623
from sklearn.tree._criterion import MSE

y = y_reg.reshape(-1, 1)
n_samples, n_outputs = y.shape
mse_criterion = MSE(n_outputs, n_samples)
est = FOREST_REGRESSORS[Forest](
n_estimators=2, n_jobs=2, criterion=mse_criterion
)

est.fit(X_reg, y)
5 changes: 5 additions & 0 deletions 5 sklearn/tree/_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import numbers
import warnings
import copy
from abc import ABCMeta
from abc import abstractmethod
from math import ceil
Expand Down Expand Up @@ -349,6 +350,10 @@ def fit(self, X, y, sample_weight=None, check_input=True,
else:
criterion = CRITERIA_REG[self.criterion](self.n_outputs_,
n_samples)
else:
# Make a deepcopy in case the criterion has mutable attributes that
# might be shared and modified concurrently during parallel fitting
criterion = copy.deepcopy(criterion)

SPLITTERS = SPARSE_SPLITTERS if issparse(X) else DENSE_SPLITTERS

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.