from __future__ import annotations import hashlib from typing import Any def need(ok: Any, message: str) -> None: if not ok: raise ValueError(message) def reloaded_variables_hash(model: Any) -> str: h = hashlib.sha256() scope = model.name + "/" for v in model.variables: path = str(getattr(v, "path", v.name)) need( path and not path.startswith(scope), "Reloaded model variable path must be nonempty and omit the model scope", ) a = v.numpy() h.update((scope + path).encode()) h.update(str((a.shape, str(a.dtype))).encode()) h.update(a.tobytes()) return h.hexdigest()