Skip to content
Open
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
2 changes: 2 additions & 0 deletions bench/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# bench/ — runnable demo

For product revision comparisons, see the [BIRD Mini-Dev SQLite benchmark](bird/README.md).

A self-contained walkthrough of Lang2SQL V1's headline value, with **no Discord
token and no live database**. It runs the real shipped modules against the
canned Postgres stub and the offline `FakeLLM`.
Expand Down
3 changes: 3 additions & 0 deletions bench/bird/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
runs/
*.sqlite
*.zip
64 changes: 64 additions & 0 deletions bench/bird/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# BIRD Mini-Dev SQLite 100문항 벤치

제품 변경 전후를 같은 문항으로 평가하는 모듈이다. 이 벤치의 커밋과 제품의 기준·변경 커밋을 각각 체크아웃하고, 같은 벤치 워크트리에서 두 제품을 평가한다.

## 고정 프로토콜

- [표본 manifest](manifest-sqlite100.json)는 기존 벤치와 동일한 SQLite 리비전에서 evidence가 있는 365문항을 대상으로 한다. `db_id`와 난이도별 비례 몫을 최대 나머지 방식으로 정하고, 각 그룹에서 `SHA-256(20261114:question_id)`가 작은 문항을 뽑았다. 고정한 100문항에 11개 DB와 `challenging` 98건, `moderate` 2건이 포함된다. [기존 결과 PR #315](https://github.com/CausalInferenceLab/Lang2SQL/pull/315)
- **B**는 질문과 같은 사용자 메시지에 `BIRD evidence` 블록으로 원문을 전달한다. **D**는 질문에 공통 SQL 제출 지시만 붙인다. 각 문항은 새 세션과 빈 인메모리 지식 저장소에서 시작한다.
- 평가 대상 커밋의 기본 도구 레지스트리 전체를 제품의 순서와 스키마 그대로 제공한다. 컬럼 보강, 용어 등록과 기억은 해당 문항의 임시 저장소에 기록된다. 문서 도구에는 `ingest_doc`의 인라인 본문을 전달할 수 있다. 도구 내부 모델 호출도 문항당 8회 예산에 포함된다.
- [프로토콜 설정](protocol.py)은 모델 ID, 응답 확인용 모델 ID, 호출 설정을 고정한다. `gpt-4.1-mini-2025-04-14`와 `gpt-4.1-2025-04-14`는 OpenAI SDK 3.51.0, `gpt-6-luna`는 SDK 3.57.0과 `reasoning_effort=none`을 사용한다. Luna는 공급자의 별칭으로 지정하고 응답 모델 ID와 제공되는 `system_fingerprint`를 기록한다. [기존 런타임 설정](https://github.com/CausalInferenceLab/Lang2SQL/pull/315)
- Python 3.13.14, SQLite 3.53.1, LiteLLM 1.109.1을 사용한다. SQLAlchemy와 나머지 패키지는 [잠금 파일](uv.lock)에 고정한다. 문항당 모델 호출은 8회, 출력은 호출당 4,096토큰으로 제한한다. 호출·문항·SQL 제한은 각각 120초·480초·30초이며, 채점 결과는 100,000행까지 읽는다. 각 모델은 B, D 순서로 실행하고 세 모델은 병렬로 돌린다. 조합당 동시성은 4다.
- EX는 읽기 전용 SQLite에서 예측과 기준 SQL의 행 튜플 집합이 같으면 1점이다. 행 순서와 중복은 무시하고, 열 순서와 값은 유지한다. SQL 미제출과 실행 실패도 분모에 포함한다. [BIRD EX 규칙](https://github.com/bird-bench/mini_dev/blob/1c49a375e76c471e218564da3f4617513d33e371/evaluation/evaluation_ex.py#L17)

같은 100문항으로 3개 프로필과 2개 입력 조건을 평가하므로 제품 버전당 600회다. 기준·변경군을 모두 새로 실행하면 1,200회다. 이미 같은 벤치와 설정으로 저장된 기준군 행렬이 있으면 `compare`에서 다시 사용할 수 있다.

## 워크트리와 입력 준비

절대 경로 `BENCH_TREE`, `BASE_TREE`, `CANDIDATE_TREE`, `RUN_ROOT`를 정한다. `BENCH_SHA`에 이 모듈의 전체 커밋 SHA를, `BASE_SHA`와 `CANDIDATE_SHA`에 제품 커밋의 전체 SHA를 지정한다. GitHub에서 벤치 PR 참조를 가져올 때는 `BENCH_PR`에 PR 번호를 넣는다.

```bash
git fetch origin "refs/pull/$BENCH_PR/head"
git worktree add --detach "$BENCH_TREE" "$BENCH_SHA"
git worktree add --detach "$BASE_TREE" "$BASE_SHA"
git worktree add --detach "$CANDIDATE_TREE" "$CANDIDATE_SHA"
cd "$BENCH_TREE"

mkdir -p "$RUN_ROOT"
UV_PROJECT_ENVIRONMENT="$RUN_ROOT/env-gpt41" \
uv sync --project bench/bird --locked --managed-python --python 3.13.14 --extra gpt41
UV_PROJECT_ENVIRONMENT="$RUN_ROOT/env-luna" \
uv sync --project bench/bird --locked --managed-python --python 3.13.14 --extra luna
export BIRD_GPT41_PY="$RUN_ROOT/env-gpt41/bin/python"
export BIRD_LUNA_PY="$RUN_ROOT/env-luna/bin/python"
```

`SOURCE`는 고정 리비전의 `data/mini_dev_sqlite.jsonl` 경로다. `DB_DIR`은 BIRD Dev 배포본의 내부 `dev_databases.zip`을 푼 디렉터리다. `<db_id>/<db_id>.sqlite`와 `<db_id>.sqlite` 구조를 지원한다. `prepare`가 주석과 DB 파일의 SHA-256을 manifest와 대조하고, 모델 입력용 `cases.jsonl`로 내보낸다. 기준 SQL은 별도의 채점 입력으로 사용한다. [고정 주석 리비전](https://huggingface.co/datasets/birdsql/bird_mini_dev/tree/38818fcbc46038129592680c51b2c8b597aac27c/data) · [SQLite 배포본](https://bird-bench.oss-cn-beijing.aliyuncs.com/dev.zip)

```bash
PREPARED="$RUN_ROOT/prepared"
"$BIRD_GPT41_PY" -m bench.bird.evaluate prepare \
--source "$SOURCE" --db-dir "$DB_DIR" --output "$PREPARED"
"$BIRD_GPT41_PY" -m bench.bird.evaluate check-gold \
--source "$SOURCE" --db-dir "$DB_DIR" --prepared "$PREPARED"
```

## 변경 전후 평가

실행 환경에 기존 벤치의 LiteLLM 인증을 제공한 뒤 아래 명령을 실행한다. `matrix.sh`에서 같은 벤치 워크트리와 준비 입력으로 각 제품의 6개 조합을 생성·채점한다. 동일한 명령을 다시 실행하면 기록이 있는 조합부터 이어서 진행한다.

```bash
bash bench/bird/matrix.sh "$BASE_TREE" "$BASE_SHA" \
"$PREPARED" "$SOURCE" "$DB_DIR" "$RUN_ROOT/baseline"
bash bench/bird/matrix.sh "$CANDIDATE_TREE" "$CANDIDATE_SHA" \
"$PREPARED" "$SOURCE" "$DB_DIR" "$RUN_ROOT/candidate"
"$BIRD_GPT41_PY" -m bench.bird.evaluate compare \
--baseline "$RUN_ROOT/baseline" --candidate "$RUN_ROOT/candidate" \
--output "$RUN_ROOT/comparison"
```

각 조합의 `settings.json`은 두 커밋, 데이터·잠금 파일 지문, 모델 프로필과 실제 런타임을 기록한다. `compare`는 동일한 벤치와 입력, 프로필별 런타임으로 완료한 조합만 받아들인다. `comparison.json`에서 조건·프로필별 제품 전후 EX, 정오답 전환, 호출 수와 모델 응답 정보를 확인할 수 있다. 제품별 B−D도 함께 계산한다. `paired-scores.jsonl`에는 문항 ID별 쌍을 저장한다.

## 오프라인 스모크

`prepare --smoke-cases 2`로 다른 DB에서 고정 문항 두 개를 준비하고 `check-gold`를 실행한다. 위의 두 `matrix.sh` 호출에 `--smoke`를 추가하고, `compare`에도 `--smoke`를 붙인다. 별도 결과 경로를 사용한다. 스크립트 응답으로 전체 도구 노출과 SQL 도구 호출, 최종 SQL 제출, EX 채점, 문항 대응을 확인한다. 오프라인 결과에는 `scripted-smoke` 모드가 기록된다.
1 change: 1 addition & 0 deletions bench/bird/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""BIRD Mini-Dev SQLite evidence evaluation."""
182 changes: 182 additions & 0 deletions bench/bird/comparison.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
"""Join the two product revisions by profile, evidence condition and question."""

from __future__ import annotations

import json
from pathlib import Path

from .dataset import load_manifest
from .io import ProtocolError, digest, json_digest, read_json, write_json, write_text
from .protocol import CONDITIONS, PROFILES, check_settings


def load_runs(root: Path, commit: str, *, smoke: bool) -> dict:
runs = {}
for profile in PROFILES:
for condition in CONDITIONS:
path = root / f"{condition}-{profile}"
manifest = load_manifest(path)
settings = read_json(path / "settings.json")
check_settings(manifest, settings, commit)
if (manifest["smoke_cases"] is not None) != smoke:
raise ProtocolError("Comparison requires runs of the selected mode")
if (
settings["condition"] != condition
or settings["profile"]["name"] != profile
):
raise ProtocolError(f"Run has the wrong condition or profile: {path}")

summary = read_json(path / "summary.json")
scores_path = path / "scores.json"
identity = {
"status": "completed",
"condition": condition,
"profile": profile,
"model": settings["profile"]["model"],
"benchmark_commit": commit,
"product_commit": settings["product_commit"],
"mode": settings["mode"],
"manifest_digest": settings["manifest_digest"],
"settings_digest": json_digest(settings),
"scores_sha256": digest(scores_path),
"total": len(manifest["cases"]),
}
if any(summary.get(key) != value for key, value in identity.items()):
raise ProtocolError(
f"Run has incomplete or mismatched score artifacts: {path}"
)
if set(summary["response_models"]) != {PROFILES[profile]["response_model"]}:
raise ProtocolError(
f"Run has unexpected provider response models: {path}"
)

rows = read_json(scores_path)
expected = [
(case["question_id"], case["db_id"]) for case in manifest["cases"]
]
if [(row["question_id"], row["db_id"]) for row in rows] != expected or any(
row["condition"] != condition
or row["profile"] != profile
or row["ex"] not in (0, 1)
for row in rows
):
raise ProtocolError(
f"Scores do not cover the exact frozen cohort: {path}"
)
if summary["correct"] != sum(row["ex"] for row in rows):
raise ProtocolError(
f"Summary correct count differs from scores: {path}"
)
runs[condition, profile] = settings, summary, rows
return runs


def paired_metrics(before: list[dict], after: list[dict]) -> dict:
pairs = list(zip(before, after, strict=True))
if any(left["question_id"] != right["question_id"] for left, right in pairs):
raise ProtocolError("Scores must be paired by question ID")
a_correct = sum(row["ex"] for row in before)
b_correct = sum(row["ex"] for row in after)
return {
"questions": len(pairs),
"before_correct": a_correct,
"after_correct": b_correct,
"before_ex_pct": 100 * a_correct / len(pairs),
"after_ex_pct": 100 * b_correct / len(pairs),
"delta_ex_pp": 100 * (b_correct - a_correct) / len(pairs),
"wrong_to_right": sum(left["ex"] < right["ex"] for left, right in pairs),
"right_to_wrong": sum(left["ex"] > right["ex"] for left, right in pairs),
}


def compare(
baseline: Path,
candidate: Path,
output: Path,
commit: str,
*,
smoke: bool,
) -> dict:
versions = {
"baseline": load_runs(baseline, commit, smoke=smoke),
"candidate": load_runs(candidate, commit, smoke=smoke),
}
manifests = {
settings["manifest_digest"]
for runs in versions.values()
for settings, _, _ in runs.values()
}
if len(manifests) != 1:
raise ProtocolError("All runs must use exactly the same prepared manifest")
products = {}
for label, runs in versions.items():
sources = {
(settings["product_commit"], settings["product_lock_sha256"])
for settings, _, _ in runs.values()
}
if len(sources) != 1:
raise ProtocolError(f"{label} mixes different product checkouts")
products[label] = sources.pop()[0]

comparisons, evidence = {}, {label: {} for label in versions}
paired = []
for profile in PROFILES:
runtimes = [
runs[condition, profile][0]["runtime"]
for runs in versions.values()
for condition in CONDITIONS
]
if any(runtime != runtimes[0] for runtime in runtimes[1:]):
raise ProtocolError(f"Comparisons must use the same runtime for {profile}")

for label, runs in versions.items():
without = runs["D", profile][2]
supplied = runs["B", profile][2]
for left, right in zip(without, supplied, strict=True):
for field in ("system_digest", "tools_digest"):
if left[field] and right[field] and left[field] != right[field]:
raise ProtocolError(
f"Evidence conditions differ in {field} for {label}/"
f"{profile}/{left['question_id']}"
)
evidence[label][profile] = paired_metrics(without, supplied)

for condition in CONDITIONS:
_, baseline_summary, before = versions["baseline"][condition, profile]
_, candidate_summary, after = versions["candidate"][condition, profile]
comparisons[f"{condition}-{profile}"] = {
**paired_metrics(before, after),
"before_model_calls": baseline_summary["model_calls"],
"after_model_calls": candidate_summary["model_calls"],
"before_case_seconds": baseline_summary["case_seconds"],
"after_case_seconds": candidate_summary["case_seconds"],
"before_system_fingerprints": baseline_summary["system_fingerprints"],
"after_system_fingerprints": candidate_summary["system_fingerprints"],
}
paired.extend(
{
"condition": condition,
"profile": profile,
"question_id": left["question_id"],
"db_id": left["db_id"],
"baseline_ex": left["ex"],
"candidate_ex": right["ex"],
}
for left, right in zip(before, after, strict=True)
)
report = {
"mode": "scripted-smoke" if smoke else "benchmark",
"benchmark_commit": commit,
"product_commits": products,
"manifest_digest": manifests.pop(),
"matched_product_pairs": len(paired),
"product_comparisons": comparisons,
"evidence_comparisons_d_to_b": evidence,
}
output.mkdir(parents=True, exist_ok=False)
write_text(
output / "paired-scores.jsonl",
"".join(json.dumps(row, ensure_ascii=False) + "\n" for row in paired),
)
write_json(output / "comparison.json", report)
return report
Loading