"""R5 VLMA: download root-level safetensors weights and configs at a PINNED revision, then run the native weight-load gate (CPU) and the E3 tokenizer round trip. No token, no .py file (allow_patterns has no *.py; a check fails the model if one appears), trust_remote_code is never set. A model passes the load gate only if from_pretrained with the native class reports no missing and no mismatched key. usage: fetch_pinned.py MODELS_DIR OUT_DIR CODEC_JSON VAL_TSV spec [spec ...] spec = short=repo@revision (revision = 40-hex commit; 'HEAD' pins the current commit and records it) spec = short=repo@HEAD:info (record the pinned commit, config and file list only; no weights, no load) """ import json import os import struct import sys import time import unicodedata os.environ.setdefault("HF_HUB_DISABLE_TELEMETRY", "1") for k in ("HF_TOKEN", "HUGGING_FACE_HUB_TOKEN"): os.environ.pop(k, None) models_dir, out_dir, codec_json, val_tsv = sys.argv[1:5] specs = sys.argv[5:] os.makedirs(out_dir, exist_ok=True) import torch import transformers from huggingface_hub import HfApi, snapshot_download CFG = ["config.json", "generation_config.json", "preprocessor_config.json", "processor_config.json", "video_preprocessor_config.json", "tokenizer*", "special_tokens_map.json", "added_tokens.json", "vocab*", "merges.txt", "chat_template*", "README.md"] CARD_CLASS = {"lightonocr_3_0_8b": "Qwen3_5ForConditionalGeneration", "lightonocr_3_1b": "LightOnOcrForConditionalGeneration"} codec = json.load(open(codec_json, encoding="utf-8")) symbols = [chr(int(c[2:], 16)) for c in codec["codepoints"]] nonspace = [s for s in symbols if not s.isspace()] labels = [] with open(val_tsv, encoding="utf-8", newline="") as f: f.readline() for line in f: labels.append(unicodedata.normalize("NFC", line.rstrip("\n").split("\t")[2])) labels = sorted(set(labels)) def roundtrip(tok, texts): bad = [] for t in texts: ids = tok.encode(t, add_special_tokens=False) back = tok.decode(ids, skip_special_tokens=True, clean_up_tokenization_spaces=False) if back != t: bad.append((t, back)) return bad def header_params(path): with open(path, "rb") as f: n = struct.unpack("