vimeml-ja-v6 / source /scripts /zh /tokenizer /probe_zh_v13_bytes.py
Voltline's picture
vimeml-ja-v6-20m: FP32 inference bundle, Core ML INT8 stateless and batched KV v4 packages, kana reading index, evaluation summary, source snapshot ecdccd8, model card and NOTICE (part 2)
eeac31b verified
Raw History Blame Contribute Delete
1.41 kB
"""Post-hoc: byte-fallback rate on validation web excluding newline (<0x0A>), per probe tokenizer.
usage: python -I -X utf8 scripts/zh/tokenizer/probe_zh_v13_bytes.py ROOT PROBE_DIR
"""
import glob, json, sys
from pathlib import Path
import pyarrow.parquet as pq
import sentencepiece as spm
ROOT, PROBE = Path(sys.argv[1]), Path(sys.argv[2])
out = PROBE / 'bytes-excluding-newline.json'
if out.exists():
raise SystemExit(f'{out} exists')
texts = []
for p in sorted((ROOT / 'outputs/zh-corpus/export-r1/validation').glob('web-*.parquet')):
texts += pq.read_table(p, columns=['text'])['text'].to_pylist()
newlines = sum(t.count('\n') for t in texts)
res = {}
for model in sorted(PROBE.glob('zh-unigram-*/tokenizer.model')):
sp = spm.SentencePieceProcessor(model_file=str(model))
nl = sp.piece_to_id('<0x0A>')
ids = sp.encode(texts)
tokens = sum(map(len, ids))
byte = sum(1 for x in ids for i in x if sp.is_byte(i))
nlc = sum(x.count(nl) for x in ids)
res[model.parent.name] = {'tokens': tokens, 'byte_tokens': byte, 'newline_byte_tokens': nlc,
'byte_fraction_excl_newline': (byte - nlc) / (tokens - nlc),
'tokens_excl_newline': tokens - nlc}
print(model.parent.name, res[model.parent.name], flush=True)
out.write_text(json.dumps({'newlines_in_text': newlines, 'results': res}, indent=1), encoding='utf-8')