cmvr-visual/inference/scripts/audit_canonical.py

70 lines
2.6 KiB
Python

"""Overlay verified small canonical files, then audit completed checkpoints offline."""
import json
import threading
import time
from pathlib import Path
import download_models as downloader
ROOT = Path("/workspace/projects/llm-services")
lock = threading.Lock()
states = {}
def update(name, **values):
with lock:
states.setdefault(name, {}).update(values, updated_at=time.time())
temp = ROOT / "state/canonical-audit.tmp"
temp.write_text(json.dumps(states, indent=2))
temp.replace(ROOT / "state/canonical-audit.json")
def main():
specs = downloader.SPECS
for name, repo, revision, variant in specs:
info = json.loads((ROOT / "config/metadata" / (name + ".json")).read_text())
if info["sha"] != revision:
raise RuntimeError("Unexpected metadata revision")
selected = downloader.select_files(info["siblings"], variant)
target = Path("/workspace/models") / name
source = ROOT / "config/canonical-configs" / name
copied = 0
for item in selected:
if item.get("size", 0) > 64 * 1024 * 1024:
continue
src = source / item["rfilename"]
dest = (target / item["rfilename"]).resolve()
if not dest.is_relative_to(target.resolve()):
raise RuntimeError("Unconfined checkpoint path")
if not downloader.matches_checkpoint_file(src, item):
raise RuntimeError("Canonical file validation failed: " + str(src))
if not downloader.matches_checkpoint_file(dest, item):
dest.parent.mkdir(parents=True, exist_ok=True)
temporary = dest.with_name(dest.name + ".canonical-tmp")
temporary.write_bytes(src.read_bytes())
temporary.replace(dest)
copied += 1
update(name, state="canonical_configs_ready", copied_files=copied)
pending = {spec[0]: spec for spec in specs}
downloader.update = update
while pending:
for name, spec in list(pending.items()):
target = Path("/workspace/models") / name
if not (target / ".deployment-manifest.json").is_file():
continue
if any(target.rglob("*.aria2")):
continue
try:
downloader.download(spec)
update(name, state="verified")
del pending[name]
except Exception as error:
update(name, state="failed", error=str(error))
raise
if pending:
time.sleep(10)
if __name__ == "__main__":
main()