okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-mac-rel (Mac Metal support) into f-144-rel6a74320ca

main
1.1 KiB23 linesraw
1#!/usr/bin/env python3
2"""chat_tokens.py REPO TOKENIZER_DIR "question" : the token ids of a one-turn chat prompt (thinking off), comma
3separated, for `strata generate --tokens` (the same template as the gates' rbench.py).
4With --decode IDS instead of the question: the text of comma-separated ids."""
5import json, sys
6from pathlib import Path
7
8repo, tdir = Path(sys.argv[1]), Path(sys.argv[2])
9sys.path.insert(0, str(repo / "tools"))
10import strata_tokenizer as ST
11
12vocab = json.loads((tdir / "vocab.json").read_text(encoding="utf-8"))
13toks = [None] * len(vocab)
14for t, i in vocab.items():
15    toks[i] = t
16tok = ST.Tokenizer(toks, (tdir / "merges.txt").read_text(encoding="utf-8").split("\n"),
17                   json.loads((tdir / "token_type.json").read_text()))
18if sys.argv[3] == "--decode":
19    print(tok.decode([int(x) for x in sys.argv[4].replace(" ", "").split(",") if x]))
20else:
21    ids = tok.encode("<|im_start|>user\n%s<|im_end|>\n<|im_start|>assistant\n<think>\n\n</think>\n\n" % sys.argv[3],
22                     parse_special=True)
23    print(",".join(str(i) for i in ids))