File size: 2,471 Bytes
ba305e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Build trimmed-vocabulary starting checkpoints: Deci (joint reader [+ option set mixer]) on an ettin reranker, then
the vocabulary trimmed to the --keep most frequent training tokens (lossless: byte tokens and merge parents kept).
  python make_init.py --sizes 17m,32m,68m --keep 8000 --out ckpt/init
  python make_init.py --sizes 150m --no-mixer --kept-from ckpt/init/68m-k8k --name 150m-k8k   # reuse a kept token set
  python make_init.py --mlm jhu-clsp/ettin-encoder-32m --kept-from ckpt/init/32m-k6k --name e32m-code-k6k   # code readout
Token counts come from training data only (trim_vocab.count_tokens), never the suite. --kept-from reuses an existing
checkpoint's tokens exactly (a recount drifts whenever the data files change)."""
import argparse, json
from pathlib import Path
from transformers import AutoTokenizer
from deci import Deci, n_params
from trim_vocab import trim, count_tokens

ap = argparse.ArgumentParser()
ap.add_argument("--sizes", default="17m,32m,68m"); ap.add_argument("--keep", type=int, default=8000)
ap.add_argument("--out", default="ckpt/init"); ap.add_argument("--rows", type=int, default=200000)
ap.add_argument("--no-mixer", action="store_true"); ap.add_argument("--kept-from", default="")
ap.add_argument("--mlm", default="", help="a raw ettin encoder repo: code-readout model (Deci.from_mlm) instead of a reranker")
ap.add_argument("--name", default="", help="output dir name (one size only)")
a = ap.parse_args()
kept = list(json.loads((Path(a.kept_from) / "tokenizer.json").read_text())["model"]["vocab"]) if a.kept_from else None
counts = None
for s in ([a.mlm] if a.mlm else a.sizes.split(",")):
    if a.mlm:
        model, tok = Deci.from_mlm(s)
        full = Path(a.out) / f"{a.name or 'mlm'}-full"
    else:
        bb = f"cross-encoder/ettin-reranker-{s}-v1"
        tok = AutoTokenizer.from_pretrained(bb)
        model = Deci.from_backbone(bb, rank=0, fmt="joint", mixer=not a.no_mixer)
        full = Path(a.out) / f"{s}-full"
    model.save(full, tok)
    if counts is None and kept is None:  # the ettin sizes share one tokenizer, so one count serves all
        counts = count_tokens(tok, "data", a.rows, "joint")
    dst = Path(a.out) / (a.name or f"{s}-k{a.keep // 1000}k")
    info = trim(full, dst, counts, a.keep, kept_tokens=kept)
    small, _ = Deci.load(dst)
    print(f"{s}: full {n_params(model) / 1e6:.2f}M -> trimmed {n_params(small) / 1e6:.2f}M ({info['vocab_after']} tokens) at {dst}", flush=True)