File size: 5,843 Bytes
02443ff | 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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | from __future__ import annotations
import argparse
import hashlib
import json
from pathlib import Path
import time
import torch
from safetensors.torch import load_file
def read_json(path: Path) -> dict:
return json.loads(path.read_text(encoding="utf-8"))
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument("--run-root", type=Path, required=True)
parser.add_argument("--selection", choices=("pilot", "train"), default="pilot")
args = parser.parse_args()
started = time.perf_counter()
manifest_path = args.run_root / "manifest.jsonl"
output_dir = args.run_root / f"anima_{args.selection}_features"
expected_records = 4_096 if args.selection == "pilot" else 104_000
expected_kind = f"factor_intervention_anima_{args.selection}"
manifest_sha256 = hashlib.sha256(manifest_path.read_bytes()).hexdigest()
with manifest_path.open(encoding="utf-8") as handle:
manifest = {
row["record_id"]: row
for line in handle
if line.strip()
for row in [json.loads(line)]
if (
row.get("anima_pilot")
if args.selection == "pilot"
else row.get("split") == "train"
)
}
errors: list[str] = []
summaries = []
for worker in range(4):
try:
summary = read_json(output_dir / f"anima-worker-{worker}.json")
except (FileNotFoundError, OSError, json.JSONDecodeError) as error:
errors.append(f"worker {worker} summary: {error}")
continue
summaries.append(summary)
if summary.get("status") != "complete":
errors.append(f"worker {worker} is incomplete")
if summary.get("manifest_sha256") != manifest_sha256:
errors.append(f"worker {worker} manifest hash mismatch")
if sum(int(row.get("records", 0)) for row in summaries) != expected_records:
errors.append("worker row total mismatch")
json_parts = sorted(output_dir.glob("anima-w*-p*.json"))
tensor_parts = sorted(output_dir.glob("anima-w*-p*.safetensors"))
if {path.stem for path in json_parts} != {
path.name.removesuffix(".safetensors") for path in tensor_parts
}:
errors.append("part JSON/safetensors pairing mismatch")
seen: set[str] = set()
tensor_bytes = 0
for metadata_path in json_parts:
metadata = read_json(metadata_path)
records = metadata.get("records", [])
expected_contract = {
"kind": expected_kind,
"blocks": [8, 18, 26],
"sigma": 0.1,
"noise_seed": 20260715,
"preprocess_version": "square-cover-v1",
"transform_resolution": 768,
"manifest_sha256": manifest_sha256,
}
for key, expected in expected_contract.items():
if metadata.get(key) != expected:
errors.append(f"{metadata_path.name}: {key} contract mismatch")
tensor_path = metadata_path.with_suffix(".safetensors")
tensors = load_file(tensor_path, device="cpu")
tensor_bytes += tensor_path.stat().st_size
features = tensors.get("features")
if set(tensors) != {"features"} or features is None:
errors.append(f"{tensor_path.name}: feature key mismatch")
continue
if features.shape != (len(records), 3, 4096) or features.dtype != torch.bfloat16:
errors.append(f"{tensor_path.name}: tensor contract mismatch")
elif not torch.isfinite(features).all().item():
errors.append(f"{tensor_path.name}: non-finite features")
for row in records:
record_id = str(row.get("record_id"))
if record_id in seen:
errors.append(f"duplicate record ID: {record_id}")
seen.add(record_id)
expected = manifest.get(record_id)
if expected is None:
errors.append(f"record absent from pilot manifest: {record_id}")
continue
for field in (
"source_record_id",
"style_id",
"source",
"split",
"shard",
"factor",
"family",
"level",
"sign",
"signed_intensity",
"operation_seed",
"transform_version",
):
if row.get(field) != expected.get(field):
errors.append(f"{record_id}: {field} alignment mismatch")
break
if row.get("source_shard") != row.get("shard"):
errors.append(f"{record_id}: source shard mismatch")
missing = set(manifest) - seen
extra = seen - set(manifest)
if missing or extra:
errors.append(f"record coverage mismatch: missing={len(missing)}, extra={len(extra)}")
report = {
"status": "pass" if not errors else "fail",
"selection": args.selection,
"manifest_sha256": manifest_sha256,
"records": len(seen),
"parts": len(json_parts),
"tensor_shape": [3, 4096],
"tensor_dtype": "bfloat16",
"tensor_bytes": tensor_bytes,
"blocks": [8, 18, 26],
"sigma": 0.1,
"noise_seed": 20260715,
"transform_resolution": 768,
"worker_summaries": summaries,
"elapsed_seconds": time.perf_counter() - started,
"errors": errors[:100],
}
output = args.run_root / f"anima_{args.selection}_verification.json"
temporary = output.with_suffix(".json.tmp")
temporary.write_text(json.dumps(report, indent=2) + "\n", encoding="utf-8")
temporary.replace(output)
print(json.dumps(report, indent=2))
return 0 if not errors else 1
if __name__ == "__main__":
raise SystemExit(main())
|