Code / benchmarks/hardware_manifest.py

benchmarks/hardware_manifest.py 72 lines
#!/usr/bin/env python3
# =============================================================================
#  Project   : modelmap
#  File      : benchmarks/hardware_manifest.py
#  Purpose   : Record chip, cores, RAM, SSD, macOS + software versions per run
#  Author    : Simon-Pierre Boucher
#  Contact   : contact@spboucher.ai
#  Website   : https://modelmap.io
#  Created   : 2026-08-12
#  Modified  : 2026-08-12
#  Platform  : macOS / Apple Silicon (arm64)
#  License   : All rights reserved (research code)
# =============================================================================
"""Hardware manifest for reproducibility (charter §0.2).

Every result file and every published map embeds this manifest so that any
number can be traced to the exact machine and software stack that produced it.
"""

from __future__ import annotations

import json
import platform
import subprocess
import sys
from importlib import metadata


def _sysctl(key: str) -> str:
    try:
        return subprocess.run(
            ["sysctl", "-n", key], capture_output=True, text=True, check=True
        ).stdout.strip()
    except subprocess.CalledProcessError:
        return ""


def _pkg_version(name: str) -> str | None:
    try:
        return metadata.version(name)
    except metadata.PackageNotFoundError:
        return None


def manifest() -> dict:
    mem_bytes = int(_sysctl("hw.memsize") or 0)
    return {
        "author": "Simon-Pierre Boucher",
        "contact": "contact@spboucher.ai",
        "website": "https://modelmap.io",
        "chip": {
            "brand": _sysctl("machdep.cpu.brand_string"),
            "cores_total": int(_sysctl("hw.ncpu") or 0),
            "cores_performance": int(_sysctl("hw.perflevel0.physicalcpu") or 0),
            "cores_efficiency": int(_sysctl("hw.perflevel1.physicalcpu") or 0),
        },
        "memory": {"unified_gb": round(mem_bytes / 2**30, 1), "pagesize": int(_sysctl("hw.pagesize") or 0)},
        "os": {"system": platform.system(), "version": platform.mac_ver()[0], "arch": platform.machine()},
        "software": {
            "python": platform.python_version(),
            "numpy": _pkg_version("numpy"),
            "mlx": _pkg_version("mlx"),
            "torch": _pkg_version("torch"),
            "safetensors": _pkg_version("safetensors"),
        },
    }


if __name__ == "__main__":
    json.dump(manifest(), sys.stdout, indent=2)
    print()