okami.mom/strata

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

Niko1221Merge f-own-5b 9b242bb5b into f-own-int (round 15: the own text encoder and tokenizer as test programs, the DiT reference tool and position tables; committed work only)11c8c701a

main
1.9 KiB42 linesraw
1"""tools/image/gate/te_tok_tables.py - TEST-ONLY generator: src/image/te/qwen2_tok_unicode.inc from te_tok_probe's
2"classes" dump (Oniguruma's \\p{L}, \\p{N}, \\s and the case folds of s t r e v m l d, as stable-diffusion.cpp's
3Qwen2 pre-tokenizer sees them).
4
5    python -I -B te_tok_tables.py <classes.txt> <out .inc>"""
6import sys
7from pathlib import Path
8
9NAMES = {"L": "kUniL", "N": "kUniN", "S": "kUniS", "fs": "kFold_s", "ft": "kFold_t", "fr": "kFold_r", "fe": "kFold_e",
10         "fv": "kFold_v", "fm": "kFold_m", "fl": "kFold_l", "fd": "kFold_d"}
11
12
13def main() -> int:
14    src, out = Path(sys.argv[1]), Path(sys.argv[2])
15    ranges = {k: [] for k in NAMES}
16    for line in src.read_text().splitlines():
17        k, a, b = line.split()
18        ranges[k].append((int(a), int(b)))
19    lines = ["// src/image/te/qwen2_tok_unicode.inc - GENERATED by tools/image/gate/te_tok_tables.py from te_tok_probe's dump of",
20             "// stable-diffusion.cpp 2988060's Oniguruma (UTF-8): code point ranges [first, last] of \\p{L}, \\p{N}, \\s and of the",
21             "// case-insensitive matches of the letters in Qwen2's contractions.  Do not edit; regenerate from the probe."]
22    for k, name in NAMES.items():
23        r = sorted(ranges[k])
24        assert r, f"class {k} is empty"
25        assert all(r[i][1] < r[i + 1][0] for i in range(len(r) - 1)), f"class {k} not disjoint"
26        lines.append(f"static const uint32_t {name}[][2] = {{  // {len(r)} ranges, {sum(b - a + 1 for a, b in r)} code points")
27        row = []
28        for a, b in r:
29            row.append(f"{{{a:#x}, {b:#x}}}")
30            if len(row) == 6:
31                lines.append("    " + ", ".join(row) + ",")
32                row = []
33        if row:
34            lines.append("    " + ", ".join(row) + ",")
35        lines.append("};")
36    out.write_text("\n".join(lines) + "\n")
37    print(f"{out}: " + ", ".join(f"{k} {len(v)}" for k, v in ranges.items()))
38    return 0
39
40
41if __name__ == "__main__":
42    sys.exit(main())