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 / tests / ecosystem6 / step-test.om
| 1 | note SPDX-License-Identifier: AGPL-3.0-only |
| 2 | note Independent exact rational oracle: X/8,W/16,b/128,T/128,learning rate1/32. |
| 3 | note Only the test oracle uses integer rationals. Production calls IEEE add32/mul32. |
| 4 | global C ulong 1 |
| 5 | global D uchar 24 |
| 6 | global RI long 18 |
| 7 | global RR long 4 |
| 8 | global SAV ulong 129 |
| 9 | global RNG ulong 1 |
| 10 | text PASS "NATIVE_AFFINE_FP32_PASS checks=" |
| 11 | text FAIL "NATIVE_AFFINE_FP32_FAIL check=" |
| 12 | text NL "\n" |
| 13 | |
| 14 | fn number n ulong to void |
| 15 | if eq n 0 |
| 16 | put D 0 48 |
| 17 | w = write 1 D 0 1 |
| 18 | if ne w 1 |
| 19 | exit 90 |
| 20 | end |
| 21 | ret |
| 22 | end |
| 23 | p = ulong 24 |
| 24 | while gt n 0 bound 24 |
| 25 | p = sub p 1 |
| 26 | put D p uchar add 48 rem n 10 |
| 27 | n = div n 10 |
| 28 | end |
| 29 | w = write 1 D p sub 24 p |
| 30 | if ne w long sub 24 p |
| 31 | exit 90 |
| 32 | end |
| 33 | end |
| 34 | |
| 35 | fn check ok bool to void |
| 36 | put C 0 add get C 0 1 |
| 37 | if not ok |
| 38 | w = write 1 FAIL 0 len FAIL |
| 39 | number get C 0 |
| 40 | w = write 1 NL 0 1 |
| 41 | exit 1 |
| 42 | end |
| 43 | end |
| 44 | |
| 45 | fn bits value long scale ulong to ulong |
| 46 | note Exact binary32 encoding of small dyadic test numbers; no production primitive. |
| 47 | if eq value 0 |
| 48 | ret 0 |
| 49 | end |
| 50 | sign = ulong 0 |
| 51 | if lt value 0 |
| 52 | sign = 2147483648 |
| 53 | value = neg value |
| 54 | end |
| 55 | n = ulong value |
| 56 | power = ulong 0 |
| 57 | q = n |
| 58 | while gt q 1 bound 24 |
| 59 | q = shr q 1 |
| 60 | power = add power 1 |
| 61 | end |
| 62 | check le power 23 |
| 63 | mantissa = sub shl n sub 23 power 8388608 |
| 64 | exponent = sub add power 127 scale |
| 65 | ret or sign or shl exponent 23 mantissa |
| 66 | end |
| 67 | |
| 68 | fn fixture to void |
| 69 | for i ulong 192 |
| 70 | put AD i 0 |
| 71 | end |
| 72 | for i ulong 64 |
| 73 | put AS i 0 |
| 74 | end |
| 75 | for i ulong 128 |
| 76 | put AP i 0 |
| 77 | end |
| 78 | put AL 0 0 |
| 79 | for t ulong 8 |
| 80 | rank = ulong 2 |
| 81 | rows = ulong 2 |
| 82 | cols = ulong 3 |
| 83 | count = ulong 6 |
| 84 | if or eq t 1 eq t 6 |
| 85 | rows = 3 |
| 86 | cols = 2 |
| 87 | end |
| 88 | if or eq t 3 eq t 4 |
| 89 | cols = 2 |
| 90 | count = 4 |
| 91 | end |
| 92 | if or eq t 2 eq t 7 |
| 93 | rank = 1 |
| 94 | cols = 1 |
| 95 | count = 2 |
| 96 | end |
| 97 | p = mul t 24 |
| 98 | put AD p 1 |
| 99 | put AD add p 1 1 |
| 100 | put AD add p 2 rank |
| 101 | put AD add p 3 count |
| 102 | put AD add p 5 add t 1 |
| 103 | put AD add p 6 17 |
| 104 | put AD add p 8 rows |
| 105 | put AD add p 16 cols |
| 106 | if eq rank 2 |
| 107 | put AD add p 9 cols |
| 108 | put AD add p 17 1 |
| 109 | end |
| 110 | p = mul t 8 |
| 111 | put AS p 1 |
| 112 | put AS add p 2 add t 1 |
| 113 | put AS add p 3 17 |
| 114 | put AS add p 4 1 |
| 115 | put AS add p 5 count |
| 116 | end |
| 117 | end |
| 118 | |
| 119 | fn save to void |
| 120 | for i ulong 128 |
| 121 | put SAV i get AP i |
| 122 | end |
| 123 | put SAV 128 get AL 0 |
| 124 | end |
| 125 | |
| 126 | fn unchanged to void |
| 127 | for i ulong 128 |
| 128 | check eq get SAV i get AP i |
| 129 | end |
| 130 | check eq get SAV 128 get AL 0 |
| 131 | end |
| 132 | |
| 133 | fn loadinputs to void |
| 134 | for i ulong 6 |
| 135 | put AP i bits get RI i 3 |
| 136 | put AP add 16 i bits get RI add 6 i 4 |
| 137 | end |
| 138 | for i ulong 2 |
| 139 | put AP add 32 i bits get RI add 12 i 7 |
| 140 | end |
| 141 | for i ulong 4 |
| 142 | put AP add 48 i bits get RI add 14 i 7 |
| 143 | end |
| 144 | end |
| 145 | |
| 146 | fn oracle to void |
| 147 | loss = long 0 |
| 148 | for row ulong 2 |
| 149 | for col ulong 2 |
| 150 | pos = add mul row 2 col |
| 151 | y = get RI add 12 col |
| 152 | for k ulong 3 |
| 153 | x = get RI add mul row 3 k |
| 154 | w = get RI add 6 add mul k 2 col |
| 155 | y = add y mul x w |
| 156 | end |
| 157 | check eq afget 4 pos bits y 7 |
| 158 | r = sub y get RI add 14 pos |
| 159 | put RR pos r |
| 160 | loss = add loss mul r r |
| 161 | end |
| 162 | end |
| 163 | check eq get AL 0 bits loss 14 |
| 164 | for k ulong 3 |
| 165 | for col ulong 2 |
| 166 | g = long 0 |
| 167 | for row ulong 2 |
| 168 | r = get RR add mul row 2 col |
| 169 | x = get RI add mul row 3 k |
| 170 | g = add g mul r x |
| 171 | end |
| 172 | pos = add mul k 2 col |
| 173 | check eq afget 6 pos bits g 9 |
| 174 | updated = sub mul get RI add 6 pos 1024 g |
| 175 | check eq afget 1 pos bits updated 14 |
| 176 | end |
| 177 | end |
| 178 | for col ulong 2 |
| 179 | g = add get RR col get RR add 2 col |
| 180 | check eq afget 7 col bits g 6 |
| 181 | updated = sub mul get RI add 12 col 16 g |
| 182 | check eq afget 2 col bits updated 11 |
| 183 | end |
| 184 | for row ulong 2 |
| 185 | for k ulong 3 |
| 186 | g = long 0 |
| 187 | for col ulong 2 |
| 188 | r = get RR add mul row 2 col |
| 189 | w = get RI add 6 add mul k 2 col |
| 190 | g = add g mul r w |
| 191 | end |
| 192 | pos = add mul row 3 k |
| 193 | check eq afget 5 pos bits g 10 |
| 194 | end |
| 195 | end |
| 196 | note Inputs and targets remain byte-identical after a successful step. |
| 197 | for i ulong 6 |
| 198 | check eq afget 0 i bits get RI i 3 |
| 199 | end |
| 200 | for i ulong 4 |
| 201 | check eq afget 3 i bits get RI add 14 i 7 |
| 202 | end |
| 203 | end |
| 204 | |
| 205 | fn direct to void |
| 206 | fixture |
| 207 | check eq afvalidate 0 |
| 208 | note X rows(1,-2,3),(2,0,-1); W(.5,-.25),(1,.75),(-.5,2). |
| 209 | put RI 0 8 |
| 210 | put RI 1 -16 |
| 211 | put RI 2 24 |
| 212 | put RI 3 16 |
| 213 | put RI 4 0 |
| 214 | put RI 5 -8 |
| 215 | put RI 6 8 |
| 216 | put RI 7 -4 |
| 217 | put RI 8 16 |
| 218 | put RI 9 12 |
| 219 | put RI 10 -8 |
| 220 | put RI 11 32 |
| 221 | put RI 12 16 |
| 222 | put RI 13 -64 |
| 223 | put RI 14 32 |
| 224 | put RI 15 128 |
| 225 | put RI 16 -128 |
| 226 | put RI 17 64 |
| 227 | loadinputs |
| 228 | check eq afstep 1023410176 0 |
| 229 | oracle |
| 230 | note Independently hand-calculated loss36.46875, not merely decreasing loss. |
| 231 | check eq get AL 0 1108467712 |
| 232 | end |
| 233 | |
| 234 | fn refusals to void |
| 235 | fixture |
| 236 | put AD 32 2 |
| 237 | put AD 33 3 |
| 238 | put AD 40 3 |
| 239 | save |
| 240 | check eq afstep 1023410176 -21 |
| 241 | unchanged |
| 242 | fixture |
| 243 | put AS 11 18 |
| 244 | save |
| 245 | check eq afstep 1023410176 -5 |
| 246 | unchanged |
| 247 | fixture |
| 248 | put AD 29 1 |
| 249 | put AS 10 1 |
| 250 | save |
| 251 | check eq afstep 1023410176 -20 |
| 252 | unchanged |
| 253 | fixture |
| 254 | put AP 0 2139095040 |
| 255 | save |
| 256 | check eq afstep 1023410176 -22 |
| 257 | unchanged |
| 258 | fixture |
| 259 | put AP 0 2143289344 |
| 260 | save |
| 261 | check eq afstep 1023410176 -22 |
| 262 | unchanged |
| 263 | fixture |
| 264 | put AP 0 4294967296 |
| 265 | save |
| 266 | check eq afstep 1023410176 -22 |
| 267 | unchanged |
| 268 | fixture |
| 269 | save |
| 270 | check eq afstep 3170893824 -22 |
| 271 | unchanged |
| 272 | fixture |
| 273 | put AP 0 2139095039 |
| 274 | put AP 16 1073741824 |
| 275 | save |
| 276 | check eq afstep 1023410176 -23 |
| 277 | unchanged |
| 278 | end |
| 279 | |
| 280 | fn next to long |
| 281 | n = add mul get RNG 0 1664525 1013904223 |
| 282 | n = and n 4294967295 |
| 283 | put RNG 0 n |
| 284 | ret sub long rem shr n 12 17 8 |
| 285 | end |
| 286 | |
| 287 | fn rounding to void |
| 288 | note Smallest subnormal through actual forward and input derivative, loss underflows. |
| 289 | fixture |
| 290 | put AP 0 1 |
| 291 | put AP 16 1065353216 |
| 292 | check eq afstep 0 0 |
| 293 | check eq afget 4 0 1 |
| 294 | check eq afget 5 0 2 |
| 295 | check eq afget 6 0 0 |
| 296 | check eq afget 7 0 2 |
| 297 | check eq get AL 0 0 |
| 298 | note Half a subnormal rounds to even zero. |
| 299 | fixture |
| 300 | put AP 0 1 |
| 301 | put AP 16 1056964608 |
| 302 | check eq afstep 0 0 |
| 303 | check eq afget 4 0 0 |
| 304 | note 1 + 2^-24 rounds to even1; 1 + 3*2^-24 rounds upward two ulps. |
| 305 | fixture |
| 306 | put AP 0 1065353216 |
| 307 | put AP 16 1065353216 |
| 308 | put AP 32 864026624 |
| 309 | check eq afstep 0 0 |
| 310 | check eq afget 4 0 1065353216 |
| 311 | check eq afget 1 0 1065353216 |
| 312 | fixture |
| 313 | put AP 0 1065353216 |
| 314 | put AP 16 1065353216 |
| 315 | put AP 32 876609536 |
| 316 | check eq afstep 0 0 |
| 317 | check eq afget 4 0 1065353218 |
| 318 | end |
| 319 | |
| 320 | fn corpus to void |
| 321 | put RNG 0 303660 |
| 322 | for trial ulong 4096 |
| 323 | fixture |
| 324 | for i ulong 18 |
| 325 | put RI i next |
| 326 | end |
| 327 | loadinputs |
| 328 | check eq afstep 1023410176 0 |
| 329 | oracle |
| 330 | end |
| 331 | end |
| 332 | |
| 333 | fn main to int |
| 334 | direct |
| 335 | refusals |
| 336 | rounding |
| 337 | corpus |
| 338 | w = write 1 PASS 0 len PASS |
| 339 | number get C 0 |
| 340 | w = write 1 NL 0 1 |
| 341 | if ne w 1 |
| 342 | ret 90 |
| 343 | end |
| 344 | ret 0 |
| 345 | end |