Add generic native LoRA training support - #1924
Open
shahrryyar wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
Adds a small, dependency-free LoRA adapter mechanism to enable parameter-efficient adaptation of CosyVoice’s LLM stage, integrating it into the existing training flow with adapter-only checkpointing behavior and basic documentation/tests.
Changes:
- Introduces
LoRALinearplus helper utilities to inject LoRA adapters, save adapter-only state, and reload adapter weights. - Updates training utilities to optimize only trainable parameters and to save adapter-only checkpoints when LoRA is enabled (in the
torch_ddppath). - Adds a minimal unit test and user documentation for enabling LoRA training/inference.
Reviewed changes
Copilot reviewed 4 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
cosyvoice/utils/lora.py |
Implements native LoRA modules plus adapter injection and adapter-only (trainable-only) state helpers. |
cosyvoice/bin/train.py |
Adds CLI flags and wiring to inject LoRA and resume from adapter-only checkpoints. |
cosyvoice/utils/train_utils.py |
Filters optimizer parameters by requires_grad and saves adapter-only checkpoints for torch_ddp when LoRA is enabled. |
docs/lora.md |
Documents how to train and load LoRA adapters using the existing training entrypoint. |
tests/test_lora.py |
Adds unit tests for injection behavior, freezing rules, and adapter state round-trip. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+102
to
+109
| def load_lora_state_dict(model: nn.Module, state: dict[str, torch.Tensor]) -> None: | ||
| missing, unexpected = model.load_state_dict(state, strict=False) | ||
| unexpected = [name for name in unexpected if name not in {"step", "epoch"}] | ||
| if unexpected: | ||
| raise RuntimeError(f"Unexpected LoRA checkpoint keys: {unexpected[:8]}") | ||
| missing_trainable = [name for name in missing if name in state] | ||
| if missing_trainable: | ||
| raise RuntimeError(f"Could not load LoRA checkpoint keys: {missing_trainable[:8]}") |
Comment on lines
+28
to
+30
| To continue from an adapter checkpoint, use `--lora_checkpoint`. The adapter | ||
| checkpoint contains only trainable adapter/head weights plus `epoch` and | ||
| `step`, so the original base checkpoint is still required. |
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
Validation
This PR contains generic LoRA infrastructure only. No task-specific datasets, audio, model weights, manifests, language-specific frontend, or private evaluation material are included.