-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Fix OFT Conv2d weight reshaping for non-square kernels #3150
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
Chessing234
wants to merge
2
commits into
huggingface:main
Choose a base branch
from
Chessing234:fix/oft-conv2d-nonsquare-kernel
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.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
|
|
@@ -5019,6 +5019,31 @@ def test_requires_grad_oft_same_targets(self): | |
| "base_model.model.lin0.oft_R.adapter1.weight", | ||
| ) | ||
|
|
||
| def test_oft_conv2d_non_square_kernel(self): | ||
| # OFT on a Conv2d with non-square kernel used to crash because the adapter | ||
| # filter dimension was computed as kernel_size[0] ** 2 instead of | ||
| # kernel_size[0] * kernel_size[1]; the forward pass then hit a shape | ||
| # mismatch and `merge_and_unload` failed to reshape the weights. | ||
| class NonSquareKernelConv2D(nn.Module): | ||
| def __init__(self): | ||
| super().__init__() | ||
| self.conv2d = nn.Conv2d(5, 10, kernel_size=(3, 5)) | ||
|
|
||
| def forward(self, X): | ||
| X = X.float().reshape(-1, 5, 3, 5) | ||
| return self.conv2d(X) | ||
|
|
||
| torch.manual_seed(0) | ||
| model = NonSquareKernelConv2D().to(self.torch_device) | ||
| config = OFTConfig(r=5, oft_block_size=0, target_modules=["conv2d"], init_weights=False) | ||
| peft_model = get_peft_model(model, config) | ||
|
|
||
| X = torch.arange(5 * 5 * 3 * 5, dtype=torch.float, device=self.torch_device).reshape(5, 5, 3, 5) | ||
| output = peft_model(X) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add a comment: "# Ensure that the forward pass does not raise". This is what we actually want to test. Equality of non-merged vs merged is nice to test too, but not the critical issue. |
||
| merged = peft_model.merge_and_unload() | ||
| output_merged = merged(X) | ||
| assert torch.allclose(output, output_merged, atol=1e-5, rtol=1e-5) | ||
|
|
||
| def test_requires_grad_hra_different_targets(self): | ||
| # test two different HRA adapters that target different modules | ||
| config0 = HRAConfig(target_modules=["lin0"]) | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test is placed in the wrong place. Please place it at the end of
TestPeftCustomModel.