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