fix: eliminate cross-terms bug when combining adapters with different weights#3013
Draft
wingding12 wants to merge 1 commit intohuggingface:mainfrom
Draft
fix: eliminate cross-terms bug when combining adapters with different weights#3013wingding12 wants to merge 1 commit intohuggingface:mainfrom
wingding12 wants to merge 1 commit intohuggingface:mainfrom
Conversation
… weights When using `add_weighted_adapter` with combination_type in ['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. For example, with two adapters (A1, B1) and (A2, B2) and weights [1, -1]: - Expected: A1@B1 - A2@B2 - Old (buggy): (A1-A2)@(B1+B2) = A1@B1 + A1@B2 - A2@B1 - A2@B2 The cross-terms A1@B2 and A2@B1 were incorrectly added. This fix 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: 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 and is expected. Users who need higher accuracy can use combination_type='svd' with a higher svd_rank. Fixes huggingface#3004
Member
|
Thanks a lot for this PR to fix the incorrect merging behavior. It would be crucial to add tests for this. Do you still have your testing code available? Let's add it in the form of unit tests. If you need assistance with that, please let me know. |
Member
|
ping @wingding12 |
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.
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).