b4ph's picture
CLAP audio-text retrieval: pinned-protocol measurement release (v15)
fb47aa2 verified
Raw History Blame Contribute Delete
3.7 kB
"""Gate G2 in the form that can actually be tested.
The original plan asked for numeric parity against an independently written
pipeline. Running it showed the two pipelines hold *different checkpoints*: the
laion_clap default checkpoint and HF `clap-htsat-fused` produce embeddings with
cosine 0.31 on identical audio, so their scores cannot agree by construction.
Comparing them would measure the checkpoints, not our code.
So G2 is split into what is testable:
G2a metric parity: our metric code vs an independent implementation on the
identical embeddings must agree exactly (`metric_parity.json`)
G2b checkpoint identity: evidence that the control is a *different* model, so
the cross-pipeline comparison is reported as not-comparable, not failed
(`checkpoint_identity_*.json`)
python gate2.py
"""
from __future__ import annotations
import json
from pathlib import Path
ROOT = Path(__file__).resolve().parent
TOL = 1e-9
def main() -> int:
fails: list[str] = []
mp = None
if not (ROOT / "metric_parity.json").exists():
print("G2a metric parity: NOT RUN")
fails.append("metric parity not run")
else:
mp = json.loads((ROOT / "metric_parity.json").read_text())
ok = bool(mp["passed"]) and mp["worst_abs_delta"] <= TOL
cells = mp.get("cells", [])
covered = mp.get("cells_with_multi_positive_ap", 0)
print(f"G2a metric parity: {'PASS' if ok else 'FAIL'} "
f"worst |delta| {mp['worst_abs_delta']:.2e} over {mp.get('n_cells', len(cells))}"
f" cells, {covered} with multi-positive AP")
for c in cells:
print(f" {c['source'][:46]:<48} clips={c['n_clips']:<5} "
f"caps/clip={c['captions_per_clip']:<2} worst={c['worst_abs_delta']:.1e}")
if not covered:
print(" WARNING: no cell exercises multi-positive AP")
if not ok:
fails.append(f"metric parity delta {mp['worst_abs_delta']:.2e}")
ident_files = sorted(ROOT.glob("checkpoint_identity_*.json"))
ci = None
if not ident_files:
print("G2b checkpoint identity: NOT RUN")
fails.append("checkpoint identity evidence missing")
else:
ci = json.loads(ident_files[0].read_text())
a = ci["audio_embedding_cosine_per_clip"]
t = ci["text_embedding_cosine_per_clip"]
mean_a, mean_t = sum(a) / len(a), sum(t) / len(t)
comparable = mean_a > 0.99
print(f"G2b checkpoint identity: {'IDENTICAL' if comparable else 'DIFFERENT'} "
f"audio cosine {mean_a:.3f}, text cosine {mean_t:.3f}, "
f"similarity corr {ci['similarity_matrix_correlation']:.3f}")
print(" cross-pipeline numeric parity: "
+ ("attainable" if comparable else "not attainable, not claimed"))
if comparable:
fails.append("control is the same model; the cross-check proves nothing")
out = {
"metric_parity": mp,
"checkpoint_identity": ci,
"metric_parity_passed": bool(mp and mp["passed"]),
"cross_pipeline_comparable": bool(ci and (sum(ci["audio_embedding_cosine_per_clip"])
/ len(ci["audio_embedding_cosine_per_clip"])) > 0.99),
"failures": fails,
}
(ROOT / "gate2.json").write_text(json.dumps(out, indent=2), encoding="utf-8",
newline="\n")
if fails:
print(f"\nG2 FAILED: {fails}")
return 1
print("\nG2 PASSED (metric parity exact; cross-pipeline comparison not applicable)")
return 0
if __name__ == "__main__":
raise SystemExit(main())