omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / tests / autodiff664 / tape-test.om
| 1 | note SPDX-License-Identifier: AGPL-3.0-only |
| 2 | note Native tape tests. Expected values come from separate test code paths: |
| 3 | note a test-local E1 tile, a sequential sum, an f64 widening reference and |
| 4 | note IEEE identities. No expected value is a hand-entered constant. |
| 5 | global QC ulong 1 |
| 6 | global QD uchar 24 |
| 7 | global QR ulong 1 |
| 8 | global QL f32 32 |
| 9 | global QF f32 4 |
| 10 | text QPASS "NATIVE_TAPE_PASS checks=" |
| 11 | text QFAIL "NATIVE_TAPE_FAIL check=" |
| 12 | text QNL "\n" |
| 13 | |
| 14 | fn qnum n ulong to void |
| 15 | if eq n 0 |
| 16 | put QD 0 48 |
| 17 | w = write 1 QD 0 1 |
| 18 | ret |
| 19 | end |
| 20 | p = ulong 24 |
| 21 | while gt n 0 bound 24 |
| 22 | p = sub p 1 |
| 23 | put QD p uchar add 48 rem n 10 |
| 24 | n = div n 10 |
| 25 | end |
| 26 | w = write 1 QD p sub 24 p |
| 27 | if ne w long sub 24 p |
| 28 | exit 90 |
| 29 | end |
| 30 | end |
| 31 | |
| 32 | fn qck ok bool to void |
| 33 | put QC 0 add get QC 0 1 |
| 34 | if not ok |
| 35 | w = write 1 QFAIL 0 len QFAIL |
| 36 | qnum get QC 0 |
| 37 | w = write 1 QNL 0 1 |
| 38 | exit 1 |
| 39 | end |
| 40 | end |
| 41 | |
| 42 | fn qbits x f32 to uint |
| 43 | ret bitcast uint x |
| 44 | end |
| 45 | |
| 46 | fn qeq x f32 y f32 to bool |
| 47 | ret eq qbits x qbits y |
| 48 | end |
| 49 | |
| 50 | fn qnext to ulong |
| 51 | x = get QR 0 |
| 52 | x = xor x shl x 13 |
| 53 | x = xor x shr x 7 |
| 54 | x = xor x shl x 17 |
| 55 | put QR 0 x |
| 56 | ret x |
| 57 | end |
| 58 | |
| 59 | note Random binary32 in +-[1,2) with a random 23-bit fraction, so products round. |
| 60 | fn qrand to f32 |
| 61 | r = qnext |
| 62 | one = qbits 1.0f32 |
| 63 | mask = sub shl ulong 1 23 ulong 1 |
| 64 | b = or one uint and r mask |
| 65 | if ne and r 4294967296 0 |
| 66 | b = or b shl uint 1 31 |
| 67 | end |
| 68 | ret bitcast f32 b |
| 69 | end |
| 70 | |
| 71 | fn qpos to f32 |
| 72 | ret fabs qrand |
| 73 | end |
| 74 | |
| 75 | note Test-local E1 tile over QL[0..n). |
| 76 | fn qtree n ulong to f32 |
| 77 | for i ulong sub 32 n |
| 78 | put QL add n i neg 0.0f32 |
| 79 | end |
| 80 | w = ulong 32 |
| 81 | for s ulong 5 |
| 82 | w = shr w 1 |
| 83 | for i ulong w |
| 84 | put QL i add get QL i get QL add i w |
| 85 | end |
| 86 | end |
| 87 | ret get QL 0 |
| 88 | end |
| 89 | |
| 90 | fn qseq n ulong to f32 |
| 91 | s = neg 0.0f32 |
| 92 | for i ulong n |
| 93 | s = add s get QL i |
| 94 | end |
| 95 | ret s |
| 96 | end |
| 97 | |
| 98 | fn qfill id int to void |
| 99 | n = ulong id |
| 100 | for i ulong mul tprows n tpcols n |
| 101 | st = tpset n i qrand |
| 102 | end |
| 103 | end |
| 104 | |
| 105 | fn qfillp id int to void |
| 106 | n = ulong id |
| 107 | for i ulong mul tprows n tpcols n |
| 108 | st = tpset n i qpos |
| 109 | end |
| 110 | end |
| 111 | |
| 112 | note L = sum0(sum1(x * s)): seeds grad(x) with s exactly. |
| 113 | fn qloss x int s int to int |
| 114 | m = tpmul ulong x ulong s |
| 115 | r = tpsum ulong m 1 |
| 116 | ret tpsum ulong r 0 |
| 117 | end |
| 118 | |
| 119 | fn trefuse to void |
| 120 | tpreset |
| 121 | a = tpleaf 2 3 1 |
| 122 | b = tpleaf 3 2 1 |
| 123 | qck eq a 0 |
| 124 | qck eq b 1 |
| 125 | qck eq tpadd ulong a ulong b -32 |
| 126 | qck eq tpmm ulong a ulong a -32 |
| 127 | qck eq tpleaf 5 13 1 -30 |
| 128 | qck eq tpleaf 0 3 1 -30 |
| 129 | qck eq tpsum ulong a 2 -32 |
| 130 | qck eq tpbcast ulong a 4 -32 |
| 131 | qck eq tpset ulong a 6 1.0f32 -32 |
| 132 | qck eq tpset 9 0 1.0f32 -32 |
| 133 | qck eq get TK 0 2 |
| 134 | k1 = tpleaf 1 33 1 |
| 135 | k2 = tpleaf 33 1 1 |
| 136 | qck eq tpmm ulong k1 ulong k2 -30 |
| 137 | qck eq get TK 0 4 |
| 138 | end |
| 139 | |
| 140 | fn tcap to void |
| 141 | tpreset |
| 142 | for i ulong 32 |
| 143 | qck eq tpleaf 1 1 1 int i |
| 144 | end |
| 145 | qck eq tpleaf 1 1 1 -30 |
| 146 | qck eq get TK 0 32 |
| 147 | end |
| 148 | |
| 149 | note An op on a node whose allocator snapshot is not live fails in M20 emcheck. |
| 150 | fn tstale to void |
| 151 | tpreset |
| 152 | a = tpleaf 1 2 1 |
| 153 | b = tpleaf 1 2 1 |
| 154 | put TS mul ulong b 8 0 |
| 155 | st = tpadd ulong a ulong b |
| 156 | qck lt st 0 |
| 157 | qck ne st -32 |
| 158 | qck eq get TK 0 2 |
| 159 | put TS mul ulong b 8 1 |
| 160 | qck eq tpadd ulong a ulong b 2 |
| 161 | end |
| 162 | |
| 163 | fn qmmref a ulong b ulong i ulong j ulong to f32 |
| 164 | k = tpcols a |
| 165 | nn = tpcols b |
| 166 | for q ulong k |
| 167 | x = tpval a add mul i k q |
| 168 | y = tpval b add mul q nn j |
| 169 | put QL q mul x y |
| 170 | end |
| 171 | ret qtree k |
| 172 | end |
| 173 | |
| 174 | fn qgaref s ulong b ulong i ulong q ulong to f32 |
| 175 | nn = tpcols s |
| 176 | for j ulong nn |
| 177 | x = tpval s add mul i nn j |
| 178 | y = tpval b add mul q nn j |
| 179 | put QL j mul x y |
| 180 | end |
| 181 | ret qtree nn |
| 182 | end |
| 183 | |
| 184 | fn qgbref a ulong s ulong q ulong j ulong to f32 |
| 185 | k = tpcols a |
| 186 | nn = tpcols s |
| 187 | for i ulong tprows a |
| 188 | x = tpval a add mul i k q |
| 189 | y = tpval s add mul i nn j |
| 190 | put QL i mul x y |
| 191 | end |
| 192 | ret qtree tprows a |
| 193 | end |
| 194 | |
| 195 | fn tmm to void |
| 196 | tpreset |
| 197 | a = ulong tpleaf 3 5 1 |
| 198 | b = ulong tpleaf 5 4 1 |
| 199 | s = ulong tpleaf 3 4 0 |
| 200 | qfill int a |
| 201 | qfill int b |
| 202 | qfill int s |
| 203 | p = ulong tpmm a b |
| 204 | root = qloss int p int s |
| 205 | qck ge root 0 |
| 206 | qck eq tpback ulong root 0 |
| 207 | for i ulong 3 |
| 208 | for j ulong 4 |
| 209 | qck qeq tpval p add mul i 4 j qmmref a b i j |
| 210 | end |
| 211 | end |
| 212 | for i ulong 3 |
| 213 | for q ulong 5 |
| 214 | qck qeq tpgrad a add mul i 5 q qgaref s b i q |
| 215 | end |
| 216 | end |
| 217 | for q ulong 5 |
| 218 | for j ulong 4 |
| 219 | qck qeq tpgrad b add mul q 4 j qgbref a s q j |
| 220 | end |
| 221 | end |
| 222 | qck not tphasg s |
| 223 | end |
| 224 | |
| 225 | note The tape's dot product must follow the E1 tile, not a sequential sum. |
| 226 | fn torder to void |
| 227 | differ = ulong 0 |
| 228 | for t ulong 64 |
| 229 | tpreset |
| 230 | a = ulong tpleaf 1 20 0 |
| 231 | b = ulong tpleaf 20 1 0 |
| 232 | qfill int a |
| 233 | qfill int b |
| 234 | p = ulong tpmm a b |
| 235 | for q ulong 20 |
| 236 | put QL q mul tpval a q tpval b q |
| 237 | end |
| 238 | want = qtree 20 |
| 239 | for q ulong 20 |
| 240 | put QL q mul tpval a q tpval b q |
| 241 | end |
| 242 | seq = qseq 20 |
| 243 | qck qeq tpval p 0 want |
| 244 | if not qeq want seq |
| 245 | differ = add differ 1 |
| 246 | end |
| 247 | end |
| 248 | qck gt differ 0 |
| 249 | end |
| 250 | |
| 251 | fn qnan to f32 |
| 252 | ret bitcast f32 or shl uint 255 23 shl uint 1 22 |
| 253 | end |
| 254 | |
| 255 | fn trelu to void |
| 256 | tpreset |
| 257 | x = ulong tpleaf 1 5 1 |
| 258 | s = ulong tpleaf 1 5 0 |
| 259 | qfillp int s |
| 260 | st = tpset x 0 neg 1.0f32 |
| 261 | st = tpset x 1 neg 0.0f32 |
| 262 | st = tpset x 2 0.0f32 |
| 263 | st = tpset x 3 1.0f32 |
| 264 | st = tpset x 4 qnan |
| 265 | r = ulong tprelu x |
| 266 | root = qloss int r int s |
| 267 | qck eq tpback ulong root 0 |
| 268 | for i ulong 5 |
| 269 | if eq i 3 |
| 270 | qck qeq tpval r i 1.0f32 |
| 271 | qck qeq tpgrad x i tpval s i |
| 272 | else |
| 273 | qck eq qbits tpval r i 0 |
| 274 | qck eq qbits tpgrad x i 0 |
| 275 | end |
| 276 | end |
| 277 | end |
| 278 | |
| 279 | fn tbcast to void |
| 280 | tpreset |
| 281 | b = ulong tpleaf 1 3 1 |
| 282 | s = ulong tpleaf 4 3 0 |
| 283 | qfill int b |
| 284 | qfill int s |
| 285 | bb = ulong tpbcast b 4 |
| 286 | for i ulong 4 |
| 287 | for j ulong 3 |
| 288 | qck qeq tpval bb add mul i 3 j tpval b j |
| 289 | end |
| 290 | end |
| 291 | root = qloss int bb int s |
| 292 | qck eq tpback ulong root 0 |
| 293 | for j ulong 3 |
| 294 | for i ulong 4 |
| 295 | put QL i tpval s add mul i 3 j |
| 296 | end |
| 297 | qck qeq tpgrad b j qtree 4 |
| 298 | end |
| 299 | end |
| 300 | |
| 301 | note x*x and x+x: second contribution is added to the stored first one. |
| 302 | fn tfanin to void |
| 303 | tpreset |
| 304 | x = ulong tpleaf 2 2 1 |
| 305 | qfill int x |
| 306 | m = ulong tpmul x x |
| 307 | r = ulong tpsum m 1 |
| 308 | root = ulong tpsum r 0 |
| 309 | qck eq tpback root 0 |
| 310 | for i ulong 4 |
| 311 | v = tpval x i |
| 312 | qck qeq tpgrad x i add v v |
| 313 | end |
| 314 | tpreset |
| 315 | y = ulong tpleaf 1 3 1 |
| 316 | s = ulong tpleaf 1 3 0 |
| 317 | qfill int y |
| 318 | st = tpset s 0 neg 0.0f32 |
| 319 | st = tpset s 1 qrand |
| 320 | st = tpset s 2 qrand |
| 321 | d = ulong tpadd y y |
| 322 | root = ulong qloss int d int s |
| 323 | qck eq tpback root 0 |
| 324 | for i ulong 3 |
| 325 | w = tpval s i |
| 326 | qck qeq tpgrad y i add w w |
| 327 | end |
| 328 | qck eq qbits tpgrad y 0 qbits neg 0.0f32 |
| 329 | end |
| 330 | |
| 331 | fn tsub to void |
| 332 | tpreset |
| 333 | a = ulong tpleaf 2 3 1 |
| 334 | b = ulong tpleaf 2 3 1 |
| 335 | s = ulong tpleaf 2 3 0 |
| 336 | qfill int a |
| 337 | qfill int b |
| 338 | qfill int s |
| 339 | d = ulong tpsub a b |
| 340 | for i ulong 6 |
| 341 | qck qeq tpval d i sub tpval a i tpval b i |
| 342 | end |
| 343 | root = ulong qloss int d int s |
| 344 | qck eq tpback root 0 |
| 345 | for i ulong 6 |
| 346 | qck qeq tpgrad a i tpval s i |
| 347 | qck qeq tpgrad b i neg tpval s i |
| 348 | end |
| 349 | end |
| 350 | |
| 351 | fn tsums to void |
| 352 | tpreset |
| 353 | x = ulong tpleaf 3 4 0 |
| 354 | qfill int x |
| 355 | r1 = ulong tpsum x 1 |
| 356 | c1 = ulong tpsum r1 0 |
| 357 | c0 = ulong tpsum x 0 |
| 358 | r0 = ulong tpsum c0 1 |
| 359 | for i ulong 3 |
| 360 | for j ulong 4 |
| 361 | put QL j tpval x add mul i 4 j |
| 362 | end |
| 363 | put QF i qtree 4 |
| 364 | end |
| 365 | for i ulong 3 |
| 366 | qck qeq tpval r1 i get QF i |
| 367 | put QL i get QF i |
| 368 | end |
| 369 | qck qeq tpval c1 0 qtree 3 |
| 370 | for j ulong 4 |
| 371 | for i ulong 3 |
| 372 | put QL i tpval x add mul i 4 j |
| 373 | end |
| 374 | qck qeq tpval c0 j qtree 3 |
| 375 | end |
| 376 | for j ulong 4 |
| 377 | put QL j tpval c0 j |
| 378 | end |
| 379 | qck qeq tpval r0 0 qtree 4 |
| 380 | end |
| 381 | |
| 382 | fn tstate to void |
| 383 | tpreset |
| 384 | x = ulong tpleaf 1 2 1 |
| 385 | qck eq tpback x -33 |
| 386 | tpreset |
| 387 | z = ulong tpleaf 1 1 0 |
| 388 | qck eq tpback z -33 |
| 389 | tpreset |
| 390 | y = ulong tpleaf 1 1 1 |
| 391 | qck eq tpback y 0 |
| 392 | qck eq tpback y -31 |
| 393 | qck eq tpleaf 1 1 1 -31 |
| 394 | qck qeq tpgrad y 0 1.0f32 |
| 395 | end |
| 396 | |
| 397 | note No contraction: a*b + c with c = -round(a*b) is exactly +0 unfused, |
| 398 | note while the f64 widening of the exact product leaves the rounding error. |
| 399 | fn qfma a f32 b f32 c f32 to f32 |
| 400 | ret add mul a b c |
| 401 | end |
| 402 | |
| 403 | fn tnofma to void |
| 404 | seen = ulong 0 |
| 405 | for t ulong 64 |
| 406 | a = qrand |
| 407 | b = qrand |
| 408 | put QF 0 mul a b |
| 409 | c = neg get QF 0 |
| 410 | got = qfma a b c |
| 411 | wide = f32 add mul f64 a f64 b f64 c |
| 412 | qck eq qbits got 0 |
| 413 | if ne qbits wide 0 |
| 414 | seen = add seen 1 |
| 415 | end |
| 416 | end |
| 417 | qck gt seen 0 |
| 418 | end |
| 419 | |
| 420 | note Signed-zero padding: every reduction of n < 32 inputs that are all -0 |
| 421 | note must give -0 (bits 0x80000000), because the pad lanes are -0 and |
| 422 | note -0 + -0 = -0. A +0 pad gives +0 (-0 + +0 = +0 under round to nearest). |
| 423 | note Random values cannot see the pad, so the checks above do not catch it. |
| 424 | fn qnz x f32 to bool |
| 425 | ret eq qbits x qbits neg 0.0f32 |
| 426 | end |
| 427 | |
| 428 | fn qfilln id int to void |
| 429 | n = ulong id |
| 430 | for i ulong mul tprows n tpcols n |
| 431 | st = tpset n i neg 0.0f32 |
| 432 | end |
| 433 | end |
| 434 | |
| 435 | fn tnzsum to void |
| 436 | qck eq qbits neg 0.0f32 shl uint 1 31 |
| 437 | qck ne qbits 0.0f32 qbits neg 0.0f32 |
| 438 | tpreset |
| 439 | x = ulong tpleaf 3 5 0 |
| 440 | qfilln int x |
| 441 | r1 = ulong tpsum x 1 |
| 442 | c0 = ulong tpsum x 0 |
| 443 | t = ulong tpsum r1 0 |
| 444 | for i ulong 3 |
| 445 | qck qnz tpval r1 i |
| 446 | end |
| 447 | for j ulong 5 |
| 448 | qck qnz tpval c0 j |
| 449 | end |
| 450 | qck qnz tpval t 0 |
| 451 | end |
| 452 | |
| 453 | note Matmul forward over k and its backward reductions over j (gA) and i (gB). |
| 454 | note -0 times a positive value is -0; s = -0 makes the seeded gradient -0. |
| 455 | fn tnzmm to void |
| 456 | tpreset |
| 457 | a = ulong tpleaf 2 3 1 |
| 458 | b = ulong tpleaf 3 4 1 |
| 459 | s = ulong tpleaf 2 4 0 |
| 460 | qfilln int a |
| 461 | qfillp int b |
| 462 | qfilln int s |
| 463 | p = ulong tpmm a b |
| 464 | root = ulong qloss int p int s |
| 465 | qck eq tpback root 0 |
| 466 | for i ulong 8 |
| 467 | qck qnz tpval p i |
| 468 | end |
| 469 | for i ulong 6 |
| 470 | qck qnz tpgrad a i |
| 471 | end |
| 472 | tpreset |
| 473 | a = ulong tpleaf 2 3 1 |
| 474 | b = ulong tpleaf 3 4 1 |
| 475 | s = ulong tpleaf 2 4 0 |
| 476 | qfillp int a |
| 477 | qfillp int b |
| 478 | qfilln int s |
| 479 | p = ulong tpmm a b |
| 480 | root = ulong qloss int p int s |
| 481 | qck eq tpback root 0 |
| 482 | for i ulong 6 |
| 483 | qck qnz tpgrad a i |
| 484 | end |
| 485 | for i ulong 12 |
| 486 | qck qnz tpgrad b i |
| 487 | end |
| 488 | end |
| 489 | |
| 490 | note Broadcast backward sums g over rows with the E1 tile. |
| 491 | fn tnzbc to void |
| 492 | tpreset |
| 493 | b = ulong tpleaf 1 3 1 |
| 494 | s = ulong tpleaf 4 3 0 |
| 495 | qfillp int b |
| 496 | qfilln int s |
| 497 | bb = ulong tpbcast b 4 |
| 498 | root = ulong qloss int bb int s |
| 499 | qck eq tpback root 0 |
| 500 | for j ulong 3 |
| 501 | qck qnz tpgrad b j |
| 502 | end |
| 503 | end |
| 504 | |
| 505 | fn main to int |
| 506 | put QR 0 88172645463325252 |
| 507 | trefuse |
| 508 | tcap |
| 509 | tstale |
| 510 | tmm |
| 511 | torder |
| 512 | trelu |
| 513 | tbcast |
| 514 | tfanin |
| 515 | tsub |
| 516 | tsums |
| 517 | tstate |
| 518 | tnofma |
| 519 | tnzsum |
| 520 | tnzmm |
| 521 | tnzbc |
| 522 | w = write 1 QPASS 0 len QPASS |
| 523 | qnum get QC 0 |
| 524 | w = write 1 QNL 0 1 |
| 525 | ret 0 |
| 526 | end |