-
Notifications
You must be signed in to change notification settings - Fork 321
[1/7][multi-lora]: utils foundation — sample/adapter types, adapter yaml config, shared helpers, CLI flags and validation #1742
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
Merged
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| """Adapter config parsing for multi-LoRA training. | ||
|
|
||
| ``AdapterRunConfig`` carries only static, YAML-sourced configuration; the | ||
| mutable slot is owned by the controller and exposed through ``AdapterRun`` | ||
| views. | ||
| """ | ||
|
|
||
| from dataclasses import dataclass, field | ||
| from pathlib import Path | ||
| from typing import Any | ||
|
|
||
| import yaml | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class AdapterRunConfig: | ||
|
|
||
| data: str | ||
|
|
||
| # resolves them to CLI defaults if None (--lora-rank / --lora-alpha) on register. | ||
| rank: int | None = None | ||
| alpha: int | None = None | ||
|
|
||
| # Prompt groups consumed per optimizer step for this adapter (group units, | ||
| # like --rollout-batch-size, which it defaults to). The samples-per-step | ||
| # analog of --global-batch-size is derived: adapter_global_batch_size = | ||
| # rollout_batch_size * n_samples_per_prompt. | ||
| rollout_batch_size: int | None = None | ||
| n_samples_per_prompt: int | None = None | ||
|
|
||
| save: str | Path | None = None | ||
|
|
||
| input_key: str = "text" | ||
| label_key: str | None = None | ||
| metadata_key: str | None = None | ||
|
|
||
| rm_type: str | None = None | ||
| custom_rm_path: str | None = None | ||
|
|
||
| # Stop after N optimizer steps; derived from num_epoch (default 1) when absent. | ||
| num_step: int | None = None | ||
| num_epoch: int | None = None | ||
|
|
||
| metadata: dict[str, Any] = field(default_factory=dict) | ||
|
|
||
| @property | ||
| def adapter_global_batch_size(self) -> int: | ||
| """Samples per optimizer step (per-adapter analog of --global-batch-size).""" | ||
| assert self.rollout_batch_size is not None and self.n_samples_per_prompt is not None | ||
| return self.rollout_batch_size * self.n_samples_per_prompt | ||
|
|
||
|
yushengsu-thu marked this conversation as resolved.
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class AdapterRun: | ||
| """Read-only join view of a run's static config and current slot.""" | ||
|
|
||
| name: str | ||
| config: AdapterRunConfig | ||
| slot: int | ||
| version: int = 0 | ||
| step: int = 0 | ||
| # Committed prompt groups accumulated toward the current optimizer step. | ||
| accumulated_groups: int = 0 | ||
| # Unique per registration (see AdapterRecord.registration_id): lets the | ||
| # rollout worker tell a re-registered name apart from the previous tenant. | ||
| registration_id: str = "" | ||
|
|
||
|
|
||
| def parse_adapter_run_yaml(path: Path) -> AdapterRunConfig: | ||
| """Parse a single adapter.yaml file. | ||
|
|
||
| ``rank``, ``alpha`` and ``save`` are optional in the YAML; when absent the | ||
| caller (e.g. the multi-LoRA controller) is responsible for resolving them. | ||
| """ | ||
| with open(path) as f: | ||
| raw = yaml.safe_load(f) | ||
|
yushengsu-thu marked this conversation as resolved.
|
||
|
|
||
| return AdapterRunConfig( | ||
| rank=raw.get("rank"), | ||
| alpha=raw.get("alpha"), | ||
| data=raw["data"], | ||
| rollout_batch_size=raw.get("rollout_batch_size"), | ||
| n_samples_per_prompt=raw.get("n_samples_per_prompt"), | ||
| save=Path(raw["save"]) if raw.get("save", None) else None, | ||
| input_key=raw.get("input_key", "text"), | ||
| label_key=raw.get("label_key"), | ||
| metadata_key=raw.get("metadata_key"), | ||
| rm_type=raw.get("rm_type"), | ||
| custom_rm_path=raw.get("custom_rm_path"), | ||
| num_step=raw.get("num_step"), | ||
| num_epoch=raw.get("num_epoch"), | ||
| metadata=raw.get("metadata") or {}, | ||
| ) | ||
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.
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.
Uh oh!
There was an error while loading. Please reload this page.