| |
| """Compare fertility of our Polish BPE vs Bielik / Llama-3 / GPT-2 on the same |
| held-out Polish sample.""" |
| import pyarrow.parquet as pq |
| from tokenizers import Tokenizer |
| import tiktoken |
|
|
| DATA = "/home/ubuntu/dynaword/data" |
| sample = [] |
| for b in pq.ParquetFile(f"{DATA}/wikipedia/wikipedia.parquet").iter_batches(columns=["text"], batch_size=1000): |
| for x in b.column("text"): |
| sample.append(x.as_py()) |
| if len(sample) >= 6000: |
| break |
| sample = sample[4000:6000] |
| words = sum(len(s.split()) for s in sample) |
| chars = sum(len(s) for s in sample) |
| print(f"held-out: {len(sample)} docs, {words:,} words, {chars:,} chars\n") |
|
|
| rows = [] |
|
|
| def add(name, vocab, ntok): |
| rows.append((name, vocab, ntok / words, ntok / chars)) |
|
|
| ours = Tokenizer.from_file("/home/ubuntu/dynaword/polish_bpe_32k.json") |
| add("polish-32k (ours)", ours.get_vocab_size(), |
| sum(len(e.ids) for e in ours.encode_batch(sample))) |
|
|
| g2 = tiktoken.get_encoding("gpt2") |
| add("gpt2-50k", 50257, sum(len(x) for x in g2.encode_ordinary_batch(sample))) |
|
|
| from transformers import AutoTokenizer |
| for label, repo in [("Bielik-11B-v3", "speakleash/Bielik-11B-v3.0-Instruct"), |
| ("Llama-3", "NousResearch/Meta-Llama-3-8B")]: |
| try: |
| t = AutoTokenizer.from_pretrained(repo) |
| enc = t(sample, add_special_tokens=False)["input_ids"] |
| add(label, t.vocab_size, sum(len(x) for x in enc)) |
| except Exception as e: |
| print(f" {label}: FAILED {str(e)[:140]}") |
|
|
| rows.sort(key=lambda r: r[2]) |
| base = next(r[2] for r in rows if r[0].startswith("polish-32k")) |
| print(f"{'tokenizer':<22}{'vocab':>8}{'tok/word':>11}{'tok/char':>11}{'vs ours':>10}") |
| for name, vocab, tpw, tpc in rows: |
| print(f"{name:<22}{vocab:>8}{tpw:>11.3f}{tpc:>11.3f}{tpw/base:>9.2f}x") |
|
|