Download scripts/r5_loopguard/lg_a1_first_step.py from treoa/turkic-ocr-code: direct link, hf CLI and curl.
- Browser
- Download file 2.12 kB
-
https://huggingface.co/treoa/turkic-ocr-code/resolve/main/scripts/r5_loopguard/lg_a1_first_step.py
- Command line
-
hf download hf://treoa/turkic-ocr-code/scripts/r5_loopguard/lg_a1_first_step.py
-
curl -L -o lg_a1_first_step.py https://huggingface.co/treoa/turkic-ocr-code/resolve/main/scripts/r5_loopguard/lg_a1_first_step.py
2.12 kB
| """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)) | |