"""Prompt token pieces + their embedding rows (bf16, exact from the checkpoint) so the page needs no tokenizer and no 600 MB embedding table. Checked against the HF tokenizer for random head boxes. usage: make_data.py OUT_DIR""" import json import os, random, sys from pathlib import Path import numpy as np from safetensors import safe_open from transformers import AutoProcessor SP = os.environ.get("LOOK_DIR", ".") # holds src/ (the code repo), ckpt/LA-2B (the checkpoint), ref/ (reference outputs) sys.path.insert(0, f"{SP}/src"); sys.path.insert(0, f"{SP}/work") from look_anything.processing_attend_thing import build_chat_text import ref_look as R out = Path(sys.argv[1]); out.mkdir(parents=True, exist_ok=True) p = AutoProcessor.from_pretrained(f"{SP}/ckpt/LA-2B"); tok = p.tokenizer IMG = tok.convert_tokens_to_ids("<|image_pad|>") def ids_of(prompt, n_img=1): return tok(build_chat_text(p, prompt).replace("<|image_pad|>", "<|image_pad|>" * n_img))["input_ids"] def split(ids): a = ids.index(IMG); b = len(ids) - ids[::-1].index(IMG) return ids[:a], ids[b:] head, mid_point = split(ids_of(R.prompt_for("point"))) head_b, mid_box = split(ids_of(R.prompt_for("box"))) ref = ids_of(R.prompt_for("b2p:1,2,3,4")); head_c, post = split(ref) digit = {str(d): tok.convert_tokens_to_ids(str(d)) for d in range(10)}; comma = tok.convert_tokens_to_ids(",") k = post.index(digit["1"]) # the box starts right after ' [' mid_b2p, tail_b2p = post[:k], post[post.index(tok.convert_tokens_to_ids("]."))] if False else post[post.index(tok.convert_tokens_to_ids("].")):] assert head == head_b == head_c pieces = {"head": head, "point": mid_point, "box": mid_box, "b2p": {"mid": mid_b2p, "tail": tail_b2p}, "digits": [digit[str(d)] for d in range(10)], "comma": comma} def build(kind, box=None, n_img=7): if kind == "b2p": mid = pieces["b2p"]["mid"] for ch in ",".join(str(v) for v in box): mid = mid + [pieces["comma"] if ch == "," else pieces["digits"][int(ch)]] tail = pieces["b2p"]["tail"] return head + [IMG] * n_img + mid + tail return head + [IMG] * n_img + pieces[kind] assert build("point") == ids_of(R.prompt_for("point"), 7) and build("box") == ids_of(R.prompt_for("box"), 7) rng = random.Random(0) for _ in range(500): x1, y1 = rng.randint(0, 900), rng.randint(0, 900); box = [x1, y1, rng.randint(x1, 1000), rng.randint(y1, 1000)] assert build("b2p", box) == ids_of(R.prompt_for("b2p:" + ",".join(map(str, box))), 7), box print("pieces verified: point, box, 500 random head boxes") used = sorted(set(head + mid_point + mid_box + mid_b2p + tail_b2p + pieces["digits"] + [comma])) w = None with safe_open(f"{SP}/ckpt/LA-2B/backbone/model.safetensors", "pt") as f: key = next(k for k in f.keys() if k.endswith("embed_tokens.weight")); print("embedding key", key) emb = f.get_tensor(key) # bf16 [V, 2048] rows = emb[used].view(__import__("torch").int16).numpy().astype(""), "vision_end": tok.convert_tokens_to_ids("<|vision_end|>")}}, open(out / "ids.json", "w"))