Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ Already served (any OpenAI-compatible endpoint):
uv run agbench eval run --model my-org/My-VLM --endpoint http://localhost:8000 --data-dir $DATA --judge
```

Hosted API (OpenAI-compatible):

```bash
export OPENAI_API_KEY=...
uv run agbench eval run --model gpt-5-mini --endpoint https://api.openai.com/v1 --api-key-env OPENAI_API_KEY --concurrency 8 --data-dir $DATA --judge
uv run agbench eval run --model gemini-2.5-flash --endpoint https://generativelanguage.googleapis.com/v1beta/openai --api-key-env GEMINI_API_KEY --data-dir $DATA --judge
uv run agbench eval run --model claude-sonnet-5-5 --endpoint https://api.anthropic.com/v1 --api-key-env ANTHROPIC_API_KEY --data-dir $DATA --judge
```

Model settings:

```bash
Expand All @@ -49,6 +58,7 @@ my-org/My-VLM:
sampling: {temperature: 0.7, top_p: 0.95}
thinking: {chat_template_kwargs: {enable_thinking: true}}
no_thinking: {chat_template_kwargs: {enable_thinking: false}}
effort: {low: {reasoning_effort: low}, high: {reasoning_effort: high}} # request fields per --reasoning-effort
box_order: xyxy # box_2d as [x_min, y_min, x_max, y_max]; default yxyx
bbox_key_format: {bbox_2d: xyxy_px} # other box keys: xyxy_1000 | xyxy_px
```
Expand Down Expand Up @@ -84,18 +94,23 @@ res.metrics
--gen-seed 1 # sampling seed (replicate runs)
--run-dir runs/<run_dir> # resume
--benchmark agml-full-v1 # default; 'none' builds items from $DATA
--strict # exit non-zero if the run check fails or the run is not comparable
```

## Results

```bash
uv run agbench eval check runs/<run_dir> # files, schema, items vs benchmark, coverage, comparable
uv run agbench eval judge --run runs/<run_dir>
uv run agbench eval judge --run runs/<run_dir> --judge-model gpt-5-mini --judge-endpoint https://api.openai.com/v1 --judge-api-key-env OPENAI_API_KEY # not comparable
uv run agbench eval report --runs runs
uv run agbench eval report --runs runs --csv reports/scores # scores.csv, ag_tasks.csv, datasets.csv
uv run agbench eval stats --runs runs --out reports/stats
uv run agbench eval view --runs runs # http://localhost:8502
```

`runs/<run_dir>/run_manifest.json`: agbench version, benchmark name and hash, model and sampling, judge, coverage, `comparable`.

## Benchmarks

```bash
Expand Down
20 changes: 16 additions & 4 deletions src/agbench/bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ def manifests(self) -> dict[str, dict]:
def raw_items(self) -> list[dict]:
return [json.loads(l) for l in self._text("items.jsonl.gz").splitlines()]

@property
def unavailable(self) -> dict[str, str]:
"""Datasets whose source repo is gone, {dataset: notice}. They stay in the benchmark (a local copy is still
evaluated); without one their items are skipped."""
return {u["dataset"]: f"{u['reason']} ({u['date']})" for u in self.spec.get("unavailable", [])}

@property
def config_path(self) -> Path:
return self.path / "config.yaml"
Expand Down Expand Up @@ -116,7 +122,8 @@ def items(self, data_dir, tasks=None, formats=None, only: str | None = None, lim
if not present[key]:
if key not in seen:
seen.add(key)
skipped.append({"dataset": r["dataset"], "task": r["task"], "reason": f"not downloaded (agbench data pull {self.name})"})
why = self.unavailable.get(r["dataset"]) or f"not downloaded (agbench data pull {self.name})"
skipped.append({"dataset": r["dataset"], "task": r["task"], "reason": why})
continue
keep.append(r)
if limit_per_dataset:
Expand Down Expand Up @@ -216,7 +223,8 @@ def freeze(name: str, items_path: Path, description: str, manifest_dir: Path | N
# ---------------------------------------------------------------- local data

def data_status(b: Bundle, data_dir, task=None, only=None) -> dict[str, str]:
"""Per dataset: done | missing | stale (wrong revision or different samples) | failed."""
"""Per dataset: done | missing | stale (wrong revision or different samples) | failed | unavailable (source repo
gone, no local copy)."""
from agbench.pipeline import base_dir, failure_path, TASK_DIRS

out = {}
Expand All @@ -228,14 +236,17 @@ def data_status(b: Bundle, data_dir, task=None, only=None) -> dict[str, str]:
have = {json.loads(l)["sample_id"] for l in open(d / "samples.jsonl")} if (d / "samples.jsonl").exists() else set()
same = meta.get("hf_sha") == m["hf_sha"] and have == {s["sample_id"] for s in m["samples"]}
out[n] = "done" if same else "stale"
elif n in b.unavailable:
out[n] = "unavailable"
else:
out[n] = "failed" if failure_path(Path(data_dir), n).exists() else "missing"
return out


def verify(b: Bundle, data_dir, task=None, only=None, decode: bool = True) -> dict[str, list[str]]:
"""Problems per dataset in a local copy: not downloaded, wrong revision, missing or extra samples, missing or
undecodable images. An empty dict means the local copy matches the bundle."""
undecodable images. An empty dict means the local copy matches the bundle. Unavailable datasets (source repo
gone) without a local copy are not problems; with one, they are checked like the rest."""
from PIL import Image, ImageFile

from agbench.pipeline import base_dir, TASK_DIRS
Expand All @@ -249,7 +260,8 @@ def verify(b: Bundle, data_dir, task=None, only=None, decode: bool = True) -> di
m, errs = b.manifests[n], []
d = base_dir(data_dir) / TASK_DIRS[m["task"]] / n
if not (d / "meta.json").exists():
problems[n] = ["not downloaded"]
if n not in b.unavailable:
problems[n] = ["not downloaded"]
continue
meta = json.loads((d / "meta.json").read_text())
if meta.get("hf_sha") != m["hf_sha"]:
Expand Down
4 changes: 4 additions & 0 deletions src/agbench/benchmarks/agml-full-v1/bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,7 @@ repinned:
date: '2026-11-23'
reason: source repo re-uploaded; images matched by SHA-256, byte-identical; 0 of
250 rows have different conversation text (frozen items unaffected)
unavailable:
- dataset: AgriBench
date: '2026-11-23'
reason: removed from Hugging Face (Project-AgML/AgriBench no longer exists)
31 changes: 24 additions & 7 deletions src/agbench/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,11 @@ def data_pull(
b = _bundle(name)
cfg = load_config(b.config_path)
status = bench.data_status(b, data_dir, task, only)
todo = [b.entry(n) for n, s in status.items() if refresh or s != "done"]
typer.echo(f"{name}: {len(status)} datasets selected, {len(status) - len(todo)} already present, {len(todo)} to download")
gone = [n for n, s in status.items() if s == "unavailable"]
todo = [b.entry(n) for n, s in status.items() if s not in ("unavailable", "done") or (refresh and n not in b.unavailable)]
typer.echo(f"{name}: {len(status)} datasets selected, {len(status) - len(todo) - len(gone)} already present, {len(todo)} to download"
+ (f", {len(gone)} unavailable" if gone else ""))
_print_unavailable(b, gone)
if not todo:
return
res = pipeline.run_many(cfg, str(b.config_path), todo, Path(data_dir).resolve(),
Expand All @@ -336,6 +339,15 @@ def data_pull(
raise typer.Exit(1)


def _print_unavailable(b, names: list[str]) -> None:
if not names:
return
n_items = Counter(r["dataset"] for r in b.raw_items)
typer.echo("Unavailable (kept in the benchmark; skipped without a local copy):")
for n in names:
typer.echo(f" {n} {b.unavailable[n]}; {n_items[n]} items")


def _print_failures(data_dir: Path, names: list[str]) -> None:
if not names:
return
Expand All @@ -353,14 +365,17 @@ def data_status(name: str = BenchArg, data_dir: Path = DataDirOpt, task: list[st
"""Which of a benchmark's datasets are present (done), missing, stale or failed in a data dir."""
from agbench import bench

st = bench.data_status(_bundle(name), data_dir, task, only)
b = _bundle(name)
st = bench.data_status(b, data_dir, task, only)
typer.echo(" ".join(f"{k}={v}" for k, v in sorted(Counter(st.values()).items())))
for n, s in st.items():
if s == "failed":
if s == "unavailable":
typer.echo(f"{s:<11} {n}: {b.unavailable[n]}")
elif s == "failed":
reason = json.loads(pipeline.failure_path(Path(data_dir), n).read_text())["reason"]
typer.echo(f"{s:<8} {n}: {reason}")
typer.echo(f"{s:<11} {n}: {reason}")
elif s != "done" or verbose:
typer.echo(f"{s:<8} {n}")
typer.echo(f"{s:<11} {n}")


@data_app.command("verify")
Expand All @@ -378,7 +393,9 @@ def data_verify(name: str = BenchArg, data_dir: Path = DataDirOpt, task: list[st
n = len(b.datasets(task, only))
for ds, errs in bad.items():
typer.echo(f"{ds}: " + "; ".join(errs[:5]) + (f" (+{len(errs) - 5} more)" if len(errs) > 5 else ""))
typer.echo(f"{n - len(bad)} of {n} datasets match {name} (hash {b.hash})")
gone = [d for d, s in bench.data_status(b, data_dir, task, only).items() if s == "unavailable"]
typer.echo(f"{n - len(bad) - len(gone)} of {n} datasets match {name} (hash {b.hash})")
_print_unavailable(b, gone)
if bad or problems:
raise typer.Exit(1)

Expand Down
207 changes: 207 additions & 0 deletions src/agbench/eval/check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
"""run_manifest.json (what a run was, and whether its scores compare with the reference results) and `agbench eval
check`: a run dir's files against the schema and its benchmark."""

import json
from collections import Counter
from datetime import datetime, timezone
from importlib.metadata import PackageNotFoundError, version
from pathlib import Path

from agbench.eval import metrics as mx

SCHEMA = 1
REQUIRED = ("item_id", "dataset", "task", "format", "answer", "text", "correct", "parse_ok", "needs_judge", "judge_correct",
"finish_reason", "error")
BY_TASK = {"classification": ("gt_label",), "detection": ("tp", "fp", "fn", "mean_iou", "pred_boxes"),
"segmentation": ("tp", "fp", "fn", "mean_iou", "pred_boxes"), "image_text_to_text": ("qa_format",)}


def _version() -> str | None:
try:
return version("agbench")
except PackageNotFoundError:
return None


def _read_jsonl(p: Path) -> list[dict]:
return [json.loads(l) for l in open(p) if l.strip()] if p.exists() else []


def _bundle(name: str | None):
from agbench import bench

try:
return bench.load(name) if name else None
except KeyError:
return None


def comparability(run: dict, records: list[dict]) -> list[str]:
"""Reasons a run's scores are not comparable with the benchmark's reference results (empty: comparable)."""
from agbench.bench import judge_prompt_sha

bm = run.get("benchmark", {}).get("benchmark")
if not bm:
return ["no frozen benchmark (--benchmark none): items built from the local data"]
why = []
b = _bundle(bm["name"])
if b is None:
why.append(f"benchmark {bm['name']} is not installed")
elif b.hash != bm["hash"]:
why.append(f"benchmark hash {bm['hash']} differs from the installed {bm['name']} ({b.hash})")
if bm.get("variant"):
why.append(f"item {bm['variant']} (seed or multiple-choice options changed)")
if any(r.get("needs_judge") for r in records):
j = run.get("judge") or {}
ref = (b.spec["judge"] if b else {}) or {}
if any(r.get("needs_judge") and r.get("judge_correct") is None for r in records):
why.append("open-ended answers not judged yet (agbench eval judge)")
elif j:
if ref and j.get("model") != ref.get("model"):
why.append(f"judge {j.get('model')} is not the reference judge {ref.get('model')}")
if ref and j.get("prompt_sha", judge_prompt_sha()) != ref.get("prompt_sha"):
why.append("judge prompt differs from the reference judge prompt")
return why


def manifest(run_dir, records: list[dict] | None = None) -> dict | None:
"""Summary of a run: agbench version, benchmark (name, hash, variant), method and sampling, item seed, judge,
coverage and comparability."""
run_dir = Path(run_dir)
if not (run_dir / "run.json").exists():
return None
run = json.loads((run_dir / "run.json").read_text())
records = records if records is not None else _read_jsonl(run_dir / "predictions.jsonl")
args = run.get("benchmark", {})
bm = args.get("benchmark")
b = _bundle(bm["name"]) if bm else None
skipped = json.loads((run_dir / "skipped.json").read_text()) if (run_dir / "skipped.json").exists() else []
answered = {r["item_id"] for r in records if not r.get("error")}
cov = {"items": run.get("n_items"), "answered": len(answered),
"errors": sum(1 for r in records if r.get("error")),
"pending_judge": sum(1 for r in records if r.get("needs_judge") and r.get("judge_correct") is None),
"skipped_datasets": sorted({s["dataset"] for s in skipped if "dataset" in s})}
if b:
filters = {k: args.get(k) for k in ("tasks", "formats", "only", "limit_per_dataset") if args.get(k)}
cov |= {"benchmark_items": len(b.raw_items), "filters": filters,
"unavailable_datasets": sorted(set(b.unavailable) & set(cov["skipped_datasets"]))}
cov["complete"] = not filters and len(answered) == len(b.raw_items) - sum(
1 for r in b.raw_items if r["dataset"] in cov["unavailable_datasets"])
why = comparability(run, records)
return {"schema": SCHEMA, "written": datetime.now(timezone.utc).isoformat(timespec="seconds"),
"agbench": {"version": _version(), "git": run.get("agbench_git")}, "benchmark": bm,
"method": run.get("method"), "item_seed": args.get("seed"), "mcqa": args.get("mcqa"),
"judge": run.get("judge"), "coverage": cov, "comparable": not why, "not_comparable": why}


def write_manifest(run_dir, records: list[dict] | None = None) -> dict | None:
m = manifest(run_dir, records)
if m:
(Path(run_dir) / "run_manifest.json").write_text(json.dumps(m, indent=2))
return m


def _items_match_bundle(items: list[dict], b) -> list[str]:
"""Run items that differ from the benchmark's frozen items (any field; images compared by relative path)."""
frozen = {r["item_id"]: r for r in b.raw_items}
bad = []
for it in items:
f = frozen.get(it["item_id"])
if f is None:
bad.append(f"{it['item_id']}: not in {b.name}")
continue
imgs_ok = len(it["images"]) == len(f["images"]) and all(a.endswith(p) for a, p in zip(it["images"], f["images"]))
diff = [k for k in f if k != "images" and it.get(k) != f[k]]
if diff or not imgs_ok:
bad.append(f"{it['item_id']}: {', '.join(diff + ([] if imgs_ok else ['images']))} differ from {b.name}")
return bad


def _same(a, b) -> bool:
if isinstance(a, float) and isinstance(b, float):
return a == b or (a != a and b != b) or abs(a - b) <= 1e-9 * max(abs(a), abs(b))
if isinstance(a, dict) and isinstance(b, dict):
return a.keys() == b.keys() and all(_same(a[k], b[k]) for k in a)
if isinstance(a, list) and isinstance(b, list):
return len(a) == len(b) and all(_same(x, y) for x, y in zip(a, b))
return a == b


def check_run(run_dir) -> dict:
"""{'errors': [...], 'warnings': [...], 'stats': {...}}. Errors: the run's files are broken, inconsistent or do not
match its benchmark. Warnings: the run is valid but incomplete or not comparable with the reference results."""
run_dir = Path(run_dir)
errors, warnings, stats = [], [], {}
for f in ("run.json", "items.jsonl", "predictions.jsonl"):
if not (run_dir / f).exists():
errors.append(f"{f} missing")
if errors:
return {"errors": errors, "warnings": warnings, "stats": stats}
run = json.loads((run_dir / "run.json").read_text())
items = _read_jsonl(run_dir / "items.jsonl")
recs = _read_jsonl(run_dir / "predictions.jsonl")
ids = {it["item_id"] for it in items}

dup = [k for k, n in Counter(r.get("item_id") for r in recs).items() if n > 1]
if dup:
errors.append(f"{len(dup)} items with more than one prediction (e.g. {dup[0]})")
extra = [r["item_id"] for r in recs if r.get("item_id") not in ids]
if extra:
errors.append(f"{len(extra)} predictions for items not in items.jsonl (e.g. {extra[0]})")
bad_schema = Counter()
for r in recs:
for k in REQUIRED + BY_TASK.get(r.get("task"), ()):
if k not in r:
bad_schema[k] += 1
if r.get("task") in mx.BOX_TASKS:
if not isinstance(r.get("correct"), (int, float)) or isinstance(r.get("correct"), bool):
bad_schema["correct (number for box tasks)"] += 1
elif not isinstance(r.get("correct"), bool):
bad_schema["correct (bool)"] += 1
for k, n in bad_schema.items():
errors.append(f"{n} predictions missing or with a wrong type for '{k}'")

bm = run.get("benchmark", {}).get("benchmark")
b = _bundle(bm["name"]) if bm else None
if bm and b and not bm.get("variant"):
diff = _items_match_bundle(items, b)
if diff:
errors.append(f"{len(diff)} items differ from the frozen benchmark (e.g. {diff[0]})")

if (run_dir / "metrics.json").exists() and not bad_schema:
stored = json.loads((run_dir / "metrics.json").read_text())
fresh = json.loads(json.dumps(mx.compute([r for r in recs if r.get("item_id") in ids])))
# A dataset row's ag_task label (its most common bucket) broke ties by set order before agbench 0.1.0's fix;
# runs from then can carry either label for a tied dataset. Scores are compared; that label is not.
strip = lambda m: {**m, "datasets": [{k: v for k, v in r.items() if k != "ag_task"} for r in m.get("datasets", [])]}
if not _same(strip(fresh), strip(stored)):
errors.append("metrics.json does not match predictions.jsonl (rewrite it: agbench eval check --rescore)")
elif not (run_dir / "metrics.json").exists():
warnings.append("metrics.json missing (run not finished)")

have = {r["item_id"] for r in recs if not r.get("error")}
n_err = sum(1 for r in recs if r.get("error"))
n = len(recs) or 1
stats = {"items": len(items), "predictions": len(recs), "answered": len(have), "errors": n_err,
"parse_fail_rate": round(sum(1 for r in recs if not r.get("parse_ok") and not r.get("error")) / n, 4),
"truncated_rate": round(sum(1 for r in recs if r.get("truncated")) / n, 4),
"idk_rate": round(sum(1 for r in recs if r.get("idk")) / n, 4),
"empty_answer_rate": round(sum(1 for r in recs if not (r.get("text") or "").strip() and not r.get("error")) / n, 4)}
if len(have) < len(items):
warnings.append(f"{len(items) - len(have)} of {len(items)} items without an answer ({n_err} request errors); "
"resume with --run-dir")
judges = Counter(r.get("judge_model") for r in recs if r.get("judge_correct") is not None)
if len(judges) > 1:
warnings.append(f"open-ended answers judged by more than one judge: {dict(judges)}")
m = manifest(run_dir, recs)
stats["comparable"] = m["comparable"]
stats["complete"] = m["coverage"].get("complete")
for w in m["not_comparable"]:
warnings.append(f"not comparable: {w}")
cov = m["coverage"]
if cov.get("complete") is False and len(have) == len(items):
warnings.append(f"covers {len(items)} of {cov['benchmark_items']} benchmark items"
+ (f" ({', '.join(f'{k}={v}' for k, v in cov['filters'].items())})" if cov["filters"] else ""))
if m["coverage"].get("unavailable_datasets"):
warnings.append("skipped (source removed): " + ", ".join(m["coverage"]["unavailable_datasets"]))
return {"errors": errors, "warnings": warnings, "stats": stats}
Loading
Loading