Delete vocab_to_bin.py
Browse files- vocab_to_bin.py +0 -53
vocab_to_bin.py
DELETED
|
@@ -1,53 +0,0 @@
|
|
| 1 |
-
"""vocab.txt -> vocab.bin (strict, minimal)
|
| 2 |
-
|
| 3 |
-
Format: u8 length + token bytes, one entry per line, id = entry order.
|
| 4 |
-
Refuses to run if the file looks wrong instead of silently corrupting.
|
| 5 |
-
"""
|
| 6 |
-
import sys
|
| 7 |
-
from pathlib import Path
|
| 8 |
-
|
| 9 |
-
HERE = Path(__file__).resolve().parent
|
| 10 |
-
SRC = HERE / "vocab.txt"
|
| 11 |
-
DST = HERE / "vocab.bin"
|
| 12 |
-
SPECIALS = ["<unk>", "<bos>", "<eos>", "<pad>", "<msk>", "<spc>", "<tab>", "<nwl>"]
|
| 13 |
-
EXPECTED_COUNT = 109 # 8 specials + 94 ascii + 7 smart punct
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
def main():
|
| 17 |
-
data = SRC.read_bytes()
|
| 18 |
-
if b"\r" in data:
|
| 19 |
-
sys.exit("refusing: CR present (CRLF line endings?) — fix the file first")
|
| 20 |
-
lines = data.split(b"\n")
|
| 21 |
-
if lines and lines[-1] == b"":
|
| 22 |
-
lines.pop()
|
| 23 |
-
if len(lines) != EXPECTED_COUNT:
|
| 24 |
-
sys.exit(f"refusing: {len(lines)} tokens, expected {EXPECTED_COUNT}")
|
| 25 |
-
for i, tok in enumerate(lines):
|
| 26 |
-
if not tok:
|
| 27 |
-
sys.exit(f"refusing: empty token at line {i + 1}")
|
| 28 |
-
if len(tok) > 255:
|
| 29 |
-
sys.exit(f"refusing: token {i + 1} longer than 255 bytes")
|
| 30 |
-
for i, want in enumerate(SPECIALS):
|
| 31 |
-
if lines[i] != want.encode():
|
| 32 |
-
sys.exit(f"refusing: specials block wrong at id {i}: {lines[i]!r} != {want!r}")
|
| 33 |
-
out = bytearray()
|
| 34 |
-
for tok in lines:
|
| 35 |
-
out.append(len(tok))
|
| 36 |
-
out.extend(tok)
|
| 37 |
-
DST.write_bytes(bytes(out))
|
| 38 |
-
print(f"wrote {DST.name}: {len(out)} bytes, {len(lines)} tokens "
|
| 39 |
-
f"({sum(len(t) for t in lines)} token bytes + {len(lines)} length bytes)")
|
| 40 |
-
|
| 41 |
-
# round-trip check
|
| 42 |
-
back = bytearray()
|
| 43 |
-
p = 0
|
| 44 |
-
while p < len(out):
|
| 45 |
-
n = out[p]
|
| 46 |
-
back.extend(out[p + 1:p + 1 + n])
|
| 47 |
-
p += 1 + n
|
| 48 |
-
assert back == b"\n".join(lines) + (b"\n" if lines else b""), "round-trip failed"
|
| 49 |
-
print("round-trip: OK")
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
if __name__ == "__main__":
|
| 53 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|