Skip to content

FIX add_weighted_adapter SVD combination types for Conv1d and Conv3d LoRA layers - #3903

Open
Nicholas022400701 wants to merge 3 commits into
huggingface:mainfrom
Nicholas022400701:fix/svd-weighted-adapter-convnd-3769
Open

Nicholas022400701 wants to merge 3 commits into
huggingface:mainfrom
Nicholas022400701:fix/svd-weighted-adapter-convnd-3769

Conversation

@Nicholas022400701

Copy link
Copy Markdown
Contributor

Resolves #3769

LoraModel._svd_generalized_task_arithmetic_weighted_adapter in src/peft/tuners/lora/model.py flattens the delta weight to 2D only when the target is a Conv2d. For Conv1d and Conv3d LoRA layers the 3D or 5D delta weight goes into torch.linalg.svd as a batch of matrices and the code after it fails (RuntimeError: size mismatch, got input (80), mat (80x10), vec (3) for Conv1d, diag(): Supports 1D or 2D tensors. Got 4D for Conv3d). This affects combination_type="svd" and the other SVD based types (ties_svd, dare_linear_svd, dare_ties_svd, magnitude_prune_svd); cat and linear already work for these layers.

The check is now isinstance(target, _ConvNd) and the delta weight is always flattened with delta_weight.flatten(start_dim=1), which turns (out, in, *kernel) into (out, in * prod(kernel)) for every conv type. For a 1x1 Conv2d this gives the same matrix as the previous squeeze(), so nothing changes for the layers that worked before. The reshape of U and Vh back to the lora_B and lora_A shapes at the end already works for any conv type.

Tests: test_add_weighted_adapter_svd_conv_layers in tests/test_custom_models.py, parametrized over Conv1d, Conv2d 3x3, Conv2d 1x1 and Conv3d with 10 input channels, two rank 4 adapters and svd_rank=8, so that the SVD is exact and the merged delta weight has to equal the weighted sum of the two adapters. On main the Conv1d and Conv3d cases fail with the two errors above and the Conv2d cases pass; with this change all four pass. pytest tests/test_custom_models.py -k weighted_adapter: 71 passed (the 67 existing add_weighted_adapter tests plus the 4 new ones). make style clean 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.

Copilot AI balanced review requested due to automatic review settings October 7, 2026 11:11

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

add_weighted_adapter with SVD based combination types fails for LoRA on Conv1d and Conv3d layers

2 participants