SPB Git

Repository Public Research preview

provenance.py 2.5 KB 77 lines python

1"""Provenance: timestamps, git state, environment and hardware descriptors (no secrets)."""2from __future__ import annotations34import datetime as _dt5import os6import platform7import subprocess8import sys9from pathlib import Path1011ROOT = Path(__file__).resolve().parents[3]121314def utc_now() -> str:15    return _dt.datetime.now(_dt.timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")161718def git_state(root: Path = ROOT) -> dict:19    def run(*args: str) -> str | None:20        try:21            return subprocess.run(["git", *args], cwd=root, capture_output=True, text=True, timeout=10).stdout.strip() or None22        except Exception:  # noqa: BLE00123            return None2425    return {"commit": run("rev-parse", "HEAD"), "branch": run("rev-parse", "--abbrev-ref", "HEAD"), "dirty": bool(run("status", "--porcelain"))}262728def library_versions() -> dict:29    out: dict[str, str | None] = {"python": sys.version.split()[0]}30    for mod in ("torch", "numpy", "anthropic", "transformers", "sentencepiece", "lxml", "pyarrow", "sacrebleu", "datasketch", "rapidfuzz"):31        try:32            m = __import__(mod)33            out[mod] = getattr(m, "__version__", "?")34        except Exception:  # noqa: BLE00135            out[mod] = None36    return out373839def hardware() -> dict:40    info: dict = {"platform": platform.platform(), "machine": platform.machine(), "cpu_count": os.cpu_count()}41    try:42        if sys.platform == "darwin":43            info["chip"] = subprocess.run(["sysctl", "-n", "machdep.cpu.brand_string"], capture_output=True, text=True).stdout.strip()44            info["ram_gb"] = round(int(subprocess.run(["sysctl", "-n", "hw.memsize"], capture_output=True, text=True).stdout) / 2**30, 1)45    except Exception:  # noqa: BLE00146        pass47    try:48        import torch4950        info["torch_mps"] = bool(torch.backends.mps.is_available())51        info["torch_cuda"] = bool(torch.cuda.is_available())52        if torch.cuda.is_available():53            info["cuda_device"] = torch.cuda.get_device_name(0)54    except Exception:  # noqa: BLE00155        pass56    return info575859def default_device() -> str:60    try:61        import torch6263        if torch.cuda.is_available():64            return "cuda"65        if torch.backends.mps.is_available():66            return "mps"67    except Exception:  # noqa: BLE00168        pass69    return "cpu"707172def run_header(extra: dict | None = None) -> dict:73    h = {"created_at": utc_now(), "git": git_state(), "libraries": library_versions(), "hardware": hardware()}74    if extra:75        h.update(extra)76    return h77
Files main
Menu

Theme