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/aienux/crates/aienos-infer/infer_decode.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 / aienux / crates / aienos-infer / infer_decode.om` 1777 lines ยท 36119 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note aienux/crates/aienos-infer/infer_decode.om: Pure-Omega model binding, BPE tokenizer, and Llama forward/decode. 3 note Ported from aienux crates/aienos-infer/src/{model,tokenizer,decode}.rs. 4 note Compose with: si/directives/om2.om aienux/crates/aienos-infer/infer_core.om aienux/crates/aienos-infer/infer_decode.om ... 5 6 note Model capacity constants 7 const INF_MAX_LAYERS ulong 8 8 const INF_MAX_WEIGHTS ulong 128 9 const INF_MAX_NORM_ELEMS ulong 8192 10 11 note Tokenizer capacity constants 12 const INF_TOK_MAX_VOCAB ulong 512 13 const INF_TOK_MAX_MERGES ulong 512 14 const INF_TOK_MAX_WORD ulong 256 15 const INF_TOK_MAX_SPECIAL ulong 64 16 const INF_TOK_TYPE_CONTROL int 3 17 const INF_TOK_TYPE_USER_DEF int 4 18 19 note Decode state capacity constants 20 const INF_DEC_MAX_KV ulong 32768 21 const INF_DEC_MAX_DIM ulong 1024 22 const INF_DEC_MAX_POS ulong 256 23 const INF_DEC_MAX_VOCAB ulong 512 24 25 note Model global tables (bound weight descriptors & f32 norm vectors) 26 global inf_w_off ulong 128 27 global inf_w_ty uint 128 28 global inf_w_rows ulong 128 29 global inf_w_cols ulong 128 30 global inf_w_rbytes ulong 128 31 global inf_w_cnt ulong 1 32 33 note Per-layer indices into inf_w_* and inf_norms 34 global inf_lyr_attn_norm ulong 8 35 global inf_lyr_wq ulong 8 36 global inf_lyr_wk ulong 8 37 global inf_lyr_wv ulong 8 38 global inf_lyr_wo ulong 8 39 global inf_lyr_ffn_norm ulong 8 40 global inf_lyr_wgate ulong 8 41 global inf_lyr_wup ulong 8 42 global inf_lyr_wdown ulong 8 43 44 global inf_norms f32 8192 45 global inf_norms_used ulong 1 46 47 note Tokenizer global tables & output buffer 48 global inf_tok_off ulong 512 49 global inf_tok_len ulong 512 50 global inf_tok_type int 512 51 global inf_tok_has_types uint 1 52 global inf_mrg_off ulong 512 53 global inf_mrg_len ulong 512 54 global inf_tok_meta ulong 8 55 global inf_tok_ids uint 512 56 global inf_tok_ids_len ulong 1 57 global inf_bpe_ss ulong 256 58 global inf_bpe_se ulong 256 59 global inf_byte_to_cp_tbl uint 256 60 global inf_byte_tbl_init uint 1 61 62 note Decode state global buffers 63 global inf_kv_k f32 32768 64 global inf_kv_v f32 32768 65 global inf_st_x f32 1024 66 global inf_st_h f32 1024 67 global inf_st_q f32 1024 68 global inf_st_kk f32 1024 69 global inf_st_vv f32 1024 70 global inf_st_att f32 1024 71 global inf_st_scores f32 256 72 global inf_st_gate f32 1024 73 global inf_st_up f32 1024 74 global inf_st_tmp f32 1024 75 global inf_st_rope f32 1024 76 global inf_st_logits f32 512 77 global inf_gen_ids uint 256 78 79 note ======================================================================== 80 note Model weight binding & matvec (model.rs) 81 note ======================================================================== 82 83 text inf_nm_tok_embd "token_embd.weight" 84 text inf_nm_output "output.weight" 85 text inf_nm_output_norm "output_norm.weight" 86 text inf_nm_rope_freqs "rope_freqs.weight" 87 text inf_sfx_attn_norm "attn_norm.weight" 88 text inf_sfx_attn_q "attn_q.weight" 89 text inf_sfx_attn_k "attn_k.weight" 90 text inf_sfx_attn_v "attn_v.weight" 91 text inf_sfx_attn_out "attn_output.weight" 92 text inf_sfx_ffn_norm "ffn_norm.weight" 93 text inf_sfx_ffn_gate "ffn_gate.weight" 94 text inf_sfx_ffn_up "ffn_up.weight" 95 text inf_sfx_ffn_down "ffn_down.weight" 96 97 fn inf_fmt_blk_name lyr uint sfx view uchar * dst edit uchar 64 to ulong 98 put dst 0 uchar 98 99 put dst 1 uchar 108 100 put dst 2 uchar 107 101 put dst 3 uchar 46 102 pos = ulong 4 103 if ge lyr 10 104 put dst pos uchar add 48 div lyr 10 105 pos = add pos 1 106 end 107 put dst pos uchar add 48 rem lyr 10 108 pos = add pos 1 109 put dst pos uchar 46 110 pos = add pos 1 111 slen = len sfx 112 for i ulong slen bound 48 113 put dst add pos i get sfx i 114 end 115 ret add pos slen 116 end 117 118 fn inf_weight_is_supported ty uint to bool 119 if eq ty INF_GGML_Q4K 120 ret true 121 end 122 if eq ty INF_GGML_Q6K 123 ret true 124 end 125 if eq ty INF_GGML_Q8_0 126 ret true 127 end 128 if eq ty INF_GGML_F32 129 ret true 130 end 131 if eq ty INF_GGML_F16 132 ret true 133 end 134 if eq ty INF_GGML_BF16 135 ret true 136 end 137 ret false 138 end 139 140 fn inf_weight_is_kquant widx ulong to bool 141 ty = get inf_w_ty widx 142 if eq ty INF_GGML_Q4K 143 ret true 144 end 145 if eq ty INF_GGML_Q6K 146 ret true 147 end 148 ret false 149 end 150 151 fn inf_weight_store u ulong dstart ulong rows ulong cols ulong to long 152 ty = get inf_ten_ty u 153 if not inf_weight_is_supported ty 154 ret long INF_ERR_BAD_TENSOR 155 end 156 if ne rem cols inf_ggml_block_w ty 0 157 ret long INF_ERR_BAD_TENSOR 158 end 159 rbytes = inf_gguf_tensor_row_bytes u 160 if ne get inf_ten_nbytes u mul rbytes rows 161 ret long INF_ERR_BAD_TENSOR 162 end 163 widx = get inf_w_cnt 0 164 if ge widx INF_MAX_WEIGHTS 165 ret long INF_ERR_CAPACITY 166 end 167 put inf_w_off widx add dstart get inf_ten_off u 168 put inf_w_ty widx ty 169 put inf_w_rows widx rows 170 put inf_w_cols widx cols 171 put inf_w_rbytes widx rbytes 172 put inf_w_cnt 0 add widx 1 173 ret long widx 174 end 175 176 fn inf_weight_bind g view InfGguf b view uchar * name view uchar * rc_dims view ulong 2 to long 177 ti = inf_gguf_find_tensor g b name 178 if lt ti 0 179 ret long INF_ERR_MISSING_TENSOR 180 end 181 u = ulong ti 182 if ne get inf_ten_ndims u 2 183 ret long INF_ERR_BAD_TENSOR 184 end 185 if ne get inf_ten_d0 u get rc_dims 1 186 ret long INF_ERR_BAD_TENSOR 187 end 188 if ne get inf_ten_d1 u get rc_dims 0 189 ret long INF_ERR_BAD_TENSOR 190 end 191 ret inf_weight_store u get g data_start get rc_dims 0 get rc_dims 1 192 end 193 194 fn inf_load_norm_vec g view InfGguf b view uchar * name view uchar * n ulong to long 195 ti = inf_gguf_find_tensor g b name 196 if lt ti 0 197 ret long INF_ERR_MISSING_TENSOR 198 end 199 u = ulong ti 200 if ne get inf_ten_ndims u 1 201 ret long INF_ERR_BAD_TENSOR 202 end 203 if ne get inf_ten_d0 u n 204 ret long INF_ERR_BAD_TENSOR 205 end 206 if ne get inf_ten_ty u INF_GGML_F32 207 ret long INF_ERR_BAD_TENSOR 208 end 209 base = get inf_norms_used 0 210 if gt add base n INF_MAX_NORM_ELEMS 211 ret long INF_ERR_CAPACITY 212 end 213 doff = add get g data_start get inf_ten_off u 214 for i ulong n bound 8192 215 put inf_norms add base i inf_rd_f32_le b add doff mul i 4 216 end 217 put inf_norms_used 0 add base n 218 ret long base 219 end 220 221 fn inf_model_bind_layer g view InfGguf b view uchar * m view InfModel lyr uint to int 222 nbuf = new uchar 64 0 223 dims = new ulong 2 0 224 n_embd = ulong get m embedding_length 225 kv_dim = get m kv_dim 226 n_ff = ulong get m feed_forward_length 227 ul = ulong lyr 228 nlen = inf_fmt_blk_name lyr inf_sfx_attn_norm nbuf 229 r = inf_load_norm_vec g b part nbuf 0 nlen n_embd 230 if lt r 0 231 ret int r 232 end 233 put inf_lyr_attn_norm ul ulong r 234 put dims 0 n_embd 235 put dims 1 n_embd 236 nlen = inf_fmt_blk_name lyr inf_sfx_attn_q nbuf 237 r = inf_weight_bind g b part nbuf 0 nlen dims 238 if lt r 0 239 ret int r 240 end 241 put inf_lyr_wq ul ulong r 242 put dims 0 kv_dim 243 put dims 1 n_embd 244 nlen = inf_fmt_blk_name lyr inf_sfx_attn_k nbuf 245 r = inf_weight_bind g b part nbuf 0 nlen dims 246 if lt r 0 247 ret int r 248 end 249 put inf_lyr_wk ul ulong r 250 nlen = inf_fmt_blk_name lyr inf_sfx_attn_v nbuf 251 r = inf_weight_bind g b part nbuf 0 nlen dims 252 if lt r 0 253 ret int r 254 end 255 put inf_lyr_wv ul ulong r 256 put dims 0 n_embd 257 put dims 1 n_embd 258 nlen = inf_fmt_blk_name lyr inf_sfx_attn_out nbuf 259 r = inf_weight_bind g b part nbuf 0 nlen dims 260 if lt r 0 261 ret int r 262 end 263 put inf_lyr_wo ul ulong r 264 nlen = inf_fmt_blk_name lyr inf_sfx_ffn_norm nbuf 265 r = inf_load_norm_vec g b part nbuf 0 nlen n_embd 266 if lt r 0 267 ret int r 268 end 269 put inf_lyr_ffn_norm ul ulong r 270 put dims 0 n_ff 271 put dims 1 n_embd 272 nlen = inf_fmt_blk_name lyr inf_sfx_ffn_gate nbuf 273 r = inf_weight_bind g b part nbuf 0 nlen dims 274 if lt r 0 275 ret int r 276 end 277 put inf_lyr_wgate ul ulong r 278 nlen = inf_fmt_blk_name lyr inf_sfx_ffn_up nbuf 279 r = inf_weight_bind g b part nbuf 0 nlen dims 280 if lt r 0 281 ret int r 282 end 283 put inf_lyr_wup ul ulong r 284 put dims 0 n_embd 285 put dims 1 n_ff 286 nlen = inf_fmt_blk_name lyr inf_sfx_ffn_down nbuf 287 r = inf_weight_bind g b part nbuf 0 nlen dims 288 if lt r 0 289 ret int r 290 end 291 put inf_lyr_wdown ul ulong r 292 ret INF_OK 293 end 294 295 fn inf_model_bind_weights g view InfGguf b view uchar * m edit InfModel to int 296 dims = new ulong 2 0 297 n_embd = ulong get m embedding_length 298 put inf_w_cnt 0 ulong 0 299 put inf_norms_used 0 ulong 0 300 put dims 0 ulong get m vocab_size 301 put dims 1 n_embd 302 r = inf_weight_bind g b inf_nm_tok_embd dims 303 if lt r 0 304 ret int r 305 end 306 put m tok_embd_w ulong r 307 put m output_w ulong r 308 if ge inf_gguf_find_tensor g b inf_nm_output 0 309 r = inf_weight_bind g b inf_nm_output dims 310 if lt r 0 311 ret int r 312 end 313 put m output_w ulong r 314 end 315 bc = ulong get m block_count 316 for l ulong bc bound 8 317 rc = inf_model_bind_layer g b m uint l 318 if ne rc INF_OK 319 ret rc 320 end 321 end 322 r = inf_load_norm_vec g b inf_nm_output_norm n_embd 323 if lt r 0 324 ret int r 325 end 326 put m output_norm_off ulong r 327 put m rope_fac_off ulong 0 328 put m has_rope_fac uint 0 329 if ge inf_gguf_find_tensor g b inf_nm_rope_freqs 0 330 r = inf_load_norm_vec g b inf_nm_rope_freqs div get m head_dim 2 331 if lt r 0 332 ret int r 333 end 334 put m rope_fac_off ulong r 335 put m has_rope_fac uint 1 336 end 337 ret INF_OK 338 end 339 340 fn inf_model_bind g view InfGguf b view uchar * m edit InfModel to int 341 p = new InfLlamaParams 0 0 0 0 0 0 0.0f32 0.0f32 0 342 rc = inf_gguf_llama_params g b p 343 if ne rc INF_OK 344 ret rc 345 end 346 n_embd = ulong get p embedding_length 347 n_head = ulong get p head_count 348 n_kv = ulong get p head_count_kv 349 if eq n_head 0 350 ret INF_ERR_BAD_PARAMS 351 end 352 if eq n_kv 0 353 ret INF_ERR_BAD_PARAMS 354 end 355 if ne rem n_embd n_head 0 356 ret INF_ERR_BAD_PARAMS 357 end 358 if ne rem n_head n_kv 0 359 ret INF_ERR_BAD_PARAMS 360 end 361 hd = div n_embd n_head 362 if ne rem hd 2 0 363 ret INF_ERR_BAD_PARAMS 364 end 365 if gt ulong get p block_count INF_MAX_LAYERS 366 ret INF_ERR_CAPACITY 367 end 368 put m block_count get p block_count 369 put m embedding_length get p embedding_length 370 put m head_count get p head_count 371 put m head_count_kv get p head_count_kv 372 put m feed_forward_length get p feed_forward_length 373 put m context_length get p context_length 374 put m rope_freq_base get p rope_freq_base 375 put m rms_norm_eps get p rms_norm_eps 376 put m vocab_size get p vocab_size 377 put m head_dim hd 378 put m kv_dim mul n_kv hd 379 ret inf_model_bind_weights g b m 380 end 381 382 fn inf_weight_row_into widx ulong b view uchar * row ulong dst edit f32 * to int 383 rows = get inf_w_rows widx 384 cols = get inf_w_cols widx 385 if ge row rows 386 ret INF_ERR_BAD_TOKEN 387 end 388 if ne len dst cols 389 ret INF_ERR_BAD_TOKEN 390 end 391 rbytes = get inf_w_rbytes widx 392 roff = add get inf_w_off widx mul row rbytes 393 ty = get inf_w_ty widx 394 if inf_plain_to_f32 ty part b roff rbytes dst 395 ret INF_OK 396 end 397 ret inf_dequantize ty part b roff rbytes dst 398 end 399 400 fn inf_dot_plain ty uint b view uchar * roff ulong x view f32 * to f32 401 acc = f32 0.0f32 402 c = ulong 0 403 while lt c len x bound 65536 404 if eq ty INF_GGML_F32 405 acc = add acc mul inf_rd_f32_le b add roff mul c 4 get x c 406 else 407 if eq ty INF_GGML_F16 408 acc = add acc mul inf_rd_f16_le b add roff mul c 2 get x c 409 else 410 acc = add acc mul inf_rd_bf16_le b add roff mul c 2 get x c 411 end 412 end 413 c = add c 1 414 end 415 ret acc 416 end 417 418 fn inf_weight_matvec_row wr view ulong 2 b view uchar * x view f32 * res edit f32 1 to int 419 widx = get wr 0 420 ty = get inf_w_ty widx 421 rbytes = get inf_w_rbytes widx 422 roff = add get inf_w_off widx mul get wr 1 rbytes 423 if eq ty INF_GGML_Q4K 424 ret inf_dot_row ty part b roff rbytes x res 425 end 426 if eq ty INF_GGML_Q6K 427 ret inf_dot_row ty part b roff rbytes x res 428 end 429 if eq ty INF_GGML_Q8_0 430 ret inf_dot_row ty part b roff rbytes x res 431 end 432 if eq ty INF_GGML_F32 433 put res 0 inf_dot_plain ty b roff x 434 ret INF_OK 435 end 436 if eq ty INF_GGML_F16 437 put res 0 inf_dot_plain ty b roff x 438 ret INF_OK 439 end 440 if eq ty INF_GGML_BF16 441 put res 0 inf_dot_plain ty b roff x 442 ret INF_OK 443 end 444 ret INF_QUANT_UNSUPPORTED 445 end 446 447 fn inf_weight_matvec_q8 widx ulong b view uchar * dst edit f32 * to int 448 if not inf_weight_is_kquant widx 449 ret INF_ERR_BAD_PARAMS 450 end 451 rows = get inf_w_rows widx 452 cols = get inf_w_cols widx 453 if ne len dst rows 454 ret INF_ERR_BAD_PARAMS 455 end 456 if ne mul inf_q8_blocks INF_QK_K cols 457 ret INF_ERR_BAD_PARAMS 458 end 459 ty = get inf_w_ty widx 460 rbytes = get inf_w_rbytes widx 461 base = get inf_w_off widx 462 res = new f32 1 0.0f32 463 for r ulong rows bound 65536 464 roff = add base mul r rbytes 465 rc = inf_dot_row_q8 ty part b roff rbytes res 466 if ne rc INF_OK 467 ret rc 468 end 469 put dst r get res 0 470 end 471 ret INF_OK 472 end 473 474 note ======================================================================== 475 note Llama-3 byte-level BPE tokenizer (tokenizer.rs) 476 note ======================================================================== 477 478 fn inf_gpt2_keep b uchar to bool 479 if ge b uchar 33 480 if le b uchar 126 481 ret true 482 end 483 end 484 if ge b uchar 161 485 if le b uchar 172 486 ret true 487 end 488 end 489 if ge b uchar 174 490 ret true 491 end 492 ret false 493 end 494 495 fn inf_byte_to_cp b uchar to uint 496 if inf_gpt2_keep b 497 ret uint b 498 end 499 n = uint 0 500 for x ulong ulong b bound 256 501 if not inf_gpt2_keep uchar x 502 n = add n 1 503 end 504 end 505 ret add 256 n 506 end 507 508 fn inf_cp_to_byte cp uint to int 509 if lt cp 256 510 b = uchar cp 511 if inf_gpt2_keep b 512 ret int b 513 end 514 ret -1 515 end 516 n = sub cp 256 517 seen = uint 0 518 for x ulong 256 519 if not inf_gpt2_keep uchar x 520 if eq seen n 521 ret int x 522 end 523 seen = add seen 1 524 end 525 end 526 ret -1 527 end 528 529 fn inf_utf8_encode cp uint dst edit uchar * pos ulong to ulong 530 if lt cp 0x80 531 put dst pos uchar cp 532 ret 1 533 end 534 if lt cp 0x800 535 put dst pos uchar or 0xc0 shr cp 6 536 put dst add pos 1 uchar or 0x80 and cp 0x3f 537 ret 2 538 end 539 if lt cp 0x10000 540 put dst pos uchar or 0xe0 shr cp 12 541 put dst add pos 1 uchar or 0x80 and shr cp 6 0x3f 542 put dst add pos 2 uchar or 0x80 and cp 0x3f 543 ret 3 544 end 545 put dst pos uchar or 0xf0 shr cp 18 546 put dst add pos 1 uchar or 0x80 and shr cp 12 0x3f 547 put dst add pos 2 uchar or 0x80 and shr cp 6 0x3f 548 put dst add pos 3 uchar or 0x80 and cp 0x3f 549 ret 4 550 end 551 552 fn inf_utf8_char_len b uchar to ulong 553 if lt b uchar 0x80 554 ret 1 555 end 556 if lt b uchar 0xe0 557 ret 2 558 end 559 if lt b uchar 0xf0 560 ret 3 561 end 562 ret 4 563 end 564 565 fn inf_utf8_decode s view uchar * pos ulong to uint 566 b0 = uint get s pos 567 if lt b0 0x80 568 ret b0 569 end 570 if lt b0 0xe0 571 b1 = uint get s add pos 1 572 ret or shl and b0 0x1f 6 and b1 0x3f 573 end 574 if lt b0 0xf0 575 b1 = uint get s add pos 1 576 b2 = uint get s add pos 2 577 r = shl and b0 0x0f 12 578 r = or r shl and b1 0x3f 6 579 ret or r and b2 0x3f 580 end 581 b1 = uint get s add pos 1 582 b2 = uint get s add pos 2 583 b3 = uint get s add pos 3 584 r = shl and b0 0x07 18 585 r = or r shl and b1 0x3f 12 586 r = or r shl and b2 0x3f 6 587 ret or r and b3 0x3f 588 end 589 590 fn inf_cp_is_letter cp uint to bool 591 if ge cp 65 592 if le cp 90 593 ret true 594 end 595 end 596 if ge cp 97 597 if le cp 122 598 ret true 599 end 600 end 601 if ge cp 0xc0 602 if le cp 0x24f 603 if ne cp 0xd7 604 if ne cp 0xf7 605 ret true 606 end 607 end 608 end 609 end 610 if ge cp 0x370 611 if le cp 0x52f 612 ret true 613 end 614 end 615 if ge cp 0x4e00 616 if le cp 0x9fff 617 ret true 618 end 619 end 620 ret false 621 end 622 623 fn inf_cp_is_number cp uint to bool 624 if ge cp 48 625 if le cp 57 626 ret true 627 end 628 end 629 ret false 630 end 631 632 fn inf_cp_is_nl cp uint to bool 633 if eq cp 10 634 ret true 635 end 636 if eq cp 13 637 ret true 638 end 639 ret false 640 end 641 642 fn inf_cp_is_ws cp uint to bool 643 if eq cp 32 644 ret true 645 end 646 if eq cp 9 647 ret true 648 end 649 if eq cp 10 650 ret true 651 end 652 if eq cp 13 653 ret true 654 end 655 if eq cp 11 656 ret true 657 end 658 if eq cp 12 659 ret true 660 end 661 if eq cp 0x3000 662 ret true 663 end 664 ret false 665 end 666 667 fn inf_cp_is_sym cp uint to bool 668 if inf_cp_is_ws cp 669 ret false 670 end 671 if inf_cp_is_letter cp 672 ret false 673 end 674 if inf_cp_is_number cp 675 ret false 676 end 677 ret true 678 end 679 680 fn inf_ascii_lower b uchar to uchar 681 if ge b uchar 65 682 if le b uchar 90 683 ret add b uchar 32 684 end 685 end 686 ret b 687 end 688 689 fn inf_run_kind s view uchar * start ulong kind uint to ulong 690 n = len s 691 pos = start 692 while lt pos n bound 4096 693 cp = inf_utf8_decode s pos 694 clen = inf_utf8_char_len get s pos 695 if gt add pos clen n 696 break 697 end 698 ok = false 699 if eq kind 0 700 ok = inf_cp_is_letter cp 701 end 702 if eq kind 1 703 ok = inf_cp_is_sym cp 704 end 705 if eq kind 2 706 ok = inf_cp_is_nl cp 707 end 708 if eq kind 3 709 ok = inf_cp_is_ws cp 710 end 711 if not ok 712 break 713 end 714 pos = add pos clen 715 end 716 ret sub pos start 717 end 718 719 fn inf_pretok_ws s view uchar * pos ulong l0 ulong to ulong 720 ws = inf_run_kind s pos 3 721 if eq ws 0 722 ret l0 723 end 724 last_nl = long -1 725 for k ulong ws bound 4096 726 bv = get s add pos k 727 if eq bv uchar 10 728 last_nl = long k 729 end 730 if eq bv uchar 13 731 last_nl = long k 732 end 733 end 734 if ge last_nl 0 735 ret add ulong last_nl 1 736 end 737 if eq add pos ws len s 738 ret ws 739 end 740 n_chars = ulong 0 741 last_len = ulong 0 742 p = pos 743 wend = add pos ws 744 while lt p wend bound 4096 745 cl = inf_utf8_char_len get s p 746 last_len = cl 747 p = add p cl 748 n_chars = add n_chars 1 749 end 750 if gt n_chars 1 751 ret sub ws last_len 752 end 753 ret ws 754 end 755 756 fn inf_pretokenize_one s view uchar * pos ulong to ulong 757 n = len s 758 rem_n = sub n pos 759 if eq rem_n 0 760 ret 0 761 end 762 c0 = inf_utf8_decode s pos 763 l0 = inf_utf8_char_len get s pos 764 note 1: contractions 765 if eq c0 39 766 if ge rem_n 3 767 a = inf_ascii_lower get s add pos 1 768 b = inf_ascii_lower get s add pos 2 769 if eq a uchar 114 770 if eq b uchar 101 771 ret 3 772 end 773 end 774 if eq a uchar 118 775 if eq b uchar 101 776 ret 3 777 end 778 end 779 if eq a uchar 108 780 if eq b uchar 108 781 ret 3 782 end 783 end 784 end 785 if ge rem_n 2 786 a = inf_ascii_lower get s add pos 1 787 if eq a uchar 115 788 ret 2 789 end 790 if eq a uchar 116 791 ret 2 792 end 793 if eq a uchar 109 794 ret 2 795 end 796 if eq a uchar 100 797 ret 2 798 end 799 end 800 end 801 note 2: optional one char, then letters 802 if inf_cp_is_letter c0 803 ret add l0 inf_run_kind s add pos l0 0 804 end 805 if not inf_cp_is_nl c0 806 if not inf_cp_is_number c0 807 if lt add pos l0 n 808 c1 = inf_utf8_decode s add pos l0 809 if inf_cp_is_letter c1 810 ret add l0 inf_run_kind s add pos l0 0 811 end 812 end 813 end 814 end 815 note 3: 1..3 digits 816 if inf_cp_is_number c0 817 p = pos 818 cnt = ulong 0 819 while lt p n bound 4 820 if eq cnt 3 821 break 822 end 823 cp = inf_utf8_decode s p 824 if not inf_cp_is_number cp 825 break 826 end 827 p = add p inf_utf8_char_len get s p 828 cnt = add cnt 1 829 end 830 ret sub p pos 831 end 832 note 4: optional space, symbols, trailing newlines 833 skip = ulong 0 834 if eq c0 32 835 skip = 1 836 end 837 body = inf_run_kind s add pos skip 1 838 if gt body 0 839 tail = inf_run_kind s add pos add skip body 2 840 ret add skip add body tail 841 end 842 note 5-7: whitespace handling 843 ret inf_pretok_ws s pos l0 844 end 845 846 text inf_key_tok_type "tokenizer.ggml.token_type" 847 text inf_key_tok_merges "tokenizer.ggml.merges" 848 text inf_key_bos "tokenizer.ggml.bos_token_id" 849 text inf_key_eos "tokenizer.ggml.eos_token_id" 850 text inf_tok_eot "<|eot_id|>" 851 text inf_chat_pre "<|begin_of_text|><|start_header_id|>user<|end_header_id|>\n\n" 852 text inf_chat_suf "<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n" 853 854 fn inf_tok_ensure_byte_tbl to void 855 if ne get inf_byte_tbl_init 0 uint 0 856 ret 857 end 858 for b ulong 256 859 put inf_byte_to_cp_tbl b inf_byte_to_cp uchar b 860 end 861 put inf_byte_tbl_init 0 uint 1 862 end 863 864 fn inf_tok_lookup b view uchar * needle view uchar * to long 865 nv = get inf_tok_meta 0 866 nlen = len needle 867 for i ulong nv bound 512 868 if eq get inf_tok_len i nlen 869 toff = get inf_tok_off i 870 eq_f = true 871 for j ulong nlen bound 256 872 if ne get b add toff j get needle j 873 eq_f = false 874 break 875 end 876 end 877 if eq_f 878 ret long i 879 end 880 end 881 end 882 ret long -1 883 end 884 885 fn inf_tok_init_merges g view InfGguf b view uchar * ts edit InfTokState to int 886 nm = ulong 0 887 cur = new ulong 1 0 888 so = new ulong 1 0 889 sl = new ulong 1 0 890 midx = inf_gguf_find_kv g b inf_key_tok_merges 891 if ge midx 0 892 mu = ulong midx 893 if eq get inf_kv_vtype mu INF_VT_ARRAY 894 if eq get inf_kv_aelem mu INF_VT_STR 895 nm = get inf_kv_alen mu 896 if gt nm INF_TOK_MAX_MERGES 897 ret INF_TOK_OVERFLOW 898 end 899 put cur 0 get inf_kv_voff mu 900 for i ulong nm bound 512 901 rc = inf_cur_str b cur so sl 902 if ne rc INF_OK 903 ret INF_TOK_BAD_VOCAB 904 end 905 put inf_mrg_off i get so 0 906 put inf_mrg_len i get sl 0 907 end 908 end 909 end 910 end 911 put inf_tok_meta 1 nm 912 put ts n_merges nm 913 ret INF_OK 914 end 915 916 fn inf_tok_init_extra g view InfGguf b view uchar * ts edit InfTokState to int 917 nv = get ts vocab_size 918 put inf_tok_has_types 0 uint 0 919 ttidx = inf_gguf_find_kv g b inf_key_tok_type 920 if ge ttidx 0 921 ttu = ulong ttidx 922 if eq get inf_kv_vtype ttu INF_VT_ARRAY 923 if eq get inf_kv_aelem ttu INF_VT_I32 924 if eq get inf_kv_alen ttu nv 925 ttoff = get inf_kv_voff ttu 926 for i ulong nv bound 512 927 put inf_tok_type i bitcast int inf_rd_u32_le b add ttoff mul i 4 928 end 929 put inf_tok_has_types 0 uint 1 930 end 931 end 932 end 933 end 934 rc = inf_tok_init_merges g b ts 935 if ne rc INF_OK 936 ret rc 937 end 938 u32_buf = new uint 1 0 939 if eq inf_gguf_u32_key g b inf_key_bos u32_buf INF_OK 940 put ts bos_id get u32_buf 0 941 put ts has_bos uint 1 942 else 943 put ts bos_id uint 0 944 put ts has_bos uint 0 945 end 946 if eq inf_gguf_u32_key g b inf_key_eos u32_buf INF_OK 947 put ts eos_id get u32_buf 0 948 put ts has_eos uint 1 949 else 950 put ts eos_id uint 0 951 put ts has_eos uint 0 952 end 953 eot = inf_tok_lookup b inf_tok_eot 954 if ge eot 0 955 put ts eot_id uint eot 956 put ts has_eot uint 1 957 else 958 put ts eot_id uint 0 959 put ts has_eot uint 0 960 end 961 ret INF_OK 962 end 963 964 fn inf_tok_init g view InfGguf b view uchar * ts edit InfTokState to int 965 inf_tok_ensure_byte_tbl 966 tidx = inf_gguf_find_kv g b inf_key_tokens 967 if lt tidx 0 968 ret INF_TOK_BAD_VOCAB 969 end 970 tu = ulong tidx 971 if ne get inf_kv_vtype tu INF_VT_ARRAY 972 ret INF_TOK_BAD_VOCAB 973 end 974 if ne get inf_kv_aelem tu INF_VT_STR 975 ret INF_TOK_BAD_VOCAB 976 end 977 nv = get inf_kv_alen tu 978 if eq nv 0 979 ret INF_TOK_BAD_VOCAB 980 end 981 if gt nv INF_TOK_MAX_VOCAB 982 ret INF_TOK_OVERFLOW 983 end 984 cur = new ulong 1 get inf_kv_voff tu 985 so = new ulong 1 0 986 sl = new ulong 1 0 987 for i ulong nv bound 512 988 rc = inf_cur_str b cur so sl 989 if ne rc INF_OK 990 ret INF_TOK_BAD_VOCAB 991 end 992 put inf_tok_off i get so 0 993 put inf_tok_len i get sl 0 994 put inf_tok_type i int 1 995 end 996 put inf_tok_meta 0 nv 997 put ts vocab_size nv 998 ret inf_tok_init_extra g b ts 999 end 1000 1001 fn inf_tok_is_special id uint to bool 1002 if eq get inf_tok_has_types 0 uint 0 1003 ret false 1004 end 1005 t = get inf_tok_type ulong id 1006 if eq t INF_TOK_TYPE_CONTROL 1007 ret true 1008 end 1009 if eq t INF_TOK_TYPE_USER_DEF 1010 ret true 1011 end 1012 ret false 1013 end 1014 1015 fn inf_tok_is_control id uint to bool 1016 if eq get inf_tok_has_types 0 uint 0 1017 ret false 1018 end 1019 ret eq get inf_tok_type ulong id INF_TOK_TYPE_CONTROL 1020 end 1021 1022 fn inf_tok_merge_rank b view uchar * pair view uchar * to long 1023 nm = get inf_tok_meta 1 1024 plen = len pair 1025 for i ulong nm bound 512 1026 if eq get inf_mrg_len i plen 1027 moff = get inf_mrg_off i 1028 eq_f = true 1029 for j ulong plen bound 256 1030 if ne get b add moff j get pair j 1031 eq_f = false 1032 break 1033 end 1034 end 1035 if eq_f 1036 ret long i 1037 end 1038 end 1039 end 1040 ret long -1 1041 end 1042 1043 fn inf_tok_emit_id tid uint to int 1044 dpos = get inf_tok_ids_len 0 1045 if ge dpos 512 1046 ret INF_TOK_OVERFLOW 1047 end 1048 put inf_tok_ids dpos tid 1049 put inf_tok_ids_len 0 add dpos 1 1050 ret INF_OK 1051 end 1052 1053 fn inf_tok_get_id i ulong to uint 1054 ret get inf_tok_ids i 1055 end 1056 1057 fn inf_tok_pair_rank b view uchar * word view uchar * idx ulong to long 1058 pbuf = new uchar 256 0 1059 s0 = get inf_bpe_ss idx 1060 l0 = sub get inf_bpe_se idx s0 1061 s1 = get inf_bpe_ss add idx 1 1062 l1 = sub get inf_bpe_se add idx 1 s1 1063 if gt add add l0 1 l1 256 1064 ret long -1 1065 end 1066 for k ulong l0 bound 256 1067 put pbuf k get word add s0 k 1068 end 1069 put pbuf l0 uchar 32 1070 for k ulong l1 bound 256 1071 put pbuf add add l0 1 k get word add s1 k 1072 end 1073 ret inf_tok_merge_rank b part pbuf 0 add add l0 1 l1 1074 end 1075 1076 fn inf_tok_best_merge b view uchar * word view uchar * nsym ulong to long 1077 best_r = long -1 1078 best_i = long -1 1079 lim = sub nsym 1 1080 for i ulong lim bound 256 1081 rk = inf_tok_pair_rank b word i 1082 if ge rk 0 1083 if lt best_r 0 1084 best_r = rk 1085 best_i = long i 1086 else 1087 if lt rk best_r 1088 best_r = rk 1089 best_i = long i 1090 end 1091 end 1092 end 1093 end 1094 ret best_i 1095 end 1096 1097 fn inf_tok_emit_syms b view uchar * word view uchar * nsym ulong to int 1098 for i ulong nsym bound 256 1099 s0 = get inf_bpe_ss i 1100 e0 = get inf_bpe_se i 1101 tid = inf_tok_lookup b part word s0 sub e0 s0 1102 if lt tid 0 1103 ret INF_TOK_MISSING_SYM 1104 end 1105 rc = inf_tok_emit_id uint tid 1106 if ne rc INF_OK 1107 ret rc 1108 end 1109 end 1110 ret INF_OK 1111 end 1112 1113 fn inf_bpe_shift u ulong nsym ulong to void 1114 put inf_bpe_se u get inf_bpe_se add u 1 1115 k = add u 1 1116 while lt add k 1 nsym bound 256 1117 put inf_bpe_ss k get inf_bpe_ss add k 1 1118 put inf_bpe_se k get inf_bpe_se add k 1 1119 k = add k 1 1120 end 1121 end 1122 1123 fn inf_tok_bpe_word b view uchar * word view uchar * to int 1124 wlen = len word 1125 whole = inf_tok_lookup b word 1126 if ge whole 0 1127 ret inf_tok_emit_id uint whole 1128 end 1129 nsym = ulong 0 1130 p = ulong 0 1131 while lt p wlen bound 256 1132 cl = inf_utf8_char_len get word p 1133 if gt add p cl wlen 1134 cl = sub wlen p 1135 end 1136 put inf_bpe_ss nsym p 1137 put inf_bpe_se nsym add p cl 1138 nsym = add nsym 1 1139 p = add p cl 1140 end 1141 while gt nsym 1 bound 256 1142 bi = inf_tok_best_merge b word nsym 1143 if lt bi 0 1144 break 1145 end 1146 inf_bpe_shift ulong bi nsym 1147 nsym = sub nsym 1 1148 end 1149 ret inf_tok_emit_syms b word nsym 1150 end 1151 1152 fn inf_tok_encode_plain b view uchar * s view uchar * to int 1153 slen = len s 1154 pos = ulong 0 1155 mapped = new uchar 512 0 1156 while lt pos slen bound 4096 1157 wlen = inf_pretokenize_one s pos 1158 if eq wlen 0 1159 break 1160 end 1161 mlen = ulong 0 1162 for k ulong wlen bound 256 1163 bv = get s add pos k 1164 cp = get inf_byte_to_cp_tbl ulong bv 1165 mlen = add mlen inf_utf8_encode cp mapped mlen 1166 end 1167 rc = inf_tok_bpe_word b part mapped 0 mlen 1168 if ne rc INF_OK 1169 ret rc 1170 end 1171 pos = add pos wlen 1172 end 1173 ret INF_OK 1174 end 1175 1176 fn inf_tok_match_special b view uchar * s view uchar * i ulong hit edit ulong 3 to bool 1177 end_lim = add i INF_TOK_MAX_SPECIAL 1178 if gt end_lim len s 1179 end_lim = len s 1180 end 1181 j = add i 2 1182 while lt add j 1 end_lim bound 64 1183 if eq get s j uchar 124 1184 if eq get s add j 1 uchar 62 1185 clen = sub add j 2 i 1186 tid = inf_tok_lookup b part s i clen 1187 if ge tid 0 1188 if inf_tok_is_special uint tid 1189 put hit 0 i 1190 put hit 1 clen 1191 put hit 2 ulong tid 1192 ret true 1193 end 1194 end 1195 ret false 1196 end 1197 end 1198 j = add j 1 1199 end 1200 ret false 1201 end 1202 1203 fn inf_tok_find_special b view uchar * s view uchar * pos ulong hit edit ulong 3 to bool 1204 n = len s 1205 i = pos 1206 while lt add i 1 n bound 4096 1207 if eq get s i uchar 60 1208 if eq get s add i 1 uchar 124 1209 if inf_tok_match_special b s i hit 1210 ret true 1211 end 1212 end 1213 end 1214 i = add i 1 1215 end 1216 ret false 1217 end 1218 1219 fn inf_tok_encode b view uchar * s view uchar * parse_special bool to long 1220 put inf_tok_ids_len 0 ulong 0 1221 hit = new ulong 3 0 1222 pos = ulong 0 1223 slen = len s 1224 while lt pos slen bound 512 1225 found = false 1226 if parse_special 1227 found = inf_tok_find_special b s pos hit 1228 end 1229 if found 1230 hat = get hit 0 1231 clen = get hit 1 1232 tid = get hit 2 1233 if gt hat pos 1234 rc = inf_tok_encode_plain b part s pos sub hat pos 1235 if ne rc INF_OK 1236 ret long rc 1237 end 1238 end 1239 rc = inf_tok_emit_id uint tid 1240 if ne rc INF_OK 1241 ret long rc 1242 end 1243 pos = add hat clen 1244 else 1245 rc = inf_tok_encode_plain b part s pos sub slen pos 1246 if ne rc INF_OK 1247 ret long rc 1248 end 1249 pos = slen 1250 end 1251 end 1252 ret long get inf_tok_ids_len 0 1253 end 1254 1255 fn inf_tok_encode_chat b view uchar * user view uchar * to long 1256 buf = new uchar 512 0 1257 p = ulong 0 1258 pre_n = len inf_chat_pre 1259 suf_n = len inf_chat_suf 1260 un = len user 1261 if gt add add pre_n un suf_n 512 1262 ret long INF_TOK_OVERFLOW 1263 end 1264 for i ulong pre_n bound 128 1265 put buf p get inf_chat_pre i 1266 p = add p 1 1267 end 1268 for i ulong un bound 384 1269 put buf p get user i 1270 p = add p 1 1271 end 1272 for i ulong suf_n bound 128 1273 put buf p get inf_chat_suf i 1274 p = add p 1 1275 end 1276 ret inf_tok_encode b part buf 0 p true 1277 end 1278 1279 fn inf_tok_copy_slice src view uchar * dst edit uchar * outp ulong to void 1280 n = len src 1281 for k ulong n bound 4 1282 put dst add outp k get src k 1283 end 1284 end 1285 1286 fn inf_tok_decode_token b view uchar * id uint dst edit uchar * dpos edit ulong 1 to int 1287 if ge ulong id get inf_tok_meta 0 1288 ret INF_TOK_BAD_ID 1289 end 1290 if inf_tok_is_control id 1291 ret INF_OK 1292 end 1293 toff = get inf_tok_off ulong id 1294 tlen = get inf_tok_len ulong id 1295 outp = get dpos 0 1296 i = ulong 0 1297 while lt i tlen bound 256 1298 cl = inf_utf8_char_len get b add toff i 1299 if gt add i cl tlen 1300 cl = sub tlen i 1301 end 1302 mb = inf_cp_to_byte inf_utf8_decode b add toff i 1303 if ge mb 0 1304 if ge outp len dst 1305 ret INF_TOK_OVERFLOW 1306 end 1307 put dst outp uchar mb 1308 outp = add outp 1 1309 else 1310 if gt add outp cl len dst 1311 ret INF_TOK_OVERFLOW 1312 end 1313 inf_tok_copy_slice part b add toff i cl dst outp 1314 outp = add outp cl 1315 end 1316 i = add i cl 1317 end 1318 put dpos 0 outp 1319 ret INF_OK 1320 end 1321 1322 fn inf_tok_decode b view uchar * ids view uint * dst edit uchar * to long 1323 dpos = new ulong 1 0 1324 n = len ids 1325 for i ulong n bound 512 1326 rc = inf_tok_decode_token b get ids i dst dpos 1327 if ne rc INF_OK 1328 ret long rc 1329 end 1330 end 1331 ret long get dpos 0 1332 end 1333 1334 note ======================================================================== 1335 note Forward pass, KV cache and greedy decode (decode.rs) 1336 note ======================================================================== 1337 1338 fn inf_decode_init m view InfModel cap ulong st edit InfDecodeState to int 1339 n_embd = ulong get m embedding_length 1340 n_ff = ulong get m feed_forward_length 1341 vocab = ulong get m vocab_size 1342 n_lyr = ulong get m block_count 1343 kv_dim = get m kv_dim 1344 if gt n_embd INF_DEC_MAX_DIM 1345 ret INF_ERR_CAPACITY 1346 end 1347 if gt n_ff INF_DEC_MAX_DIM 1348 ret INF_ERR_CAPACITY 1349 end 1350 if gt vocab INF_DEC_MAX_VOCAB 1351 ret INF_ERR_CAPACITY 1352 end 1353 if gt cap INF_DEC_MAX_POS 1354 ret INF_ERR_CAPACITY 1355 end 1356 if gt mul mul n_lyr cap kv_dim INF_DEC_MAX_KV 1357 ret INF_ERR_CAPACITY 1358 end 1359 put st capacity cap 1360 put st pos ulong 0 1361 put st kv_dim kv_dim 1362 put st emulate_q8k uint 0 1363 if inf_cpu_dotprod 1364 put st use_dotprod uint 1 1365 else 1366 put st use_dotprod uint 0 1367 end 1368 ret INF_OK 1369 end 1370 1371 fn inf_decode_reset st edit InfDecodeState to void 1372 put st pos ulong 0 1373 end 1374 1375 fn inf_dec_rms_norm_x_to_h norm_off ulong n ulong eps f32 to void 1376 ss = f32 0.0f32 1377 for i ulong n bound 1024 1378 v = get inf_st_x i 1379 ss = add ss mul v v 1380 end 1381 ss = div ss f32 n 1382 scale = div 1.0f32 inf_sqrt add ss eps 1383 for i ulong n bound 1024 1384 put inf_st_h i mul mul get inf_st_x i scale get inf_norms add norm_off i 1385 end 1386 end 1387 1388 fn inf_dec_prep st view InfDecodeState which uint n ulong to int 1389 use_dp = ne get st use_dotprod 0 1390 use_em = ne get st emulate_q8k 0 1391 if not use_dp 1392 if not use_em 1393 ret INF_OK 1394 end 1395 end 1396 xtmp = new f32 1024 0.0f32 1397 for i ulong n bound 1024 1398 if eq which 0 1399 put xtmp i get inf_st_h i 1400 else 1401 if eq which 1 1402 put xtmp i get inf_st_att i 1403 else 1404 put xtmp i get inf_st_gate i 1405 end 1406 end 1407 end 1408 if use_dp 1409 ret inf_q8_quantize part xtmp 0 n n 1410 end 1411 inf_q8k_round part xtmp 0 n n 1412 for i ulong n bound 1024 1413 if eq which 0 1414 put inf_st_h i get xtmp i 1415 else 1416 if eq which 1 1417 put inf_st_att i get xtmp i 1418 else 1419 put inf_st_gate i get xtmp i 1420 end 1421 end 1422 end 1423 ret INF_OK 1424 end 1425 1426 fn inf_dec_store_dst dst_id uint r ulong v f32 to void 1427 if eq dst_id 0 1428 put inf_st_q r v 1429 end 1430 if eq dst_id 1 1431 put inf_st_kk r v 1432 end 1433 if eq dst_id 2 1434 put inf_st_vv r v 1435 end 1436 if eq dst_id 3 1437 put inf_st_tmp r v 1438 end 1439 if eq dst_id 4 1440 put inf_st_gate r v 1441 end 1442 if eq dst_id 5 1443 put inf_st_up r v 1444 end 1445 if eq dst_id 6 1446 put inf_st_logits r v 1447 end 1448 end 1449 1450 fn inf_dec_mv_q8 widx ulong b view uchar * dst_id uint to int 1451 rows = get inf_w_rows widx 1452 ty = get inf_w_ty widx 1453 rbytes = get inf_w_rbytes widx 1454 base = get inf_w_off widx 1455 res = new f32 1 0.0f32 1456 for r ulong rows bound 1024 1457 rc = inf_dot_row_q8 ty part b add base mul r rbytes rbytes res 1458 if ne rc INF_OK 1459 ret rc 1460 end 1461 inf_dec_store_dst dst_id r get res 0 1462 end 1463 ret INF_OK 1464 end 1465 1466 fn inf_dec_mv_f32 widx ulong b view uchar * src_id uint dst_id uint to int 1467 rows = get inf_w_rows widx 1468 cols = get inf_w_cols widx 1469 res = new f32 1 0.0f32 1470 wr = new ulong 2 0 1471 xtmp = new f32 1024 0.0f32 1472 for c ulong cols bound 1024 1473 if eq src_id 0 1474 put xtmp c get inf_st_h c 1475 end 1476 if eq src_id 1 1477 put xtmp c get inf_st_att c 1478 end 1479 if eq src_id 2 1480 put xtmp c get inf_st_gate c 1481 end 1482 end 1483 put wr 0 widx 1484 for r ulong rows bound 1024 1485 put wr 1 r 1486 rc = inf_weight_matvec_row wr b part xtmp 0 cols res 1487 if ne rc INF_OK 1488 ret rc 1489 end 1490 inf_dec_store_dst dst_id r get res 0 1491 end 1492 ret INF_OK 1493 end 1494 1495 fn inf_dec_mv st view InfDecodeState widx ulong b view uchar * src_id uint dst_id uint to int 1496 if ne get st use_dotprod 0 1497 if inf_weight_is_kquant widx 1498 ret inf_dec_mv_q8 widx b dst_id 1499 end 1500 end 1501 ret inf_dec_mv_f32 widx b src_id dst_id 1502 end 1503 1504 fn inf_dec_rope_init m view InfModel pos ulong to void 1505 half = div get m head_dim 2 1506 theta_scale = inf_powf get m rope_freq_base div -2.0f32 f32 get m head_dim 1507 theta = f32 pos 1508 sc = new f32 2 0.0f32 1509 for i ulong half bound 512 1510 if ne get m has_rope_fac 0 1511 inf_sin_cos div theta get inf_norms add get m rope_fac_off i sc 1512 else 1513 inf_sin_cos theta sc 1514 end 1515 put inf_st_rope mul i 2 get sc 1 1516 put inf_st_rope add mul i 2 1 get sc 0 1517 theta = mul theta theta_scale 1518 end 1519 end 1520 1521 fn inf_dec_rope_head which uint base ulong half ulong to void 1522 for i ulong half bound 512 1523 idx0 = add base mul i 2 1524 c = get inf_st_rope mul i 2 1525 s = get inf_st_rope add mul i 2 1 1526 if eq which 0 1527 a = get inf_st_q idx0 1528 b = get inf_st_q add idx0 1 1529 put inf_st_q idx0 sub mul a c mul b s 1530 put inf_st_q add idx0 1 add mul a s mul b c 1531 else 1532 a = get inf_st_kk idx0 1533 b = get inf_st_kk add idx0 1 1534 put inf_st_kk idx0 sub mul a c mul b s 1535 put inf_st_kk add idx0 1 add mul a s mul b c 1536 end 1537 end 1538 end 1539 1540 fn inf_dec_rope_apply_buf which uint n ulong hd ulong to void 1541 n_heads = div n hd 1542 half = div hd 2 1543 for h ulong n_heads bound 512 1544 inf_dec_rope_head which mul h hd half 1545 end 1546 end 1547 1548 fn inf_dec_gqa_dot qb ulong kb ulong hd ulong to f32 1549 dot = f32 0.0f32 1550 for d ulong hd bound 512 1551 dot = add dot mul get inf_st_q add qb d get inf_kv_k add kb d 1552 end 1553 ret dot 1554 end 1555 1556 fn inf_dec_gqa_acc qb ulong vb ulong hd ulong wt f32 to void 1557 for d ulong hd bound 512 1558 cur_a = get inf_st_att add qb d 1559 put inf_st_att add qb d add cur_a mul wt get inf_kv_v add vb d 1560 end 1561 end 1562 1563 fn inf_dec_gqa_softmax npos ulong to void 1564 mx = get inf_st_scores 0 1565 for t ulong npos bound 256 1566 sv = get inf_st_scores t 1567 if gt sv mx 1568 mx = sv 1569 end 1570 end 1571 sm = f32 0.0f32 1572 for t ulong npos bound 256 1573 ev = inf_exp sub get inf_st_scores t mx 1574 put inf_st_scores t ev 1575 sm = add sm ev 1576 end 1577 for t ulong npos bound 256 1578 put inf_st_scores t div get inf_st_scores t sm 1579 end 1580 end 1581 1582 fn inf_dec_gqa_head base ulong npos ulong kv_dim ulong qb ulong kvh ulong hd ulong to void 1583 scale = div 1.0f32 inf_sqrt f32 hd 1584 for t ulong npos bound 256 1585 kb = add add base mul t kv_dim kvh 1586 put inf_st_scores t mul inf_dec_gqa_dot qb kb hd scale 1587 end 1588 inf_dec_gqa_softmax npos 1589 for d ulong hd bound 512 1590 put inf_st_att add qb d 0.0f32 1591 end 1592 for t ulong npos bound 256 1593 vb = add add base mul t kv_dim kvh 1594 inf_dec_gqa_acc qb vb hd get inf_st_scores t 1595 end 1596 end 1597 1598 fn inf_dec_gqa_attn m view InfModel st view InfDecodeState lyr ulong to void 1599 kv_dim = get m kv_dim 1600 hd = get m head_dim 1601 n_head = ulong get m head_count 1602 group = div n_head ulong get m head_count_kv 1603 base = mul mul lyr get st capacity kv_dim 1604 kvo = add base mul get st pos kv_dim 1605 for i ulong kv_dim bound 1024 1606 put inf_kv_k add kvo i get inf_st_kk i 1607 put inf_kv_v add kvo i get inf_st_vv i 1608 end 1609 npos = add get st pos 1 1610 for h ulong n_head bound 512 1611 inf_dec_gqa_head base npos kv_dim mul h hd mul div h group hd hd 1612 end 1613 end 1614 1615 fn inf_dec_forward_ffn m view InfModel b view uchar * st view InfDecodeState lyr ulong to int 1616 n_embd = ulong get m embedding_length 1617 n_ff = ulong get m feed_forward_length 1618 inf_dec_rms_norm_x_to_h get inf_lyr_ffn_norm lyr n_embd get m rms_norm_eps 1619 rc = inf_dec_prep st 0 n_embd 1620 if ne rc INF_OK 1621 ret rc 1622 end 1623 rc = inf_dec_mv st get inf_lyr_wgate lyr b 0 4 1624 if ne rc INF_OK 1625 ret rc 1626 end 1627 rc = inf_dec_mv st get inf_lyr_wup lyr b 0 5 1628 if ne rc INF_OK 1629 ret rc 1630 end 1631 for i ulong n_ff bound 1024 1632 gv = get inf_st_gate i 1633 put inf_st_gate i mul inf_silu gv get inf_st_up i 1634 end 1635 rc = inf_dec_prep st 2 n_ff 1636 if ne rc INF_OK 1637 ret rc 1638 end 1639 rc = inf_dec_mv st get inf_lyr_wdown lyr b 2 3 1640 if ne rc INF_OK 1641 ret rc 1642 end 1643 for i ulong n_embd bound 1024 1644 put inf_st_x i add get inf_st_x i get inf_st_tmp i 1645 end 1646 ret INF_OK 1647 end 1648 1649 fn inf_dec_forward_layer m view InfModel b view uchar * st view InfDecodeState lyr ulong to int 1650 n_embd = ulong get m embedding_length 1651 hd = get m head_dim 1652 inf_dec_rms_norm_x_to_h get inf_lyr_attn_norm lyr n_embd get m rms_norm_eps 1653 rc = inf_dec_prep st 0 n_embd 1654 if ne rc INF_OK 1655 ret rc 1656 end 1657 rc = inf_dec_mv st get inf_lyr_wq lyr b 0 0 1658 if ne rc INF_OK 1659 ret rc 1660 end 1661 rc = inf_dec_mv st get inf_lyr_wk lyr b 0 1 1662 if ne rc INF_OK 1663 ret rc 1664 end 1665 rc = inf_dec_mv st get inf_lyr_wv lyr b 0 2 1666 if ne rc INF_OK 1667 ret rc 1668 end 1669 inf_dec_rope_apply_buf 0 n_embd hd 1670 inf_dec_rope_apply_buf 1 get m kv_dim hd 1671 inf_dec_gqa_attn m st lyr 1672 rc = inf_dec_prep st 1 n_embd 1673 if ne rc INF_OK 1674 ret rc 1675 end 1676 rc = inf_dec_mv st get inf_lyr_wo lyr b 1 3 1677 if ne rc INF_OK 1678 ret rc 1679 end 1680 for i ulong n_embd bound 1024 1681 put inf_st_x i add get inf_st_x i get inf_st_tmp i 1682 end 1683 ret inf_dec_forward_ffn m b st lyr 1684 end 1685 1686 fn inf_decode_forward m view InfModel b view uchar * st edit InfDecodeState token uint want_logits bool to int 1687 if ge token get m vocab_size 1688 ret INF_ERR_BAD_TOKEN 1689 end 1690 pos = get st pos 1691 if ge pos get st capacity 1692 ret INF_ERR_CONTEXT_FULL 1693 end 1694 n_embd = ulong get m embedding_length 1695 xtmp = new f32 1024 0.0f32 1696 rc = inf_weight_row_into get m tok_embd_w b ulong token part xtmp 0 n_embd 1697 if ne rc INF_OK 1698 ret rc 1699 end 1700 for i ulong n_embd bound 1024 1701 put inf_st_x i get xtmp i 1702 end 1703 inf_dec_rope_init m pos 1704 n_lyr = ulong get m block_count 1705 for l ulong n_lyr bound 8 1706 rc = inf_dec_forward_layer m b st l 1707 if ne rc INF_OK 1708 ret rc 1709 end 1710 end 1711 put st pos add pos 1 1712 if want_logits 1713 inf_dec_rms_norm_x_to_h get m output_norm_off n_embd get m rms_norm_eps 1714 rc = inf_dec_prep st 0 n_embd 1715 if ne rc INF_OK 1716 ret rc 1717 end 1718 rc = inf_dec_mv st get m output_w b 0 6 1719 if ne rc INF_OK 1720 ret rc 1721 end 1722 end 1723 ret INF_OK 1724 end 1725 1726 fn inf_decode_prefill m view InfModel b view uchar * st edit InfDecodeState ids view uint * to int 1727 n = len ids 1728 for i ulong n bound 256 1729 wl = eq add i 1 n 1730 rc = inf_decode_forward m b st get ids i wl 1731 if ne rc INF_OK 1732 ret rc 1733 end 1734 end 1735 ret INF_OK 1736 end 1737 1738 fn inf_decode_argmax m view InfModel to uint 1739 nv = ulong get m vocab_size 1740 best = ulong 0 1741 if eq nv 0 1742 ret 0 1743 end 1744 bv = get inf_st_logits 0 1745 for i ulong nv bound 512 1746 v = get inf_st_logits i 1747 if gt v bv 1748 bv = v 1749 best = i 1750 end 1751 end 1752 ret uint best 1753 end 1754 1755 fn inf_gen_get_id i ulong to uint 1756 ret get inf_gen_ids i 1757 end 1758 1759 fn inf_generate_greedy m view InfModel b view uchar * st edit InfDecodeState max_new ulong stop_id uint to long 1760 cnt = ulong 0 1761 for i ulong max_new bound 256 1762 t = inf_decode_argmax m 1763 put inf_gen_ids cnt t 1764 cnt = add cnt 1 1765 if eq t stop_id 1766 break 1767 end 1768 if eq add i 1 max_new 1769 break 1770 end 1771 rc = inf_decode_forward m b st t true 1772 if ne rc INF_OK 1773 ret long rc 1774 end 1775 end 1776 ret long cnt 1777 end