Skip to content
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

Improve memory usage of MetaLearnerGridSearch #62

Merged
merged 17 commits into from
Jul 22, 2024

Conversation

FrancescMartiEscofetQC
Copy link
Contributor

@FrancescMartiEscofetQC FrancescMartiEscofetQC commented Jul 17, 2024

The idea of this PR is to improve the memory usage of MetaLearnerGridSearch. The main issue was that all metalearners instances were saved in memory. Each of this was referenced in two places, the jobs list and the raw_results_ list.

  • To remove it from the jobs list now _FitAndScoreJob stores only the factory and the parameters, then the MetaLearner object is created inside the joblib job which runs _fit_and_score.
  • For the reference in the raw_results_ now one can choose to return a generator from joblib.parallel instead of materializing all the results in a list.

It is important to notice that if the user wants to iterate themselves over the results and not materialize them (for example, to compute policy values and only store the one with the highest policy) then store_results and store_raw_results must be set to False as if store_results is True then the generator would be consumed when calling _format_results.

It also adds a grid_size_ attribute which can be useful if a generator is returned.

Checklist

  • Added a CHANGELOG.rst entry

Copy link

codecov bot commented Jul 17, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.88%. Comparing base (9406ef7) to head (1590e94).
Report is 24 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #62      +/-   ##
==========================================
+ Coverage   94.85%   94.88%   +0.03%     
==========================================
  Files          15       15              
  Lines        1534     1544      +10     
==========================================
+ Hits         1455     1465      +10     
  Misses         79       79              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@FrancescMartiEscofetQC FrancescMartiEscofetQC marked this pull request as ready for review July 17, 2024 14:06
Copy link
Collaborator

@kklein kklein left a comment

Choose a reason for hiding this comment

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

Thanks for working on this! :)

metalearners/grid_search.py Outdated Show resolved Hide resolved
metalearners/grid_search.py Outdated Show resolved Hide resolved
metalearners/grid_search.py Outdated Show resolved Hide resolved
and you do not want to store all MetaLearners objects rather evaluate them after
fitting each one and just store one.

``grid_size_`` will contain the number of hyperparameter combinations after fitting.
Copy link
Collaborator

Choose a reason for hiding this comment

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

I haven't quite grasped yet what the motivation for this attribute is. If the user expects a generator, won't they just be used to querying it until it no longer yields?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes! But I think it may be useful in the case the user wants to show a progress bar or similar, this way it's easier for them to access the number of metalearners to fit instead of having to calculate it manually.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think I understand what you mean. At the same time this use case is not super clear to me. Shall we simplify and remove it until we witness that there is a need for it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think I may not explain it correctly, in the case we set store_raw_results = False and store_results = False then the fit method finishes "instantly" (it does not wait for fitting the individual metalearners). Then, afaict these are only fitted when the user requests them by iterating over the generator where it may be of use to use this grid_size_ to display some progress bar or similar. If you still think it's not clear lmk and we can discuss it further.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As discussed, I added some explanation about this in the docstring:
1590e94

kklein
kklein previously approved these changes Jul 22, 2024
Copy link
Collaborator

@kklein kklein left a comment

Choose a reason for hiding this comment

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

LGTM :)

Copy link
Collaborator

@kklein kklein left a comment

Choose a reason for hiding this comment

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

LGTM

@FrancescMartiEscofetQC FrancescMartiEscofetQC merged commit 80ce219 into main Jul 22, 2024
16 checks passed
@FrancescMartiEscofetQC FrancescMartiEscofetQC deleted the generator_grid_search branch July 22, 2024 15:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants