-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[WIP] Add LoRA multihead attention module #1324
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
Merged
BenjaminBossan
merged 61 commits into
huggingface:main
from
BenjaminBossan:feat-add-lora-multihead-attention
Jan 8, 2025
Merged
Changes from 1 commit
Commits
Show all changes
61 commits
Select commit
Hold shift + click to select a range
49fab86
[WIP] Add LoRA multihead attention module
BenjaminBossan d8e9589
Make style
BenjaminBossan 0e188a3
Remove commented code
BenjaminBossan b409d81
Remove assignment of weight to new module
BenjaminBossan 173062c
Make state_dict and named_parameters work
BenjaminBossan 1e007f5
Extend test coverage a bit
BenjaminBossan 557c4a1
Clean ups after reviewer feedback:
BenjaminBossan add1f51
Reviewer feedback: removed another unnecessary arg
BenjaminBossan e44e030
Make style
BenjaminBossan 8d62579
Merge branch 'main' into feat-add-lora-multihead-attention
BenjaminBossan c5d8a6b
Apply LoRA also to the out_proj of MHA
BenjaminBossan 9dc4a4d
Merge branch 'main' into feat-add-lora-multihead-attention
BenjaminBossan c3fb2ce
Fix bug with incorrectly set gradient
BenjaminBossan 17d407b
Fix failing tests
BenjaminBossan 4cbf6e9
Merge branch 'main' into feat-add-lora-multihead-attention
BenjaminBossan e0cae11
Move to pytest style asserts
BenjaminBossan 52c8d9b
Fix safe merging code
BenjaminBossan 977c84b
Merge branch 'main' into feat-add-lora-multihead-attention
BenjaminBossan 96d376d
No need to set bias for MHA anymore, see #1530
BenjaminBossan 0c17476
Merge branch 'main' into feat-add-lora-multihead-attention
BenjaminBossan 4b8db0c
Fix style
BenjaminBossan 7e91712
Merge branch 'main' into feat-add-lora-multihead-attention
BenjaminBossan e12070b
Merge branch 'main' into feat-add-lora-multihead-attention
BenjaminBossan 7b6c7cb
Remove duplicate merge
BenjaminBossan e6ab8ed
Raise error for multi adapter batch inference
BenjaminBossan 8ec6c3c
Raise error for DoRA + MHA
BenjaminBossan f6ba465
Fix error when adding multiple adapters to MHA
BenjaminBossan fb18886
Better way of param initialization
BenjaminBossan 4ff2ec3
Add tests for broken loading and workaround
BenjaminBossan d1f6ab2
make style
BenjaminBossan 65363be
Merge branch 'main' into feat-add-lora-multihead-attention
BenjaminBossan 7ba2e68
Fix wrong merge conflict resolution in test
BenjaminBossan 6ef04b0
Ensure that base weights have requires_grad False
BenjaminBossan 07c7240
Merge branch 'main' into feat-add-lora-multihead-attention
BenjaminBossan cc3ac3d
Remove xpass-ing test
BenjaminBossan 03c466f
Merge branch 'main' into feat-add-lora-multihead-attention
BenjaminBossan e558caa
MAINT: Give stale bot permissions for PRs too (#2064)
BenjaminBossan 38f4a98
ENH BOFT don't save boft_P buffer (#2050)
sywangyi 7e5c61d
FIX Command line args in PiSSA preprocess (#2053)
keakon 183bf52
MNT Update deprecated evaluation_strategy (#1664)
muellerzr b970607
ENH Multi adapters in same batch: modules_to_save (#1990)
saeid93 732e8e7
FIX Bug that prevents BOFT from loading 2 adapters (#2068)
BenjaminBossan 79e2b38
TST Skip some quantization tests on XPU (#2074)
faaany 61e6934
Improve test coverage for initialization of MHA
BenjaminBossan ced2f15
Merge branch 'main' into feat-add-lora-multihead-attention
BenjaminBossan 4c31bbc
Fix bug with unloading multihead attention layer
BenjaminBossan 1dbb9a5
Fix bug in unloading
BenjaminBossan e094234
Fix for low_cpu_mem_usage
BenjaminBossan e90af48
Merge branch 'main' into feat-add-lora-multihead-attention
BenjaminBossan 30a08e7
Merge branch 'main' into feat-add-lora-multihead-attention
BenjaminBossan 09f5ea6
Add tests for init_empty_weights
BenjaminBossan 6a83bd7
Merge branch 'main' into feat-add-lora-multihead-attention
BenjaminBossan 3b0471a
Merge branch 'main' into feat-add-lora-multihead-attention
BenjaminBossan 465a85e
Add MHA to modules unsupported by EVA
BenjaminBossan 266f9da
Add comment on why/how empty init works
BenjaminBossan 39e755e
Expose attributes of underlying MHA module
BenjaminBossan 4857858
Apply suggestions from code review
BenjaminBossan 74cbba6
Remove trailing whitespace
BenjaminBossan 14deb9f
Linting..
BenjaminBossan ba2a8dd
Reviewer comment: Add comments for clarification
BenjaminBossan ac10b18
Reviewer feedback: Remove q_proj_weight
BenjaminBossan 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
Make state_dict and named_parameters work
There was a bug because the removal of the parameter resulted in it no longer appearing in the state_dict and named_parameters. This commit fixes this bug. The bug also exists in the referenced lora-torch library.
- Loading branch information
commit 173062cd048a979d857c02195424413c414e8a07
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -832,6 +832,27 @@ def forward(self, x: torch.Tensor, *args: Any, **kwargs: Any) -> torch.Tensor: | |
| result = (result[0].to(previous_dtype), result[1].to(previous_dtype) if result[1] is not None else result[1]) | ||
| return result | ||
|
|
||
| def _restore_weights(self): | ||
| # Restore the weights as registered parameters on the base layer. | ||
| # This is necessary because the way that weights are merged/unmerged (which is necessary for forward to work | ||
| # correctly), the Module "forgets" these attributes. Therefore, we need to call register_parameter explicitly. | ||
| # We cannot call register_parameter for merging/unmerging because that cuts them off from the autograd graph. | ||
| # Note that this is hacky, since we need to ensure that _restore_weights is called by each method that needs it. | ||
|
|
||
| # TODO work with separate weights | ||
| base_layer = self.get_base_layer() | ||
| weight = base_layer.in_proj_weight.data | ||
| del base_layer.in_proj_weight | ||
| base_layer.register_parameter("in_proj_weight", nn.Parameter(weight)) | ||
|
|
||
| def state_dict(self, *args, **kwargs): | ||
| self._restore_weights() | ||
| return super().state_dict(*args, **kwargs) | ||
|
|
||
| def named_modules(self, *args, **kwargs): | ||
|
Contributor
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. do we need also to over-write the
Member
Author
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. Not needed, as |
||
| self._restore_weights() | ||
| return super().named_modules(*args, **kwargs) | ||
|
|
||
| def __repr__(self) -> str: | ||
| rep = super().__repr__() | ||
| return "lora." + rep | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.