File size: 2,332 Bytes
f45837e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Download cmd-shard artifacts from GCS, preserving their repo-relative paths.

    gke/.venv/bin/python gke/fetch_artifacts.py --run tgt10-dog-breed-identification
    gke/.venv/bin/python gke/fetch_artifacts.py --prefix tgt10-   # every matching run

`submit_eval.py collect` concatenates every *.jsonl under a run into one file,
which is right for eval rows and wrong for cmd artifacts: a collection run
uploads tgt_<comp>.jsonl AND its summary sidecars, and a pool run uploads both
the raw states and the scored states — merging those duplicates every row.
This keeps each artifact as the file the pod wrote it as.
"""

from __future__ import annotations

import argparse
from pathlib import Path

from google.cloud import storage

from settings import BUCKET, PROJECT

REPO = Path(__file__).resolve().parent.parent


def main() -> int:
    ap = argparse.ArgumentParser(description=__doc__)
    ap.add_argument("--run", action="append", default=[])
    ap.add_argument("--prefix", default="",
                    help="fetch every run whose name starts with this")
    ap.add_argument("--dest", default=str(REPO),
                    help="artifact paths are repo-relative; default writes "
                         "them into the repo tree")
    args = ap.parse_args()

    client = storage.Client(project=PROJECT)
    bucket = client.bucket(BUCKET)

    runs = list(args.run)
    if args.prefix:
        seen = set()
        for bl in client.list_blobs(BUCKET, prefix="runs/"):
            name = bl.name.split("/")[1]
            if name.startswith(args.prefix):
                seen.add(name)
        runs += sorted(seen - set(runs))
    if not runs:
        raise SystemExit("nothing to fetch: pass --run or --prefix")

    n = 0
    for run in runs:
        for bl in client.list_blobs(BUCKET, prefix=f"runs/{run}/shards/"):
            if "-artifacts/" not in bl.name:
                continue
            rel = bl.name.split("-artifacts/", 1)[1]
            dest = Path(args.dest) / rel
            dest.parent.mkdir(parents=True, exist_ok=True)
            bl.download_to_filename(dest)
            print(f"[{run}] {rel} ({(bl.size or 0)/1e6:.1f} MB)")
            n += 1
    print(f"{n} artifacts from {len(runs)} runs")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())