-
-
Notifications
You must be signed in to change notification settings - Fork 26k
ENH Greatly reduces memory usage of histogram gradient boosting #18242
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
Closed
thomasjpfan
wants to merge
30
commits into
scikit-learn:main
from
thomasjpfan:memory_hist_gradient_boosting
Closed
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
4c750e7
ENH Greatly reduces memory usage of histogram gradient boosting
thomasjpfan eacbd1b
CLN Fill histograms when reseting
thomasjpfan a03d8e8
CLN Rename to pool
thomasjpfan b1e71f3
DOC Adds whats new
thomasjpfan 9e43c6b
STY Linting
thomasjpfan a6724f4
ENH Only reset histograms that were used to zero
thomasjpfan 7b3d195
CLN Address comments
thomasjpfan 359d917
CLN Address comments
thomasjpfan f29258f
DOC Adds docstring
thomasjpfan ed8cb9b
TST Adds tests for histograms pool
thomasjpfan d2c06a7
DOC Changes tag in whats_new
thomasjpfan 6e99b44
CLN Removes weakref
thomasjpfan 030febe
Merge remote-tracking branch 'upstream/master' into memory_hist_gradi…
thomasjpfan b059bb4
CLN Lowers diff
thomasjpfan 527d12d
ENH Speeds up algorithm more
thomasjpfan e6275d8
REV Revert diff
thomasjpfan 8645ec9
CLN Address comments
thomasjpfan 5423850
Merge branch 'master' into memory_hist_gradient_boosting
ogrisel 537d734
CLN Adds comments and renames the pool
thomasjpfan dd4be64
ENH: release histograms earlier
ogrisel a1f34e3
PEP8
ogrisel 59829c5
Update sklearn/ensemble/_hist_gradient_boosting/_histogram_pool.py
ogrisel 0094559
Update doc/whats_new/v0.24.rst
ogrisel 7067e9c
CLN Only need to release
thomasjpfan a940c15
ENH free leaf histograms as soon as possible
ogrisel e327c08
Style: avoid for / else
ogrisel c2e9e10
Remove useless TreeNode attributes to break cyclic references
ogrisel bc40625
Merge remote-tracking branch 'origin/master' into memory_hist_gradien…
ogrisel 8ce38d1
Update motivation for HistogramPool
ogrisel be4788e
Merge branch 'master' of github.com:scikit-learn/scikit-learn into me…
NicolasHug File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
sklearn/ensemble/_hist_gradient_boosting/_histogram_pool.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import numpy as np | ||
from .common import HISTOGRAM_DTYPE | ||
|
||
|
||
class HistogramPool: | ||
"""Histogram pool to be used by the growers. | ||
|
||
The pool allocates and returns histograms to the caller. When the `reset` | ||
method is called, all the previously allocated histograms will be available | ||
for the next grower to use. New histograms will be allocated when there are | ||
no histograms left in the available_pool. HistogramPool is used for memory | ||
allocation/management only. The computation of the histograms is done in | ||
HistogramBuilder. | ||
|
||
Empirically, this strategy has proven to be more memory efficient (on macOS | ||
in particular) than allocating new histograms each time and relying on the | ||
Python GC to keep the overall python process memory low when fitting GBRT | ||
on datasets with many features and target classes. | ||
""" | ||
def __init__(self, n_features, n_bins): | ||
self.n_features = n_features | ||
self.n_bins = n_bins | ||
self.available_pool = [] | ||
self.used_pool = [] | ||
|
||
def reset(self): | ||
"""Reset the pool.""" | ||
self.available_pool.extend(self.used_pool) | ||
self.used_pool = [] | ||
|
||
def get(self): | ||
"""Return a non-initialized array of histogram for one grower node.""" | ||
if self.available_pool: | ||
histograms = self.available_pool.pop() | ||
else: | ||
histograms = np.empty( | ||
shape=(self.n_features, self.n_bins), dtype=HISTOGRAM_DTYPE | ||
) | ||
self.used_pool.append(histograms) | ||
return histograms | ||
|
||
def release(self, histograms): | ||
"""Move a specific histogram array to the available pool""" | ||
try: | ||
idx = next(idx for idx, h in enumerate(self.used_pool) | ||
if h is histograms) | ||
except StopIteration as e: | ||
raise ValueError("Could not find histograms in used_pool") from e | ||
self.used_pool.pop(idx) | ||
self.available_pool.append(histograms) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
sklearn/ensemble/_hist_gradient_boosting/tests/test_histogram_pool.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from sklearn.ensemble._hist_gradient_boosting._histogram_pool import ( | ||
HistogramPool | ||
) | ||
import pytest | ||
|
||
|
||
def test_histograms_pool(): | ||
# simple check how HistogramPool manages state | ||
n_features, n_bins = 20, 5 | ||
pool = HistogramPool(n_features=n_features, n_bins=n_bins) | ||
|
||
histograms1 = pool.get() | ||
assert histograms1.shape == (n_features, n_bins) | ||
|
||
assert pool.used_pool == [histograms1] | ||
assert pool.available_pool == [] | ||
|
||
histograms2 = pool.get() | ||
assert histograms2.shape == (n_features, n_bins) | ||
assert pool.used_pool == [histograms1, histograms2] | ||
assert pool.available_pool == [] | ||
|
||
pool.release(histograms1) | ||
assert pool.used_pool == [histograms2] | ||
assert pool.available_pool == [histograms1] | ||
|
||
# Cannot release an already released histogram | ||
with pytest.raises(ValueError): | ||
pool.release(histograms1) | ||
|
||
# when pool is reset histograms in the used pool is moved to the | ||
# avaliable pool | ||
pool.reset() | ||
assert pool.available_pool == [histograms1, histograms2] | ||
assert pool.used_pool == [] | ||
|
||
histograms3 = pool.get() | ||
assert histograms3.shape == (n_features, n_bins) | ||
|
||
# only histograms1 is in the avaliable pool | ||
assert pool.available_pool == [histograms1] | ||
assert histograms3 is histograms2 | ||
assert pool.used_pool == [histograms3] | ||
ogrisel marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Maybe make it explicit that empirically, this strategy has proven much more memory efficient than allocating new histograms each time and relying on the Python GC to keep the overall python process memory low when fitting GBRT on datasets with many features and target classes.
Uh oh!
There was an error while loading. Please reload this page.
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 believe this comment (and the one in the code) might be outdated considering that the memory boost mainly came from removing cycles