File size: 2,932 Bytes
24aaa76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
57
58
59
60
61
62
63
64
65
66
"""LOOPGUARD-1 arm B flags: the pages on which the pipeline's reading replaces the page model's (A0) reading.
Flag = the A2 rule fires on the page's A0 token stream, or the page hit the cap.
  val     : fires = pages where the A2 run fired at least once (before its first fire A2 equals A0 token for token, checked by
            lg_accept.py), cap = hit_cap of the A0 given by --a0 (the fresh control A0 of the GPU job, amendment AM1)
  heldout : fires and cap from the A0 run with the observe-only guard (exact ids); needs the unseal token (role 'score')
Writes JSON {set: [ids], '_detail': {...}} (ids and counts only).

usage: lg_flags.py val --a2 DIR --a0 DIR --out JSON [--sets gen_val,school_val]
       lg_flags.py heldout --a0obs DIR --out JSON [--sealed-token FILE]
"""
import argparse
import json
import os
import sys

HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, HERE)
import om_text as T  # noqa: E402

ap = argparse.ArgumentParser()
ap.add_argument("mode", choices=["val", "heldout"])
ap.add_argument("--a2", default="")
ap.add_argument("--a0", default="")
ap.add_argument("--a0obs", default="")
ap.add_argument("--sets", default="")
ap.add_argument("--sealed-token", default=T.OFFICIAL_UNSEAL)
ap.add_argument("--out", required=True)
a = ap.parse_args()


def read(p, sealed=False):
    out = {}
    f = T.open_sealed(p, a.sealed_token, "score") if sealed else open(p, encoding="utf-8")
    with f:
        for line in f:
            try:
                r = json.loads(line)
            except ValueError:
                continue
            out[r["id"]] = r
    return out


res, det = {}, {}
if a.mode == "val":
    for s in (a.sets or "gen_val,school_val").split(","):
        A2 = read(os.path.join(a.a2, s + ".jsonl"))
        A0 = read(os.path.join(a.a0, s + ".jsonl"))
        fires = sorted(i for i, r in A2.items() if r.get("guard_events"))
        cap = sorted(i for i, r in A0.items() if r.get("hit_cap"))
        res[s] = sorted(set(fires) | set(cap))
        det[s] = {"a2_pages": len(A2), "a0_pages": len(A0), "fired_pages": fires, "capped_pages_a0": cap,
                  "fired_not_capped": sorted(set(fires) - set(cap)), "capped_not_fired": sorted(set(cap) - set(fires))}
else:
    for s in (a.sets or "gen_test,school_test").split(","):
        assert s in T.HELDOUT_SETS
        A0 = read(os.path.join(a.a0obs, s + ".jsonl"), sealed=True)
        fires = sorted(i for i, r in A0.items() if r.get("guard_events"))
        cap = sorted(i for i, r in A0.items() if r.get("hit_cap"))
        res[s] = sorted(set(fires) | set(cap))
        det[s] = {"a0_pages": len(A0), "fired_pages": fires, "capped_pages": cap,
                  "fired_not_capped": sorted(set(fires) - set(cap)), "capped_not_fired": sorted(set(cap) - set(fires))}
res["_detail"] = det
json.dump(res, open(a.out, "w"), indent=1)
print(json.dumps({s: len(v) for s, v in res.items() if isinstance(v, list)}))