Download embed_infer.py from Quenlla/columnist: direct link, hf CLI and curl.
- Browser
- Download file 1.82 kB
-
https://huggingface.co/Quenlla/columnist/resolve/main/embed_infer.py
- Command line
-
hf download hf://Quenlla/columnist/embed_infer.py
-
curl -L -o embed_infer.py https://huggingface.co/Quenlla/columnist/resolve/main/embed_infer.py
1.82 kB
| """Chart embedding with a Columnist pretraining checkpoint (needs columnist.py next to this file). | |
| from embed_infer import load_encoder, embed_chart | |
| enc = load_encoder("c_big.pt") | |
| vec = embed_chart(enc, tok, gap, sv) # (d,) numpy vector; tok/gap/sv are the arrays of one 4K chart (see README.md) | |
| """ | |
| import numpy as np | |
| import torch | |
| import columnist as C | |
| def load_encoder(path, device=None): | |
| dev = torch.device(device or ("cuda" if torch.cuda.is_available() else "cpu")) | |
| ck = torch.load(path, map_location="cpu", weights_only=False) | |
| a = ck["args"] | |
| m = C.Columnist(a["d"], a["layers"], look=bool(a.get("look", False)), v3=bool(a.get("v3_inputs", False))) | |
| m.load_state_dict(ck["model"], strict=False) # the pretraining checkpoints also hold optional heads | |
| return m.to(dev).eval() | |
| def embed_chart(enc, tok, gap, sv, L=512): | |
| """Mean over the valid rows of the encoder output of each non-overlapping L-row window, averaged over windows weighted by their length.""" | |
| dev = next(enc.parameters()).device | |
| tok = np.asarray(tok).astype(np.int64); gap = np.clip(np.nan_to_num(np.asarray(gap, dtype=np.float32)), 0, 60000); sv = np.asarray(sv, dtype=np.float32) | |
| n = len(tok); starts = list(range(0, max(1, n), L)); W = len(starts) | |
| T = np.zeros((W, L), np.int64); G = np.zeros((W, L), np.float32); S = np.ones((W, L), np.float32); V = np.zeros((W, L), bool) | |
| for k, s in enumerate(starts): | |
| m = min(L, n - s); T[k, :m], G[k, :m], S[k, :m], V[k, :m] = tok[s:s + m], gap[s:s + m], sv[s:s + m], True | |
| T, G, S, V = (torch.from_numpy(x).to(dev) for x in (T, G, S, V)) | |
| e = C.Columnist.pool(enc(T, G, S, V).float(), V) # (W, d) | |
| w = V.sum(1).float() | |
| return ((e * w[:, None]).sum(0) / w.sum()).cpu().numpy() | |