Fix hook memory leak in MRFI by tracking and exposing RemovableHandle references#5
Open
Ziad-Ijja wants to merge 1 commit into
Open
Fix hook memory leak in MRFI by tracking and exposing RemovableHandle references#5Ziad-Ijja wants to merge 1 commit into
Ziad-Ijja wants to merge 1 commit into
Conversation
Welcome to Codecov 🎉Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests. Thanks for integrating Codecov - We've got you covered ☂️ |
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.
This pull request fixes a memory leak that occurs when
MRFIis instantiated multiple times over a fault injection campaign. The main theme is: making MRFI hooks explicitly managed so they can be properly released.Root cause:
__add_hooksregistered forward pre-hooks and forward hooks viaregister_forward_pre_hook/register_forward_hook, but the returnedRemovableHandleobjects were discarded. PyTorch keeps hooks alive as long as their handle exists, meaning everyMRFIinstantiation accumulated closures (including references toFI_configand internal state) that were never freed, even after theMRFIobject itself was deleted. Over a long campaign this causes progressive GPU/CPU memory exhaustion.Changes in
mrfi.py:_hook_handles(list) and_closed(bool) instance attributes, initialized in__init__before__add_hooksis called.__add_hooksto append everyRemovableHandlereturned byregister_forward_pre_hookandregister_forward_hookto_hook_handles, instead of discarding them.remove_hooks(): iterates_hook_handlesand calls.remove()on each handle, then clears the list.close(): callsremove_hooks(), then removes theFI_configattribute from every submodule viamodule.modules(), and sets_closed = Trueto prevent double-closing. This fully detaches MRFI instrumentation from the underlying model, allowing the same model instance to be safely re-wrapped in a subsequentMRFIinstantiation.Tests are passing with
pytest --cov=mrfi