Repository navigation
FIX add_weighted_adapter SVD combination types for Conv1d and Conv3d LoRA layers - #3903
Open
Nicholas022400701 wants to merge 3 commits into
Open
Nicholas022400701 wants to merge 3 commits into
Nicholas022400701 wants to merge 3 commits into
Conversation
Sync main with upstream
Sync main with huggingface/peft
This branch has not been deployed
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.
Resolves #3769
LoraModel._svd_generalized_task_arithmetic_weighted_adapterinsrc/peft/tuners/lora/model.pyflattens the delta weight to 2D only when the target is aConv2d. ForConv1dandConv3dLoRA layers the 3D or 5D delta weight goes intotorch.linalg.svdas a batch of matrices and the code after it fails (RuntimeError: size mismatch, got input (80), mat (80x10), vec (3)forConv1d,diag(): Supports 1D or 2D tensors. Got 4DforConv3d). This affectscombination_type="svd"and the other SVD based types (ties_svd,dare_linear_svd,dare_ties_svd,magnitude_prune_svd);catandlinearalready work for these layers.The check is now
isinstance(target, _ConvNd)and the delta weight is always flattened withdelta_weight.flatten(start_dim=1), which turns(out, in, *kernel)into(out, in * prod(kernel))for every conv type. For a 1x1Conv2dthis gives the same matrix as the previoussqueeze(), so nothing changes for the layers that worked before. ThereshapeofUandVhback to thelora_Bandlora_Ashapes at the end already works for any conv type.Tests:
test_add_weighted_adapter_svd_conv_layersintests/test_custom_models.py, parametrized overConv1d,Conv2d3x3,Conv2d1x1 andConv3dwith 10 input channels, two rank 4 adapters andsvd_rank=8, so that the SVD is exact and the merged delta weight has to equal the weighted sum of the two adapters. OnmaintheConv1dandConv3dcases fail with the two errors above and theConv2dcases pass; with this change all four pass.pytest tests/test_custom_models.py -k weighted_adapter: 71 passed (the 67 existingadd_weighted_adaptertests plus the 4 new ones).make styleclean with ruff 0.16.4.AI disclosure: I used an AI coding agent to help write this patch, the tests and this description. I have read the change and the tests myself and I will answer review comments personally.