Repository navigation
lora: honor lora_target_modules and fix the sidecar save crash #9
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
Open
jenny-luo-0727
wants to merge
2
commits into
AI-ModCon:main
Choose a base branch
from
jenny-luo-0727:fix/lora-target-modules
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.
Open
Changes from all commits
Commits
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
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
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 |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| """apply_lora_torchtune honors target_modules, and the adapter sidecar records it. | ||
|
|
||
| Previously ``training.lora_target_modules`` was written to the adapter sidecar | ||
| but never reached ``apply_lora_torchtune``, so every nn.Linear except lm_head was | ||
| adapted regardless; and a Hydra-supplied list crashed ``save_lora_adapter``'s | ||
| json.dump. The layer-selection and name-check tests need no torchtune and run in | ||
| CI; the LoRALinear and sidecar tests skip where torchtune is not installed (CI | ||
| does not install it). | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
|
|
||
| import pytest | ||
| import torch.nn as nn | ||
| from src.utils.lora_utils import _select_lora_targets, apply_lora_torchtune, save_lora_adapter | ||
|
|
||
| pytestmark = pytest.mark.unit | ||
|
|
||
|
|
||
| class _Block(nn.Module): | ||
| def __init__(self): | ||
| super().__init__() | ||
| self.q_proj = nn.Linear(8, 8) | ||
| self.v_proj = nn.Linear(8, 8) | ||
| self.o_proj = nn.Linear(8, 8) | ||
|
|
||
|
|
||
| class _Tiny(nn.Module): | ||
| def __init__(self): | ||
| super().__init__() | ||
| self.layers = nn.ModuleList([_Block(), _Block()]) | ||
| self.lm_head = nn.Linear(8, 16) | ||
|
|
||
|
|
||
| _ALL = {f"layers.{i}.{leaf}" for i in range(2) for leaf in ("q_proj", "v_proj", "o_proj")} | ||
| _QV = {f"layers.{i}.{leaf}" for i in range(2) for leaf in ("q_proj", "v_proj")} | ||
|
|
||
|
|
||
| def _selected(target_modules) -> set[str]: | ||
| return {name for name, _ in _select_lora_targets(_Tiny(), ("lm_head",), target_modules)} | ||
|
|
||
|
|
||
| def test_default_selects_every_linear_except_lm_head(): | ||
| assert _selected(None) == _ALL | ||
|
|
||
|
|
||
| def test_target_modules_restricts_selection(): | ||
| assert _selected(["q_proj", "v_proj"]) == _QV | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("target_modules", "error"), | ||
| [ | ||
| (["q_prj"], ValueError), # typo matches nothing | ||
| (["lm_head"], ValueError), # excluded | ||
| ([], ValueError), | ||
| ("q_proj", TypeError), # bare string, not a list | ||
| ], | ||
| ) | ||
| def test_bad_target_modules_raise(target_modules, error): | ||
| with pytest.raises(error): | ||
| _selected(target_modules) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("target_modules", "expected"), [(None, _ALL), (["q_proj", "v_proj"], _QV)] | ||
| ) | ||
| def test_apply_adapts_selected_layers_and_trains_only_lora(target_modules, expected): | ||
| LoRALinear = pytest.importorskip("torchtune.modules.peft").LoRALinear | ||
| model = apply_lora_torchtune(_Tiny(), rank=2, alpha=4.0, target_modules=target_modules) | ||
| assert { | ||
| name for name, module in model.named_modules() if isinstance(module, LoRALinear) | ||
| } == expected | ||
| trainable = {name for name, p in model.named_parameters() if p.requires_grad} | ||
| assert trainable == {f"{name}.lora_{ab}.weight" for name in expected for ab in ("a", "b")} | ||
|
|
||
|
|
||
| def test_sidecar_records_hydra_list(tmp_path): | ||
| pytest.importorskip("torchtune") | ||
| omegaconf = pytest.importorskip("omegaconf") | ||
| targets = omegaconf.OmegaConf.create({"t": ["q_proj"]}).t # as cfg.training.get() returns it | ||
| model = apply_lora_torchtune(_Tiny(), rank=2, alpha=4.0, target_modules=targets) | ||
| path = tmp_path / "lora_adapter.pt" | ||
| save_lora_adapter(model, str(path), rank=2, alpha=4.0, target_modules=targets) | ||
| meta = json.loads((tmp_path / "lora_adapter.pt.meta.json").read_text()) | ||
| assert meta == {"rank": 2, "alpha": 4.0, "target_modules": ["q_proj"]} |
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
Oops, something went wrong.
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.
This fixes the crash here, but
TrainingConfig.lora_target_modulesis still aListConfigeverywhere else at runtime. For example, both trainers writeself.config.__dict__totraining_state.jsonwithjson.dump(..., default=str), so that file records the string"['q_proj', 'v_proj']"rather than a list. Converting once insrc/train.py:463, the way_coerce_str_or_nonealready handles other fields (e.g. viaOmegaConf.to_container), would give every consumer a plain list. Keeping thislist()as a fallback for direct callers is fine.