8000 mini-verl/src/miniverl/cli.py at main · DaoyuanLi2816/mini-verl · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
2891 lines (2659 loc) · 115 KB

cli.py

File metadata and controls

2891 lines (2659 loc) · 115 KB
_emit_json(plan)
else:
console.print(f"[green]dry run ok[/green] {_esc(recipe)}")
for key, value in plan.items():
console.print(f" [dim]{key}[/dim] {_esc(value)}")
console.print("\nNo models were loaded, files written, or downloads attempted.")
return
try:
_require_training_stack("miniverl prepare-offline-kd")
from miniverl.trainer import OPDTrainer
from miniverl.training.checkpoint import load_checkpoint, validate_checkpoint
validated = validate_checkpoint(checkpoint)
with OPDTrainer.from_config(
config,
output_dir=out.parent,
run_id=out.name,
local_files_only=offline,
) as trainer:
load_checkpoint(
checkpoint,
backend=trainer.student,
optimizer=None,
device=trainer.student.device,
include_optimizer=False,
include_rng=False,
expected_identity=(trainer._checkpoint_identity() if validated.identity else None),
)
trainer.set_offline_collection_checkpoint_digest(validated.content_digest)
summary = trainer.prepare_offline_dataset()
destination = trainer.paths.root
except (MiniVerlError, ModuleNotFoundError) as exc:
_fail(exc)
return
payload = {**plan, "written": str(destination), **summary}
if as_json:
_emit_json(payload)
return
console.print(f"[green]offline dataset prepared[/green] {_esc(destination)}")
console.print(f" digest {_esc(summary['dataset_digest'])}")
console.print(f" trajectories {_esc(summary['trajectories'])}")
# --------------------------------------------------------- qualify-teacher
@app.command("qualify-teacher")
def qualify_teacher_command(
recipe: Path = typer.Option(..., "--recipe", help="Recipe defining the frozen teacher."),
candidate: str = typer.Option(..., "--candidate", help="Preregistered candidate id."),
out: Path = typer.Option(..., "--out", help="New immutable gate-result directory."),
tasks: Optional[int] = typer.Option(None, "--tasks", help="Eval-task count override."),
offline: bool = typer.Option(
False, "--offline", help="Refuse network access; use only cached model files."
),
as_json: bool = typer.Option(False, "--json", help="Emit machine-readable JSON."),
) -> None:
"""Evaluate one RecoveryBench teacher candidate on eval only."""
from miniverl.config import RunConfig
try:
config = RunConfig.from_yaml(recipe)
if config.require_environment("qualify-teacher").name != "sqlite_recovery":
raise ConfigError("qualify-teacher requires environment.name=sqlite_recovery")
_require_training_stack("miniverl qualify-teacher")
from miniverl.evaluation.teacher_gate import evaluate_teacher_candidate
result = evaluate_teacher_candidate(
config,
candidate_id=candidate,
out=out,
tasks=tasks,
split="eval",
local_files_only=offline,
)
except (ValidationError, MiniVerlError, ModuleNotFoundError) as exc:
if isinstance(exc, ValidationError):
err_console.print(f"[red]invalid recipe[/red] {_esc(recipe)}\n{_esc(exc)}")
raise typer.Exit(1) from None
_fail(exc)
return
if as_json:
_emit_json(result)
return
status = "passed" if result["gate"]["passed"] else "failed"
style = "green" if result["gate"]["passed"] else "yellow"
console.print(f"[{style}]teacher gate {status}[/{style}] {_esc(candidate)}")
console.print(f" result {_esc(out / 'result.json')}")
# ------------------------------------------------------------- evidence
@evidence_app.command("show")
def evidence_show(
study_id: str = typer.Argument(..., help="Packaged study identifier."),
as_json: bool = typer.Option(False, "--json", help="Emit machine-readable JSON."),
) -> None:
"""Show a packaged, typed study result without a repository checkout."""
from miniverl.evidence import show_builtin_study
try:
payload = show_builtin_study(study_id)
except (MiniVerlError, OSError, ValidationError) as exc:
_fail(exc)
return
if as_json:
_emit_json(payload)
return
console.print(f"[bold]{_esc(study_id)}[/bold]")
console.print_json(json.dumps(payload["result"], allow_nan=False))
@evidence_app.command("validate")
def evidence_validate(
study_id: str = typer.Argument(..., help="Packaged study identifier."),
as_json: bool = typer.Option(False, "--json", help="Emit machine-readable JSON."),
) -> None:
"""Validate packaged result, schema, preregistration and task evidence."""
from miniverl.evidence import validate_builtin_study
try:
payload = validate_builtin_study(study_id)
except (MiniVerlError, OSError, ValidationError) as exc:
_fail(exc)
return
if as_json:
_emit_json(payload)
elif payload["valid"]:
console.print(
f"[green]valid[/green] {_esc(study_id)} · {_esc(payload['task_rows'])} task rows"
)
else:
for problem in payload["problems"]:
err_console.print(f"[red]invalid[/red] {_esc(problem)}")
raise typer.Exit(1)
# ----------------------------------------------------------------- pilot
@app.command()
def pilot(
recipe: Optional[Path] = typer.Argument(
None, help="Alignment recipe containing bounded pilot evidence."
),
study_result: Optional[Path] = typer.Option(
None,
"--study-result",
help="Schema-validated external-study result; does not load a model.",
),
builtin_study: Optional[str] = typer.Option(
None,
"--builtin-study",
help="Packaged external-study result; works from an installed wheel.",
),
out: Optional[Path] = typer.Option(None, "--out", help="Optional JSON output path."),
as_json: bool = typer.Option(False, "--json", help="Emit machine-readable JSON."),
) -> None:
"""Recommend a method from explicit pilot evidence without loading a model."""
from miniverl.utils.runs import write_json_atomic
payload: dict[str, Any]
try:
selected = sum(value is not None for value in (recipe, study_result, builtin_study))
if selected > 1:
raise ConfigError(
"miniverl pilot accepts exactly one of a recipe, --study-result, or --builtin-study"
)
builtin = None
if builtin_study is not None:
from miniverl.evidence import get_builtin_study
builtin = get_builtin_study(builtin_study)
study_result = builtin.result_path
if study_result is not None:
from miniverl.alignment_external.result import load_alignment_external_result
result = load_alignment_external_result(study_result)
if result.study_status != "terminated_at_checkpoint_selection":
raise ConfigError(
"this pilot evidence path currently requires "
"study_status=terminated_at_checkpoint_selection"
)
payload = {
"study_status": result.study_status,
"recommendation": "do_not_continue",
"recommendation_scope": "do_not_continue_this_study",
"method_recommendation": "insufficient_evidence",
"reasons": [
"no candidate satisfied the retained-utility gate",
"no starting checkpoint was selected",
"teacher qualification was not run",
"no continuation method was authorized",
"the reserved final test was not accessed",
],
"evidence": {
"path": str(study_result),
"builtin_study": builtin.study_id if builtin is not None else None,
"sha256": hashlib.sha256(study_result.read_bytes()).hexdigest(),
"preregistration": result.preregistration.model_dump(mode="json"),
"task_evidence": result.checkpoint_selection.task_evidence.model_dump(
mode="json"
),
},
"universal_claim": False,
}
else:
if recipe is None:
raise ConfigError(
"miniverl pilot requires a recipe, --study-result or --builtin-study"
)
from miniverl.alignment import PilotEvidence, recommend_alignment_method
from miniverl.config import RunConfig
config = RunConfig.from_yaml(recipe)
if config.alignment is None:
raise ConfigError("miniverl pilot requires a recipe with an alignment section")
evidence = config.alignment.pilot or PilotEvidence()
recommendation = recommend_alignment_method(evidence)
payload = recommendation.model_dump(mode="json")
if out is not None:
write_json_atomic(out, payload)
except (ValidationError, MiniVerlError) as exc:
if isinstance(exc, MiniVerlError):
_fail(exc)
source = recipe if recipe is not None else study_result or builtin_study
err_console.print(f"[red]invalid pilot evidence[/red] {_esc(source)}\n{_esc(exc)}")
raise typer.Exit(1) from None
if as_json:
_emit_json(payload)
return
recommendation_text = payload.get("method_recommendation", payload["recommendation"])
console.print(f"[bold]recommendation[/bold] {_esc(recommendation_text)}")
for reason in payload["reasons"]:
console.print(f" - {_esc(reason)}")
if out is not None:
console.print(f" evidence {_esc(out)}")
@app.command()
def align(
recipe: Path = typer.Argument(..., help="Path to a post-SFT alignment recipe YAML file."),
output: Optional[Path] = typer.Option(
None, "--output", help="Parent directory for the run (default: run.output_dir)."
),
run_id: Optional[str] = typer.Option(None, "--run-id", help="Explicit run id."),
overwrite: bool = typer.Option(
False,
"--overwrite",
help="Explicitly replace the whole target run directory.",
),
dry_run: bool = typer.Option(
False,
"--dry-run",
help="Validate and print all alignment stages without loading models.",
),
offline: bool = typer.Option(
False, "--offline", help="Refuse network access; use only cached model files."
),
as_json: bool = typer.Option(False, "--json", help="Emit machine-readable JSON."),
) -> None:
"""Run base -> SFT checkpoint -> teacher -> alignment -> eval -> card."""
from miniverl.alignment import build_alignment_stage_plan
from miniverl.config import RunConfig
try:
config = RunConfig.from_yaml(recipe)
if config.alignment is None:
raise ConfigError("miniverl align requires a recipe with an alignment section")
except (ValidationError, MiniVerlError) as exc:
if isinstance(exc, MiniVerlError):
_fail(exc)
err_console.print(f"[red]invalid recipe[/red] {_esc(recipe)}\n{_esc(exc)}")
raise typer.Exit(1) from None
workflow = build_alignment_stage_plan(
config.alignment,
sft_warmup_cycles=config.train.sft_warmup_cycles,
)
if dry_run:
payload = {
"dry_run": True,
"recipe": str(recipe),
"method": config.alignment.method.value,
"workflow": workflow,
"backend": config.models.backend.value,
"downloads_required": config.models.backend.value == "hf",
"output_dir": str(output or config.run.output_dir),
}
if as_json:
_emit_json(payload)
else:
console.print(f"[green]alignment dry run ok[/green] {_esc(recipe)}")
for stage in workflow["stages"]:
console.print(f" - {_esc(stage['name'])}")
console.print("\nNo models were loaded and nothing was downloaded.")
return
try:
_require_training_stack("miniverl align")
from miniverl.alignment import run_alignment
payload = run_alignment(
config,
output_dir=output,
run_id=run_id,
local_files_only=offline,
overwrite=overwrite,
)
except (MiniVerlError, ModuleNotFoundError) as exc:
_fail(exc)
return
if as_json:
_emit_json(payload)
return
console.print(f"[bold green]alignment complete[/bold green] {_esc(payload['run_dir'])}")
console.print(f" card {_esc(Path(str(payload['run_dir'])) / 'alignment-card.md')}")
# ---------------------------------------------------------- verl-shaped plan/run
@app.command(
"plan",
context_settings={"allow_extra_args": True, "ignore_unknown_options": True},
)
def plan_command(
ctx: typer.Context,
config: str = typer.Option(..., "--config", help="Resolved YAML path or builtin profile."),
profile: str = typer.Option(
"verl-opd-v0.8-single-gpu-v1", "--profile", help="Pinned compatibility profile."
),
rollout_backend: Optional[str] = typer.Option(
None,
"--rollout-backend",
help="Local execution backend: hf_reference, hf_cached, or vllm.",
),
overrides: list[str] = typer.Option([], "--set", help="Repeatable dotted key=value override."),
override_files: list[Path] = typer.Option(
[],
"--overrides-file",
help="Repeatable plain key=value file or JSON argv array.",
),
accept_local_reinterpretations: bool = typer.Option(
False,
"--accept-local-reinterpretations",
0