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 / tools / omegallm / wprobe / wprobe.om
| 1 | note SPDX-License-Identifier: AGPL-3.0-only |
| 2 | note wprobe.om (DRA-187): CPU probes and greedy decoding with a reused prefix for the OLLM |
| 3 | note checkpoint. tools/omegallm/wdrive/wbuild.om appends it to ollm.om with that unit's main renamed. |
| 4 | note Hook groups may also come from a file: a last argument @PATH (see wzhread). |
| 5 | note Commands (argument 1): |
| 6 | note gen CK MERGES NN FILE [K L I A]... greedy decode (TEMPMILLI 0 of `ollm sample`) of every record |
| 7 | note probe CK MERGES FILE [K L I A]... teacher-forced answer; readout at the first divergence |
| 8 | note save CK OUT [K L I A]... rank META PROBE controls CK SEED [K L I A]... |
| 9 | note survive BASE EDIT BASE1 EDIT1 [EDIT2] rng SEED N (each described at its function) |
| 10 | note FILE: records, each ended by byte 0; a record is PROMPT, optionally byte 31 and the expected |
| 11 | note ANSWER (probe needs it). The context is token 0, the prompt's encoding, then (probe) the |
| 12 | note answer's encoding, as cmdsamp builds it; the answer starts a pre-tokenizer chunk, so the two |
| 13 | note encodings join exactly where generation would. |
| 14 | note Hook groups K L I A, at most 64 groups, L < layers; A in thousandths, at most 4000 for kinds |
| 15 | note 1 .. 3 and 100000 for kinds 4 and 5. Weight hooks (1, 2, 5) apply in their order before any record: |
| 16 | note K 1 scales the wo rows of head I of layer L (a weight edit in memory); |
| 17 | note K 2 scales w2 row I of layer L (neuron I's output, a weight edit in memory); |
| 18 | note K 3 scales head I of layer L's output O at answer rows only (the rows that predict answer |
| 19 | note tokens: the last prompt row and later), an activation intervention; |
| 20 | note K 4 (gen) subtracts A/1000 from logit I when the last context token is a digit byte (48 .. 57), |
| 21 | note a decoding adjustment; |
| 22 | note K 5 (L 0, I 0) rounds every parameter to the nearest f32: the GPU runtime's representation. |
| 23 | note The forward computes rows t0 .. CT-1 of every layer with the model.om row kernels (olayf1, |
| 24 | note fatts, fatto, olayf2), so each row gets the bits ofwd gives it; rows below t0 are reused. It |
| 25 | note needs every layer and activation in bank A (og[35] = og[38] = L), else exit 60. |
| 26 | note gen prints `G idx<TAB>text` per record (bytes 9, 10, 13, 34, 92 and below 32 escaped as \t \n |
| 27 | note \r \" \\ \xHH). probe prints `R idx np na div exp pred` and, on a divergence at answer token |
| 28 | note div, the readout of the row that predicts it with a = expected and b = predicted token: |
| 29 | note T the top 8 logits; LD logit[a] - logit[b] and the sum of the parts below (equal up to rounding); |
| 30 | note E the embedding part; H l n the part of head n of layer l; M l the part of layer l's MLP; |
| 31 | note N l f the 16 largest |neuron parts| of each MLP; A l n j p tok the 4 most attended positions of |
| 32 | note the 6 heads with the largest |part|. A part of a residual stream term x is |
| 33 | note sum_c x[c] gf[c] (wte[a][c] - wte[b][c]) / s with s the final rms of the row, so the parts sum |
| 34 | note to LD. Values print with 4 decimals. |
| 35 | note Exits: 2 usage, 60 layout, 61 file or record too long, 62 context full (T tokens); the other |
| 36 | note commands' codes are listed at their functions. |
| 37 | |
| 38 | global wzh ulong 256 |
| 39 | global wzhn ulong 1 |
| 40 | global wzs ulong 4 |
| 41 | global wzat ulong 4096 |
| 42 | global wzw f64 4096 |
| 43 | global wzu f64 4096 |
| 44 | global wzc f64 256 |
| 45 | global wzm f64 64 |
| 46 | global wzk ulong 16 |
| 47 | global wzr f64 16 |
| 48 | note rank state. wrs: 0 probe bytes, 1 cursor, 2 failure rows, 3 failures predicting ".", 4 other |
| 49 | note divergences, 5 records, 6 current record is a failure row, 7 head keys, 8 neuron keys, 9 meta end, |
| 50 | note 10 buffer end, 11 meta rows. Keys: head l*64+n, neuron l*4096+f, MLP l (l < 16). |
| 51 | global wrs ulong 16 |
| 52 | global wrm ulong 1024 |
| 53 | global wrl ulong 1024 |
| 54 | global wrx ulong 1024 |
| 55 | global wrq f64 4 |
| 56 | global wrh f64 1024 |
| 57 | global wrhl ulong 1024 |
| 58 | global wrhs ulong 1024 |
| 59 | global wrn f64 65536 |
| 60 | global wrnl ulong 65536 |
| 61 | global wrns ulong 65536 |
| 62 | global wrmm f64 16 |
| 63 | global wrmu ulong 16 |
| 64 | global wrk ulong 65536 |
| 65 | text wt0 "target " |
| 66 | text wt1 "control " |
| 67 | text wt2 " | " |
| 68 | text wt3 "digit-copy failure rows " |
| 69 | text wt4 " (pred \".\" " |
| 70 | text wt5 ", pred digit " |
| 71 | text wt6 "); other divergences " |
| 72 | text wt7 "mean logit gap " |
| 73 | text wt8 "heads by mean part (top 8):" |
| 74 | text wt9 "heads by mean part (bottom 4):" |
| 75 | text wta "MLP mean part:" |
| 76 | text wtb "neurons by mean part (most negative 32):" |
| 77 | text wtc "neurons by mean part (most positive 8):" |
| 78 | text wtd "H-control-" |
| 79 | text wte " H gen" |
| 80 | global wcr ulong 1 |
| 81 | global wmt ulong 624 |
| 82 | global wcu ulong 64 |
| 83 | global wcs ulong 4 |
| 84 | global wsf long 8 |
| 85 | global wsa f64 4 |
| 86 | global wsn ulong 2 |
| 87 | global wsb uchar 2097152 |
| 88 | |
| 89 | note argument 1: 1 gen, 2 probe, 3 save, 4 rank, 5 controls, 6 survive, 7 rng, else 0 |
| 90 | fn wzcmd to ulong |
| 91 | n = oargp 1 |
| 92 | c = get oarg 0 |
| 93 | if and eq n 3 eq c 103 |
| 94 | ret 1 |
| 95 | end |
| 96 | if and eq n 5 eq c 112 |
| 97 | ret 2 |
| 98 | end |
| 99 | if and eq n 4 eq c 115 |
| 100 | ret 3 |
| 101 | end |
| 102 | if and eq n 4 eq c 114 |
| 103 | ret 4 |
| 104 | end |
| 105 | if and eq n 8 eq c 99 |
| 106 | ret 5 |
| 107 | end |
| 108 | if and eq n 7 eq c 115 |
| 109 | ret 6 |
| 110 | end |
| 111 | if and eq n 3 eq c 114 |
| 112 | ret 7 |
| 113 | end |
| 114 | ret 0 |
| 115 | end |
| 116 | |
| 117 | fn main to int |
| 118 | if lt oargc 2 |
| 119 | exit 2 |
| 120 | end |
| 121 | c = wzcmd |
| 122 | if eq c 1 |
| 123 | wzgenc |
| 124 | ret 0 |
| 125 | end |
| 126 | if eq c 2 |
| 127 | wzprbc |
| 128 | ret 0 |
| 129 | end |
| 130 | if eq c 5 |
| 131 | wctlc |
| 132 | ret 0 |
| 133 | end |
| 134 | if eq c 6 |
| 135 | wsurvc |
| 136 | ret 0 |
| 137 | end |
| 138 | if eq c 7 |
| 139 | wrngc |
| 140 | ret 0 |
| 141 | end |
| 142 | if eq c 3 |
| 143 | wzsavec |
| 144 | ret 0 |
| 145 | end |
| 146 | if eq c 4 |
| 147 | wrankc |
| 148 | ret 0 |
| 149 | end |
| 150 | exit 2 |
| 151 | end |
| 152 | |
| 153 | note save CK OUT [K L I A]...: weight hooks (kinds 1, 2, 5) applied to CK, written to OUT with CK's |
| 154 | note header fields and optimizer state unchanged (wpatch policy 0). Refuses OUT that exists (63), an |
| 155 | note activation or decoding hook (2), and any nonfinite parameter or one over 1e6 in magnitude (64). |
| 156 | fn wzsavec to void |
| 157 | if lt oargc 4 |
| 158 | exit 2 |
| 159 | end |
| 160 | wznone 3 |
| 161 | ockload 2 |
| 162 | l = get og 5 |
| 163 | if or ne get og 35 l ne get og 38 l |
| 164 | exit 60 |
| 165 | end |
| 166 | wzhooks 4 |
| 167 | for i ulong get wzhn 0 |
| 168 | k = get wzh mul i 4 |
| 169 | if or eq k 3 eq k 4 |
| 170 | exit 2 |
| 171 | end |
| 172 | end |
| 173 | wzfin |
| 174 | ocksave 3 |
| 175 | end |
| 176 | |
| 177 | note exit 63 when the file named by argument ai exists, 70 on another open error |
| 178 | fn wznone ai ulong to void |
| 179 | n = oargp ai |
| 180 | fd = int fopen oarg 0 n 0 0 |
| 181 | if ge fd 0 |
| 182 | r = fclose fd |
| 183 | exit 63 |
| 184 | end |
| 185 | if ne fd -2 |
| 186 | exit 70 |
| 187 | end |
| 188 | end |
| 189 | |
| 190 | note exit 64 unless every parameter is finite with magnitude at most 1e6 |
| 191 | fn wzfin to void |
| 192 | n = get og 8 |
| 193 | for q ulong ocn n |
| 194 | i = mul q OCH |
| 195 | ie = oce q n |
| 196 | while lt i ie bound 999999 |
| 197 | v = owg i |
| 198 | if not le fabs v 1000000.0 |
| 199 | exit 64 |
| 200 | end |
| 201 | i = add i 1 |
| 202 | end |
| 203 | end |
| 204 | end |
| 205 | |
| 206 | note checkpoint argument 2, merges argument 3 |
| 207 | fn wzload to void |
| 208 | ockload 2 |
| 209 | obload 3 get og 0 |
| 210 | l = get og 5 |
| 211 | if or ne get og 35 l ne get og 38 l |
| 212 | exit 60 |
| 213 | end |
| 214 | end |
| 215 | |
| 216 | note hook groups from argument b on: check them, then apply the weight hooks in order |
| 217 | fn wzhooks b ulong to void |
| 218 | k = wzhread b |
| 219 | for i ulong k |
| 220 | wzhchk i |
| 221 | end |
| 222 | for i2 ulong k |
| 223 | wzhw i2 |
| 224 | end |
| 225 | end |
| 226 | |
| 227 | note hook words from argument b on into wzh and wzhn; returns the group count. When argument b is the last |
| 228 | note and starts with "@", the words come from the file named by the rest of it instead (decimals separated by |
| 229 | note any bytes up to 32, at most 189999 bytes): an exec'd argv is capped at 64 records at this pin, too few |
| 230 | note for 32 groups. Exits 2 for a word count not a multiple of 4 or over 64 groups, 61 file too long, 70 I/O. |
| 231 | fn wzhread b ulong to ulong |
| 232 | na = oargc |
| 233 | if lt na b |
| 234 | exit 2 |
| 235 | end |
| 236 | n = sub na b |
| 237 | if eq n 1 |
| 238 | if wzisat b |
| 239 | ret wzhfile b |
| 240 | end |
| 241 | end |
| 242 | if ne rem n 4 0 |
| 243 | exit 2 |
| 244 | end |
| 245 | k = div n 4 |
| 246 | if gt k 64 |
| 247 | exit 2 |
| 248 | end |
| 249 | put wzhn 0 k |
| 250 | for i ulong k |
| 251 | for j ulong 4 |
| 252 | put wzh add mul i 4 j oargu add b add mul i 4 j |
| 253 | end |
| 254 | end |
| 255 | ret k |
| 256 | end |
| 257 | |
| 258 | fn wzisat b ulong to bool |
| 259 | n = oargp b |
| 260 | ret eq get oarg 0 64 |
| 261 | end |
| 262 | |
| 263 | note the hook words of the file named by argument b after its "@", read into opw at 1900000 |
| 264 | fn wzhfile b ulong to ulong |
| 265 | n = oargp b |
| 266 | fd = int fopen oarg 1 sub n 1 0 0 |
| 267 | if lt fd 0 |
| 268 | exit 70 |
| 269 | end |
| 270 | m = ulong 0 |
| 271 | while lt m 190000 bound 999999 |
| 272 | r = pread fd opw add 1900000 m sub 190000 m long m |
| 273 | if lt r 0 |
| 274 | exit 70 |
| 275 | end |
| 276 | if eq r 0 |
| 277 | break |
| 278 | end |
| 279 | m = add m ulong r |
| 280 | end |
| 281 | c = fclose fd |
| 282 | if ge m 190000 |
| 283 | exit 61 |
| 284 | end |
| 285 | put wrs 1 1900000 |
| 286 | put wrs 10 add 1900000 m |
| 287 | w = ulong 0 |
| 288 | while lt get wrs 1 get wrs 10 bound 999999 |
| 289 | if le wrc 32 |
| 290 | wradv |
| 291 | else |
| 292 | if not wrdig |
| 293 | exit 2 |
| 294 | end |
| 295 | if ge w 256 |
| 296 | exit 2 |
| 297 | end |
| 298 | put wzh w ulong wrint |
| 299 | w = add w 1 |
| 300 | end |
| 301 | end |
| 302 | if ne rem w 4 0 |
| 303 | exit 2 |
| 304 | end |
| 305 | put wzhn 0 div w 4 |
| 306 | ret div w 4 |
| 307 | end |
| 308 | |
| 309 | fn wzhchk i ulong to void |
| 310 | q = mul i 4 |
| 311 | kd = get wzh q |
| 312 | x = get wzh add q 2 |
| 313 | a = get wzh add q 3 |
| 314 | if or lt kd 1 gt kd 5 |
| 315 | exit 2 |
| 316 | end |
| 317 | if ge get wzh add q 1 get og 5 |
| 318 | exit 2 |
| 319 | end |
| 320 | if lt kd 4 |
| 321 | if gt a 4000 |
| 322 | exit 2 |
| 323 | end |
| 324 | end |
| 325 | if gt a 100000 |
| 326 | exit 2 |
| 327 | end |
| 328 | if ge x wzhlim kd |
| 329 | exit 2 |
| 330 | end |
| 331 | end |
| 332 | |
| 333 | note the bound on a hook's index I: F neurons (kind 2), V tokens (4), 1 (5), else H heads |
| 334 | fn wzhlim kd ulong to ulong |
| 335 | if eq kd 2 |
| 336 | ret get og 4 |
| 337 | end |
| 338 | if eq kd 4 |
| 339 | ret get og 0 |
| 340 | end |
| 341 | if eq kd 5 |
| 342 | ret 1 |
| 343 | end |
| 344 | ret get og 3 |
| 345 | end |
| 346 | |
| 347 | note apply weight hook i (kinds 1 and 2) |
| 348 | fn wzhw i ulong to void |
| 349 | q = mul i 4 |
| 350 | kd = get wzh q |
| 351 | l = get wzh add q 1 |
| 352 | x = get wzh add q 2 |
| 353 | a = div f64 get wzh add q 3 1000.0 |
| 354 | d = get og 2 |
| 355 | if eq kd 1 |
| 356 | e = get og 7 |
| 357 | wzscl add opwo l mul mul x e d mul e d a |
| 358 | end |
| 359 | if eq kd 2 |
| 360 | wzscl add opw2 l mul x d d a |
| 361 | end |
| 362 | if eq kd 5 |
| 363 | wzf32 |
| 364 | end |
| 365 | end |
| 366 | |
| 367 | note every parameter rounded to the nearest f32 and back: the values the GPU runtime uploads |
| 368 | fn wzf32 to void |
| 369 | n = get og 8 |
| 370 | for q ulong ocn n |
| 371 | i = mul q OCH |
| 372 | ie = oce q n |
| 373 | while lt i ie bound 999999 |
| 374 | owp i f64 f32 owg i |
| 375 | i = add i 1 |
| 376 | end |
| 377 | end |
| 378 | end |
| 379 | |
| 380 | note decoding adjustment: every kind-4 hook subtracts A/1000 from logit[I] of TMP when the last |
| 381 | note context token is a digit (byte tokens 48 .. 57) |
| 382 | fn wzbias to void |
| 383 | b = get obx sub get og 9 1 |
| 384 | if or lt b 48 gt b 57 |
| 385 | ret |
| 386 | end |
| 387 | tm = get og 27 |
| 388 | for i ulong get wzhn 0 |
| 389 | q = mul i 4 |
| 390 | if eq get wzh q 4 |
| 391 | o = add tm get wzh add q 2 |
| 392 | v = div f64 get wzh add q 3 1000.0 |
| 393 | put oa o sub get oa o v |
| 394 | end |
| 395 | end |
| 396 | end |
| 397 | |
| 398 | note ow[i] *= a for virtual i in [o, o + n) |
| 399 | fn wzscl o ulong n ulong a f64 to void |
| 400 | for i ulong n |
| 401 | owp add o i mul owg add o i a |
| 402 | end |
| 403 | end |
| 404 | |
| 405 | note activation hook: O[t] of head n of layer l scaled by kind-3 hooks when t >= wzs[0] |
| 406 | fn wzhook l ulong n ulong t ulong to void |
| 407 | if lt t get wzs 0 |
| 408 | ret |
| 409 | end |
| 410 | for i ulong get wzhn 0 |
| 411 | if wzhmat i 3 l n |
| 412 | wzsclo l n t div f64 get wzh add mul i 4 3 1000.0 |
| 413 | end |
| 414 | end |
| 415 | end |
| 416 | |
| 417 | note hook i has kind kd, layer l and index x |
| 418 | fn wzhmat i ulong kd ulong l ulong x ulong to bool |
| 419 | q = mul i 4 |
| 420 | if ne get wzh q kd |
| 421 | ret false |
| 422 | end |
| 423 | if ne get wzh add q 1 l |
| 424 | ret false |
| 425 | end |
| 426 | ret eq get wzh add q 2 x |
| 427 | end |
| 428 | |
| 429 | fn wzsclo l ulong n ulong t ulong a f64 to void |
| 430 | e = get og 7 |
| 431 | o = add add oao l mul t get og 2 mul n e |
| 432 | for i ulong e |
| 433 | put oa add o i mul get oa add o i a |
| 434 | end |
| 435 | end |
| 436 | |
| 437 | note h[t] = wte[x[t]] + wpe[t] for t0 <= t < CT (oemb's rows) |
| 438 | fn wzemb t0 ulong to void |
| 439 | d = get og 2 |
| 440 | t = t0 |
| 441 | while lt t get og 9 bound 4096 |
| 442 | wr = mul get obx t d |
| 443 | pr = add get og 30 mul t d |
| 444 | kadd3w add oahin 0 mul t d wr pr d |
| 445 | t = add t 1 |
| 446 | end |
| 447 | end |
| 448 | |
| 449 | note olayf for rows t0 .. CT-1 only |
| 450 | fn wzlay l ulong t0 ulong to void |
| 451 | ct = get og 9 |
| 452 | t = t0 |
| 453 | while lt t ct bound 4096 |
| 454 | olayf1 l t |
| 455 | t = add t 1 |
| 456 | end |
| 457 | for n ulong get og 3 |
| 458 | t2 = t0 |
| 459 | while lt t2 ct bound 4096 |
| 460 | fatts l n t2 |
| 461 | fatto l n t2 |
| 462 | wzhook l n t2 |
| 463 | t2 = add t2 1 |
| 464 | end |
| 465 | end |
| 466 | t3 = t0 |
| 467 | while lt t3 ct bound 4096 |
| 468 | olayf2 l t3 |
| 469 | t3 = add t3 1 |
| 470 | end |
| 471 | end |
| 472 | |
| 473 | fn wzfwd t0 ulong to void |
| 474 | wzemb t0 |
| 475 | for l ulong get og 5 |
| 476 | wzlay l t0 |
| 477 | end |
| 478 | end |
| 479 | |
| 480 | note z = rms(HF[r], gf) and the logits of row r into TMP (olast's values for r = CT-1) |
| 481 | fn wzhead r ulong to void |
| 482 | d = get og 2 |
| 483 | o = mul r d |
| 484 | z = add get og 17 o |
| 485 | frms add get og 15 o get og 33 z add get og 16 r |
| 486 | klogit z get og 27 get og 0 d |
| 487 | end |
| 488 | |
| 489 | note record [p, e) of opw; s the first byte 31 in it or e |
| 490 | fn wzsep p ulong e ulong to ulong |
| 491 | s = p |
| 492 | while lt s e bound 999999 |
| 493 | if eq get opw s 31 |
| 494 | ret s |
| 495 | end |
| 496 | s = add s 1 |
| 497 | end |
| 498 | ret e |
| 499 | end |
| 500 | |
| 501 | fn wzend p ulong n ulong to ulong |
| 502 | e = p |
| 503 | while lt e n bound 999999 |
| 504 | if eq get opw e 0 |
| 505 | ret e |
| 506 | end |
| 507 | e = add e 1 |
| 508 | end |
| 509 | ret n |
| 510 | end |
| 511 | |
| 512 | note encode opw[p, q) into obtk; returns the token count |
| 513 | fn wzenc p ulong q ulong to ulong |
| 514 | n = sub q p |
| 515 | if gt n 4000 |
| 516 | exit 61 |
| 517 | end |
| 518 | for i ulong n |
| 519 | put oarg i get opw add p i |
| 520 | end |
| 521 | if eq n 0 |
| 522 | ret 0 |
| 523 | end |
| 524 | ret obpenc n |
| 525 | end |
| 526 | |
| 527 | note push obtk[0 .. n) onto the context, refusing a full window |
| 528 | fn wzpush n ulong to void |
| 529 | for i ulong n |
| 530 | if ge get og 9 get og 1 |
| 531 | exit 62 |
| 532 | end |
| 533 | ocpush get obtk i |
| 534 | end |
| 535 | end |
| 536 | |
| 537 | note the file named by argument ai into opw; returns its length |
| 538 | fn wzfile ai ulong to ulong |
| 539 | n = oprd ai 0 0 999999 |
| 540 | if ge n 999999 |
| 541 | exit 61 |
| 542 | end |
| 543 | ret n |
| 544 | end |
| 545 | |
| 546 | fn wzgenc to void |
| 547 | if lt oargc 6 |
| 548 | exit 2 |
| 549 | end |
| 550 | wzload |
| 551 | nn = oargu 4 |
| 552 | if gt nn 4096 |
| 553 | exit 2 |
| 554 | end |
| 555 | wzhooks 6 |
| 556 | n = wzfile 5 |
| 557 | p = ulong 0 |
| 558 | k = ulong 0 |
| 559 | while lt p n bound 999999 |
| 560 | e = wzend p n |
| 561 | s = wzsep p e |
| 562 | put og 9 0 |
| 563 | ocpush 0 |
| 564 | wzpush wzenc p s |
| 565 | put wzs 0 sub get og 9 1 |
| 566 | oputc 71 |
| 567 | oputc 32 |
| 568 | oputu k |
| 569 | oputc 9 |
| 570 | wzgen nn |
| 571 | oputc 10 |
| 572 | oflush |
| 573 | k = add k 1 |
| 574 | p = add e 1 |
| 575 | end |
| 576 | end |
| 577 | |
| 578 | fn wzgen nn ulong to void |
| 579 | t0 = ulong 0 |
| 580 | for g ulong nn |
| 581 | wzfwd t0 |
| 582 | wzhead sub get og 9 1 |
| 583 | wzbias |
| 584 | c = oargmax |
| 585 | if eq c 0 |
| 586 | break |
| 587 | end |
| 588 | wzputt c |
| 589 | if ge get og 9 get og 1 |
| 590 | exit 62 |
| 591 | end |
| 592 | t0 = get og 9 |
| 593 | ocpush c |
| 594 | end |
| 595 | end |
| 596 | |
| 597 | note decoded bytes of token c, escaped |
| 598 | fn wzputt c ulong to void |
| 599 | obtcap c |
| 600 | for i ulong get obon 0 |
| 601 | wzesc ulong get obo i |
| 602 | end |
| 603 | end |
| 604 | |
| 605 | fn wzesc b ulong to void |
| 606 | if eq b 9 |
| 607 | oputc 92 |
| 608 | oputc 116 |
| 609 | ret |
| 610 | end |
| 611 | if eq b 10 |
| 612 | oputc 92 |
| 613 | oputc 110 |
| 614 | ret |
| 615 | end |
| 616 | if eq b 13 |
| 617 | oputc 92 |
| 618 | oputc 114 |
| 619 | ret |
| 620 | end |
| 621 | if or eq b 34 eq b 92 |
| 622 | oputc 92 |
| 623 | oputc b |
| 624 | ret |
| 625 | end |
| 626 | if or lt b 32 eq b 127 |
| 627 | oputc 92 |
| 628 | oputc 120 |
| 629 | wzhex1 shr b 4 |
| 630 | wzhex1 and b 15 |
| 631 | ret |
| 632 | end |
| 633 | oputc b |
| 634 | end |
| 635 | |
| 636 | fn wzhex1 h ulong to void |
| 637 | if lt h 10 |
| 638 | oputc add h 48 |
| 639 | else |
| 640 | oputc add h 87 |
| 641 | end |
| 642 | end |
| 643 | |
| 644 | note x with 4 decimals |
| 645 | fn wzpf x f64 to void |
| 646 | y = x |
| 647 | if lt y 0.0 |
| 648 | oputc 45 |
| 649 | y = neg y |
| 650 | end |
| 651 | if gt y 1.0e12 |
| 652 | y = 1.0e12f64 |
| 653 | end |
| 654 | q = ulong add mul y 10000.0 0.5 |
| 655 | oputu div q 10000 |
| 656 | oputc 46 |
| 657 | f = rem q 10000 |
| 658 | oputc add 48 div f 1000 |
| 659 | oputc add 48 rem div f 100 10 |
| 660 | oputc add 48 rem div f 10 10 |
| 661 | oputc add 48 rem f 10 |
| 662 | end |
| 663 | |
| 664 | fn wzsp to void |
| 665 | oputc 32 |
| 666 | end |
| 667 | |
| 668 | fn wzprbc to void |
| 669 | if lt oargc 5 |
| 670 | exit 2 |
| 671 | end |
| 672 | wzload |
| 673 | wzhooks 5 |
| 674 | n = wzfile 4 |
| 675 | p = ulong 0 |
| 676 | k = ulong 0 |
| 677 | while lt p n bound 999999 |
| 678 | e = wzend p n |
| 679 | s = wzsep p e |
| 680 | wzprb1 k p s e |
| 681 | oflush |
| 682 | k = add k 1 |
| 683 | p = add e 1 |
| 684 | end |
| 685 | end |
| 686 | |
| 687 | note one probe record: prompt opw[p, s), answer opw[s + 1, e) |
| 688 | fn wzprb1 k ulong p ulong s ulong e ulong to void |
| 689 | if ge s e |
| 690 | exit 2 |
| 691 | end |
| 692 | put og 9 0 |
| 693 | ocpush 0 |
| 694 | wzpush wzenc p s |
| 695 | npc = get og 9 |
| 696 | na = wzenc add s 1 e |
| 697 | for i ulong na |
| 698 | put wzat i get obtk i |
| 699 | end |
| 700 | wzpush na |
| 701 | put wzs 0 sub npc 1 |
| 702 | wzfwd 0 |
| 703 | dv = wzdiv npc na |
| 704 | oputc 82 |
| 705 | wzsp |
| 706 | oputu k |
| 707 | wzsp |
| 708 | oputu npc |
| 709 | wzsp |
| 710 | oputu na |
| 711 | wzsp |
| 712 | if ge dv na |
| 713 | oputc 45 |
| 714 | oputc 49 |
| 715 | oputc 10 |
| 716 | ret |
| 717 | end |
| 718 | oputu dv |
| 719 | wzsp |
| 720 | oputu get wzat dv |
| 721 | wzsp |
| 722 | r = add sub npc 1 dv |
| 723 | wzhead r |
| 724 | b = oargmax |
| 725 | oputu b |
| 726 | oputc 10 |
| 727 | wzread r get wzat dv b |
| 728 | end |
| 729 | |
| 730 | note the first answer index whose row argmax differs from the expected token, else na |
| 731 | fn wzdiv npc ulong na ulong to ulong |
| 732 | for k ulong na |
| 733 | wzhead add sub npc 1 k |
| 734 | if ne oargmax get wzat k |
| 735 | ret k |
| 736 | end |
| 737 | end |
| 738 | ret na |
| 739 | end |
| 740 | |
| 741 | note readout of row r for expected a, predicted b (wzhead r already ran) |
| 742 | fn wzread r ulong a ulong b ulong to void |
| 743 | wztop8 |
| 744 | d = get og 2 |
| 745 | s = get oa add get og 16 r |
| 746 | g = get og 33 |
| 747 | for c ulong d |
| 748 | x = sub owg add mul a d c owg add mul b d c |
| 749 | put wzw c div mul x owg add g c s |
| 750 | end |
| 751 | tm = get og 27 |
| 752 | oputc 76 |
| 753 | oputc 68 |
| 754 | wzsp |
| 755 | wzpf sub get oa add tm a get oa add tm b |
| 756 | wzsp |
| 757 | wzpf wzdot add get og 15 mul r d |
| 758 | oputc 10 |
| 759 | oputc 69 |
| 760 | wzsp |
| 761 | wzpf wzdot add oahin 0 mul r d |
| 762 | oputc 10 |
| 763 | for l ulong get og 5 |
| 764 | wzattr l r |
| 765 | wzmlpr l r |
| 766 | end |
| 767 | wzattn r |
| 768 | end |
| 769 | |
| 770 | note sum_c oa[o + c] wzw[c] |
| 771 | fn wzdot o ulong to f64 |
| 772 | v = 0.0f64 |
| 773 | for c ulong get og 2 |
| 774 | v = add v mul get oa add o c get wzw c |
| 775 | end |
| 776 | ret v |
| 777 | end |
| 778 | |
| 779 | note sum_c ow[virtual o + c] wzw[c] |
| 780 | fn wzrdot o ulong to f64 |
| 781 | v = 0.0f64 |
| 782 | for c ulong get og 2 |
| 783 | v = add v mul owg add o c get wzw c |
| 784 | end |
| 785 | ret v |
| 786 | end |
| 787 | |
| 788 | note head parts of layer l at row r: wzu[k] = wo[k] . wzw, part n = O[r][nE ..] . wzu[nE ..] |
| 789 | fn wzattr l ulong r ulong to void |
| 790 | d = get og 2 |
| 791 | e = get og 7 |
| 792 | w = opwo l |
| 793 | for k ulong d |
| 794 | put wzu k wzrdot add w mul k d |
| 795 | end |
| 796 | o = add oao l mul r d |
| 797 | for n ulong get og 3 |
| 798 | v2 = wzhpart add o mul n e mul n e |
| 799 | put wzc add mul l get og 3 n v2 |
| 800 | oputc 72 |
| 801 | wzsp |
| 802 | oputu l |
| 803 | wzsp |
| 804 | oputu n |
| 805 | wzsp |
| 806 | wzpf v2 |
| 807 | oputc 10 |
| 808 | end |
| 809 | end |
| 810 | |
| 811 | note sum_i oa[o + i] wzu[u + i] for i < E |
| 812 | fn wzhpart o ulong u ulong to f64 |
| 813 | v = 0.0f64 |
| 814 | for i ulong get og 7 |
| 815 | v = add v mul get oa add o i get wzu add u i |
| 816 | end |
| 817 | ret v |
| 818 | end |
| 819 | |
| 820 | note MLP part of layer l at row r and its 16 largest |neuron parts| |
| 821 | fn wzmlpr l ulong r ulong to void |
| 822 | d = get og 2 |
| 823 | f = get og 4 |
| 824 | w = opw2 l |
| 825 | o = add oarl l mul r f |
| 826 | t = 0.0f64 |
| 827 | for i ulong f |
| 828 | v = mul wzrdot add w mul i d get oa add o i |
| 829 | put wzu i v |
| 830 | t = add t v |
| 831 | end |
| 832 | put wzm l t |
| 833 | oputc 77 |
| 834 | wzsp |
| 835 | oputu l |
| 836 | wzsp |
| 837 | wzpf t |
| 838 | oputc 10 |
| 839 | wztopu f 16 |
| 840 | for q ulong 16 |
| 841 | oputc 78 |
| 842 | wzsp |
| 843 | oputu l |
| 844 | wzsp |
| 845 | oputu get wzk q |
| 846 | wzsp |
| 847 | wzpf get wzr q |
| 848 | oputc 10 |
| 849 | end |
| 850 | end |
| 851 | |
| 852 | note the m (<= 16) entries of wzu[0 .. n) with the largest |value| into wzk/wzr, largest first |
| 853 | fn wztopu n ulong m ulong to void |
| 854 | for q ulong m |
| 855 | bi = ulong 0 |
| 856 | bv = neg 1.0f64 |
| 857 | for i ulong n |
| 858 | v = get wzu i |
| 859 | if lt v 0.0 |
| 860 | v = neg v |
| 861 | end |
| 862 | if and gt v bv not wztaken i q |
| 863 | bv = v |
| 864 | bi = i |
| 865 | end |
| 866 | end |
| 867 | put wzk q bi |
| 868 | put wzr q get wzu bi |
| 869 | end |
| 870 | end |
| 871 | |
| 872 | fn wztaken i ulong q ulong to bool |
| 873 | for j ulong q |
| 874 | if eq get wzk j i |
| 875 | ret true |
| 876 | end |
| 877 | end |
| 878 | ret false |
| 879 | end |
| 880 | |
| 881 | note the top 8 logits of TMP |
| 882 | fn wztop8 to void |
| 883 | v = get og 0 |
| 884 | tm = get og 27 |
| 885 | for i ulong v |
| 886 | put wzu i get oa add tm i |
| 887 | end |
| 888 | oputc 84 |
| 889 | for q ulong 8 |
| 890 | bi = ulong 0 |
| 891 | bv = neg 1.0e300f64 |
| 892 | for i2 ulong v |
| 893 | if and gt get wzu i2 bv not wztaken i2 q |
| 894 | bv = get wzu i2 |
| 895 | bi = i2 |
| 896 | end |
| 897 | end |
| 898 | put wzk q bi |
| 899 | wzsp |
| 900 | oputu bi |
| 901 | oputc 58 |
| 902 | oputc 34 |
| 903 | wzputt bi |
| 904 | oputc 34 |
| 905 | oputc 58 |
| 906 | wzpf bv |
| 907 | end |
| 908 | oputc 10 |
| 909 | end |
| 910 | |
| 911 | note attention rows of the 6 heads with the largest |part| at row r |
| 912 | fn wzattn r ulong to void |
| 913 | nh = mul get og 5 get og 3 |
| 914 | for i ulong nh |
| 915 | put wzu i get wzc i |
| 916 | end |
| 917 | wztopu nh 6 |
| 918 | for q ulong 6 |
| 919 | put wzk add 8 q get wzk q |
| 920 | end |
| 921 | for q2 ulong 6 |
| 922 | x = get wzk add 8 q2 |
| 923 | wzatt1 div x get og 3 rem x get og 3 r |
| 924 | end |
| 925 | end |
| 926 | |
| 927 | note the 4 most attended positions of head n of layer l from row r |
| 928 | fn wzatt1 l ulong n ulong r ulong to void |
| 929 | pa = opar l n r |
| 930 | for j ulong add r 1 |
| 931 | put wzu j get opa add pa j |
| 932 | end |
| 933 | wztopu add r 1 4 |
| 934 | for q ulong 4 |
| 935 | j2 = get wzk q |
| 936 | oputc 65 |
| 937 | wzsp |
| 938 | oputu l |
| 939 | wzsp |
| 940 | oputu n |
| 941 | wzsp |
| 942 | oputu j2 |
| 943 | wzsp |
| 944 | wzpf get wzr q |
| 945 | wzsp |
| 946 | oputc 34 |
| 947 | wzputt get obx j2 |
| 948 | oputc 34 |
| 949 | oputc 10 |
| 950 | end |
| 951 | end |
| 952 | |
| 953 | note rank META PROBE: META has lines `idx role length kind` (role target or control), PROBE is `wprobe |
| 954 | note probe` output for the same records. Prints PROBE's name, then the exact counts (div = -1) by role |
| 955 | note and length; then over the digit-copy failure rows (expected a digit byte 48 .. 57, predicted "." 46 |
| 956 | note or a digit): their count, the mean logit gap, the mean head parts (top 8, bottom 4), the mean MLP |
| 957 | note parts, the mean neuron parts (most negative 32, most positive 8; a neuron outside a row's top 16 |
| 958 | note counts 0 there). Means accumulate v/n in file order and ties keep first appearance, as the earlier |
| 959 | note rank.py did, so its output is reproduced. Exits: 2 usage, 61 file too long, 65 PROBE and META |
| 960 | note disagree on the record count. |
| 961 | fn wrankc to void |
| 962 | if ne oargc 4 |
| 963 | exit 2 |
| 964 | end |
| 965 | nb = oprd 3 0 0 1900000 |
| 966 | if ge nb 1900000 |
| 967 | exit 61 |
| 968 | end |
| 969 | nm = oprd 2 0 1900000 190000 |
| 970 | if ge nm 190000 |
| 971 | exit 61 |
| 972 | end |
| 973 | put wrs 1 1900000 |
| 974 | put wrs 10 add 1900000 nm |
| 975 | wrmeta |
| 976 | put wrs 1 0 |
| 977 | put wrs 10 nb |
| 978 | wrpas1 |
| 979 | wrline1 |
| 980 | wrline2 |
| 981 | if gt get wrs 2 0 |
| 982 | put wrs 1 0 |
| 983 | wrpas2 |
| 984 | wrtail |
| 985 | end |
| 986 | oflush |
| 987 | end |
| 988 | |
| 989 | note the byte at the cursor, 0 past the end |
| 990 | fn wrc to ulong |
| 991 | p = get wrs 1 |
| 992 | if ge p get wrs 10 |
| 993 | ret 0 |
| 994 | end |
| 995 | ret ulong get opw p |
| 996 | end |
| 997 | |
| 998 | fn wradv to void |
| 999 | put wrs 1 add get wrs 1 1 |
| 1000 | end |
| 1001 | |
| 1002 | fn wrdig to bool |
| 1003 | c = wrc |
| 1004 | ret and ge c 48 le c 57 |
| 1005 | end |
| 1006 | |
| 1007 | note true at a space, a newline or the end |
| 1008 | fn wrws to bool |
| 1009 | c = wrc |
| 1010 | ret or or eq c 32 eq c 10 eq c 0 |
| 1011 | end |
| 1012 | |
| 1013 | fn wrsp to void |
| 1014 | while eq wrc 32 bound 999999 |
| 1015 | wradv |
| 1016 | end |
| 1017 | end |
| 1018 | |
| 1019 | note past the next newline |
| 1020 | fn wreol to void |
| 1021 | while lt get wrs 1 get wrs 10 bound 999999 |
| 1022 | c = wrc |
| 1023 | wradv |
| 1024 | if eq c 10 |
| 1025 | ret |
| 1026 | end |
| 1027 | end |
| 1028 | end |
| 1029 | |
| 1030 | note a signed decimal integer |
| 1031 | fn wrint to long |
| 1032 | wrsp |
| 1033 | ng = eq wrc 45 |
| 1034 | if ng |
| 1035 | wradv |
| 1036 | end |
| 1037 | v = long 0 |
| 1038 | while wrdig bound 999999 |
| 1039 | v = add mul v 10 long sub wrc 48 |
| 1040 | wradv |
| 1041 | end |
| 1042 | if ng |
| 1043 | ret neg v |
| 1044 | end |
| 1045 | ret v |
| 1046 | end |
| 1047 | |
| 1048 | note a signed decimal with at most 4 decimals, in units of 1e-4 |
| 1049 | fn wrdec to long |
| 1050 | wrsp |
| 1051 | ng = eq wrc 45 |
| 1052 | if ng |
| 1053 | wradv |
| 1054 | end |
| 1055 | v = long 0 |
| 1056 | while wrdig bound 999999 |
| 1057 | v = add mul v 10 long sub wrc 48 |
| 1058 | wradv |
| 1059 | end |
| 1060 | k = ulong 0 |
| 1061 | if eq wrc 46 |
| 1062 | wradv |
| 1063 | while wrdig bound 999999 |
| 1064 | if lt k 4 |
| 1065 | v = add mul v 10 long sub wrc 48 |
| 1066 | k = add k 1 |
| 1067 | end |
| 1068 | wradv |
| 1069 | end |
| 1070 | end |
| 1071 | while lt k 4 bound 5 |
| 1072 | v = mul v 10 |
| 1073 | k = add k 1 |
| 1074 | end |
| 1075 | if ng |
| 1076 | ret neg v |
| 1077 | end |
| 1078 | ret v |
| 1079 | end |
| 1080 | |
| 1081 | note META lines: idx role length kind; role 1 target, 2 otherwise |
| 1082 | fn wrmeta to void |
| 1083 | n = ulong 0 |
| 1084 | while lt get wrs 1 get wrs 10 bound 999999 |
| 1085 | if wrdig |
| 1086 | i = ulong wrint |
| 1087 | if ge i 1024 |
| 1088 | exit 61 |
| 1089 | end |
| 1090 | wrsp |
| 1091 | r = ulong 2 |
| 1092 | if eq wrc 116 |
| 1093 | r = ulong 1 |
| 1094 | end |
| 1095 | while not wrws bound 999999 |
| 1096 | wradv |
| 1097 | end |
| 1098 | put wrm i r |
| 1099 | put wrl i ulong wrint |
| 1100 | n = add n 1 |
| 1101 | end |
| 1102 | wreol |
| 1103 | end |
| 1104 | put wrs 11 n |
| 1105 | end |
| 1106 | |
| 1107 | note pass 1: per record exact (1), digit-copy failure row (2) or other divergence (3) |
| 1108 | fn wrpas1 to void |
| 1109 | while lt get wrs 1 get wrs 10 bound 999999 |
| 1110 | if eq wrc 82 |
| 1111 | wradv |
| 1112 | wrrec1 |
| 1113 | end |
| 1114 | wreol |
| 1115 | end |
| 1116 | end |
| 1117 | |
| 1118 | fn wrrec1 to void |
| 1119 | i = ulong wrint |
| 1120 | if ge i 1024 |
| 1121 | exit 61 |
| 1122 | end |
| 1123 | a = wrint |
| 1124 | a = wrint |
| 1125 | d = wrint |
| 1126 | put wrs 5 add get wrs 5 1 |
| 1127 | if lt d 0 |
| 1128 | put wrx i 1 |
| 1129 | ret |
| 1130 | end |
| 1131 | e = wrint |
| 1132 | p = wrint |
| 1133 | dg = and ge e 48 le e 57 |
| 1134 | pd = or eq p 46 and ge p 48 le p 57 |
| 1135 | if and dg pd |
| 1136 | put wrx i 2 |
| 1137 | put wrs 2 add get wrs 2 1 |
| 1138 | if eq p 46 |
| 1139 | put wrs 3 add get wrs 3 1 |
| 1140 | end |
| 1141 | ret |
| 1142 | end |
| 1143 | put wrx i 3 |
| 1144 | put wrs 4 add get wrs 4 1 |
| 1145 | end |
| 1146 | |
| 1147 | fn wrps k ulong to void |
| 1148 | if eq k 0 |
| 1149 | for i ulong len wt0 |
| 1150 | oputc ulong get wt0 i |
| 1151 | end |
| 1152 | end |
| 1153 | if eq k 1 |
| 1154 | for i1 ulong len wt1 |
| 1155 | oputc ulong get wt1 i1 |
| 1156 | end |
| 1157 | end |
| 1158 | if eq k 2 |
| 1159 | for i2 ulong len wt2 |
| 1160 | oputc ulong get wt2 i2 |
| 1161 | end |
| 1162 | end |
| 1163 | if eq k 3 |
| 1164 | for i3 ulong len wt3 |
| 1165 | oputc ulong get wt3 i3 |
| 1166 | end |
| 1167 | end |
| 1168 | if eq k 4 |
| 1169 | for i4 ulong len wt4 |
| 1170 | oputc ulong get wt4 i4 |
| 1171 | end |
| 1172 | end |
| 1173 | if eq k 5 |
| 1174 | for i5 ulong len wt5 |
| 1175 | oputc ulong get wt5 i5 |
| 1176 | end |
| 1177 | end |
| 1178 | if eq k 6 |
| 1179 | for i6 ulong len wt6 |
| 1180 | oputc ulong get wt6 i6 |
| 1181 | end |
| 1182 | end |
| 1183 | wrps2 k |
| 1184 | end |
| 1185 | |
| 1186 | fn wrps2 k ulong to void |
| 1187 | if eq k 7 |
| 1188 | for i ulong len wt7 |
| 1189 | oputc ulong get wt7 i |
| 1190 | end |
| 1191 | end |
| 1192 | if eq k 8 |
| 1193 | for i1 ulong len wt8 |
| 1194 | oputc ulong get wt8 i1 |
| 1195 | end |
| 1196 | end |
| 1197 | if eq k 9 |
| 1198 | for i2 ulong len wt9 |
| 1199 | oputc ulong get wt9 i2 |
| 1200 | end |
| 1201 | end |
| 1202 | if eq k 10 |
| 1203 | for i3 ulong len wta |
| 1204 | oputc ulong get wta i3 |
| 1205 | end |
| 1206 | end |
| 1207 | if eq k 11 |
| 1208 | for i4 ulong len wtb |
| 1209 | oputc ulong get wtb i4 |
| 1210 | end |
| 1211 | end |
| 1212 | if eq k 12 |
| 1213 | for i5 ulong len wtc |
| 1214 | oputc ulong get wtc i5 |
| 1215 | end |
| 1216 | end |
| 1217 | end |
| 1218 | |
| 1219 | note rows of role r and length l (99: any length); ex 1 counts exact rows only |
| 1220 | fn wrcnt r ulong l ulong ex ulong to ulong |
| 1221 | k = ulong 0 |
| 1222 | for i ulong 1024 |
| 1223 | if eq get wrm i r |
| 1224 | if or eq l 99 eq get wrl i l |
| 1225 | if or eq ex 0 eq get wrx i 1 |
| 1226 | k = add k 1 |
| 1227 | end |
| 1228 | end |
| 1229 | end |
| 1230 | end |
| 1231 | ret k |
| 1232 | end |
| 1233 | |
| 1234 | note `target p/t (L6 a/b L7 c/d ...)` |
| 1235 | fn wrrole r ulong to void |
| 1236 | wrps sub r 1 |
| 1237 | oputu wrcnt r 99 1 |
| 1238 | oputc 47 |
| 1239 | oputu wrcnt r 99 0 |
| 1240 | oputc 32 |
| 1241 | oputc 40 |
| 1242 | f = true |
| 1243 | for l ulong 64 |
| 1244 | t = wrcnt r l 0 |
| 1245 | if gt t 0 |
| 1246 | if not f |
| 1247 | oputc 32 |
| 1248 | end |
| 1249 | f = false |
| 1250 | oputc 76 |
| 1251 | oputu l |
| 1252 | oputc 32 |
| 1253 | oputu wrcnt r l 1 |
| 1254 | oputc 47 |
| 1255 | oputu t |
| 1256 | end |
| 1257 | end |
| 1258 | oputc 41 |
| 1259 | end |
| 1260 | |
| 1261 | fn wrline1 to void |
| 1262 | if ne get wrs 5 get wrs 11 |
| 1263 | exit 65 |
| 1264 | end |
| 1265 | n = oargp 3 |
| 1266 | for i ulong n |
| 1267 | oputc ulong get oarg i |
| 1268 | end |
| 1269 | wrps 2 |
| 1270 | wrrole 1 |
| 1271 | wrps 2 |
| 1272 | wrrole 2 |
| 1273 | oputc 10 |
| 1274 | end |
| 1275 | |
| 1276 | fn wrline2 to void |
| 1277 | wrps 3 |
| 1278 | oputu get wrs 2 |
| 1279 | wrps 4 |
| 1280 | oputu get wrs 3 |
| 1281 | wrps 5 |
| 1282 | oputu sub get wrs 2 get wrs 3 |
| 1283 | wrps 6 |
| 1284 | oputu get wrs 4 |
| 1285 | oputc 10 |
| 1286 | end |
| 1287 | |
| 1288 | note pass 2: sums over the failure rows; wrs 6 says whether the current record is one |
| 1289 | fn wrpas2 to void |
| 1290 | n = f64 get wrs 2 |
| 1291 | while lt get wrs 1 get wrs 10 bound 999999 |
| 1292 | c = wrc |
| 1293 | if eq c 82 |
| 1294 | wradv |
| 1295 | i = ulong wrint |
| 1296 | put wrs 6 0 |
| 1297 | if eq get wrx i 2 |
| 1298 | put wrs 6 1 |
| 1299 | end |
| 1300 | end |
| 1301 | if eq get wrs 6 1 |
| 1302 | wrpart c n |
| 1303 | end |
| 1304 | wreol |
| 1305 | end |
| 1306 | end |
| 1307 | |
| 1308 | note one LD, H, M or N line of a failure row |
| 1309 | fn wrpart c ulong n f64 to void |
| 1310 | if eq c 76 |
| 1311 | wradv |
| 1312 | wradv |
| 1313 | v = wrdec |
| 1314 | put wrq 0 add get wrq 0 div f64 v 10000.0 |
| 1315 | end |
| 1316 | if eq c 72 |
| 1317 | wradv |
| 1318 | wrhadd n |
| 1319 | end |
| 1320 | if eq c 77 |
| 1321 | wradv |
| 1322 | l = ulong wrint |
| 1323 | if ge l 16 |
| 1324 | exit 61 |
| 1325 | end |
| 1326 | v2 = wrdec |
| 1327 | put wrmu l 1 |
| 1328 | put wrmm l add get wrmm l div div f64 v2 10000.0 n |
| 1329 | end |
| 1330 | if eq c 78 |
| 1331 | wradv |
| 1332 | wrnadd n |
| 1333 | end |
| 1334 | end |
| 1335 | |
| 1336 | fn wrhadd n f64 to void |
| 1337 | l = ulong wrint |
| 1338 | h = ulong wrint |
| 1339 | v = wrdec |
| 1340 | if or ge l 16 ge h 64 |
| 1341 | exit 61 |
| 1342 | end |
| 1343 | k = add mul l 64 h |
| 1344 | if eq get wrhs k 0 |
| 1345 | put wrhs k 1 |
| 1346 | put wrhl get wrs 7 k |
| 1347 | put wrs 7 add get wrs 7 1 |
| 1348 | end |
| 1349 | put wrh k add get wrh k div div f64 v 10000.0 n |
| 1350 | end |
| 1351 | |
| 1352 | fn wrnadd n f64 to void |
| 1353 | l = ulong wrint |
| 1354 | f = ulong wrint |
| 1355 | v = wrdec |
| 1356 | if or ge l 16 ge f 4096 |
| 1357 | exit 61 |
| 1358 | end |
| 1359 | k = add mul l 4096 f |
| 1360 | if eq get wrns k 0 |
| 1361 | put wrns k 1 |
| 1362 | put wrnl get wrs 8 k |
| 1363 | put wrs 8 add get wrs 8 1 |
| 1364 | end |
| 1365 | put wrn k add get wrn k div div f64 v 10000.0 n |
| 1366 | end |
| 1367 | |
| 1368 | fn wrtail to void |
| 1369 | wrps 7 |
| 1370 | wzpf div get wrq 0 f64 get wrs 2 |
| 1371 | oputc 10 |
| 1372 | wrps 8 |
| 1373 | wrsel 0 8 1 |
| 1374 | oputc 10 |
| 1375 | wrps 9 |
| 1376 | wrsel 0 4 0 |
| 1377 | oputc 10 |
| 1378 | wrps 10 |
| 1379 | for l ulong 16 |
| 1380 | if eq get wrmu l 1 |
| 1381 | oputc 32 |
| 1382 | oputu l |
| 1383 | oputc 58 |
| 1384 | wrpf3 get wrmm l |
| 1385 | end |
| 1386 | end |
| 1387 | oputc 10 |
| 1388 | wrps 11 |
| 1389 | wrsel 1 32 0 |
| 1390 | oputc 10 |
| 1391 | wrps 12 |
| 1392 | wrsel 1 8 1 |
| 1393 | oputc 10 |
| 1394 | end |
| 1395 | |
| 1396 | note the key at list position j: heads (kd 0) or neurons (kd 1) |
| 1397 | fn wrkey kd ulong j ulong to ulong |
| 1398 | if eq kd 0 |
| 1399 | ret get wrhl j |
| 1400 | end |
| 1401 | ret get wrnl j |
| 1402 | end |
| 1403 | |
| 1404 | fn wrval kd ulong k ulong to f64 |
| 1405 | if eq kd 0 |
| 1406 | ret get wrh k |
| 1407 | end |
| 1408 | ret get wrn k |
| 1409 | end |
| 1410 | |
| 1411 | note ds 1 when v ranks above b in a descending order, else in an ascending one |
| 1412 | fn wrbet v f64 b f64 ds ulong to bool |
| 1413 | if eq ds 1 |
| 1414 | ret gt v b |
| 1415 | end |
| 1416 | ret lt v b |
| 1417 | end |
| 1418 | |
| 1419 | note the untaken list position with the best value, the first on ties; nk when none |
| 1420 | fn wrbest kd ulong nk ulong ds ulong to ulong |
| 1421 | bi = nk |
| 1422 | bv = 0.0f64 |
| 1423 | for j ulong nk |
| 1424 | if eq get wrk j 0 |
| 1425 | v = wrval kd wrkey kd j |
| 1426 | if or eq bi nk wrbet v bv ds |
| 1427 | bi = j |
| 1428 | bv = v |
| 1429 | end |
| 1430 | end |
| 1431 | end |
| 1432 | ret bi |
| 1433 | end |
| 1434 | |
| 1435 | note the first k keys in order (ds 1 descending), each ` l.i:+x.xxx` |
| 1436 | fn wrsel kd ulong k ulong ds ulong to void |
| 1437 | nk = get wrs 7 |
| 1438 | w = ulong 64 |
| 1439 | if eq kd 1 |
| 1440 | nk = get wrs 8 |
| 1441 | w = ulong 4096 |
| 1442 | end |
| 1443 | for j ulong nk |
| 1444 | put wrk j 0 |
| 1445 | end |
| 1446 | for q ulong k |
| 1447 | b = wrbest kd nk ds |
| 1448 | if lt b nk |
| 1449 | put wrk b 1 |
| 1450 | key = wrkey kd b |
| 1451 | oputc 32 |
| 1452 | oputu div key w |
| 1453 | oputc 46 |
| 1454 | oputu rem key w |
| 1455 | oputc 58 |
| 1456 | wrpf3 wrval kd key |
| 1457 | end |
| 1458 | end |
| 1459 | end |
| 1460 | |
| 1461 | note x as %+.3f |
| 1462 | fn wrpf3 x f64 to void |
| 1463 | y = x |
| 1464 | if lt y 0.0 |
| 1465 | oputc 45 |
| 1466 | y = neg y |
| 1467 | else |
| 1468 | oputc 43 |
| 1469 | end |
| 1470 | q = ulong add mul y 1000.0 0.5 |
| 1471 | oputu div q 1000 |
| 1472 | oputc 46 |
| 1473 | f = rem q 1000 |
| 1474 | oputc add 48 div f 100 |
| 1475 | oputc add 48 rem div f 10 10 |
| 1476 | oputc add 48 rem f 10 |
| 1477 | end |
| 1478 | |
| 1479 | note rng SEED N: the first N 32-bit outputs of MT19937 seeded as CPython's random.Random(SEED) seeds it |
| 1480 | note (init_by_array with the one key word SEED, SEED < 2^32). The controls command draws from it. |
| 1481 | fn wrngc to void |
| 1482 | if ne oargc 4 |
| 1483 | exit 2 |
| 1484 | end |
| 1485 | wmseed oargu 2 |
| 1486 | n = oargu 3 |
| 1487 | for i ulong n |
| 1488 | oputu wmgen |
| 1489 | oputc 10 |
| 1490 | end |
| 1491 | oflush |
| 1492 | end |
| 1493 | |
| 1494 | fn wminit s ulong to void |
| 1495 | put wmt 0 s |
| 1496 | for i ulong 623 |
| 1497 | p = get wmt i |
| 1498 | put wmt add i 1 and add mul 1812433253 xor p shr p 30 add i 1 4294967295 |
| 1499 | end |
| 1500 | end |
| 1501 | |
| 1502 | note init_by_array with the key [s]; exit 2 when s needs more than 32 bits |
| 1503 | fn wmseed s ulong to void |
| 1504 | if gt s 4294967295 |
| 1505 | exit 2 |
| 1506 | end |
| 1507 | wminit 19650218 |
| 1508 | i = ulong 1 |
| 1509 | for k ulong 624 |
| 1510 | p = get wmt sub i 1 |
| 1511 | put wmt i and add xor get wmt i mul xor p shr p 30 1664525 s 4294967295 |
| 1512 | i = wmwrap add i 1 |
| 1513 | end |
| 1514 | for k2 ulong 623 |
| 1515 | p2 = get wmt sub i 1 |
| 1516 | put wmt i and sub xor get wmt i mul xor p2 shr p2 30 1566083941 i 4294967295 |
| 1517 | i = wmwrap add i 1 |
| 1518 | end |
| 1519 | put wmt 0 2147483648 |
| 1520 | put wcr 0 624 |
| 1521 | end |
| 1522 | |
| 1523 | note i, or 1 after copying mt[623] to mt[0] when i reached 624 |
| 1524 | fn wmwrap i ulong to ulong |
| 1525 | if lt i 624 |
| 1526 | ret i |
| 1527 | end |
| 1528 | put wmt 0 get wmt 623 |
| 1529 | ret 1 |
| 1530 | end |
| 1531 | |
| 1532 | note the next tempered 32-bit output |
| 1533 | fn wmgen to ulong |
| 1534 | if ge get wcr 0 624 |
| 1535 | wmtwist |
| 1536 | end |
| 1537 | i = get wcr 0 |
| 1538 | put wcr 0 add i 1 |
| 1539 | y = get wmt i |
| 1540 | y = xor y shr y 11 |
| 1541 | y = xor y and shl y 7 2636928640 |
| 1542 | y = xor y and shl y 15 4022730752 |
| 1543 | ret xor y shr y 18 |
| 1544 | end |
| 1545 | |
| 1546 | fn wmtwist to void |
| 1547 | for k ulong 624 |
| 1548 | y = or and get wmt k 2147483648 and get wmt rem add k 1 624 2147483647 |
| 1549 | v = xor get wmt rem add k 397 624 shr y 1 |
| 1550 | if eq and y 1 1 |
| 1551 | v = xor v 2567483615 |
| 1552 | end |
| 1553 | put wmt k v |
| 1554 | end |
| 1555 | put wcr 0 0 |
| 1556 | end |
| 1557 | |
| 1558 | note CPython's _randbelow(m): k = bit length of m, draw getrandbits(k) = output >> (32 - k) until < m |
| 1559 | fn wcbelow m ulong to ulong |
| 1560 | k = ulong 0 |
| 1561 | while gt shr m k 0 bound 64 |
| 1562 | k = add k 1 |
| 1563 | end |
| 1564 | for i ulong 100000 |
| 1565 | x = shr wmgen sub 32 k |
| 1566 | if lt x m |
| 1567 | ret x |
| 1568 | end |
| 1569 | end |
| 1570 | exit 66 |
| 1571 | end |
| 1572 | |
| 1573 | note controls CK SEED [K L I A]... (or @PATH): PREREG section 6 item 5. K is 1 (wo rows of head I of layer L) |
| 1574 | note or 2 (w2 row I of layer L). For each kind present (1 then 2) one unit of the same layer is drawn |
| 1575 | note per selected group (MT19937 as random.Random(SEED); a unit selected or already drawn for that kind is |
| 1576 | note redrawn), and one common scale A' per kind is solved so the Frobenius norm of the change equals |
| 1577 | note the selected one's: |A/1000 - 1| sqrt(sum of squared entries of the scaled rows), on the same side |
| 1578 | note of 1. A' is rounded to thousandths, the realised norm recomputed and required within 1%. |
| 1579 | note Prints per kind `# KIND TARGET REALISED A'`, then the arm line |
| 1580 | note `H-control-SEED H gen K L I A' ... 5 0 0 0`. |
| 1581 | note Exits: 2 usage or a selection mixing sides of 1, 60 layout, 66 norm not matched or A' above 4000. |
| 1582 | fn wctlc to void |
| 1583 | if lt oargc 5 |
| 1584 | exit 2 |
| 1585 | end |
| 1586 | ockload 2 |
| 1587 | l = get og 5 |
| 1588 | if or ne get og 35 l ne get og 38 l |
| 1589 | exit 60 |
| 1590 | end |
| 1591 | wmseed oargu 3 |
| 1592 | put wcs 3 oargu 3 |
| 1593 | g = wzhread 4 |
| 1594 | if eq g 0 |
| 1595 | exit 2 |
| 1596 | end |
| 1597 | for i ulong g |
| 1598 | wzhchk i |
| 1599 | k = get wzh mul i 4 |
| 1600 | if or lt k 1 gt k 2 |
| 1601 | exit 2 |
| 1602 | end |
| 1603 | end |
| 1604 | put wcs 0 0 |
| 1605 | wctk 1 |
| 1606 | wctk 2 |
| 1607 | wcarm |
| 1608 | oflush |
| 1609 | end |
| 1610 | |
| 1611 | note sum of squared entries of unit x of layer l: wo rows of a head (kd 1) or a w2 row (kd 2) |
| 1612 | fn wcsq kd ulong l ulong x ulong to f64 |
| 1613 | d = get og 2 |
| 1614 | o = add opw2 l mul x d |
| 1615 | n = d |
| 1616 | if eq kd 1 |
| 1617 | e = get og 7 |
| 1618 | o = add opwo l mul mul x e d |
| 1619 | n = mul e d |
| 1620 | end |
| 1621 | s = 0.0f64 |
| 1622 | for i ulong n |
| 1623 | v = owg add o i |
| 1624 | s = add s mul v v |
| 1625 | end |
| 1626 | ret s |
| 1627 | end |
| 1628 | |
| 1629 | note 1 above 1000, else 2; exit 2 when groups of kind kd disagree |
| 1630 | fn wcside kd ulong to ulong |
| 1631 | sd = ulong 0 |
| 1632 | for i ulong get wzhn 0 |
| 1633 | q = mul i 4 |
| 1634 | if eq get wzh q kd |
| 1635 | s = ulong 2 |
| 1636 | if gt get wzh add q 3 1000 |
| 1637 | s = ulong 1 |
| 1638 | end |
| 1639 | if eq sd 0 |
| 1640 | sd = s |
| 1641 | end |
| 1642 | if ne sd s |
| 1643 | exit 2 |
| 1644 | end |
| 1645 | end |
| 1646 | end |
| 1647 | ret sd |
| 1648 | end |
| 1649 | |
| 1650 | fn wctk kd ulong to void |
| 1651 | sd = wcside kd |
| 1652 | if eq sd 0 |
| 1653 | ret |
| 1654 | end |
| 1655 | tg = 0.0f64 |
| 1656 | for i ulong get wzhn 0 |
| 1657 | q = mul i 4 |
| 1658 | if eq get wzh q kd |
| 1659 | d = sub div f64 get wzh add q 3 1000.0 1.0 |
| 1660 | tg = add tg mul mul d d wcsq kd get wzh add q 1 get wzh add q 2 |
| 1661 | end |
| 1662 | end |
| 1663 | tg = fsqrt tg |
| 1664 | ss = 0.0f64 |
| 1665 | for i2 ulong get wzhn 0 |
| 1666 | q2 = mul i2 4 |
| 1667 | if eq get wzh q2 kd |
| 1668 | l = get wzh add q2 1 |
| 1669 | c = wcpick kd l |
| 1670 | ss = add ss wcsq kd l c |
| 1671 | end |
| 1672 | end |
| 1673 | wcfin kd tg fsqrt ss sd |
| 1674 | end |
| 1675 | |
| 1676 | note draw an untaken unit of layer l for kind kd and record it |
| 1677 | fn wcpick kd ulong l ulong to ulong |
| 1678 | m = get og 3 |
| 1679 | if eq kd 2 |
| 1680 | m = get og 4 |
| 1681 | end |
| 1682 | for t ulong 100000 |
| 1683 | c = wcbelow m |
| 1684 | if not wctaken kd l c |
| 1685 | n = get wcs 0 |
| 1686 | put wcu n add add shl kd 32 shl l 16 c |
| 1687 | put wcs 0 add n 1 |
| 1688 | ret c |
| 1689 | end |
| 1690 | end |
| 1691 | exit 66 |
| 1692 | end |
| 1693 | |
| 1694 | fn wctaken kd ulong l ulong c ulong to bool |
| 1695 | for i ulong get wzhn 0 |
| 1696 | if wzhmat i kd l c |
| 1697 | ret true |
| 1698 | end |
| 1699 | end |
| 1700 | key = add add shl kd 32 shl l 16 c |
| 1701 | for j ulong get wcs 0 |
| 1702 | if eq get wcu j key |
| 1703 | ret true |
| 1704 | end |
| 1705 | end |
| 1706 | ret false |
| 1707 | end |
| 1708 | |
| 1709 | note scale for kind kd from target norm tg and the drawn rows' root sum of squares r |
| 1710 | fn wcfin kd ulong tg f64 r f64 sd ulong to void |
| 1711 | x = div tg r |
| 1712 | if eq sd 2 |
| 1713 | x = neg x |
| 1714 | end |
| 1715 | y = mul 1000.0 add 1.0 x |
| 1716 | if lt y -0.5 |
| 1717 | exit 66 |
| 1718 | end |
| 1719 | a = ulong add y 0.5 |
| 1720 | if gt a 4000 |
| 1721 | exit 66 |
| 1722 | end |
| 1723 | rl = mul fabs sub div f64 a 1000.0 1.0 r |
| 1724 | if gt fabs sub rl tg mul 0.01 tg |
| 1725 | exit 66 |
| 1726 | end |
| 1727 | put wcs kd a |
| 1728 | oputc 35 |
| 1729 | oputc 32 |
| 1730 | oputu kd |
| 1731 | oputc 32 |
| 1732 | wzpf tg |
| 1733 | oputc 32 |
| 1734 | wzpf rl |
| 1735 | oputc 32 |
| 1736 | oputu a |
| 1737 | oputc 10 |
| 1738 | end |
| 1739 | |
| 1740 | fn wcarm to void |
| 1741 | for i ulong len wtd |
| 1742 | oputc ulong get wtd i |
| 1743 | end |
| 1744 | oputu get wcs 3 |
| 1745 | for i2 ulong len wte |
| 1746 | oputc ulong get wte i2 |
| 1747 | end |
| 1748 | for j ulong get wcs 0 |
| 1749 | key = get wcu j |
| 1750 | kd = shr key 32 |
| 1751 | oputc 32 |
| 1752 | oputu kd |
| 1753 | oputc 32 |
| 1754 | oputu and shr key 16 65535 |
| 1755 | oputc 32 |
| 1756 | oputu and key 65535 |
| 1757 | oputc 32 |
| 1758 | oputu get wcs kd |
| 1759 | end |
| 1760 | oputc 32 |
| 1761 | oputc 53 |
| 1762 | for q ulong 3 |
| 1763 | oputc 32 |
| 1764 | oputc 48 |
| 1765 | end |
| 1766 | oputc 10 |
| 1767 | end |
| 1768 | |
| 1769 | note survive BASE EDIT BASE1 EDIT1 [EDIT2]: PREREG section 9. Over the parameters where EDIT differs |
| 1770 | note from BASE, d0 = EDIT - BASE and d1 = EDIT1 - BASE1 (both after one training step); prints |
| 1771 | note `S changed N survival <d1,d0>/<d0,d0> maxrel max |d1 - d0| / |d0|` with 9 decimals. EDIT2 (an |
| 1772 | note edit saved under another optimizer policy) must have EDIT's parameters exactly, else exit 67. |
| 1773 | note The checkpoints stream in chunks of 32768 parameters; all must carry BASE's header word 12 (P). |
| 1774 | note Exits: 2 usage, 61 short read, 67 parameter count mismatch or EDIT2 differs, 68 nothing changed, |
| 1775 | note 70 open error. |
| 1776 | fn wsurvc to void |
| 1777 | if or lt oargc 6 gt oargc 7 |
| 1778 | exit 2 |
| 1779 | end |
| 1780 | nf = sub oargc 2 |
| 1781 | for f ulong nf |
| 1782 | n = oargp add f 2 |
| 1783 | fd = long int fopen oarg 0 n 0 0 |
| 1784 | if lt fd 0 |
| 1785 | exit 70 |
| 1786 | end |
| 1787 | put wsf f fd |
| 1788 | end |
| 1789 | p = wsp12 0 |
| 1790 | for f2 ulong nf |
| 1791 | if ne wsp12 f2 p |
| 1792 | exit 67 |
| 1793 | end |
| 1794 | end |
| 1795 | c = ulong 0 |
| 1796 | while lt c p bound 999999 |
| 1797 | m = sub p c |
| 1798 | if gt m 32768 |
| 1799 | m = ulong 32768 |
| 1800 | end |
| 1801 | for f3 ulong nf |
| 1802 | wsread f3 add 120 mul c 8 mul m 8 |
| 1803 | end |
| 1804 | for i ulong m |
| 1805 | wsone i nf |
| 1806 | end |
| 1807 | c = add c m |
| 1808 | end |
| 1809 | if eq get wsn 0 0 |
| 1810 | exit 68 |
| 1811 | end |
| 1812 | oputc 83 |
| 1813 | wzsp |
| 1814 | oputu get wsn 0 |
| 1815 | wzsp |
| 1816 | wspf9 div get wsa 0 get wsa 1 |
| 1817 | wzsp |
| 1818 | wspf9 get wsa 2 |
| 1819 | oputc 10 |
| 1820 | oflush |
| 1821 | end |
| 1822 | |
| 1823 | note header word 12 (P) of file f |
| 1824 | fn wsp12 f ulong to ulong |
| 1825 | wsread f 96 8 |
| 1826 | ret wsw f 0 |
| 1827 | end |
| 1828 | |
| 1829 | note n bytes of file f from byte pos into wsb at f * 262144 |
| 1830 | fn wsread f ulong pos ulong n ulong to void |
| 1831 | fd = int get wsf f |
| 1832 | o = mul f 262144 |
| 1833 | k = ulong 0 |
| 1834 | while lt k n bound 999999 |
| 1835 | r = pread fd wsb add o k sub n k long add pos k |
| 1836 | if le r 0 |
| 1837 | exit 61 |
| 1838 | end |
| 1839 | k = add k ulong r |
| 1840 | end |
| 1841 | end |
| 1842 | |
| 1843 | note the 8 bytes of chunk entry i of file f as a little-endian word |
| 1844 | fn wsw f ulong i ulong to ulong |
| 1845 | o = add mul f 262144 mul i 8 |
| 1846 | x = ulong 0 |
| 1847 | for k ulong 8 |
| 1848 | x = or x shl ulong get wsb add o k mul k 8 |
| 1849 | end |
| 1850 | ret x |
| 1851 | end |
| 1852 | |
| 1853 | note parameter i of the chunk of file f |
| 1854 | fn wsd f ulong i ulong to f64 |
| 1855 | ret bitcast f64 wsw f i |
| 1856 | end |
| 1857 | |
| 1858 | fn wsone i ulong nf ulong to void |
| 1859 | b = wsd 0 i |
| 1860 | e = wsd 1 i |
| 1861 | if eq nf 5 |
| 1862 | if ne wsd 4 i e |
| 1863 | exit 67 |
| 1864 | end |
| 1865 | end |
| 1866 | if ne b e |
| 1867 | d0 = sub e b |
| 1868 | d1 = sub wsd 3 i wsd 2 i |
| 1869 | put wsa 0 add get wsa 0 mul d1 d0 |
| 1870 | put wsa 1 add get wsa 1 mul d0 d0 |
| 1871 | r = div fabs sub d1 d0 fabs d0 |
| 1872 | put wsa 2 fmax get wsa 2 r |
| 1873 | put wsn 0 add get wsn 0 1 |
| 1874 | end |
| 1875 | end |
| 1876 | |
| 1877 | note x with 9 decimals |
| 1878 | fn wspf9 x f64 to void |
| 1879 | y = x |
| 1880 | if lt y 0.0 |
| 1881 | oputc 45 |
| 1882 | y = neg y |
| 1883 | end |
| 1884 | if gt y 1.0e9 |
| 1885 | y = 1.0e9f64 |
| 1886 | end |
| 1887 | q = ulong add mul y 1000000000.0 0.5 |
| 1888 | oputu div q 1000000000 |
| 1889 | oputc 46 |
| 1890 | f = rem q 1000000000 |
| 1891 | d = ulong 100000000 |
| 1892 | for k ulong 9 |
| 1893 | oputc add 48 rem div f d 10 |
| 1894 | d = div d 10 |
| 1895 | end |
| 1896 | end |