-
Notifications
You must be signed in to change notification settings - Fork 2.3k
feat: registry-based dispatch for resolve_lora_variant in Linear layer #3219
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
Draft
isha822
wants to merge
6
commits into
huggingface:main
Choose a base branch
from
isha822:feature/lora-variant-registry
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+125
−85
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ff9ad5a
feat: registry-based dispatch for resolve_lora_variant in Linear layer
isha822 4bab6db
Refactor LoRA variant resolution into base LoraLayer and add config s…
isha822 da3bd92
Update src/peft/tuners/lora/layer.py
isha822 e59ed42
refactor: address review comments - rename variables, extract lora_va…
isha822 c5d90b7
style: fix indentation in resolve_lora_variant
isha822 347a310
Merge branch 'main' into feature/lora-variant-registry
isha822 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
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
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
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 |
|---|---|---|
|
|
@@ -12,6 +12,8 @@ | |
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from unittest.mock import PropertyMock, patch | ||
|
|
||
| import pytest | ||
| import torch | ||
| from torch import nn | ||
|
|
@@ -174,6 +176,34 @@ def test_dora_params_have_gradients(self): | |
| for layer in layer_names: | ||
| assert getattr(peft_model.base_model.model, layer).lora_magnitude_vector["default"].weight.grad is not None | ||
|
|
||
| def test_unregistered_variant_raises_error(self): | ||
| # 1. Create a config and dummy linear layer | ||
| config = LoraConfig() | ||
| base_layer = nn.Linear(10, 10) | ||
| layer = LoraLinear(base_layer, "default", config, r=8, lora_alpha=8) | ||
|
|
||
| # 2. Monkey-patch the lora_variants property to include a fake variant | ||
| with patch("peft.tuners.lora.layer.Linear.lora_variants", new_callable=PropertyMock) as mock_variants: | ||
| mock_variants.return_value = {("fake_unregistered_variant",): None} | ||
|
|
||
| # 3. Assert that the sanity check catches it and throws the right error | ||
| with pytest.raises(ValueError, match="Variant 'fake_unregistered_variant' found in lora_variants but it is not tagged with 'is_lora_variant' in LoraConfig."): | ||
| layer.resolve_lora_variant(config=config) | ||
|
|
||
| def test_invalid_variant_combination_raises_error(self): | ||
| # 1. Create a config with no variants active | ||
| config = LoraConfig() | ||
| base_layer = nn.Linear(10, 10) | ||
| layer = LoraLinear(base_layer, "default", config, r=8, lora_alpha=8) | ||
|
|
||
| # 2. Monkey-patch lora_variants to include a valid tagged combo that isn't active | ||
| with patch("peft.tuners.lora.layer.Linear.lora_variants", new_callable=PropertyMock) as mock_variants: | ||
| mock_variants.return_value = { | ||
| ("use_dora",): None, # only use_dora is valid, empty combo not listed | ||
| } | ||
| # 3. Assert invalid combination error is raised | ||
| with pytest.raises(ValueError, match="Invalid or unsupported variant combination"): | ||
| layer.resolve_lora_variant(config=config) | ||
|
|
||
| class TestActivatedLora: | ||
| @pytest.mark.parametrize( | ||
|
|
@@ -225,9 +255,8 @@ def test_alora_activation_matches_base_until_invocation(self): | |
|
|
||
| input_ids = torch.tensor([[0, 1, 2, 3]]) | ||
| start = 2 | ||
| with lora_model.disable_adapter(): | ||
| with torch.no_grad(): | ||
| base_out = lora_model(X=input_ids) | ||
| with lora_model.disable_adapter(), torch.no_grad(): | ||
|
Member
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. Let's not touch unrelated code. |
||
| base_out = lora_model(X=input_ids) | ||
|
|
||
| kwargs = get_alora_offsets_for_forward(lora_model, input_ids) | ||
| with torch.no_grad(): | ||
|
|
@@ -272,9 +301,8 @@ def test_num_beams_error(self): | |
| lora_model.eval() | ||
|
|
||
| input_ids = torch.tensor([[0, 1, 2, 3]]) | ||
| with pytest.raises(ValueError) as e: | ||
| with torch.no_grad(): | ||
| lora_out = lora_model(X=input_ids, num_beams=2, alora_offsets=[3]) | ||
| with pytest.raises(ValueError) as e, torch.no_grad(): | ||
| lora_out = lora_model(X=input_ids, num_beams=2, alora_offsets=[3]) | ||
| assert "Beam search not yet supported for aLoRA." in str(e.value) | ||
|
|
||
| def test_gradient_checkpointing_double_forward_raises(self): | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please also add a test that checks for the
Invalid or unsupported variant combinationerror? It'll probably require monkey-patching too.