File size: 2,656 Bytes
76ec4b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Recompute the original cup endpoint from individual recorded predictions."""
import gzip
import json
import math
from collections import Counter
from itertools import combinations

from novel_tokens.runtime import RESULTS, save_json


def main():
    folder=RESULTS/"cups-symbols-17"
    metrics=json.loads((folder/"test-cups.json").read_bytes())
    with gzip.open(folder/metrics["raw"],"rt",encoding="utf-8") as file:
        rows=[json.loads(line) for line in file]
    index={r["key"]:r for r in rows}
    pairs=list(combinations(range(4),2))
    alphabet={v["id"] for v in json.loads((RESULTS/"vocabulary.json").read_bytes())["symbols"]}
    counts=Counter()
    gold_counts=Counter()
    own_counts=Counter()
    for r in rows:
        # Follow a single ball's position; the training verifier permutes the complete layout.
        location=r["start"].index(r["color"])
        for move in r["program"]:
            a,b=pairs[move]
            location=b if location==a else a if location==b else location
        gold="ABCD"[location]
        donor=index[r["donor_key"]]
        assert gold==r["gold"] and r["key"]!=donor["key"] and donor["donor_key"]==r["key"]
        assert r["intervention_ids"]["donor"]==donor["trace_ids"]
        assert r["intervention_ids"]["intact"]==r["trace_ids"] and r["intervention_ids"]["direct"]==[]
        assert len(r["trace_ids"])==8 and set(r["trace_ids"])<=alphabet
        assert sorted(r["reveal_order"])==list(range(8)) and len(r["answer_uniforms"])==1
        gold_counts[gold]+=1
        own_counts[r["answer"]]+=1
        for condition,answer in r["outputs"].items():
            counts[condition]+=answer==gold
    programs=len({tuple(r["program"]) for r in rows})
    assert programs==len(index)==len(rows)==metrics["n"]
    assert all(metrics["conditions"][key]==hits/len(rows) for key,hits in counts.items())
    assert metrics["delta"]==(counts["intact"]-counts["donor"])/len(rows)
    train=[x for x in json.loads((folder/"curves.json").read_bytes()) if "grad_norm" in x]
    assert all(math.isfinite(x["grad_norm"]) and math.isfinite(x["replay_error"]) for x in train)
    result={"programs":programs,"donor_pairs":len(rows)//2,"gold_counts":dict(gold_counts),
            "own_answer_counts":dict(own_counts),"correct_counts":dict(counts),
            "changed_answers":sum(r["answer"]!=r["donor_answer"] for r in rows),
            "nonzero_gradient_updates":sum(x["grad_norm"]>0 for x in train),
            "max_replay_error":max(x["replay_error"] for x in train)}
    save_json(RESULTS/"primary-audit.json",result)
    print(json.dumps(result,indent=2))


if __name__=="__main__":
    main()