-
-
Notifications
You must be signed in to change notification settings - Fork 26k
[MRG + 1] FIX gil acquisition in dist_metric #9311
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
Conversation
@@ -1093,22 +1093,29 @@ cdef class PyFuncDistance(DistanceMetric): | |||
self.func = func | |||
self.kwargs = kwargs | |||
|
|||
# in cython < 0.26, GIL was required to be acquired during definition of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this comments or shall I remove it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's helpful
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why we needed to acquire it twice previously. Seems strange. But I've not been following the discussions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There has been some cleanup regarding inconsistencies between nogil and withgil on declaration (including inheritance) vs. definition in previous versions of Cython. This seems like a fine workaround to support both versions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand. I'll try read the back-story when I have time.
sklearn/neighbors/dist_metrics.pyx
Outdated
# in cython < 0.26, GIL was required to be acquired during definition of | ||
# the function and inside the body of the function. This behaviour is not | ||
# allowed in cython >= 0.26 since it is a redundant GIL acquisition. The | ||
# only way to be back compatible is to inherate `dist` from the base class |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
*inherit
@@ -1093,22 +1093,29 @@ cdef class PyFuncDistance(DistanceMetric): | |||
self.func = func | |||
self.kwargs = kwargs | |||
|
|||
# in cython < 0.26, GIL was required to be acquired during definition of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's helpful
@@ -1093,22 +1093,29 @@ cdef class PyFuncDistance(DistanceMetric): | |||
self.func = func | |||
self.kwargs = kwargs | |||
|
|||
# in cython < 0.26, GIL was required to be acquired during definition of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why we needed to acquire it twice previously. Seems strange. But I've not been following the discussions.
sklearn/neighbors/dist_metrics.pyx
Outdated
cdef inline DTYPE_t dist(self, DTYPE_t* x1, DTYPE_t* x2, | ||
ITYPE_t size) except -1 with gil: | ||
ITYPE_t size) nogil except -1: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't get why this should be no gil
.
It seems like a Cython bug. My Cython skills are to weak to know the explanation. I could think about something like: self was kinda of inspected and func was found to be a python object. Therefore dist needed to be declared/defined with gil but the inheritance (with nogil) was messing up, imposing to acquire the gil in the body.
|
Not a cython expert, but it LGTM |
This looks good to me. I think the issue may have been that defining a function as nogil doesn't release the GIL, it simply allows the function to be called when the GIL is released, so the author may have thought that |
The explanation, IIUC, is that Cython requires the |
LGTM. Does this run on cython master? The beta segfaults, right? |
The beta was segfaulting. But it was good on master.
|
The problem seems well understood, let's merge this fix |
Thanks @glemaitre ! |
Thanks! |
Reference Issue
partly addressing #9272
continues #9133
continues #9296
What does this implement/fix? Explain your changes.
Due to the inheritance,
dist
needs to be declared withnogil
.Therefore a
_dist
declaredwith gil
is used and called indist
.Any other comments?