Fix feature_info channels for pruned models#2723
Open
prawnsgupta wants to merge 1 commit into
Open
Conversation
Pruned models (efficientnet_b1/b2/b3_pruned, ecaresnet50d/101d_pruned) reported the original, unpruned channel counts from feature_info.channels(), not matching the actual feature map channels produced with features_only=True. adapt_model_from_string() rebuilds Conv/BN/Linear layers with pruned dimensions but never updated feature_info, which is populated at original build time. This adds _adapt_feature_info(), which runs a single dry-run forward with hooks on the feature modules to read their true output channel counts and writes them back. It is fail-safe: any error leaves feature_info untouched and never breaks model construction, and it handles both the plain-list and FeatureInfo representations. With this, pruned models pass the feature extraction tests, so the '*pruned*' exclusions (previously marked "hopefully fix at some point") are removed from tests/test_models.py. Fixes huggingface#2370
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
Pruned models report incorrect
feature_info.channels()— the original (unpruned) channel counts rather than the actual pruned feature-map channels. This fixes #2370.Affected models:
efficientnet_b1_pruned,efficientnet_b2_pruned,efficientnet_b3_pruned,ecaresnet50d_pruned,ecaresnet101d_pruned.Root cause
adapt_model_from_string()intimm/models/_prune.pyrebuildsConv2d/BatchNorm/Linearlayers with pruned dimensions, butfeature_infois populated at original build time and was never updated. Sofeature_info.channels()(and anything built on it, e.g.features_only=True) kept the dense channel counts.Before (
efficientnet_b1_pruned,features_only=True):Fix
Adds
_adapt_feature_info(), called at the end ofadapt_model_from_string(). It runs a single dry-run forward with hooks on the feature modules to read their true output channel counts and writes them back intofeature_info. Properties:_pruneutility rather than any single model file.feature_infountouched and never breaks model construction (logs a warning).FeatureInforepresentations, and derives input channels/size from the model so it respects custom stems andpretrained_cfginput size.After the fix,
feature_info.channels()matches the real feature shapes for all five models.Tests
Pruned models now pass the feature-extraction tests, so the
*pruned*exclusions intests/test_models.py(one marked# hopefully fix at some point) are removed. Verified locally:test_model_forward_features,test_model_forward_intermediates_features,test_model_forward_intermediates— 28 passed for pruned models (previously excluded).Note: the separate
in_chans-on-pruned limitation (#1597) is out of scope here and left untouched.