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 / data.om
| 1 | note SPDX-License-Identifier: AGPL-3.0-only |
| 2 | note omegallm data.om (aien-train#52): argument paths, whole-file data reads, prep (CONTRACT |
| 3 | note section 4), the splitmix64 batch rule and the add-one byte baselines. Needs math.om before it. |
| 4 | note A data file lives in two byte pools (od0 then od1), 2^25 bytes in all; a byte is read by odb. |
| 5 | const ODHALF ulong 16777216 |
| 6 | const ODCAP ulong 33554432 |
| 7 | global od0 uchar 16777216 |
| 8 | global od1 uchar 16777216 |
| 9 | global odn ulong 1 |
| 10 | global oarg uchar 4100 |
| 11 | global ochk uchar 65536 |
| 12 | global obx ulong 16384 |
| 13 | global oby ulong 16384 |
| 14 | global owb0 uchar 65536 |
| 15 | global owb1 uchar 65536 |
| 16 | global owbn ulong 2 |
| 17 | global ocnt ulong 65536 |
| 18 | global ocnu ulong 256 |
| 19 | global ocnr ulong 256 |
| 20 | global oseps uchar 16 |
| 21 | note v1 (CONTRACT-V1 section 3): the window that the platform's positioned read oprd fills; prep, |
| 22 | note the bpe commands and the windowed data reads all use it. |
| 23 | const OPWCAP ulong 2097152 |
| 24 | global opw uchar 2097152 |
| 25 | |
| 26 | note exit codes of this unit: 2 usage, 20 data file empty or too short for T, 21 data file over |
| 27 | note 2^25 bytes, 70 I/O. |
| 28 | |
| 29 | note argument i into oarg; returns its length (1 .. 4096) or exits 2. |
| 30 | fn oargp i ulong to ulong |
| 31 | r = oargr i |
| 32 | if or le r 0 gt r 4096 |
| 33 | exit 2 |
| 34 | end |
| 35 | ret ulong r |
| 36 | end |
| 37 | |
| 38 | note argv[i] as an unsigned decimal of 1 .. 19 digits. |
| 39 | fn oargu i ulong to ulong |
| 40 | n = oargp i |
| 41 | if gt n 19 |
| 42 | exit 2 |
| 43 | end |
| 44 | v = ulong 0 |
| 45 | for k ulong n |
| 46 | c = ulong get oarg k |
| 47 | if or lt c 48 gt c 57 |
| 48 | exit 2 |
| 49 | end |
| 50 | v = add mul v 10 sub c 48 |
| 51 | end |
| 52 | ret v |
| 53 | end |
| 54 | |
| 55 | fn odb i ulong to ulong |
| 56 | if lt i ODHALF |
| 57 | ret ulong get od0 i |
| 58 | end |
| 59 | ret ulong get od1 sub i ODHALF |
| 60 | end |
| 61 | |
| 62 | fn odset i ulong c ulong to void |
| 63 | if lt i ODHALF |
| 64 | put od0 i uchar c |
| 65 | else |
| 66 | put od1 sub i ODHALF uchar c |
| 67 | end |
| 68 | end |
| 69 | |
| 70 | note prep output: two buffered writers (k = 0 TRAIN, k = 1 HELD); owopen, owflush and owclose are |
| 71 | note the platform unit's (lio.om, eio.om). |
| 72 | fn owbyte k ulong c ulong to void |
| 73 | n = get owbn k |
| 74 | if ge n 65536 |
| 75 | owflush k |
| 76 | n = ulong 0 |
| 77 | end |
| 78 | if eq k 0 |
| 79 | put owb0 n uchar c |
| 80 | else |
| 81 | put owb1 n uchar c |
| 82 | end |
| 83 | put owbn k add n 1 |
| 84 | end |
| 85 | |
| 86 | note 1 when the separator <|endoftext|> starts at opw[p] (p + 13 <= n). |
| 87 | fn oissep p ulong n ulong to bool |
| 88 | if gt add p 13 n |
| 89 | ret false |
| 90 | end |
| 91 | for k ulong 13 |
| 92 | if ne get opw add p k get oseps k |
| 93 | ret false |
| 94 | end |
| 95 | end |
| 96 | ret true |
| 97 | end |
| 98 | |
| 99 | fn owsp c ulong to bool |
| 100 | ret or or eq c 9 eq c 10 or eq c 13 eq c 32 |
| 101 | end |
| 102 | |
| 103 | note Emit story opw[a, e) after trimming whitespace; an empty story is dropped. st[0..4] count |
| 104 | note stories, train stories, held stories, train bytes, held bytes. A story over 999999 bytes is |
| 105 | note refused with exit 24 (cmdprep refuses it already while scanning; every loop here is bounded). |
| 106 | fn ostory a ulong e ulong st edit ulong 8 to void |
| 107 | if gt sub e a 999999 |
| 108 | exit 24 |
| 109 | end |
| 110 | lo = a |
| 111 | hi = e |
| 112 | while lt lo hi bound 999999 |
| 113 | if not owsp ulong get opw lo |
| 114 | break |
| 115 | end |
| 116 | lo = add lo 1 |
| 117 | end |
| 118 | while gt hi lo bound 999999 |
| 119 | if not owsp ulong get opw sub hi 1 |
| 120 | break |
| 121 | end |
| 122 | hi = sub hi 1 |
| 123 | end |
| 124 | if lt lo hi |
| 125 | k = ulong 0 |
| 126 | if eq rem get st 0 10 9 |
| 127 | k = ulong 1 |
| 128 | end |
| 129 | put st 0 add get st 0 1 |
| 130 | put st add k 1 add get st add k 1 1 |
| 131 | for i ulong sub hi lo |
| 132 | owbyte k ulong get opw add lo i |
| 133 | end |
| 134 | owbyte k 0 |
| 135 | put st add k 3 add add get st add k 3 sub hi lo 1 |
| 136 | end |
| 137 | end |
| 138 | |
| 139 | note `<|endoftext|>` = 60 124 101 110 100 111 102 116 101 120 116 124 62. |
| 140 | fn oseptab to void |
| 141 | put oseps 0 60 |
| 142 | put oseps 1 124 |
| 143 | put oseps 2 101 |
| 144 | put oseps 3 110 |
| 145 | put oseps 4 100 |
| 146 | put oseps 5 111 |
| 147 | put oseps 6 102 |
| 148 | put oseps 7 116 |
| 149 | put oseps 8 101 |
| 150 | put oseps 9 120 |
| 151 | put oseps 10 116 |
| 152 | put oseps 11 124 |
| 153 | put oseps 12 62 |
| 154 | end |
| 155 | |
| 156 | note Stream window over argv[ai] (prep and the bpe commands): opw holds the file's bytes |
| 157 | note [wb, wb + wn) with opst = wb, wn, eof (1 once a refill came back short). opstart fills it from |
| 158 | note byte 0; opshift a moves opw[a, wn) to the front and refills the rest of the window. |
| 159 | global opst ulong 4 |
| 160 | fn opstart ai ulong to void |
| 161 | wn = oprd ai 0 0 OPWCAP |
| 162 | put opst 0 0 |
| 163 | put opst 1 wn |
| 164 | put opst 2 0 |
| 165 | if lt wn OPWCAP |
| 166 | put opst 2 1 |
| 167 | end |
| 168 | end |
| 169 | |
| 170 | fn opshift ai ulong a ulong to void |
| 171 | wn = get opst 1 |
| 172 | cnt = sub wn a |
| 173 | i = ulong 0 |
| 174 | for o ulong 3 |
| 175 | lim = mul add o 1 999999 |
| 176 | if gt lim cnt |
| 177 | lim = cnt |
| 178 | end |
| 179 | while lt i lim bound 999999 |
| 180 | put opw i get opw add a i |
| 181 | i = add i 1 |
| 182 | end |
| 183 | end |
| 184 | wb = add get opst 0 a |
| 185 | wn = sub wn a |
| 186 | r = oprd ai add wb wn wn sub OPWCAP wn |
| 187 | put opst 0 wb |
| 188 | put opst 1 add wn r |
| 189 | put opst 2 0 |
| 190 | if lt r sub OPWCAP wn |
| 191 | put opst 2 1 |
| 192 | end |
| 193 | end |
| 194 | |
| 195 | note prep IN TRAIN HELD (CONTRACT-V1 section 3): IN is read as a stream through the window opw. |
| 196 | note The current story starts at opw[a]; p scans. When the separator test at p needs bytes past |
| 197 | note the window, opshift moves [a, wn) to the front and refills. A story whose raw length passes |
| 198 | note 999999 is exit 24 as soon as the scan shows it (v0 refused the same story, after the same |
| 199 | note earlier stories, in ostory); so a moved story keeps at most 1000012 bytes and every refill |
| 200 | note reads at least 1097140. Each inner pass advances p by 1 or 13 or ends the scan: 999999 x 999999 |
| 201 | note passes cover any IN below 2^39 bytes. |
| 202 | fn cmdprep to void |
| 203 | if ne oargc 5 |
| 204 | exit 2 |
| 205 | end |
| 206 | oseptab |
| 207 | opstart 2 |
| 208 | wn = get opst 1 |
| 209 | eof = ne get opst 2 0 |
| 210 | owopen 0 3 |
| 211 | owopen 1 4 |
| 212 | st = new ulong 8 0 |
| 213 | a = ulong 0 |
| 214 | p = ulong 0 |
| 215 | dn = false |
| 216 | while not dn bound 999999 |
| 217 | q = ulong 0 |
| 218 | while and not dn lt q 999999 bound 999999 |
| 219 | q = add q 1 |
| 220 | if and gt add p 13 wn not eof |
| 221 | opshift 2 a |
| 222 | p = sub p a |
| 223 | a = ulong 0 |
| 224 | wn = get opst 1 |
| 225 | eof = ne get opst 2 0 |
| 226 | end |
| 227 | if ge p wn |
| 228 | dn = true |
| 229 | else |
| 230 | if eq get opw p 60 |
| 231 | if oissep p wn |
| 232 | ostory a p st |
| 233 | p = add p 13 |
| 234 | a = p |
| 235 | continue |
| 236 | end |
| 237 | end |
| 238 | p = add p 1 |
| 239 | if gt sub p a 999999 |
| 240 | exit 24 |
| 241 | end |
| 242 | end |
| 243 | end |
| 244 | end |
| 245 | ostory a wn st |
| 246 | owclose 0 |
| 247 | owclose 1 |
| 248 | oputc 80 |
| 249 | for i ulong 5 |
| 250 | oputc 32 |
| 251 | oputu get st i |
| 252 | end |
| 253 | oputc 10 |
| 254 | oflush |
| 255 | end |
| 256 | |
| 257 | note Batch for step s (CONTRACT section 4): x and y for b = 0..B-1 into obx/oby (stride T); N, the |
| 258 | note data length in tokens, is odn[0] (odopen). |
| 259 | note OMUTB is the target offset: 1 in the product, 2 in mutant b (targets shifted by one). |
| 260 | const OMUTB ulong 1 |
| 261 | fn obatch s ulong to void |
| 262 | bb = get og 6 |
| 263 | tt = get og 1 |
| 264 | n = get odn 0 |
| 265 | sd = get og 40 |
| 266 | for b ulong bb |
| 267 | o = rem smx xor sd mul add mul s bb b 11400714819323198485 sub n tt |
| 268 | obrow b o |
| 269 | end |
| 270 | end |
| 271 | |
| 272 | note v1 data file (CONTRACT-V1 section 3): argument ai holds bytes when V = 256 and u16 |
| 273 | note little-endian tokens when V > 256 (og 49 = bytes per token, og 47 = the argument). Sets |
| 274 | note odn[0] = N tokens; exit 21 on an odd byte length at V > 256, 20 when N < T + 1. Rows are read |
| 275 | note through oprd when used, so a token >= V is refused (25) only in a row actually read. |
| 276 | fn odopen ai ulong to void |
| 277 | w = ulong 1 |
| 278 | if gt get og 0 256 |
| 279 | w = 2 |
| 280 | end |
| 281 | nb = ofsz ai |
| 282 | if ne rem nb w 0 |
| 283 | exit 21 |
| 284 | end |
| 285 | put og 47 ai |
| 286 | put og 49 w |
| 287 | put odn 0 div nb w |
| 288 | if le get odn 0 get og 1 |
| 289 | exit 20 |
| 290 | end |
| 291 | end |
| 292 | |
| 293 | note token k of the window last read by odrow. OMUTE: 1 in the product (u16 little-endian); 0 in |
| 294 | note mutant e (big-endian), which the referee comparison must catch. |
| 295 | const OMUTE ulong 1 |
| 296 | fn odtk k ulong to ulong |
| 297 | if eq get og 49 1 |
| 298 | ret ulong get opw k |
| 299 | end |
| 300 | lo = ulong get opw mul k 2 |
| 301 | hi = ulong get opw add mul k 2 1 |
| 302 | if eq OMUTE 0 |
| 303 | ret or shl lo 8 hi |
| 304 | end |
| 305 | ret or shl hi 8 lo |
| 306 | end |
| 307 | |
| 308 | note read tokens o .. o + n - 1 (fewer at the end of the file; the rest read as 0) into opw; |
| 309 | note exit 25 when a token read is >= V |
| 310 | fn odrow o ulong n ulong to void |
| 311 | w = get og 49 |
| 312 | nb = mul n w |
| 313 | for i ulong nb |
| 314 | put opw i 0 |
| 315 | end |
| 316 | r = oprd get og 47 mul o w 0 nb |
| 317 | m = div r w |
| 318 | v = get og 0 |
| 319 | for k ulong m |
| 320 | if ge odtk k v |
| 321 | exit 25 |
| 322 | end |
| 323 | end |
| 324 | end |
| 325 | |
| 326 | fn obrow b ulong o ulong to void |
| 327 | tt = get og 1 |
| 328 | odrow o add tt OMUTB |
| 329 | for t ulong tt |
| 330 | put obx add mul b tt t odtk t |
| 331 | put oby add mul b tt t odtk add t OMUTB |
| 332 | end |
| 333 | end |
| 334 | |
| 335 | note base TRAIN HELD (CONTRACT section 7 and agreed reading A5): unigram and bigram add-one |
| 336 | note counts over all TRAIN bytes and all N-1 adjacent TRAIN pairs; scored on HELD targets 1..M-1. |
| 337 | fn cmdbase to void |
| 338 | if ne oargc 4 |
| 339 | exit 2 |
| 340 | end |
| 341 | n = odread 2 |
| 342 | if lt n 2 |
| 343 | exit 20 |
| 344 | end |
| 345 | for o ulong 64 |
| 346 | for i ulong 524288 |
| 347 | j = add mul o 524288 i |
| 348 | if ge j n |
| 349 | break |
| 350 | end |
| 351 | obfit j |
| 352 | end |
| 353 | end |
| 354 | m = odread 3 |
| 355 | if lt m 2 |
| 356 | exit 20 |
| 357 | end |
| 358 | su = 0.0f64 |
| 359 | sb = 0.0f64 |
| 360 | dn = f64 add n 256 |
| 361 | for o2 ulong 64 |
| 362 | for i2 ulong 524288 |
| 363 | j2 = add mul o2 524288 i2 |
| 364 | if ge j2 m |
| 365 | break |
| 366 | end |
| 367 | if gt j2 0 |
| 368 | su = add su obsu j2 dn |
| 369 | sb = add sb obsb j2 |
| 370 | end |
| 371 | end |
| 372 | end |
| 373 | fm = f64 sub m 1 |
| 374 | oputc 66 |
| 375 | oputc 32 |
| 376 | oputl rmic div div neg su fm 0.6931471805599453 |
| 377 | oputc 32 |
| 378 | oputl rmic div div neg sb fm 0.6931471805599453 |
| 379 | oputc 10 |
| 380 | oflush |
| 381 | end |
| 382 | |
| 383 | note count TRAIN byte j and the pair (j-1, j) |
| 384 | fn obfit j ulong to void |
| 385 | c = odb j |
| 386 | put ocnu c add get ocnu c 1 |
| 387 | if gt j 0 |
| 388 | pv = odb sub j 1 |
| 389 | q = add mul pv 256 c |
| 390 | put ocnt q add get ocnt q 1 |
| 391 | put ocnr pv add get ocnr pv 1 |
| 392 | end |
| 393 | end |
| 394 | |
| 395 | note log of the unigram probability (c+1)/(N+256) of HELD byte j |
| 396 | fn obsu j ulong dn f64 to f64 |
| 397 | ret flog div f64 add get ocnu odb j 1 dn |
| 398 | end |
| 399 | |
| 400 | note log of the bigram probability (c+1)/(row+256) of HELD pair (j-1, j) |
| 401 | fn obsb j ulong to f64 |
| 402 | pv = odb sub j 1 |
| 403 | q = add mul pv 256 odb j |
| 404 | ret flog div f64 add get ocnt q 1 f64 add get ocnr pv 256 |
| 405 | end |