Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aien-train/blob/main/native/adtrain/mlp.om # aien-train AIEN training programme: language-model history, GB10 constraints and the route to training on the project engine. Plans and runnable checks are distinguished. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aien-train / native / adtrain / mlp.om` 409 lines ยท 7066 bytes Raw Blame History 1 note SPDX-License-Identifier: AGPL-3.0-only 2 note adtrain: an 8x4 -> 8 -> 2 relu MLP trained by full-batch gradient descent. 3 note Gradients come from the native reverse-mode tape (omega-mv native/autodiff664); 4 note nothing here is hand-derived. Unit order: metadata.om, tape.om, this file. 5 note Graph, nodes 0..16: X W1 b1 W2 b2 T MM1 BC1 ADD1 RELU MM2 BC2 ADD2 SUB MUL SUM1 SUM0. 6 note Params (58, MP order): W1[4,8] 0..31, b1[1,8] 32..39, W2[8,2] 40..55, b2[1,2] 56..57. 7 note Exits: 86 non-finite loss or parameter, 87 tape refusal, 74 file I/O. 8 global MX f32 32 9 global MT f32 16 10 global MP f32 58 11 global MG f32 58 12 global MS ulong 2 13 global MB uchar 1024 14 global MQ uchar 64 15 text MCKP "adtrain.ck" 16 text MFXP "adtrain.fix" 17 text MCKM "ADCKPT01" 18 text MFXM "ADFX" 19 20 fn mlbits x f32 to ulong 21 ret ulong bitcast uint x 22 end 23 24 note Bit-pattern finiteness test of aien-train native/ecosystem6/affine.om affinite. 25 fn mlfinite x f32 to bool 26 b = mlbits x 27 if gt b `4294967295` 28 ret false 29 end 30 ret lt and b `2147483647` `2139095040` 31 end 32 33 fn mlnext to ulong 34 x = get MS 0 35 x = xor x shl x 13 36 x = xor x shr x 7 37 x = xor x shl x 17 38 put MS 0 x 39 ret x 40 end 41 42 note Next generator value k in -8..8. 43 fn mlk to long 44 ret sub long rem mlnext 17 8 45 end 46 47 note Dyadic sample k / scale; exact in binary32. 48 fn mldy scale f32 to f32 49 ret div f32 mlk scale 50 end 51 52 note Data X, T (k/8) then W1, W2 (k/16) from one xorshift64 stream; biases 0. 53 fn mlinit seed ulong to void 54 put MS 0 seed 55 put MS 1 0 56 for i ulong 32 57 put MX i mldy 8.0f32 58 end 59 for i ulong 16 60 put MT i mldy 8.0f32 61 end 62 for i ulong 32 63 put MP i mldy 16.0f32 64 end 65 for i ulong 16 66 put MP add 40 i mldy 16.0f32 67 end 68 for i ulong 8 69 put MP add 32 i 0.0f32 70 end 71 put MP 56 0.0f32 72 put MP 57 0.0f32 73 end 74 75 fn mllr to f32 76 ret div 1.0f32 64.0f32 77 end 78 79 fn mlid got int want ulong to void 80 if ne got int want 81 exit 87 82 end 83 end 84 85 fn mlldp id ulong off ulong cnt ulong to void 86 for i ulong cnt 87 mlid tpset id i get MP add off i 0 88 end 89 end 90 91 fn mlleaf to void 92 mlid tpleaf 8 4 0 0 93 mlid tpleaf 4 8 1 1 94 mlid tpleaf 1 8 1 2 95 mlid tpleaf 8 2 1 3 96 mlid tpleaf 1 2 1 4 97 mlid tpleaf 8 2 0 5 98 for i ulong 32 99 mlid tpset 0 i get MX i 0 100 end 101 mlldp 1 0 32 102 mlldp 2 32 8 103 mlldp 3 40 16 104 mlldp 4 56 2 105 for i ulong 16 106 mlid tpset 5 i get MT i 0 107 end 108 end 109 110 note Forward pass on a fresh tape; node ids are checked against the fixed order. 111 fn mlfwd to f32 112 tpreset 113 mlleaf 114 mlid tpmm 0 1 6 115 mlid tpbcast 2 8 7 116 mlid tpadd 6 7 8 117 mlid tprelu 8 9 118 mlid tpmm 9 3 10 119 mlid tpbcast 4 8 11 120 mlid tpadd 10 11 12 121 mlid tpsub 12 5 13 122 mlid tpmul 13 13 14 123 mlid tpsum 14 1 15 124 mlid tpsum 15 0 16 125 l = tpval 16 0 126 if not mlfinite l 127 exit 86 128 end 129 ret l 130 end 131 132 note Forward + backward; gradients of the 58 parameters into MG. Returns the loss. 133 fn mlgrad to f32 134 l = mlfwd 135 mlid tpback 16 0 136 for i ulong 32 137 put MG i tpgrad 1 i 138 end 139 for i ulong 8 140 put MG add 32 i tpgrad 2 i 141 end 142 for i ulong 16 143 put MG add 40 i tpgrad 3 i 144 end 145 put MG 56 tpgrad 4 0 146 put MG 57 tpgrad 4 1 147 ret l 148 end 149 150 note p = p - round(lr * g); every parameter must stay finite. 151 fn mlupd to void 152 lr = mllr 153 for i ulong 58 154 p = sub get MP i mul lr get MG i 155 if not mlfinite p 156 exit 86 157 end 158 put MP i p 159 end 160 end 161 162 note One training step: loss at the current parameters, then the update. 163 fn mlstep to f32 164 l = mlgrad 165 mlupd 166 put MS 1 add get MS 1 1 167 ret l 168 end 169 170 note Relu pattern of ADD1 (node 8) after mlfwd: 1 where the input is > 0. 171 fn mlmask to void 172 for i ulong 64 173 put MQ i 0 174 if gt tpval 8 i 0.0f32 175 put MQ i 1 176 end 177 end 178 end 179 180 fn mlsame to bool 181 for i ulong 64 182 v = uchar 0 183 if gt tpval 8 i 0.0f32 184 v = uchar 1 185 end 186 if ne v get MQ i 187 ret false 188 end 189 end 190 ret true 191 end 192 193 fn mlput pos ulong v ulong to void 194 for i ulong 8 195 put MB add pos i uchar and shr v mul i 8 255 196 end 197 end 198 199 fn mlget pos ulong to ulong 200 v = ulong 0 201 for i ulong 8 202 v = or v shl ulong get MB add pos i mul i 8 203 end 204 ret v 205 end 206 207 fn mlput4 pos ulong v ulong to void 208 for i ulong 4 209 put MB add pos i uchar and shr v mul i 8 255 210 end 211 end 212 213 note FNV-1a 64 of the four little-endian bytes of w, continuing h. 214 fn mlfnv4 h ulong w ulong to ulong 215 for i ulong 4 216 h = mul xor h and shr w mul i 8 255 `1099511628211` 217 end 218 ret h 219 end 220 221 note Data binding: FNV-1a 64 over the 32 X and 16 T bit patterns. 222 fn mlhash to ulong 223 h = ulong `14695981039346656037` 224 for i ulong 32 225 h = mlfnv4 h mlbits get MX i 226 end 227 for i ulong 16 228 h = mlfnv4 h mlbits get MT i 229 end 230 ret h 231 end 232 233 fn mlimgfnv n ulong to ulong 234 h = ulong `14695981039346656037` 235 for i ulong n 236 h = mul xor h ulong get MB i `1099511628211` 237 end 238 ret h 239 end 240 241 note The u64 whose little-endian bytes are the text "ADCKPT01". 242 fn mlmagic to ulong 243 v = ulong 0 244 for i ulong 8 245 v = or v shl ulong get MCKM i mul i 8 246 end 247 ret v 248 end 249 250 fn mlwrfile fd int n ulong to void 251 o = ulong 0 252 while lt o n bound 1025 253 w = write fd MB o sub n o 254 if le w 0 255 exit 74 256 end 257 o = add o ulong w 258 end 259 end 260 261 note 536-byte image, u64 LE words: magic, 1, step, 58, lr bits, loss bits, 262 note data hash, 0, 58 params (f32 bits zero-extended), FNV-1a 64 of words 0..65. 263 fn cksave loss f32 to void 264 mlput 0 mlmagic 265 mlput 8 1 266 mlput 16 get MS 1 267 mlput 24 58 268 mlput 32 mlbits mllr 269 mlput 40 mlbits loss 270 mlput 48 mlhash 271 mlput 56 0 272 for i ulong 58 273 mlput add 64 mul i 8 mlbits get MP i 274 end 275 mlput 528 mlimgfnv 528 276 fd = int fopen MCKP 0 len MCKP 578 384 277 if lt fd 0 278 exit 74 279 end 280 mlwrfile fd 536 281 if ne fsync fd 0 282 exit 74 283 end 284 if ne fclose fd 0 285 exit 74 286 end 287 end 288 289 fn mlrdfile fd int to ulong 290 n = ulong 0 291 while lt n 1024 bound 1025 292 r = read fd MB n sub 1024 n 293 if lt r 0 294 exit 74 295 end 296 if eq r 0 297 ret n 298 end 299 n = add n ulong r 300 end 301 ret n 302 end 303 304 note Every structural check of an image already in MB; -1 on any failure. 305 fn ckcheck to int 306 if ne mlget 528 mlimgfnv 528 307 ret -1 308 end 309 if ne mlget 0 mlmagic 310 ret -1 311 end 312 if or ne mlget 8 1 ne mlget 24 58 313 ret -1 314 end 315 if or gt mlget 16 200 ne mlget 32 mlbits mllr 316 ret -1 317 end 318 if ne mlget 56 0 319 ret -1 320 end 321 for i ulong 58 322 w = mlget add 64 mul i 8 323 if not mlfinite bitcast f32 uint and w `4294967295` 324 ret -1 325 end 326 if gt w `4294967295` 327 ret -1 328 end 329 end 330 ret 0 331 end 332 333 note Load adtrain.ck: -1 corrupt (size, checksum, header, params), -2 data hash 334 note differs from the current X, T. A refused load changes no parameter. 335 fn ckload to int 336 fd = int fopen MCKP 0 len MCKP 0 0 337 if lt fd 0 338 ret -1 339 end 340 n = mlrdfile fd 341 if ne fclose fd 0 342 exit 74 343 end 344 if ne n 536 345 ret -1 346 end 347 st = ckcheck 348 if ne st 0 349 ret st 350 end 351 if ne mlget 48 mlhash 352 ret -2 353 end 354 for i ulong 58 355 put MP i bitcast f32 uint mlget add 64 mul i 8 356 end 357 put MS 1 mlget 16 358 ret 0 359 end 360 361 fn mlfxp pos ulong x f32 to ulong 362 mlput4 pos mlbits x 363 ret add pos 4 364 end 365 366 note Fixture for the C M21 oracle (omega-mv tests/test_omega_ad_fixture.c): 367 note u32 LE words "ADFX", 1, 8, 4, 8, 2, X W1 b1 W2 b2 T, then Y loss dW1 db1 368 note dW2 db2 from the last mlgrad: 187 words, 748 bytes. 369 fn mlfix to void 370 for i ulong 4 371 put MB i get MFXM i 372 end 373 mlput4 4 1 374 mlput4 8 8 375 mlput4 12 4 376 mlput4 16 8 377 mlput4 20 2 378 p = ulong 24 379 for i ulong 32 380 p = mlfxp p get MX i 381 end 382 for i ulong 58 383 p = mlfxp p get MP i 384 end 385 for i ulong 16 386 p = mlfxp p get MT i 387 end 388 for i ulong 16 389 p = mlfxp p tpval 12 i 390 end 391 p = mlfxp p tpval 16 0 392 for i ulong 58 393 p = mlfxp p get MG i 394 end 395 if ne p 748 396 exit 74 397 end 398 fd = int fopen MFXP 0 len MFXP 578 384 399 if lt fd 0 400 exit 74 401 end 402 mlwrfile fd 748 403 if ne fsync fd 0 404 exit 74 405 end 406 if ne fclose fd 0 407 exit 74 408 end 409 end