2019-03 - 1/1¶
Nogil, numpy, cython¶
I had to implement a custom criterion to optimize a decision tree and I wanted to leverage scikit-learn instead of rewriting my own. Version 0.21 of scikit-learn introduced some changed in the API which make possible to overload an existing criterion and replace some of the logic by another one: _criterion.pyx. The purpose was to show that a fast implementation requires some tricks (see Custom DecisionTreeRegressor adapted to a linear regression) and piecewise_tree_regression_criterion.pyx, piecewise_tree_regression_criterion_fast.pyx for the code. Other than that, every function to overlaod is marked as nogil. Every function or method marked as nogil cannot go through the GIL (see also PEP-0311), which no python object can be created in that method. In fact, no python can be called inside a Cython method protected with nogil. The issue with that is that any numpy method cannot be called.