interplane.Get Omega
Read in your language

MojoLlama

Archived Mojo/MAX quantizer and serving research repository (read-only history)

Brought by drakestapleton

branch main Branches Tags History

MojoLlama / src / mojollama / mojo_engine / scan_zaya_vocab.py

68 lines · 1886 bytes Raw Blame History
1#!/usr/bin/env python3
2"""Scan ZAYA vocab for special tokens and test encoding."""
3import struct
4
5with open('/tmp/vocab_zaya.bin', 'rb') as f:
6 data = f.read()
7
8nv = struct.unpack('<I', data[0:4])[0]
9print(f'Vocab size: {nv}')
10
11# Check first 15 tokens
12print('\nFirst 15 tokens:')
13off = 4
14for i in range(min(15, nv)):
15 plen = struct.unpack('<I', data[off:off+4])[0]
16 off += 4
17 tok_data = data[off:off+plen]
18 off += plen
19 tid = struct.unpack('<I', data[off:off+4])[0]
20 off += 4
21 txt = tok_data.decode('utf-8', errors='replace')
22 print(f' {tid}: {repr(txt)}')
23
24# Search special tokens
25print('\nSpecial tokens:')
26keywords = ['<s>', '</s>', '<pad>', '<unk>', '<|im_end', '<|start', '<|user',
27 '<|assistant', '<|system', '<|begin', '[INST', '[/INST]']
28off = 4
29found = {}
30for i in range(nv):
31 plen = struct.unpack('<I', data[off:off+4])[0]
32 off += 4
33 tok_data = data[off:off+plen]
34 off += plen
35 tid = struct.unpack('<I', data[off:off+4])[0]
36 off += 4
37 txt = tok_data.decode('utf-8', errors='replace')
38 for kw in keywords:
39 if kw in txt:
40 if kw not in found:
41 found[kw] = []
42 found[kw].append((tid, txt))
43
44for kw, entries in sorted(found.items()):
45 for tid, txt in entries[:3]:
46 print(f' tok {tid}: {repr(txt)}')
47
48# Check last 5 tokens
49print('\nLast 5 tokens:')
50off = 4
51for i in range(nv - 5, nv):
52 # Quick seek by re-scanning
53 pass
54
55# Better: sequential scan from end
56offsets = []
57off = 4
58for i in range(nv):
59 plen = struct.unpack('<I', data[off:off+4])[0]
60 offsets.append((off, plen))
61 off += 4 + plen + 4
62
63for i in range(max(0, nv-5), nv):
64 off, plen = offsets[i]
65 tok_data = data[off+4:off+4+plen]
66 tid = struct.unpack('<I', data[off+4+plen:off+4+plen+4])[0]
67 txt = tok_data.decode('utf-8', errors='replace')
68 print(f' {tid}: {repr(txt)}')