interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / train / omega-llm / native / tok / tkcore.om

387 lines · 8035 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note omegallm tok/tkcore.om: core of the own byte-level BPE tokenizer, format OLLMTOK1 (spec:
3note tests/omegallm/tok/FORMAT.md). No host words and about 1.7 MiB of globals, so a model unit
4note can concatenate it beside bpe.om: the caller fills tkw (text) or tkmf (merges file) and calls
5note tkinit, tkload, tknext, tkencc and tkdec1. Every name starts with tk.
6note Token ids: 0 .. 255 bytes; 256 .. 271 special ids (256 .. 263 the chat markers below, 264 ..
7note 271 reserved and never valid); 272 .. V - 1 the merges in rank order. 272 <= V <= 32768.
8note Exits: 26 a bad merges file (any fault below).
9const TKVMAX ulong 32768
10const TKFIRST ulong 272
11const TKNSP ulong 8
12const TKWCAP ulong 1048576
13const TKMFCAP ulong 130016
14text tkmagic "OLLMTOK1"
15text tkmarks "<|end|><|system|><|user|><|assistant|><|tool_call|><|tool_result|><|pad|><|doc|>"
16global tkw uchar 1048576
17global tkmf uchar 131072
18global tkma ushort 32768
19global tkmb ushort 32768
20global tkln uchar 32768
21global tkrk uint 65536
22global tkrt ushort 65536
23global tkv ulong 2
24global tkcl uchar 256
25global tkmo uchar 16
26global tkml uchar 16
27global tkct ushort 128
28global tkdb uchar 64
29global tksk ushort 128
30
31note tkv: 0 V of the loaded merges (0 before tkload), 1 special id of the last tknext piece (0:
32note a text chunk).
33
34note byte classes (FORMAT.md section 3): 1 A (digits, ASCII letters, bytes >= 128), 2 W (9 .. 13
35note and 32), 0 P (every other byte); and the offset and length of each chat marker in tkmarks.
36fn tkinit to void
37for c ulong 256
38v = uchar 0
39if or or and ge c 48 le c 57 and ge c 65 le c 90 or and ge c 97 le c 122 ge c 128
40v = uchar 1
41end
42if or and ge c 9 le c 13 eq c 32
43v = uchar 2
44end
45put tkcl c v
46end
47o = ulong 0
48k = ulong 0
49for j ulong len tkmarks
50if eq get tkmarks j 62
51put tkmo k uchar o
52put tkml k uchar sub add j 1 o
53o = add j 1
54k = add k 1
55end
56end
57put tkv 0 0
58end
59
60note 256 + k when chat marker k starts at tkw[i] and ends by n; 0 when none does
61fn tkmark i ulong n ulong to ulong
62if ne get tkw i 60
63ret 0
64end
65for k ulong TKNSP
66l = ulong get tkml k
67if le add i l n
68o = ulong get tkmo k
69hit = true
70for j ulong l
71if ne get tkw add i j get tkmarks add o j
72hit = false
73end
74end
75if hit
76ret add 256 k
77end
78end
79end
80ret 0
81end
82
83note length (1 .. 64) of the chunk at tkw[i] in a text ending at n > i (FORMAT.md section 3).
84note Reads at most tkw[i .. i + 65).
85fn tkclen i ulong n ulong to ulong
86c = ulong get tkw i
87k = ulong get tkcl c
88e = add i 64
89if lt n e
90e = n
91end
92j = add i 1
93sp = false
94if eq c 32
95if lt j n
96if ne get tkcl get tkw j 2
97sp = true
98end
99end
100end
101if sp
102k = ulong get tkcl get tkw j
103j = add j 1
104end
105while lt j e bound 64
106if ne ulong get tkcl get tkw j k
107break
108end
109j = add j 1
110end
111if and eq k 2 lt j n
112if ne get tkcl get tkw j 2
113if and ge sub j i 2 eq get tkw sub j 1 32
114j = sub j 1
115end
116end
117end
118ret sub j i
119end
120
121note the next piece at tkw[i] of a text ending at n > i: in chat mode a marker (its length; tkv 1
122note = its special id), else a chunk (tkv 1 = 0) that ends before the next marker. Reads at most
123note tkw[i .. i + 80): a caller streaming a file keeps 80 bytes ahead of i until the file ends.
124fn tknext i ulong n ulong chat bool to ulong
125put tkv 1 0
126e = add i 65
127if lt n e
128e = n
129end
130if chat
131m = tkmark i n
132if ne m 0
133put tkv 1 m
134ret ulong get tkml sub m 256
135end
136j = add i 1
137while lt j e bound 65
138if eq get tkw j 60
139if ne tkmark j n 0
140e = j
141break
142end
143end
144j = add j 1
145end
146end
147ret tkclen i e
148end
149
150note u16 and u64 little-endian at tkmf[p]
151fn tkrd16 p ulong to ulong
152ret or ulong get tkmf p shl ulong get tkmf add p 1 8
153end
154fn tkrd64 p ulong to ulong
155v = ulong 0
156for k ulong 8
157v = or v shl ulong get tkmf add p k mul k 8
158end
159ret v
160end
161fn tkwr64 p ulong v ulong to void
162for k ulong 8
163put tkmf add p k uchar and shr v mul k 8 255
164end
165end
166
167note FNV-1a 64 of a merges file of n bytes: bytes 0 .. 23, then 32 .. n - 1 (all but the field)
168fn tkfnv n ulong to ulong
169h = ulong 14695981039346656037
170for k ulong 24
171h = mul xor h ulong get tkmf k 1099511628211
172end
173for k ulong sub n 32
174h = mul xor h ulong get tkmf add 32 k 1099511628211
175end
176ret h
177end
178
179note header of a merges file of V = v whose pairs are already at tkmf[32 ..); returns its length
180fn tkseal v ulong to ulong
181for k ulong 8
182put tkmf k get tkmagic k
183end
184tkwr64 8 v
185tkwr64 16 16
186n = add 32 mul 4 sub v TKFIRST
187tkwr64 24 tkfnv n
188ret n
189end
190
191note may pair member a appear in merge token t? (a byte, or an earlier merge; never a special)
192fn tkpok a ulong t ulong to bool
193ret or lt a 256 and ge a TKFIRST lt a t
194end
195
196note rank table: slot of key a 32768 + b
197fn tkslot key ulong to ulong
198ret shr mul add key 1 ulong 11400714819323198485 48
199end
200
201note put pair key -> merge token t in the rank table; false when the key is there already
202fn tkrins key ulong t ulong to bool
203s = tkslot key
204for q ulong 65536
205k = ulong get tkrk s
206if eq k 0
207put tkrk s uint add key 1
208put tkrt s ushort t
209ret true
210end
211if eq k add key 1
212ret false
213end
214s = and add s 1 65535
215end
216ret false
217end
218
219note merge token of pair (a, b), or 65535 when (a, b) is not a merge
220fn tkrank a ulong b ulong to ulong
221key = or shl a 15 b
222s = tkslot key
223for q ulong 65536
224k = ulong get tkrk s
225if eq k 0
226ret 65535
227end
228if eq k add key 1
229ret ulong get tkrt s
230end
231s = and add s 1 65535
232end
233ret 65535
234end
235
236note check merge k of a file being loaded (tkmf[32 + 4 k ..)) and use it; exit 26 on a fault
237fn tkld1 k ulong to void
238p = add 32 mul 4 k
239a = tkrd16 p
240b = tkrd16 add p 2
241t = add TKFIRST k
242if not and tkpok a t tkpok b t
243exit 26
244end
245l = add ulong get tkln a ulong get tkln b
246if gt l 64
247exit 26
248end
249if not tkrins or shl a 15 b t
250exit 26
251end
252put tkma t ushort a
253put tkmb t ushort b
254put tkln t uchar l
255end
256
257note check tkmf[0 .. n) as an OLLMTOK1 merges file (FORMAT.md section 2) and use it; returns its
258note V. Exit 26 on any fault: length, magic, V, special count, length for V, checksum, a pair
259note member that is a special or not below its own token, a token longer than 64 bytes, a pair
260note that is already a merge.
261fn tkload n ulong to ulong
262put tkv 0 0
263if or lt n 32 gt n TKMFCAP
264exit 26
265end
266for k ulong 8
267if ne get tkmf k get tkmagic k
268exit 26
269end
270end
271v = tkrd64 8
272if or lt v TKFIRST gt v TKVMAX
273exit 26
274end
275if ne tkrd64 16 16
276exit 26
277end
278if ne n add 32 mul 4 sub v TKFIRST
279exit 26
280end
281if ne tkfnv n tkrd64 24
282exit 26
283end
284for s ulong 65536
285put tkrk s 0
286end
287for t ulong 256
288put tkln t 1
289end
290for k ulong 16
291put tkln add 256 k 0
292end
293for k ulong TKNSP
294put tkln add 256 k get tkml k
295end
296for k ulong sub v TKFIRST
297tkld1 k
298end
299put tkv 0 v
300ret v
301end
302
303note is t a token of the loaded merges? (below V and not a reserved special)
304fn tkvalid t ulong to bool
305if ge t get tkv 0
306ret false
307end
308ret or lt t add 256 TKNSP ge t TKFIRST
309end
310
311note replace in tkct[0 .. m) every pair of merge token t, left to right without overlap
312fn tkrepl m ulong t ulong to ulong
313a = get tkma t
314b = get tkmb t
315w = ulong 0
316j = ulong 0
317while lt j m bound 128
318x = get tkct j
319z = ushort 65535
320if lt add j 1 m
321z = get tkct add j 1
322end
323if and eq x a eq z b
324put tkct w ushort t
325j = add j 2
326else
327put tkct w x
328j = add j 1
329end
330w = add w 1
331end
332ret w
333end
334
335note encode the chunk tkw[i .. i + l) (l <= 64) into tkct (FORMAT.md section 4: the present merge
336note of lowest rank, until none is left); returns the token count
337fn tkencc i ulong l ulong to ulong
338for k ulong l
339put tkct k ushort get tkw add i k
340end
341m = l
342dn = false
343while and not dn gt m 1 bound 64
344best = ulong 65535
345for j ulong sub m 1
346r = tkrank ulong get tkct j ulong get tkct add j 1
347if lt r best
348best = r
349end
350end
351if eq best 65535
352dn = true
353else
354m = tkrepl m best
355end
356end
357ret m
358end
359
360note the bytes of valid token t into tkdb; returns their count (1 .. 64)
361fn tkdec1 t ulong to ulong
362if and ge t 256 lt t TKFIRST
363k = sub t 256
364o = ulong get tkmo k
365l = ulong get tkml k
366for j ulong l
367put tkdb j get tkmarks add o j
368end
369ret l
370end
371put tksk 0 ushort t
372sp = ulong 1
373w = ulong 0
374while gt sp 0 bound 200
375sp = sub sp 1
376x = ulong get tksk sp
377if lt x 256
378put tkdb w uchar x
379w = add w 1
380else
381put tksk sp get tkmb x
382put tksk add sp 1 get tkma x
383sp = add sp 2
384end
385end
386ret w
387end