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 / native / autodiff664 / tape.om
| 1 | note SPDX-License-Identifier: AGPL-3.0-only |
| 2 | note Bounded native reverse-mode tape over M20 metadata-v1 (omega-mv #664). |
| 3 | note Values are IEEE binary32. Rank 2 only, at most 32 nodes, 64 elements per node. |
| 4 | note Every reduced axis is at most 32 long: one E1 tile, lanes padded with -0.0, |
| 5 | note lane deltas 16,8,4,2,1 (src/omega_numeric_reduce.c cpu_sum_like order). |
| 6 | note Backward mirrors src/autodiff/omega_autodiff.c: nodes in decreasing order, |
| 7 | note in0 before in1, first contribution stored, later ones added as grad + contrib. |
| 8 | note TN node words: 0 op,1 in0,2 in1,3 rows,4 cols,5 offset,6 spare,7 rg,8 hasgrad,9 axis. |
| 9 | note TK: 0 count,1 backward done,2 failed,3 next arena offset. |
| 10 | global TN ulong 512 |
| 11 | global TV f32 2048 |
| 12 | global TG f32 2048 |
| 13 | global TD ulong 768 |
| 14 | global TS ulong 256 |
| 15 | global TK ulong 4 |
| 16 | global TL f32 32 |
| 17 | |
| 18 | fn tnget n ulong f ulong to ulong |
| 19 | ret get TN add mul n 16 f |
| 20 | end |
| 21 | |
| 22 | fn tnput n ulong f ulong v ulong to void |
| 23 | put TN add mul n 16 f v |
| 24 | end |
| 25 | |
| 26 | fn tprows n ulong to ulong |
| 27 | ret tnget n 3 |
| 28 | end |
| 29 | |
| 30 | fn tpcols n ulong to ulong |
| 31 | ret tnget n 4 |
| 32 | end |
| 33 | |
| 34 | fn tpoff n ulong to ulong |
| 35 | ret tnget n 5 |
| 36 | end |
| 37 | |
| 38 | fn tpok n ulong to bool |
| 39 | ret lt n get TK 0 |
| 40 | end |
| 41 | |
| 42 | note E1 tile sum of TL[0..n), 1 <= n <= 32. |
| 43 | fn e1tree n ulong to f32 |
| 44 | for i ulong sub 32 n |
| 45 | put TL add n i neg 0.0f32 |
| 46 | end |
| 47 | d = ulong 16 |
| 48 | for s ulong 5 |
| 49 | for i ulong d |
| 50 | put TL i add get TL i get TL add i d |
| 51 | end |
| 52 | d = shr d 1 |
| 53 | end |
| 54 | ret get TL 0 |
| 55 | end |
| 56 | |
| 57 | fn tpdesc n ulong r ulong c ulong to void |
| 58 | b = mul n 24 |
| 59 | for i ulong 24 |
| 60 | put TD add b i 0 |
| 61 | end |
| 62 | put TD b 1 |
| 63 | put TD add b 1 1 |
| 64 | put TD add b 2 2 |
| 65 | put TD add b 3 mul r c |
| 66 | put TD add b 5 add n 1 |
| 67 | put TD add b 6 1 |
| 68 | put TD add b 8 r |
| 69 | put TD add b 9 c |
| 70 | put TD add b 16 c |
| 71 | put TD add b 17 1 |
| 72 | s = mul n 8 |
| 73 | put TS s 1 |
| 74 | put TS add s 1 0 |
| 75 | put TS add s 2 add n 1 |
| 76 | put TS add s 3 1 |
| 77 | put TS add s 4 1 |
| 78 | put TS add s 5 mul r c |
| 79 | put TS add s 6 0 |
| 80 | put TS add s 7 0 |
| 81 | end |
| 82 | |
| 83 | note Admit one node through the M20 emcheck, then the tape's own layout rules. |
| 84 | fn tpadmit n ulong to int |
| 85 | for i ulong 24 |
| 86 | put ED i get TD add mul n 24 i |
| 87 | end |
| 88 | for i ulong 8 |
| 89 | put ES i get TS add mul n 8 i |
| 90 | end |
| 91 | st = emcheck |
| 92 | if ne st 0 |
| 93 | ret st |
| 94 | end |
| 95 | if or ne get ED 1 1 ne get ED 4 0 |
| 96 | ret -32 |
| 97 | end |
| 98 | if or ne get ED 2 2 ne get ED 5 add n 1 |
| 99 | ret -32 |
| 100 | end |
| 101 | ret 0 |
| 102 | end |
| 103 | |
| 104 | fn tpadm2 a ulong b ulong to int |
| 105 | st = tpadmit a |
| 106 | if ne st 0 |
| 107 | ret st |
| 108 | end |
| 109 | ret tpadmit b |
| 110 | end |
| 111 | |
| 112 | fn tprg a ulong b ulong to ulong |
| 113 | if or ne tnget a 7 0 ne tnget b 7 0 |
| 114 | ret 1 |
| 115 | end |
| 116 | ret 0 |
| 117 | end |
| 118 | |
| 119 | note Record a node. Nothing is recorded on refusal. |
| 120 | fn tpnew op ulong a ulong b ulong r ulong c ulong rg ulong to int |
| 121 | if ne get TK 1 0 |
| 122 | ret -31 |
| 123 | end |
| 124 | n = get TK 0 |
| 125 | if ge n 32 |
| 126 | ret -30 |
| 127 | end |
| 128 | if or eq r 0 eq c 0 |
| 129 | ret -30 |
| 130 | end |
| 131 | if or gt r 64 gt c 64 |
| 132 | ret -30 |
| 133 | end |
| 134 | if gt mul r c 64 |
| 135 | ret -30 |
| 136 | end |
| 137 | off = get TK 3 |
| 138 | if gt add off mul r c 2048 |
| 139 | ret -30 |
| 140 | end |
| 141 | tpdesc n r c |
| 142 | st = tpadmit n |
| 143 | if ne st 0 |
| 144 | ret st |
| 145 | end |
| 146 | for f ulong 16 |
| 147 | tnput n f 0 |
| 148 | end |
| 149 | tnput n 0 op |
| 150 | tnput n 1 a |
| 151 | tnput n 2 b |
| 152 | tnput n 3 r |
| 153 | tnput n 4 c |
| 154 | tnput n 5 off |
| 155 | tnput n 7 rg |
| 156 | put TK 0 add n 1 |
| 157 | put TK 3 add off mul r c |
| 158 | ret int n |
| 159 | end |
| 160 | |
| 161 | fn tpleaf r ulong c ulong rg ulong to int |
| 162 | id = tpnew 0 0 0 r c rg |
| 163 | if lt id 0 |
| 164 | ret id |
| 165 | end |
| 166 | o = tpoff ulong id |
| 167 | for i ulong mul r c |
| 168 | put TV add o i 0.0f32 |
| 169 | end |
| 170 | ret id |
| 171 | end |
| 172 | |
| 173 | fn tpset n ulong i ulong v f32 to int |
| 174 | if not tpok n |
| 175 | ret -32 |
| 176 | end |
| 177 | if or ne tnget n 0 0 ge i mul tprows n tpcols n |
| 178 | ret -32 |
| 179 | end |
| 180 | put TV add tpoff n i v |
| 181 | ret 0 |
| 182 | end |
| 183 | |
| 184 | fn tpval n ulong i ulong to f32 |
| 185 | if or not tpok n ge i mul tprows n tpcols n |
| 186 | exit 97 |
| 187 | end |
| 188 | ret get TV add tpoff n i |
| 189 | end |
| 190 | |
| 191 | fn tphasg n ulong to bool |
| 192 | if not tpok n |
| 193 | ret false |
| 194 | end |
| 195 | ret ne tnget n 8 0 |
| 196 | end |
| 197 | |
| 198 | fn tpgrad n ulong i ulong to f32 |
| 199 | if or not tphasg n ge i mul tprows n tpcols n |
| 200 | exit 97 |
| 201 | end |
| 202 | ret get TG add tpoff n i |
| 203 | end |
| 204 | |
| 205 | fn tpreset to void |
| 206 | for i ulong 4 |
| 207 | put TK i 0 |
| 208 | end |
| 209 | end |
| 210 | |
| 211 | fn tpbinf n ulong to void |
| 212 | op = tnget n 0 |
| 213 | o = tpoff n |
| 214 | oa = tpoff tnget n 1 |
| 215 | ob = tpoff tnget n 2 |
| 216 | for i ulong mul tprows n tpcols n |
| 217 | x = get TV add oa i |
| 218 | y = get TV add ob i |
| 219 | if eq op 1 |
| 220 | put TV add o i add x y |
| 221 | end |
| 222 | if eq op 2 |
| 223 | put TV add o i sub x y |
| 224 | end |
| 225 | if eq op 3 |
| 226 | put TV add o i mul x y |
| 227 | end |
| 228 | end |
| 229 | end |
| 230 | |
| 231 | fn tpbin op ulong a ulong b ulong to int |
| 232 | if not and tpok a tpok b |
| 233 | ret -32 |
| 234 | end |
| 235 | r = tprows a |
| 236 | c = tpcols a |
| 237 | if or ne r tprows b ne c tpcols b |
| 238 | ret -32 |
| 239 | end |
| 240 | st = tpadm2 a b |
| 241 | if ne st 0 |
| 242 | ret st |
| 243 | end |
| 244 | id = tpnew op a b r c tprg a b |
| 245 | if lt id 0 |
| 246 | ret id |
| 247 | end |
| 248 | tpbinf ulong id |
| 249 | ret id |
| 250 | end |
| 251 | |
| 252 | fn tpadd a ulong b ulong to int |
| 253 | ret tpbin 1 a b |
| 254 | end |
| 255 | |
| 256 | fn tpsub a ulong b ulong to int |
| 257 | ret tpbin 2 a b |
| 258 | end |
| 259 | |
| 260 | fn tpmul a ulong b ulong to int |
| 261 | ret tpbin 3 a b |
| 262 | end |
| 263 | |
| 264 | note One matmul output: products A[i,q]*B[q,j] in q order, then the E1 tile. |
| 265 | fn tpmmo n ulong i ulong j ulong to f32 |
| 266 | k = tpcols tnget n 1 |
| 267 | oa = add tpoff tnget n 1 mul i k |
| 268 | ob = add tpoff tnget n 2 j |
| 269 | nn = tpcols n |
| 270 | for q ulong k |
| 271 | x = get TV add oa q |
| 272 | y = get TV add ob mul q nn |
| 273 | put TL q mul x y |
| 274 | end |
| 275 | ret e1tree k |
| 276 | end |
| 277 | |
| 278 | fn tpmmf n ulong to void |
| 279 | nn = tpcols n |
| 280 | o = tpoff n |
| 281 | for i ulong tprows n |
| 282 | for j ulong nn |
| 283 | put TV add o add mul i nn j tpmmo n i j |
| 284 | end |
| 285 | end |
| 286 | end |
| 287 | |
| 288 | fn tpmm a ulong b ulong to int |
| 289 | if not and tpok a tpok b |
| 290 | ret -32 |
| 291 | end |
| 292 | m = tprows a |
| 293 | k = tpcols a |
| 294 | nn = tpcols b |
| 295 | if ne k tprows b |
| 296 | ret -32 |
| 297 | end |
| 298 | if or gt k 32 or gt m 32 gt nn 32 |
| 299 | ret -30 |
| 300 | end |
| 301 | st = tpadm2 a b |
| 302 | if ne st 0 |
| 303 | ret st |
| 304 | end |
| 305 | id = tpnew 4 a b m nn tprg a b |
| 306 | if lt id 0 |
| 307 | ret id |
| 308 | end |
| 309 | tpmmf ulong id |
| 310 | ret id |
| 311 | end |
| 312 | |
| 313 | fn tprluf n ulong to void |
| 314 | o = tpoff n |
| 315 | oa = tpoff tnget n 1 |
| 316 | for i ulong mul tprows n tpcols n |
| 317 | x = get TV add oa i |
| 318 | if gt x 0.0f32 |
| 319 | put TV add o i x |
| 320 | else |
| 321 | put TV add o i 0.0f32 |
| 322 | end |
| 323 | end |
| 324 | end |
| 325 | |
| 326 | fn tprelu a ulong to int |
| 327 | if not tpok a |
| 328 | ret -32 |
| 329 | end |
| 330 | st = tpadmit a |
| 331 | if ne st 0 |
| 332 | ret st |
| 333 | end |
| 334 | id = tpnew 5 a a tprows a tpcols a tnget a 7 |
| 335 | if lt id 0 |
| 336 | ret id |
| 337 | end |
| 338 | tprluf ulong id |
| 339 | ret id |
| 340 | end |
| 341 | |
| 342 | fn tpbcf n ulong to void |
| 343 | c = tpcols n |
| 344 | o = tpoff n |
| 345 | oa = tpoff tnget n 1 |
| 346 | for i ulong tprows n |
| 347 | for j ulong c |
| 348 | x = get TV add oa j |
| 349 | put TV add o add mul i c j x |
| 350 | end |
| 351 | end |
| 352 | end |
| 353 | |
| 354 | fn tpbcast a ulong rows ulong to int |
| 355 | if not tpok a |
| 356 | ret -32 |
| 357 | end |
| 358 | if ne tprows a 1 |
| 359 | ret -32 |
| 360 | end |
| 361 | if or eq rows 0 gt rows 32 |
| 362 | ret -30 |
| 363 | end |
| 364 | st = tpadmit a |
| 365 | if ne st 0 |
| 366 | ret st |
| 367 | end |
| 368 | id = tpnew 6 a a rows tpcols a tnget a 7 |
| 369 | if lt id 0 |
| 370 | ret id |
| 371 | end |
| 372 | tpbcf ulong id |
| 373 | ret id |
| 374 | end |
| 375 | |
| 376 | note Axis-0 output j: column j of the input in row order, then the E1 tile. |
| 377 | fn tpsc n ulong j ulong to f32 |
| 378 | a = tnget n 1 |
| 379 | c = tpcols a |
| 380 | oa = add tpoff a j |
| 381 | for i ulong tprows a |
| 382 | put TL i get TV add oa mul i c |
| 383 | end |
| 384 | ret e1tree tprows a |
| 385 | end |
| 386 | |
| 387 | note Axis-1 output i: row i of the input in column order, then the E1 tile. |
| 388 | fn tpsr n ulong i ulong to f32 |
| 389 | a = tnget n 1 |
| 390 | c = tpcols a |
| 391 | oa = add tpoff a mul i c |
| 392 | for j ulong c |
| 393 | put TL j get TV add oa j |
| 394 | end |
| 395 | ret e1tree c |
| 396 | end |
| 397 | |
| 398 | fn tpsumf n ulong to void |
| 399 | o = tpoff n |
| 400 | if eq tnget n 9 0 |
| 401 | for j ulong tpcols n |
| 402 | put TV add o j tpsc n j |
| 403 | end |
| 404 | else |
| 405 | for i ulong tprows n |
| 406 | put TV add o i tpsr n i |
| 407 | end |
| 408 | end |
| 409 | end |
| 410 | |
| 411 | fn tpsum a ulong axis ulong to int |
| 412 | if not tpok a |
| 413 | ret -32 |
| 414 | end |
| 415 | if gt axis 1 |
| 416 | ret -32 |
| 417 | end |
| 418 | r = tprows a |
| 419 | c = tpcols a |
| 420 | if eq axis 0 |
| 421 | if gt r 32 |
| 422 | ret -30 |
| 423 | end |
| 424 | r = 1 |
| 425 | else |
| 426 | if gt c 32 |
| 427 | ret -30 |
| 428 | end |
| 429 | c = 1 |
| 430 | end |
| 431 | st = tpadmit a |
| 432 | if ne st 0 |
| 433 | ret st |
| 434 | end |
| 435 | id = tpnew 7 a a r c tnget a 7 |
| 436 | if lt id 0 |
| 437 | ret id |
| 438 | end |
| 439 | tnput ulong id 9 axis |
| 440 | tpsumf ulong id |
| 441 | ret id |
| 442 | end |
| 443 | |
| 444 | note Accumulate one contribution element: store when first, else grad + contrib. |
| 445 | fn tpacc d ulong e ulong v f32 first bool to void |
| 446 | p = add tpoff d e |
| 447 | if first |
| 448 | put TG p v |
| 449 | else |
| 450 | put TG p add get TG p v |
| 451 | end |
| 452 | end |
| 453 | |
| 454 | note in0 of ADD/SUB/MUL: g, g, g*value(in1). |
| 455 | fn tpbewa n ulong to void |
| 456 | a = tnget n 1 |
| 457 | f = eq tnget a 8 0 |
| 458 | og = tpoff n |
| 459 | ob = tpoff tnget n 2 |
| 460 | mulop = eq tnget n 0 3 |
| 461 | for i ulong mul tprows n tpcols n |
| 462 | v = get TG add og i |
| 463 | if mulop |
| 464 | v = mul v get TV add ob i |
| 465 | end |
| 466 | tpacc a i v f |
| 467 | end |
| 468 | tnput a 8 1 |
| 469 | end |
| 470 | |
| 471 | note in1 of ADD/SUB/MUL: g, neg g, g*value(in0). |
| 472 | fn tpbewb n ulong to void |
| 473 | b = tnget n 2 |
| 474 | f = eq tnget b 8 0 |
| 475 | og = tpoff n |
| 476 | oa = tpoff tnget n 1 |
| 477 | op = tnget n 0 |
| 478 | for i ulong mul tprows n tpcols n |
| 479 | w = get TG add og i |
| 480 | if eq op 2 |
| 481 | w = neg w |
| 482 | end |
| 483 | if eq op 3 |
| 484 | w = mul w get TV add oa i |
| 485 | end |
| 486 | tpacc b i w f |
| 487 | end |
| 488 | tnput b 8 1 |
| 489 | end |
| 490 | |
| 491 | fn tpbew n ulong to void |
| 492 | if ne tnget tnget n 1 7 0 |
| 493 | tpbewa n |
| 494 | end |
| 495 | if ne tnget tnget n 2 7 0 |
| 496 | tpbewb n |
| 497 | end |
| 498 | end |
| 499 | |
| 500 | note gA[i,q]: products g[i,j]*B[q,j] in j order (g @ B^T), then the E1 tile. |
| 501 | fn tpgao n ulong i ulong q ulong to f32 |
| 502 | nn = tpcols n |
| 503 | og = add tpoff n mul i nn |
| 504 | ob = add tpoff tnget n 2 mul q nn |
| 505 | for j ulong nn |
| 506 | x = get TG add og j |
| 507 | y = get TV add ob j |
| 508 | put TL j mul x y |
| 509 | end |
| 510 | ret e1tree nn |
| 511 | end |
| 512 | |
| 513 | note gB[q,j]: products A[i,q]*g[i,j] in i order (A^T @ g), then the E1 tile. |
| 514 | fn tpgbo n ulong q ulong j ulong to f32 |
| 515 | k = tpcols tnget n 1 |
| 516 | oa = add tpoff tnget n 1 q |
| 517 | nn = tpcols n |
| 518 | og = add tpoff n j |
| 519 | for i ulong tprows n |
| 520 | x = get TV add oa mul i k |
| 521 | y = get TG add og mul i nn |
| 522 | put TL i mul x y |
| 523 | end |
| 524 | ret e1tree tprows n |
| 525 | end |
| 526 | |
| 527 | fn tpbmma n ulong to void |
| 528 | a = tnget n 1 |
| 529 | k = tpcols a |
| 530 | f = eq tnget a 8 0 |
| 531 | for i ulong tprows n |
| 532 | for q ulong k |
| 533 | tpacc a add mul i k q tpgao n i q f |
| 534 | end |
| 535 | end |
| 536 | tnput a 8 1 |
| 537 | end |
| 538 | |
| 539 | fn tpbmmb n ulong to void |
| 540 | b = tnget n 2 |
| 541 | nn = tpcols n |
| 542 | f = eq tnget b 8 0 |
| 543 | for q ulong tprows b |
| 544 | for j ulong nn |
| 545 | tpacc b add mul q nn j tpgbo n q j f |
| 546 | end |
| 547 | end |
| 548 | tnput b 8 1 |
| 549 | end |
| 550 | |
| 551 | fn tpbmm n ulong to void |
| 552 | if ne tnget tnget n 1 7 0 |
| 553 | tpbmma n |
| 554 | end |
| 555 | if ne tnget tnget n 2 7 0 |
| 556 | tpbmmb n |
| 557 | end |
| 558 | end |
| 559 | |
| 560 | fn tpbrelu n ulong to void |
| 561 | a = tnget n 1 |
| 562 | if eq tnget a 7 0 |
| 563 | ret |
| 564 | end |
| 565 | f = eq tnget a 8 0 |
| 566 | for i ulong mul tprows n tpcols n |
| 567 | v = 0.0f32 |
| 568 | x = get TV add tpoff a i |
| 569 | if gt x 0.0f32 |
| 570 | v = get TG add tpoff n i |
| 571 | end |
| 572 | tpacc a i v f |
| 573 | end |
| 574 | tnput a 8 1 |
| 575 | end |
| 576 | |
| 577 | note Unbroadcast column j of g over rows, then the E1 tile. |
| 578 | fn tpbcg n ulong j ulong to f32 |
| 579 | c = tpcols n |
| 580 | og = add tpoff n j |
| 581 | for i ulong tprows n |
| 582 | put TL i get TG add og mul i c |
| 583 | end |
| 584 | ret e1tree tprows n |
| 585 | end |
| 586 | |
| 587 | fn tpbbc n ulong to void |
| 588 | a = tnget n 1 |
| 589 | if eq tnget a 7 0 |
| 590 | ret |
| 591 | end |
| 592 | f = eq tnget a 8 0 |
| 593 | for j ulong tpcols n |
| 594 | tpacc a j tpbcg n j f |
| 595 | end |
| 596 | tnput a 8 1 |
| 597 | end |
| 598 | |
| 599 | fn tpbsum n ulong to void |
| 600 | a = tnget n 1 |
| 601 | if eq tnget a 7 0 |
| 602 | ret |
| 603 | end |
| 604 | f = eq tnget a 8 0 |
| 605 | c = tpcols a |
| 606 | og = tpoff n |
| 607 | col = eq tnget n 9 0 |
| 608 | for i ulong tprows a |
| 609 | for j ulong c |
| 610 | g = get TG add og i |
| 611 | if col |
| 612 | g = get TG add og j |
| 613 | end |
| 614 | tpacc a add mul i c j g f |
| 615 | end |
| 616 | end |
| 617 | tnput a 8 1 |
| 618 | end |
| 619 | |
| 620 | fn tpbnode n ulong to void |
| 621 | op = tnget n 0 |
| 622 | if and ge op 1 le op 3 |
| 623 | tpbew n |
| 624 | end |
| 625 | if eq op 4 |
| 626 | tpbmm n |
| 627 | end |
| 628 | if eq op 5 |
| 629 | tpbrelu n |
| 630 | end |
| 631 | if eq op 6 |
| 632 | tpbbc n |
| 633 | end |
| 634 | if eq op 7 |
| 635 | tpbsum n |
| 636 | end |
| 637 | end |
| 638 | |
| 639 | note Reverse pass from a [1,1] root with seed 1.0. Runs once per tape. |
| 640 | fn tpback root ulong to int |
| 641 | if or ne get TK 1 0 ne get TK 2 0 |
| 642 | ret -31 |
| 643 | end |
| 644 | if not tpok root |
| 645 | ret -32 |
| 646 | end |
| 647 | if or ne tprows root 1 ne tpcols root 1 |
| 648 | ret -33 |
| 649 | end |
| 650 | if eq tnget root 7 0 |
| 651 | ret -33 |
| 652 | end |
| 653 | put TK 1 1 |
| 654 | for i ulong get TK 0 |
| 655 | tnput i 8 0 |
| 656 | end |
| 657 | put TG tpoff root 1.0f32 |
| 658 | tnput root 8 1 |
| 659 | for s ulong add root 1 |
| 660 | i = sub root s |
| 661 | if and ne tnget i 8 0 ne tnget i 0 0 |
| 662 | tpbnode i |
| 663 | end |
| 664 | end |
| 665 | ret 0 |
| 666 | end |