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 / btwave / trit.om
| 1 | note SPDX-License-Identifier: AGPL-3.0-only |
| 2 | note BTWAVE-P0: Non-binary balanced-ternary SWAR wave foundation (native/btwave/trit.om). |
| 3 | note Pure seeded OM-2 (om2) implementation: |
| 4 | note 1. Trit32: 32 balanced trits {-1, 0, +1} packed in u64 ((pos32 << 32) | neg32) |
| 5 | note with constructor postcondition (pos32 & neg32) == 0 (traps 10 -> exit 110). |
| 6 | note 2. WaveDup: mirrored 64-bit waveguide frame tag with OM-2 holds dup32 (traps 10). |
| 7 | note 3. CreditJ and GrantJ: distinct abstract types separating earned compute credit |
| 8 | note from authorized compute extraction (swaps fail omc compilation with error 7). |
| 9 | note 4. Multi-scale M=6 balanced-ternary toroidal codec on [-364, 364] (729 states, |
| 10 | note enc(0) = 0 Dark Consensus, 1-op ROR negation symmetry) + M=3 16/16 hypercube. |
| 11 | note 5. Orthogonal H16 Walsh-Hadamard ternary wave spreading (d_H = 8), correcting |
| 12 | note all 0..3 carrier corruptions and fail-closed refusing >= 4-carrier ties. |
| 13 | note 6. Pure OM-2 FIPS 180-4 SHA-256 with fresh message schedule and unrolled rounds. |
| 14 | om2 |
| 15 | abstract Trit32 ulong ops rotr rotl |
| 16 | abstract WaveDup ulong |
| 17 | holds WaveDup dup32 |
| 18 | abstract CreditJ ulong |
| 19 | abstract GrantJ ulong |
| 20 | |
| 21 | const M32 ulong 0xffffffff |
| 22 | const M16 ulong 0xffff |
| 23 | const VMAX long 65536 |
| 24 | const WMAX ulong 1000 |
| 25 | |
| 26 | fn Trit32mk w ulong to Trit32 |
| 27 | ensures eq and shr w 32 and w 0xffffffff 0 |
| 28 | ret seal Trit32 w |
| 29 | end |
| 30 | |
| 31 | fn WaveDupmk w ulong to WaveDup |
| 32 | ret seal WaveDup w |
| 33 | end |
| 34 | |
| 35 | fn CreditJmk w ulong to CreditJ |
| 36 | ret seal CreditJ w |
| 37 | end |
| 38 | |
| 39 | fn GrantJmk w ulong to GrantJ |
| 40 | ret seal GrantJ w |
| 41 | end |
| 42 | |
| 43 | fn wdup_tag tag uint to WaveDup |
| 44 | u = ulong tag |
| 45 | ret WaveDupmk or shl u 32 u |
| 46 | end |
| 47 | |
| 48 | fn wdup_chk d WaveDup exp uint to bool |
| 49 | ret eq and open d 0xffffffff ulong exp |
| 50 | end |
| 51 | |
| 52 | note 1-op balanced-ternary negation across all 32 lanes via single AArch64 ROR #32. |
| 53 | fn t32neg a Trit32 to Trit32 |
| 54 | ret rotr a 32 |
| 55 | end |
| 56 | |
| 57 | note Zero-multiply 32-wide balanced-ternary wave product under constant-time SWAR. |
| 58 | fn t32mul a Trit32 b Trit32 to Trit32 |
| 59 | wa = open a |
| 60 | wb = open b |
| 61 | ws = rotr wb 32 |
| 62 | ab = and wa wb |
| 63 | as = and wa ws |
| 64 | p = or shr ab 32 and ab M32 |
| 65 | n = or shr as 32 and as M32 |
| 66 | ret Trit32mk or shl p 32 n |
| 67 | end |
| 68 | |
| 69 | note Branchless SWAR popcount on 32-bit unsigned word (zero hardware multiplies). |
| 70 | fn pop32 x uint to long |
| 71 | ensures and ge result 0 le result 32 |
| 72 | v = sub x and shr x 1 0x55555555 |
| 73 | v = add and v 0x33333333 and shr v 2 0x33333333 |
| 74 | v = and add v shr v 4 0x0f0f0f0f |
| 75 | v = add v shr v 8 |
| 76 | v = add v shr v 16 |
| 77 | ret long and v 0x3f |
| 78 | end |
| 79 | |
| 80 | note Branchless 32-trit inner product in [-32, +32]. |
| 81 | fn t32dot a Trit32 b Trit32 to long |
| 82 | ensures and ge result -32 le result 32 |
| 83 | m = open t32mul a b |
| 84 | pp = pop32 uint shr m 32 |
| 85 | nn = pop32 uint and m M32 |
| 86 | ret sub pp nn |
| 87 | end |
| 88 | |
| 89 | note Active non-zero carrier count in [0, 32]; 0 iff Dark Zero (all 32 lanes 0). |
| 90 | fn t32pow a Trit32 to long |
| 91 | ensures and ge result 0 le result 32 |
| 92 | wa = open a |
| 93 | act = or shr wa 32 and wa M32 |
| 94 | ret pop32 uint act |
| 95 | end |
| 96 | |
| 97 | note Flip carrier sign (+1 <-> -1, leaving 0 unchanged) at lane c < 32. |
| 98 | fn t32flip a Trit32 c ulong to Trit32 |
| 99 | requires lt c 32 |
| 100 | wa = open a |
| 101 | bit = shl ulong 1 c |
| 102 | p = shr wa 32 |
| 103 | n = and wa M32 |
| 104 | bp = and p bit |
| 105 | bn = and n bit |
| 106 | np = or and p xor M32 bit bn |
| 107 | nn = or and n xor M32 bit bp |
| 108 | ret Trit32mk or shl np 32 nn |
| 109 | end |
| 110 | |
| 111 | note Erase carrier to Dark Zero (0) at lane c < 32. |
| 112 | fn t32eras a Trit32 c ulong to Trit32 |
| 113 | requires lt c 32 |
| 114 | wa = open a |
| 115 | msk = xor M32 shl ulong 1 c |
| 116 | p = and shr wa 32 msk |
| 117 | n = and and wa M32 msk |
| 118 | ret Trit32mk or shl p 32 n |
| 119 | end |
| 120 | |
| 121 | note M=6 balanced-ternary encoder on [-364, +364] (3^6 = 729 states). |
| 122 | note Branchless digit split: r in {0,1,2} -> pos = r>>1, neg = (2-r)>>1. |
| 123 | fn bt6enc u long to Trit32 |
| 124 | requires and ge u -364 le u 364 |
| 125 | z = ulong add u 364 |
| 126 | p = ulong 0 |
| 127 | n = ulong 0 |
| 128 | for k ulong 6 unroll |
| 129 | r = rem z 3 |
| 130 | z = div z 3 |
| 131 | ip = shr r 1 |
| 132 | ineg = shr sub 2 r 1 |
| 133 | p = or p shl ip k |
| 134 | n = or n shl ineg k |
| 135 | end |
| 136 | ret Trit32mk or shl p 32 n |
| 137 | end |
| 138 | |
| 139 | note M=6 balanced-ternary decoder to [-364, +364]. |
| 140 | fn bt6dec_w wa ulong to long |
| 141 | requires eq and wa 0xffffffc0ffffffc0 0 |
| 142 | ensures and ge result -364 le result 364 |
| 143 | p = shr wa 32 |
| 144 | n = and wa M32 |
| 145 | acc = long 0 |
| 146 | w = long 1 |
| 147 | for k ulong 6 unroll |
| 148 | dp = long and shr p k 1 |
| 149 | dn = long and shr n k 1 |
| 150 | acc = add acc mul sub dp dn w |
| 151 | w = mul w 3 |
| 152 | end |
| 153 | ret acc |
| 154 | end |
| 155 | |
| 156 | fn bt6dec a Trit32 to long |
| 157 | ret bt6dec_w open a |
| 158 | end |
| 159 | |
| 160 | note Toroidal group addition on Z/729Z centered on [-364, +364]. |
| 161 | fn bt6tor u long v long to long |
| 162 | requires and ge u -364 le u 364 |
| 163 | requires and ge v -364 le v 364 |
| 164 | ensures and ge result -364 le result 364 |
| 165 | s = add add u v 1093 |
| 166 | ret sub rem s 729 364 |
| 167 | end |
| 168 | |
| 169 | note Constructive 3-trit (3^3 = 27 > 16) hypercube encoder for 4-bit corner m < 16. |
| 170 | fn hc3enc m ulong to Trit32 |
| 171 | requires lt m 16 |
| 172 | z = add m 6 |
| 173 | p = ulong 0 |
| 174 | n = ulong 0 |
| 175 | for k ulong 3 unroll |
| 176 | r = rem z 3 |
| 177 | z = div z 3 |
| 178 | p = or p shl shr r 1 k |
| 179 | n = or n shl shr sub 2 r 1 k |
| 180 | end |
| 181 | ret Trit32mk or shl p 32 n |
| 182 | end |
| 183 | |
| 184 | note Constructive 3-trit hypercube decoder; returns 0..15 or 99 if outside [-7, +8]. |
| 185 | fn hc3dec_w wa ulong to ulong |
| 186 | requires eq and wa 0xfffffff8fffffff8 0 |
| 187 | p = shr wa 32 |
| 188 | n = and wa M32 |
| 189 | u = sub long and p 1 long and n 1 |
| 190 | u = add u mul 3 sub long and shr p 1 1 long and shr n 1 1 |
| 191 | u = add u mul 9 sub long and shr p 2 1 long and shr n 2 1 |
| 192 | if or lt u -7 gt u 8 |
| 193 | ret 99 |
| 194 | end |
| 195 | ret ulong add u 7 |
| 196 | end |
| 197 | |
| 198 | fn hc3dec a Trit32 to ulong |
| 199 | ret hc3dec_w open a |
| 200 | end |
| 201 | |
| 202 | note Sylvester Walsh-Hadamard H16 biorthogonal wave encoder (32 codewords, d_H = 8). |
| 203 | fn h16enc sym ulong to Trit32 |
| 204 | requires lt sym 32 |
| 205 | r = and sym 15 |
| 206 | pol = shr sym 4 |
| 207 | p = ulong 0 |
| 208 | n = ulong 0 |
| 209 | for c ulong 16 |
| 210 | pc = ulong and pop32 uint and r c 1 |
| 211 | par = xor pc pol |
| 212 | ib = shl ulong 1 c |
| 213 | if eq par 0 |
| 214 | p = or p ib |
| 215 | else |
| 216 | n = or n ib |
| 217 | end |
| 218 | end |
| 219 | ret Trit32mk or shl p 32 n |
| 220 | end |
| 221 | |
| 222 | note Branchless-metric H16 decoder: corrects all 0..3 carrier corruptions (flips or erasures) |
| 223 | note and fail-closed returns -1 on >= 4-carrier corruptions or midpoint ties. |
| 224 | fn h16dec rx Trit32 to long |
| 225 | bestd = long 99 |
| 226 | bests = long -1 |
| 227 | ties = long 0 |
| 228 | for s ulong 32 |
| 229 | cw = h16enc s |
| 230 | pr = open t32mul rx cw |
| 231 | mt = pop32 uint and shr pr 32 M16 |
| 232 | d = sub 16 mt |
| 233 | if lt d bestd |
| 234 | bestd = d |
| 235 | bests = long s |
| 236 | ties = 1 |
| 237 | else |
| 238 | if eq d bestd |
| 239 | ties = add ties 1 |
| 240 | end |
| 241 | end |
| 242 | end |
| 243 | if and le bestd 3 eq ties 1 |
| 244 | ret bests |
| 245 | end |
| 246 | ret -1 |
| 247 | end |
| 248 | |
| 249 | note Pack full 32-carrier job wave from decoded H16 symbol sym < 32. |
| 250 | fn job_wave sym ulong to Trit32 |
| 251 | requires lt sym 32 |
| 252 | lo = open h16enc sym |
| 253 | hi = open h16enc xor sym 21 |
| 254 | plo = and shr lo 32 M16 |
| 255 | nlo = and lo M16 |
| 256 | phi = shl and shr hi 32 M16 16 |
| 257 | nhi = shl and hi M16 16 |
| 258 | ret Trit32mk or shl or plo phi 32 or nlo nhi |
| 259 | end |
| 260 | |
| 261 | note Deterministic 32-trit operator bank row r in 0..3. |
| 262 | fn wbank_row r ulong to Trit32 |
| 263 | requires lt r 4 |
| 264 | p = ulong 0 |
| 265 | n = ulong 0 |
| 266 | for i ulong 32 |
| 267 | rm = rem add add mul 3 r i 1 3 |
| 268 | ib = shl ulong 1 i |
| 269 | if eq rm 2 |
| 270 | p = or p ib |
| 271 | else |
| 272 | if eq rm 0 |
| 273 | n = or n ib |
| 274 | end |
| 275 | end |
| 276 | end |
| 277 | ret Trit32mk or shl p 32 n |
| 278 | end |
| 279 | |
| 280 | note Weighted harmonic inner-product sum across the 4-row balanced-ternary bank. |
| 281 | fn wave_sum jw Trit32 to long |
| 282 | ensures and ge result -1280 le result 1280 |
| 283 | d0 = t32dot wbank_row 0 jw |
| 284 | d1 = t32dot wbank_row 1 jw |
| 285 | d2 = t32dot wbank_row 2 jw |
| 286 | d3 = t32dot wbank_row 3 jw |
| 287 | acc = add d0 mul 3 d1 |
| 288 | acc = add acc mul 9 d2 |
| 289 | acc = add acc mul 27 d3 |
| 290 | ret acc |
| 291 | end |
| 292 | |
| 293 | note Scaled wave evaluation; checked signed mul traps 1 (exit 101) if scale overflows. |
| 294 | fn wave_eval jw Trit32 sc long to long |
| 295 | ws = wave_sum jw |
| 296 | ret mul sc ws |
| 297 | end |
| 298 | |
| 299 | note Two's-complement conversion of signed long to unsigned 64-bit word without trap 5. |
| 300 | fn u64_tc x long to ulong |
| 301 | if ge x 0 |
| 302 | ret ulong x |
| 303 | end |
| 304 | xp = neg add x 1 |
| 305 | ret sub 0xffffffffffffffff ulong xp |
| 306 | end |
| 307 | |
| 308 | fn i64_tc u ulong to long |
| 309 | if lt u 0x8000000000000000 |
| 310 | ret long u |
| 311 | end |
| 312 | up = sub 0xffffffffffffffff u |
| 313 | ret sub neg long up 1 |
| 314 | end |
| 315 | |
| 316 | fn wr64le b edit uchar * o ulong u ulong to void |
| 317 | for i ulong 8 unroll |
| 318 | put b add o i uchar and shr u mul i 8 0xff |
| 319 | end |
| 320 | end |
| 321 | |
| 322 | fn rd64le b view uchar * o ulong to ulong |
| 323 | u = ulong 0 |
| 324 | for i ulong 8 unroll |
| 325 | u = or u shl ulong get b add o i mul i 8 |
| 326 | end |
| 327 | ret u |
| 328 | end |
| 329 | |
| 330 | fn rd64be b view uchar * o ulong to ulong |
| 331 | u = ulong 0 |
| 332 | for i ulong 8 unroll |
| 333 | u = or shl u 8 ulong get b add o i |
| 334 | end |
| 335 | ret u |
| 336 | end |
| 337 | |
| 338 | fn wr32le b edit uchar * o ulong u uint to void |
| 339 | for i ulong 4 unroll |
| 340 | put b add o i uchar and shr u uint mul i 8 0xff |
| 341 | end |
| 342 | end |
| 343 | |
| 344 | fn rd32le b view uchar * o ulong to uint |
| 345 | u = uint 0 |
| 346 | for i ulong 4 unroll |
| 347 | u = or u shl uint get b add o i uint mul i 8 |
| 348 | end |
| 349 | ret u |
| 350 | end |
| 351 | |
| 352 | note FIPS 180-4 SHA-256 round constants K[0..63]. |
| 353 | fn wskinit k edit uint 64 to void |
| 354 | put k 0 0x428a2f98 |
| 355 | put k 1 0x71374491 |
| 356 | put k 2 0xb5c0fbcf |
| 357 | put k 3 0xe9b5dba5 |
| 358 | put k 4 0x3956c25b |
| 359 | put k 5 0x59f111f1 |
| 360 | put k 6 0x923f82a4 |
| 361 | put k 7 0xab1c5ed5 |
| 362 | put k 8 0xd807aa98 |
| 363 | put k 9 0x12835b01 |
| 364 | put k 10 0x243185be |
| 365 | put k 11 0x550c7dc3 |
| 366 | put k 12 0x72be5d74 |
| 367 | put k 13 0x80deb1fe |
| 368 | put k 14 0x9bdc06a7 |
| 369 | put k 15 0xc19bf174 |
| 370 | put k 16 0xe49b69c1 |
| 371 | put k 17 0xefbe4786 |
| 372 | put k 18 0x0fc19dc6 |
| 373 | put k 19 0x240ca1cc |
| 374 | put k 20 0x2de92c6f |
| 375 | put k 21 0x4a7484aa |
| 376 | put k 22 0x5cb0a9dc |
| 377 | put k 23 0x76f988da |
| 378 | put k 24 0x983e5152 |
| 379 | put k 25 0xa831c66d |
| 380 | put k 26 0xb00327c8 |
| 381 | put k 27 0xbf597fc7 |
| 382 | put k 28 0xc6e00bf3 |
| 383 | put k 29 0xd5a79147 |
| 384 | put k 30 0x06ca6351 |
| 385 | put k 31 0x14292967 |
| 386 | put k 32 0x27b70a85 |
| 387 | put k 33 0x2e1b2138 |
| 388 | put k 34 0x4d2c6dfc |
| 389 | put k 35 0x53380d13 |
| 390 | put k 36 0x650a7354 |
| 391 | put k 37 0x766a0abb |
| 392 | put k 38 0x81c2c92e |
| 393 | put k 39 0x92722c85 |
| 394 | put k 40 0xa2bfe8a1 |
| 395 | put k 41 0xa81a664b |
| 396 | put k 42 0xc24b8b70 |
| 397 | put k 43 0xc76c51a3 |
| 398 | put k 44 0xd192e819 |
| 399 | put k 45 0xd6990624 |
| 400 | put k 46 0xf40e3585 |
| 401 | put k 47 0x106aa070 |
| 402 | put k 48 0x19a4c116 |
| 403 | put k 49 0x1e376c08 |
| 404 | put k 50 0x2748774c |
| 405 | put k 51 0x34b0bcb5 |
| 406 | put k 52 0x391c0cb3 |
| 407 | put k 53 0x4ed8aa4a |
| 408 | put k 54 0x5b9cca4f |
| 409 | put k 55 0x682e6ff3 |
| 410 | put k 56 0x748f82ee |
| 411 | put k 57 0x78a5636f |
| 412 | put k 58 0x84c87814 |
| 413 | put k 59 0x8cc70208 |
| 414 | put k 60 0x90befffa |
| 415 | put k 61 0xa4506ceb |
| 416 | put k 62 0xbef9a3f7 |
| 417 | put k 63 0xc67178f2 |
| 418 | end |
| 419 | |
| 420 | fn wsiv s edit uint 8 to void |
| 421 | put s 0 0x6a09e667 |
| 422 | put s 1 0xbb67ae85 |
| 423 | put s 2 0x3c6ef372 |
| 424 | put s 3 0xa54ff53a |
| 425 | put s 4 0x510e527f |
| 426 | put s 5 0x9b05688c |
| 427 | put s 6 0x1f83d9ab |
| 428 | put s 7 0x5be0cd19 |
| 429 | end |
| 430 | |
| 431 | fn wssched w edit uint 64 fresh m view uchar * o ulong to void |
| 432 | q = ulong 0 |
| 433 | x = uint 0 |
| 434 | y = uint 0 |
| 435 | for i ulong 16 unroll |
| 436 | q = add o mul i 4 |
| 437 | x = uint get m q |
| 438 | x = or shl x 8 uint get m add q 1 |
| 439 | x = or shl x 8 uint get m add q 2 |
| 440 | put w i or shl x 8 uint get m add q 3 |
| 441 | end |
| 442 | for i ulong 48 unroll |
| 443 | x = get w add i 1 |
| 444 | y = get w add i 14 |
| 445 | x = add add get w i get w add i 9 xor xor rotr x 7 rotr x 18 shr x 3 |
| 446 | put w add i 16 add x xor xor rotr y 17 rotr y 19 shr y 10 |
| 447 | end |
| 448 | end |
| 449 | |
| 450 | fn wsround s edit uint 8 w view uint 64 kt view uint 64 to void |
| 451 | a = get s 0 |
| 452 | b = get s 1 |
| 453 | c = get s 2 |
| 454 | d = get s 3 |
| 455 | e = get s 4 |
| 456 | f = get s 5 |
| 457 | g = get s 6 |
| 458 | h = get s 7 |
| 459 | t = uint 0 |
| 460 | for i ulong 64 unroll |
| 461 | h = add h get kt i |
| 462 | h = add h get w i |
| 463 | t = xor rotr e 6 rotr e 11 |
| 464 | t = xor t rotr e 25 |
| 465 | h = add h t |
| 466 | h = add h xor g and e xor f g |
| 467 | d = add d h |
| 468 | t = xor rotr a 2 rotr a 13 |
| 469 | t = xor t rotr a 22 |
| 470 | h = add h t |
| 471 | t = and c or a b |
| 472 | h = add h or and a b t |
| 473 | t = h |
| 474 | h = g |
| 475 | g = f |
| 476 | f = e |
| 477 | e = d |
| 478 | d = c |
| 479 | c = b |
| 480 | b = a |
| 481 | a = t |
| 482 | end |
| 483 | put s 0 add get s 0 a |
| 484 | put s 1 add get s 1 b |
| 485 | put s 2 add get s 2 c |
| 486 | put s 3 add get s 3 d |
| 487 | put s 4 add get s 4 e |
| 488 | put s 5 add get s 5 f |
| 489 | put s 6 add get s 6 g |
| 490 | put s 7 add get s 7 h |
| 491 | end |
| 492 | |
| 493 | fn wscomp s edit uint 8 w edit uint 64 kt view uint 64 m view uchar * o ulong to void |
| 494 | wssched w m o |
| 495 | wsround s w kt |
| 496 | end |
| 497 | |
| 498 | fn wsout dg edit uchar 32 s view uint 8 to void |
| 499 | for i ulong 8 unroll |
| 500 | v = get s i |
| 501 | o = mul i 4 |
| 502 | put dg o uchar and shr v 24 0xff |
| 503 | put dg add o 1 uchar and shr v 16 0xff |
| 504 | put dg add o 2 uchar and shr v 8 0xff |
| 505 | put dg add o 3 uchar and v 0xff |
| 506 | end |
| 507 | end |
| 508 | |
| 509 | fn wsfin s edit uint 8 w edit uint 64 kt view uint 64 pad edit uchar 128 n ulong to void |
| 510 | remb = and n 63 |
| 511 | put pad remb 0x80 |
| 512 | eb = ulong 64 |
| 513 | if ge remb 56 |
| 514 | eb = 128 |
| 515 | end |
| 516 | bits = shl n 3 |
| 517 | for i ulong 8 unroll |
| 518 | put pad sub eb add i 1 uchar and shr bits mul i 8 0xff |
| 519 | end |
| 520 | wscomp s w kt view pad 0 |
| 521 | if eq eb 128 |
| 522 | wscomp s w kt view pad 64 |
| 523 | end |
| 524 | end |
| 525 | |
| 526 | note Hash arbitrary slice m (up to 64 MiB per call) into 32-byte SHA-256 digest dg. |
| 527 | fn sha256_slice m view uchar * dg edit uchar 32 to void |
| 528 | n = len m |
| 529 | s = new uint 8 0 |
| 530 | w = new uint 64 0 |
| 531 | kt = new uint 64 0 |
| 532 | pad = new uchar 128 0 |
| 533 | wsiv s |
| 534 | wskinit kt |
| 535 | nb = shr n 6 |
| 536 | for b ulong nb |
| 537 | wscomp s w kt m shl b 6 |
| 538 | end |
| 539 | remb = and n 63 |
| 540 | base = shl nb 6 |
| 541 | for i ulong remb |
| 542 | put pad i get m add base i |
| 543 | end |
| 544 | wsfin s w kt pad n |
| 545 | wsout dg s |
| 546 | end |