File size: 2,701 Bytes
48ba1a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
90
91
92
#!/usr/bin/env python3
"""Aggregate per-cell calibration JSONs into the planner's table.

Reads every per-cell JSON produced by ``calibrate_memory_planner.py``
and emits ``recovar/utils/memory_calibration_data.json`` in the schema
the planner expects.
"""

from __future__ import annotations

import argparse
import json
import os
from pathlib import Path


REPO_ROOT = Path(__file__).resolve().parents[1]


def main() -> int:
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--cells-dir",
        type=Path,
        default=Path("/scratch/gpfs/GILLES/mg6942/calibration_runs/cells"),
    )
    parser.add_argument(
        "--out",
        type=Path,
        default=REPO_ROOT / "recovar" / "utils" / "memory_calibration_data.json",
    )
    parser.add_argument("--gpu-kind", default="NVIDIA H100")
    parser.add_argument("--driver", default="auto")
    parser.add_argument("--jax-version", default="auto")
    args = parser.parse_args()

    if args.driver == "auto":
        args.driver = (
            os.popen("nvidia-smi --query-gpu=driver_version --format=csv,noheader 2>/dev/null").read().strip()
            or "unknown"
        )
    if args.jax_version == "auto":
        try:
            import jax

            args.jax_version = jax.__version__
        except Exception:
            args.jax_version = "unknown"

    tables: dict[str, list[dict]] = {"pipeline": [], "compute_state": []}
    for path in sorted(args.cells_dir.glob("*.json")):
        with path.open() as fh:
            cell = json.load(fh)
        if cell.get("status") not in ("ok", "oom"):
            continue
        cmd = cell.get("command")
        if cmd not in tables:
            continue
        tables[cmd].append(
            {
                "grid_size": cell["grid_size"],
                "backend": cell["backend"],
                "n_pcs": cell["n_pcs"],
                "peak_gb_total": cell.get("peak_gb_total") or 0.0,
                "peak_gb_by_phase": cell.get("peak_gb_by_phase") or {},
                "status": cell["status"],
            }
        )

    payload = {
        "schema_version": 1,
        "calibrated_on": {
            "gpu_kind": args.gpu_kind,
            "driver": args.driver,
            "jax": args.jax_version,
        },
        "tables": tables,
    }
    args.out.parent.mkdir(parents=True, exist_ok=True)
    with args.out.open("w") as fh:
        json.dump(payload, fh, indent=2, default=str)
    print(
        f"[aggregate] wrote {args.out}: "
        f"pipeline cells={len(tables['pipeline'])}, "
        f"compute_state cells={len(tables['compute_state'])}"
    )
    return 0


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