File size: 2,117 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 | """LOOPGUARD-1 smoke: arm A1's NoRepeatNGramLogitsProcessor(20) sees the whole input row, prompt included (about 2,000
identical image placeholders and left padding). On the real page prompts of the smoke pages, batched per set as
om_eval_pages batches them, count the tokens it would ban at the first generated token (expected 0). No model is loaded.
usage: lg_a1_first_step.py --ids a,b,c --out JSON
"""
import argparse
import json
import os
import sys
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, HERE)
import om_common as C # noqa: E402
import om_text as T # noqa: E402
import torch # noqa: E402
from PIL import Image # noqa: E402
from transformers import NoRepeatNGramLogitsProcessor # noqa: E402
ap = argparse.ArgumentParser()
ap.add_argument("--ids", required=True)
ap.add_argument("--model-dir", default=C.DATA + "/checkpoints/r6_onemodel/seed1/final")
ap.add_argument("--out", required=True)
a = ap.parse_args()
want = set(a.ids.split(","))
proc, tok = C.vc.load_processor(C.SHORT, a.model_dir)
tok.padding_side = "left"
exp = json.load(open(os.path.join(a.model_dir, "r5_export.json"))) if os.path.exists(os.path.join(a.model_dir, "r5_export.json")) else {}
tmpl_kw = exp.get("chat_template_kwargs", C.vc.template_kwargs(proc, tok))
res = {"rows": [], "banned_per_row": [], "prompt_len": {}}
for s in ("gen_val", "school_val"):
p = C.DATA + "/data/processed/r6_onemodel/records/%s.jsonl" % s
T.guard_not_test(p)
recs = [json.loads(x) for x in open(p, encoding="utf-8")]
recs = [r for r in recs if r["id"] in want]
if not recs:
continue
imgs = [Image.open(r["image"]).convert("RGB") for r in recs]
enc = C.page_prompt_batch(proc, imgs, tmpl_kw)
ids = enc["input_ids"]
vocab = max(len(tok), 101298)
out = NoRepeatNGramLogitsProcessor(20)(ids, torch.zeros((ids.shape[0], vocab)))
for k, r in enumerate(recs):
res["rows"].append(r["id"])
res["banned_per_row"].append(int(torch.isinf(out[k]).sum()))
res["prompt_len"][s] = int(ids.shape[1])
json.dump(res, open(a.out, "w"), indent=1)
print(json.dumps(res))
|