-
Notifications
You must be signed in to change notification settings - Fork 92
Fix ignore_layers not working for FP8 models #1286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
8
commits into
main
Choose a base branch
from
copilot/fix-issue-with-auto-rounding
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+148
−2
Open
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d2758f8
Initial plan
Copilot f8b62fc
Fix ignore_layers for FP8 models by checking INNER_SUPPORTED_LAYER_TYPES
Copilot 9f0eaf8
Add test for ignore_layers with FP8 models
Copilot 97abed0
Add comprehensive unit tests for get_fp_layer_names function
Copilot 4f77d6d
Address code review feedback: improve test mocking and remove debug p…
Copilot 17fbf3a
Address review feedback: remove unused import and add None test
Copilot 3bee2e5
fix pre-commit
yiliu30 4775df9
Merge branch 'main' into copilot/fix-issue-with-auto-rounding
yiliu30 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| """ | ||
| Unit tests for auto_round.compressors.utils module. | ||
| """ | ||
| import torch | ||
| import pytest | ||
|
|
||
| from auto_round.compressors.utils import get_fp_layer_names | ||
| from auto_round.utils import INNER_SUPPORTED_LAYER_TYPES | ||
|
|
||
|
|
||
| class TestGetFpLayerNames: | ||
| """Test suite for get_fp_layer_names function.""" | ||
|
|
||
| def test_regular_linear_layers(self): | ||
| """Test with regular Linear layers.""" | ||
| class MockModel(torch.nn.Module): | ||
| def __init__(self): | ||
| super().__init__() | ||
| self.layer1 = torch.nn.Linear(10, 10) | ||
| self.layer2 = torch.nn.Linear(10, 10) | ||
| self.mlp = torch.nn.Sequential( | ||
| torch.nn.Linear(10, 10), | ||
| torch.nn.Linear(10, 10) | ||
| ) | ||
|
|
||
| model = MockModel() | ||
|
|
||
| # Test finding specific layer | ||
| result = get_fp_layer_names(model, 'layer1') | ||
| assert 'layer1' in result, "Should find layer1" | ||
|
|
||
| # Test finding layers with pattern | ||
| result = get_fp_layer_names(model, 'mlp') | ||
| assert len(result) == 2, "Should find 2 layers in mlp" | ||
| assert 'mlp.0' in result and 'mlp.1' in result | ||
|
|
||
| def test_fp8linear_layers(self): | ||
| """Test with FP8Linear layers (mocked by creating a proper class).""" | ||
| # Create a proper mock FP8Linear class | ||
| class FP8Linear(torch.nn.Linear): | ||
| """Mock FP8Linear class for testing.""" | ||
| def __init__(self, in_features, out_features): | ||
| super().__init__(in_features, out_features) | ||
|
|
||
| class MockModel(torch.nn.Module): | ||
| def __init__(self): | ||
| super().__init__() | ||
| self.layer1 = torch.nn.Linear(10, 10) | ||
| # Use proper FP8Linear mock | ||
| self.layer2 = FP8Linear(10, 10) | ||
|
|
||
| self.mlp = torch.nn.Sequential() | ||
| linear1 = torch.nn.Linear(10, 10) | ||
| self.mlp.add_module('0', linear1) | ||
| linear2 = FP8Linear(10, 10) | ||
| self.mlp.add_module('1', linear2) | ||
|
|
||
| model = MockModel() | ||
|
|
||
| # Test finding FP8Linear layer | ||
| result = get_fp_layer_names(model, 'layer2') | ||
| assert 'layer2' in result, "Should find FP8Linear layer (layer2)" | ||
|
|
||
| # Test finding mixed Linear and FP8Linear in mlp | ||
| result = get_fp_layer_names(model, 'mlp') | ||
| assert len(result) == 2, "Should find 2 layers in mlp (both Linear and FP8Linear)" | ||
| assert 'mlp.0' in result, "Should find regular Linear in mlp" | ||
| assert 'mlp.1' in result, "Should find FP8Linear in mlp" | ||
|
|
||
| def test_empty_ignore_layers(self): | ||
| """Test with empty ignore_layers string.""" | ||
| class MockModel(torch.nn.Module): | ||
| def __init__(self): | ||
| super().__init__() | ||
| self.layer1 = torch.nn.Linear(10, 10) | ||
|
|
||
| model = MockModel() | ||
| result = get_fp_layer_names(model, '') | ||
| assert len(result) == 0, "Empty ignore_layers should return empty list" | ||
yiliu30 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| def test_multiple_ignore_patterns(self): | ||
| """Test with multiple ignore patterns.""" | ||
| class MockModel(torch.nn.Module): | ||
| def __init__(self): | ||
| super().__init__() | ||
| self.layer1 = torch.nn.Linear(10, 10) | ||
| self.layer2 = torch.nn.Linear(10, 10) | ||
| self.layer3 = torch.nn.Linear(10, 10) | ||
|
|
||
| model = MockModel() | ||
| result = get_fp_layer_names(model, 'layer1,layer3') | ||
| assert 'layer1' in result, "Should find layer1" | ||
| assert 'layer3' in result, "Should find layer3" | ||
| assert 'layer2' not in result, "Should not find layer2" | ||
|
|
||
| def test_pattern_with_digits(self): | ||
| """Test pattern matching with digits (special case in the code).""" | ||
| class MockModel(torch.nn.Module): | ||
| def __init__(self): | ||
| super().__init__() | ||
| self.layers = torch.nn.ModuleList([ | ||
| torch.nn.Linear(10, 10), | ||
| torch.nn.Linear(10, 10), | ||
| torch.nn.Linear(10, 10) | ||
| ]) | ||
|
|
||
| model = MockModel() | ||
| # Pattern ending with digit should get a dot appended for matching | ||
| result = get_fp_layer_names(model, 'layers.0') | ||
| # Should match 'layers.0' | ||
| assert 'layers.0' in result, "Should match layers.0" | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.