Skip to content

ENH expose max_memory_mb parameter to contol trade-off speed/memory during computation - #30111

Open
glemaitre wants to merge 20 commits into
scikit-learn:mainfrom
glemaitre:new_param_max_memory_pd
Open

glemaitre wants to merge 20 commits into
scikit-learn:mainfrom
glemaitre:new_param_max_memory_pd

Conversation

@glemaitre

@glemaitre glemaitre commented Oct 19, 2024 •

Copy link
Copy Markdown
Member

Related to #28375

TODO:

  • changelog entry
  • add tests

This PR proposes to add a new parameter to control the maximum amount of memory allowed to be used during the computation of the partial dependence "brute" computation.

In short, instead of computing a single point from the grid, we could have a batch approach. For each point in the grid, we request the estimator to predict on X. Therefore, the total memory footprint is n_points * X.bytes. So there is a trade-off between memory and speed. We gain speed by calling predict on few matrices reducing non-negligible validation overhead at the cost of a much larger memory footprint.

The new parameter allows to control the memory footprint to not exceed a certain amount.

Benchmark

# %%
import numpy as np
from sklearn.datasets import make_regression
from sklearn.ensemble import RandomForestRegressor
from sklearn.inspection import partial_dependence

X, y = make_regression(n_samples=10_000, n_features=10, random_state=0)
model = RandomForestRegressor(n_jobs=8).fit(X, y)

# %%
# 3.73 s ± 56.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%%timeit
partial_dependence(model, X, features=[0], method="brute", max_memory_mb=1)

# %%
# 1.94 s ± 39.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%%timeit
partial_dependence(model, X, features=[0], method="brute", max_memory_mb=1_024)

When using the private _partial_dependence_brute function in #28375, @mayer79 gets a x10 speed-up that could be handy.

Additional thoughts

Note that we could call _partial_dependence_brute in #28375 and meet the similar speed-up as the current fast computation. I did run the bench exposed in #28375 (comment) and I have similar results. The plus here is that we provide the user a control in case that our default is not suitable.

@github-actions

github-actions Bot commented Oct 19, 2024 •

Copy link
Copy Markdown

✔️ Linting Passed

All linting checks passed. Your pull request is in excellent shape! ☀️

Generated for commit: 08037f1. Link to the linter CI: here

@glemaitre
glemaitre marked this pull request as draft October 19, 2024 11:36
@glemaitre
glemaitre marked this pull request as ready for review October 28, 2024 09:51
@glemaitre

Copy link
Copy Markdown
Member Author

@ogrisel I switch to your preferred alternative. Do you have any other thoughts regarding the feature on its own and if this is worth introducing it?

@betatim

betatim commented Oct 29, 2024

Copy link
Copy Markdown
Member

I like the idea of using batches to speed things up. Is there a way to make use of them without exposing this extra parameter? I'm not sure what I'd set it to, a few GBs sounds reasonable - so maybe that? The default is 1GB, so that sounds about right.

I'm also thinking that probably the speed up vs used memory curve quickly levels off. Going from one sample per call to 100 per call already cuts down the validation overhead by ~100x, to get another factor 100 reduction in overhead you'd have to pass 10_000 samples. And surely by this time the overhead from validation is pretty small compared to the computation time?

All this makes me think: could we make the parameter be a on/off switch that uses a reasonable batch size? Easier to think about for a user, maybe simpler to implement and gets most of the benefit?

@mayer79

mayer79 commented Oct 29, 2024

Copy link
Copy Markdown
Contributor

Or set batch_size = 1?

@glemaitre

Copy link
Copy Markdown
Member Author

ping @adrinjalali to have your thoughts?

@adrinjalali

Copy link
Copy Markdown
Member

I think people have different enough RAM available to them that it makes sense to have this parameter exposed. One could be running this on a very small machine (cloud or not), could be running on a laptop with 8-16GB, or a laptop with 64GB, or a large machine somewhere. So it makes sense to be able to configure this.

The one thing that would make me think we might not want to expose this, is if using 10GB instead of 1GB would NOT make a difference. And a 2-3x difference is already significant enough for me.

@glemaitre

glemaitre commented Oct 30, 2024 •

Copy link
Copy Markdown
Member Author

To answer @adrinjalali question, in the following basic example from our gallery, increasing the max-memory from 1GB to 10GB speed-up the processing from 1.748 s to 0.239 s on my laptop. So I'm more and more convinced that exposing the parameter is interesting even for the end user.

Edit: actually the the way the code is shown, we also measure some caching operation from matplotlib so there is no speed up on this particular example :)

@adrinjalali

Copy link
Copy Markdown
Member

I'm surprised that this is what I see on this branch:

In [1]: # %%
   ...: import numpy as np
   ...: from sklearn.datasets import make_regression
   ...: from sklearn.ensemble import RandomForestRegressor
   ...: from sklearn.inspection import partial_dependence
   ...: 
   ...: X, y = make_regression(n_samples=100_000, n_features=30, random_state=0)
   ...: model = RandomForestRegressor(n_jobs=8).fit(X, y)
   ...: 

In [2]: %timeit partial_dependence(model, X, features=[0], method="brute", max_memory_mb=1)
1min ± 702 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [3]: %timeit partial_dependence(model, X, features=[0], method="brute", max_memory_mb=1_000)
   ...: 
59.8 s ± 551 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [4]: %timeit partial_dependence(model, X, features=[0], method="brute", max_memory_mb=10_000)
1min 1s ± 669 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

@glemaitre

Copy link
Copy Markdown
Member Author

@adrinjalali Since you have a max_depth=None and a really large dataset, you might be in another regime where the trade-off is different and this is not only the size of the dataset per se that is important but the model as well (for random forest).

@adrinjalali

Copy link
Copy Markdown
Member

I'm not sure how I can trigger the benefit of this PR 😁 Here's more data:

In [2]:    ...: import numpy as np
   ...:    ...: from sklearn.datasets import make_regression
   ...:    ...: from sklearn.ensemble import RandomForestRegressor
   ...:    ...: from sklearn.inspection import partial_dependence
   ...:    ...:
   ...:    ...: X, y = make_regression(n_samples=100_000, n_features=30, random_state=0)
   ...:    ...: model = RandomForestRegressor(n_jobs=8, max_depth=7).fit(X, y)
   ...:    ...:

In [3]: %timeit partial_dependence(model, X, features=[0], method="brute", max_memory_mb=1)
6.91 s ± 35.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [4]: %timeit partial_dependence(model, X, features=[0], method="brute", max_memory_mb=1000)
6.42 s ± 37.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [5]: %timeit partial_dependence(model, X, features=[0], method="brute", max_memory_mb=10000)
6.51 s ± 75.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [6]:    ...: import numpy as np
   ...:    ...: from sklearn.datasets import make_regression
   ...:    ...: from sklearn.ensemble import RandomForestRegressor
   ...:    ...: from sklearn.inspection import partial_dependence
   ...:    ...:
   ...:    ...: X, y = make_regression(n_samples=100_000, n_features=30, random_state=0)
   ...:    ...: model = RandomForestRegressor(n_jobs=8, max_depth=4).fit(X, y)
   ...:    ...:

In [7]: %timeit partial_dependence(model, X, features=[0], method="brute", max_memory_mb=1)
4.85 s ± 63.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [8]: %timeit partial_dependence(model, X, features=[0], method="brute", max_memory_mb=1000)
4.73 s ± 62.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [9]: %timeit partial_dependence(model, X, features=[0], method="brute", max_memory_mb=10000)
4.84 s ± 173 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [10]: %timeit partial_dependence(model, X, features=[0], method="brute", max_memory_mb=20000)
4.7 s ± 23.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [11]: %timeit partial_dependence(model, X, features=[0], method="brute", max_memory_mb=30000)
4.72 s ± 15.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

@lorentzenchr

Copy link
Copy Markdown
Member

I have a question about API: Wouldn't it be something completely new for users to specify some amount of memory (RAM) limit? I would prefer to have a much simpler, more boolean option like batched : bool or batch_size : int.

@glemaitre

Copy link
Copy Markdown
Member Author

I have a question about API: Wouldn't it be something completely new for users to specify some amount of memory (RAM) limit? I would prefer to have a much simpler, more boolean option like batched : bool or batch_size : int.

We have something similar for the working_memory in the set_config and config_context.

However, the current experiment does not make show that we scale with the available amount of memory available. Therefore, if I cannot have something more conclusive, exposing this parameter as-is will not be an option. In case, that actually a 1 MB memory is always optimum then, we just don't need any parameter at all.

I'll come back on this PR just right after dealing with the one in the 1.6 milestone to be sure that we release the RC next week.

This branch has not been deployed

No deployments
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.

6 participants