fix: make CallSequenceTracker unwrap idempotent for bound methods#701
Merged
FAdrien merged 1 commit intoJun 25, 2026
Merged
Conversation
begumcig
approved these changes
Jun 25, 2026
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.
Description
Fixes a
TypeErrorinCallSequenceTrackerthat breakstotal_macsandtotal_paramsmetrics when the same module’sforwardis unwrapped more than once.Context
ModelArchitectureStatswraps allnn.Moduleforwards before a single inference run, records the call sequence, then callsunwrap()on each module while computing MACs/params. If a module appears multiple times in the call sequence,unwrap()runs on the same module more than once.Cause
unwrap()previously restored the module'sforwardby checking if the it has the__wrapped__attribute. The first unwrap correctly restores the bound method. As this bound method can still expose the__wrapped__attribute, a second unwrap then replaces it with the unbound function. Next calls to the model (e.g., to generate an image) then fail with aTypeErrorbecauseselfis no longer bound.Fix
This PR stores the original forward on a dedicated
__call_tracker_original_forward__attribute, skips re-wrapping modules that are already tracked, and makesunwrap()idempotent on multiple calls.Related Issue
Fixes #(issue number)
Type of Change
Testing
uv run pytest -m "cpu and not slow")For full setup and testing instructions, see the Contributing Guide.
Checklist
Thanks for contributing to Pruna! We're excited to review your work.
New to contributing? Check out our Contributing Guide for everything you need to get started.