| 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()) |
|
|