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/omega-mv/blob/main/src/autodiff/test_autodiff.om # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / src / autodiff / test_autodiff.om` 1718 lines ยท 39745 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note test_autodiff.om -- Pure seeded OM-2 M21 OMEGA_AUTODIFF reverse-mode tape tests. 3 note Comprehensive coverage ported from tests/test_omega_autodiff.c: 4 note 1. All 21 OpCases (ADD, SUB, MUL, DIV, MAX, SQRT, MATMUL 2D & batched 3D, 5 note TRANSPOSE, BROADCAST, SUM, MEAN with keepdims 0/1) verified 3 ways: 6 note - Layer 1: exact f64 calculus reference within per-op gamma(k) bound 7 note - Layer 2: bit-exact raw-u32 replay via direct M20 tensor ops (+ 1-bit flip control) 8 note - Layer 3: central finite differences (h=2^-6, pure 1e-3 relative tolerance) 9 note 2. Comparator counterexamples (contract & FD reject wrong gradients) 10 note 3. MAX ties routing to b (ReLU gradient 0 at 0, +0/-0 ties) 11 note 4. Composite linear + squared-error fan-out graph (d*d) vs closed form & central FD 12 note 5. Determinism: two fresh runs produce bit-identical gradients & loss value_id 13 note 6. Error paths, tape capacity limits, scratch limits, shape/axis/state/dtype/flatten/leak checks 14 15 const TAD_FD_H f64 0.015625 16 const TAD_FD_RTOL f64 0.001 17 const TAD_U32 f64 0.`000000059604644775390625` 18 19 global tad_c ulong 2 20 global tad_rng uint 1 21 note tad_cm: 0=op, 1=ra, 2=rb, 3=has_b, 4=axis, 5=kp, 6=rt, 7=data_mode, 8=ry, 9=na, 10=nb, 11=ny 22 global tad_cm ulong 12 23 global tad_sa ulong 8 24 global tad_sb ulong 8 25 global tad_st ulong 8 26 global tad_sy ulong 8 27 global tad_lim f32 4 28 global tad_h ulong 24 29 global tad_ids ulong 16 30 global tad_a f32 128 31 global tad_b f32 128 32 global tad_r f32 128 33 global tad_ga f32 128 34 global tad_gb f32 128 35 global tad_ea f32 128 36 global tad_eb f32 128 37 global tad_ra f64 128 38 global tad_aa f64 128 39 global tad_rb f64 128 40 global tad_ab f64 128 41 global tad_ka ulong 128 42 global tad_kb ulong 128 43 global tad_lx f32 12 44 global tad_lw f32 6 45 global tad_lb f32 2 46 global tad_ly f32 8 47 global tad_fg1 f32 8 48 global tad_fg2 f32 8 49 global tad_ee f64 8 50 global tad_id1 uchar 32 51 global tad_id2 uchar 32 52 53 text tad_pass "M21_OMEGA_AUTODIFF_TESTS_PASS\n" 54 text tad_fail "M21_OMEGA_AUTODIFF_TESTS_FAIL\n" 55 text tad_sum1 "M21 OMEGA_AUTODIFF CPU tests: " 56 text tad_sum2 " pass, " 57 text tad_sum3 " fail\n" 58 text tad_fc "FAIL autodiff check #" 59 text tad_nl "\n" 60 61 fn ad_w_u64 fd int v ulong to void 62 buf = new uchar 24 0 63 tmp = new uchar 24 0 64 if eq v 0 65 put buf 0 uchar 48 66 w = write fd buf 0 1 67 ret 68 end 69 cnt = ulong 0 70 x = v 71 while gt x 0 bound 24 72 put tmp cnt uchar add 48 rem x 10 73 cnt = add cnt 1 74 x = div x 10 75 end 76 for i ulong cnt bound 24 77 put buf i get tmp sub sub cnt 1 i 78 end 79 w = write fd buf 0 cnt 80 end 81 82 fn ad_chk cond bool to void 83 n = add get tad_c 0 1 84 put tad_c 0 n 85 if not cond 86 put tad_c 1 add get tad_c 1 1 87 w1 = write 2 tad_fc 0 len tad_fc 88 ad_w_u64 2 n 89 w2 = write 2 tad_nl 0 1 90 end 91 end 92 93 fn ad_save_h idx ulong h view ulong 2 to void 94 put tad_h mul idx 2 get h 0 95 put tad_h add mul idx 2 1 get h 1 96 end 97 98 fn ad_rel_h idx ulong to void 99 r = ot_release get tad_h mul idx 2 get tad_h add mul idx 2 1 100 end 101 102 fn ad_urand lo f32 hi f32 to f32 103 s = add mul get tad_rng 0 uint `1664525` uint `1013904223` 104 put tad_rng 0 s 105 r = div f64 shr s 8 `16777216`.0f64 106 ret f32 add f64 lo mul sub f64 hi f64 lo r 107 end 108 109 fn ad_fd_ok an f64 fd f64 to bool 110 ret le fabs sub an fd mul TAD_FD_RTOL fabs fd 111 end 112 113 fn ad_gamma k ulong to f64 114 ku = mul f64 k TAD_U32 115 ret div ku sub 1.0f64 ku 116 end 117 118 fn ad_levels m ulong to ulong 119 l = ulong 1 120 cap = ulong 32 121 while lt cap m bound 16 122 cap = mul cap 32 123 l = add l 1 124 end 125 ret l 126 end 127 128 fn ad_numel r ulong which ulong to ulong 129 n = ulong 1 130 for d ulong r bound 8 131 if eq which 0 132 n = mul n get tad_sa d 133 else 134 if eq which 1 135 n = mul n get tad_sb d 136 else 137 n = mul n get tad_sy d 138 end 139 end 140 end 141 ret n 142 end 143 144 fn ad_unravel_y flat ulong oc edit ulong 8 to void 145 ry = get tad_cm 8 146 f = flat 147 for di ulong ry bound 8 148 d = sub sub ry 1 di 149 sd = get tad_sy d 150 put oc d rem f sd 151 f = div f sd 152 end 153 end 154 155 fn ad_bmap oc view ulong 8 which ulong to ulong 156 ry = get tad_cm 8 157 ri = get tad_cm 1 158 if eq which 1 159 ri = get tad_cm 2 160 end 161 idx = ulong 0 162 lead = sub ry ri 163 for d ulong ri bound 8 164 sd = get tad_sa d 165 if eq which 1 166 sd = get tad_sb d 167 end 168 c = get oc add d lead 169 if eq sd 1 170 c = 0 171 end 172 idx = add mul idx sd c 173 end 174 ret idx 175 end 176 177 fn ad_unb_depth which ulong to ulong 178 ry = get tad_cm 8 179 ri = get tad_cm 1 180 if eq which 1 181 ri = get tad_cm 2 182 end 183 tot = ulong 0 184 lead = sub ry ri 185 for d ulong lead bound 8 186 if gt get tad_sy d 1 187 tot = add tot mul 5 ad_levels get tad_sy d 188 end 189 end 190 for d ulong ri bound 8 191 sd = get tad_sa d 192 if eq which 1 193 sd = get tad_sb d 194 end 195 if and eq sd 1 gt get tad_sy add d lead 1 196 tot = add tot mul 5 ad_levels get tad_sy add d lead 197 end 198 end 199 ret tot 200 end 201 202 fn ad_chk_leaks to void 203 lc = new ulong 2 0 204 ot_live_counts lc 205 ad_chk and eq get lc 0 0 eq get lc 1 0 206 end 207 208 fn ad_mk_g which ulong out_idx ulong to long 209 sh = new ulong 8 0 210 buf = new f32 128 0.0f32 211 h = new ulong 2 0 212 r = get tad_cm 1 213 n = get tad_cm 9 214 if eq which 1 215 r = get tad_cm 2 216 n = get tad_cm 10 217 else 218 if eq which 2 219 r = get tad_cm 8 220 n = get tad_cm 11 221 end 222 end 223 for d ulong r bound 8 224 if eq which 0 225 put sh d get tad_sa d 226 else 227 if eq which 1 228 put sh d get tad_sb d 229 else 230 put sh d get tad_sy d 231 end 232 end 233 end 234 for i ulong n bound 128 235 if eq which 0 236 put buf i get tad_a i 237 else 238 if eq which 1 239 put buf i get tad_b i 240 else 241 put buf i get tad_r i 242 end 243 end 244 end 245 rc = ot_from_f32 r sh part buf 0 n h 246 if eq rc OT_OK 247 ad_save_h out_idx h 248 end 249 ret rc 250 end 251 252 fn ad_build_case to long 253 tmp = new ulong 1 0 254 st = new ulong 8 0 255 rc = ad_leaf get tad_h 0 get tad_h 1 1 tmp 256 if ne rc AD_OK 257 ret rc 258 end 259 put tad_ids 0 get tmp 0 260 put tad_ids 1 AD_NONE 261 if ne get tad_cm 3 0 262 rc = ad_leaf get tad_h 2 get tad_h 3 1 tmp 263 if ne rc AD_OK 264 ret rc 265 end 266 put tad_ids 1 get tmp 0 267 end 268 op = get tad_cm 0 269 if le op AD_MAX 270 rc = ad_binary op get tad_ids 0 get tad_ids 1 tmp 271 else 272 if eq op AD_SQRT 273 rc = ad_sqrt get tad_ids 0 tmp 274 else 275 if eq op AD_MATMUL 276 rc = ad_matmul get tad_ids 0 get tad_ids 1 tmp 277 else 278 if eq op AD_TRANSPOSE 279 rc = ad_transpose get tad_ids 0 tmp 280 else 281 if eq op AD_BROADCAST 282 rt = get tad_cm 6 283 for d ulong rt bound 8 284 put st d get tad_st d 285 end 286 rc = ad_broadcast get tad_ids 0 rt st tmp 287 else 288 rc = ad_reduce op get tad_ids 0 get tad_cm 4 get tad_cm 5 tmp 289 end 290 end 291 end 292 end 293 end 294 put tad_ids 2 get tmp 0 295 ret rc 296 end 297 298 note ---- Layer 1: exact f64 calculus reference ---- 299 300 fn ad_ref_ew_a j ulong oc view ulong 8 da ulong to void 301 xa = ad_bmap oc 0 302 op = get tad_cm 0 303 gv = f64 get tad_r j 304 bv = 0.0f64 305 if ne get tad_cm 3 0 306 bv = f64 get tad_b ad_bmap oc 1 307 end 308 ta_v = gv 309 ea = da 310 if eq op AD_MUL 311 ta_v = mul gv bv 312 ea = add ea 1 313 else 314 if eq op AD_DIV 315 ta_v = div gv bv 316 ea = add ea 1 317 else 318 if eq op AD_MAX 319 if not gt f64 get tad_a xa bv 320 ta_v = 0.0f64 321 end 322 end 323 end 324 end 325 put tad_ra xa add get tad_ra xa ta_v 326 put tad_aa xa add get tad_aa xa fabs ta_v 327 put tad_ka xa ea 328 end 329 330 fn ad_ref_ew_b j ulong oc view ulong 8 db ulong to void 331 xb = ad_bmap oc 1 332 op = get tad_cm 0 333 gv = f64 get tad_r j 334 av = f64 get tad_a ad_bmap oc 0 335 bv = f64 get tad_b xb 336 tb_v = gv 337 eb = db 338 if eq op AD_SUB 339 tb_v = sub 0.0f64 gv 340 else 341 if eq op AD_MUL 342 tb_v = mul gv av 343 eb = add eb 1 344 else 345 if eq op AD_DIV 346 tb_v = div sub 0.0f64 mul gv av mul bv bv 347 eb = add eb 3 348 else 349 if eq op AD_MAX 350 if gt av bv 351 tb_v = 0.0f64 352 end 353 end 354 end 355 end 356 end 357 put tad_rb xb add get tad_rb xb tb_v 358 put tad_ab xb add get tad_ab xb fabs tb_v 359 put tad_kb xb eb 360 end 361 362 fn ad_ref_elemwise to void 363 ny = get tad_cm 11 364 da = ad_unb_depth 0 365 db = ulong 0 366 has_b = get tad_cm 3 367 if ne has_b 0 368 db = ad_unb_depth 1 369 end 370 oc = new ulong 8 0 371 for j ulong ny bound 128 372 ad_unravel_y j oc 373 ad_ref_ew_a j oc da 374 if ne has_b 0 375 ad_ref_ew_b j oc db 376 end 377 end 378 end 379 380 fn ad_mm_prod_rb rbase ulong bbase ulong j ulong to f64 381 rv = f64 get tad_r add rbase j 382 bv = f64 get tad_b add bbase j 383 ret mul rv bv 384 end 385 386 fn ad_mm_prod_ar row ulong k ulong j ulong to f64 387 ra = get tad_cm 1 388 k_d = get tad_sa sub ra 1 389 n_d = get tad_sb 1 390 av = f64 get tad_a add mul row k_d k 391 rv = f64 get tad_r add mul row n_d j 392 ret mul av rv 393 end 394 395 fn ad_ref_mm_a_one row ulong k ulong k_d ulong n_d ulong to void 396 s = 0.0f64 397 sa_v = 0.0f64 398 rbase = mul row n_d 399 bbase = mul k n_d 400 for j ulong n_d bound 16 401 p = ad_mm_prod_rb rbase bbase j 402 s = add s p 403 sa_v = add sa_v fabs p 404 end 405 x = add mul row k_d k 406 put tad_ra x s 407 put tad_aa x sa_v 408 put tad_ka x add 1 mul 5 ad_levels n_d 409 end 410 411 fn ad_ref_mm_b_one k ulong j ulong bt ulong m ulong to void 412 n_d = get tad_sb 1 413 s = 0.0f64 414 sa_v = 0.0f64 415 tot_rows = mul bt m 416 for row ulong tot_rows bound 64 417 p = ad_mm_prod_ar row k j 418 s = add s p 419 sa_v = add sa_v fabs p 420 end 421 kb = add 1 mul 5 ad_levels m 422 if gt bt 1 423 kb = add kb mul 5 ad_levels bt 424 end 425 x = add mul k n_d j 426 put tad_rb x s 427 put tad_ab x sa_v 428 put tad_kb x kb 429 end 430 431 fn ad_ref_matmul to void 432 ra = get tad_cm 1 433 bt = ulong 1 434 if eq ra 3 435 bt = get tad_sa 0 436 end 437 m = get tad_sa sub ra 2 438 k_d = get tad_sa sub ra 1 439 n_d = get tad_sb 1 440 tot_rows = mul bt m 441 for row ulong tot_rows bound 64 442 for k ulong k_d bound 16 443 ad_ref_mm_a_one row k k_d n_d 444 end 445 end 446 for k ulong k_d bound 16 447 for j ulong n_d bound 16 448 ad_ref_mm_b_one k j bt m 449 end 450 end 451 end 452 453 fn ad_ref_reduce to void 454 op = get tad_cm 0 455 ra = get tad_cm 1 456 na = get tad_cm 9 457 ax = get tad_cm 4 458 n_ax = f64 get tad_sa ax 459 ic = new ulong 8 0 460 for i ulong na bound 128 461 f = i 462 for di ulong ra bound 8 463 d = sub sub ra 1 di 464 sd = get tad_sa d 465 put ic d rem f sd 466 f = div f sd 467 end 468 j = ulong 0 469 for d ulong ra bound 8 470 if ne d ax 471 j = add mul j get tad_sa d get ic d 472 end 473 end 474 rv = f64 get tad_r j 475 if eq op AD_MEAN 476 rv = div rv n_ax 477 put tad_ka i 1 478 else 479 put tad_ka i 0 480 end 481 put tad_ra i rv 482 put tad_aa i fabs rv 483 end 484 end 485 486 fn ad_compute_ref to void 487 na = get tad_cm 9 488 nb = get tad_cm 10 489 for i ulong na bound 128 490 put tad_ra i 0.0f64 491 put tad_aa i 0.0f64 492 put tad_ka i 0 493 end 494 for i ulong nb bound 128 495 put tad_rb i 0.0f64 496 put tad_ab i 0.0f64 497 put tad_kb i 0 498 end 499 op = get tad_cm 0 500 if or le op AD_MAX eq op AD_BROADCAST 501 ad_ref_elemwise 502 ret 503 end 504 if eq op AD_SQRT 505 for i ulong na bound 128 506 v = div f64 get tad_r i mul 2.0f64 fsqrt f64 get tad_a i 507 put tad_ra i v 508 put tad_aa i fabs v 509 put tad_ka i 2 510 end 511 ret 512 end 513 if eq op AD_MATMUL 514 ad_ref_matmul 515 ret 516 end 517 if eq op AD_TRANSPOSE 518 m = get tad_sa 0 519 n_d = get tad_sa 1 520 for i ulong m bound 16 521 for j ulong n_d bound 16 522 rv = f64 get tad_r add mul j m i 523 put tad_ra add mul i n_d j rv 524 put tad_aa add mul i n_d j fabs rv 525 end 526 end 527 ret 528 end 529 ad_ref_reduce 530 end 531 532 fn ad_bound_viols which ulong to ulong 533 n = get tad_cm 9 534 if eq which 1 535 n = get tad_cm 10 536 end 537 bad = ulong 0 538 for i ulong n bound 128 539 an = f64 get tad_ga i 540 rf = get tad_ra i 541 ab = get tad_aa i 542 kv = get tad_ka i 543 if eq which 1 544 an = f64 get tad_gb i 545 rf = get tad_rb i 546 ab = get tad_ab i 547 kv = get tad_kb i 548 end 549 lim = add add mul ad_gamma kv ab mul 0.`000000000001f64` ab 0.`000000000000000000000000000001f64` 550 if not le fabs sub an rf lim 551 bad = add bad 1 552 end 553 end 554 ret bad 555 end 556 557 note ---- Layer 2: bit-exact M20 replay ---- 558 559 fn ad_rp_take cs ulong cg ulong which ulong to long 560 nt = get tad_cm 9 561 if eq which 1 562 nt = get tad_cm 10 563 end 564 buf = new f32 128 0.0f32 565 rc = ot_read_f32 cs cg part buf 0 nt 566 rr = ot_release cs cg 567 if ne rc OT_OK 568 ret rc 569 end 570 for i ulong nt bound 128 571 if eq which 0 572 put tad_ea i get buf i 573 else 574 put tad_eb i get buf i 575 end 576 end 577 ret OT_OK 578 end 579 580 fn ad_rp_out fs ulong fg ulong which ulong to long 581 rt = get tad_cm 1 582 if eq which 1 583 rt = get tad_cm 2 584 end 585 cs = fs 586 cg = fg 587 nh = new ulong 2 0 588 while gt get ot_ts_meta add mul cs 10 4 rt bound 8 589 rc = ot_reduce OT_TR_SUM cs cg 0 0 nh 590 rr = ot_release cs cg 591 if ne rc OT_OK 592 ret rc 593 end 594 cs = get nh 0 595 cg = get nh 1 596 end 597 for d ulong rt bound 8 598 want = get tad_sa d 599 if eq which 1 600 want = get tad_sb d 601 end 602 if eq want 1 603 cur_d = get ot_ts_shape add mul cs 8 d 604 if ne cur_d 1 605 rc = ot_reduce OT_TR_SUM cs cg d 1 nh 606 rr = ot_release cs cg 607 if ne rc OT_OK 608 ret rc 609 end 610 cs = get nh 0 611 cg = get nh 1 612 end 613 end 614 end 615 ret ad_rp_take cs cg which 616 end 617 618 fn ad_rp_max_load h_idx ulong ry ulong sy view ulong 8 dst edit f32 128 to long 619 ny = get tad_cm 11 620 x = new ulong 2 0 621 rc = ot_broadcast_to get tad_h mul h_idx 2 get tad_h add mul h_idx 2 1 ry sy x 622 if ne rc OT_OK 623 ret rc 624 end 625 rc = ot_read_f32 get x 0 get x 1 part dst 0 ny 626 rr = ot_release get x 0 get x 1 627 ret rc 628 end 629 630 fn ad_rp_max to long 631 ry = get tad_cm 8 632 ny = get tad_cm 11 633 sy = new ulong 8 0 634 x = new ulong 2 0 635 va = new f32 128 0.0f32 636 vb = new f32 128 0.0f32 637 for d ulong ry bound 8 638 put sy d get tad_sy d 639 end 640 rc = ad_rp_max_load 0 ry sy va 641 if eq rc OT_OK 642 rc = ad_rp_max_load 1 ry sy vb 643 end 644 if ne rc OT_OK 645 ret rc 646 end 647 for i ulong ny bound 128 648 gvi = get tad_r i 649 if ad_max_routes_a get va i get vb i 650 put va i gvi 651 put vb i 0.0f32 652 else 653 put va i 0.0f32 654 put vb i gvi 655 end 656 end 657 rc = ot_from_f32 ry sy part va 0 ny x 658 if eq rc OT_OK 659 rc = ad_rp_out get x 0 get x 1 0 660 end 661 if eq rc OT_OK 662 rc = ot_from_f32 ry sy part vb 0 ny x 663 if eq rc OT_OK 664 rc = ad_rp_out get x 0 get x 1 1 665 end 666 end 667 ret rc 668 end 669 670 fn ad_rp_div to long 671 x = new ulong 2 0 672 y = new ulong 2 0 673 x2 = new ulong 2 0 674 k = new ulong 2 0 675 rc = ot_binary OT_TB_DIV get tad_h 4 get tad_h 5 get tad_h 2 get tad_h 3 x 676 if ne rc OT_OK 677 ret rc 678 end 679 rc = ad_rp_out get x 0 get x 1 0 680 if ne rc OT_OK 681 ret rc 682 end 683 rc = ot_binary OT_TB_DIV get tad_h 0 get tad_h 1 get tad_h 2 get tad_h 3 y 684 if ne rc OT_OK 685 ret rc 686 end 687 rc = ot_binary OT_TB_DIV get y 0 get y 1 get tad_h 2 get tad_h 3 x2 688 rr = ot_release get y 0 get y 1 689 if ne rc OT_OK 690 ret rc 691 end 692 rc = ot_binary OT_TB_MUL get tad_h 4 get tad_h 5 get x2 0 get x2 1 x 693 rr = ot_release get x2 0 get x2 1 694 if ne rc OT_OK 695 ret rc 696 end 697 rc = ad_scalar -1.0f32 k 698 if ne rc OT_OK 699 rr = ot_release get x 0 get x 1 700 ret rc 701 end 702 rc = ot_binary OT_TB_MUL get x 0 get x 1 get k 0 get k 1 x2 703 rr = ot_release get k 0 get k 1 704 rr = ot_release get x 0 get x 1 705 if ne rc OT_OK 706 ret rc 707 end 708 ret ad_rp_out get x2 0 get x2 1 1 709 end 710 711 fn ad_rp_reduce to long 712 ra = get tad_cm 1 713 ax = get tad_cm 4 714 ks = new ulong 8 0 715 sa = new ulong 8 0 716 r = new ulong 2 0 717 bc = new ulong 2 0 718 x2 = new ulong 2 0 719 for d ulong ra bound 8 720 put sa d get tad_sa d 721 if eq d ax 722 put ks d 1 723 else 724 put ks d get tad_sa d 725 end 726 end 727 rc = ot_reshape get tad_h 4 get tad_h 5 ra ks r 728 if ne rc OT_OK 729 ret rc 730 end 731 rc = ot_broadcast_to get r 0 get r 1 ra sa bc 732 if ne rc OT_OK 733 rr = ot_release get r 0 get r 1 734 ret rc 735 end 736 rc = ot_contiguous get bc 0 get bc 1 x2 737 rr = ot_release get bc 0 get bc 1 738 rr = ot_release get r 0 get r 1 739 if ne rc OT_OK 740 ret rc 741 end 742 if eq get tad_cm 0 AD_SUM 743 ret ad_rp_out get x2 0 get x2 1 0 744 end 745 rc = ad_scalar f32 get tad_sa ax r 746 if ne rc OT_OK 747 rr = ot_release get x2 0 get x2 1 748 ret rc 749 end 750 rc = ot_binary OT_TB_DIV get x2 0 get x2 1 get r 0 get r 1 bc 751 rr = ot_release get r 0 get r 1 752 rr = ot_release get x2 0 get x2 1 753 if ne rc OT_OK 754 ret rc 755 end 756 ret ad_rp_out get bc 0 get bc 1 0 757 end 758 759 fn ad_replay_case to long 760 op = get tad_cm 0 761 x = new ulong 2 0 762 x2 = new ulong 2 0 763 k = new ulong 2 0 764 if or eq op AD_ADD eq op AD_SUB 765 rc = ot_contiguous get tad_h 4 get tad_h 5 x 766 if eq rc OT_OK 767 rc = ad_rp_out get x 0 get x 1 0 768 end 769 if ne rc OT_OK 770 ret rc 771 end 772 if eq op AD_ADD 773 rc = ot_contiguous get tad_h 4 get tad_h 5 x 774 else 775 rc = ad_scalar -1.0f32 k 776 if eq rc OT_OK 777 rc = ot_binary OT_TB_MUL get tad_h 4 get tad_h 5 get k 0 get k 1 x 778 rr = ot_release get k 0 get k 1 779 end 780 end 781 if eq rc OT_OK 782 rc = ad_rp_out get x 0 get x 1 1 783 end 784 ret rc 785 end 786 if eq op AD_MUL 787 rc = ot_binary OT_TB_MUL get tad_h 4 get tad_h 5 get tad_h 2 get tad_h 3 x 788 if eq rc OT_OK 789 rc = ad_rp_out get x 0 get x 1 0 790 end 791 if eq rc OT_OK 792 rc = ot_binary OT_TB_MUL get tad_h 4 get tad_h 5 get tad_h 0 get tad_h 1 x 793 end 794 if eq rc OT_OK 795 rc = ad_rp_out get x 0 get x 1 1 796 end 797 ret rc 798 end 799 if eq op AD_DIV 800 ret ad_rp_div 801 end 802 if eq op AD_MAX 803 ret ad_rp_max 804 end 805 if eq op AD_SQRT 806 rc = ot_unary OT_TU_SQRT get tad_h 0 get tad_h 1 x 807 if eq rc OT_OK 808 rc = ot_binary OT_TB_ADD get x 0 get x 1 get x 0 get x 1 x2 809 rr = ot_release get x 0 get x 1 810 end 811 if eq rc OT_OK 812 rc = ot_binary OT_TB_DIV get tad_h 4 get tad_h 5 get x2 0 get x2 1 x 813 rr = ot_release get x2 0 get x2 1 814 end 815 if eq rc OT_OK 816 rc = ad_rp_out get x 0 get x 1 0 817 end 818 ret rc 819 end 820 if eq op AD_MATMUL 821 rc = ot_transpose get tad_h 2 get tad_h 3 x2 822 if eq rc OT_OK 823 rc = ot_matmul get tad_h 4 get tad_h 5 get x2 0 get x2 1 x 824 rr = ot_release get x2 0 get x2 1 825 end 826 if eq rc OT_OK 827 rc = ad_rp_out get x 0 get x 1 0 828 end 829 if ne rc OT_OK 830 ret rc 831 end 832 rc = ot_transpose get tad_h 0 get tad_h 1 x2 833 if eq rc OT_OK 834 rc = ot_matmul get x2 0 get x2 1 get tad_h 4 get tad_h 5 x 835 rr = ot_release get x2 0 get x2 1 836 end 837 if eq rc OT_OK 838 rc = ad_rp_out get x 0 get x 1 1 839 end 840 ret rc 841 end 842 if eq op AD_TRANSPOSE 843 rc = ot_transpose get tad_h 4 get tad_h 5 x2 844 if eq rc OT_OK 845 rc = ot_contiguous get x2 0 get x2 1 x 846 rr = ot_release get x2 0 get x2 1 847 end 848 if eq rc OT_OK 849 rc = ad_rp_out get x 0 get x 1 0 850 end 851 ret rc 852 end 853 if eq op AD_BROADCAST 854 rc = ot_contiguous get tad_h 4 get tad_h 5 x 855 if eq rc OT_OK 856 rc = ad_rp_out get x 0 get x 1 0 857 end 858 ret rc 859 end 860 ret ad_rp_reduce 861 end 862 863 note ---- Layer 3: central finite differences ---- 864 865 fn ad_gen_data fd_mode ulong to void 866 na = get tad_cm 9 867 nb = get tad_cm 10 868 dm = get tad_cm 7 869 op = get tad_cm 0 870 for i ulong nb bound 128 871 put tad_b i ad_urand get tad_lim 2 get tad_lim 3 872 end 873 for i ulong na bound 128 874 if eq dm 1 875 m = ad_urand 0.1f32 1.0f32 876 if lt ad_urand 0.0f32 1.0f32 0.5f32 877 put tad_a i neg m 878 else 879 put tad_a i m 880 end 881 else 882 if eq dm 2 883 m = ad_urand 0.1f32 0.6f32 884 if lt ad_urand 0.0f32 1.0f32 0.5f32 885 put tad_a i sub get tad_b i m 886 else 887 put tad_a i add get tad_b i m 888 end 889 else 890 put tad_a i ad_urand get tad_lim 0 get tad_lim 1 891 end 892 end 893 end 894 if ne fd_mode 0 895 if or eq op AD_MUL eq op AD_MATMUL 896 for i ulong na bound 128 897 put tad_a i ad_urand 0.5f32 1.0f32 898 end 899 for i ulong nb bound 128 900 put tad_b i ad_urand 0.5f32 1.0f32 901 end 902 else 903 if eq op AD_DIV 904 for i ulong na bound 128 905 put tad_a i ad_urand 1.0f32 2.0f32 906 end 907 end 908 end 909 end 910 end 911 912 fn ad_fwd_loss to f64 913 ybuf = new f32 128 0.0f32 914 rc = ad_mk_g 0 0 915 if ne get tad_cm 3 0 916 rc = ad_mk_g 1 1 917 end 918 rc = ad_tape_init 8 1024 919 rc = ad_build_case 920 iy = get tad_ids 2 921 ny = get tad_cm 11 922 ys = get ad_nodes add mul iy 12 8 923 yg = get ad_nodes add mul iy 12 9 924 rc = ot_read_f32 ys yg part ybuf 0 ny 925 s = 0.0f64 926 for i ulong ny bound 128 927 s = add s mul f64 get ybuf i f64 get tad_r i 928 end 929 ad_tape_release 930 ad_rel_h 0 931 if ne get tad_cm 3 0 932 ad_rel_h 1 933 end 934 ret s 935 end 936 937 fn ad_run_tape_grads rlo f32 rhi f32 do_replay ulong to void 938 gb = new f32 128 0.0f32 939 ad_chk eq ad_mk_g 0 0 OT_OK 940 if ne get tad_cm 3 0 941 ad_chk eq ad_mk_g 1 1 OT_OK 942 end 943 ad_chk eq ad_tape_init 8 1024 AD_OK 944 ad_chk eq ad_build_case AD_OK 945 iy = get tad_ids 2 946 ys = get ad_nodes add mul iy 12 8 947 ry = get ot_ts_meta add mul ys 10 4 948 ny = get ot_ts_meta add mul ys 10 6 949 put tad_cm 8 ry 950 put tad_cm 11 ny 951 for d ulong ry bound 8 952 put tad_sy d get ot_ts_shape add mul ys 8 d 953 end 954 for i ulong ny bound 128 955 put tad_r i ad_urand rlo rhi 956 end 957 ad_chk eq ad_mk_g 2 2 OT_OK 958 ad_chk eq ad_backward_seed iy get tad_h 4 get tad_h 5 AD_OK 959 na = get tad_cm 9 960 nb = get tad_cm 10 961 ad_chk eq ad_grad_read_f32 get tad_ids 0 part gb 0 na AD_OK 962 for i ulong na bound 128 963 put tad_ga i get gb i 964 end 965 if ne get tad_cm 3 0 966 ad_chk eq ad_grad_read_f32 get tad_ids 1 part gb 0 nb AD_OK 967 for i ulong nb bound 128 968 put tad_gb i get gb i 969 end 970 end 971 ad_tape_release 972 if ne do_replay 0 973 ad_chk eq ad_replay_case OT_OK 974 end 975 ad_rel_h 2 976 ad_rel_h 0 977 if ne get tad_cm 3 0 978 ad_rel_h 1 979 end 980 end 981 982 fn ad_fd_one which ulong i ulong to bool 983 x0 = get tad_a i 984 an = f64 get tad_ga i 985 if eq which 1 986 x0 = get tad_b i 987 an = f64 get tad_gb i 988 end 989 xp = f32 add f64 x0 TAD_FD_H 990 xm = f32 sub f64 x0 TAD_FD_H 991 if eq which 0 992 put tad_a i xp 993 else 994 put tad_b i xp 995 end 996 lp = ad_fwd_loss 997 if eq which 0 998 put tad_a i xm 999 else 1000 put tad_b i xm 1001 end 1002 lm = ad_fwd_loss 1003 if eq which 0 1004 put tad_a i x0 1005 else 1006 put tad_b i x0 1007 end 1008 fd = div sub lp lm sub f64 xp f64 xm 1009 ret ad_fd_ok an fd 1010 end 1011 1012 fn ad_check_fd_side which ulong to void 1013 nx = get tad_cm 9 1014 if eq which 1 1015 nx = get tad_cm 10 1016 end 1017 bad = ulong 0 1018 for i ulong nx bound 128 1019 if not ad_fd_one which i 1020 bad = add bad 1 1021 end 1022 end 1023 ad_chk eq bad 0 1024 end 1025 1026 fn ad_run_one_case chk_flip ulong to void 1027 put tad_cm 9 ad_numel get tad_cm 1 0 1028 if ne get tad_cm 3 0 1029 put tad_cm 10 ad_numel get tad_cm 2 1 1030 else 1031 put tad_cm 10 0 1032 end 1033 ad_gen_data 0 1034 ad_run_tape_grads -1.0f32 1.0f32 1 1035 na = get tad_cm 9 1036 nb = get tad_cm 10 1037 mm_a = ulong 0 1038 for i ulong na bound 128 1039 if ne bitcast uint get tad_ga i bitcast uint get tad_ea i 1040 mm_a = add mm_a 1 1041 end 1042 end 1043 ad_chk eq mm_a 0 1044 if ne get tad_cm 3 0 1045 mm_b = ulong 0 1046 for i ulong nb bound 128 1047 if ne bitcast uint get tad_gb i bitcast uint get tad_eb i 1048 mm_b = add mm_b 1 1049 end 1050 end 1051 ad_chk eq mm_b 0 1052 end 1053 if ne chk_flip 0 1054 b0 = xor bitcast uint get tad_ea 0 uint 1 1055 ad_chk ne bitcast uint get tad_ga 0 b0 1056 end 1057 ad_compute_ref 1058 ad_chk eq ad_bound_viols 0 0 1059 if ne get tad_cm 3 0 1060 ad_chk eq ad_bound_viols 1 0 1061 end 1062 ad_gen_data 1 1063 ad_run_tape_grads 0.5f32 1.0f32 0 1064 ad_check_fd_side 0 1065 if ne get tad_cm 3 0 1066 ad_check_fd_side 1 1067 end 1068 end 1069 1070 fn ad_set_lim alo f32 ahi f32 blo f32 bhi f32 to void 1071 put tad_lim 0 alo 1072 put tad_lim 1 ahi 1073 put tad_lim 2 blo 1074 put tad_lim 3 bhi 1075 end 1076 1077 fn ad_set_bin op ulong ra ulong rb ulong dm ulong to void 1078 put tad_cm 0 op 1079 put tad_cm 1 ra 1080 put tad_cm 2 rb 1081 put tad_cm 3 1 1082 put tad_cm 4 0 1083 put tad_cm 5 0 1084 put tad_cm 6 0 1085 put tad_cm 7 dm 1086 end 1087 1088 fn ad_set_un op ulong ra ulong ax ulong kp ulong rt ulong to void 1089 put tad_cm 0 op 1090 put tad_cm 1 ra 1091 put tad_cm 2 0 1092 put tad_cm 3 0 1093 put tad_cm 4 ax 1094 put tad_cm 5 kp 1095 put tad_cm 6 rt 1096 put tad_cm 7 0 1097 end 1098 1099 fn test_all_21_cases_a to void 1100 ad_set_lim -2.0f32 2.0f32 -2.0f32 2.0f32 1101 put tad_sa 0 3 1102 put tad_sa 1 4 1103 put tad_sb 0 4 1104 ad_set_bin AD_ADD 2 1 0 1105 ad_run_one_case 1 1106 put tad_sa 0 2 1107 put tad_sa 1 1 1108 put tad_sa 2 3 1109 put tad_sb 0 4 1110 put tad_sb 1 1 1111 ad_set_bin AD_ADD 3 2 0 1112 ad_run_one_case 0 1113 put tad_sa 0 3 1114 put tad_sa 1 4 1115 put tad_sb 0 3 1116 put tad_sb 1 4 1117 ad_set_bin AD_ADD 2 2 0 1118 ad_run_one_case 0 1119 put tad_sb 0 3 1120 put tad_sb 1 1 1121 ad_set_bin AD_SUB 2 2 0 1122 ad_run_one_case 0 1123 put tad_sb 0 3 1124 put tad_sb 1 4 1125 ad_set_bin AD_SUB 2 2 0 1126 ad_run_one_case 0 1127 put tad_sb 0 1 1128 put tad_sb 1 4 1129 ad_set_bin AD_MUL 2 2 0 1130 ad_run_one_case 0 1131 put tad_sa 0 2 1132 put tad_sa 1 3 1133 put tad_sb 0 1 1134 ad_set_bin AD_MUL 2 1 0 1135 ad_run_one_case 0 1136 ad_set_lim -2.0f32 2.0f32 1.0f32 2.0f32 1137 put tad_sa 0 3 1138 put tad_sa 1 4 1139 put tad_sb 0 4 1140 ad_set_bin AD_DIV 2 1 0 1141 ad_run_one_case 0 1142 put tad_sb 0 3 1143 put tad_sb 1 4 1144 ad_set_bin AD_DIV 2 2 0 1145 ad_run_one_case 0 1146 ad_set_lim -1.0f32 1.0f32 0.0f32 0.0f32 1147 put tad_sb 0 1 1148 ad_set_bin AD_MAX 2 1 1 1149 ad_run_one_case 0 1150 ad_set_lim -1.0f32 1.0f32 -1.0f32 1.0f32 1151 put tad_sb 0 3 1152 put tad_sb 1 4 1153 ad_set_bin AD_MAX 2 2 2 1154 ad_run_one_case 0 1155 end 1156 1157 fn test_all_21_cases_b to void 1158 ad_set_lim 1.0f32 4.0f32 0.0f32 0.0f32 1159 put tad_sa 0 3 1160 put tad_sa 1 4 1161 ad_set_un AD_SQRT 2 0 0 0 1162 ad_run_one_case 0 1163 ad_set_lim -1.0f32 1.0f32 -1.0f32 1.0f32 1164 put tad_sa 0 3 1165 put tad_sa 1 5 1166 put tad_sb 0 5 1167 put tad_sb 1 4 1168 ad_set_bin AD_MATMUL 2 2 0 1169 ad_run_one_case 0 1170 put tad_sa 0 2 1171 put tad_sa 1 3 1172 put tad_sa 2 5 1173 put tad_sb 0 5 1174 put tad_sb 1 4 1175 ad_set_bin AD_MATMUL 3 2 0 1176 ad_run_one_case 0 1177 ad_set_lim -2.0f32 2.0f32 0.0f32 0.0f32 1178 put tad_sa 0 3 1179 put tad_sa 1 4 1180 ad_set_un AD_TRANSPOSE 2 0 0 0 1181 ad_run_one_case 0 1182 put tad_sa 0 4 1183 put tad_st 0 3 1184 put tad_st 1 4 1185 ad_set_un AD_BROADCAST 1 0 0 2 1186 ad_run_one_case 0 1187 put tad_sa 0 3 1188 put tad_sa 1 1 1189 put tad_st 0 2 1190 put tad_st 1 3 1191 put tad_st 2 4 1192 ad_set_un AD_BROADCAST 2 0 0 3 1193 ad_run_one_case 0 1194 put tad_sa 0 3 1195 put tad_sa 1 4 1196 ad_set_un AD_SUM 2 1 0 0 1197 ad_run_one_case 0 1198 ad_set_un AD_SUM 2 0 1 0 1199 ad_run_one_case 0 1200 ad_set_un AD_MEAN 2 1 0 0 1201 ad_run_one_case 0 1202 ad_set_un AD_MEAN 2 0 1 0 1203 ad_run_one_case 0 1204 end 1205 1206 note ---- 2. comparator counterexamples ---- 1207 1208 fn test_comparators_reject to void 1209 ad_set_lim -2.0f32 2.0f32 -2.0f32 2.0f32 1210 put tad_sa 0 3 1211 put tad_sa 1 4 1212 put tad_sb 0 1 1213 put tad_sb 1 4 1214 ad_set_bin AD_MUL 2 2 0 1215 put tad_cm 9 12 1216 put tad_cm 10 4 1217 put tad_cm 8 2 1218 put tad_cm 11 12 1219 put tad_sy 0 3 1220 put tad_sy 1 4 1221 ad_gen_data 0 1222 for i ulong 12 1223 put tad_r i ad_urand -1.0f32 1.0f32 1224 end 1225 ad_compute_ref 1226 for i ulong 12 1227 put tad_ga i f32 mul get tad_ra i 1.`0000009536743164f64` 1228 end 1229 ad_chk gt ad_bound_viols 0 0 1230 for i ulong 12 1231 put tad_ga i f32 get tad_ra i 1232 end 1233 ad_chk eq ad_bound_viols 0 0 1234 ad_gen_data 1 1235 for i ulong 12 1236 put tad_r i ad_urand 0.5f32 1.0f32 1237 end 1238 ad_compute_ref 1239 facc = ulong 0 1240 fneg = ulong 0 1241 fbig = ulong 0 1242 for i ulong 12 1243 x0 = get tad_a i 1244 xp = f32 add f64 x0 TAD_FD_H 1245 xm = f32 sub f64 x0 TAD_FD_H 1246 put tad_a i xp 1247 lp = ad_fwd_loss 1248 put tad_a i xm 1249 lm = ad_fwd_loss 1250 put tad_a i x0 1251 fd = div sub lp lm sub f64 xp f64 xm 1252 rf = get tad_ra i 1253 if ad_fd_ok rf fd 1254 facc = add facc 1 1255 end 1256 if not ad_fd_ok sub 0.0f64 rf fd 1257 fneg = add fneg 1 1258 end 1259 if not ad_fd_ok mul rf 1.002f64 fd 1260 fbig = add fbig 1 1261 end 1262 end 1263 ad_chk eq facc 12 1264 ad_chk gt fneg 0 1265 ad_chk eq fbig 12 1266 end 1267 1268 note ---- 3. MAX ties go to b ---- 1269 1270 fn test_max_ties to void 1271 sh = new ulong 8 0 1272 buf = new f32 4 0.0f32 1273 h = new ulong 2 0 1274 tmp = new ulong 1 0 1275 put sh 0 4 1276 put buf 0 0.5f32 1277 put buf 1 -0.25f32 1278 put buf 2 0.0f32 1279 put buf 3 bitcast f32 OT_NEG0_BITS 1280 ad_chk eq ot_from_f32 1 sh buf h OT_OK 1281 ad_save_h 0 h 1282 put buf 2 bitcast f32 OT_NEG0_BITS 1283 put buf 3 0.0f32 1284 ad_chk eq ot_from_f32 1 sh buf h OT_OK 1285 ad_save_h 1 h 1286 put buf 0 1.0f32 1287 put buf 1 2.0f32 1288 put buf 2 3.0f32 1289 put buf 3 4.0f32 1290 ad_chk eq ot_from_f32 1 sh buf h OT_OK 1291 ad_save_h 2 h 1292 ad_chk eq ad_tape_init 4 12 AD_OK 1293 ad_chk eq ad_leaf get tad_h 0 get tad_h 1 1 tmp AD_OK 1294 put tad_ids 0 get tmp 0 1295 ad_chk eq ad_leaf get tad_h 2 get tad_h 3 1 tmp AD_OK 1296 put tad_ids 1 get tmp 0 1297 ad_chk eq ad_binary AD_MAX get tad_ids 0 get tad_ids 1 tmp AD_OK 1298 ad_chk eq ad_backward_seed get tmp 0 get tad_h 4 get tad_h 5 AD_OK 1299 ad_chk eq ad_grad_read_f32 get tad_ids 0 buf AD_OK 1300 for i ulong 4 1301 ad_chk eq get buf i 0.0f32 1302 end 1303 ad_chk eq ad_grad_read_f32 get tad_ids 1 buf AD_OK 1304 for i ulong 4 1305 ad_chk eq get buf i f32 add i 1 1306 end 1307 ad_tape_release 1308 ad_rel_h 0 1309 ad_rel_h 1 1310 ad_rel_h 2 1311 ad_chk_leaks 1312 end 1313 1314 note ---- 4. composite linear + MSE fan-out graph & 5. determinism ---- 1315 1316 fn ad_lin_setup to void 1317 sh = new ulong 8 0 1318 buf = new f32 12 0.0f32 1319 h = new ulong 2 0 1320 put sh 0 4 1321 put sh 1 3 1322 for i ulong 12 1323 put buf i get tad_lx i 1324 end 1325 ad_chk eq ot_from_f32 2 sh buf h OT_OK 1326 ad_save_h 0 h 1327 put sh 0 3 1328 put sh 1 2 1329 for i ulong 6 1330 put buf i get tad_lw i 1331 end 1332 ad_chk eq ot_from_f32 2 sh part buf 0 6 h OT_OK 1333 ad_save_h 1 h 1334 put sh 0 2 1335 put buf 0 get tad_lb 0 1336 put buf 1 get tad_lb 1 1337 ad_chk eq ot_from_f32 1 sh part buf 0 2 h OT_OK 1338 ad_save_h 2 h 1339 put sh 0 4 1340 put sh 1 2 1341 for i ulong 8 1342 put buf i get tad_ly i 1343 end 1344 ad_chk eq ot_from_f32 2 sh part buf 0 8 h OT_OK 1345 ad_save_h 3 h 1346 end 1347 1348 fn ad_lin_bwd_and_lid do_bwd ulong fg_slot ulong lid_slot ulong to void 1349 dv = new f32 8 0.0f32 1350 pids = new ulong 2 0 1351 gx = new ulong 2 0 1352 lid = new uchar 32 0 1353 if ne lid_slot 0 1354 ls = get ad_nodes add mul get tad_ids 9 12 8 1355 lg = get ad_nodes add mul get tad_ids 9 12 9 1356 ad_chk eq ot_value_id ls lg lid OT_OK 1357 for i ulong 32 1358 if eq lid_slot 1 1359 put tad_id1 i get lid i 1360 else 1361 put tad_id2 i get lid i 1362 end 1363 end 1364 end 1365 if ne do_bwd 0 1366 ad_chk eq ad_backward get tad_ids 9 AD_OK 1367 put pids 0 get tad_ids 1 1368 put pids 1 get tad_ids 2 1369 ad_chk eq ad_grads_flatten pids dv AD_OK 1370 for i ulong 8 1371 if eq fg_slot 0 1372 put tad_fg1 i get dv i 1373 else 1374 put tad_fg2 i get dv i 1375 end 1376 end 1377 ad_chk eq ad_grad get tad_ids 0 gx AD_ERR_NO_GRAD 1378 ad_chk eq ad_grad get tad_ids 3 gx AD_ERR_NO_GRAD 1379 ad_chk eq ad_grad get tad_ids 1 gx AD_OK 1380 end 1381 end 1382 1383 fn ad_lin_run do_bwd ulong fg_slot ulong lid_slot ulong to f64 1384 tmp = new ulong 1 0 1385 dv = new f32 8 0.0f32 1386 ad_lin_setup 1387 ad_chk eq ad_tape_init 16 0 AD_OK 1388 ad_chk eq ad_leaf get tad_h 0 get tad_h 1 0 tmp AD_OK 1389 put tad_ids 0 get tmp 0 1390 ad_chk eq ad_leaf get tad_h 2 get tad_h 3 1 tmp AD_OK 1391 put tad_ids 1 get tmp 0 1392 ad_chk eq ad_leaf get tad_h 4 get tad_h 5 1 tmp AD_OK 1393 put tad_ids 2 get tmp 0 1394 ad_chk eq ad_leaf get tad_h 6 get tad_h 7 0 tmp AD_OK 1395 put tad_ids 3 get tmp 0 1396 ad_chk eq ad_matmul get tad_ids 0 get tad_ids 1 tmp AD_OK 1397 put tad_ids 4 get tmp 0 1398 ad_chk eq ad_binary AD_ADD get tad_ids 4 get tad_ids 2 tmp AD_OK 1399 put tad_ids 5 get tmp 0 1400 ad_chk eq ad_binary AD_SUB get tad_ids 5 get tad_ids 3 tmp AD_OK 1401 put tad_ids 6 get tmp 0 1402 ad_chk eq ad_binary AD_MUL get tad_ids 6 get tad_ids 6 tmp AD_OK 1403 put tad_ids 7 get tmp 0 1404 ad_chk eq ad_reduce AD_SUM get tad_ids 7 1 0 tmp AD_OK 1405 put tad_ids 8 get tmp 0 1406 ad_chk eq ad_reduce AD_MEAN get tad_ids 8 0 0 tmp AD_OK 1407 put tad_ids 9 get tmp 0 1408 ds = get ad_nodes add mul get tad_ids 6 12 8 1409 dg = get ad_nodes add mul get tad_ids 6 12 9 1410 ad_chk eq ot_read_f32 ds dg dv OT_OK 1411 acc = 0.0f64 1412 for i ulong 8 1413 dvi = f64 get dv i 1414 acc = add acc mul dvi dvi 1415 end 1416 loss_d = div acc 4.0f64 1417 ad_lin_bwd_and_lid do_bwd fg_slot lid_slot 1418 ad_tape_release 1419 ad_rel_h 0 1420 ad_rel_h 1 1421 ad_rel_h 2 1422 ad_rel_h 3 1423 ret loss_d 1424 end 1425 1426 fn test_comp_lin_err to void 1427 for i ulong 4 1428 for j ulong 2 1429 s = f64 get tad_lb j 1430 for k ulong 3 1431 xv = f64 get tad_lx add mul i 3 k 1432 wv = f64 get tad_lw add mul k 2 j 1433 s = add s mul xv wv 1434 end 1435 ij = add mul i 2 j 1436 put tad_ee ij sub s f64 get tad_ly ij 1437 end 1438 end 1439 end 1440 1441 fn test_composite_closed_form to void 1442 test_comp_lin_err 1443 bad_ref = ulong 0 1444 for k ulong 3 1445 for j ulong 2 1446 s = 0.0f64 1447 for i ulong 4 1448 xv = f64 get tad_lx add mul i 3 k 1449 ev = get tad_ee add mul i 2 j 1450 s = add s mul xv ev 1451 end 1452 s = mul s 0.5f64 1453 gv = f64 get tad_fg1 add mul k 2 j 1454 lim = add 0.`00001f64` mul 0.`00001f64` fabs s 1455 if not le fabs sub gv s lim 1456 bad_ref = add bad_ref 1 1457 end 1458 end 1459 end 1460 for j ulong 2 1461 s = 0.0f64 1462 for i ulong 4 1463 s = add s get tad_ee add mul i 2 j 1464 end 1465 s = mul s 0.5f64 1466 gv = f64 get tad_fg1 add 6 j 1467 lim = add 0.`00001f64` mul 0.`00001f64` fabs s 1468 if not le fabs sub gv s lim 1469 bad_ref = add bad_ref 1 1470 end 1471 end 1472 ad_chk eq bad_ref 0 1473 end 1474 1475 fn test_comp_lin_fd_one is_b ulong idx ulong to bool 1476 x0 = get tad_lw idx 1477 gv = f64 get tad_fg1 idx 1478 if ne is_b 0 1479 x0 = get tad_lb idx 1480 gv = f64 get tad_fg1 add 6 idx 1481 end 1482 xp = f32 add f64 x0 TAD_FD_H 1483 xm = f32 sub f64 x0 TAD_FD_H 1484 if eq is_b 0 1485 put tad_lw idx xp 1486 else 1487 put tad_lb idx xp 1488 end 1489 lp = ad_lin_run 0 1 0 1490 if eq is_b 0 1491 put tad_lw idx xm 1492 else 1493 put tad_lb idx xm 1494 end 1495 lm = ad_lin_run 0 1 0 1496 if eq is_b 0 1497 put tad_lw idx x0 1498 else 1499 put tad_lb idx x0 1500 end 1501 fd = div sub lp lm sub f64 xp f64 xm 1502 ret ad_fd_ok gv fd 1503 end 1504 1505 fn test_composite_and_det to void 1506 for i ulong 12 1507 put tad_lx i ad_urand -1.0f32 1.0f32 1508 end 1509 for i ulong 6 1510 put tad_lw i ad_urand -1.0f32 1.0f32 1511 end 1512 for i ulong 2 1513 put tad_lb i ad_urand -1.0f32 1.0f32 1514 end 1515 for i ulong 8 1516 put tad_ly i ad_urand -1.0f32 1.0f32 1517 end 1518 l1 = ad_lin_run 1 0 1 1519 test_composite_closed_form 1520 l2 = ad_lin_run 1 1 2 1521 for i ulong 8 1522 ad_chk eq bitcast uint get tad_fg1 i bitcast uint get tad_fg2 i 1523 end 1524 for i ulong 32 1525 ad_chk eq get tad_id1 i get tad_id2 i 1526 end 1527 ad_chk ne bitcast uint get tad_fg1 3 xor bitcast uint get tad_fg2 3 uint 1 1528 for i ulong 12 1529 put tad_lx i ad_urand 0.5f32 1.0f32 1530 end 1531 for i ulong 6 1532 put tad_lw i ad_urand 0.5f32 1.0f32 1533 end 1534 for i ulong 2 1535 put tad_lb i ad_urand 0.5f32 1.0f32 1536 end 1537 for i ulong 8 1538 put tad_ly i ad_urand -0.5f32 0.0f32 1539 end 1540 l3 = ad_lin_run 1 0 0 1541 fbad = ulong 0 1542 for i ulong 6 1543 if not test_comp_lin_fd_one 0 i 1544 fbad = add fbad 1 1545 end 1546 end 1547 for i ulong 2 1548 if not test_comp_lin_fd_one 1 i 1549 fbad = add fbad 1 1550 end 1551 end 1552 ad_chk eq fbad 0 1553 ad_chk_leaks 1554 end 1555 1556 note ---- 6. error paths, arena/tape/scratch limits, flatten untouched, leaks ---- 1557 1558 fn test_errors_cap to void 1559 tmp = new ulong 1 77 1560 lc = new ulong 2 0 1561 ad_chk eq ad_tape_init 0 0 AD_ERR_BAD_ARGS 1562 ad_chk eq ad_tape_init add AD_MAX_NODES 1 0 AD_ERR_BAD_ARGS 1563 ad_chk eq ad_tape_init 4 add AD_SCRATCH_MAX 1 AD_ERR_BAD_ARGS 1564 ad_chk eq ad_tape_init 3 0 AD_OK 1565 ad_chk eq ad_leaf get tad_h 0 get tad_h 1 1 tmp AD_OK 1566 put tad_ids 0 get tmp 0 1567 ad_chk eq ad_leaf get tad_h 2 get tad_h 3 1 tmp AD_OK 1568 put tad_ids 1 get tmp 0 1569 ad_chk eq ad_binary AD_ADD get tad_ids 0 get tad_ids 1 tmp AD_OK 1570 put tad_ids 2 get tmp 0 1571 ad_chk eq get ad_tape_meta 1 3 1572 ot_live_counts lc 1573 t0 = get lc 0 1574 s0 = get lc 1 1575 put tmp 0 77 1576 ad_chk eq ad_binary AD_ADD get tad_ids 2 get tad_ids 1 tmp AD_ERR_TAPE_FULL 1577 ot_live_counts lc 1578 ad_chk and eq get ad_tape_meta 1 3 eq get tmp 0 77 1579 ad_chk and eq get lc 0 t0 eq get lc 1 s0 1580 ad_chk eq ad_leaf get tad_h 0 get tad_h 1 1 tmp AD_ERR_TAPE_FULL 1581 ad_tape_release 1582 end 1583 1584 fn test_errors_shapes_scratch to void 1585 tmp = new ulong 1 0 1586 lc = new ulong 2 0 1587 s4 = new ulong 8 0 1588 put s4 0 4 1589 ad_chk eq ad_tape_init 8 6 AD_OK 1590 ad_chk eq ad_leaf get tad_h 0 get tad_h 1 1 tmp AD_OK 1591 put tad_ids 0 get tmp 0 1592 ad_chk eq ad_leaf get tad_h 4 get tad_h 5 1 tmp AD_OK 1593 put tad_ids 1 get tmp 0 1594 ad_chk eq ad_leaf get tad_h 6 get tad_h 7 1 tmp AD_OK 1595 put tad_ids 2 get tmp 0 1596 ad_chk eq ad_leaf get tad_h 8 get tad_h 9 1 tmp AD_OK 1597 put tad_ids 3 get tmp 0 1598 ad_chk eq ad_sqrt 99 tmp AD_ERR_BAD_ARGS 1599 ot_live_counts lc 1600 t0 = get lc 0 1601 s0 = get lc 1 1602 cnt0 = get ad_tape_meta 1 1603 ad_chk eq ad_binary AD_ADD get tad_ids 0 get tad_ids 1 tmp OT_ERR_SHAPE 1604 ad_chk eq ad_matmul get tad_ids 2 get tad_ids 3 tmp OT_ERR_SHAPE 1605 ad_chk eq ad_matmul get tad_ids 2 get tad_ids 2 tmp OT_ERR_SHAPE 1606 ad_chk eq ad_broadcast get tad_ids 1 1 s4 tmp OT_ERR_SHAPE 1607 ad_chk eq ad_reduce AD_SUM get tad_ids 0 2 0 tmp OT_ERR_AXIS 1608 ad_chk eq ad_binary AD_MATMUL get tad_ids 0 get tad_ids 0 tmp AD_ERR_BAD_ARGS 1609 ot_live_counts lc 1610 ad_chk eq get ad_tape_meta 1 cnt0 1611 ad_chk eq get lc 0 t0 1612 ad_chk eq get lc 1 s0 1613 ad_chk eq ad_binary AD_MAX get tad_ids 0 get tad_ids 0 tmp AD_OK 1614 imax = get tmp 0 1615 ad_chk eq ad_scratch_need 36 1616 put ad_tape_meta 2 36 1617 ad_chk eq ad_backward imax AD_ERR_SHAPE 1618 ad_chk eq ad_backward_seed imax get tad_h 2 get tad_h 3 AD_ERR_SHAPE 1619 put ad_tape_meta 2 6 1620 ad_chk eq ad_backward_seed imax get tad_h 0 get tad_h 1 AD_ERR_SCRATCH 1621 ad_chk eq get ad_tape_meta 3 0 1622 put ad_tape_meta 2 36 1623 ad_chk eq ad_backward_seed imax get tad_h 0 get tad_h 1 AD_OK 1624 ad_chk eq ad_backward_seed imax get tad_h 0 get tad_h 1 AD_ERR_STATE 1625 ad_chk eq ad_sqrt get tad_ids 0 tmp AD_ERR_STATE 1626 end 1627 1628 fn test_errors_flatten_and_leaves to void 1629 gx = new ulong 2 0 1630 flat13 = new f32 13 7.0f32 1631 dir12 = new f32 12 0.0f32 1632 one_id = new ulong 1 0 1633 put one_id 0 get tad_ids 0 1634 ad_chk eq ad_grad get tad_ids 0 gx AD_OK 1635 ad_chk eq ad_grad get tad_ids 2 gx AD_ERR_NO_GRAD 1636 ad_chk eq ad_grad_read_f32 get tad_ids 0 part dir12 0 11 AD_ERR_COUNT 1637 ad_chk eq ad_grads_flatten one_id flat13 AD_ERR_COUNT 1638 for i ulong 13 1639 ad_chk eq get flat13 i 7.0f32 1640 end 1641 ad_chk eq ad_grads_flatten one_id part flat13 0 12 AD_OK 1642 ad_chk eq ad_grad_read_f32 get tad_ids 0 dir12 AD_OK 1643 for i ulong 12 1644 ad_chk eq bitcast uint get flat13 i bitcast uint get dir12 i 1645 end 1646 ad_tape_release 1647 note Root without req_grad, F16 leaf, stale leaf 1648 ad_chk eq ad_tape_init 4 0 AD_OK 1649 ad_chk eq ad_leaf get tad_h 0 get tad_h 1 0 one_id AD_OK 1650 ia = get one_id 0 1651 ad_chk eq ad_binary AD_ADD ia ia one_id AD_OK 1652 ad_chk eq ad_backward_seed get one_id 0 get tad_h 0 get tad_h 1 AD_ERR_NO_GRAD 1653 ad_chk eq ad_leaf get tad_h 10 get tad_h 11 1 one_id OT_ERR_DTYPE 1654 ad_rel_h 5 1655 ad_chk eq ad_leaf get tad_h 10 get tad_h 11 1 one_id OT_ERR_STALE 1656 ad_tape_release 1657 end 1658 1659 fn test_errors to void 1660 note tad_h: 0=ha[3,4], 1=hb4[4], 2=hb5[5], 3=hm35[3,5], 4=hm42[4,2], 5=hf16[4] 1661 s = new ulong 8 0 1662 d = new f32 20 1.0f32 1663 hb8 = new uchar 8 0 1664 h = new ulong 2 0 1665 put s 0 3 1666 put s 1 4 1667 ad_chk eq ot_from_f32 2 s part d 0 12 h OT_OK 1668 ad_save_h 0 h 1669 put s 0 4 1670 ad_chk eq ot_from_f32 1 s part d 0 4 h OT_OK 1671 ad_save_h 1 h 1672 put s 0 5 1673 ad_chk eq ot_from_f32 1 s part d 0 5 h OT_OK 1674 ad_save_h 2 h 1675 put s 0 3 1676 put s 1 5 1677 ad_chk eq ot_from_f32 2 s part d 0 15 h OT_OK 1678 ad_save_h 3 h 1679 put s 0 4 1680 put s 1 2 1681 ad_chk eq ot_from_f32 2 s part d 0 8 h OT_OK 1682 ad_save_h 4 h 1683 put s 0 4 1684 ad_chk eq ot_from_bytes OT_DT_F16 1 s hb8 h OT_OK 1685 ad_save_h 5 h 1686 test_errors_cap 1687 test_errors_shapes_scratch 1688 test_errors_flatten_and_leaves 1689 for i ulong 5 1690 ad_rel_h i 1691 end 1692 ad_chk_leaks 1693 end 1694 1695 fn main to int 1696 put tad_rng 0 uint 0x2545f491 1697 ad_chk eq ot_ctx_init 256 OT_OK 1698 test_all_21_cases_a 1699 test_all_21_cases_b 1700 test_comparators_reject 1701 test_max_ties 1702 test_composite_and_det 1703 test_errors 1704 ad_chk_leaks 1705 tot = get tad_c 0 1706 fails = get tad_c 1 1707 w1 = write 1 tad_sum1 0 len tad_sum1 1708 ad_w_u64 1 sub tot fails 1709 w2 = write 1 tad_sum2 0 len tad_sum2 1710 ad_w_u64 1 fails 1711 w3 = write 1 tad_sum3 0 len tad_sum3 1712 if ne fails 0 1713 w4 = write 1 tad_fail 0 len tad_fail 1714 ret 1 1715 end 1716 w4 = write 1 tad_pass 0 len tad_pass 1717 ret 0 1718 end