#!/usr/bin/env python3 """Verify the downloaded real weights and exercise the NOEON bridge on real bytes. For each downloaded source file this: 1. opens it with ``safetensors.safe_open`` (memory-mapped, no RAM blow-up); 2. compares every tensor's shape and dtype to the recorded header (weights/source_tensors.json); 3. counts real parameters; 4. pulls one real hidden-width tensor slice and runs it through the NOEON bridge, proving the bridge consumes genuine source data at 2560-d. Usage: python scripts/verify_real_weights.py --src /tmp/noeon_sources """ from __future__ import annotations import argparse import json import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) import torch ROOT = Path(__file__).resolve().parent.parent def main() -> int: ap = argparse.ArgumentParser() ap.add_argument("--src", default="/tmp/noeon_sources") args = ap.parse_args() src_root = Path(args.src) recorded = json.loads((ROOT / "weights" / "source_tensors.json").read_text()) try: from safetensors import safe_open except ImportError: print("safetensors not installed") return 2 from noeon.core.bridge import BridgeBank from noeon.core.config import NoeonConfig cfg = NoeonConfig() banks = BridgeBank(cfg.bridges) banks.eval() overall_ok = True for key, entry in recorded.items(): src_dir = src_root / key if not src_dir.exists(): print(f"[{key}] not downloaded yet, skipping") continue print(f"\n=== {key} ===") for rel, info in entry["files"].items(): p = src_dir / rel if not p.exists(): print(f" MISSING {rel}") overall_ok = False continue with safe_open(str(p), framework="pt", device="cpu") as f: keys = list(f.keys()) n_match = n_bad = 0 params = 0 for name in keys: sl = f.get_slice(name) shp = tuple(sl.get_shape()) dt = sl.get_dtype() rec = entry["tensors"].get(name) params += int(torch.tensor(shp).prod()) if shp else 0 if rec and tuple(rec["shape"]) == shp: n_match += 1 else: n_bad += 1 print(f" {rel}: {len(keys)} tensors, " f"{n_match} shapes match, {n_bad} mismatch, " f"{params/1e9:.3f} B params") if n_bad: overall_ok = False # run one REAL hidden tensor through the bridge try: existing = [rel for rel in entry["files"] if (src_dir / rel).exists()] if not existing: raise FileNotFoundError("no source shard present") first = existing[0] with safe_open(str(src_dir / first), framework="pt", device="cpu") as f: cand = [k for k in f.keys() if k.endswith("embed_tokens.weight")] width = 2560 if cand: sl = f.get_slice(cand[0]) # (V, 2560) sample = sl[0:4].float().unsqueeze(0) # (1, 4, 2560) real bytes else: # diffusion text conditioning: txt_in.weight is (out, 2560), # so a row slice is real 2560-d context data. cand = [k for k in f.keys() if k.endswith("txt_in.weight")] if not cand: raise LookupError("no 2560-d embed or context tensor") w = f.get_slice(cand[0])[:] if w.shape[-1] != width: raise LookupError(f"context width {w.shape[-1]} != {width}") sample = w[:4, :].float().unsqueeze(0) out = banks(key, sample) print(f" bridge({key}) on REAL bytes {tuple(sample.shape)} " f"-> {tuple(out.shape)} mean={out.mean().item():.4f}") except Exception as e: # noqa: BLE001 print(f" bridge-on-real-bytes skipped: {type(e).__name__}: {e}") print("\nOK" if overall_ok else "\nMISMATCHES FOUND") return 0 if overall_ok else 1 if __name__ == "__main__": raise SystemExit(main())