File size: 3,487 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 67 68 69 70 71 72 73 | """rc1 check helper (VLM): is the decoding code copied into the package the same as our evaluation code?
Run with CODE/.venv_vlm/bin/python -I. Imports the evaluation module CODE/scripts/r5_vlma/vlm_common.py and the package's infer.py (by file
path) in ONE process, only to compare them; the package itself is tested separately (rc1_check.py) without any CODE path.
For each package: template variables, stop-token set, the rendered prompt (token ids) and the processed image tensors for two dummy
crops, and the INSTR / MAXNEW constants must be equal. No model weights are loaded (a stub carries the generation config).
usage: rc1_ref_compare_vlm.py REPORT_JSON PKG_DIR [PKG_DIR ...]
"""
import importlib.util
import json
import os
import sys
import types
CODE_VLMA = "/path/to/TURKIC_OCR_CODE/scripts/r5_vlma"
sys.path.insert(0, CODE_VLMA)
os.environ.setdefault("HF_HUB_OFFLINE", "1")
import torch # noqa: E402
import transformers # noqa: E402
from PIL import Image # noqa: E402
import vlm_common as vc # noqa: E402
def load(name, path):
spec = importlib.util.spec_from_file_location(name, path)
mod = importlib.util.module_from_spec(spec)
sys.modules[name] = mod
spec.loader.exec_module(mod)
return mod
def main():
rep_path = sys.argv[1]
out = {"vlm_common": CODE_VLMA + "/vlm_common.py", "packages": {}}
for pkg in sys.argv[2:]:
mod = load("pkg_" + os.path.basename(pkg).replace(".", "_").replace("-", "_"), os.path.join(pkg, "infer.py"))
mdir = os.path.join(pkg, "model")
short = json.load(open(os.path.join(mdir, "r5_export.json")))["short"]
meta = json.load(open(os.path.join(mdir, "r5_export.json")))
proc, tok = vc.load_processor(short, mdir)
tok.padding_side = "left"
gcfg = transformers.GenerationConfig.from_pretrained(mdir)
stub = types.SimpleNamespace(generation_config=gcfg)
r = {"short": short}
r["constants_equal"] = (mod.INSTR == vc.INSTR and mod.MAXNEW == vc.MAXNEW)
r["template_kwargs_equal"] = (mod.template_kwargs(proc, tok) == vc.template_kwargs(proc, tok))
r["export_chat_template_kwargs"] = meta.get("chat_template_kwargs")
tk = meta.get("chat_template_kwargs", vc.template_kwargs(proc, tok))
a = vc.gen_setup(stub, tok, meta.get("answer_suffix_ids"))
b = mod.gen_setup(stub, tok, meta.get("answer_suffix_ids"))
r["gen_setup_equal"] = (a[1] == b[1] and a[2] == b[2] and a[0].to_dict() == b[0].to_dict())
r["stop_ids"] = a[1]
r["stop_set"] = sorted(a[2])
imgs = [Image.new("RGB", (256, 48), (255, 255, 255)), Image.new("RGB", (97, 31), (240, 230, 220))]
ea = vc.prompt_batch(short, proc, imgs, tk)
eb = mod.prompt_batch(proc, imgs, tk)
r["prompt_keys_equal"] = (sorted(ea.keys()) == sorted(eb.keys()))
r["prompt_tensors_equal"] = all(torch.equal(ea[k], eb[k]) for k in ea.keys() if torch.is_tensor(ea[k]))
r["rendered_prompt"] = vc.rendered_prompt(short, proc, tk)
out["packages"][pkg] = r
out["all_equal"] = all(v["constants_equal"] and v["template_kwargs_equal"] and v["gen_setup_equal"] and v["prompt_keys_equal"]
and v["prompt_tensors_equal"] for v in out["packages"].values())
with open(rep_path, "w", encoding="utf-8") as f:
json.dump(out, f, indent=1, ensure_ascii=False)
print(json.dumps({"all_equal": out["all_equal"]}))
if __name__ == "__main__":
main()
|