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 / tkfit.om

432 lines · 9283 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note omegallm tok/tkfit.om: the merge learner of the own BPE (FORMAT.md section 5) and the chunk
3note type table that tokenc also uses as its encode cache. No host words; about 441 MiB of
4note globals, so it lives only in the standalone program ollm-tok, never in ollm.
5note Fit counts chunk types (tkcli.om tcscan), then merges with a hashed pair table, per-pair lists
6note of the types that hold the pair and a lazy max-heap keyed count << 30 | (32767 - a) << 15 |
7note (32767 - b): the largest key is the largest count, then the smallest a, then the smallest b.
8note A pair's count only falls once its newest token exists, so a popped key whose count is still
9note current is the true maximum; a stale one is pushed again with its current count (if any).
10note A merged pair never appears again, so the list nodes of its pair go back to a free list.
11note Counts stay below 2^34 because tcscan refuses texts of 2^33 bytes or more.
12note Exits: 21 a capacity below; 28 no pair left to merge.
13note Capacities: 2097152 chunk types, 16777216 pool symbols, 6000000 pairs, 16777216 list nodes.
14const TFTCAP ulong 2097152
15const TFHMSK ulong 4194303
16const TFSCAP ulong 16777216
17const TFPCAP ulong 6000000
18const TFPMSK ulong 8388607
19const TFNCAP ulong 16777216
20global tfhs uint 4194304
21global tfto uint 2097152
22global tftl uchar 2097152
23global tftn uchar 2097152
24global tftc ulong 2097152
25global tfts uint 2097152
26global tfsy ushort 16777216
27global tfpk uint 8388608
28global tfpc ulong 8388608
29global tfph uint 8388608
30global tfpl uint 8388608
31global tfnl uint 6000000
32global tfnt uint 16777216
33global tfnx uint 16777216
34global tfhp ulong 6000000
35global tfst ulong 16
36
37note tfst: 0 types, 1 pool symbols used, 2 chunks, 3 bytes scanned, 4 special markers, 5 pairs,
38note 6 new pairs of this merge (tfnl), 7 list nodes made, 8 heap entries, 9 free node list head
39note (node + 1; 0 empty), 10 tokens written (tokenc).
40
41note FNV-1a 64 of tkw[i .. i + l)
42fn tfhash i ulong l ulong to ulong
43h = ulong 14695981039346656037
44for k ulong l
45h = mul xor h ulong get tkw add i k 1099511628211
46end
47ret h
48end
49
50note does type y hold the bytes tkw[i .. i + l)?
51fn tfsame y ulong i ulong l ulong to bool
52if ne ulong get tftl y l
53ret false
54end
55o = ulong get tfto y
56for k ulong l
57if ne ulong get tfsy add o k ulong get tkw add i k
58ret false
59end
60end
61ret true
62end
63
64note new type in empty hash slot s for tkw[i .. i + l), its bytes as pool symbols; TFTCAP when
65note the type table or the pool is full
66fn tfins s ulong i ulong l ulong to ulong
67nt = get tfst 0
68o = get tfst 1
69if ge nt TFTCAP
70ret TFTCAP
71end
72if gt add o l TFSCAP
73ret TFTCAP
74end
75put tfhs s uint add nt 1
76put tfto nt uint o
77put tftl nt uchar l
78put tftn nt uchar l
79put tftc nt 0
80put tfts nt 0
81for k ulong l
82put tfsy add o k ushort get tkw add i k
83end
84put tfst 0 add nt 1
85put tfst 1 add o l
86ret nt
87end
88
89note type id of the chunk tkw[i .. i + l), added when absent; TFTCAP when absent and full
90fn tffind i ulong l ulong to ulong
91s = and tfhash i l TFHMSK
92for q ulong 4194305
93y = ulong get tfhs s
94if eq y 0
95ret tfins s i l
96end
97y = sub y 1
98if tfsame y i l
99ret y
100end
101s = and add s 1 TFHMSK
102end
103ret TFTCAP
104end
105
106note fit: count one chunk
107fn tfcount i ulong l ulong to void
108y = tffind i l
109if eq y TFTCAP
110exit 21
111end
112put tftc y add get tftc y 1
113put tfst 2 add get tfst 2 1
114end
115
116note pair table slot of key a 32768 + b, made (count 0, no list) when absent; new slots are listed
117note in tfnl; exit 21 past TFPCAP pairs
118fn tfpslot key ulong to ulong
119s = shr mul add key 1 ulong 11400714819323198485 41
120for q ulong 8388609
121k = ulong get tfpk s
122if eq k 0
123nf = get tfst 5
124if ge nf TFPCAP
125exit 21
126end
127put tfpk s uint add key 1
128put tfpc s 0
129put tfph s 0
130put tfpl s 0
131put tfst 5 add nf 1
132nn = get tfst 6
133put tfnl nn uint s
134put tfst 6 add nn 1
135ret s
136end
137if eq k add key 1
138ret s
139end
140s = and add s 1 TFPMSK
141end
142ret s
143end
144
145note list type y under pair slot s (once per type in a row), reusing a freed node when there is
146note one; exit 21 past TFNCAP nodes
147fn tfnode s ulong y ulong to void
148if ne ulong get tfpl s add y 1
149put tfpl s uint add y 1
150f = get tfst 9
151q = ulong 0
152if gt f 0
153q = sub f 1
154put tfst 9 ulong get tfnx q
155else
156q = get tfst 7
157if ge q TFNCAP
158exit 21
159end
160put tfst 7 add q 1
161end
162put tfnt q uint y
163put tfnx q get tfph s
164put tfph s uint add q 1
165end
166end
167
168fn tfkey c ulong a ulong b ulong to ulong
169ret or or shl c 30 shl sub 32767 a 15 sub 32767 b
170end
171
172fn tfhpush x ulong to void
173i = get tfst 8
174put tfst 8 add i 1
175dn = false
176while and not dn gt i 0 bound 64
177p = div sub i 1 2
178v = get tfhp p
179if ge v x
180dn = true
181else
182put tfhp i v
183i = p
184end
185end
186put tfhp i x
187end
188
189note pop the heap's largest key (the heap is not empty)
190fn tfhpop to ulong
191top = get tfhp 0
192n = sub get tfst 8 1
193put tfst 8 n
194x = get tfhp n
195i = ulong 0
196dn = false
197while not dn bound 64
198l = add mul i 2 1
199if ge l n
200dn = true
201else
202c = l
203r = add l 1
204if lt r n
205if gt get tfhp r get tfhp l
206c = r
207end
208end
209v = get tfhp c
210if ge x v
211dn = true
212else
213put tfhp i v
214i = c
215end
216end
217end
218if gt n 0
219put tfhp i x
220end
221ret top
222end
223
224note tfpc[s] += c and -= c, one big array per function
225fn tfpcadd s ulong c ulong to void
226put tfpc s add get tfpc s c
227end
228fn tfpcsub s ulong c ulong to void
229put tfpc s sub get tfpc s c
230end
231
232note count the pairs of type y and list y under them
233fn tfinty y ulong to void
234o = ulong get tfto y
235c = get tftc y
236for j ulong sub ulong get tftn y 1
237x = ulong get tfsy add o j
238z = ulong get tfsy add add o j 1
239s = tfpslot or shl x 15 z
240tfpcadd s c
241tfnode s y
242end
243end
244
245note count the pairs of every type, list the types under them, heap every pair
246fn tfinit to void
247nt = get tfst 0
248y = ulong 0
249for o ulong 3
250lim = mul add o 1 999999
251if gt lim nt
252lim = nt
253end
254while lt y lim bound 999999
255if gt ulong get tftn y 1
256tfinty y
257end
258y = add y 1
259end
260end
261for s ulong 8388608
262k = ulong get tfpk s
263if ne k 0
264c = get tfpc s
265if gt c 0
266k = sub k 1
267tfhpush tfkey c shr k 15 and k 32767
268end
269end
270end
271end
272
273note the pair to merge: the slot of the heap's largest current key; exit 28 when none is left.
274note Each pair has at most one heap entry, and one pop sequence meets a pair at most twice.
275fn tfsel to ulong
276for o ulong 13
277q = ulong 0
278while and gt get tfst 8 0 lt q 999999 bound 999999
279q = add q 1
280x = tfhpop
281c = shr x 30
282a = sub 32767 and shr x 15 32767
283b = sub 32767 and x 32767
284s = tfpslot or shl a 15 b
285cc = get tfpc s
286if eq cc c
287ret s
288end
289if gt cc 0
290tfhpush tfkey cc a b
291end
292end
293end
294exit 28
295end
296
297fn tfhas o ulong n ulong a ulong b ulong to bool
298for j ulong sub n 1
299x = ulong get tfsy add o j
300z = ulong get tfsy add add o j 1
301if and eq x a eq z b
302ret true
303end
304end
305ret false
306end
307
308note take count c of every pair in tfsy[o .. o + n)
309fn tfsub o ulong n ulong c ulong to void
310for j ulong sub n 1
311x = ulong get tfsy add o j
312z = ulong get tfsy add add o j 1
313s = tfpslot or shl x 15 z
314tfpcsub s c
315end
316end
317
318note replace (a, b) by t in tfsy[o .. o + n), left to right without overlap; returns the length
319fn tfrpool o ulong n ulong a ulong b ulong t ulong to ulong
320w = ulong 0
321j = ulong 0
322while lt j n bound 128
323x = ulong get tfsy add o j
324z = ulong 65535
325if lt add j 1 n
326z = ulong get tfsy add add o j 1
327end
328if and eq x a eq z b
329put tfsy add o w ushort t
330j = add j 2
331else
332put tfsy add o w ushort x
333j = add j 1
334end
335w = add w 1
336end
337ret w
338end
339
340note add count c of every pair in tfsy[o .. o + m) of type y; list y under the pairs holding t
341fn tfadd o ulong m ulong c ulong y ulong t ulong to void
342for j ulong sub m 1
343x = ulong get tfsy add o j
344z = ulong get tfsy add add o j 1
345s = tfpslot or shl x 15 z
346tfpcadd s c
347if or eq x t eq z t
348tfnode s y
349end
350end
351end
352
353note apply merge (a, b) -> t to type y, moving its pair counts
354fn tfmtype y ulong a ulong b ulong t ulong to void
355o = ulong get tfto y
356n = ulong get tftn y
357if tfhas o n a b
358c = get tftc y
359tfsub o n c
360m = tfrpool o n a b t
361put tftn y uchar m
362tfadd o m c y t
363end
364end
365
366note free list node e; returns its successor (node + 1; 0 at the end)
367fn tffree e ulong to ulong
368nx = ulong get tfnx e
369put tfnx e uint get tfst 9
370put tfst 9 add e 1
371ret nx
372end
373
374note merge t into type y unless y already met this merge (stamp t - 271)
375fn tfvisit y ulong a ulong b ulong t ulong to void
376st = uint sub t 271
377if ne get tfts y st
378put tfts y st
379tfmtype y a b t
380end
381end
382
383note merge t over every type listed under pair slot s; the walked nodes go to the free list (the
384note pair never comes back)
385fn tfwalk s ulong a ulong b ulong t ulong to void
386nd = ulong get tfph s
387put tfph s 0
388for o ulong 17
389q = ulong 0
390while and gt nd 0 lt q 999999 bound 999999
391q = add q 1
392e = sub nd 1
393y = ulong get tfnt e
394nd = tffree e
395tfvisit y a b t
396end
397end
398end
399
400note heap every pair slot made by this merge (tfnl[0 .. tfst 6)) that has a count
401fn tfnewp to void
402nw = get tfst 6
403for i ulong nw
404q = ulong get tfnl i
405c = get tfpc q
406if gt c 0
407kq = sub ulong get tfpk q 1
408tfhpush tfkey c shr kq 15 and kq 32767
409end
410end
411end
412
413note merges 0 .. v - 273 into tkma/tkmb and the pair records of tkmf (tkseal adds the header)
414fn tfmerge v ulong to void
415for k ulong sub v TKFIRST
416s = tfsel
417key = sub ulong get tfpk s 1
418a = shr key 15
419b = and key 32767
420t = add TKFIRST k
421put tkma t ushort a
422put tkmb t ushort b
423p = add 32 mul 4 k
424put tkmf p uchar and a 255
425put tkmf add p 1 uchar shr a 8
426put tkmf add p 2 uchar and b 255
427put tkmf add p 3 uchar shr b 8
428put tfst 6 0
429tfwalk s a b t
430tfnewp
431end
432end