8000 ENH reuse parent histograms as one of the child's histogram by lorentzenchr · Pull Request #27865 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

ENH reuse parent histograms as one of the child's histogram #27865

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
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions doc/whats_new/v1.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,12 @@ Changelog
:pr:`13649` by :user:`Samuel Ronsin <samronsin>`,
initiated by :user:`Patrick O'Reilly <pat-oreilly>`.

- |Efficiency| :class:`ensemble.HistGradientBoostingClassifier` and
:class:`ensemble.HistGradientBoostingRegressor` are now a bit faster by reusing
the parent node's histogram as children node's histogram in the subtraction trick.
In effect, less memory has to be allocated and deallocated.
:pr:`27865` by :user:`Christian Lorentzen <lorentzenchr>`.

- |Efficiency| :class:`ensemble.GradientBoostingClassifier` is faster,
for binary and in particular for multiclass problems thanks to the private loss
function module.
Expand Down
3 changes: 3 additions & 0 deletions sklearn/ensemble/_hist_gradient_boosting/grower.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,9 @@ def split_next(self):
smallest_child.allowed_features,
)
)
# node.histograms is reused in largest_child.histograms. To break cyclic
# memory references and help garbage collection, we set it to None.
node.histograms = None
self.total_compute_hist_time += time() - tic

tic = time()
Expand Down
32 changes: 10 additions & 22 deletions sklearn/ensemble/_hist_gradient_boosting/histogram.pyx
< 8000 /tr>
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ cdef class HistogramBuilder:

def compute_histograms_subtraction(
HistogramBuilder self,
hist_struct [:, ::1] parent_histograms, # IN
hist_struct [:, ::1] parent_histograms, # IN and OUT
hist_struct [:, ::1] sibling_histograms, # IN
const unsigned int [:] allowed_features=None, # IN
):
Expand Down Expand Up @@ -252,16 +252,14 @@ cdef class HistogramBuilder:
-------
histograms : ndarray of HISTOGRAM_DTYPE, shape(n_features, n_bins)
The computed histograms of the current node.
We repurpose parent_histograms for this and don't need to allocate new
memory.
"""

cdef:
int feature_idx
int f_idx
int n_allowed_features = self.n_features
hist_struct [:, ::1] histograms = np.empty(
shape=(self.n_features, self.n_bins),
dtype=HISTOGRAM_DTYPE
)
bint has_interaction_cst = allowed_features is not None
int n_threads = self.n_threads

Expand All @@ -281,9 +279,8 @@ cdef class HistogramBuilder:
self.n_bins,
parent_histograms,
sibling_histograms,
histograms,
)
return histograms
return parent_histograms


cpdef void _build_histogram_naive(
Expand Down Expand Up @@ -313,25 +310,16 @@ cpdef void _build_histogram_naive(
cpdef void _subtract_histograms(
const int feature_idx,
unsigned int n_bins,
hist_struct [:, ::1] hist_a, # IN
hist_struct [:, ::1] hist_a, # IN and OUT
hist_struct [:, ::1] hist_b, # IN
hist_struct [:, ::1] out) noexcept nogil: # OUT
"""compute (hist_a - hist_b) in out"""
) noexcept nogil: # OUT
"""compute hist_a = hist_a - hist_b"""
cdef:
unsigned int i = 0
for i in range(n_bins):
out[feature_idx, i].sum_gradients = (
hist_a[feature_idx, i].sum_gradients -
hist_b[feature_idx, i].sum_gradients
)
out[feature_idx, i].sum_hessians = (
hist_a[feature_idx, i].sum_hessians -
hist_b[feature_idx, i].sum_hessians
)
out[feature_idx, i].count = (
hist_a[feature_idx, i].count -
hist_b[feature_idx, i].count
)
hist_a[feature_idx, i].sum_gradients -= hist_b[feature_idx, i].sum_gradients
hist_a[feature_idx, i].sum_hessians -= hist_b[feature_idx, i].sum_hessians
hist_a[feature_idx, i].count -= hist_b[feature_idx, i].count


cpdef void _build_histogram(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,10 @@ def test_hist_subtraction(constant_hessian):
hist_right,
)

hist_left_sub = np.zeros((1, n_bins), dtype=HISTOGRAM_DTYPE)
hist_right_sub = np.zeros((1, n_bins), dtype=HISTOGRAM_DTYPE)
_subtract_histograms(0, n_bins, hist_parent, hist_right, hist_left_sub)
_subtract_histograms(0, n_bins, hist_parent, hist_left, hist_right_sub)
hist_left_sub = np.copy(hist_parent)
hist_right_sub = np.copy(hist_parent)
_subtract_histograms(0, n_bins, hist_left_sub, hist_right)
_subtract_histograms(0, n_bins, hist_right_sub, hist_left)

for key in ("count", "sum_hessians", "sum_gradients"):
assert_allclose(hist_left[key], hist_left_sub[key], rtol=1e-6)
Expand Down
0