MojoLlama
Archived Mojo/MAX quantizer and serving research repository (read-only history)
Brought by drakestapleton
MojoLlama / src / mojollama / mojo_engine / scan_zaya_vocab.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Scan ZAYA vocab for special tokens and test encoding.""" |
| 3 | import struct |
| 4 | |
| 5 | with open('/tmp/vocab_zaya.bin', 'rb') as f: |
| 6 | data = f.read() |
| 7 | |
| 8 | nv = struct.unpack('<I', data[0:4])[0] |
| 9 | print(f'Vocab size: {nv}') |
| 10 | |
| 11 | # Check first 15 tokens |
| 12 | print('\nFirst 15 tokens:') |
| 13 | off = 4 |
| 14 | for 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 |
| 25 | print('\nSpecial tokens:') |
| 26 | keywords = ['<s>', '</s>', '<pad>', '<unk>', '<|im_end', '<|start', '<|user', |
| 27 | '<|assistant', '<|system', '<|begin', '[INST', '[/INST]'] |
| 28 | off = 4 |
| 29 | found = {} |
| 30 | for 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 | |
| 44 | for 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 |
| 49 | print('\nLast 5 tokens:') |
| 50 | off = 4 |
| 51 | for i in range(nv - 5, nv): |
| 52 | # Quick seek by re-scanning |
| 53 | pass |
| 54 | |
| 55 | # Better: sequential scan from end |
| 56 | offsets = [] |
| 57 | off = 4 |
| 58 | for 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 | |
| 63 | for 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)}') |