Skip to content

Add generic native LoRA training support - #1924

Open
shahrryyar wants to merge 1 commit into
QwenAudio:mainfrom
shahrryyar:lora/native-lora-pr
Open

Add generic native LoRA training support#1924
shahrryyar wants to merge 1 commit into
QwenAudio:mainfrom
shahrryyar:lora/native-lora-pr

Conversation

@shahrryyar

Copy link
Copy Markdown

Summary

  • Add dependency-free native LoRA adapters for CosyVoice LLM adaptation.
  • Freeze base weights while keeping LoRA projections and adaptation heads trainable.
  • Save and load adapter-only checkpoints and optimize only trainable parameters.
  • Add generic usage documentation and unit coverage.

Validation

  • python3 -m unittest discover -s tests -v
  • python3 -m py_compile cosyvoice/utils/lora.py cosyvoice/bin/train.py cosyvoice/utils/train_utils.py tests/test_lora.py
  • git diff --check

This PR contains generic LoRA infrastructure only. No task-specific datasets, audio, model weights, manifests, language-specific frontend, or private evaluation material are included.

@shahrryyar
shahrryyar marked this pull request as ready for review August 3, 2026 08:54
Copilot AI review requested due to automatic review settings August 3, 2026 08:54

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 LoRALinear plus 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_ddp path).
  • 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 thread cosyvoice/utils/lora.py
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 thread docs/lora.md
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants