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 / bpe.om
| 1 | note SPDX-License-Identifier: AGPL-3.0-only |
| 2 | note omegallm bpe.om (aien-train#52, CONTRACT-V1 sections 2, 7 and 8): the byte-pair tokenizer. |
| 3 | note Text is read as a stream through the opw window of data.om (opstart, opshift) and cut into |
| 4 | note chunks (2.1). bpefit counts chunk types, then merges with a hashed pair table, per-pair lists |
| 5 | note of the types that hold the pair and a lazy max-heap keyed count << 24 | (4095 - a) << 12 | |
| 6 | note (4095 - b): the largest key is the largest count, then the smallest a, then the smallest b. |
| 7 | note A pair's count only falls once its newest token exists, so a popped key whose count is still |
| 8 | note current is the true maximum; a stale one is pushed again with its current count (if any). |
| 9 | note Encode applies, per chunk, the present merge of lowest rank until none is left. That equals |
| 10 | note merges 0, 1, ... in order (2.4): merge k's pair never reappears after merge k, since a later |
| 11 | note merge only makes pairs that hold its own new token. Chunk types cache their tokens. |
| 12 | note Exits: 2 usage (bpefit V outside 257..4096); 21 a capacity below, an odd token file, or |
| 13 | note MAXBYTES with no byte 0 inside; 25 a token >= V; 26 a bad merges file; 27 V above the file's |
| 14 | note V, or V <= 256; 28 no pair left to merge; 70 I/O. |
| 15 | note Interface for the model units (section 8): obload, obtlen, obtput, obprompt and obtk. |
| 16 | note Capacities: 524288 chunk types, 4194304 pool symbols (2 per byte of type), 900000 pairs, |
| 17 | note 4194304 type-in-pair list nodes. |
| 18 | const BQTCAP ulong 524288 |
| 19 | const BQHMSK ulong 1048575 |
| 20 | const BQSCAP ulong 4194304 |
| 21 | const BQPCAP ulong 900000 |
| 22 | const BQPMSK ulong 1048575 |
| 23 | const BQNCAP ulong 4194304 |
| 24 | const BQLCAP ulong 1099511627776 |
| 25 | global obtk ulong 4096 |
| 26 | global obo uchar 4096 |
| 27 | global obon ulong 1 |
| 28 | global obm uchar 15384 |
| 29 | global bqma ushort 4096 |
| 30 | global bqmb ushort 4096 |
| 31 | global bqln ulong 4096 |
| 32 | global bqrk uint 8192 |
| 33 | global bqrt ushort 8192 |
| 34 | global bqv ulong 2 |
| 35 | global bqcl uchar 256 |
| 36 | global bqtm ushort 128 |
| 37 | global bqsk ushort 4100 |
| 38 | global bqst ulong 16 |
| 39 | global bqhs uint 1048576 |
| 40 | global bqto uint 524288 |
| 41 | global bqtl uchar 524288 |
| 42 | global bqtn uchar 524288 |
| 43 | global bqtc ulong 524288 |
| 44 | global bqts uint 524288 |
| 45 | global bqsy ushort 4194304 |
| 46 | global bqpk uint 1048576 |
| 47 | global bqpc ulong 1048576 |
| 48 | global bqph uint 1048576 |
| 49 | global bqpl uint 1048576 |
| 50 | global bqnl uint 1048576 |
| 51 | global bqnt uint 4194304 |
| 52 | global bqnx uint 4194304 |
| 53 | global bqhp ulong 1048576 |
| 54 | |
| 55 | note bqst: 0 types, 1 pool symbols used, 2 chunks, 3 bytes read, 4 tokens, 5 pairs, 6 new pairs |
| 56 | note of this merge (bqnl), 7 list nodes, 8 heap entries. |
| 57 | |
| 58 | note class table: bqcl[c] = 1 when A(c) (section 2) |
| 59 | fn bpcls to void |
| 60 | for c ulong 256 |
| 61 | v = uchar 0 |
| 62 | 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 |
| 63 | v = uchar 1 |
| 64 | end |
| 65 | put bqcl c v |
| 66 | end |
| 67 | end |
| 68 | |
| 69 | note length of the chunk at opw[i] (section 2.1, pieces of at most 64) in a text ending at n > i |
| 70 | fn bpclen i ulong n ulong to ulong |
| 71 | c = ulong get opw i |
| 72 | j = add i 1 |
| 73 | if eq get bqcl c 0 |
| 74 | if ne c 32 |
| 75 | ret 1 |
| 76 | end |
| 77 | if ge j n |
| 78 | ret 1 |
| 79 | end |
| 80 | if eq get bqcl get opw j 0 |
| 81 | ret 1 |
| 82 | end |
| 83 | j = add j 1 |
| 84 | end |
| 85 | e = add i 64 |
| 86 | if lt n e |
| 87 | e = n |
| 88 | end |
| 89 | while lt j e bound 64 |
| 90 | if eq get bqcl get opw j 0 |
| 91 | break |
| 92 | end |
| 93 | j = add j 1 |
| 94 | end |
| 95 | ret sub j i |
| 96 | end |
| 97 | |
| 98 | note as bpclen, over the argument bytes oarg (obprompt) |
| 99 | fn bpclena i ulong n ulong to ulong |
| 100 | c = ulong get oarg i |
| 101 | j = add i 1 |
| 102 | if eq get bqcl c 0 |
| 103 | if ne c 32 |
| 104 | ret 1 |
| 105 | end |
| 106 | if ge j n |
| 107 | ret 1 |
| 108 | end |
| 109 | if eq get bqcl get oarg j 0 |
| 110 | ret 1 |
| 111 | end |
| 112 | j = add j 1 |
| 113 | end |
| 114 | e = add i 64 |
| 115 | if lt n e |
| 116 | e = n |
| 117 | end |
| 118 | while lt j e bound 64 |
| 119 | if eq get bqcl get oarg j 0 |
| 120 | break |
| 121 | end |
| 122 | j = add j 1 |
| 123 | end |
| 124 | ret sub j i |
| 125 | end |
| 126 | |
| 127 | note FNV-1a 64 of opw[i .. i + l) |
| 128 | fn bphash i ulong l ulong to ulong |
| 129 | h = ulong 14695981039346656037 |
| 130 | for k ulong l |
| 131 | h = mul xor h ulong get opw add i k 1099511628211 |
| 132 | end |
| 133 | ret h |
| 134 | end |
| 135 | |
| 136 | note does type y hold the bytes opw[i .. i + l)? |
| 137 | fn bpsame y ulong i ulong l ulong to bool |
| 138 | if ne ulong get bqtl y l |
| 139 | ret false |
| 140 | end |
| 141 | o = ulong get bqto y |
| 142 | for k ulong l |
| 143 | if ne ulong get bqsy add o k ulong get opw add i k |
| 144 | ret false |
| 145 | end |
| 146 | end |
| 147 | ret true |
| 148 | end |
| 149 | |
| 150 | note new type in empty slot s for opw[i .. i + l): its bytes become symbols at bqsy[o], 2 l pool |
| 151 | note symbols are reserved (encode stores the tokens after the bytes); BQTCAP when full |
| 152 | fn bpins s ulong i ulong l ulong to ulong |
| 153 | nt = get bqst 0 |
| 154 | o = get bqst 1 |
| 155 | if ge nt BQTCAP |
| 156 | ret BQTCAP |
| 157 | end |
| 158 | if gt add o mul 2 l BQSCAP |
| 159 | ret BQTCAP |
| 160 | end |
| 161 | put bqhs s uint add nt 1 |
| 162 | put bqto nt uint o |
| 163 | put bqtl nt uchar l |
| 164 | put bqtn nt uchar l |
| 165 | put bqtc nt 0 |
| 166 | put bqts nt 0 |
| 167 | for k ulong l |
| 168 | put bqsy add o k ushort get opw add i k |
| 169 | end |
| 170 | put bqst 0 add nt 1 |
| 171 | put bqst 1 add o l |
| 172 | ret nt |
| 173 | end |
| 174 | |
| 175 | note type id of the chunk opw[i .. i + l), added when absent; BQTCAP when absent and full |
| 176 | fn bpfind i ulong l ulong to ulong |
| 177 | s = and bphash i l BQHMSK |
| 178 | for q ulong 524289 |
| 179 | y = ulong get bqhs s |
| 180 | if eq y 0 |
| 181 | ret bpins s i l |
| 182 | end |
| 183 | y = sub y 1 |
| 184 | if bpsame y i l |
| 185 | ret y |
| 186 | end |
| 187 | s = and add s 1 BQHMSK |
| 188 | end |
| 189 | ret BQTCAP |
| 190 | end |
| 191 | |
| 192 | note fit: count one chunk |
| 193 | fn bpcount i ulong l ulong to void |
| 194 | y = bpfind i l |
| 195 | if eq y BQTCAP |
| 196 | exit 21 |
| 197 | end |
| 198 | put bqtc y add get bqtc y 1 |
| 199 | put bqst 2 add get bqst 2 1 |
| 200 | end |
| 201 | |
| 202 | note append token t to output 0 as u16 LE |
| 203 | fn bpwtok t ulong to void |
| 204 | owbyte 0 and t 255 |
| 205 | owbyte 0 shr t 8 |
| 206 | end |
| 207 | |
| 208 | note rank of pair (a, b): its merge token, or 65535 when it is not a merge below V |
| 209 | fn bprank a ulong b ulong to ulong |
| 210 | key = add mul a 4096 b |
| 211 | s = shr mul add key 1 ulong 11400714819323198485 51 |
| 212 | for q ulong 8192 |
| 213 | k = ulong get bqrk s |
| 214 | if eq k 0 |
| 215 | ret 65535 |
| 216 | end |
| 217 | if eq k add key 1 |
| 218 | ret ulong get bqrt s |
| 219 | end |
| 220 | s = and add s 1 8191 |
| 221 | end |
| 222 | ret 65535 |
| 223 | end |
| 224 | |
| 225 | note replace in bqtm[0 .. m) every (a, b) of merge token t, left to right without overlap |
| 226 | fn bprepl m ulong t ulong to ulong |
| 227 | a = get bqma t |
| 228 | b = get bqmb t |
| 229 | w = ulong 0 |
| 230 | j = ulong 0 |
| 231 | while lt j m bound 128 |
| 232 | x = get bqtm j |
| 233 | z = ushort 65535 |
| 234 | if lt add j 1 m |
| 235 | z = get bqtm add j 1 |
| 236 | end |
| 237 | if and eq x a eq z b |
| 238 | put bqtm w ushort t |
| 239 | j = add j 2 |
| 240 | else |
| 241 | put bqtm w x |
| 242 | j = add j 1 |
| 243 | end |
| 244 | w = add w 1 |
| 245 | end |
| 246 | ret w |
| 247 | end |
| 248 | |
| 249 | note encode bqtm[0 .. n) (bytes) in place (section 2.4); returns the token count |
| 250 | fn bpenct n ulong to ulong |
| 251 | m = n |
| 252 | dn = false |
| 253 | while and not dn gt m 1 bound 64 |
| 254 | best = ulong 65535 |
| 255 | for j ulong sub m 1 |
| 256 | r = bprank ulong get bqtm j ulong get bqtm add j 1 |
| 257 | if lt r best |
| 258 | best = r |
| 259 | end |
| 260 | end |
| 261 | if eq best 65535 |
| 262 | dn = true |
| 263 | else |
| 264 | m = bprepl m best |
| 265 | end |
| 266 | end |
| 267 | ret m |
| 268 | end |
| 269 | |
| 270 | note encode: one chunk opw[i .. i + l) to output 0, through the type cache when it has room |
| 271 | fn bpenc1 i ulong l ulong to void |
| 272 | put bqst 2 add get bqst 2 1 |
| 273 | y = bpfind i l |
| 274 | if eq y BQTCAP |
| 275 | for k ulong l |
| 276 | put bqtm k ushort get opw add i k |
| 277 | end |
| 278 | m = bpenct l |
| 279 | for k ulong m |
| 280 | bpwtok ulong get bqtm k |
| 281 | end |
| 282 | put bqst 4 add get bqst 4 m |
| 283 | else |
| 284 | o = add ulong get bqto y l |
| 285 | if eq get bqtc y 0 |
| 286 | for k ulong l |
| 287 | put bqtm k ushort get opw add i k |
| 288 | end |
| 289 | mt = bpenct l |
| 290 | for k ulong mt |
| 291 | put bqsy add o k get bqtm k |
| 292 | end |
| 293 | put bqtn y uchar mt |
| 294 | put bqtc y 1 |
| 295 | put bqst 1 add get bqst 1 mt |
| 296 | end |
| 297 | m = ulong get bqtn y |
| 298 | for k ulong m |
| 299 | bpwtok ulong get bqsy add o k |
| 300 | end |
| 301 | put bqst 4 add get bqst 4 m |
| 302 | end |
| 303 | end |
| 304 | |
| 305 | note the chunks of the first lim bytes of argument ai (all of it when shorter) through bpcount |
| 306 | note (mode 0) or bpenc1 (mode 1, output 0 opened on argument bo once the text is open); bqst 3 = |
| 307 | note bytes read. A chunk needs at most 65 bytes past its start, so the window moves to the |
| 308 | note current chunk when fewer are left and the file goes on. |
| 309 | note window position of i after a refill: when fewer than need bytes are left from opw[i] and the |
| 310 | note file goes on, opw[i ..] moves to the front (0); else i |
| 311 | fn bprefil ai ulong i ulong need ulong to ulong |
| 312 | if gt add i need get opst 1 |
| 313 | if eq get opst 2 0 |
| 314 | opshift ai i |
| 315 | ret 0 |
| 316 | end |
| 317 | end |
| 318 | ret i |
| 319 | end |
| 320 | |
| 321 | fn bpscan ai ulong lim ulong mode ulong bo ulong to void |
| 322 | opstart ai |
| 323 | if eq mode 1 |
| 324 | owopen 0 bo |
| 325 | end |
| 326 | i = ulong 0 |
| 327 | dn = false |
| 328 | while not dn bound 999999 |
| 329 | q = ulong 0 |
| 330 | while and not dn lt q 999999 bound 999999 |
| 331 | q = add q 1 |
| 332 | i = bprefil ai i 65 |
| 333 | wn = get opst 1 |
| 334 | wb = get opst 0 |
| 335 | n = wn |
| 336 | if lt sub lim wb n |
| 337 | n = sub lim wb |
| 338 | end |
| 339 | if ge i n |
| 340 | dn = true |
| 341 | put bqst 3 add wb i |
| 342 | else |
| 343 | l = bpclen i n |
| 344 | if eq mode 0 |
| 345 | bpcount i l |
| 346 | else |
| 347 | bpenc1 i l |
| 348 | end |
| 349 | i = add i l |
| 350 | end |
| 351 | end |
| 352 | end |
| 353 | end |
| 354 | |
| 355 | note fitbytes for MAXBYTES m: just after the last byte 0 among the first m bytes of argument ai; |
| 356 | note exit 21 when there is none |
| 357 | fn bpcut ai ulong m ulong to ulong |
| 358 | opstart ai |
| 359 | last = ulong 0 |
| 360 | dn = false |
| 361 | while not dn bound 999999 |
| 362 | wb = get opst 0 |
| 363 | wn = get opst 1 |
| 364 | e = wn |
| 365 | if lt sub m wb e |
| 366 | e = sub m wb |
| 367 | end |
| 368 | j = ulong 0 |
| 369 | for o ulong 3 |
| 370 | lj = mul add o 1 999999 |
| 371 | if gt lj e |
| 372 | lj = e |
| 373 | end |
| 374 | while lt j lj bound 999999 |
| 375 | if eq get opw j 0 |
| 376 | last = add add wb j 1 |
| 377 | end |
| 378 | j = add j 1 |
| 379 | end |
| 380 | end |
| 381 | if or lt e wn ne get opst 2 0 |
| 382 | dn = true |
| 383 | else |
| 384 | opshift ai wn |
| 385 | end |
| 386 | end |
| 387 | if eq last 0 |
| 388 | exit 21 |
| 389 | end |
| 390 | ret last |
| 391 | end |
| 392 | |
| 393 | note pair table slot of key a 4096 + b, made (count 0, no list) when absent; new slots are listed |
| 394 | note in bqnl; exit 21 past BQPCAP pairs |
| 395 | fn bppslot key ulong to ulong |
| 396 | s = shr mul add key 1 ulong 11400714819323198485 44 |
| 397 | for q ulong 1048577 |
| 398 | k = ulong get bqpk s |
| 399 | if eq k 0 |
| 400 | nf = get bqst 5 |
| 401 | if ge nf BQPCAP |
| 402 | exit 21 |
| 403 | end |
| 404 | put bqpk s uint add key 1 |
| 405 | put bqpc s 0 |
| 406 | put bqph s 0 |
| 407 | put bqpl s 0 |
| 408 | put bqst 5 add nf 1 |
| 409 | nn = get bqst 6 |
| 410 | put bqnl nn uint s |
| 411 | put bqst 6 add nn 1 |
| 412 | ret s |
| 413 | end |
| 414 | if eq k add key 1 |
| 415 | ret s |
| 416 | end |
| 417 | s = and add s 1 BQPMSK |
| 418 | end |
| 419 | ret s |
| 420 | end |
| 421 | |
| 422 | note list type y under pair slot s (once per type in a row); exit 21 past BQNCAP nodes |
| 423 | fn bpnode s ulong y ulong to void |
| 424 | if ne ulong get bqpl s add y 1 |
| 425 | put bqpl s uint add y 1 |
| 426 | nn = get bqst 7 |
| 427 | if ge nn BQNCAP |
| 428 | exit 21 |
| 429 | end |
| 430 | put bqnt nn uint y |
| 431 | put bqnx nn get bqph s |
| 432 | put bqph s uint add nn 1 |
| 433 | put bqst 7 add nn 1 |
| 434 | end |
| 435 | end |
| 436 | |
| 437 | fn bpkey c ulong a ulong b ulong to ulong |
| 438 | ret or or shl c 24 shl sub 4095 a 12 sub 4095 b |
| 439 | end |
| 440 | |
| 441 | fn bphpush x ulong to void |
| 442 | i = get bqst 8 |
| 443 | put bqst 8 add i 1 |
| 444 | dn = false |
| 445 | while and not dn gt i 0 bound 64 |
| 446 | p = div sub i 1 2 |
| 447 | v = get bqhp p |
| 448 | if ge v x |
| 449 | dn = true |
| 450 | else |
| 451 | put bqhp i v |
| 452 | i = p |
| 453 | end |
| 454 | end |
| 455 | put bqhp i x |
| 456 | end |
| 457 | |
| 458 | note pop the heap's largest key (the heap is not empty) |
| 459 | fn bphpop to ulong |
| 460 | top = get bqhp 0 |
| 461 | n = sub get bqst 8 1 |
| 462 | put bqst 8 n |
| 463 | x = get bqhp n |
| 464 | i = ulong 0 |
| 465 | dn = false |
| 466 | while not dn bound 64 |
| 467 | l = add mul i 2 1 |
| 468 | if ge l n |
| 469 | dn = true |
| 470 | else |
| 471 | c = l |
| 472 | r = add l 1 |
| 473 | if lt r n |
| 474 | if gt get bqhp r get bqhp l |
| 475 | c = r |
| 476 | end |
| 477 | end |
| 478 | v = get bqhp c |
| 479 | if ge x v |
| 480 | dn = true |
| 481 | else |
| 482 | put bqhp i v |
| 483 | i = c |
| 484 | end |
| 485 | end |
| 486 | end |
| 487 | if gt n 0 |
| 488 | put bqhp i x |
| 489 | end |
| 490 | ret top |
| 491 | end |
| 492 | |
| 493 | note bqpc[s] += c and -= c, one big array per function (error 12 at this pin otherwise) |
| 494 | fn bppcadd s ulong c ulong to void |
| 495 | put bqpc s add get bqpc s c |
| 496 | end |
| 497 | fn bppcsub s ulong c ulong to void |
| 498 | put bqpc s sub get bqpc s c |
| 499 | end |
| 500 | |
| 501 | note count the pairs of type y and list y under them |
| 502 | fn bpinty y ulong to void |
| 503 | o = ulong get bqto y |
| 504 | c = get bqtc y |
| 505 | for j ulong sub ulong get bqtn y 1 |
| 506 | x = ulong get bqsy add o j |
| 507 | z = ulong get bqsy add add o j 1 |
| 508 | s = bppslot add mul x 4096 z |
| 509 | bppcadd s c |
| 510 | bpnode s y |
| 511 | end |
| 512 | end |
| 513 | |
| 514 | note count the pairs of every type, list the types under them, heap every pair |
| 515 | fn bpinit to void |
| 516 | nt = get bqst 0 |
| 517 | for y ulong nt |
| 518 | bpinty y |
| 519 | end |
| 520 | for s ulong 1048576 |
| 521 | k = ulong get bqpk s |
| 522 | if ne k 0 |
| 523 | c = get bqpc s |
| 524 | if gt c 0 |
| 525 | k = sub k 1 |
| 526 | bphpush bpkey c div k 4096 and k 4095 |
| 527 | end |
| 528 | end |
| 529 | end |
| 530 | end |
| 531 | |
| 532 | note the pair to merge: the slot of the heap's largest current key; exit 28 when none is left. |
| 533 | note Each pair has at most one heap entry, and one pop sequence meets a pair at most twice. |
| 534 | fn bpsel to ulong |
| 535 | for o ulong 3 |
| 536 | while gt get bqst 8 0 bound 999999 |
| 537 | x = bphpop |
| 538 | c = shr x 24 |
| 539 | a = sub 4095 and shr x 12 4095 |
| 540 | b = sub 4095 and x 4095 |
| 541 | s = bppslot add mul a 4096 b |
| 542 | cc = get bqpc s |
| 543 | if eq cc c |
| 544 | ret s |
| 545 | end |
| 546 | if gt cc 0 |
| 547 | bphpush bpkey cc a b |
| 548 | end |
| 549 | end |
| 550 | end |
| 551 | exit 28 |
| 552 | end |
| 553 | |
| 554 | fn bphas o ulong n ulong a ulong b ulong to bool |
| 555 | for j ulong sub n 1 |
| 556 | x = ulong get bqsy add o j |
| 557 | z = ulong get bqsy add add o j 1 |
| 558 | if and eq x a eq z b |
| 559 | ret true |
| 560 | end |
| 561 | end |
| 562 | ret false |
| 563 | end |
| 564 | |
| 565 | note take count c of every pair in bqsy[o .. o + n) |
| 566 | fn bpsub o ulong n ulong c ulong to void |
| 567 | for j ulong sub n 1 |
| 568 | x = ulong get bqsy add o j |
| 569 | z = ulong get bqsy add add o j 1 |
| 570 | s = bppslot add mul x 4096 z |
| 571 | bppcsub s c |
| 572 | end |
| 573 | end |
| 574 | |
| 575 | note replace (a, b) by t in bqsy[o .. o + n), left to right without overlap; returns the length |
| 576 | fn bprpool o ulong n ulong a ulong b ulong t ulong to ulong |
| 577 | w = ulong 0 |
| 578 | j = ulong 0 |
| 579 | while lt j n bound 128 |
| 580 | x = ulong get bqsy add o j |
| 581 | z = ulong 65535 |
| 582 | if lt add j 1 n |
| 583 | z = ulong get bqsy add add o j 1 |
| 584 | end |
| 585 | if and eq x a eq z b |
| 586 | put bqsy add o w ushort t |
| 587 | j = add j 2 |
| 588 | else |
| 589 | put bqsy add o w ushort x |
| 590 | j = add j 1 |
| 591 | end |
| 592 | w = add w 1 |
| 593 | end |
| 594 | ret w |
| 595 | end |
| 596 | |
| 597 | note add count c of every pair in bqsy[o .. o + m) of type y; list y under the pairs holding t |
| 598 | fn bpadd o ulong m ulong c ulong y ulong t ulong to void |
| 599 | for j ulong sub m 1 |
| 600 | x = ulong get bqsy add o j |
| 601 | z = ulong get bqsy add add o j 1 |
| 602 | s = bppslot add mul x 4096 z |
| 603 | bppcadd s c |
| 604 | if or eq x t eq z t |
| 605 | bpnode s y |
| 606 | end |
| 607 | end |
| 608 | end |
| 609 | |
| 610 | note apply merge (a, b) -> t to type y, moving its pair counts |
| 611 | fn bpmtype y ulong a ulong b ulong t ulong to void |
| 612 | o = ulong get bqto y |
| 613 | n = ulong get bqtn y |
| 614 | if bphas o n a b |
| 615 | c = get bqtc y |
| 616 | bpsub o n c |
| 617 | m = bprpool o n a b t |
| 618 | put bqtn y uchar m |
| 619 | bpadd o m c y t |
| 620 | end |
| 621 | end |
| 622 | |
| 623 | note merge t over every type listed under pair slot s (each type once: stamp t - 255) |
| 624 | fn bpwalk s ulong a ulong b ulong t ulong to void |
| 625 | nd = ulong get bqph s |
| 626 | st = uint sub t 255 |
| 627 | for o ulong 5 |
| 628 | while gt nd 0 bound 999999 |
| 629 | y = ulong get bqnt sub nd 1 |
| 630 | nd = ulong get bqnx sub nd 1 |
| 631 | if ne get bqts y st |
| 632 | put bqts y st |
| 633 | bpmtype y a b t |
| 634 | end |
| 635 | end |
| 636 | end |
| 637 | end |
| 638 | |
| 639 | note merges 0 .. v - 257 into bqma/bqmb and obm[24 ..) |
| 640 | fn bpmerge v ulong to void |
| 641 | for k ulong sub v 256 |
| 642 | s = bpsel |
| 643 | key = sub ulong get bqpk s 1 |
| 644 | a = div key 4096 |
| 645 | b = and key 4095 |
| 646 | t = add 256 k |
| 647 | put bqma t ushort a |
| 648 | put bqmb t ushort b |
| 649 | p = add 24 mul 4 k |
| 650 | put obm p uchar and a 255 |
| 651 | put obm add p 1 uchar shr a 8 |
| 652 | put obm add p 2 uchar and b 255 |
| 653 | put obm add p 3 uchar shr b 8 |
| 654 | put bqst 6 0 |
| 655 | bpwalk s a b t |
| 656 | bpnewp |
| 657 | end |
| 658 | end |
| 659 | |
| 660 | note heap every pair slot made by this merge (bqnl[0 .. bqst 6)) that has a count |
| 661 | fn bpnewp to void |
| 662 | nw = get bqst 6 |
| 663 | for i ulong nw |
| 664 | q = ulong get bqnl i |
| 665 | c = get bqpc q |
| 666 | if gt c 0 |
| 667 | kq = sub ulong get bqpk q 1 |
| 668 | bphpush bpkey c div kq 4096 and kq 4095 |
| 669 | end |
| 670 | end |
| 671 | end |
| 672 | |
| 673 | note obm[p .. p + 8) = v as u64 LE |
| 674 | fn bpu64 p ulong v ulong to void |
| 675 | for k ulong 8 |
| 676 | put obm add p k uchar and shr v mul k 8 255 |
| 677 | end |
| 678 | end |
| 679 | |
| 680 | note FNV-1a 64 of obm[24 .. e) |
| 681 | fn bpfnvm e ulong to ulong |
| 682 | h = ulong 14695981039346656037 |
| 683 | for k ulong sub e 24 |
| 684 | h = mul xor h ulong get obm add 24 k 1099511628211 |
| 685 | end |
| 686 | ret h |
| 687 | end |
| 688 | |
| 689 | note u16 LE at opw[p] |
| 690 | fn bprd16 p ulong to ulong |
| 691 | lo = ulong get opw p |
| 692 | hi = ulong get opw add p 1 |
| 693 | ret or lo shl hi 8 |
| 694 | end |
| 695 | |
| 696 | note u64 LE at opw[p] |
| 697 | fn bprd64 p ulong to ulong |
| 698 | v = ulong 0 |
| 699 | for k ulong 8 |
| 700 | v = or v shl ulong get opw add p k mul k 8 |
| 701 | end |
| 702 | ret v |
| 703 | end |
| 704 | |
| 705 | note read and check the merges file of argument ai (section 2.3; refusals 26) into bqma/bqmb; |
| 706 | note returns its V. Uses opw. |
| 707 | fn bpload ai ulong to ulong |
| 708 | n = oprd ai 0 0 15385 |
| 709 | if or lt n 24 gt n 15384 |
| 710 | exit 26 |
| 711 | end |
| 712 | if ne bprd64 0 3550332126946413647 |
| 713 | exit 26 |
| 714 | end |
| 715 | vm = bprd64 8 |
| 716 | if or lt vm 257 gt vm 4096 |
| 717 | exit 26 |
| 718 | end |
| 719 | if ne n add 24 mul 4 sub vm 256 |
| 720 | exit 26 |
| 721 | end |
| 722 | h = ulong 14695981039346656037 |
| 723 | for k ulong sub n 24 |
| 724 | h = mul xor h ulong get opw add 24 k 1099511628211 |
| 725 | end |
| 726 | if ne h bprd64 16 |
| 727 | exit 26 |
| 728 | end |
| 729 | for k ulong sub vm 256 |
| 730 | p = add 24 mul 4 k |
| 731 | a = bprd16 p |
| 732 | b = bprd16 add p 2 |
| 733 | lk = add 256 k |
| 734 | if or ge a lk ge b lk |
| 735 | exit 26 |
| 736 | end |
| 737 | t = add 256 k |
| 738 | put bqma t ushort a |
| 739 | put bqmb t ushort b |
| 740 | end |
| 741 | put bqv 1 vm |
| 742 | ret vm |
| 743 | end |
| 744 | |
| 745 | note use the first v - 256 merges: decoded lengths (held at 2^40) and the rank table |
| 746 | fn bpsetv v ulong to void |
| 747 | for t ulong 256 |
| 748 | put bqln t 1 |
| 749 | end |
| 750 | for k ulong sub v 256 |
| 751 | t = add 256 k |
| 752 | ma = ulong get bqma t |
| 753 | mb = ulong get bqmb t |
| 754 | la = get bqln ma |
| 755 | s = add la get bqln mb |
| 756 | if gt s BQLCAP |
| 757 | s = BQLCAP |
| 758 | end |
| 759 | put bqln t s |
| 760 | end |
| 761 | for s ulong 8192 |
| 762 | put bqrk s 0 |
| 763 | end |
| 764 | for k ulong sub v 256 |
| 765 | t = add 256 k |
| 766 | ma = ulong get bqma t |
| 767 | mb = ulong get bqmb t |
| 768 | key = add mul ma 4096 mb |
| 769 | s = shr mul add key 1 ulong 11400714819323198485 51 |
| 770 | dn = false |
| 771 | while not dn bound 8192 |
| 772 | if eq get bqrk s 0 |
| 773 | put bqrk s uint add key 1 |
| 774 | put bqrt s ushort t |
| 775 | dn = true |
| 776 | else |
| 777 | s = and add s 1 8191 |
| 778 | end |
| 779 | end |
| 780 | end |
| 781 | put bqv 0 v |
| 782 | end |
| 783 | |
| 784 | note write the decoded bytes of token t to stdout (mode 0), to output 0 (mode 1) or to the capture |
| 785 | note buffer obo (mode 2, serve.om; bytes past obo's 4096 are dropped, so callers check obtlen first) |
| 786 | fn bpexp t ulong mode ulong to void |
| 787 | put bqsk 0 ushort t |
| 788 | sp = ulong 1 |
| 789 | while gt sp 0 bound 999999 |
| 790 | q = ulong 0 |
| 791 | while and gt sp 0 lt q 999999 bound 999999 |
| 792 | q = add q 1 |
| 793 | sp = sub sp 1 |
| 794 | x = ulong get bqsk sp |
| 795 | if lt x 256 |
| 796 | if eq mode 0 |
| 797 | oputc x |
| 798 | else |
| 799 | if eq mode 1 |
| 800 | owbyte 0 x |
| 801 | else |
| 802 | n = get obon 0 |
| 803 | if lt n 4096 |
| 804 | put obo n uchar x |
| 805 | put obon 0 add n 1 |
| 806 | end |
| 807 | end |
| 808 | end |
| 809 | else |
| 810 | put bqsk sp get bqmb x |
| 811 | put bqsk add sp 1 get bqma x |
| 812 | sp = add sp 2 |
| 813 | end |
| 814 | end |
| 815 | end |
| 816 | end |
| 817 | |
| 818 | note decode token t to output 0 and add its length to bqst 4 |
| 819 | fn bpdec1 t ulong to void |
| 820 | bpexp t 1 |
| 821 | put bqst 4 add get bqst 4 get bqln t |
| 822 | end |
| 823 | |
| 824 | note token file argument ai: mode 0 checks it (exit 21 odd length, then 25 a token >= v) and |
| 825 | note returns its token count; mode 1 decodes it to output 0 (bqst 4 = bytes) |
| 826 | fn bpdscan ai ulong v ulong mode ulong to ulong |
| 827 | opstart ai |
| 828 | i = ulong 0 |
| 829 | nt = ulong 0 |
| 830 | bad = false |
| 831 | odd = false |
| 832 | dn = false |
| 833 | while not dn bound 999999 |
| 834 | q = ulong 0 |
| 835 | while and not dn lt q 999999 bound 999999 |
| 836 | q = add q 1 |
| 837 | i = bprefil ai i 2 |
| 838 | wn = get opst 1 |
| 839 | if gt add i 2 wn |
| 840 | dn = true |
| 841 | if lt i wn |
| 842 | odd = true |
| 843 | end |
| 844 | else |
| 845 | t = bprd16 i |
| 846 | if ge t v |
| 847 | bad = true |
| 848 | end |
| 849 | if eq mode 1 |
| 850 | bpdec1 t |
| 851 | end |
| 852 | nt = add nt 1 |
| 853 | i = add i 2 |
| 854 | end |
| 855 | end |
| 856 | end |
| 857 | if odd |
| 858 | exit 21 |
| 859 | end |
| 860 | if bad |
| 861 | exit 25 |
| 862 | end |
| 863 | ret nt |
| 864 | end |
| 865 | |
| 866 | note print C a b c (C = 77 M, 75 K, 68 D) |
| 867 | fn bpline c ulong a ulong b ulong d ulong to void |
| 868 | oputc c |
| 869 | oputc 32 |
| 870 | oputu a |
| 871 | oputc 32 |
| 872 | oputu b |
| 873 | if ne c 75 |
| 874 | if ne c 68 |
| 875 | oputc 32 |
| 876 | oputu d |
| 877 | end |
| 878 | end |
| 879 | oputc 10 |
| 880 | oflush |
| 881 | end |
| 882 | |
| 883 | note section 8: load merges argument ai for vocabulary v (26, 27) |
| 884 | fn obload ai ulong v ulong to void |
| 885 | vm = bpload ai |
| 886 | if or le v 256 gt v vm |
| 887 | exit 27 |
| 888 | end |
| 889 | bpsetv v |
| 890 | bpcls |
| 891 | end |
| 892 | |
| 893 | fn obtlen t ulong to ulong |
| 894 | ret get bqln t |
| 895 | end |
| 896 | |
| 897 | fn obtput t ulong to void |
| 898 | bpexp t 0 |
| 899 | end |
| 900 | |
| 901 | note the decoded bytes of token t into obo[0 .. obon[0]) (serve.om) |
| 902 | fn obtcap t ulong to void |
| 903 | put obon 0 0 |
| 904 | bpexp t 2 |
| 905 | end |
| 906 | |
| 907 | note section 8: encode argument ai (at most 4000 bytes, CONTRACT 7.2; longer exit 2) into obtk; |
| 908 | note returns the token count |
| 909 | fn obprompt ai ulong to ulong |
| 910 | n = oargp ai |
| 911 | if gt n 4000 |
| 912 | exit 2 |
| 913 | end |
| 914 | ret obpenc n |
| 915 | end |
| 916 | |
| 917 | note encode oarg[0 .. n), n <= 4000, into obtk; returns the token count (obprompt, serve.om) |
| 918 | fn obpenc n ulong to ulong |
| 919 | i = ulong 0 |
| 920 | m = ulong 0 |
| 921 | while lt i n bound 4097 |
| 922 | l = bpclena i n |
| 923 | for k ulong l |
| 924 | put bqtm k ushort get oarg add i k |
| 925 | end |
| 926 | c = bpenct l |
| 927 | for k ulong c |
| 928 | put obtk add m k ulong get bqtm k |
| 929 | end |
| 930 | m = add m c |
| 931 | i = add i l |
| 932 | end |
| 933 | ret m |
| 934 | end |
| 935 | |
| 936 | note bpefit TEXT MERGES V [MAXBYTES] |
| 937 | fn cmdbpfit to void |
| 938 | na = oargc |
| 939 | if or lt na 5 gt na 6 |
| 940 | exit 2 |
| 941 | end |
| 942 | v = oargu 4 |
| 943 | if or lt v 257 gt v 4096 |
| 944 | exit 2 |
| 945 | end |
| 946 | lim = ulong 18446744073709551615 |
| 947 | if eq na 6 |
| 948 | lim = bpcut 2 oargu 5 |
| 949 | end |
| 950 | bpcls |
| 951 | bpscan 2 lim 0 0 |
| 952 | bpinit |
| 953 | bpmerge v |
| 954 | nb = add 24 mul 4 sub v 256 |
| 955 | bpu64 0 3550332126946413647 |
| 956 | bpu64 8 v |
| 957 | bpu64 16 bpfnvm nb |
| 958 | obmsave 3 nb |
| 959 | oputc 77 |
| 960 | oputc 32 |
| 961 | oputu v |
| 962 | oputc 32 |
| 963 | oputu get bqst 0 |
| 964 | oputc 32 |
| 965 | oputu get bqst 2 |
| 966 | oputc 32 |
| 967 | oputu get bqst 3 |
| 968 | oputc 10 |
| 969 | oflush |
| 970 | end |
| 971 | |
| 972 | note bpeenc MERGES V TEXT TOKS |
| 973 | fn cmdbpenc to void |
| 974 | if ne oargc 6 |
| 975 | exit 2 |
| 976 | end |
| 977 | v = oargu 3 |
| 978 | obload 2 v |
| 979 | bpscan 4 ulong 18446744073709551615 1 5 |
| 980 | owclose 0 |
| 981 | bpline 75 get bqst 3 get bqst 4 0 |
| 982 | end |
| 983 | |
| 984 | note bpedec MERGES TOKS OUT (V = the file's V) |
| 985 | fn cmdbpdec to void |
| 986 | if ne oargc 5 |
| 987 | exit 2 |
| 988 | end |
| 989 | vm = bpload 2 |
| 990 | bpsetv vm |
| 991 | nt = bpdscan 3 vm 0 |
| 992 | owopen 0 4 |
| 993 | put bqst 4 0 |
| 994 | nd = bpdscan 3 vm 1 |
| 995 | owclose 0 |
| 996 | bpline 68 nt get bqst 4 0 |
| 997 | end |