File size: 1,497 Bytes
f76c3bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Integrity check of a Space bundle's checkpoint copies. deploy/sync.py --flagship ships each checkpoint without its train_keys (unused at
inference) and records both hashes in models/<tier>/info.json: <key>_sha256 = the published checkpoint (= registry, the provenance) and
<key>_bundle_sha256 = the shipped copy. install() makes geomgnn.paper check the copy against the latter; folders without
*_bundle_sha256 and the registry's source paths keep the registry check unchanged."""
from __future__ import annotations

import json
from pathlib import Path

from . import paper as PA

_REGISTRY_CHECK = PA._check


def _check(reg, name, model_dir=None):
    md = PA._model_dir(model_dir)
    info = Path(md) / name / "info.json" if md else None
    z = json.loads(info.read_text()) if info is not None and info.exists() else {}
    if "checkpoints_bundle_sha256" not in z:
        return _REGISTRY_CHECK(reg, name, model_dir)
    a = PA.arms(reg)[name]
    for key in ("checkpoints", "surrogate"):
        fs, hs = PA.files(reg, name, key, model_dir), z.get(key + "_bundle_sha256") or []
        if z.get(key + "_sha256", []) != PA._hashes(a, key) or len(fs) != len(hs):
            raise RuntimeError(f"{name}: {info} does not list the registry's {key} (sha256)")
        for f, h in zip(fs, hs):
            if PA._sha(f) != h:
                raise RuntimeError(f"{name}: {f.name} SHA256 differs from the bundle manifest ({h[:12]})")
    return a


def install() -> None:
    PA._check = _check