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