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/tensor/omega_tensor.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 / tensor / omega_tensor.om` 1760 lines ยท 41014 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note omega_tensor.om -- Pure seeded OM-2 M20 semantic tensor layer. 3 note Immutable tensors, generation-checked storage and descriptor slots, 4 note views (permute, transpose, slice, reshape, broadcast_to), value/view IDs, 5 note elementwise, compare, where, reduce, reduce_axes, sum_to_shape, matmul, 6 note embed, concat, cast. Composed with tensor_shape.om, tensor_ops.om, 7 note tg_sha256.om, and omega_tensor_cpu.om. 8 9 const OT_MAX_SLOTS ulong 256 10 const OT_SLOT_BYTES ulong 16384 11 const OT_MAX_ELEMS ulong `1073741824` 12 const OT_MAX_RANK ulong 8 13 const OT_U64_MAX ulong 0xffffffffffffffff 14 15 global ot_ctx_cap ulong 1 16 global ot_arena uchar `4194304` 17 global ot_st_gen ulong 256 18 global ot_st_meta ulong 1024 19 note ot_st_meta (stride 4): 0=live, 1=retired, 2=dtype, 3=elems 20 21 global ot_ts_gen ulong 256 22 global ot_ts_meta ulong 2560 23 note ot_ts_meta (stride 10): 0=live, 1=retired, 2=owns, 3=dtype, 4=rank, 24 note 5=offset, 6=elems, 7=st_slot, 8=st_gen, 9=is_view 25 global ot_ts_parent ulong 512 26 global ot_ts_shape ulong 2048 27 global ot_ts_strides ulong 2048 28 29 note Dense gather/compute scratch buffers (16384 bytes = 4096 f32s each) 30 global ot_buf_a uchar 16384 31 global ot_buf_b uchar 16384 32 global ot_buf_c uchar 16384 33 global ot_buf_o uchar 16384 34 global ot_mm_a uchar 16384 35 global ot_mm_bt uchar 16384 36 global ot_tmp_sh ulong 8 37 global ot_tmp_st ulong 8 38 global ot_tmp_sh2 ulong 8 39 global ot_tmp_st2 ulong 8 40 global ot_tmp_h ulong 4 41 42 text ot_dom_val "OMEGA_TENSOR_VALUE_ID_V1" 43 text ot_dom_view "OMEGA_TENSOR_VIEW_ID_V1" 44 45 fn ot_dtype_size dt ulong to ulong 46 if eq dt OT_DT_F32 47 ret 4 48 end 49 if or eq dt OT_DT_F16 eq dt OT_DT_BF16 50 ret 2 51 end 52 ret 0 53 end 54 55 fn ot_ctx_init cap ulong to long 56 if or eq cap 0 gt cap OT_MAX_SLOTS 57 ret OT_ERR_BAD_ARGS 58 end 59 put ot_ctx_cap 0 cap 60 for i ulong cap 61 put ot_st_gen i 1 62 put ot_ts_gen i 1 63 sb = mul i 4 64 for j ulong 4 65 put ot_st_meta add sb j 0 66 end 67 tb = mul i 10 68 for j ulong 10 69 put ot_ts_meta add tb j 0 70 end 71 end 72 ret OT_OK 73 end 74 75 fn ot_st_valid s ulong g ulong to bool 76 if ge s get ot_ctx_cap 0 77 ret false 78 end 79 if eq get ot_st_meta mul s 4 0 80 ret false 81 end 82 ret eq get ot_st_gen s g 83 end 84 85 fn ot_ts_check s ulong g ulong to long 86 if ge s get ot_ctx_cap 0 87 ret OT_ERR_STALE 88 end 89 tb = mul s 10 90 if eq get ot_ts_meta tb 0 91 ret OT_ERR_STALE 92 end 93 if ne get ot_ts_gen s g 94 ret OT_ERR_STALE 95 end 96 if not ot_st_valid get ot_ts_meta add tb 7 get ot_ts_meta add tb 8 97 ret OT_ERR_STALE 98 end 99 ret OT_OK 100 end 101 102 fn ot_st_alloc dt ulong elems ulong out_h edit ulong 2 to long 103 es = ot_dtype_size dt 104 if or eq es 0 or eq elems 0 gt mul elems es OT_SLOT_BYTES 105 ret OT_ERR_CAPACITY 106 end 107 cap = get ot_ctx_cap 0 108 for i ulong cap 109 sb = mul i 4 110 if and eq get ot_st_meta sb 0 eq get ot_st_meta add sb 1 0 111 put ot_st_meta sb 1 112 put ot_st_meta add sb 2 dt 113 put ot_st_meta add sb 3 elems 114 base = mul i OT_SLOT_BYTES 115 nb = mul elems es 116 for k ulong nb bound 16385 117 put ot_arena add base k 0 118 end 119 put out_h 0 i 120 put out_h 1 get ot_st_gen i 121 ret OT_OK 122 end 123 end 124 ret OT_ERR_CAPACITY 125 end 126 127 fn ot_st_release s ulong g ulong to void 128 if not ot_st_valid s g 129 ret 130 end 131 sb = mul s 4 132 put ot_st_meta sb 0 133 cur = get ot_st_gen s 134 if eq cur OT_U64_MAX 135 put ot_st_meta add sb 1 1 136 else 137 put ot_st_gen s add cur 1 138 end 139 end 140 141 fn ot_ts_alloc owns ulong dt ulong rank ulong elems ulong st_s ulong st_g ulong to long 142 cap = get ot_ctx_cap 0 143 for i ulong cap 144 tb = mul i 10 145 if eq get ot_ts_meta tb 0 146 if eq get ot_ts_meta add tb 1 0 147 put ot_ts_meta tb 1 148 put ot_ts_meta add tb 2 owns 149 put ot_ts_meta add tb 3 dt 150 put ot_ts_meta add tb 4 rank 151 put ot_ts_meta add tb 5 0 152 put ot_ts_meta add tb 6 elems 153 put ot_ts_meta add tb 7 st_s 154 put ot_ts_meta add tb 8 st_g 155 put ot_ts_meta add tb 9 0 156 put ot_ts_parent mul i 2 i 157 put ot_ts_parent add mul i 2 1 get ot_ts_gen i 158 put ot_tmp_h 0 i 159 put ot_tmp_h 1 get ot_ts_gen i 160 ret OT_OK 161 end 162 end 163 end 164 ret OT_ERR_CAPACITY 165 end 166 167 fn ot_release s ulong g ulong to long 168 if ge s get ot_ctx_cap 0 169 ret OT_ERR_STALE 170 end 171 tb = mul s 10 172 if or eq get ot_ts_meta tb 0 ne get ot_ts_gen s g 173 ret OT_ERR_STALE 174 end 175 if ne get ot_ts_meta add tb 2 0 176 ot_st_release get ot_ts_meta add tb 7 get ot_ts_meta add tb 8 177 end 178 put ot_ts_meta tb 0 179 cur = get ot_ts_gen s 180 if eq cur OT_U64_MAX 181 put ot_ts_meta add tb 1 1 182 else 183 put ot_ts_gen s add cur 1 184 end 185 ret OT_OK 186 end 187 188 fn ot_live_counts out_c edit ulong 2 to void 189 cap = get ot_ctx_cap 0 190 tc = ulong 0 191 sc = ulong 0 192 for i ulong cap 193 if ne get ot_ts_meta mul i 10 0 194 tc = add tc 1 195 end 196 if ne get ot_st_meta mul i 4 0 197 sc = add sc 1 198 end 199 end 200 put out_c 0 tc 201 put out_c 1 sc 202 end 203 204 fn ot_set_st_gen_test s ulong gen ulong to long 205 if or ge s get ot_ctx_cap 0 eq gen 0 206 ret OT_ERR_BAD_ARGS 207 end 208 sb = mul s 4 209 if or ne get ot_st_meta sb 0 ne get ot_st_meta add sb 1 0 210 ret OT_ERR_BAD_ARGS 211 end 212 if le gen get ot_st_gen s 213 ret OT_ERR_BAD_ARGS 214 end 215 put ot_st_gen s gen 216 ret OT_OK 217 end 218 219 fn ot_shape_check rank ulong shape view ulong 8 elems edit ulong 1 to long 220 if gt rank OT_MAX_RANK 221 ret OT_ERR_RANK 222 end 223 if eq rank 0 224 put elems 0 1 225 ret OT_OK 226 end 227 ret tensor_shape_size part shape 0 rank elems 228 end 229 230 fn ot_new_dense dt ulong rank ulong shape view ulong 8 out_h edit ulong 2 to long 231 elems = new ulong 1 0 232 rc = ot_shape_check rank shape elems 233 if ne rc OT_OK 234 ret rc 235 end 236 sh = new ulong 2 0 237 rc = ot_st_alloc dt get elems 0 sh 238 if ne rc OT_OK 239 ret rc 240 end 241 rc = ot_ts_alloc 1 dt rank get elems 0 get sh 0 get sh 1 242 if ne rc OT_OK 243 ot_st_release get sh 0 get sh 1 244 ret rc 245 end 246 slot = get ot_tmp_h 0 247 sbase = mul slot 8 248 ds = new ulong 8 0 249 for d ulong 8 250 put ot_ts_shape add sbase d 0 251 put ot_ts_strides add sbase d 0 252 end 253 if gt rank 0 254 tensor_dense_strides part shape 0 rank part ds 0 rank 255 for d ulong rank 256 put ot_ts_shape add sbase d get shape d 257 put ot_ts_strides add sbase d get ds d 258 end 259 end 260 put out_h 0 slot 261 put out_h 1 get ot_tmp_h 1 262 ret OT_OK 263 end 264 265 fn ot_copy_to_slot slot ulong es ulong data view uchar * to long 266 tb = mul slot 10 267 need = mul get ot_ts_meta add tb 6 es 268 if lt len data need 269 ret OT_ERR_CAPACITY 270 end 271 base = mul get ot_ts_meta add tb 7 OT_SLOT_BYTES 272 for i ulong need bound 16385 273 put ot_arena add base i get data i 274 end 275 ret OT_OK 276 end 277 278 fn ot_from_bytes dt ulong rank ulong shape view ulong 8 data view uchar * out_h edit ulong 2 to long 279 es = ot_dtype_size dt 280 if eq es 0 281 ret OT_ERR_DTYPE 282 end 283 rc = ot_new_dense dt rank shape out_h 284 if ne rc OT_OK 285 ret rc 286 end 287 rc = ot_copy_to_slot get out_h 0 es data 288 if ne rc OT_OK 289 rr = ot_release get out_h 0 get out_h 1 290 end 291 ret rc 292 end 293 294 fn ot_copy_f32_to_slot slot ulong data view f32 * to long 295 tb = mul slot 10 296 elems = get ot_ts_meta add tb 6 297 if lt len data elems 298 ret OT_ERR_CAPACITY 299 end 300 base = mul get ot_ts_meta add tb 7 OT_SLOT_BYTES 301 for i ulong elems bound 4097 302 ot_arena_put_f32 base i get data i 303 end 304 ret OT_OK 305 end 306 307 fn ot_from_f32 rank ulong shape view ulong 8 data view f32 * out_h edit ulong 2 to long 308 rc = ot_new_dense OT_DT_F32 rank shape out_h 309 if ne rc OT_OK 310 ret rc 311 end 312 rc = ot_copy_f32_to_slot get out_h 0 data 313 if ne rc OT_OK 314 rr = ot_release get out_h 0 get out_h 1 315 end 316 ret rc 317 end 318 319 fn ot_step_odo idx edit ulong 8 rank ulong pos ulong to ulong 320 p = pos 321 for k ulong rank 322 ax = sub sub rank 1 k 323 ni = add get idx ax 1 324 if lt ni get ot_tmp_sh ax 325 put idx ax ni 326 ret add p get ot_tmp_st ax 327 end 328 p = sub p mul sub get ot_tmp_sh ax 1 get ot_tmp_st ax 329 put idx ax 0 330 end 331 ret p 332 end 333 334 fn ot_put_dst dst_id ulong idx ulong v uchar to void 335 if eq dst_id 0 336 put ot_buf_a idx v 337 ret 338 end 339 if eq dst_id 1 340 put ot_buf_b idx v 341 ret 342 end 343 if eq dst_id 2 344 put ot_buf_c idx v 345 ret 346 end 347 if eq dst_id 3 348 put ot_buf_o idx v 349 ret 350 end 351 if eq dst_id 4 352 put ot_mm_a idx v 353 ret 354 end 355 put ot_mm_bt idx v 356 end 357 358 fn ot_copy_elem_dst dst_id ulong sb ulong db ulong es ulong to void 359 for b ulong es 360 ot_put_dst dst_id add db b get ot_arena add sb b 361 end 362 end 363 364 fn ot_gather_desc st_s ulong rank ulong off ulong es ulong elems ulong dst_id ulong to void 365 idx = new ulong 8 0 366 pos = off 367 sbase = mul st_s OT_SLOT_BYTES 368 for e ulong elems bound 4097 369 ot_copy_elem_dst dst_id add sbase mul pos es mul e es es 370 pos = ot_step_odo idx rank pos 371 end 372 end 373 374 fn ot_load_ts_desc slot ulong to void 375 sbase = mul slot 8 376 for d ulong 8 377 put ot_tmp_sh d get ot_ts_shape add sbase d 378 put ot_tmp_st d get ot_ts_strides add sbase d 379 end 380 end 381 382 fn ot_gather_slot slot ulong dst_id ulong to void 383 tb = mul slot 10 384 ot_load_ts_desc slot 385 ot_gather_desc get ot_ts_meta add tb 7 get ot_ts_meta add tb 4 get ot_ts_meta add tb 5 ot_dtype_size get ot_ts_meta add tb 3 get ot_ts_meta add tb 6 dst_id 386 end 387 388 fn ot_copy_elem_arena sb ulong db ulong es ulong to void 389 for b ulong es 390 put ot_arena add db b get ot_buf_a add sb b 391 end 392 end 393 394 fn ot_scatter_buf_a st_s ulong rank ulong off ulong es ulong elems ulong to void 395 idx = new ulong 8 0 396 pos = off 397 dbase = mul st_s OT_SLOT_BYTES 398 for e ulong elems bound 4097 399 ot_copy_elem_arena mul e es add dbase mul pos es es 400 pos = ot_step_odo idx rank pos 401 end 402 end 403 404 fn ot_read_f32 slot ulong gen ulong dst edit f32 * to long 405 rc = ot_ts_check slot gen 406 if ne rc OT_OK 407 ret rc 408 end 409 tb = mul slot 10 410 if ne get ot_ts_meta add tb 3 OT_DT_F32 411 ret OT_ERR_DTYPE 412 end 413 elems = get ot_ts_meta add tb 6 414 if lt len dst elems 415 ret OT_ERR_CAPACITY 416 end 417 ot_gather_slot slot 0 418 for i ulong elems bound 4097 419 p = mul i 4 420 u = uint get ot_buf_a p 421 u = or u shl uint get ot_buf_a add p 1 8 422 u = or u shl uint get ot_buf_a add p 2 16 423 u = or u shl uint get ot_buf_a add p 3 24 424 put dst i bitcast f32 u 425 end 426 ret OT_OK 427 end 428 429 fn ot_sha_put_u64 ctx edit uchar 104 v ulong to void 430 b = new uchar 8 0 431 for i ulong 8 432 put b i uchar and shr v mul i 8 255 433 end 434 tg_sha_update ctx b 8 435 end 436 437 fn ot_value_id_payload ctx edit uchar 104 dt ulong nb ulong to void 438 loc = new uchar 16384 0 439 for i ulong nb bound 16385 440 put loc i get ot_buf_a i 441 end 442 tensor_value_bytes part loc 0 nb dt 1 443 tg_sha_update ctx part loc 0 nb nb 444 end 445 446 fn ot_value_id slot ulong gen ulong dig edit uchar 32 to long 447 rc = ot_ts_check slot gen 448 if ne rc OT_OK 449 ret rc 450 end 451 tb = mul slot 10 452 dt = get ot_ts_meta add tb 3 453 rank = get ot_ts_meta add tb 4 454 ot_gather_slot slot 0 455 ctx = new uchar 104 0 456 tg_sha_init ctx 457 dom = new uchar 25 0 458 for i ulong 24 459 put dom i get ot_dom_val i 460 end 461 put dom 24 0 462 tg_sha_update ctx dom 25 463 ot_sha_put_u64 ctx dt 464 ot_sha_put_u64 ctx rank 465 for d ulong rank 466 ot_sha_put_u64 ctx get ot_ts_shape add mul slot 8 d 467 end 468 ot_value_id_payload ctx dt mul get ot_ts_meta add tb 6 ot_dtype_size dt 469 tg_sha_final ctx dig 470 ret OT_OK 471 end 472 473 fn ot_view_id slot ulong gen ulong dig edit uchar 32 to long 474 rc = ot_ts_check slot gen 475 if ne rc OT_OK 476 ret rc 477 end 478 tb = mul slot 10 479 ctx = new uchar 104 0 480 tg_sha_init ctx 481 dom = new uchar 24 0 482 for i ulong 23 483 put dom i get ot_dom_view i 484 end 485 put dom 23 0 486 tg_sha_update ctx dom 24 487 ot_sha_put_u64 ctx get ot_ts_meta add tb 7 488 ot_sha_put_u64 ctx get ot_ts_meta add tb 8 489 ot_sha_put_u64 ctx get ot_ts_parent mul slot 2 490 ot_sha_put_u64 ctx get ot_ts_parent add mul slot 2 1 491 ot_sha_put_u64 ctx get ot_ts_meta add tb 3 492 ot_sha_put_u64 ctx get ot_ts_meta add tb 5 493 rank = get ot_ts_meta add tb 4 494 ot_sha_put_u64 ctx rank 495 for d ulong rank 496 ot_sha_put_u64 ctx get ot_ts_shape add mul slot 8 d 497 ot_sha_put_u64 ctx get ot_ts_strides add mul slot 8 d 498 end 499 tg_sha_final ctx dig 500 ret OT_OK 501 end 502 503 fn ot_value_equal s1 ulong g1 ulong s2 ulong g2 ulong eq_out edit ulong 1 to long 504 d1 = new uchar 32 0 505 d2 = new uchar 32 0 506 rc = ot_value_id s1 g1 d1 507 if ne rc OT_OK 508 ret rc 509 end 510 rc = ot_value_id s2 g2 d2 511 if ne rc OT_OK 512 ret rc 513 end 514 same = ulong 1 515 for i ulong 32 516 if ne get d1 i get d2 i 517 same = 0 518 end 519 end 520 put eq_out 0 same 521 ret OT_OK 522 end 523 524 fn ot_finish_view ns ulong ps ulong pg ulong rank ulong off ulong to void 525 ntb = mul ns 10 526 put ot_ts_meta add ntb 5 off 527 put ot_ts_meta add ntb 9 1 528 put ot_ts_parent mul ns 2 ps 529 put ot_ts_parent add mul ns 2 1 pg 530 for d ulong 8 531 put ot_ts_shape add mul ns 8 d 0 532 put ot_ts_strides add mul ns 8 d 0 533 end 534 for d ulong rank 535 put ot_ts_shape add mul ns 8 d get ot_tmp_sh d 536 put ot_ts_strides add mul ns 8 d get ot_tmp_st d 537 end 538 end 539 540 fn ot_check_tmp_bounds rank ulong off ulong se ulong to long 541 sh = new ulong 8 0 542 st = new ulong 8 0 543 for d ulong rank 544 put sh d get ot_tmp_sh d 545 put st d get ot_tmp_st d 546 end 547 ret tensor_view_bounds part sh 0 rank part st 0 rank off se 548 end 549 550 fn ot_ts_alloc_view ps ulong rank ulong elems ulong to long 551 ptb = mul ps 10 552 dt = get ot_ts_meta add ptb 3 553 sts = get ot_ts_meta add ptb 7 554 stg = get ot_ts_meta add ptb 8 555 ret ot_ts_alloc 0 dt rank elems sts stg 556 end 557 558 fn ot_register_view ps ulong pg ulong rank ulong out_h edit ulong 2 to long 559 off = get ot_tmp_h 2 560 elems = get ot_tmp_h 3 561 sts = get ot_ts_meta add mul ps 10 7 562 rc = ot_check_tmp_bounds rank off get ot_st_meta add mul sts 4 3 563 if ne rc OT_OK 564 ret rc 565 end 566 rc = ot_ts_alloc_view ps rank elems 567 if ne rc OT_OK 568 ret rc 569 end 570 ot_finish_view get ot_tmp_h 0 ps pg rank off 571 put out_h 0 get ot_tmp_h 0 572 put out_h 1 get ot_tmp_h 1 573 ret OT_OK 574 end 575 576 fn ot_permute_plan s ulong rank ulong perm view ulong 8 to long 577 seen = new ulong 8 0 578 s8 = mul s 8 579 for d ulong rank 580 p = get perm d 581 if ge p rank 582 ret OT_ERR_AXIS 583 end 584 if ne get seen p 0 585 ret OT_ERR_AXIS 586 end 587 put seen p 1 588 idx = add s8 p 589 put ot_tmp_sh d get ot_ts_shape idx 590 put ot_tmp_st d get ot_ts_strides idx 591 end 592 ret OT_OK 593 end 594 595 fn ot_permute s ulong g ulong perm view ulong 8 out_h edit ulong 2 to long 596 rc = ot_ts_check s g 597 if ne rc OT_OK 598 ret rc 599 end 600 tb = mul s 10 601 rank = get ot_ts_meta add tb 4 602 rc = ot_permute_plan s rank perm 603 if ne rc OT_OK 604 ret rc 605 end 606 put ot_tmp_h 2 get ot_ts_meta add tb 5 607 put ot_tmp_h 3 get ot_ts_meta add tb 6 608 ret ot_register_view s g rank out_h 609 end 610 611 fn ot_transpose s ulong g ulong out_h edit ulong 2 to long 612 rc = ot_ts_check s g 613 if ne rc OT_OK 614 ret rc 615 end 616 rank = get ot_ts_meta add mul s 10 4 617 if lt rank 2 618 ret OT_ERR_RANK 619 end 620 perm = new ulong 8 0 621 for d ulong rank 622 put perm d d 623 end 624 put perm sub rank 2 sub rank 1 625 put perm sub rank 1 sub rank 2 626 ret ot_permute s g perm out_h 627 end 628 629 fn ot_is_slot_row_major s ulong to ulong 630 ir = get ot_ts_meta add mul s 10 4 631 ish = new ulong 8 0 632 ist = new ulong 8 0 633 for d ulong ir 634 put ish d get ot_ts_shape add mul s 8 d 635 put ist d get ot_ts_strides add mul s 8 d 636 end 637 ret tensor_is_row_major part ish 0 ir part ist 0 ir 638 end 639 640 fn ot_reshape s ulong g ulong rank ulong shape view ulong 8 out_h edit ulong 2 to long 641 rc = ot_ts_check s g 642 if ne rc OT_OK 643 ret rc 644 end 645 elems = new ulong 1 0 646 rc = ot_shape_check rank shape elems 647 if ne rc OT_OK 648 ret rc 649 end 650 tb = mul s 10 651 if ne get elems 0 get ot_ts_meta add tb 6 652 ret OT_ERR_SHAPE 653 end 654 if eq ot_is_slot_row_major s 0 655 ret OT_ERR_NOT_CONTIGUOUS 656 end 657 ds = new ulong 8 0 658 if gt rank 0 659 tensor_dense_strides part shape 0 rank part ds 0 rank 660 end 661 for d ulong rank 662 put ot_tmp_sh d get shape d 663 put ot_tmp_st d get ds d 664 end 665 put ot_tmp_h 2 get ot_ts_meta add tb 5 666 put ot_tmp_h 3 get elems 0 667 ret ot_register_view s g rank out_h 668 end 669 670 fn ot_broadcast_plan s ulong rank ulong shape view ulong 8 elems_out edit ulong 1 to long 671 ir = get ot_ts_meta add mul s 10 4 672 if lt rank ir 673 ret OT_ERR_SHAPE 674 end 675 rc = ot_shape_check rank shape elems_out 676 if ne rc OT_OK 677 ret rc 678 end 679 sbase = mul s 8 680 for i ulong rank 681 d = sub sub rank 1 i 682 put ot_tmp_sh d get shape d 683 if lt i ir 684 idx = add sbase sub sub ir 1 i 685 idim = get ot_ts_shape idx 686 if eq idim get shape d 687 put ot_tmp_st d get ot_ts_strides idx 688 else 689 if eq idim 1 690 put ot_tmp_st d 0 691 else 692 ret OT_ERR_SHAPE 693 end 694 end 695 else 696 put ot_tmp_st d 0 697 end 698 end 699 ret OT_OK 700 end 701 702 fn ot_broadcast_to s ulong g ulong rank ulong shape view ulong 8 out_h edit ulong 2 to long 703 rc = ot_ts_check s g 704 if ne rc OT_OK 705 ret rc 706 end 707 elems = new ulong 1 0 708 rc = ot_broadcast_plan s rank shape elems 709 if ne rc OT_OK 710 ret rc 711 end 712 put ot_tmp_h 2 get ot_ts_meta add mul s 10 5 713 put ot_tmp_h 3 get elems 0 714 ret ot_register_view s g rank out_h 715 end 716 717 fn ot_slice_dim s ulong d ulong a ulong b ulong st ulong res edit ulong 2 to long 718 if eq st 0 719 ret OT_ERR_BAD_ARGS 720 end 721 idx = add mul s 8 d 722 if gt b get ot_ts_shape idx 723 ret OT_ERR_BOUNDS 724 end 725 if ge a b 726 ret OT_ERR_SHAPE 727 end 728 span = sub sub b a 1 729 odim = add div span st 1 730 put ot_tmp_sh d odim 731 istr = get ot_ts_strides idx 732 put res 0 add get res 0 mul a istr 733 if eq odim 1 734 put ot_tmp_st d 0 735 else 736 put ot_tmp_st d mul istr st 737 end 738 put res 1 mul get res 1 odim 739 ret OT_OK 740 end 741 742 fn ot_slice_plan s ulong start view ulong 8 stop view ulong 8 step view ulong 8 res edit ulong 2 to long 743 tb = mul s 10 744 rank = get ot_ts_meta add tb 4 745 if eq rank 0 746 ret OT_ERR_RANK 747 end 748 put res 0 get ot_ts_meta add tb 5 749 put res 1 1 750 for d ulong rank 751 rc = ot_slice_dim s d get start d get stop d get step d res 752 if ne rc OT_OK 753 ret rc 754 end 755 end 756 ret OT_OK 757 end 758 759 fn ot_slice s ulong g ulong start view ulong 8 stop view ulong 8 step view ulong 8 out_h edit ulong 2 to long 760 rc = ot_ts_check s g 761 if ne rc OT_OK 762 ret rc 763 end 764 res = new ulong 2 0 765 rc = ot_slice_plan s start stop step res 766 if ne rc OT_OK 767 ret rc 768 end 769 rank = get ot_ts_meta add mul s 10 4 770 put ot_tmp_h 2 get res 0 771 put ot_tmp_h 3 get res 1 772 ret ot_register_view s g rank out_h 773 end 774 775 fn ot_slot_dbase slot ulong to ulong 776 ret mul get ot_ts_meta add mul slot 10 7 OT_SLOT_BYTES 777 end 778 779 fn ot_copy_buf_a_to_slot slot ulong nb ulong to void 780 dbase = ot_slot_dbase slot 781 for i ulong nb bound 16385 782 put ot_arena add dbase i get ot_buf_a i 783 end 784 end 785 786 fn ot_contiguous s ulong g ulong out_h edit ulong 2 to long 787 rc = ot_ts_check s g 788 if ne rc OT_OK 789 ret rc 790 end 791 tb = mul s 10 792 dt = get ot_ts_meta add tb 3 793 rank = get ot_ts_meta add tb 4 794 elems = get ot_ts_meta add tb 6 795 sh = new ulong 8 0 796 for d ulong rank 797 put sh d get ot_ts_shape add mul s 8 d 798 end 799 ot_gather_slot s 0 800 rc = ot_new_dense dt rank sh out_h 801 if ne rc OT_OK 802 ret rc 803 end 804 ot_copy_buf_a_to_slot get out_h 0 mul elems ot_dtype_size dt 805 ret OT_OK 806 end 807 808 fn ot_fill_f32 dbase ulong elems ulong val f32 to void 809 for i ulong elems bound 4097 810 ot_arena_put_f32 dbase i val 811 end 812 end 813 814 fn ot_full rank ulong shape view ulong 8 val f32 out_h edit ulong 2 to long 815 rc = ot_new_dense OT_DT_F32 rank shape out_h 816 if ne rc OT_OK 817 ret rc 818 end 819 slot = get out_h 0 820 ot_fill_f32 ot_slot_dbase slot get ot_ts_meta add mul slot 10 6 val 821 ret OT_OK 822 end 823 824 fn ot_zeros rank ulong shape view ulong 8 out_h edit ulong 2 to long 825 ret ot_full rank shape 0.0f32 out_h 826 end 827 828 fn ot_ones rank ulong shape view ulong 8 out_h edit ulong 2 to long 829 ret ot_full rank shape 1.0f32 out_h 830 end 831 832 fn ot_buf_get_f32 bid ulong idx ulong to f32 833 p = mul idx 4 834 u = uint 0 835 if eq bid 0 836 u = uint get ot_buf_a p 837 u = or u shl uint get ot_buf_a add p 1 8 838 u = or u shl uint get ot_buf_a add p 2 16 839 u = or u shl uint get ot_buf_a add p 3 24 840 else 841 if eq bid 1 842 u = uint get ot_buf_b p 843 u = or u shl uint get ot_buf_b add p 1 8 844 u = or u shl uint get ot_buf_b add p 2 16 845 u = or u shl uint get ot_buf_b add p 3 24 846 else 847 u = uint get ot_buf_c p 848 u = or u shl uint get ot_buf_c add p 1 8 849 u = or u shl uint get ot_buf_c add p 2 16 850 u = or u shl uint get ot_buf_c add p 3 24 851 end 852 end 853 ret bitcast f32 u 854 end 855 856 fn ot_arena_put_f32 dbase ulong idx ulong v f32 to void 857 u = bitcast uint v 858 p = add dbase mul idx 4 859 put ot_arena p uchar and u 255 860 put ot_arena add p 1 uchar and shr u 8 255 861 put ot_arena add p 2 uchar and shr u 16 255 862 put ot_arena add p 3 uchar and shr u 24 255 863 end 864 865 fn ot_cast_loop dt ulong to_dt ulong elems ulong dbase ulong to void 866 for i ulong elems bound 4097 867 if eq dt OT_DT_F32 868 u = bitcast uint ot_buf_get_f32 0 i 869 h = uint 0 870 if eq to_dt OT_DT_F16 871 h = ot_f32_to_f16 u 872 else 873 h = ot_f32_to_bf16 u 874 end 875 p = add dbase mul i 2 876 put ot_arena p uchar and h 255 877 put ot_arena add p 1 uchar and shr h 8 255 878 else 879 p = mul i 2 880 h = or uint get ot_buf_a p shl uint get ot_buf_a add p 1 8 881 u = uint 0 882 if eq dt OT_DT_F16 883 u = ot_f16_to_f32 h 884 else 885 u = ot_bf16_to_f32 h 886 end 887 ot_arena_put_f32 dbase i bitcast f32 u 888 end 889 end 890 end 891 892 fn ot_cast s ulong g ulong to_dt ulong out_h edit ulong 2 to long 893 if eq ot_dtype_size to_dt 0 894 ret OT_ERR_DTYPE 895 end 896 rc = ot_ts_check s g 897 if ne rc OT_OK 898 ret rc 899 end 900 tb = mul s 10 901 dt = get ot_ts_meta add tb 3 902 if eq dt to_dt 903 ret ot_contiguous s g out_h 904 end 905 if and ne dt OT_DT_F32 ne to_dt OT_DT_F32 906 ret OT_ERR_DTYPE 907 end 908 rank = get ot_ts_meta add tb 4 909 sh = new ulong 8 0 910 for d ulong rank 911 put sh d get ot_ts_shape add mul s 8 d 912 end 913 ot_gather_slot s 0 914 rc = ot_new_dense to_dt rank sh out_h 915 if ne rc OT_OK 916 ret rc 917 end 918 ot_cast_loop dt to_dt get ot_ts_meta add tb 6 ot_slot_dbase get out_h 0 919 ret OT_OK 920 end 921 922 fn ot_unary_exec op ulong elems ulong dbase ulong to void 923 for i ulong elems bound 4097 924 ot_arena_put_f32 dbase i ot_unary_scalar op ot_buf_get_f32 0 i 925 end 926 end 927 928 fn ot_unary op ulong s ulong g ulong out_h edit ulong 2 to long 929 if ge op OT_TU_COUNT 930 ret OT_ERR_BAD_ARGS 931 end 932 rc = ot_ts_check s g 933 if ne rc OT_OK 934 ret rc 935 end 936 tb = mul s 10 937 if ne get ot_ts_meta add tb 3 OT_DT_F32 938 ret OT_ERR_DTYPE 939 end 940 rank = get ot_ts_meta add tb 4 941 elems = get ot_ts_meta add tb 6 942 sh = new ulong 8 0 943 for d ulong rank 944 put sh d get ot_ts_shape add mul s 8 d 945 end 946 ot_gather_slot s 0 947 rc = ot_new_dense OT_DT_F32 rank sh out_h 948 if ne rc OT_OK 949 ret rc 950 end 951 ot_unary_exec op elems ot_slot_dbase get out_h 0 952 ret OT_OK 953 end 954 955 fn ot_bcast_dims_3 sha view ulong 8 shb view ulong 8 sout edit ulong 8 dims view ulong 3 to long 956 ret tensor_broadcast_dims part sha 0 get dims 0 part shb 0 get dims 1 part sout 0 get dims 2 957 end 958 959 fn ot_bcast_two_shapes sa ulong sb ulong rout edit ulong 1 sout edit ulong 8 to long 960 ra = get ot_ts_meta add mul sa 10 4 961 rb = get ot_ts_meta add mul sb 10 4 962 r = ra 963 if gt rb r 964 r = rb 965 end 966 put rout 0 r 967 if eq r 0 968 ret OT_OK 969 end 970 sha = new ulong 8 0 971 shb = new ulong 8 0 972 for d ulong ra 973 put sha d get ot_ts_shape add mul sa 8 d 974 end 975 for d ulong rb 976 put shb d get ot_ts_shape add mul sb 8 d 977 end 978 dims = new ulong 3 0 979 put dims 0 ra 980 put dims 1 rb 981 put dims 2 r 982 ret ot_bcast_dims_3 sha shb sout dims 983 end 984 985 fn ot_gather_bcast s ulong rank ulong sh view ulong 8 dst_id ulong to long 986 elems = new ulong 1 0 987 rc = ot_broadcast_plan s rank sh elems 988 if ne rc OT_OK 989 ret rc 990 end 991 tb = mul s 10 992 ot_gather_desc get ot_ts_meta add tb 7 rank get ot_ts_meta add tb 5 4 get elems 0 dst_id 993 ret OT_OK 994 end 995 996 fn ot_binary_exec op ulong os ulong to void 997 elems = get ot_ts_meta add mul os 10 6 998 dbase = ot_slot_dbase os 999 for i ulong elems bound 4097 1000 ot_arena_put_f32 dbase i ot_binary_scalar op ot_buf_get_f32 0 i ot_buf_get_f32 1 i 1001 end 1002 end 1003 1004 fn ot_binary op ulong sa ulong ga ulong sb ulong gb ulong out_h edit ulong 2 to long 1005 if ge op OT_TB_COUNT 1006 ret OT_ERR_BAD_ARGS 1007 end 1008 rc = ot_ts_check sa ga 1009 if ne rc OT_OK 1010 ret rc 1011 end 1012 rc = ot_ts_check sb gb 1013 if ne rc OT_OK 1014 ret rc 1015 end 1016 if ne get ot_ts_meta add mul sa 10 3 OT_DT_F32 1017 ret OT_ERR_DTYPE 1018 end 1019 if ne get ot_ts_meta add mul sb 10 3 OT_DT_F32 1020 ret OT_ERR_DTYPE 1021 end 1022 ro = new ulong 1 0 1023 sh = new ulong 8 0 1024 rc = ot_bcast_two_shapes sa sb ro sh 1025 if ne rc OT_OK 1026 ret rc 1027 end 1028 r = get ro 0 1029 rc = ot_gather_bcast sa r sh 0 1030 if ne rc OT_OK 1031 ret rc 1032 end 1033 rc = ot_gather_bcast sb r sh 1 1034 if ne rc OT_OK 1035 ret rc 1036 end 1037 rc = ot_new_dense OT_DT_F32 r sh out_h 1038 if ne rc OT_OK 1039 ret rc 1040 end 1041 ot_binary_exec op get out_h 0 1042 ret OT_OK 1043 end 1044 1045 fn ot_check_f32_h h view ulong 2 to long 1046 s = get h 0 1047 rc = ot_ts_check s get h 1 1048 if ne rc OT_OK 1049 ret rc 1050 end 1051 if ne get ot_ts_meta add mul s 10 3 OT_DT_F32 1052 ret OT_ERR_DTYPE 1053 end 1054 ret OT_OK 1055 end 1056 1057 fn ot_bcast_merge_third sc ulong ro edit ulong 1 sh edit ulong 8 to long 1058 rc2 = get ot_ts_meta add mul sc 10 4 1059 r = get ro 0 1060 r3 = r 1061 if gt rc2 r3 1062 r3 = rc2 1063 end 1064 shc = new ulong 8 0 1065 for d ulong rc2 1066 put shc d get ot_ts_shape add mul sc 8 d 1067 end 1068 sh3 = new ulong 8 0 1069 dims = new ulong 3 0 1070 if gt r3 0 1071 put dims 0 r 1072 put dims 1 rc2 1073 put dims 2 r3 1074 rc = ot_bcast_dims_3 sh shc sh3 dims 1075 if ne rc OT_OK 1076 ret rc 1077 end 1078 for d ulong r3 1079 put sh d get sh3 d 1080 end 1081 end 1082 put ro 0 r3 1083 ret OT_OK 1084 end 1085 1086 fn ot_bcast_three ha view ulong 2 hb view ulong 2 hc view ulong 2 ro edit ulong 1 sh edit ulong 8 to long 1087 rc = ot_check_f32_h ha 1088 if ne rc OT_OK 1089 ret rc 1090 end 1091 rc = ot_check_f32_h hb 1092 if ne rc OT_OK 1093 ret rc 1094 end 1095 rc = ot_check_f32_h hc 1096 if ne rc OT_OK 1097 ret rc 1098 end 1099 rc = ot_bcast_two_shapes get ha 0 get hb 0 ro sh 1100 if ne rc OT_OK 1101 ret rc 1102 end 1103 rc = ot_bcast_merge_third get hc 0 ro sh 1104 if ne rc OT_OK 1105 ret rc 1106 end 1107 r3 = get ro 0 1108 rc = ot_gather_bcast get ha 0 r3 sh 0 1109 if ne rc OT_OK 1110 ret rc 1111 end 1112 rc = ot_gather_bcast get hb 0 r3 sh 1 1113 if ne rc OT_OK 1114 ret rc 1115 end 1116 ret ot_gather_bcast get hc 0 r3 sh 2 1117 end 1118 1119 fn ot_fma_exec os ulong to void 1120 elems = get ot_ts_meta add mul os 10 6 1121 dbase = ot_slot_dbase os 1122 for i ulong elems bound 4097 1123 ot_arena_put_f32 dbase i fma ot_buf_get_f32 0 i ot_buf_get_f32 1 i ot_buf_get_f32 2 i 1124 end 1125 end 1126 1127 fn ot_fma ha view ulong 2 hb view ulong 2 hc view ulong 2 out_h edit ulong 2 to long 1128 ro = new ulong 1 0 1129 sh = new ulong 8 0 1130 rc = ot_bcast_three ha hb hc ro sh 1131 if ne rc OT_OK 1132 ret rc 1133 end 1134 rc = ot_new_dense OT_DT_F32 get ro 0 sh out_h 1135 if ne rc OT_OK 1136 ret rc 1137 end 1138 ot_fma_exec get out_h 0 1139 ret OT_OK 1140 end 1141 1142 fn ot_where_exec os ulong n ulong to void 1143 dbase = ot_slot_dbase os 1144 for i ulong n bound 4097 1145 u = bitcast uint ot_buf_get_f32 0 i 1146 if eq u OT_ONE_BITS 1147 ot_arena_put_f32 dbase i ot_buf_get_f32 1 i 1148 else 1149 ot_arena_put_f32 dbase i ot_buf_get_f32 2 i 1150 end 1151 end 1152 end 1153 1154 fn ot_where hc view ulong 2 ha view ulong 2 hb view ulong 2 out_h edit ulong 2 to long 1155 ro = new ulong 1 0 1156 sh = new ulong 8 0 1157 rc = ot_bcast_three hc ha hb ro sh 1158 if ne rc OT_OK 1159 ret rc 1160 end 1161 elems = new ulong 1 0 1162 rc = ot_shape_check get ro 0 sh elems 1163 if ne rc OT_OK 1164 ret rc 1165 end 1166 n = get elems 0 1167 for i ulong n bound 4097 1168 u = bitcast uint ot_buf_get_f32 0 i 1169 if and ne u OT_ONE_BITS ne u uint 0 1170 ret OT_ERR_MASK 1171 end 1172 end 1173 rc = ot_new_dense OT_DT_F32 get ro 0 sh out_h 1174 if ne rc OT_OK 1175 ret rc 1176 end 1177 ot_where_exec get out_h 0 n 1178 ret OT_OK 1179 end 1180 1181 fn ot_reduce_exec op ulong rows ulong n ulong dbase ulong to void 1182 for r ulong rows bound 4097 1183 for k ulong n bound 4097 1184 put ot_red_a k ot_buf_get_f32 0 add mul r n k 1185 end 1186 ot_arena_put_f32 dbase r ot_reduce_cpu op n 1187 end 1188 end 1189 1190 fn ot_gather_f32 s ulong rank ulong elems ulong dst_id ulong to void 1191 tb = mul s 10 1192 ot_gather_desc get ot_ts_meta add tb 7 rank get ot_ts_meta add tb 5 4 elems dst_id 1193 end 1194 1195 fn ot_reduce_plan s ulong rank ulong axis ulong keepdims ulong osh edit ulong 8 to ulong 1196 ish = new ulong 8 0 1197 ist = new ulong 8 0 1198 sbase = mul s 8 1199 for d ulong rank 1200 put ish d get ot_ts_shape add sbase d 1201 put ist d get ot_ts_strides add sbase d 1202 end 1203 od = new ulong 24 0 1204 meta = new ulong 4 0 1205 put meta 0 rank 1206 put meta 1 axis 1207 put meta 2 keepdims 1208 put meta 3 1 1209 tensor_plan_op ish ist od meta 1210 for d ulong rank 1211 put ot_tmp_sh d get od d 1212 put ot_tmp_st d get od add 8 d 1213 end 1214 orank = sub rank 1 1215 if ne keepdims 0 1216 orank = rank 1217 end 1218 for d ulong orank 1219 put osh d get od add 16 d 1220 end 1221 ret get ish axis 1222 end 1223 1224 fn ot_reduce op ulong s ulong g ulong axis ulong keepdims ulong out_h edit ulong 2 to long 1225 if ge op OT_TR_COUNT 1226 ret OT_ERR_BAD_ARGS 1227 end 1228 rc = ot_ts_check s g 1229 if ne rc OT_OK 1230 ret rc 1231 end 1232 tb = mul s 10 1233 if ne get ot_ts_meta add tb 3 OT_DT_F32 1234 ret OT_ERR_DTYPE 1235 end 1236 rank = get ot_ts_meta add tb 4 1237 if eq rank 0 1238 ret OT_ERR_RANK 1239 end 1240 if ge axis rank 1241 ret OT_ERR_AXIS 1242 end 1243 osh = new ulong 8 0 1244 n = ot_reduce_plan s rank axis keepdims osh 1245 orank = sub rank 1 1246 if ne keepdims 0 1247 orank = rank 1248 end 1249 elems = get ot_ts_meta add tb 6 1250 ot_gather_f32 s rank elems 0 1251 rc = ot_new_dense OT_DT_F32 orank osh out_h 1252 if ne rc OT_OK 1253 ret rc 1254 end 1255 ot_reduce_exec op div elems n n ot_slot_dbase get out_h 0 1256 ret OT_OK 1257 end 1258 1259 fn ot_reduce_axes_run op ulong s ulong g ulong seen view ulong 8 keep_and_out edit ulong 3 to long 1260 rank = get ot_ts_meta add mul s 10 4 1261 keepdims = get keep_and_out 2 1262 cs = s 1263 cg = g 1264 step = ulong 0 1265 nh = new ulong 2 0 1266 for di ulong rank 1267 d = sub sub rank 1 di 1268 if eq get seen d 0 1269 continue 1270 end 1271 rc = ot_reduce op cs cg d keepdims nh 1272 if gt step 0 1273 rr = ot_release cs cg 1274 end 1275 if ne rc OT_OK 1276 ret rc 1277 end 1278 cs = get nh 0 1279 cg = get nh 1 1280 step = add step 1 1281 end 1282 put keep_and_out 0 cs 1283 put keep_and_out 1 cg 1284 ret OT_OK 1285 end 1286 1287 fn ot_reduce_axes_mark naxes ulong axes view ulong 8 rank ulong seen edit ulong 8 to long 1288 for i ulong naxes 1289 ax = get axes i 1290 if ge ax rank 1291 ret OT_ERR_AXIS 1292 end 1293 if ne get seen ax 0 1294 ret OT_ERR_AXIS 1295 end 1296 put seen ax 1 1297 end 1298 ret OT_OK 1299 end 1300 1301 fn ot_reduce_axes op ulong s ulong g ulong naxes ulong axes view ulong 8 keep_and_out edit ulong 3 to long 1302 if ge op OT_TR_COUNT 1303 ret OT_ERR_BAD_ARGS 1304 end 1305 rc = ot_ts_check s g 1306 if ne rc OT_OK 1307 ret rc 1308 end 1309 tb = mul s 10 1310 if ne get ot_ts_meta add tb 3 OT_DT_F32 1311 ret OT_ERR_DTYPE 1312 end 1313 rank = get ot_ts_meta add tb 4 1314 if eq rank 0 1315 ret OT_ERR_RANK 1316 end 1317 if gt naxes rank 1318 ret OT_ERR_AXIS 1319 end 1320 if and eq op OT_TR_MEAN gt naxes 1 1321 ret OT_ERR_BAD_ARGS 1322 end 1323 seen = new ulong 8 0 1324 out_h = new ulong 2 0 1325 rc = ot_reduce_axes_mark naxes axes rank seen 1326 if ne rc OT_OK 1327 ret rc 1328 end 1329 if eq naxes 0 1330 rc = ot_contiguous s g out_h 1331 put keep_and_out 0 get out_h 0 1332 put keep_and_out 1 get out_h 1 1333 ret rc 1334 end 1335 ret ot_reduce_axes_run op s g seen keep_and_out 1336 end 1337 1338 fn ot_sum_to_shape_exec s ulong g ulong n ulong ax view ulong 8 kp view ulong 8 out_h edit ulong 2 to long 1339 cs = s 1340 cg = g 1341 nh = new ulong 2 0 1342 for i ulong n 1343 rc = ot_reduce OT_TR_SUM cs cg get ax i get kp i nh 1344 if gt i 0 1345 rr = ot_release cs cg 1346 end 1347 if ne rc OT_OK 1348 ret rc 1349 end 1350 cs = get nh 0 1351 cg = get nh 1 1352 end 1353 put out_h 0 cs 1354 put out_h 1 cg 1355 ret OT_OK 1356 end 1357 1358 fn ot_sum_to_shape_plan s ulong rank ulong shape view ulong 8 ax edit ulong 8 kp edit ulong 8 to long 1359 ir = get ot_ts_meta add mul s 10 4 1360 if gt rank ir 1361 ret OT_ERR_SHAPE 1362 end 1363 lead = sub ir rank 1364 n = ulong 0 1365 sbase = mul s 8 1366 for di ulong ir 1367 d = sub sub ir 1 di 1368 idim = get ot_ts_shape add sbase d 1369 if lt d lead 1370 if gt idim 1 1371 put ax n d 1372 put kp n 0 1373 n = add n 1 1374 end 1375 else 1376 want = get shape sub d lead 1377 if ne want idim 1378 if ne want 1 1379 ret OT_ERR_SHAPE 1380 end 1381 put ax n d 1382 put kp n 1 1383 n = add n 1 1384 end 1385 end 1386 end 1387 ret long n 1388 end 1389 1390 fn ot_sum_to_shape s ulong g ulong rank ulong shape view ulong 8 out_h edit ulong 2 to long 1391 rc = ot_ts_check s g 1392 if ne rc OT_OK 1393 ret rc 1394 end 1395 elems = new ulong 1 0 1396 rc = ot_shape_check rank shape elems 1397 if ne rc OT_OK 1398 ret rc 1399 end 1400 if ne get ot_ts_meta add mul s 10 3 OT_DT_F32 1401 ret OT_ERR_DTYPE 1402 end 1403 ax = new ulong 8 0 1404 kp = new ulong 8 0 1405 nr = ot_sum_to_shape_plan s rank shape ax kp 1406 if lt nr 0 1407 ret nr 1408 end 1409 n = ulong nr 1410 rh = new ulong 2 0 1411 rc = ot_sum_to_shape_exec s g n ax kp rh 1412 if ne rc OT_OK 1413 ret rc 1414 end 1415 ot_gather_slot get rh 0 0 1416 rc = ot_new_dense OT_DT_F32 rank shape out_h 1417 if gt n 0 1418 rr = ot_release get rh 0 get rh 1 1419 end 1420 if ne rc OT_OK 1421 ret rc 1422 end 1423 ot_copy_buf_a_to_slot get out_h 0 mul get elems 0 4 1424 ret OT_OK 1425 end 1426 1427 fn ot_mm_get_f32 is_bt ulong idx ulong to f32 1428 p = mul idx 4 1429 u = uint 0 1430 if eq is_bt 0 1431 u = uint get ot_mm_a p 1432 u = or u shl uint get ot_mm_a add p 1 8 1433 u = or u shl uint get ot_mm_a add p 2 16 1434 u = or u shl uint get ot_mm_a add p 3 24 1435 else 1436 u = uint get ot_mm_bt p 1437 u = or u shl uint get ot_mm_bt add p 1 8 1438 u = or u shl uint get ot_mm_bt add p 2 16 1439 u = or u shl uint get ot_mm_bt add p 3 24 1440 end 1441 ret bitcast f32 u 1442 end 1443 1444 fn ot_matmul_dot ab ulong bb ulong k ulong to f32 1445 for t ulong k bound 4096 1446 va = ot_mm_get_f32 0 add ab t 1447 vb = ot_mm_get_f32 1 add bb t 1448 put ot_red_a t mul va vb 1449 end 1450 ret ot_reduce_cpu OT_TR_SUM k 1451 end 1452 1453 fn ot_matmul_row ab ulong bbase ulong n ulong k ulong obase ulong dbase ulong to void 1454 for j ulong n bound 256 1455 bb = mul add bbase j k 1456 ot_arena_put_f32 dbase add obase j ot_matmul_dot ab bb k 1457 end 1458 end 1459 1460 fn ot_matmul_exec batches ulong m ulong n ulong k ulong dbase ulong to void 1461 for b ulong batches bound 256 1462 bbase = mul b n 1463 for i ulong m bound 256 1464 row = add mul b m i 1465 ot_matmul_row mul row k bbase n k mul row n dbase 1466 end 1467 end 1468 end 1469 1470 fn ot_matmul_bcast_batch sa ulong sb ulong bra ulong brb ulong br ulong bsh edit ulong 8 to long 1471 if eq br 0 1472 ret OT_OK 1473 end 1474 sha0 = new ulong 8 0 1475 shb0 = new ulong 8 0 1476 for d ulong bra 1477 put sha0 d get ot_ts_shape add mul sa 8 d 1478 end 1479 for d ulong brb 1480 put shb0 d get ot_ts_shape add mul sb 8 d 1481 end 1482 dims = new ulong 3 0 1483 put dims 0 bra 1484 put dims 1 brb 1485 put dims 2 br 1486 ret ot_bcast_dims_3 sha0 shb0 bsh dims 1487 end 1488 1489 fn ot_matmul_plan sa ulong sb ulong dims edit ulong 4 bsh edit ulong 8 to long 1490 ra = get ot_ts_meta add mul sa 10 4 1491 rb = get ot_ts_meta add mul sb 10 4 1492 if or lt ra 2 lt rb 2 1493 ret OT_ERR_RANK 1494 end 1495 k = get ot_ts_shape add mul sa 8 sub ra 1 1496 if ne k get ot_ts_shape add mul sb 8 sub rb 2 1497 ret OT_ERR_SHAPE 1498 end 1499 br = sub ra 2 1500 if gt sub rb 2 br 1501 br = sub rb 2 1502 end 1503 if gt add br 2 OT_MAX_RANK 1504 ret OT_ERR_RANK 1505 end 1506 put dims 0 br 1507 put dims 1 get ot_ts_shape add mul sa 8 sub ra 2 1508 put dims 2 get ot_ts_shape add mul sb 8 sub rb 1 1509 put dims 3 k 1510 ret ot_matmul_bcast_batch sa sb sub ra 2 sub rb 2 br bsh 1511 end 1512 1513 fn ot_matmul sa ulong ga ulong sb ulong gb ulong out_h edit ulong 2 to long 1514 rc = ot_ts_check sa ga 1515 if ne rc OT_OK 1516 ret rc 1517 end 1518 rc = ot_ts_check sb gb 1519 if ne rc OT_OK 1520 ret rc 1521 end 1522 if ne get ot_ts_meta add mul sa 10 3 OT_DT_F32 1523 ret OT_ERR_DTYPE 1524 end 1525 if ne get ot_ts_meta add mul sb 10 3 OT_DT_F32 1526 ret OT_ERR_DTYPE 1527 end 1528 bsh = new ulong 8 0 1529 dims = new ulong 4 0 1530 rc = ot_matmul_plan sa sb dims bsh 1531 if ne rc OT_OK 1532 ret rc 1533 end 1534 ret ot_matmul_run sa sb dims bsh out_h 1535 end 1536 1537 fn ot_matmul_pack_b sb ulong br ulong shb view ulong 8 to long 1538 r2 = add br 2 1539 eb = new ulong 1 0 1540 rc = ot_broadcast_plan sb r2 shb eb 1541 if ne rc OT_OK 1542 ret rc 1543 end 1544 t0 = get ot_tmp_sh br 1545 t1 = get ot_tmp_st br 1546 put ot_tmp_sh br get ot_tmp_sh add br 1 1547 put ot_tmp_st br get ot_tmp_st add br 1 1548 put ot_tmp_sh add br 1 t0 1549 put ot_tmp_st add br 1 t1 1550 ot_gather_f32 sb r2 get eb 0 5 1551 ret OT_OK 1552 end 1553 1554 fn ot_matmul_dispatch ea_n ulong dims view ulong 4 out_slot ulong to void 1555 m = get dims 1 1556 n = get dims 2 1557 k = get dims 3 1558 batches = div ea_n mul m k 1559 ot_matmul_exec batches m n k ot_slot_dbase out_slot 1560 end 1561 1562 fn ot_matmul_run sa ulong sb ulong dims view ulong 4 bsh view ulong 8 out_h edit ulong 2 to long 1563 br = get dims 0 1564 sha = new ulong 8 0 1565 shb = new ulong 8 0 1566 sho = new ulong 8 0 1567 for d ulong br 1568 put sha d get bsh d 1569 put shb d get bsh d 1570 put sho d get bsh d 1571 end 1572 put sha br get dims 1 1573 put sha add br 1 get dims 3 1574 put shb br get dims 3 1575 put shb add br 1 get dims 2 1576 put sho br get dims 1 1577 put sho add br 1 get dims 2 1578 r2 = add br 2 1579 ea = new ulong 1 0 1580 rc = ot_broadcast_plan sa r2 sha ea 1581 if ne rc OT_OK 1582 ret rc 1583 end 1584 ot_gather_f32 sa r2 get ea 0 4 1585 rc = ot_matmul_pack_b sb br shb 1586 if ne rc OT_OK 1587 ret rc 1588 end 1589 rc = ot_new_dense OT_DT_F32 r2 sho out_h 1590 if ne rc OT_OK 1591 ret rc 1592 end 1593 ot_matmul_dispatch get ea 0 dims get out_h 0 1594 ret OT_OK 1595 end 1596 1597 fn ot_embed_dim s ulong d ulong osh_d ulong a ulong st ulong ds_d ulong to long 1598 if eq st 0 1599 ret OT_ERR_BAD_ARGS 1600 end 1601 idim = get ot_ts_shape add mul s 8 d 1602 last = add a mul sub idim 1 st 1603 if ge last osh_d 1604 ret OT_ERR_BOUNDS 1605 end 1606 put ot_tmp_sh d idim 1607 if eq idim 1 1608 put ot_tmp_st d 0 1609 else 1610 put ot_tmp_st d mul ds_d st 1611 end 1612 ret long mul a ds_d 1613 end 1614 1615 fn ot_embed_plan s ulong out_sh view ulong 8 start view ulong 8 step view ulong 8 off_out edit ulong 1 to long 1616 rank = get ot_ts_meta add mul s 10 4 1617 if eq rank 0 1618 ret OT_ERR_RANK 1619 end 1620 oe = new ulong 1 0 1621 rc = ot_shape_check rank out_sh oe 1622 if ne rc OT_OK 1623 ret rc 1624 end 1625 ds = new ulong 8 0 1626 tensor_dense_strides part out_sh 0 rank part ds 0 rank 1627 off = ulong 0 1628 for d ulong rank 1629 rc = ot_embed_dim s d get out_sh d get start d get step d get ds d 1630 if lt rc 0 1631 ret rc 1632 end 1633 off = add off ulong rc 1634 end 1635 put off_out 0 off 1636 ret OT_OK 1637 end 1638 1639 fn ot_scatter_from_slot s ulong dst_slot ulong off ulong to void 1640 tb = mul s 10 1641 dst_st = get ot_ts_meta add mul dst_slot 10 7 1642 ot_scatter_buf_a dst_st get ot_ts_meta add tb 4 off ot_dtype_size get ot_ts_meta add tb 3 get ot_ts_meta add tb 6 1643 end 1644 1645 fn ot_embed s ulong g ulong out_sh view ulong 8 start view ulong 8 step view ulong 8 out_h edit ulong 2 to long 1646 rc = ot_ts_check s g 1647 if ne rc OT_OK 1648 ret rc 1649 end 1650 off_a = new ulong 1 0 1651 rc = ot_embed_plan s out_sh start step off_a 1652 if ne rc OT_OK 1653 ret rc 1654 end 1655 tb = mul s 10 1656 dt = get ot_ts_meta add tb 3 1657 rank = get ot_ts_meta add tb 4 1658 ot_gather_slot s 0 1659 rc = ot_new_dense dt rank out_sh out_h 1660 if ne rc OT_OK 1661 ret rc 1662 end 1663 ot_scatter_from_slot s get out_h 0 get off_a 0 1664 ret OT_OK 1665 end 1666 1667 fn ot_concat_copy_one sk ulong rank ulong ds view ulong 8 dst_slot ulong off ulong to void 1668 ot_gather_slot sk 0 1669 sbase = mul sk 8 1670 for d ulong rank 1671 put ot_tmp_sh d get ot_ts_shape add sbase d 1672 put ot_tmp_st d get ds d 1673 end 1674 ot_scatter_from_slot sk dst_slot off 1675 end 1676 1677 fn ot_concat_copy n ulong slots view ulong 16 osh view ulong 8 axis ulong dst_slot ulong to void 1678 rank = get ot_ts_meta add mul dst_slot 10 4 1679 ds = new ulong 8 0 1680 tensor_dense_strides part osh 0 rank part ds 0 rank 1681 aoff = ulong 0 1682 for k ulong n 1683 sk = get slots k 1684 ot_concat_copy_one sk rank ds dst_slot mul aoff get ds axis 1685 aoff = add aoff get ot_ts_shape add mul sk 8 axis 1686 end 1687 end 1688 1689 fn ot_concat_check_one sk ulong gk ulong dt ulong rank ulong axis ulong osh edit ulong 8 to long 1690 rc = ot_ts_check sk gk 1691 if ne rc OT_OK 1692 ret rc 1693 end 1694 tbk = mul sk 10 1695 if ne get ot_ts_meta add tbk 3 dt 1696 ret OT_ERR_DTYPE 1697 end 1698 if ne get ot_ts_meta add tbk 4 rank 1699 ret OT_ERR_RANK 1700 end 1701 sbase = mul sk 8 1702 for d ulong rank 1703 if ne d axis 1704 if ne get ot_ts_shape add sbase d get osh d 1705 ret OT_ERR_SHAPE 1706 end 1707 end 1708 end 1709 put osh axis add get osh axis get ot_ts_shape add sbase axis 1710 ret OT_OK 1711 end 1712 1713 fn ot_concat_plan n ulong slots view ulong 16 gens view ulong 16 axis ulong osh edit ulong 8 to long 1714 s0 = get slots 0 1715 tb0 = mul s0 10 1716 dt = get ot_ts_meta add tb0 3 1717 rank = get ot_ts_meta add tb0 4 1718 for d ulong rank 1719 put osh d get ot_ts_shape add mul s0 8 d 1720 end 1721 put osh axis 0 1722 for k ulong n 1723 rc = ot_concat_check_one get slots k get gens k dt rank axis osh 1724 if ne rc OT_OK 1725 ret rc 1726 end 1727 end 1728 ret OT_OK 1729 end 1730 1731 fn ot_concat n ulong slots view ulong 16 gens view ulong 16 axis ulong out_h edit ulong 2 to long 1732 if or eq n 0 gt n 16 1733 ret OT_ERR_BAD_ARGS 1734 end 1735 s0 = get slots 0 1736 rc = ot_ts_check s0 get gens 0 1737 if ne rc OT_OK 1738 ret rc 1739 end 1740 tb0 = mul s0 10 1741 dt = get ot_ts_meta add tb0 3 1742 rank = get ot_ts_meta add tb0 4 1743 if eq rank 0 1744 ret OT_ERR_RANK 1745 end 1746 if ge axis rank 1747 ret OT_ERR_AXIS 1748 end 1749 osh = new ulong 8 0 1750 rc = ot_concat_plan n slots gens axis osh 1751 if ne rc OT_OK 1752 ret rc 1753 end 1754 rc = ot_new_dense dt rank osh out_h 1755 if ne rc OT_OK 1756 ret rc 1757 end 1758 ot_concat_copy n slots osh axis get out_h 0 1759 ret OT_OK 1760 end