Repository navigation
Fix _FanovaTree instances never being garbage collected - #6864
Closed
DeepakJaggupalli wants to merge 1 commit into
Closed
DeepakJaggupalli wants to merge 1 commit into
DeepakJaggupalli wants to merge 1 commit into
Conversation
_FanovaTree's _is_node_leaf/_get_node_* helper methods are decorated with @lru_cache(maxsize=None) directly on the instance method. Since lru_cache's cache lives on the function object (shared by every instance of the class) and keys on all positional arguments including self, every _FanovaTree that ever called one of these methods stays referenced by that cache for the lifetime of the process - it can never be garbage collected, even after nothing else references it. _FanovaTree instances are created fresh on every call to _Fanova.fit() (one per tree in the random forest, default 64), which FanovaImportanceEvaluator.evaluate() calls on every invocation. Any code path that computes fANOVA parameter importances more than once in the same process - a dashboard, a notebook, a report generated in a loop - leaks the full previous forest every time. Verified directly: building a few _FanovaTree instances, exercising the cached methods (as __init__ already does), and confirming via weakref that they survive gc.collect() on the current code, but are correctly collected after this fix. Also confirmed the fix produces bit-identical variance/importance values to before, by running the same seeded RandomForestRegressor through both implementations. Replaced the class-level lru_cache with per-instance dict caches so each _FanovaTree's cache dies with the instance instead of outliving it.
Member
|
Thank you. However, this can be resolved with a simpler implementation, so I am closing this PR. |
1 task done
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.
Motivation
I ran a static analysis pass looking for objects that never get garbage collected and found that
_FanovaTree(optuna/importance/_fanova/_tree.py) leaks every instance it ever creates, for the lifetime of the process._is_node_leaf,_get_node_left_child,_get_node_right_child,_get_node_children,_get_node_value,_get_node_split_threshold, and_get_node_split_featureare all decorated with@lru_cache(maxsize=None)directly on the instance method.functools.lru_cache's cache lives on the function object itself, which is shared by every instance of the class, and it keys on all positional arguments - includingself. So every_FanovaTreethat ever calls one of these methods (which__init__does immediately, via_precompute_statistics()etc.) stays referenced by that cache forever, and can never be collected even after nothing else references it._FanovaTreeinstances are created fresh on every call to_Fanova.fit()- one per tree in the random forest, 64 by default - andFanovaImportanceEvaluator.evaluate()callsfit()on every invocation. So any code path that computes fANOVA parameter importances more than once in the same process (a dashboard, a notebook, a report regenerated in a loop) leaks the entire previous forest every time it runs.I verified this directly: built a few
_FanovaTreeinstances, exercised the cached methods, dropped all references, and confirmed viaweakrefthat they survivegc.collect()on the current code - and that they're correctly collected after this fix.Description of the changes
Replaced the class-level
@lru_cache(maxsize=None)decorators on those 7 methods with per-instancedictcaches, initialized in__init__. Each_FanovaTree's cache now lives and dies with the instance instead of outliving it. Semantics are otherwise unchanged - same memoization behavior, just scoped correctly.I also confirmed the fix doesn't change any computed values: ran the same seeded
RandomForestRegressorthrough both the original and fixed_FanovaTreeand comparedvariance/get_marginal_varianceoutput - bit-identical.Added a regression test (
test_tree_is_garbage_collected) that builds a_FanovaTree, exercises the cached methods, drops all references, and asserts viaweakrefthat the instance is actually collected. It fails on the current code and passes with this fix.Checklist