Repository navigation
ENH Speed up histogram gradient categorical data prediction (and perhaps other code) by ensuring C/Cython inlining actually happens - #34486
Conversation
|
Note there are cases of this problem in the code, e.g. |
|
|
||
| cdef void set_bitset(BITSET_DTYPE_C bitset, uint8_t val) noexcept nogil | ||
| for i in range(BITSET_LENGTH): | ||
| bitset[i] = 0 |
There was a problem hiding this comment.
I am curious can one use something like memcpy for this?
There was a problem hiding this comment.
- Note that isn't new code, I'm just moving it.
- Could
memset, yes.
|
@cakedev0 FYI |
|
(I have made progress on |
There was a problem hiding this comment.
Interesting! There's actually many places (e.g. in sklearn/tree/..., that this also occurs.
For my education, Is there some documentation, or evidence beyond the Cython docs saying:
They can contain functions which are well suited for inlining by the C compiler. Such functions should be marked inline, example:
inline.pxd
cdef inline int int_min(int a, int b):
return b if b < a else a
? I'm curious when this happens? Is it whenever it's not in pxd, or sometimes?
I.e. if you look at the corresponding compiled function. In the case of this PR, do we have before/after C++ code that shows the init_bitset is not inlined, and then afterwards is inlined?
Might be of interest for generalized Cython discussion in #17299
|
I inspected the code, yes. I can share examples on Monday. |
|
Looking at Before (in /* Module declarations from "sklearn.utils._bitset" */
static __pyx_t_7sklearn_5utils_9_typedefs_uint8_t (*__pyx_f_7sklearn_5utils_7_bitset_in_bitset_2d_memoryview)(__Pyx_memviewslice, __pyx_t_7sklearn_5utils_9_typedefs_uint8_t, unsigned int); /*proto*/In this PR it's an inline declaration of the function prototype: /* Module declarations from "sklearn.utils._bitset" */
static CYTHON_INLINE __pyx_t_7sklearn_5utils_9_typedefs_uint8_t __pyx_f_7sklearn_5utils_7_bitset_in_bitset_memoryview(__Pyx_memviewslice, __pyx_t_7sklearn_5utils_9_typedefs_uint8_t, int __pyx_skip_dispatch); /*proto*/ |
|
Wow TIL! |
lorentzenchr
left a comment
There was a problem hiding this comment.
LGTM once a changelog is added.
|
I have added a changelog. |
…aps other code) by ensuring C/Cython inlining actually happens (scikit-learn#34486)
Because of the structure of the
.pyx/.pxd, inlining wasn't actually happening for bitview functions; I will file an issue againstcython-lintto catch this problem. Here are the relevant Cython docs; they mention including the whole function in the.pxdin order to ensure inlining.As a result, the usage of bitviews in
_predictor.pyxwas a lot slower.There are probably other codepaths using bitviews that will also be sped up by this change, but this is the one I tried.
Running
OMP_NUM_THREADS=1 python benchmarks/bench_hist_gradient_boosting_adult.py:upstream/main:This branch: