Skip to content

[MRG] Workaround limitation of cloudpickle under PyPy #12566

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
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
30 changes: 24 additions & 6 deletions sklearn/neighbors/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,15 @@ def _pairwise(self):
return self.metric == 'precomputed'


def _tree_query_parallel_helper(tree, data, n_neighbors, return_distance):
"""Helper for the Parallel calls in KNeighborsMixin.kneighbors

The Cython method tree.query is not directly picklable by cloudpickle
under PyPy.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting that this works while pickling directly fails. I don't really have a good understanding of cloudpickle (or pickling in general) to have much to say about this. LGTM since it fixes the original failure.

"""
return tree.query(data, n_neighbors, return_distance)


class KNeighborsMixin(object):
"""Mixin for k-neighbors searches"""

Expand Down Expand Up @@ -433,15 +442,15 @@ class from an array representing our data set and ask who's
if (sys.version_info < (3,) or
LooseVersion(joblib_version) < LooseVersion('0.12')):
# Deal with change of API in joblib
delayed_query = delayed(self._tree.query,
delayed_query = delayed(_tree_query_parallel_helper,
check_pickle=False)
parallel_kwargs = {"backend": "threading"}
else:
delayed_query = delayed(self._tree.query)
delayed_query = delayed(_tree_query_parallel_helper)
parallel_kwargs = {"prefer": "threads"}
result = Parallel(n_jobs, **parallel_kwargs)(
delayed_query(
X[s], n_neighbors, return_distance)
self._tree, X[s], n_neighbors, return_distance)
for s in gen_even_slices(X.shape[0], n_jobs)
)
else:
Expand Down Expand Up @@ -562,6 +571,15 @@ def kneighbors_graph(self, X=None, n_neighbors=None,
return kneighbors_graph


def _tree_query_radius_parallel_helper(tree, data, radius, return_distance):
"""Helper for the Parallel calls in RadiusNeighborsMixin.radius_neighbors

The Cython method tree.query_radius is not directly picklable by
cloudpickle under PyPy.
"""
return tree.query_radius(data, radius, return_distance)


class RadiusNeighborsMixin(object):
"""Mixin for radius-based neighbors searches"""

Expand Down Expand Up @@ -718,14 +736,14 @@ class from an array representing our data set and ask who's
n_jobs = effective_n_jobs(self.n_jobs)
if LooseVersion(joblib_version) < LooseVersion('0.12'):
# Deal with change of API in joblib
delayed_query = delayed(self._tree.query_radius,
delayed_query = delayed(_tree_query_radius_parallel_helper,
check_pickle=False)
parallel_kwargs = {"backend": "threading"}
else:
delayed_query = delayed(self._tree.query_radius)
delayed_query = delayed(_tree_query_radius_parallel_helper)
parallel_kwargs = {"prefer": "threads"}
results = Parallel(n_jobs, **parallel_kwargs)(
delayed_query(X[s], radius, return_distance)
delayed_query(self._tree, X[s], radius, return_distance)
for s in gen_even_slices(X.shape[0], n_jobs)
)
if return_distance:
Expand Down