interplane.Get Omega
Read in your language

aien-train

AIEN training programme: language-model history, GB10 constraints and the route to training on the project engine. Plans and runnable checks are distinguished.

Brought by drakestapleton

branch main Branches Tags History

aien-train / native / omegallm / tok / tkcli.om

386 lines · 7215 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note omegallm tok/tkcli.om: the commands of the standalone tokenizer program ollm-tok (FORMAT.md
3note section 6). No host words; tkio.om is the platform unit. Text and token files are read as a
4note stream through the window tkw: tcws 0 = file offset of tkw[0], 1 = bytes in tkw, 2 = 1 once
5note the file ended inside the window.
6note ollm-tok toktrain MODE TEXT MERGES V MAXBYTES learn V - 272 merges (MAXBYTES 0: all)
7note ollm-tok tokenc MODE MERGES TEXT TOKS encode to u16 LE tokens
8note ollm-tok tokdec MERGES TOKS OUT decode
9note ollm-tok tokinfo MERGES check a merges file
10note MODE is raw (every byte is text) or chat (the chat markers are special tokens).
11note Exits: 2 usage; 21 a capacity, a text of 2^33 bytes or more, or an odd token file; 25 a token
12note that is not valid for the merges; 26 a bad merges file; 28 no pair left to merge; 70 I/O.
13text tccmds "toktrain tokenc tokdec tokinfo "
14text tcmodes "raw chat "
15global tcws ulong 4
16global tcdg uchar 24
17
18note slot k (9 bytes, space padded) of the 5-byte or 9-byte name table equals argv[i]?
19fn tcis i ulong k ulong tab ulong to bool
20r = tkargp i
21w = ulong 9
22if eq tab 1
23w = ulong 5
24end
25if ge r w
26ret false
27end
28for j ulong add r 1
29c = ulong 32
30if lt j r
31c = ulong get tkarg j
32end
33d = ulong 0
34if eq tab 0
35d = ulong get tccmds add mul k w j
36else
37d = ulong get tcmodes add mul k w j
38end
39if ne c d
40ret false
41end
42end
43ret true
44end
45
46note argv[i] as raw (false) or chat (true); exit 2 otherwise
47fn tcmode i ulong to bool
48if tcis i 0 1
49ret false
50end
51if tcis i 1 1
52ret true
53end
54exit 2
55end
56
57note argv[i] as an unsigned decimal of 1 .. 19 digits
58fn tcargu i ulong to ulong
59n = tkargp i
60if gt n 19
61exit 2
62end
63v = ulong 0
64for k ulong n
65c = ulong get tkarg k
66if or lt c 48 gt c 57
67exit 2
68end
69v = add mul v 10 sub c 48
70end
71ret v
72end
73
74note unsigned decimal v to stdout
75fn tcpu v ulong to void
76n = ulong 0
77x = v
78dn = false
79while not dn bound 24
80put tcdg n uchar add 48 rem x 10
81n = add n 1
82x = div x 10
83if eq x 0
84dn = true
85end
86end
87for k ulong n
88tkpc ulong get tcdg sub sub n 1 k
89end
90end
91
92note a space, then v
93fn tcsu v ulong to void
94tkpc 32
95tcpu v
96end
97
98note window: tkw from byte 0 of argv[ai]
99fn tcwstart ai ulong to void
100wn = tkrdw ai 0 0 TKWCAP
101put tcws 0 0
102put tcws 1 wn
103put tcws 2 0
104if lt wn TKWCAP
105put tcws 2 1
106end
107end
108
109note window: move tkw[a .. wn) to the front and fill the rest from the file
110fn tcwshift ai ulong a ulong to void
111wn = get tcws 1
112cnt = sub wn a
113i = ulong 0
114for o ulong 2
115lim = mul add o 1 999999
116if gt lim cnt
117lim = cnt
118end
119while lt i lim bound 999999
120put tkw i get tkw add a i
121i = add i 1
122end
123end
124wb = add get tcws 0 a
125r = tkrdw ai add wb cnt cnt sub TKWCAP cnt
126put tcws 0 wb
127put tcws 1 add cnt r
128put tcws 2 0
129if lt r sub TKWCAP cnt
130put tcws 2 1
131end
132end
133
134note window position of i after a refill: when fewer than need bytes are left from tkw[i] and the
135note file goes on, tkw[i ..] moves to the front (0); else i
136fn tcrefil ai ulong i ulong need ulong to ulong
137if gt add i need get tcws 1
138if eq get tcws 2 0
139tcwshift ai i
140ret 0
141end
142end
143ret i
144end
145
146note token t to the output file as u16 LE
147fn tcwtok t ulong to void
148tkwb and t 255
149tkwb shr t 8
150end
151
152note encode: one chunk tkw[i .. i + l) to the output, through the type cache when it has room
153fn tcenc1 i ulong l ulong to void
154put tfst 2 add get tfst 2 1
155y = tffind i l
156c = ulong 0
157if ne y TFTCAP
158c = get tftc y
159end
160if eq c 0
161m = tkencc i l
162if ne y TFTCAP
163o = get tfst 1
164if le add o m TFSCAP
165for k ulong m
166put tfsy add o k get tkct k
167end
168put tfts y uint o
169put tftn y uchar m
170put tftc y 1
171put tfst 1 add o m
172end
173end
174for k ulong m
175tcwtok ulong get tkct k
176end
177put tfst 10 add get tfst 10 m
178else
179o = ulong get tfts y
180m = ulong get tftn y
181for k ulong m
182tcwtok ulong get tfsy add o k
183end
184put tfst 10 add get tfst 10 m
185end
186end
187
188note one piece at tkw[i] of a text ending at n: a marker is counted in tfst 4 and, in mode 1,
189note written as its special id; a chunk is counted (mode 0) or encoded (mode 1). Returns its length.
190fn tcpiece i ulong n ulong chat bool mode ulong to ulong
191l = tknext i n chat
192sp = get tkv 1
193if ne sp 0
194put tfst 4 add get tfst 4 1
195if eq mode 1
196tcwtok sp
197put tfst 10 add get tfst 10 1
198end
199else
200if eq mode 0
201tfcount i l
202else
203tcenc1 i l
204end
205end
206ret l
207end
208
209note the pieces of the first lim bytes of argv[ai] (all of it when shorter), through tcpiece.
210note tfst 3 = bytes scanned.
211fn tcscan ai ulong lim ulong chat bool mode ulong to void
212tcwstart ai
213i = ulong 0
214dn = false
215while not dn bound 999999
216q = ulong 0
217while and not dn lt q 999999 bound 999999
218q = add q 1
219i = tcrefil ai i 80
220wb = get tcws 0
221n = get tcws 1
222if lt sub lim wb n
223n = sub lim wb
224end
225if ge i n
226dn = true
227put tfst 3 add wb i
228else
229i = add i tcpiece i n chat mode
230end
231end
232end
233end
234
235note the u16 LE token at tkw[i]: exit 25 when it is not valid for the merges; mode 1 writes its bytes
236fn tcdtok i ulong mode ulong to void
237t = or ulong get tkw i shl ulong get tkw add i 1 8
238if not tkvalid t
239exit 25
240end
241if eq mode 1
242l = tkdec1 t
243for k ulong l
244tkwb ulong get tkdb k
245end
246put tfst 3 add get tfst 3 l
247end
248end
249
250note decode argv[ai] (u16 LE tokens; even length checked by the caller): mode 0 only checks every
251note token (exit 25 on the first invalid one), mode 1 writes the bytes; returns the token count
252fn tcdscan ai ulong mode ulong to ulong
253tcwstart ai
254i = ulong 0
255nt = ulong 0
256dn = false
257while not dn bound 999999
258q = ulong 0
259while and not dn lt q 999999 bound 999999
260q = add q 1
261i = tcrefil ai i 2
262if ge add i 2 add get tcws 1 1
263dn = true
264else
265tcdtok i mode
266nt = add nt 1
267i = add i 2
268end
269end
270end
271ret nt
272end
273
274note the merges file of argv[ai], checked and loaded; returns its V
275fn tcload ai ulong to ulong
276n = tkrdm ai
277ret tkload n
278end
279
280note toktrain MODE TEXT MERGES V MAXBYTES; prints T V types chunks bytes markers pairs nodes
281fn tccmdtr to void
282if ne tkargc 7
283exit 2
284end
285chat = tcmode 2
286v = tcargu 5
287if or lt v TKFIRST gt v TKVMAX
288exit 2
289end
290lim = tcargu 6
291if eq lim 0
292lim = ulong 9223372036854775807
293end
294tcscan 3 lim chat 0
295if ge get tfst 3 8589934592
296exit 21
297end
298tfinit
299tfmerge v
300n = tkseal v
301w = tkload n
302tkmsave 4 n
303tkpc 84
304tcsu w
305tcsu get tfst 0
306tcsu get tfst 2
307tcsu get tfst 3
308tcsu get tfst 4
309tcsu get tfst 5
310tcsu get tfst 7
311tkpc 10
312end
313
314note tokenc MODE MERGES TEXT TOKS; prints E V bytes chunks tokens markers
315fn tccmden to void
316if ne tkargc 6
317exit 2
318end
319chat = tcmode 2
320v = tcload 3
321tkwopen 5
322tcscan 4 9223372036854775807 chat 1
323tkwclose
324tkpc 69
325tcsu v
326tcsu get tfst 3
327tcsu get tfst 2
328tcsu get tfst 10
329tcsu get tfst 4
330tkpc 10
331end
332
333note tokdec MERGES TOKS OUT; prints D V tokens bytes. Every token is checked before OUT is opened.
334fn tccmdde to void
335if ne tkargc 5
336exit 2
337end
338v = tcload 2
339if ne and tkfsz 3 1 0
340exit 21
341end
342nt = tcdscan 3 0
343tkwopen 4
344nt = tcdscan 3 1
345tkwclose
346tkpc 68
347tcsu v
348tcsu nt
349tcsu get tfst 3
350tkpc 10
351end
352
353note tokinfo MERGES; prints I V merges FNV
354fn tccmdin to void
355if ne tkargc 3
356exit 2
357end
358v = tcload 2
359tkpc 73
360tcsu v
361tcsu sub v TKFIRST
362tcsu tkrd64 24
363tkpc 10
364end
365
366note run the command named by argv[1]
367fn tkcmd to int
368tkinit
369if tcis 1 0 0
370tccmdtr
371ret 0
372end
373if tcis 1 1 0
374tccmden
375ret 0
376end
377if tcis 1 2 0
378tccmdde
379ret 0
380end
381if tcis 1 3 0
382tccmdin
383ret 0
384end
385exit 2
386end