|
| 1 | +"""``vla-eval data`` subcommand handlers. |
| 2 | +
|
| 3 | +Provides a uniform fetch flow for benchmarks whose dataset is licensed |
| 4 | +independently of the harness (e.g. BEHAVIOR-1K's BEHAVIOR Dataset |
| 5 | +ToS). See :class:`vla_eval.benchmarks.base.DataRequirement` and |
| 6 | +:meth:`vla_eval.benchmarks.base.Benchmark.data_requirements`. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import argparse |
| 12 | +import os |
| 13 | +import shutil |
| 14 | +import subprocess |
| 15 | +import sys |
| 16 | +from pathlib import Path |
| 17 | + |
| 18 | +from vla_eval.benchmarks.base import Benchmark, DataRequirement |
| 19 | +from vla_eval.cli.config_loader import load_config as _load_config |
| 20 | +from vla_eval.config import DockerConfig |
| 21 | +from vla_eval.registry import resolve_import_string |
| 22 | + |
| 23 | + |
| 24 | +def _stderr_console(): # pragma: no cover — same shim cmd_run uses |
| 25 | + from rich.console import Console |
| 26 | + |
| 27 | + return Console(stderr=True, soft_wrap=True) |
| 28 | + |
| 29 | + |
| 30 | +def _resolve_benchmark_class(config: dict) -> tuple[type[Benchmark], str]: |
| 31 | + """Return ``(class, cache_subdir)`` for the first benchmark in config. |
| 32 | +
|
| 33 | + ``cache_subdir`` is the module-path's last package segment, e.g. |
| 34 | + ``vla_eval.benchmarks.behavior1k.benchmark:X`` → ``behavior1k``. |
| 35 | + """ |
| 36 | + benchmarks = config.get("benchmarks") or [] |
| 37 | + if not benchmarks: |
| 38 | + raise ValueError("config has no 'benchmarks' entries") |
| 39 | + import_string = benchmarks[0].get("benchmark") |
| 40 | + if not import_string: |
| 41 | + raise ValueError("first benchmark entry is missing 'benchmark' import string") |
| 42 | + cls = resolve_import_string(import_string) |
| 43 | + if not (isinstance(cls, type) and issubclass(cls, Benchmark)): |
| 44 | + raise TypeError(f"resolved {import_string} to {cls!r}, which is not a Benchmark subclass") |
| 45 | + module_path = import_string.split(":", 1)[0] |
| 46 | + parts = module_path.split(".") |
| 47 | + # Expect …benchmarks.<key>.benchmark — take the second-to-last part. |
| 48 | + cache_subdir = parts[-2] if len(parts) >= 2 else parts[-1] |
| 49 | + return cls, cache_subdir |
| 50 | + |
| 51 | + |
| 52 | +def _default_host_data_dir(cache_subdir: str) -> Path: |
| 53 | + """Return ``${VLA_EVAL_DATA_DIR}/<cache_subdir>`` or the XDG-style default.""" |
| 54 | + base = os.environ.get("VLA_EVAL_DATA_DIR") |
| 55 | + if base: |
| 56 | + return Path(base).expanduser() / cache_subdir |
| 57 | + return Path.home() / ".cache" / "vla-eval" / cache_subdir |
| 58 | + |
| 59 | + |
| 60 | +def _build_docker_argv( |
| 61 | + image: str, |
| 62 | + docker_cfg: DockerConfig, |
| 63 | + host_dir: Path, |
| 64 | + requirement: DataRequirement, |
| 65 | + extra_gpus: str | None, |
| 66 | +) -> list[str]: |
| 67 | + """Build the ``docker run`` argv that downloads the dataset.""" |
| 68 | + argv: list[str] = ["docker", "run", "--rm"] |
| 69 | + gpus = extra_gpus or docker_cfg.gpus or "all" |
| 70 | + argv.extend(["--gpus", gpus]) |
| 71 | + for env_pair in docker_cfg.env: |
| 72 | + argv.extend(["-e", env_pair]) |
| 73 | + argv.extend(["-v", f"{host_dir}:{requirement.container_data_path}"]) |
| 74 | + argv.append(image) |
| 75 | + argv.extend(requirement.download_command) |
| 76 | + return argv |
| 77 | + |
| 78 | + |
| 79 | +def cmd_data_fetch(args: argparse.Namespace) -> None: |
| 80 | + """Fetch the external dataset for a benchmark, mounted at the |
| 81 | + canonical host cache directory.""" |
| 82 | + con = _stderr_console() |
| 83 | + config = _load_config(args.config) |
| 84 | + |
| 85 | + try: |
| 86 | + bench_cls, cache_subdir = _resolve_benchmark_class(config) |
| 87 | + except (TypeError, ValueError) as exc: |
| 88 | + con.print(f"[red]ERROR: {exc}[/red]") |
| 89 | + sys.exit(1) |
| 90 | + |
| 91 | + requirement = bench_cls.data_requirements() |
| 92 | + if requirement is None: |
| 93 | + con.print(f"[yellow]{bench_cls.__name__} declares no external data requirement; nothing to fetch.[/yellow]") |
| 94 | + return |
| 95 | + |
| 96 | + accepted = set(args.accept_license or []) |
| 97 | + if requirement.license_id not in accepted: |
| 98 | + con.print( |
| 99 | + f"[red]ERROR: this dataset requires accepting licence '{requirement.license_id}'.[/red]\n" |
| 100 | + f" Read: {requirement.license_url}\n" |
| 101 | + f" Re-run: vla-eval data fetch -c {args.config} --accept-license {requirement.license_id}" |
| 102 | + ) |
| 103 | + sys.exit(1) |
| 104 | + |
| 105 | + host_dir = Path(args.data_dir).expanduser().resolve() if args.data_dir else _default_host_data_dir(cache_subdir) |
| 106 | + host_dir.mkdir(parents=True, exist_ok=True) |
| 107 | + |
| 108 | + marker = host_dir / requirement.marker |
| 109 | + if marker.exists() and not args.force: |
| 110 | + con.print( |
| 111 | + f"[green]Data already present at {host_dir} (marker: {requirement.marker}). " |
| 112 | + "Use --force to refetch.[/green]" |
| 113 | + ) |
| 114 | + return |
| 115 | + |
| 116 | + docker_cfg = DockerConfig.from_dict(config.get("docker")) |
| 117 | + if not docker_cfg.image: |
| 118 | + con.print("[red]ERROR: 'docker.image' must be set in the config to fetch data[/red]") |
| 119 | + sys.exit(1) |
| 120 | + if shutil.which("docker") is None: |
| 121 | + con.print("[red]ERROR: 'docker' not found on PATH[/red]") |
| 122 | + sys.exit(1) |
| 123 | + |
| 124 | + argv = _build_docker_argv( |
| 125 | + docker_cfg.image, |
| 126 | + docker_cfg, |
| 127 | + host_dir, |
| 128 | + requirement, |
| 129 | + extra_gpus=getattr(args, "gpus", None), |
| 130 | + ) |
| 131 | + |
| 132 | + con.print(f"[bold]Fetching data → {host_dir}[/bold]") |
| 133 | + con.print(f" image: {docker_cfg.image}") |
| 134 | + con.print(f" mount: {host_dir} → {requirement.container_data_path}") |
| 135 | + if args.dry_run: |
| 136 | + con.print(" [yellow]--dry-run[/yellow]: would run:") |
| 137 | + con.print(f" {' '.join(argv)}") |
| 138 | + return |
| 139 | + |
| 140 | + completed = subprocess.run(argv, check=False) |
| 141 | + if completed.returncode != 0: |
| 142 | + con.print(f"[red]ERROR: docker run exited with {completed.returncode}[/red]") |
| 143 | + sys.exit(completed.returncode) |
| 144 | + con.print(f"[green]Done. Dataset available at {host_dir}.[/green]") |
| 145 | + |
| 146 | + |
| 147 | +def register(subparsers: argparse._SubParsersAction) -> None: |
| 148 | + """Wire ``data fetch`` into the top-level ``vla-eval`` parser.""" |
| 149 | + data_parser = subparsers.add_parser( |
| 150 | + "data", |
| 151 | + help="Manage external benchmark datasets", |
| 152 | + description=( |
| 153 | + "Fetch external datasets that aren't redistributable in the docker image. " |
| 154 | + "Each benchmark's data requirements are declared in its Benchmark class via " |
| 155 | + "data_requirements(); see vla_eval.benchmarks.base.DataRequirement." |
| 156 | + ), |
| 157 | + ) |
| 158 | + data_sub = data_parser.add_subparsers(dest="data_command", required=True) |
| 159 | + |
| 160 | + fetch_parser = data_sub.add_parser( |
| 161 | + "fetch", |
| 162 | + help="Download a benchmark's external data into the local cache", |
| 163 | + description=( |
| 164 | + "Resolves the benchmark class from the config, runs its download command " |
| 165 | + "inside the benchmark's docker image with the host cache mounted " |
| 166 | + "read-write at the container's data path. Idempotent: skips if the " |
| 167 | + "marker file already exists." |
| 168 | + ), |
| 169 | + ) |
| 170 | + fetch_parser.add_argument("--config", "-c", required=True, help="Path to a benchmark eval config YAML.") |
| 171 | + fetch_parser.add_argument( |
| 172 | + "--accept-license", |
| 173 | + action="append", |
| 174 | + default=[], |
| 175 | + metavar="ID", |
| 176 | + help="License ID to opt into (e.g. 'behavior-dataset-tos'). Repeatable.", |
| 177 | + ) |
| 178 | + fetch_parser.add_argument( |
| 179 | + "--data-dir", |
| 180 | + default=None, |
| 181 | + help="Override host data directory. Defaults to " |
| 182 | + "${VLA_EVAL_DATA_DIR}/<benchmark> or ~/.cache/vla-eval/<benchmark>.", |
| 183 | + ) |
| 184 | + fetch_parser.add_argument( |
| 185 | + "--gpus", |
| 186 | + default=None, |
| 187 | + help="GPU devices for the fetch container (e.g. '0,1'). Defaults to docker.gpus or 'all'.", |
| 188 | + ) |
| 189 | + fetch_parser.add_argument( |
| 190 | + "--force", |
| 191 | + action="store_true", |
| 192 | + help="Re-run the download even if the marker file is already present.", |
| 193 | + ) |
| 194 | + fetch_parser.add_argument( |
| 195 | + "--dry-run", |
| 196 | + action="store_true", |
| 197 | + help="Print the docker command that would run and exit.", |
| 198 | + ) |
| 199 | + fetch_parser.set_defaults(func=cmd_data_fetch) |
0 commit comments