--- a/ritz-ckpt.py +++ b/ritz-ckpt.py @@ -74,7 +74,8 @@ def _read_json(path, default=None): try: - return json.load(open(path)) - except Exception: + with open(path, encoding="utf-8") as stream: + return json.load(stream) + except FileNotFoundError: return default @@ -126,7 +127,13 @@ os.makedirs(self.dir, exist_ok=True) self.fp = _fp(k, eps, d, prec) self.stop_path = stop_path or os.path.join(self.dir, "STOP") - meta = _read_json(os.path.join(self.dir, "meta.json")) + meta_path = os.path.join(self.dir, "meta.json") + meta = _read_json(meta_path) + if meta is None and os.path.exists(meta_path): + raise ValueError("checkpoint metadata must not be null") + artifacts = ("dg.npy", "Ld.npy", "Af.npy", "vec.npz") + if meta is None and any(os.path.exists(self.path(name)) for name in artifacts): + raise ValueError("checkpoint artifacts lack their parameter manifest") if meta is not None and meta != self.fp: raise ValueError(f"checkpoint fingerprint mismatch: {meta} != {self.fp}") _atomic_json(os.path.join(self.dir, "meta.json"), self.fp)