Skip to content

ENH Speed up histogram gradient categorical data prediction (and perhaps other code) by ensuring C/Cython inlining actually happens - #34486

Merged
lorentzenchr merged 2 commits into
scikit-learn:mainfrom
itamarst:make-inlining-actually-happen
Jul 20, 2026
Merged

lorentzenchr merged 2 commits into
scikit-learn:mainfrom
itamarst:make-inlining-actually-happen

Conversation

@itamarst

@itamarst itamarst commented Jul 15, 2026 •

Copy link
Copy Markdown
Contributor

Because of the structure of the .pyx/.pxd, inlining wasn't actually happening for bitview functions; I will file an issue against cython-lint to catch this problem. Here are the relevant Cython docs; they mention including the whole function in the .pxd in order to ensure inlining.

As a result, the usage of bitviews in _predictor.pyx was 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:

fitted in 0.449s
predicted in 0.121s, ROC AUC: 0.9172, ACC: 0.8629

This branch:

fitted in 0.423s
predicted in 0.078s, ROC AUC: 0.9172, ACC: 0.8629

@itamarst itamarst changed the title Speed up histogram bin raw data prediction (and perhaps other code) by ensuring inlining actually happens Speed up histogram gradient raw data prediction (and perhaps other code) by ensuring inlining actually happens Jul 15, 2026
@itamarst itamarst changed the title Speed up histogram gradient raw data prediction (and perhaps other code) by ensuring inlining actually happens Speed up histogram gradient raw categorical data prediction (and perhaps other code) by ensuring inlining actually happens Jul 15, 2026
@itamarst
itamarst marked this pull request as ready for review July 15, 2026 20:54
@itamarst itamarst changed the title Speed up histogram gradient raw categorical data prediction (and perhaps other code) by ensuring inlining actually happens Speed up histogram gradient categorical data prediction (and perhaps other code) by ensuring inlining actually happens Jul 15, 2026
@itamarst

Copy link
Copy Markdown
Contributor Author

Note there are cases of this problem in the code, e.g. sklearn.tree._utils has a number of them. I can do them in same PR, or in separate PR.

Comment thread sklearn/utils/_bitset.pxd

cdef void set_bitset(BITSET_DTYPE_C bitset, uint8_t val) noexcept nogil
for i in range(BITSET_LENGTH):
bitset[i] = 0

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I am curious can one use something like memcpy for this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

  1. Note that isn't new code, I'm just moving it.
  2. Could memset, yes.

@itamarst

Copy link
Copy Markdown
Contributor Author

@cakedev0 FYI

@itamarst

Copy link
Copy Markdown
Contributor Author

(I have made progress on cython-lint lint, so eventually these will be flagged automatically.)

@adam2392 adam2392 left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

@itamarst

Copy link
Copy Markdown
Contributor Author

I inspected the code, yes. I can share examples on Monday.

@itamarst

itamarst commented Jul 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Looking at sklearn/ensemble/histogram/_predictor.pyx, which relies on bitset.pxd, and specifically at the C code it generates:

Before (in main), it's a function pointer that gets loaded later on:

/* 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*/

@adam2392

Copy link
Copy Markdown
Member

Wow TIL!

@adam2392 adam2392 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the cool insight @itamarst !

This LGTM, and I would be keen to fix this in other places it occurs throughout our codebase.

I tagged a few other maintainers to see if they have opinions too.

@lorentzenchr lorentzenchr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM once a changelog is added.

@itamarst

Copy link
Copy Markdown
Contributor Author

I have added a changelog.

@lorentzenchr lorentzenchr changed the title Speed up histogram gradient categorical data prediction (and perhaps other code) by ensuring inlining actually happens ENH Speed up histogram gradient categorical data prediction (and perhaps other code) by ensuring C(ython) inlining actually happens Jul 20, 2026
@lorentzenchr lorentzenchr changed the title ENH Speed up histogram gradient categorical data prediction (and perhaps other code) by ensuring C(ython) inlining actually happens ENH Speed up histogram gradient categorical data prediction (and perhaps other code) by ensuring C/Cython inlining actually happens Jul 20, 2026
@lorentzenchr
lorentzenchr enabled auto-merge (squash) July 20, 2026 16:51
@lorentzenchr
lorentzenchr merged commit 2668f2d into scikit-learn:main Jul 20, 2026
33 checks passed
@itamarst
itamarst deleted the make-inlining-actually-happen branch July 20, 2026 20:31
prady0t pushed a commit to prady0t/scikit-learn that referenced this pull request Sep 2, 2026
…aps other code) by ensuring C/Cython inlining actually happens (scikit-learn#34486)
@jeremiedbb jeremiedbb mentioned this pull request Sep 8, 2026
14 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants