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

Skip to content

Latest commit

 

History

History
1573 lines (1409 loc) · 59.8 KB

cli.py

File metadata and controls

1573 lines (1409 loc) · 59.8 KB
"""miniVERL command line interface.
A thin layer: every command parses arguments, calls one library function, and
renders the result. No training logic lives here.
Only the heavy commands import torch, and they do it inside the command body,
so ``miniverl --help``, ``doctor``, ``validate``, ``inspect``, ``report`` and
``cache`` all work from a bare ``pip install miniverl``.
"""
from __future__ import annotations
import json
import sys
from pathlib import Path
from typing import Any, Literal, Optional
import typer
from pydantic import ValidationError
from rich.console import Console
from rich.markup import escape
from rich.table import Table
from miniverl import __version__
from miniverl.errors import ConfigError, MiniVerlError
from miniverl.utils.runs import make_run_id
app = typer.Typer(
name="miniverl",
help=(
"On-policy distillation for tool-using agents on one GPU.\n\n"
"A readable single-CUDA-GPU post-training lab for exact and budgeted "
"on-policy distillation."
),
add_completion=False,
no_args_is_help=True,
rich_markup_mode=None,
)
cache_app = typer.Typer(help="Inspect and validate a teacher-target cache.", no_args_is_help=True)
app.add_typer(cache_app, name="cache")
bridge_app = typer.Typer(
help="Inspect a pinned, exported verl scale-out bundle.", no_args_is_help=True
)
app.add_typer(bridge_app, name="bridge")
console = Console()
err_console = Console(stderr=True)
_STATUS_STYLE = {"ok": "green", "warn": "yellow", "missing": "yellow", "fail": "red"}
def _emit_json(payload: Any) -> None:
try:
serialized = json.dumps(payload, default=str, allow_nan=False)
except (OverflowError, RecursionError, TypeError, ValueError) as exc:
from miniverl.errors import SerializationError
_fail(SerializationError(f"command result is not finite JSON: {exc}"))
return
console.print_json(serialized)
def _esc(value: object) -> str:
"""Escape dynamic text before it reaches Rich.
Rich treats square brackets as markup, so unescaped values silently lose
content: the hint ``pip install "miniverl[train]"`` would print as
``pip install "miniverl"`` -- the wrong command. Every dynamic string this
module prints goes through here.
"""
return escape(str(value))
def _require_training_stack(purpose: str) -> None:
"""Fail with an install command if the training extra is not present.
Called before any heavy import so a bare ``pip install miniverl`` produces
the exact command to run rather than a bare ``ModuleNotFoundError``.
"""
from miniverl.errors import MissingDependencyError
from miniverl.utils.lazy import have_module
for module in ("torch", "transformers", "peft"):
if not have_module(module):
raise MissingDependencyError(module, "train", purpose)
def _fail(exc: Exception, *, code: int = 1) -> None:
"""Print an actionable error and exit non-zero."""
if isinstance(exc, ModuleNotFoundError):
from miniverl.errors import MissingDependencyError
exc = MissingDependencyError(exc.name or "a dependency", "train", "This command")
if isinstance(exc, MiniVerlError):
err_console.print(f"[red]error[/red] {_esc(exc.message)}")
if exc.hint:
err_console.print(f"[yellow]hint[/yellow] {_esc(exc.hint)}")
else:
err_console.print(f"[red]error[/red] {_esc(exc)}")
raise typer.Exit(code)
def _version_callback(value: bool) -> None:
if value:
console.print(f"miniverl {__version__}")
raise typer.Exit(0)
@app.callback()
def main(
version: bool = typer.Option(
False,
"--version",
"-V",
help="Print the miniVERL version and exit.",
callback=_version_callback,
is_eager=True,
),
log_level: str = typer.Option(
"INFO",
"--log-level",
help="Logging verbosity (DEBUG, INFO, WARNING, ERROR).",
envvar="MINIVERL_LOG_LEVEL",
),
) -> None:
"""miniVERL: on-policy distillation for tool-using agents on one GPU."""
from miniverl.utils.logging import configure_logging
configure_logging(log_level)
# ---------------------------------------------------------------- doctor
@app.command()
def doctor(
as_json: bool = typer.Option(False, "--json", help="Emit machine-readable JSON."),
output_dir: Path = typer.Option(
Path("runs"), "--output", help="Directory to test for writability."
),
) -> None:
"""Report what this machine can run, and what to install for the rest."""
from miniverl.doctor import run_doctor
try:
report = run_doctor(output_dir)
except Exception as exc:
_fail(exc)
return
if as_json:
_emit_json(report.to_dict())
return
table = Table(title=f"miniVERL {report.miniverl_version} environment", show_lines=False)
table.add_column("check", style="bold")
table.add_column("status")
table.add_column("detail")
for check in report.checks:
table.add_row(
_esc(check.name),
f"[{_STATUS_STYLE.get(check.status, 'white')}]{check.status}[/]",
_esc(check.detail),
)
console.print(table)
verdict = report.to_dict()["verdict"]
console.print()
for label, key, command in (
("core commands (doctor/validate/inspect/report/cache)", "core_commands", None),
(
"CPU + toy training (demo, recipes/toy_cpu.yaml)",
"cpu_training",
'pip install "miniverl[train]"',
),
("GPU training", "gpu_training", "install a CUDA build of torch"),
("4-bit QLoRA", "qlora_4bit", 'pip install "miniverl[train,cuda]"'),
):
ready = verdict[key]
# Pad to the width of the longer word so the labels line up in a column.
mark = "[green]yes[/green]" if ready else "[yellow]no [/yellow]"
suffix = "" if ready or not command else f" -> {command}"
console.print(f" {mark} {_esc(label)}{_esc(suffix)}")
hints = [c for c in report.checks if c.hint and c.status in {"fail", "missing", "warn"}]
if hints:
console.print()
console.print("[bold]suggestions[/bold]")
for check in hints:
console.print(f" - {_esc(check.name)}: {_esc(check.hint)}")
# -------------------------------------------------------------- validate
@app.command()
def validate(
recipe: Path = typer.Argument(..., help="Path to a recipe YAML file."),
as_json: bool = typer.Option(False, "--json", help="Emit machine-readable JSON."),
) -> None:
"""Validate a recipe without downloading models or allocating memory."""
from miniverl.config import RunConfig
from miniverl.environments.registry import make_environment
try:
config = RunConfig.from_yaml(recipe)
except ValidationError as exc:
if as_json:
_emit_json({"valid": False, "path": str(recipe), "errors": exc.errors()})
raise typer.Exit(1) from None
err_console.print(f"[red]invalid recipe[/red] {_esc(recipe)}")
for error in exc.errors():
location = ".".join(str(p) for p in error["loc"])
err_console.print(f" [red]{_esc(location or '<root>')}[/red]: {_esc(error['msg'])}")
err_console.print(
"\n[yellow]hint[/yellow] compare against recipes/toy_cpu.yaml, which is "
"validated in CI"
)
raise typer.Exit(1) from None
except MiniVerlError as exc:
if as_json:
_emit_json({"valid": False, "path": str(recipe), "errors": [exc.message]})
raise typer.Exit(1) from None
_fail(exc)
return
warnings: list[str] = []
try:
environment = make_environment(config.environment.name, **config.environment.params)
if config.models.teacher.mode.value == "privileged_context" and not hasattr(
environment, "privileged_context"
):
warnings.append("environment provides no privileged context")
except MiniVerlError as exc:
if as_json:
_emit_json({"valid": False, "path": str(recipe), "errors": [exc.message]})
raise typer.Exit(1) from None
_fail(exc)
return
steps_per_cycle = max(
1,
8BD0 (config.train.rollouts_per_cycle + config.train.gradient_accumulation_steps - 1)
// config.train.gradient_accumulation_steps,
)
if config.run.mode.value == "opd" and config.train.opd_freshness.value == "replay":
warnings.append(
f"opd_freshness=replay permits {steps_per_cycle} optimizer step(s) per "
"rollout batch; this is online distillation with replay, not genuine OPD"
)
if config.models.backend.value == "hf" and not config.models.student.revision:
warnings.append("models.student.revision is unpinned; the manifest will record 'unpinned'")
if config.models.backend.value == "hf" and not config.models.teacher.revision:
warnings.append("models.teacher.revision is unpinned")
payload = {
"valid": True,
"path": str(recipe),
"run_name": config.run.name,
"mode": config.run.mode.value,
"is_on_policy": config.is_on_policy,
"opd_freshness": (
config.train.opd_freshness.value if config.run.mode.value == "opd" else None
),
"backend": config.models.backend.value,
"student": config.models.student.model_id,
"teacher": config.models.teacher.model_id,
"environment": config.environment.name,
"difficulty": config.environment.difficulty,
"objective": (
"sft_cross_entropy"
if config.run.mode.value == "sft"
else (
"online_distillation_with_replay"
if config.run.mode.value == "opd" and not config.is_on_policy
else config.run.mode.value
)
),
"loss_mode": config.loss.mode.value if config.run.mode.value != "sft" else None,
"divergence": config.loss.divergence.value if config.run.mode.value != "sft" else None,
"top_k": config.loss.top_k if config.run.mode.value != "sft" else None,
"selector": config.selection.selector.value,
"memory_strategy": config.memory.strategy.value,
"cycles": config.train.cycles,
"sft_warmup_cycles": config.train.sft_warmup_cycles,
"optimizer_steps_per_cycle": steps_per_cycle,
"planned_optimizer_steps": steps_per_cycle
* (config.train.cycles + config.train.sft_warmup_cycles),
"eval_tasks": config.effective_eval_tasks,
"seed": config.run.seed,
"warnings": warnings,
}
if as_json:
_emit_json(payload)
return
console.print(f"[green]valid[/green] {_esc(recipe)}")
table = Table(show_header=False, box=None, pad_edge=False)
for key, value in payload.items():
if key in {"valid", "path", "warnings"}:
continue
table.add_row(f"[dim]{key}[/dim]", _esc(value))
console.print(table)
for warning in warnings:
console.print(f"[yellow]warning[/yellow] {_esc(warning)}")
# ------------------------------------------------------------------ demo
@app.command()
def demo(
output: Path = typer.Option(Path("runs/demo"), "--output", help="Run directory to create."),
fast: bool = typer.Option(False, "--fast", help="Shrink every budget (CI smoke test)."),
overwrite: bool = typer.Option(
False,
"--overwrite",
help="Explicitly replace the whole existing demo run directory.",
),
as_json: bool = typer.Option(False, "--json", help="Emit machine-readable JSON."),
report_html: bool = typer.Option(True, "--report/--no-report", help="Also render report.html."),
) -> None:
"""Run the embedded no-network toy pipeline end to end."""
try:
_require_training_stack("miniverl demo")
from miniverl.demo import demo_config
from miniverl.trainer import OPDTrainer
except (MiniVerlError, ModuleNotFoundError) as exc:
_fail(exc)
return
target = Path(output)
try:
config = demo_config(fast=fast, output_dir=target.parent)
if overwrite and target.exists() and not as_json:
console.print(
f"[yellow]overwrite[/yellow] replacing whole run directory {_esc(target)}"
)
with OPDTrainer.from_config(
config,
output_dir=target.parent,
run_id=target.name,
overwrite=overwrite,
) as trainer:
result = trainer.train()
paths = trainer.paths
report_path: Path | None = None
if report_html:
from miniverl.reporting import ReportData, write_markdown, write_report
report_path = write_report(paths.root, paths.report_html)
write_markdown(ReportData.from_run(paths.root), paths.summary_md)
artifacts = _artifact_listing(paths.root)
except MiniVerlError as exc:
_fail(exc)
return
payload = {
**result.to_dict(),
"artifacts": artifacts,
"report": str(report_path) if report_path else None,
}
if as_json:
_emit_json(payload)
return
console.print()
console.print(f"[bold green]demo complete[/bold green] {_esc(paths.root)}")
table = Table(show_header=False, box=None)
baseline = (result.baseline_eval or {}).get("success_rate")
final = (result.eval or {}).get("success_rate")
table.add_row("mode", f"{result.mode} (genuine on-policy distillation)")
table.add_row("optimizer steps", str(result.global_step))
table.add_row("parameter version", str(result.parameter_version))
table.add_row("rollout iterations", str(result.cycles_completed))
table.add_row("wall clock", f"{result.duration_seconds:.1f} s")
provenance = _provenance_summary(paths.trajectories)
if provenance:
table.add_row("token provenance", provenance)
compression = _cache_summary(paths.teacher_cache)
if compression:
table.add_row("teacher cache", compression)
table.add_row(
"task success",
f"{_fmt_pct(baseline)} -> {_fmt_pct(final)} (greedy, held-out eval split)",
)
console.print(table)
console.print()
for line in (
"This demo proves the [bold]machinery[/bold], not capability.",
"At this size the toy student learns the tool-call format and not the",
"arithmetic, so 0% here is the expected outcome, not a failure.",
"For a CPU run that does learn (measured 0.0% -> 91.7% in 192 s):",
" [bold]miniverl train recipes/toy_cpu.yaml[/bold]",
):
console.print(line)
console.print()
console.print("[bold]artifacts[/bold]")
for name, size in artifacts.items():
console.print(f" {_esc(name)} [dim]{_esc(size)}[/dim]")
console.print()
console.print("[bold]next[/bold]")
console.print(f" miniverl inspect {_esc(paths.trajectories)}")
console.print(f" miniverl cache stats {_esc(paths.teacher_cache)}")
console.print(f" miniverl report {_esc(paths.root)} --out {_esc(paths.report_html)}")
def _provenance_summary(path: Path) -> str:
"""One line summarizing which tokens could enter the loss."""
try:
from miniverl.inspection import summarize_file
summary = summarize_file(path, limit=0)
except (MiniVerlError, OSError):
# A cosmetic summary line must never turn a completed run into a failure.
return ""
if not summary.tokens:
return ""
return (
f"{summary.model_tokens} of {summary.tokens} tokens trainable "
f"({summary.model_token_fraction * 100:.0f}%); "
f"{summary.context_tokens} are context and can never be a target"
)
def _cache_summary(path: Path) -> str:
"""One line summarizing the teacher-target cache."""
try:
from miniverl.cache.stats import compute_stats
stats = compute_stats(path, verify_checksums=False)
except (MiniVerlError, OSError):
return ""
if not stats.get("selected_positions"):
return ""
return (
f"{stats['selected_positions']} scored positions, "
f"{stats['actual_bytes'] / 1024:.1f} KiB on disk, "
f"{stats['compression_ratio']:.1f}x smaller than a dense fp16 dump"
)
def _fmt_pct(value: Any) -> str:
try:
return f"{float(value) * 100:.1f}%"
except (TypeError, ValueError):
return "n/a"
def _artifact_listing(root: Path) -> dict[str, str]:
out: dict[str, str] = {}
for path in sorted(root.rglob("*")):
if path.is_file():
rel = path.relative_to(root).as_posix()
out[rel] = f"{path.stat().st_size} B"
return out
# ----------------------------------------------------- prepare-offline-kd
@app.command("prepare-offline-kd")
def prepare_offline_kd_command(
recipe: Path = typer.Option(..., "--recipe", help="Frozen-student offline-KD recipe."),
checkpoint: Path = typer.Option(
..., "--checkpoint", help="Exact shared cold-start checkpoint directory."
),
out: Path = typer.Option(..., "--out", help="New immutable dataset bundle directory."),
offline: bool = typer.Option(
False, "--offline", help="Refuse network access; use only cached model files."
),
dry_run: bool = typer.Option(
False,
"--dry-run",
help="Validate and print the collection plan without loading models or writing files.",
),
as_json: bool = typer.Option(False, "--json", help="Emit machine-readable JSON."),
) -> None:
"""Collect one frozen-student trajectory/teacher-target dataset."""
from miniverl.config import OfflineKDTrajectorySource, RunConfig, TrainingMode
try:
config = RunConfig.from_yaml(recipe)
if config.run.mode is not TrainingMode.OFFLINE_KD:
raise ConfigError("prepare-offline-kd requires run.mode=offline_kd")
if config.offline_kd.trajectory_source is not OfflineKDTrajectorySource.FROZEN_STUDENT:
raise ConfigError(
"prepare-offline-kd requires offline_kd.trajectory_source=frozen_student"
)
if not checkpoint.is_dir():
raise ConfigError(f"checkpoint directory not found: {checkpoint}")
if out.exists():
raise ConfigError(
f"output already exists: {out}",
hint="choose a new directory; frozen datasets are never overwritten",
)
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
plan = {
"dry_run": dry_run,
"recipe": str(recipe),
"checkpoint": str(checkpoint),
"out": str(out),
"offline": offline,
"trajectory_source": config.offline_kd.trajectory_source.value,
"collection_seed": config.offline_kd.collection_seed,
"collection_tasks": (config.offline_kd.collection_tasks or config.train.rollouts_per_cycle),
"student": config.models.student.model_id,
"student_revision": config.models.student.revision,
"teacher": config.models.teacher.model_id,
"teacher_revision": config.models.teacher.revision,
}
if dry_run:
if as_json:
_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.environment.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')}")
# ----------------------------------------------------------------- train
@app.command()
def pilot(
recipe: Path = typer.Argument(..., help="Alignment recipe containing bounded pilot evidence."),
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.alignment import PilotEvidence, recommend_alignment_method
from miniverl.config import RunConfig
from miniverl.utils.runs import write_json_atomic
try:
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()
result = recommend_alignment_method(evidence)
payload = result.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)
err_console.print(f"[red]invalid recipe[/red] {_esc(recipe)}\n{_esc(exc)}")
raise typer.Exit(1) from None
if as_json:
_emit_json(payload)
return
console.print(f"[bold]recommendation[/bold] {_esc(payload['recommendation'])}")
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')}")
# ----------------------------------------------------------------- train
@app.command()
def train(
recipe: Path = typer.Argument(..., help="Path to a 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."),
resume: Optional[Path] = typer.Option(
None,
"--resume",
help="Continue an existing run from its highest valid checkpoint.",
),
resume_from: Optional[Path] = typer.Option(
None,
"--resume-from",
help="Continue an existing run from this exact checkpoint directory.",
),
overwrite: bool = typer.Option(
False,
"--overwrite",
help="Explicitly replace the whole target run directory.",
),
dry_run: bool = typer.Option(
False,
"--dry-run",
help="Validate, resolve and print the plan without loading models or downloading anything.",
),
offline: bool = typer.Option(
False, "--offline", help="Refuse network access; use only already-cached model files."
),
as_json: bool = typer.Option(False, "--json", help="Emit machine-readable JSON."),
make_report: bool = typer.Option(
True, "--report/--no-report", help="Render report.html when the run finishes."
),
) -> None:
"""Train a student with SFT, offline KD or on-policy distillation."""
from miniverl.config import RunConfig
try:
config = RunConfig.from_yaml(recipe)
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
if resume is not None and resume_from is not None:
_fail(ConfigError("--resume and --resume-from are mutually exclusive"))
return
if overwrite and (resume is not None or resume_from is not None):
_fail(ConfigError("--overwrite cannot be combined with --resume or --resume-from"))
return
if (resume is not None or resume_from is not None) and (
run_id is not None or output is not None
):
_fail(
ConfigError(
"--resume/--resume-from cannot be combined with --run-id or --output",
hint="the existing run directory already determines its id and output root",
)
)
return
if dry_run:
steps_per_cycle = max(
1,
(config.train.rollouts_per_cycle + config.train.gradient_accumulation_steps - 1)
// config.train.gradient_accumulation_steps,
)
plan = {
"dry_run": True,
"recipe": str(recipe),
"run_name": config.run.name,
"mode": config.run.mode.value,
"backend": config.models.backend.value,
"student": config.models.student.model_id,
"student_revision": config.models.student.revision,
"teacher": config.models.teacher.model_id,
"teacher_revision": config.models.teacher.revision,
"downloads_required": config.models.backend.value == "hf",
"planned_optimizer_steps": steps_per_cycle
* (config.train.cycles + config.train.sft_warmup_cycles),
"planned_rollouts": config.train.rollouts_per_cycle * config.train.cycles,
"output_dir": str(output or config.run.output_dir),
"resume": str(resume) if resume is not None else None,
"resume_from": str(resume_from) if resume_from is not None else None,
"overwrite": overwrite,
"resolved_config": json.loads(json.dumps(config.model_dump(mode="json"), default=str)),
}
if as_json:
_emit_json(plan)
else:
console.print(f"[green]dry run ok[/green] {_esc(recipe)}")
for key in (
"mode",
"backend",
"student",
"teacher",
"downloads_required",
"planned_optimizer_steps",
"planned_rollouts",
"output_dir",
):
console.print(f" [dim]{key}[/dim] {_esc(plan[key])}")
console.print("\nNo models were loaded and nothing was downloaded.")
return
try:
_require_training_stack("miniverl train")
from miniverl.trainer import OPDTrainer
explicit_id = run_id or config.run.run_id
if overwrite and explicit_id and not as_json:
target = Path(output or config.run.output_dir) / make_run_id(
config.run.name,
explicit=explicit_id,
)
if target.exists():
console.print(
f"[yellow]overwrite[/yellow] replacing whole run directory {_esc(target)}"
)
with OPDTrainer.from_config(
config,
output_dir=output,
run_id=run_id,
local_files_only=offline,
overwrite=overwrite,
resume=resume,
resume_from=resume_from,
) as trainer:
result = trainer.train()
paths = trainer.paths
report_path: Path | None = None
if make_report and config.report.enabled:
from miniverl.reporting import ReportData, write_markdown, write_report
report_path = write_report(
paths.root,
paths.report_html,
max_trajectories=config.report.max_trajectories,
max_tokens=config.report.max_tokens_per_trajectory,
)
write_markdown(ReportData.from_run(paths.root), paths.summary_md)
except (MiniVerlError, ModuleNotFoundError) as exc:
_fail(exc)
return
payload = {**result.to_dict(), "report": str(report_path) if report_path else None}
if as_json:
_emit_json(payload)
return
console.print()
console.print(f"[bold green]run complete[/bold green] {_esc(paths.root)}")
baseline = (result.baseline_eval or {}).get("success_rate")
final = (result.eval or {}).get("success_rate")
console.print(
f" task success {_fmt_pct(baseline)} -> {_fmt_pct(final)} "
f"in {result.global_step} optimizer steps ({result.duration_seconds:.1f} s)"
)
console.print(f" eval miniverl eval --run {_esc(paths.root)}")
console.print(f" report {_esc(report_path or paths.report_html)}")
# ------------------------------------------------------------------- eval
@app.command("eval")
def eval_command(
run: Path = typer.Option(..., "--run", help="Run directory produced by `miniverl train`."),
split: Optional[str] = typer.Option(
None, "--split", help="Split to evaluate: train, eval or test."
),
tasks: Optional[int] = typer.Option(None, "--tasks", help="Limit the number of tasks."),
checkpoint: Optional[Path] = typer.Option(
None, "--checkpoint", help="Specific checkpoint directory (default: the latest)."
),
out: Optional[Path] = typer.Option(None, "--out", help="Where to write the eval JSON."),
tag: str = typer.Option("standalone", "--tag", help="Label recorded with the results."),
offline: bool = typer.Option(
False,
"--offline",
help="Refuse network access; use only local or already-cached model and adapter files.",
),
as_json: bool = typer.Option(False, "--json", help="Emit machine-readable JSON."),
) -> None:
"""Re-evaluate a finished run deterministically."""
try:
_require_training_stack("miniverl eval")
from miniverl.evaluation.evaluator import evaluate_run
payload = evaluate_run(
run,
split=split,
tasks=tasks,
checkpoint=checkpoint,
out=out,
tag=tag,
local_files_only=offline,
)
except (MiniVerlError, ModuleNotFoundError) as exc:
_fail(exc)
return
if as_json:
_emit_json(payload)
return
console.print(f"[bold]eval[/bold] {_esc(payload['run_dir'])} split={_esc(payload['split'])}")
table = Table(show_header=False, box=None)
for key in (
"tasks",
"success_rate",
"strict_task_success_rate",
"lenient_diagnostic_success_rate",
"avg_turns",
"avg_tool_calls",
"emitted_tool_calls",
"parsed_tool_calls",
"parse_valid_tool_call_rate",
"tool_execution_success_rate",
"tool_execution_error_rate",
"final_answer_format_validity_rate",
"protocol_token_accuracy",
"generated_tokens_per_task",
"rollout_tokens_per_second",
"seconds",
):
table.add_row(f"[dim]{key}[/dim]", _esc(payload.get(key)))
console.print(table)
console.print(f" failures {_esc(payload.get('failure_categories'))}")
console.print(f" written {_esc(payload.get('written_to'))}")
# ----------------------------------------------------------- verl bridge
@app.command("import-verl")
def import_verl_command(
source: Path = typer.Argument(..., help="Pinned verl YAML configuration."),
profile: str = typer.Option(..., "--profile", help="Documented bridge profile."),
target_verl: str = typer.Option(..., "--target-verl", help="Pinned verl tag or commit."),
out: Path = typer.Option(..., "--out", help="New miniVERL recipe path."),
environment: Optional[str] = typer.Option(
None, "--environment", help="Explicit registered miniVERL environment."
),
teacher_model: Optional[str] = typer.Option(
None, "--teacher-model", help="Explicit frozen teacher model identity."
),
teacher_adapter: Optional[str] = typer.Option(
None, "--teacher-adapter", help="Optional local teacher adapter path."
),
loss_profile: Optional[str] = typer.Option(
None, "--loss-profile", help="Explicit miniVERL distillation objective profile."
),
schedule_mapping: Optional[str] = typer.Option(
None, "--schedule-mapping", help="Explicit schedule-unit mapping."
),
as_json: bool = typer.Option(False, "--json", help="Emit machine-readable JSON."),
) -> None:
"""Import the documented whitelist, never generic verl YAML."""
try:
from miniverl.bridge.config import import_verl_config
report = import_verl_config(
source,
profile=profile,
target_verl=target_verl,
out=out,
environment=environment,
teacher_model=teacher_model,
teacher_adapter=teacher_adapter,
loss_profile=loss_profile,
schedule_mapping=schedule_mapping,
)
0