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/omega_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 / omega_autodiff.om` 997 lines ยท 22335 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note omega_autodiff.om -- Pure seeded OM-2 M21 reverse-mode autodiff tape over M20 OMEGA_TENSOR. 3 note Composed with gradient_shape.om, tensor_shape.om, tensor_ops.om, tg_sha256.om, 4 note omega_tensor_cpu.om, and omega_tensor.om. 5 6 const AD_NONE ulong 0xffffffff 7 const AD_MAX_NODES ulong 256 8 const AD_SCRATCH_MAX ulong 12288 9 10 const AD_OK long 0 11 const AD_ERR_BAD_ARGS long -101 12 const AD_ERR_TAPE_FULL long -102 13 const AD_ERR_SHAPE long -103 14 const AD_ERR_STATE long -104 15 const AD_ERR_NO_GRAD long -105 16 const AD_ERR_SCRATCH long -106 17 const AD_ERR_COUNT long -107 18 19 const AD_LEAF ulong 0 20 const AD_ADD ulong 1 21 const AD_SUB ulong 2 22 const AD_MUL ulong 3 23 const AD_DIV ulong 4 24 const AD_MAX ulong 5 25 const AD_SQRT ulong 6 26 const AD_MATMUL ulong 7 27 const AD_TRANSPOSE ulong 8 28 const AD_BROADCAST ulong 9 29 const AD_SUM ulong 10 30 const AD_MEAN ulong 11 31 const AD_OP_COUNT ulong 12 32 33 global ad_tape_meta ulong 6 34 note ad_tape_meta: 0=cap, 1=count, 2=scratch_cap, 3=backward_done, 4=failed 35 global ad_nodes ulong 3072 36 note per node (stride 12): 37 note 0=op, 1=in0, 2=in1, 3=axis, 4=keepdims, 5=req_grad, 38 note 6=owns_val, 7=has_grad, 8=val_s, 9=val_g, 10=grad_s, 11=grad_g 39 global ad_scratch f32 12288 40 41 fn ad_tape_init cap ulong scratch_cap ulong to long 42 if or eq cap 0 or gt cap AD_MAX_NODES gt scratch_cap AD_SCRATCH_MAX 43 ret AD_ERR_BAD_ARGS 44 end 45 put ad_tape_meta 0 cap 46 put ad_tape_meta 1 0 47 put ad_tape_meta 2 scratch_cap 48 put ad_tape_meta 3 0 49 put ad_tape_meta 4 0 50 ret AD_OK 51 end 52 53 fn ad_tape_release to void 54 cnt = get ad_tape_meta 1 55 for k ulong cnt 56 i = sub sub cnt 1 k 57 nb = mul i 12 58 if ne get ad_nodes add nb 7 0 59 r1 = ot_release get ad_nodes add nb 10 get ad_nodes add nb 11 60 put ad_nodes add nb 7 0 61 end 62 if ne get ad_nodes add nb 6 0 63 r2 = ot_release get ad_nodes add nb 8 get ad_nodes add nb 9 64 put ad_nodes add nb 6 0 65 end 66 end 67 put ad_tape_meta 1 0 68 put ad_tape_meta 3 0 69 put ad_tape_meta 4 0 70 end 71 72 fn ad_can_record to long 73 if eq get ad_tape_meta 0 0 74 ret AD_ERR_BAD_ARGS 75 end 76 if or ne get ad_tape_meta 3 0 ne get ad_tape_meta 4 0 77 ret AD_ERR_STATE 78 end 79 if ge get ad_tape_meta 1 get ad_tape_meta 0 80 ret AD_ERR_TAPE_FULL 81 end 82 ret AD_OK 83 end 84 85 fn ad_push op ulong in0 ulong in1 ulong axis_kp ulong flags ulong val_h view ulong 2 to ulong 86 id = get ad_tape_meta 1 87 nb = mul id 12 88 put ad_nodes nb op 89 put ad_nodes add nb 1 in0 90 put ad_nodes add nb 2 in1 91 put ad_nodes add nb 3 div axis_kp 2 92 put ad_nodes add nb 4 rem axis_kp 2 93 put ad_nodes add nb 5 rem flags 2 94 put ad_nodes add nb 6 div flags 2 95 put ad_nodes add nb 7 0 96 put ad_nodes add nb 8 get val_h 0 97 put ad_nodes add nb 9 get val_h 1 98 put ad_nodes add nb 10 0 99 put ad_nodes add nb 11 0 100 put ad_tape_meta 1 add id 1 101 ret id 102 end 103 104 fn ad_leaf val_s ulong val_g ulong req_grad ulong id_out edit ulong 1 to long 105 rc = ad_can_record 106 if ne rc AD_OK 107 ret rc 108 end 109 rc = ot_ts_check val_s val_g 110 if ne rc OT_OK 111 ret rc 112 end 113 if ne get ot_ts_meta add mul val_s 10 3 OT_DT_F32 114 ret OT_ERR_DTYPE 115 end 116 vh = new ulong 2 0 117 put vh 0 val_s 118 put vh 1 val_g 119 rg = ulong 0 120 if ne req_grad 0 121 rg = 1 122 end 123 put id_out 0 ad_push AD_LEAF AD_NONE AD_NONE 0 rg vh 124 ret AD_OK 125 end 126 127 fn ad_record_op op ulong a ulong b ulong axis_kp ulong vh view ulong 2 id_out edit ulong 1 to long 128 rg = get ad_nodes add mul a 12 5 129 if ne b AD_NONE 130 if ne get ad_nodes add mul b 12 5 0 131 rg = 1 132 end 133 end 134 put id_out 0 ad_push op a b axis_kp add 2 rg vh 135 ret AD_OK 136 end 137 138 fn ad_call_ot_binary bop ulong a ulong b ulong vh edit ulong 2 to long 139 nba = mul a 12 140 nbb = mul b 12 141 sa = get ad_nodes add nba 8 142 ga = get ad_nodes add nba 9 143 sb = get ad_nodes add nbb 8 144 gb = get ad_nodes add nbb 9 145 ret ot_binary bop sa ga sb gb vh 146 end 147 148 fn ad_binary op ulong a ulong b ulong id_out edit ulong 1 to long 149 rc = ad_can_record 150 if ne rc AD_OK 151 ret rc 152 end 153 cnt = get ad_tape_meta 1 154 if or ge a cnt ge b cnt 155 ret AD_ERR_BAD_ARGS 156 end 157 bop = ulong 0 158 if eq op AD_ADD 159 bop = OT_TB_ADD 160 else 161 if eq op AD_SUB 162 bop = OT_TB_SUB 163 else 164 if eq op AD_MUL 165 bop = OT_TB_MUL 166 else 167 if eq op AD_DIV 168 bop = OT_TB_DIV 169 else 170 if eq op AD_MAX 171 bop = OT_TB_MAX 172 else 173 ret AD_ERR_BAD_ARGS 174 end 175 end 176 end 177 end 178 end 179 vh = new ulong 2 0 180 rc = ad_call_ot_binary bop a b vh 181 if ne rc OT_OK 182 ret rc 183 end 184 ret ad_record_op op a b 0 vh id_out 185 end 186 187 fn ad_sqrt a ulong id_out edit ulong 1 to long 188 rc = ad_can_record 189 if ne rc AD_OK 190 ret rc 191 end 192 if ge a get ad_tape_meta 1 193 ret AD_ERR_BAD_ARGS 194 end 195 vh = new ulong 2 0 196 nba = mul a 12 197 rc = ot_unary OT_TU_SQRT get ad_nodes add nba 8 get ad_nodes add nba 9 vh 198 if ne rc OT_OK 199 ret rc 200 end 201 ret ad_record_op AD_SQRT a AD_NONE 0 vh id_out 202 end 203 204 fn ad_call_ot_matmul a ulong b ulong vh edit ulong 2 to long 205 nba = mul a 12 206 nbb = mul b 12 207 sa = get ad_nodes add nba 8 208 ga = get ad_nodes add nba 9 209 sb = get ad_nodes add nbb 8 210 gb = get ad_nodes add nbb 9 211 ret ot_matmul sa ga sb gb vh 212 end 213 214 fn ad_matmul a ulong b ulong id_out edit ulong 1 to long 215 rc = ad_can_record 216 if ne rc AD_OK 217 ret rc 218 end 219 cnt = get ad_tape_meta 1 220 if or ge a cnt ge b cnt 221 ret AD_ERR_BAD_ARGS 222 end 223 vh = new ulong 2 0 224 rc = ad_call_ot_matmul a b vh 225 if ne rc OT_OK 226 ret rc 227 end 228 ret ad_record_op AD_MATMUL a b 0 vh id_out 229 end 230 231 fn ad_transpose a ulong id_out edit ulong 1 to long 232 rc = ad_can_record 233 if ne rc AD_OK 234 ret rc 235 end 236 if ge a get ad_tape_meta 1 237 ret AD_ERR_BAD_ARGS 238 end 239 v = new ulong 2 0 240 y = new ulong 2 0 241 nba = mul a 12 242 rc = ot_transpose get ad_nodes add nba 8 get ad_nodes add nba 9 v 243 if ne rc OT_OK 244 ret rc 245 end 246 rc = ot_contiguous get v 0 get v 1 y 247 rr = ot_release get v 0 get v 1 248 if ne rc OT_OK 249 ret rc 250 end 251 ret ad_record_op AD_TRANSPOSE a AD_NONE 0 y id_out 252 end 253 254 fn ad_broadcast a ulong rank ulong shape view ulong 8 id_out edit ulong 1 to long 255 rc = ad_can_record 256 if ne rc AD_OK 257 ret rc 258 end 259 if ge a get ad_tape_meta 1 260 ret AD_ERR_BAD_ARGS 261 end 262 v = new ulong 2 0 263 y = new ulong 2 0 264 nba = mul a 12 265 rc = ot_broadcast_to get ad_nodes add nba 8 get ad_nodes add nba 9 rank shape v 266 if ne rc OT_OK 267 ret rc 268 end 269 rc = ot_contiguous get v 0 get v 1 y 270 rr = ot_release get v 0 get v 1 271 if ne rc OT_OK 272 ret rc 273 end 274 ret ad_record_op AD_BROADCAST a AD_NONE 0 y id_out 275 end 276 277 fn ad_reduce op ulong a ulong axis ulong keepdims ulong id_out edit ulong 1 to long 278 rc = ad_can_record 279 if ne rc AD_OK 280 ret rc 281 end 282 if ge a get ad_tape_meta 1 283 ret AD_ERR_BAD_ARGS 284 end 285 rop = ulong 0 286 if eq op AD_SUM 287 rop = OT_TR_SUM 288 else 289 if eq op AD_MEAN 290 rop = OT_TR_MEAN 291 else 292 ret AD_ERR_BAD_ARGS 293 end 294 end 295 y = new ulong 2 0 296 nba = mul a 12 297 rc = ot_reduce rop get ad_nodes add nba 8 get ad_nodes add nba 9 axis keepdims y 298 if ne rc OT_OK 299 ret rc 300 end 301 kp = ulong 0 302 if ne keepdims 0 303 kp = 1 304 end 305 ret ad_record_op op a AD_NONE add mul axis 2 kp y id_out 306 end 307 308 fn ad_scalar val f32 out_h edit ulong 2 to long 309 sh = new ulong 8 1 310 d = new f32 1 0.0f32 311 put d 0 val 312 ret ot_from_f32 0 sh d out_h 313 end 314 315 fn ad_scale s ulong g ulong k f32 out_h edit ulong 2 to long 316 c = new ulong 2 0 317 rc = ad_scalar k c 318 if ne rc OT_OK 319 ret rc 320 end 321 rc = ot_binary OT_TB_MUL s g get c 0 get c 1 out_h 322 rr = ot_release get c 0 get c 1 323 ret rc 324 end 325 326 fn ad_same_ts_shape sa ulong sb ulong to ulong 327 ra = get ot_ts_meta add mul sa 10 4 328 rb = get ot_ts_meta add mul sb 10 4 329 sha = new ulong 8 0 330 shb = new ulong 8 0 331 for d ulong ra 332 put sha d get ot_ts_shape add mul sa 8 d 333 end 334 for d ulong rb 335 put shb d get ot_ts_shape add mul sb 8 d 336 end 337 ret ad_same_shape part sha 0 ra part shb 0 rb 338 end 339 340 fn ad_unbroadcast_plan fs ulong target_s ulong steps edit uchar 8 to long 341 cr = get ot_ts_meta add mul fs 10 4 342 tr = get ot_ts_meta add mul target_s 10 4 343 csh = new ulong 8 0 344 tsh = new ulong 8 0 345 for d ulong cr 346 put csh d get ot_ts_shape add mul fs 8 d 347 end 348 for d ulong tr 349 put tsh d get ot_ts_shape add mul target_s 8 d 350 end 351 ret ad_unbroadcast_steps part csh 0 cr part tsh 0 tr steps 352 end 353 354 fn ad_unbroadcast_step cs ulong cg ulong st ulong nh edit ulong 2 to long 355 rc = ot_reduce OT_TR_SUM cs cg div st 2 rem st 2 nh 356 rr = ot_release cs cg 357 ret rc 358 end 359 360 fn ad_unbroadcast_run fs ulong fg ulong cnt ulong steps view uchar 8 out_h edit ulong 2 to long 361 cs = fs 362 cg = fg 363 nh = new ulong 2 0 364 for i ulong cnt 365 rc = ad_unbroadcast_step cs cg ulong get steps i nh 366 if ne rc OT_OK 367 ret rc 368 end 369 cs = get nh 0 370 cg = get nh 1 371 end 372 put out_h 0 cs 373 put out_h 1 cg 374 ret AD_OK 375 end 376 377 fn ad_unbroadcast fs ulong fg ulong target_s ulong out_h edit ulong 2 to long 378 rc = ot_ts_check fs fg 379 if ne rc OT_OK 380 rr = ot_release fs fg 381 ret rc 382 end 383 steps = new uchar 8 0 384 cnt = ad_unbroadcast_plan fs target_s steps 385 if lt cnt 0 386 rr = ot_release fs fg 387 ret AD_ERR_SHAPE 388 end 389 ret ad_unbroadcast_run fs fg ulong cnt steps out_h 390 end 391 392 fn ad_accumulate idx ulong cs ulong cg ulong to long 393 nb = mul idx 12 394 vs = get ad_nodes add nb 8 395 if eq ad_same_ts_shape vs cs 0 396 rr = ot_release cs cg 397 ret AD_ERR_SHAPE 398 end 399 if eq get ad_nodes add nb 7 0 400 put ad_nodes add nb 10 cs 401 put ad_nodes add nb 11 cg 402 put ad_nodes add nb 7 1 403 ret AD_OK 404 end 405 sum_h = new ulong 2 0 406 rc = ot_binary OT_TB_ADD get ad_nodes add nb 10 get ad_nodes add nb 11 cs cg sum_h 407 rr = ot_release cs cg 408 if ne rc OT_OK 409 ret rc 410 end 411 rr = ot_release get ad_nodes add nb 10 get ad_nodes add nb 11 412 put ad_nodes add nb 10 get sum_h 0 413 put ad_nodes add nb 11 get sum_h 1 414 ret AD_OK 415 end 416 417 fn ad_contribute idx ulong fs ulong fg ulong to long 418 nb = mul idx 12 419 if eq get ad_nodes add nb 5 0 420 rr = ot_release fs fg 421 ret AD_OK 422 end 423 u = new ulong 2 0 424 rc = ad_unbroadcast fs fg get ad_nodes add nb 8 u 425 if ne rc AD_OK 426 ret rc 427 end 428 ret ad_accumulate idx get u 0 get u 1 429 end 430 431 fn ad_wants idx ulong to bool 432 if eq idx AD_NONE 433 ret false 434 end 435 ret ne get ad_nodes add mul idx 12 5 0 436 end 437 438 fn ad_max_routes_a a f32 b f32 to bool 439 if ot_is_nan bitcast uint a 440 ret false 441 end 442 if ot_is_nan bitcast uint b 443 ret true 444 end 445 ret gt a b 446 end 447 448 fn ad_scratch_store dst_off ulong m ulong tmp view f32 4096 to void 449 for i ulong m bound 4097 450 put ad_scratch add dst_off i get tmp i 451 end 452 end 453 454 fn ad_bwd_max_load in_id ulong yr ulong ysh view ulong 8 dst_off ulong m ulong to long 455 v = new ulong 2 0 456 nb = mul in_id 12 457 rc = ot_broadcast_to get ad_nodes add nb 8 get ad_nodes add nb 9 yr ysh v 458 if ne rc OT_OK 459 ret rc 460 end 461 tmp = new f32 4096 0.0f32 462 rc = ot_read_f32 get v 0 get v 1 part tmp 0 m 463 rr = ot_release get v 0 get v 1 464 if ne rc OT_OK 465 ret rc 466 end 467 ad_scratch_store dst_off m tmp 468 ret AD_OK 469 end 470 471 fn ad_bwd_max_emit in_id ulong yr ulong ysh view ulong 8 src_off ulong m ulong to long 472 if not ad_wants in_id 473 ret AD_OK 474 end 475 tmp = new f32 4096 0.0f32 476 for i ulong m bound 4097 477 put tmp i get ad_scratch add src_off i 478 end 479 fh = new ulong 2 0 480 rc = ot_from_f32 yr ysh part tmp 0 m fh 481 if ne rc OT_OK 482 ret rc 483 end 484 ret ad_contribute in_id get fh 0 get fh 1 485 end 486 487 fn ad_bwd_max_route nb ulong m ulong to long 488 tmp = new f32 4096 0.0f32 489 rc = ot_read_f32 get ad_nodes add nb 10 get ad_nodes add nb 11 part tmp 0 m 490 if ne rc OT_OK 491 ret rc 492 end 493 for i ulong m bound 4097 494 to_a = ad_max_routes_a get ad_scratch i get ad_scratch add m i 495 g = get tmp i 496 if to_a 497 put ad_scratch i g 498 put ad_scratch add m i 0.0f32 499 else 500 put ad_scratch i 0.0f32 501 put ad_scratch add m i g 502 end 503 end 504 ret AD_OK 505 end 506 507 fn ad_bwd_max idx ulong to long 508 nb = mul idx 12 509 ys = get ad_nodes add nb 8 510 yr = get ot_ts_meta add mul ys 10 4 511 m = get ot_ts_meta add mul ys 10 6 512 if gt mul m 3 get ad_tape_meta 2 513 ret AD_ERR_SCRATCH 514 end 515 ysh = new ulong 8 0 516 for d ulong yr 517 put ysh d get ot_ts_shape add mul ys 8 d 518 end 519 rc = ad_bwd_max_load get ad_nodes add nb 1 yr ysh 0 m 520 if ne rc AD_OK 521 ret rc 522 end 523 rc = ad_bwd_max_load get ad_nodes add nb 2 yr ysh m m 524 if ne rc AD_OK 525 ret rc 526 end 527 rc = ad_bwd_max_route nb m 528 if ne rc AD_OK 529 ret rc 530 end 531 rc = ad_bwd_max_emit get ad_nodes add nb 1 yr ysh 0 m 532 if ne rc AD_OK 533 ret rc 534 end 535 ret ad_bwd_max_emit get ad_nodes add nb 2 yr ysh m m 536 end 537 538 fn ad_bwd_reduce_div in0 ulong c2 view ulong 2 axlen ulong to long 539 nt = new ulong 2 0 540 rc = ad_scalar f32 axlen nt 541 if ne rc OT_OK 542 rr = ot_release get c2 0 get c2 1 543 ret rc 544 end 545 c = new ulong 2 0 546 rc = ot_binary OT_TB_DIV get c2 0 get c2 1 get nt 0 get nt 1 c 547 rr = ot_release get nt 0 get nt 1 548 rr = ot_release get c2 0 get c2 1 549 if ne rc OT_OK 550 ret rc 551 end 552 ret ad_contribute in0 get c 0 get c 1 553 end 554 555 fn ad_bwd_reduce_expand nb ulong ar ulong ash view ulong 8 ksh view ulong 8 c2 edit ulong 2 to long 556 r = new ulong 2 0 557 b = new ulong 2 0 558 rc = ot_reshape get ad_nodes add nb 10 get ad_nodes add nb 11 ar ksh r 559 if ne rc OT_OK 560 ret rc 561 end 562 rc = ot_broadcast_to get r 0 get r 1 ar ash b 563 if ne rc OT_OK 564 rr = ot_release get r 0 get r 1 565 ret rc 566 end 567 rc = ot_contiguous get b 0 get b 1 c2 568 rr = ot_release get b 0 get b 1 569 rr = ot_release get r 0 get r 1 570 ret rc 571 end 572 573 fn ad_bwd_reduce idx ulong to long 574 nb = mul idx 12 575 in0 = get ad_nodes add nb 1 576 if not ad_wants in0 577 ret AD_OK 578 end 579 as = get ad_nodes add mul in0 12 8 580 ar = get ot_ts_meta add mul as 10 4 581 ash = new ulong 8 0 582 ksh = new ulong 8 0 583 for d ulong ar 584 put ash d get ot_ts_shape add mul as 8 d 585 end 586 ax = get ad_nodes add nb 3 587 ad_restore_axis part ash 0 ar ax part ksh 0 ar 588 c2 = new ulong 2 0 589 rc = ad_bwd_reduce_expand nb ar ash ksh c2 590 if ne rc OT_OK 591 ret rc 592 end 593 if eq get ad_nodes nb AD_SUM 594 ret ad_contribute in0 get c2 0 get c2 1 595 end 596 ret ad_bwd_reduce_div in0 c2 get ash ax 597 end 598 599 fn ad_bwd_div_rhs nb ulong in1 ulong gs ulong gg ulong bs ulong bg ulong to long 600 tmp = new ulong 2 0 601 c2 = new ulong 2 0 602 c = new ulong 2 0 603 ys = get ad_nodes add nb 8 604 yg = get ad_nodes add nb 9 605 rc = ot_binary OT_TB_DIV ys yg bs bg tmp 606 if ne rc OT_OK 607 ret rc 608 end 609 rc = ot_binary OT_TB_MUL gs gg get tmp 0 get tmp 1 c2 610 rr = ot_release get tmp 0 get tmp 1 611 if ne rc OT_OK 612 ret rc 613 end 614 rc = ad_scale get c2 0 get c2 1 -1.0f32 c 615 rr = ot_release get c2 0 get c2 1 616 if ne rc OT_OK 617 ret rc 618 end 619 ret ad_contribute in1 get c 0 get c 1 620 end 621 622 fn ad_bwd_div idx ulong to long 623 nb = mul idx 12 624 gs = get ad_nodes add nb 10 625 gg = get ad_nodes add nb 11 626 in0 = get ad_nodes add nb 1 627 in1 = get ad_nodes add nb 2 628 nbb = mul in1 12 629 bs = get ad_nodes add nbb 8 630 bg = get ad_nodes add nbb 9 631 c = new ulong 2 0 632 if ad_wants in0 633 rc = ot_binary OT_TB_DIV gs gg bs bg c 634 if ne rc OT_OK 635 ret rc 636 end 637 rc = ad_contribute in0 get c 0 get c 1 638 if ne rc AD_OK 639 ret rc 640 end 641 end 642 if ad_wants in1 643 ret ad_bwd_div_rhs nb in1 gs gg bs bg 644 end 645 ret AD_OK 646 end 647 648 fn ad_bwd_matmul idx ulong to long 649 nb = mul idx 12 650 gs = get ad_nodes add nb 10 651 gg = get ad_nodes add nb 11 652 in0 = get ad_nodes add nb 1 653 in1 = get ad_nodes add nb 2 654 tmp = new ulong 2 0 655 c = new ulong 2 0 656 if ad_wants in0 657 nbb = mul in1 12 658 rc = ot_transpose get ad_nodes add nbb 8 get ad_nodes add nbb 9 tmp 659 if ne rc OT_OK 660 ret rc 661 end 662 rc = ot_matmul gs gg get tmp 0 get tmp 1 c 663 rr = ot_release get tmp 0 get tmp 1 664 if ne rc OT_OK 665 ret rc 666 end 667 rc = ad_contribute in0 get c 0 get c 1 668 if ne rc AD_OK 669 ret rc 670 end 671 end 672 if ad_wants in1 673 nba = mul in0 12 674 rc = ot_transpose get ad_nodes add nba 8 get ad_nodes add nba 9 tmp 675 if ne rc OT_OK 676 ret rc 677 end 678 rc = ot_matmul get tmp 0 get tmp 1 gs gg c 679 rr = ot_release get tmp 0 get tmp 1 680 if ne rc OT_OK 681 ret rc 682 end 683 ret ad_contribute in1 get c 0 get c 1 684 end 685 ret AD_OK 686 end 687 688 fn ad_bwd_mul_side target ulong other ulong gs ulong gg ulong to long 689 if not ad_wants target 690 ret AD_OK 691 end 692 nbo = mul other 12 693 os = get ad_nodes add nbo 8 694 og = get ad_nodes add nbo 9 695 c = new ulong 2 0 696 rc = ot_binary OT_TB_MUL gs gg os og c 697 if ne rc OT_OK 698 ret rc 699 end 700 ret ad_contribute target get c 0 get c 1 701 end 702 703 fn ad_bwd_sqrt nb ulong in0 ulong gs ulong gg ulong to long 704 if not ad_wants in0 705 ret AD_OK 706 end 707 tmp = new ulong 2 0 708 c = new ulong 2 0 709 vs = get ad_nodes add nb 8 710 vg = get ad_nodes add nb 9 711 rc = ot_binary OT_TB_ADD vs vg vs vg tmp 712 if ne rc OT_OK 713 ret rc 714 end 715 rc = ot_binary OT_TB_DIV gs gg get tmp 0 get tmp 1 c 716 rr = ot_release get tmp 0 get tmp 1 717 if ne rc OT_OK 718 ret rc 719 end 720 ret ad_contribute in0 get c 0 get c 1 721 end 722 723 fn ad_bwd_node idx ulong to long 724 nb = mul idx 12 725 op = get ad_nodes nb 726 if eq op AD_LEAF 727 ret AD_OK 728 end 729 gs = get ad_nodes add nb 10 730 gg = get ad_nodes add nb 11 731 in0 = get ad_nodes add nb 1 732 in1 = get ad_nodes add nb 2 733 c = new ulong 2 0 734 tmp = new ulong 2 0 735 if or eq op AD_ADD eq op AD_SUB 736 if ad_wants in0 737 rc = ot_contiguous gs gg c 738 if ne rc OT_OK 739 ret rc 740 end 741 rc = ad_contribute in0 get c 0 get c 1 742 if ne rc AD_OK 743 ret rc 744 end 745 end 746 if ad_wants in1 747 rc = OT_OK 748 if eq op AD_ADD 749 rc = ot_contiguous gs gg c 750 else 751 rc = ad_scale gs gg -1.0f32 c 752 end 753 if ne rc OT_OK 754 ret rc 755 end 756 ret ad_contribute in1 get c 0 get c 1 757 end 758 ret AD_OK 759 end 760 if eq op AD_MUL 761 rc = ad_bwd_mul_side in0 in1 gs gg 762 if ne rc AD_OK 763 ret rc 764 end 765 ret ad_bwd_mul_side in1 in0 gs gg 766 end 767 if eq op AD_DIV 768 ret ad_bwd_div idx 769 end 770 if eq op AD_MAX 771 ret ad_bwd_max idx 772 end 773 if eq op AD_SQRT 774 ret ad_bwd_sqrt nb in0 gs gg 775 end 776 if eq op AD_MATMUL 777 ret ad_bwd_matmul idx 778 end 779 if eq op AD_TRANSPOSE 780 if not ad_wants in0 781 ret AD_OK 782 end 783 rc = ot_transpose gs gg tmp 784 if ne rc OT_OK 785 ret rc 786 end 787 rc = ot_contiguous get tmp 0 get tmp 1 c 788 rr = ot_release get tmp 0 get tmp 1 789 if ne rc OT_OK 790 ret rc 791 end 792 ret ad_contribute in0 get c 0 get c 1 793 end 794 if eq op AD_BROADCAST 795 if not ad_wants in0 796 ret AD_OK 797 end 798 rc = ot_contiguous gs gg c 799 if ne rc OT_OK 800 ret rc 801 end 802 ret ad_contribute in0 get c 0 get c 1 803 end 804 if or eq op AD_SUM eq op AD_MEAN 805 ret ad_bwd_reduce idx 806 end 807 ret AD_ERR_BAD_ARGS 808 end 809 810 fn ad_scratch_need to ulong 811 cnt = get ad_tape_meta 1 812 need = ulong 0 813 for i ulong cnt 814 nb = mul i 12 815 if eq get ad_nodes nb AD_MAX 816 ys = get ad_nodes add nb 8 817 m = mul get ot_ts_meta add mul ys 10 6 3 818 if gt m need 819 need = m 820 end 821 end 822 end 823 ret need 824 end 825 826 fn ad_begin_backward root ulong to long 827 if eq get ad_tape_meta 0 0 828 ret AD_ERR_BAD_ARGS 829 end 830 if or ne get ad_tape_meta 3 0 ne get ad_tape_meta 4 0 831 ret AD_ERR_STATE 832 end 833 if ge root get ad_tape_meta 1 834 ret AD_ERR_BAD_ARGS 835 end 836 if eq get ad_nodes add mul root 12 5 0 837 ret AD_ERR_NO_GRAD 838 end 839 if gt ad_scratch_need get ad_tape_meta 2 840 ret AD_ERR_SCRATCH 841 end 842 ret AD_OK 843 end 844 845 fn ad_bwd_visit i ulong to long 846 nib = mul i 12 847 if eq get ad_nodes add nib 7 0 848 ret AD_OK 849 end 850 if eq get ad_nodes nib AD_LEAF 851 ret AD_OK 852 end 853 ret ad_bwd_node i 854 end 855 856 fn ad_finish_backward root ulong ss ulong sg ulong to long 857 nb = mul root 12 858 put ad_nodes add nb 10 ss 859 put ad_nodes add nb 11 sg 860 put ad_nodes add nb 7 1 861 put ad_tape_meta 3 1 862 for k ulong add root 1 863 rc = ad_bwd_visit sub root k 864 if ne rc AD_OK 865 put ad_tape_meta 4 1 866 ret rc 867 end 868 end 869 ret AD_OK 870 end 871 872 fn ad_backward root ulong to long 873 rc = ad_begin_backward root 874 if ne rc AD_OK 875 ret rc 876 end 877 rs = get ad_nodes add mul root 12 8 878 if ne get ot_ts_meta add mul rs 10 6 1 879 ret AD_ERR_SHAPE 880 end 881 rr = get ot_ts_meta add mul rs 10 4 882 rsh = new ulong 8 0 883 for d ulong rr 884 put rsh d get ot_ts_shape add mul rs 8 d 885 end 886 one = new f32 1 1.0f32 887 sh = new ulong 2 0 888 rc = ot_from_f32 rr rsh one sh 889 if ne rc OT_OK 890 ret rc 891 end 892 ret ad_finish_backward root get sh 0 get sh 1 893 end 894 895 fn ad_backward_seed root ulong seed_s ulong seed_g ulong to long 896 rc = ad_begin_backward root 897 if ne rc AD_OK 898 ret rc 899 end 900 rc = ot_ts_check seed_s seed_g 901 if ne rc OT_OK 902 ret rc 903 end 904 if ne get ot_ts_meta add mul seed_s 10 3 OT_DT_F32 905 ret OT_ERR_DTYPE 906 end 907 rs = get ad_nodes add mul root 12 8 908 if eq ad_same_ts_shape rs seed_s 0 909 ret AD_ERR_SHAPE 910 end 911 sh = new ulong 2 0 912 rc = ot_contiguous seed_s seed_g sh 913 if ne rc OT_OK 914 ret rc 915 end 916 ret ad_finish_backward root get sh 0 get sh 1 917 end 918 919 fn ad_grad id ulong out_h edit ulong 2 to long 920 if ge id get ad_tape_meta 1 921 ret AD_ERR_BAD_ARGS 922 end 923 if ne get ad_tape_meta 4 0 924 ret AD_ERR_STATE 925 end 926 nb = mul id 12 927 if eq get ad_nodes add nb 7 0 928 ret AD_ERR_NO_GRAD 929 end 930 put out_h 0 get ad_nodes add nb 10 931 put out_h 1 get ad_nodes add nb 11 932 ret AD_OK 933 end 934 935 fn ad_grad_read_f32 id ulong dst edit f32 * to long 936 gh = new ulong 2 0 937 rc = ad_grad id gh 938 if ne rc AD_OK 939 ret rc 940 end 941 el = get ot_ts_meta add mul get gh 0 10 6 942 if ne el len dst 943 ret AD_ERR_COUNT 944 end 945 ret ot_read_f32 get gh 0 get gh 1 dst 946 end 947 948 fn ad_grads_total ids view ulong * to long 949 n = len ids 950 gh = new ulong 2 0 951 total = ulong 0 952 for k ulong n 953 rc = ad_grad get ids k gh 954 if ne rc AD_OK 955 ret rc 956 end 957 total = add total get ot_ts_meta add mul get gh 0 10 6 958 end 959 ret long total 960 end 961 962 fn ad_grad_copy_at id ulong dst edit f32 * off ulong to long 963 gh = new ulong 2 0 964 rc = ad_grad id gh 965 if ne rc AD_OK 966 ret rc 967 end 968 el = get ot_ts_meta add mul get gh 0 10 6 969 rc = ot_read_f32 get gh 0 get gh 1 part dst off el 970 if ne rc OT_OK 971 ret rc 972 end 973 ret long el 974 end 975 976 fn ad_grads_flatten ids view ulong * dst edit f32 * to long 977 n = len ids 978 if eq n 0 979 ret AD_ERR_BAD_ARGS 980 end 981 tot = ad_grads_total ids 982 if lt tot 0 983 ret tot 984 end 985 if ne ulong tot len dst 986 ret AD_ERR_COUNT 987 end 988 off = ulong 0 989 for k ulong n 990 el = ad_grad_copy_at get ids k dst off 991 if lt el 0 992 ret el 993 end 994 off = add off ulong el 995 end 996 ret AD_OK 997 end