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