File size: 4,314 Bytes
aa1df78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""The training pool: SWE-smith tasks (MIT) from repositories that share nothing with the evaluation benchmark.

    python train/make_pool.py eval/subset.json train/pool            # writes pool.json, pool_instances.json, disjoint.json

Rules, checked here and written to disjoint.json:
  - a training repository is never a repository of SWE-bench Verified (owner/name or bare name, case ignored);
  - only tasks with a problem statement, and only repositories whose tests run with pytest;
  - the draw is fixed: the REPOS largest eligible repositories, PER_REPO tasks each, one seed.
Run with the SWE-smith environment's Python (it holds the repository profiles).
"""
import collections
import json
import random
import re
import sys
from pathlib import Path

from datasets import load_dataset
from swesmith.profiles import registry

SEED, REPOS, PER_REPO = 20261201, 30, 8
DATASET = "SWE-bench/SWE-smith"


def owner_name(repo):                       # "swesmith/oauthlib__oauthlib.1fd52536" -> "oauthlib/oauthlib"
    m = re.fullmatch(r"swesmith/(.+?)__(.+)\.[0-9a-f]{8}", repo)
    return (m.group(1) + "/" + m.group(2)).lower() if m else None


def main():
    subset, out = json.load(open(sys.argv[1])), Path(sys.argv[2])
    out.mkdir(parents=True, exist_ok=True)
    bench = [r.lower() for r in subset["all_repos_of_the_benchmark"]]
    bench_names = {r.split("/")[1] for r in bench}
    ds = load_dataset(DATASET, split="train")
    by_repo = collections.defaultdict(list)
    skipped = collections.Counter()
    for i, (iid, repo, ps) in enumerate(zip(ds["instance_id"], ds["repo"], ds["problem_statement"])):
        on = owner_name(repo)
        if on is None:
            skipped["name not understood"] += 1
        elif on in bench or on.split("/")[1] in bench_names:
            skipped["repository of the benchmark"] += 1
        elif not (ps and ps.strip()):
            skipped["no problem statement"] += 1
        else:
            by_repo[repo].append(i)
    eligible = {}
    for repo, rows in by_repo.items():
        try:
            prof = registry.get(repo)
            cmd = prof.test_cmd
        except Exception:
            skipped["no profile"] += len(rows)
            continue
        if "pytest" not in cmd:
            skipped["tests are not pytest"] += len(rows)
            continue
        eligible[repo] = (rows, prof.image_name)
    chosen = sorted(eligible, key=lambda r: (-len(eligible[r][0]), r))[:REPOS]
    rng = random.Random(SEED)
    rows, images = [], {}
    for repo in chosen:
        idx, image = eligible[repo]
        pick = sorted(rng.sample(sorted(idx), min(PER_REPO, len(idx))))
        rows += pick
        images[repo] = {"harness_image": image, "dataset_image": ds[pick[0]]["image_name"], "tasks": len(pick), "eligible_tasks": len(idx)}
    inst = [ds[i] for i in rows]
    ids = sorted(x["instance_id"] for x in inst)
    train_repos = sorted({owner_name(r) for r in chosen})
    overlap = sorted(set(train_repos) & set(bench)) + sorted({r.split("/")[1] for r in train_repos} & bench_names)
    (out / "pool.json").write_text(json.dumps({
        "dataset": DATASET, "license": "mit", "split": "train", "dataset_fingerprint": ds._fingerprint, "seed": SEED,
        "method": f"the {REPOS} largest eligible repositories, random.Random(seed).sample of {PER_REPO} tasks each",
        "n": len(ids), "instance_ids": ids, "repositories": images, "skipped": dict(skipped)}, indent=1) + "\n", encoding="utf-8")
    (out / "pool_instances.json").write_text(json.dumps(inst) + "\n", encoding="utf-8")
    (out / "disjoint.json").write_text(json.dumps({
        "evaluation_benchmark": subset["dataset"], "evaluation_repositories": bench, "evaluation_instance_ids": subset["instance_ids"],
        "training_dataset": DATASET, "training_repositories": train_repos, "training_instance_ids": ids,
        "repositories_in_both": overlap, "instance_ids_in_both": sorted(set(ids) & set(subset["instance_ids"])),
        "disjoint": not overlap and not (set(ids) & set(subset["instance_ids"]))}, indent=1) + "\n", encoding="utf-8")
    print(f"{len(ids)} tasks from {len(chosen)} repositories; skipped {dict(skipped)}; in both: {overlap}")
    if overlap:
        raise SystemExit("TRAINING AND EVALUATION SHARE A REPOSITORY")


if __name__ == "__main__":
    main()