Fix memory blowup in GAM.gridsearch over large lambda grids#581
Open
RogerPR wants to merge 1 commit into
Open
Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Closses issue: #242
GAM.gridsearchpreviously kept every fitted candidate model in memoryfor the whole duration of the search, even when the caller did not need
them. For large
lamgrids this caused memory usage to grow linearlywith the grid size and could exhaust available RAM.
This PR keeps only the models that are actually needed:
best_model— to copy back intoselfwhenkeep_best=True.last_model— used as the warm-start coefficients for the next fit.modelslist is only populated whenreturn_scores=True,since that branch returns it to the caller.
In addition, fresh candidate models are now instantiated via
self.__class__(**self.get_params())instead ofdeepcopy(self).deepcopywas eagerly copying any large fitted state attached toself, contributing to peak memory.The public API is unchanged:
gridsearchreturnsselfby default andthe
OrderedDict[model -> score]whenreturn_scores=True, exactly asbefore.
Changes
pygam/pygam.py—GAM.gridsearch: only retainmodelswhenreturn_scores=True; tracklast_modelseparately for warm-start;build candidates from
ModelClass(**base_params)instead ofdeepcopy(self).pygam/tests/test_memory_leak_gridsearch.py— new regression testthat runs a 100-point
gridsearchand asserts peak RSS stays boundedand end-of-run RSS does not grow beyond a tolerance.
pyproject.toml— addspsutilto the[dev]extras (used by thenew test).
Test plan
pytest— 163 passed, 1 skipped (full suite green locally).pre-commit run --files <changed files>— all hooks pass(
ruff-format,ruff, trailing-whitespace, end-of-file-fixer).return_scores=Truecallers (covered bytest_gridsearch_returns_scores,test_gridsearch_keep_best,test_no_cartesian_product, etc.) still pass — theOrderedDict[model -> score]return contract is preserved.