fix: eliminate cross-terms bug when combining adapters with different weights #3013
+71
−40
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
Fixes #3004
When using
add_weighted_adapterwithcombination_typein['linear', 'ties', 'dare_linear', 'dare_ties', 'magnitude_prune'], the previous implementation combined A matrices and B matrices separately before multiplying. This introduced cross-terms that corrupted the result when combining multiple adapters with different weights.The Bug
For example, with two adapters (A1, B1) and (A2, B2) and weights [1, -1]:
A1@B1 - A2@B2(A1-A2)@(B1+B2) = A1@B1 + A1@B2 - A2@B1 - A2@B2The cross-terms
A1@B2andA2@B1were incorrectly added, causing significant errors (in my tests, ~1.55 error norm vs ~1.55 expected norm - essentially completely wrong).The Fix
This PR computes the full delta weight (
A@B) for each adapter first, then combines them, and finally decomposes the result back to A and B using SVD. This ensures mathematically correct results.Note on Approximation
The decomposition uses truncated SVD with the same rank as the input adapters, which introduces some approximation error. This is the same behavior as the existing SVD combination types (
svd,ties_svd, etc.) and is expected.In my tests:
combination_type='svd')Users who need higher accuracy can use
combination_type='svd'with a highersvd_rankparameter.Test Plan
I tested manually with:
Also includes Conv2d layer handling (following the same pattern as the existing SVD implementation).