Download validate.py from CounterSteer/sycophancy-artifacts: direct link, hf CLI and curl.
- Browser
- Download file 3.18 kB
-
https://huggingface.co/datasets/CounterSteer/sycophancy-artifacts/resolve/main/validate.py
- Command line
-
hf download hf://datasets/CounterSteer/sycophancy-artifacts/validate.py
-
curl -L -o validate.py https://huggingface.co/datasets/CounterSteer/sycophancy-artifacts/resolve/main/validate.py
3.18 kB
| #!/usr/bin/env python3 | |
| """Validate checksums and tensor invariants for this artifact bundle.""" | |
| from __future__ import annotations | |
| import hashlib | |
| import json | |
| import math | |
| from pathlib import Path | |
| ROOT = Path(__file__).resolve().parent | |
| def sha256(path: Path) -> str: | |
| digest = hashlib.sha256() | |
| with path.open("rb") as handle: | |
| for chunk in iter(lambda: handle.read(1024 * 1024), b""): | |
| digest.update(chunk) | |
| return digest.hexdigest() | |
| def verify_manifest() -> None: | |
| manifest = json.loads((ROOT / "manifest.json").read_text()) | |
| for relative, expected in manifest["files"].items(): | |
| path = ROOT / relative | |
| if not path.is_file(): | |
| raise AssertionError(f"missing file: {relative}") | |
| if path.stat().st_size != expected["bytes"]: | |
| raise AssertionError(f"size mismatch: {relative}") | |
| if sha256(path) != expected["sha256"]: | |
| raise AssertionError(f"checksum mismatch: {relative}") | |
| print(f"Checksums OK: {len(manifest['files'])} files") | |
| def verify_tensors() -> None: | |
| try: | |
| import torch | |
| except ImportError: | |
| print("PyTorch not installed; skipped tensor-content validation") | |
| return | |
| checked = 0 | |
| for metadata_path in sorted(ROOT.glob("*/*/metadata.json")): | |
| metadata = json.loads(metadata_path.read_text()) | |
| path = metadata_path.parent / metadata["vector"]["file"] | |
| try: | |
| bundle = torch.load(path, map_location="cpu", weights_only=True) | |
| except TypeError: | |
| bundle = torch.load(path, map_location="cpu") | |
| if set(bundle) != {"raw", "unit", "norm"}: | |
| raise AssertionError(f"unexpected keys in {path}") | |
| layers = metadata["vector"]["layers"] | |
| hidden = metadata["model"]["hidden_size"] | |
| for family in ("raw", "unit", "norm"): | |
| if sorted(bundle[family]) != sorted(layers): | |
| raise AssertionError(f"layer mismatch in {path}:{family}") | |
| for layer in layers: | |
| raw = bundle["raw"][layer].float() | |
| unit = bundle["unit"][layer].float() | |
| if tuple(raw.shape) != (hidden,) or tuple(unit.shape) != (hidden,): | |
| raise AssertionError(f"shape mismatch in {path}, layer {layer}") | |
| if not math.isclose(float(raw.norm()), float(bundle["norm"][layer]), | |
| rel_tol=1e-5, abs_tol=1e-5): | |
| raise AssertionError(f"raw norm mismatch in {path}, layer {layer}") | |
| if not math.isclose(float(unit.norm()), 1.0, rel_tol=1e-5, abs_tol=1e-5): | |
| raise AssertionError(f"unit norm mismatch in {path}, layer {layer}") | |
| if not torch.allclose(unit, raw / raw.norm(), rtol=1e-5, atol=1e-6): | |
| raise AssertionError(f"unit vector mismatch in {path}, layer {layer}") | |
| selected = metadata.get("selection") or {} | |
| if "layer" in selected and selected["layer"] not in layers: | |
| raise AssertionError(f"selected layer {selected['layer']} not in {path}") | |
| checked += 1 | |
| print(f"Tensor contents OK: {checked} bundles") | |
| if __name__ == "__main__": | |
| verify_manifest() | |
| verify_tensors() | |