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_core.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_core.om` 1819 lines ยท 37420 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note aienux/crates/aienos-infer/infer_core.om: Pure-Omega core inference library (half, math, quant, dotprod, gguf). 3 note Ported from aienux crates/aienos-infer/src/{half,math,quant,dotprod,gguf}.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 Common status and error codes 7 const INF_OK int 0 8 9 note GGUF error codes 10 const INF_GGUF_TRUNCATED int -1 11 const INF_GGUF_BAD_MAGIC int -2 12 const INF_GGUF_BAD_VERSION int -3 13 const INF_GGUF_TOO_MANY int -4 14 const INF_GGUF_BAD_UTF8 int -5 15 const INF_GGUF_BAD_VTYPE int -6 16 const INF_GGUF_BAD_ALIGN int -7 17 const INF_GGUF_OVERFLOW int -8 18 const INF_GGUF_BAD_TYPE int -9 19 const INF_GGUF_BAD_DIMS int -10 20 const INF_GGUF_TENSOR_OOB int -11 21 const INF_GGUF_MISSING int -12 22 const INF_GGUF_WRONG_TYPE int -13 23 const INF_GGUF_CAPACITY int -14 24 25 note Quantization error codes 26 const INF_QUANT_UNSUPPORTED int -20 27 const INF_QUANT_BAD_LEN int -21 28 29 note Model and decode error codes 30 const INF_ERR_MISSING_TENSOR int -30 31 const INF_ERR_BAD_TENSOR int -31 32 const INF_ERR_BAD_PARAMS int -32 33 const INF_ERR_BAD_TOKEN int -33 34 const INF_ERR_CONTEXT_FULL int -34 35 const INF_ERR_CAPACITY int -35 36 37 note Tokenizer error codes 38 const INF_TOK_BAD_VOCAB int -40 39 const INF_TOK_MISSING_SYM int -41 40 const INF_TOK_BAD_ID int -42 41 const INF_TOK_OVERFLOW int -43 42 43 note GGUF metadata value types 44 const INF_VT_U8 uint 0 45 const INF_VT_I8 uint 1 46 const INF_VT_U16 uint 2 47 const INF_VT_I16 uint 3 48 const INF_VT_U32 uint 4 49 const INF_VT_I32 uint 5 50 const INF_VT_F32 uint 6 51 const INF_VT_BOOL uint 7 52 const INF_VT_STR uint 8 53 const INF_VT_ARRAY uint 9 54 const INF_VT_U64 uint 10 55 const INF_VT_I64 uint 11 56 const INF_VT_F64 uint 12 57 58 note GGML tensor storage types 59 const INF_GGML_F32 uint 0 60 const INF_GGML_F16 uint 1 61 const INF_GGML_Q4_0 uint 2 62 const INF_GGML_Q4_1 uint 3 63 const INF_GGML_Q5_0 uint 6 64 const INF_GGML_Q5_1 uint 7 65 const INF_GGML_Q8_0 uint 8 66 const INF_GGML_Q8_1 uint 9 67 const INF_GGML_Q2K uint 10 68 const INF_GGML_Q3K uint 11 69 const INF_GGML_Q4K uint 12 70 const INF_GGML_Q5K uint 13 71 const INF_GGML_Q6K uint 14 72 const INF_GGML_Q8K uint 15 73 const INF_GGML_I8 uint 24 74 const INF_GGML_I16 uint 25 75 const INF_GGML_I32 uint 26 76 const INF_GGML_I64 uint 27 77 const INF_GGML_F64 uint 28 78 const INF_GGML_BF16 uint 30 79 80 note Quantization block sizes 81 const INF_QK_K ulong 256 82 const INF_Q4K_BYTES ulong 144 83 const INF_Q6K_BYTES ulong 210 84 const INF_Q8_0_QK ulong 32 85 const INF_Q8_0_BYTES ulong 34 86 87 note Capacity constants 88 const INF_MAX_KV ulong 256 89 const INF_MAX_TENSORS ulong 256 90 const INF_Q8_MAX_BLKS ulong 64 91 const INF_Q8_MAX_QS ulong 16384 92 const INF_Q8_MAX_BSUMS ulong 1024 93 94 note Math constants (exact IEEE-754 f32 matching math.rs) 95 const INF_PI f32 3.`14159265` 96 const INF_TAU f32 6.`28318530` 97 const INF_FRAC_PI_2 f32 1.`57079632` 98 const INF_LN_2 f32 0.`69314718` 99 const INF_LOG2_E f32 1.`44269504` 100 const INF_SQRT_2 f32 1.`41421356` 101 const INF_LN_2_HI f32 0.`693359375` 102 const INF_LN_2_LO_ABS f32 0.`00021219444` 103 104 note All struct declarations (must precede the first fn in the composed unit) 105 106 struct InfGguf version uint n_kv ulong n_tensors ulong alignment ulong data_start ulong file_len ulong end 107 108 struct InfLlamaParams block_count uint embedding_length uint head_count uint head_count_kv uint feed_forward_length uint context_length uint rope_freq_base f32 rms_norm_eps f32 vocab_size uint end 109 110 struct InfModel block_count uint embedding_length uint head_count uint head_count_kv uint feed_forward_length uint context_length uint rope_freq_base f32 rms_norm_eps f32 vocab_size uint head_dim ulong kv_dim ulong tok_embd_w ulong output_w ulong output_norm_off ulong rope_fac_off ulong has_rope_fac uint end 111 112 struct InfDecodeState capacity ulong pos ulong kv_dim ulong emulate_q8k uint use_dotprod uint end 113 114 struct InfTokState vocab_size ulong n_merges ulong bos_id uint has_bos uint eos_id uint has_eos uint eot_id uint has_eot uint end 115 116 note Core global tables for Q8_K activations and GGUF parsed metadata/tensors 117 118 global inf_cpu_dp_flag uint 1 119 global inf_q8_qs byte 16384 120 global inf_q8_d f32 64 121 global inf_q8_bsums short 1024 122 global inf_q8_nblk ulong 1 123 124 note GGUF KV table (parallel arrays up to INF_MAX_KV = 256) 125 global inf_kv_koff ulong 256 126 global inf_kv_klen ulong 256 127 global inf_kv_vtype uint 256 128 global inf_kv_u64 ulong 256 129 global inf_kv_f32 f32 256 130 global inf_kv_voff ulong 256 131 global inf_kv_vlen ulong 256 132 global inf_kv_aelem uint 256 133 global inf_kv_alen ulong 256 134 135 note GGUF tensor table (parallel arrays up to INF_MAX_TENSORS = 256) 136 global inf_ten_noff ulong 256 137 global inf_ten_nlen ulong 256 138 global inf_ten_ndims uint 256 139 global inf_ten_d0 ulong 256 140 global inf_ten_d1 ulong 256 141 global inf_ten_d2 ulong 256 142 global inf_ten_d3 ulong 256 143 global inf_ten_ty uint 256 144 global inf_ten_off ulong 256 145 global inf_ten_nelem ulong 256 146 global inf_ten_nbytes ulong 256 147 148 note ======================================================================== 149 note Little-endian byte readers & IEEE-754 binary16 / bfloat16 (half.rs) 150 note ======================================================================== 151 152 fn inf_rd_u16_le b view uchar * off ulong to ushort 153 b0 = ushort get b off 154 b1 = ushort get b add off 1 155 ret or b0 shl b1 8 156 end 157 158 fn inf_rd_u32_le b view uchar * off ulong to uint 159 b0 = uint get b off 160 b1 = uint get b add off 1 161 b2 = uint get b add off 2 162 b3 = uint get b add off 3 163 r = or b0 shl b1 8 164 r = or r shl b2 16 165 ret or r shl b3 24 166 end 167 168 fn inf_rd_u64_le b view uchar * off ulong to ulong 169 r = ulong 0 170 for i ulong 8 171 v = ulong get b add off i 172 r = or r shl v mul i 8 173 end 174 ret r 175 end 176 177 fn inf_rd_f32_le b view uchar * off ulong to f32 178 u = inf_rd_u32_le b off 179 ret bitcast f32 u 180 end 181 182 fn inf_i8_from_u8 v uchar to int 183 if ge v uchar 128 184 ret sub int v 256 185 end 186 ret int v 187 end 188 189 fn inf_f16_to_f32 h ushort to f32 190 hu = uint h 191 sign = shl shr hu 15 31 192 exp = and shr hu 10 0x1f 193 man = and hu 0x3ff 194 bits = uint 0 195 if eq exp 0 196 if eq man 0 197 bits = sign 198 else 199 note Subnormal: normalize man (1..1023) 200 m = man 201 e = uint 113 202 while eq and m 0x400 0 bound 16 203 m = shl m 1 204 e = sub e 1 205 end 206 m = and m 0x3ff 207 bits = or sign or shl e 23 shl m 13 208 end 209 else 210 if eq exp 0x1f 211 bits = or sign or 0x7f800000 shl man 13 212 else 213 e2 = add exp 112 214 bits = or sign or shl e2 23 shl man 13 215 end 216 end 217 ret bitcast f32 bits 218 end 219 220 fn inf_bf16_to_f32 b ushort to f32 221 u = shl uint b 16 222 ret bitcast f32 u 223 end 224 225 fn inf_f32_to_f16 v f32 to ushort 226 b = bitcast uint v 227 sign = ushort and shr b 16 0x8000 228 if eq v 0.0f32 229 ret sign 230 end 231 exp = sub int and shr b 23 0xff 112 232 man = and b 0x7fffff 233 if le exp 0 234 ret sign 235 end 236 if ge exp 31 237 ret or sign ushort 0x7c00 238 end 239 h = or shl uint exp 10 shr man 13 240 if ne and man 0x1000 0 241 h = add h 1 242 end 243 ret or sign ushort and h 0xffff 244 end 245 246 fn inf_rd_f16_le b view uchar * off ulong to f32 247 ret inf_f16_to_f32 inf_rd_u16_le b off 248 end 249 250 fn inf_rd_bf16_le b view uchar * off ulong to f32 251 ret inf_bf16_to_f32 inf_rd_u16_le b off 252 end 253 254 note ======================================================================== 255 note Scalar f32 math without libm (math.rs) + NN primitives 256 note ======================================================================== 257 258 fn inf_sqrt x f32 to f32 259 if le x 0.0f32 260 ret 0.0f32 261 end 262 bits = bitcast uint x 263 seed = add shr bits 1 0x1fc00000 264 y = bitcast f32 seed 265 for i ulong 5 266 y = mul 0.5f32 add y div x y 267 end 268 ret y 269 end 270 271 fn inf_exp x f32 to f32 272 xc = fmax -87.0f32 fmin x 88.0f32 273 scaled = mul xc INF_LOG2_E 274 n = int 0 275 if ge scaled 0.0f32 276 n = int add scaled 0.5f32 277 else 278 n = int sub scaled 0.5f32 279 end 280 nf = f32 n 281 r = add sub xc mul nf INF_LN_2_HI mul nf INF_LN_2_LO_ABS 282 p = add 0.`0083333338f32` div r 720.0f32 283 p = add 0.`041666668f32` mul r p 284 p = add 0.`16666667f32` mul r p 285 p = add 0.5f32 mul r p 286 p = add 1.0f32 mul r p 287 p = add 1.0f32 mul r p 288 e_bits = shl uint add n 127 23 289 ret mul p bitcast f32 e_bits 290 end 291 292 fn inf_ln x f32 to f32 293 bits = bitcast uint x 294 e = sub int and shr bits 23 0xff 127 295 m = bitcast f32 or and bits 0x7fffff shl uint 127 23 296 if gt m INF_SQRT_2 297 m = mul m 0.5f32 298 e = add e 1 299 end 300 y = div sub m 1.0f32 add m 1.0f32 301 y2 = mul y y 302 s = add 0.`11111111f32` div y2 11.0f32 303 s = add 0.`14285715f32` mul y2 s 304 s = add 0.2f32 mul y2 s 305 s = add 0.`33333334f32` mul y2 s 306 s = mul y add 1.0f32 mul y2 s 307 ret add mul 2.0f32 s mul f32 e INF_LN_2 308 end 309 310 fn inf_powf base f32 e f32 to f32 311 ret inf_exp mul e inf_ln base 312 end 313 314 fn inf_sin_cos angle f32 dst edit f32 2 to void 315 q = div add angle INF_PI INF_TAU 316 kf = f32 int q 317 if lt q kf 318 kf = sub kf 1.0f32 319 end 320 r = sub add angle INF_PI mul kf INF_TAU 321 if lt r 0.0f32 322 r = add r INF_TAU 323 end 324 if ge r INF_TAU 325 r = sub r INF_TAU 326 end 327 x = sub r INF_PI 328 csign = f32 1.0f32 329 if gt x INF_FRAC_PI_2 330 x = sub INF_PI x 331 csign = -1.0f32 332 else 333 if lt x neg INF_FRAC_PI_2 334 x = sub neg INF_PI x 335 csign = -1.0f32 336 end 337 end 338 x2 = mul x x 339 sn = add -0.`0001984127f32` mul x2 0.`0000027557319f32` 340 sn = add 0.`0083333338f32` mul x2 sn 341 sn = add -0.`16666667f32` mul x2 sn 342 sn = mul x add 1.0f32 mul x2 sn 343 cn = sub 0.`000024801587f32` div x2 `3628800`.0f32 344 cn = add -0.`0013888889f32` mul x2 cn 345 cn = add 0.`041666668f32` mul x2 cn 346 cn = add -0.5f32 mul x2 cn 347 cn = add 1.0f32 mul x2 cn 348 put dst 0 sn 349 put dst 1 mul csign cn 350 end 351 352 fn inf_silu x f32 to f32 353 ret div x add 1.0f32 inf_exp neg x 354 end 355 356 fn inf_copy_f32 src view f32 * dst edit f32 * n ulong to void 357 for i ulong n bound 65536 358 put dst i get src i 359 end 360 end 361 362 fn inf_rms_norm_inplace x edit f32 * w view f32 * eps f32 to void 363 n = len x 364 ss = f32 0.0f32 365 for i ulong n bound 65536 366 v = get x i 367 ss = add ss mul v v 368 end 369 ss = div ss f32 n 370 scale = div 1.0f32 inf_sqrt add ss eps 371 for i ulong n bound 65536 372 put x i mul mul get x i scale get w i 373 end 374 end 375 376 fn inf_softmax x edit f32 * n ulong to void 377 if eq n 0 378 ret 379 end 380 mx = get x 0 381 for i ulong n bound 65536 382 v = get x i 383 if gt v mx 384 mx = v 385 end 386 end 387 sm = f32 0.0f32 388 for i ulong n bound 65536 389 e = inf_exp sub get x i mx 390 put x i e 391 sm = add sm e 392 end 393 for i ulong n bound 65536 394 put x i div get x i sm 395 end 396 end 397 398 fn inf_rope_cache pos ulong base f32 factors view f32 * cos_sin edit f32 * to void 399 theta_scale = inf_powf base div -2.0f32 f32 len cos_sin 400 theta = f32 pos 401 sc = new f32 2 0.0f32 402 i = ulong 0 403 while lt i div len cos_sin 2 bound 4096 404 if lt i len factors 405 inf_sin_cos div theta get factors i sc 406 else 407 inf_sin_cos theta sc 408 end 409 put cos_sin mul i 2 get sc 1 410 put cos_sin add mul i 2 1 get sc 0 411 theta = mul theta theta_scale 412 i = add i 1 413 end 414 end 415 416 fn inf_rope_apply_head x edit f32 * base ulong cos_sin view f32 * to void 417 i = ulong 0 418 while lt i div len cos_sin 2 bound 4096 419 idx0 = add base mul i 2 420 a = get x idx0 421 b = get x add idx0 1 422 c = get cos_sin mul i 2 423 s = get cos_sin add mul i 2 1 424 put x idx0 sub mul a c mul b s 425 put x add idx0 1 add mul a s mul b c 426 i = add i 1 427 end 428 end 429 430 fn inf_rope_apply x edit f32 * head_dim ulong cos_sin view f32 * to void 431 n_heads = div len x head_dim 432 for h ulong n_heads bound 4096 433 inf_rope_apply_head x mul h head_dim cos_sin 434 end 435 end 436 437 fn inf_argmax logits view f32 * n ulong to uint 438 best = ulong 0 439 if eq n 0 440 ret 0 441 end 442 bv = get logits 0 443 for i ulong n bound 262144 444 v = get logits i 445 if gt v bv 446 bv = v 447 best = i 448 end 449 end 450 ret uint best 451 end 452 453 note ======================================================================== 454 note GGML block math & dequantizers (quant.rs: Q4_K, Q6_K, Q8_0, F32, F16, BF16) 455 note ======================================================================== 456 457 fn inf_ggml_block_w ty uint to ulong 458 if eq ty INF_GGML_F32 459 ret 1 460 end 461 if eq ty INF_GGML_F16 462 ret 1 463 end 464 if eq ty INF_GGML_BF16 465 ret 1 466 end 467 if eq ty INF_GGML_I8 468 ret 1 469 end 470 if eq ty INF_GGML_I16 471 ret 1 472 end 473 if eq ty INF_GGML_I32 474 ret 1 475 end 476 if eq ty INF_GGML_I64 477 ret 1 478 end 479 if eq ty INF_GGML_F64 480 ret 1 481 end 482 if eq ty INF_GGML_Q4_0 483 ret 32 484 end 485 if eq ty INF_GGML_Q4_1 486 ret 32 487 end 488 if eq ty INF_GGML_Q5_0 489 ret 32 490 end 491 if eq ty INF_GGML_Q5_1 492 ret 32 493 end 494 if eq ty INF_GGML_Q8_0 495 ret 32 496 end 497 if eq ty INF_GGML_Q8_1 498 ret 32 499 end 500 if eq ty INF_GGML_Q2K 501 ret 256 502 end 503 if eq ty INF_GGML_Q3K 504 ret 256 505 end 506 if eq ty INF_GGML_Q4K 507 ret 256 508 end 509 if eq ty INF_GGML_Q5K 510 ret 256 511 end 512 if eq ty INF_GGML_Q6K 513 ret 256 514 end 515 if eq ty INF_GGML_Q8K 516 ret 256 517 end 518 ret 0 519 end 520 521 fn inf_ggml_block_b ty uint to ulong 522 if eq ty INF_GGML_F32 523 ret 4 524 end 525 if eq ty INF_GGML_I32 526 ret 4 527 end 528 if eq ty INF_GGML_F16 529 ret 2 530 end 531 if eq ty INF_GGML_BF16 532 ret 2 533 end 534 if eq ty INF_GGML_I16 535 ret 2 536 end 537 if eq ty INF_GGML_I8 538 ret 1 539 end 540 if eq ty INF_GGML_I64 541 ret 8 542 end 543 if eq ty INF_GGML_F64 544 ret 8 545 end 546 if eq ty INF_GGML_Q4_0 547 ret 18 548 end 549 if eq ty INF_GGML_Q4_1 550 ret 20 551 end 552 if eq ty INF_GGML_Q5_0 553 ret 22 554 end 555 if eq ty INF_GGML_Q5_1 556 ret 24 557 end 558 if eq ty INF_GGML_Q8_0 559 ret 34 560 end 561 if eq ty INF_GGML_Q8_1 562 ret 36 563 end 564 if eq ty INF_GGML_Q2K 565 ret 84 566 end 567 if eq ty INF_GGML_Q3K 568 ret 110 569 end 570 if eq ty INF_GGML_Q4K 571 ret 144 572 end 573 if eq ty INF_GGML_Q5K 574 ret 176 575 end 576 if eq ty INF_GGML_Q6K 577 ret 210 578 end 579 if eq ty INF_GGML_Q8K 580 ret 292 581 end 582 ret 0 583 end 584 585 fn inf_scale_min_k4_sc j ulong blk view uchar * soff ulong to uchar 586 if lt j 4 587 ret and get blk add soff j uchar 63 588 end 589 lo = and get blk add soff add j 4 uchar 0x0f 590 hi = shl shr get blk add soff sub j 4 6 4 591 ret or lo hi 592 end 593 594 fn inf_scale_min_k4_mn j ulong blk view uchar * soff ulong to uchar 595 if lt j 4 596 ret and get blk add soff add j 4 uchar 63 597 end 598 lo = shr get blk add soff add j 4 4 599 hi = shl shr get blk add soff j 6 4 600 ret or lo hi 601 end 602 603 fn inf_dequant_q4k_sub blk view uchar * boff ulong dst edit f32 * sb ulong to void 604 dc = mul inf_rd_f16_le blk boff f32 inf_scale_min_k4_sc sb blk add boff 4 605 mc = mul inf_rd_f16_le blk add boff 2 f32 inf_scale_min_k4_mn sb blk add boff 4 606 qb = add boff 16 607 qb = add qb mul div sb 2 32 608 for l ulong 32 609 bv = get blk add qb l 610 if eq rem sb 2 0 611 bv = and bv uchar 0x0f 612 else 613 bv = shr bv 4 614 end 615 put dst l sub mul dc f32 bv mc 616 end 617 end 618 619 fn inf_dequant_q4k_block blk view uchar * boff ulong dst edit f32 * doff ulong to void 620 for sb ulong 8 621 inf_dequant_q4k_sub blk boff part dst add doff mul sb 32 32 sb 622 end 623 end 624 625 fn inf_q6k_qval ql uchar qh uchar qshift ulong to int 626 lo = and ql uchar 0x0f 627 if ge qshift 4 628 lo = shr ql 4 629 end 630 hi = shl and shr qh qshift uchar 3 4 631 ret sub int or lo hi 32 632 end 633 634 fn inf_q6k_qterm blk view uchar * boff ulong half ulong l ulong slot ulong to int 635 is = div l 16 636 ho = add add boff 128 mul half 32 637 h = get blk add ho l 638 qlo = add boff mul half 64 639 sco = add add boff 192 mul half 8 640 if eq slot 0 641 q = inf_q6k_qval get blk add qlo l h 0 642 ret mul q inf_i8_from_u8 get blk add sco is 643 end 644 if eq slot 1 645 q = inf_q6k_qval get blk add qlo add l 32 h 2 646 ret mul q inf_i8_from_u8 get blk add sco add is 2 647 end 648 if eq slot 2 649 q = inf_q6k_qval get blk add qlo l h 4 650 ret mul q inf_i8_from_u8 get blk add sco add is 4 651 end 652 q = inf_q6k_qval get blk add qlo add l 32 h 6 653 ret mul q inf_i8_from_u8 get blk add sco add is 6 654 end 655 656 fn inf_dequant_q6k_half blk view uchar * boff ulong dst edit f32 * half ulong to void 657 d = inf_rd_f16_le blk add boff 208 658 for l ulong 32 659 put dst l mul d f32 inf_q6k_qterm blk boff half l 0 660 put dst add l 32 mul d f32 inf_q6k_qterm blk boff half l 1 661 put dst add l 64 mul d f32 inf_q6k_qterm blk boff half l 2 662 put dst add l 96 mul d f32 inf_q6k_qterm blk boff half l 3 663 end 664 end 665 666 fn inf_dequant_q6k_block blk view uchar * boff ulong dst edit f32 * doff ulong to void 667 inf_dequant_q6k_half blk boff part dst doff 128 0 668 inf_dequant_q6k_half blk boff part dst add doff 128 128 1 669 end 670 671 fn inf_dequant_q8_0_block blk view uchar * boff ulong dst edit f32 * doff ulong to void 672 d = inf_rd_f16_le blk boff 673 qoff = add boff 2 674 for l ulong 32 675 q = inf_i8_from_u8 get blk add qoff l 676 put dst add doff l mul d f32 q 677 end 678 end 679 680 fn inf_dot_q4k_f32 blk view uchar * boff ulong x view f32 * xoff ulong to f32 681 w = new f32 256 0.0f32 682 inf_dequant_q4k_block blk boff w 0 683 acc = f32 0.0f32 684 for i ulong 256 685 acc = add acc mul get w i get x add xoff i 686 end 687 ret acc 688 end 689 690 fn inf_dot_q6k_f32 blk view uchar * boff ulong x view f32 * xoff ulong to f32 691 w = new f32 256 0.0f32 692 inf_dequant_q6k_block blk boff w 0 693 acc = f32 0.0f32 694 for i ulong 256 695 acc = add acc mul get w i get x add xoff i 696 end 697 ret acc 698 end 699 700 fn inf_dot_q8_0_f32 blk view uchar * boff ulong x view f32 * xoff ulong to f32 701 w = new f32 32 0.0f32 702 inf_dequant_q8_0_block blk boff w 0 703 acc = f32 0.0f32 704 for i ulong 32 705 acc = add acc mul get w i get x add xoff i 706 end 707 ret acc 708 end 709 710 fn inf_plain_to_f32 ty uint src view uchar * dst edit f32 * to bool 711 n = len dst 712 if eq ty INF_GGML_F32 713 if ne len src mul n 4 714 ret false 715 end 716 for i ulong n bound 262144 717 put dst i inf_rd_f32_le src mul i 4 718 end 719 ret true 720 end 721 if eq ty INF_GGML_F16 722 if ne len src mul n 2 723 ret false 724 end 725 for i ulong n bound 262144 726 put dst i inf_rd_f16_le src mul i 2 727 end 728 ret true 729 end 730 if eq ty INF_GGML_BF16 731 if ne len src mul n 2 732 ret false 733 end 734 for i ulong n bound 262144 735 put dst i inf_rd_bf16_le src mul i 2 736 end 737 ret true 738 end 739 ret false 740 end 741 742 fn inf_dequantize ty uint src view uchar * dst edit f32 * to int 743 bw = inf_ggml_block_w ty 744 bb = inf_ggml_block_b ty 745 if eq bw 0 746 ret INF_QUANT_UNSUPPORTED 747 end 748 n = len dst 749 if ne rem n bw 0 750 ret INF_QUANT_BAD_LEN 751 end 752 nblk = div n bw 753 if ne len src mul nblk bb 754 ret INF_QUANT_BAD_LEN 755 end 756 if eq ty INF_GGML_Q4K 757 for b ulong nblk bound 65536 758 inf_dequant_q4k_block src mul b INF_Q4K_BYTES dst mul b INF_QK_K 759 end 760 ret INF_OK 761 end 762 if eq ty INF_GGML_Q6K 763 for b ulong nblk bound 65536 764 inf_dequant_q6k_block src mul b INF_Q6K_BYTES dst mul b INF_QK_K 765 end 766 ret INF_OK 767 end 768 if eq ty INF_GGML_Q8_0 769 for b ulong nblk bound 65536 770 inf_dequant_q8_0_block src mul b INF_Q8_0_BYTES dst mul b INF_Q8_0_QK 771 end 772 ret INF_OK 773 end 774 if inf_plain_to_f32 ty src dst 775 ret INF_OK 776 end 777 ret INF_QUANT_UNSUPPORTED 778 end 779 780 fn inf_dot_row_nblk ty uint slen ulong xlen ulong to long 781 bw = inf_ggml_block_w ty 782 bb = inf_ggml_block_b ty 783 if eq bw 0 784 ret long INF_QUANT_UNSUPPORTED 785 end 786 is_ok = false 787 if eq ty INF_GGML_Q4K 788 is_ok = true 789 end 790 if eq ty INF_GGML_Q6K 791 is_ok = true 792 end 793 if eq ty INF_GGML_Q8_0 794 is_ok = true 795 end 796 if not is_ok 797 ret long INF_QUANT_UNSUPPORTED 798 end 799 if ne rem xlen bw 0 800 ret long INF_QUANT_BAD_LEN 801 end 802 nblk = div xlen bw 803 if ne slen mul nblk bb 804 ret long INF_QUANT_BAD_LEN 805 end 806 ret long nblk 807 end 808 809 fn inf_dot_row ty uint src view uchar * x view f32 * res edit f32 1 to int 810 nb = inf_dot_row_nblk ty len src len x 811 if lt nb 0 812 ret int nb 813 end 814 acc = f32 0.0f32 815 b = ulong 0 816 while lt b ulong nb bound 65536 817 if eq ty INF_GGML_Q4K 818 acc = add acc inf_dot_q4k_f32 src mul b INF_Q4K_BYTES x mul b INF_QK_K 819 else 820 if eq ty INF_GGML_Q6K 821 acc = add acc inf_dot_q6k_f32 src mul b INF_Q6K_BYTES x mul b INF_QK_K 822 else 823 acc = add acc inf_dot_q8_0_f32 src mul b INF_Q8_0_BYTES x mul b INF_Q8_0_QK 824 end 825 end 826 b = add b 1 827 end 828 put res 0 acc 829 ret INF_OK 830 end 831 832 note ======================================================================== 833 note Q8_K activation quantization & integer dot products (dotprod.rs) 834 note ======================================================================== 835 836 fn inf_set_cpu_dotprod on bool to void 837 if on 838 put inf_cpu_dp_flag 0 uint 1 839 else 840 put inf_cpu_dp_flag 0 uint 0 841 end 842 end 843 844 fn inf_cpu_dotprod to bool 845 ret ne get inf_cpu_dp_flag 0 uint 0 846 end 847 848 fn inf_q8k_round x edit f32 * n ulong to void 849 nblk = div n INF_QK_K 850 for b ulong nblk bound 65536 851 base = mul b INF_QK_K 852 amax = f32 0.0f32 853 mx = f32 0.0f32 854 for i ulong 256 855 v = get x add base i 856 av = fabs v 857 if gt av amax 858 amax = av 859 mx = v 860 end 861 end 862 if ne amax 0.0f32 863 iscale = div -127.0f32 mx 864 d = div 1.0f32 iscale 865 for i ulong 256 866 t = mul get x add base i iscale 867 q = fmin sub add t `12582912`.0f32 `12582912`.0f32 127.0f32 868 put x add base i mul q d 869 end 870 end 871 end 872 end 873 874 fn inf_q8_quantize_one x view f32 * b ulong to void 875 base = mul b INF_QK_K 876 bbase = mul b 16 877 amax = f32 0.0f32 878 mx = f32 0.0f32 879 for i ulong 256 880 v = get x add base i 881 av = fabs v 882 if gt av amax 883 amax = av 884 mx = v 885 end 886 end 887 if eq amax 0.0f32 888 put inf_q8_d b 0.0f32 889 for i ulong 256 890 put inf_q8_qs add base i byte 0 891 end 892 for g ulong 16 893 put inf_q8_bsums add bbase g short 0 894 end 895 ret 896 end 897 iscale = div -127.0f32 mx 898 for i ulong 256 899 t = mul get x add base i iscale 900 qf = fmin sub add t `12582912`.0f32 `12582912`.0f32 127.0f32 901 put inf_q8_qs add base i byte qf 902 end 903 for g ulong 16 904 gs = int 0 905 gbase = add base mul g 16 906 for l ulong 16 907 gs = add gs int get inf_q8_qs add gbase l 908 end 909 put inf_q8_bsums add bbase g short gs 910 end 911 put inf_q8_d b div 1.0f32 iscale 912 end 913 914 fn inf_q8_quantize x view f32 * n ulong to int 915 if ne rem n INF_QK_K 0 916 ret INF_QUANT_BAD_LEN 917 end 918 nblk = div n INF_QK_K 919 if gt nblk INF_Q8_MAX_BLKS 920 ret INF_QUANT_BAD_LEN 921 end 922 put inf_q8_nblk 0 nblk 923 for b ulong nblk bound 64 924 inf_q8_quantize_one x b 925 end 926 ret INF_OK 927 end 928 929 fn inf_q8_blocks to ulong 930 ret get inf_q8_nblk 0 931 end 932 933 fn inf_q8_value i ulong to f32 934 b = div i INF_QK_K 935 ret mul f32 int get inf_q8_qs i get inf_q8_d b 936 end 937 938 fn inf_q4k_accum_pair blk view uchar * boff ulong bi ulong j ulong to int 939 qb = add add boff 16 mul j 32 940 yb = add mul bi INF_QK_K mul j 64 941 lo = int 0 942 hi = int 0 943 for l ulong 32 944 bv = get blk add qb l 945 q0 = int get inf_q8_qs add yb l 946 lo = add lo mul int and bv uchar 0x0f q0 947 q1 = int get inf_q8_qs add yb add l 32 948 hi = add hi mul int shr bv 4 q1 949 end 950 sc0 = int inf_scale_min_k4_sc mul j 2 blk add boff 4 951 sc1 = int inf_scale_min_k4_sc add mul j 2 1 blk add boff 4 952 ret add mul lo sc0 mul hi sc1 953 end 954 955 fn inf_q4k_accum blk view uchar * boff ulong bi ulong acc edit int 2 to void 956 sumi = int 0 957 for j ulong 4 958 sumi = add sumi inf_q4k_accum_pair blk boff bi j 959 end 960 put acc 0 sumi 961 mins = int 0 962 bbase = mul bi 16 963 for sb ulong 8 964 mn = int inf_scale_min_k4_mn sb blk add boff 4 965 k = add bbase mul sb 2 966 bs = add int get inf_q8_bsums k int get inf_q8_bsums add k 1 967 mins = add mins mul mn bs 968 end 969 put acc 1 mins 970 end 971 972 fn inf_dot_q4k_q8 blk view uchar * boff ulong bi ulong to f32 973 acc = new int 2 0 974 inf_q4k_accum blk boff bi acc 975 dy = get inf_q8_d bi 976 d = mul dy inf_rd_f16_le blk boff 977 dmin = mul dy inf_rd_f16_le blk add boff 2 978 ret sub mul d f32 get acc 0 mul dmin f32 get acc 1 979 end 980 981 fn inf_q6k_uterm blk view uchar * boff ulong half ulong l ulong slot ulong to int 982 is = div l 16 983 ho = add add boff 128 mul half 32 984 h = get blk add ho l 985 qlo = add boff mul half 64 986 sco = add add boff 192 mul half 8 987 if eq slot 0 988 q = add inf_q6k_qval get blk add qlo l h 0 32 989 ret mul q inf_i8_from_u8 get blk add sco is 990 end 991 if eq slot 1 992 q = add inf_q6k_qval get blk add qlo add l 32 h 2 32 993 ret mul q inf_i8_from_u8 get blk add sco add is 2 994 end 995 if eq slot 2 996 q = add inf_q6k_qval get blk add qlo l h 4 32 997 ret mul q inf_i8_from_u8 get blk add sco add is 4 998 end 999 q = add inf_q6k_qval get blk add qlo add l 32 h 6 32 1000 ret mul q inf_i8_from_u8 get blk add sco add is 6 1001 end 1002 1003 fn inf_q6k_accum_half blk view uchar * boff ulong bi ulong half ulong to int 1004 yb = add mul bi INF_QK_K mul half 128 1005 isum = int 0 1006 for l ulong 32 1007 t = inf_q6k_uterm blk boff half l 0 1008 isum = add isum mul t int get inf_q8_qs add yb l 1009 t = inf_q6k_uterm blk boff half l 1 1010 isum = add isum mul t int get inf_q8_qs add yb add l 32 1011 t = inf_q6k_uterm blk boff half l 2 1012 isum = add isum mul t int get inf_q8_qs add yb add l 64 1013 t = inf_q6k_uterm blk boff half l 3 1014 isum = add isum mul t int get inf_q8_qs add yb add l 96 1015 end 1016 ret isum 1017 end 1018 1019 fn inf_q6k_accum blk view uchar * boff ulong bi ulong acc edit int 2 to void 1020 h0 = inf_q6k_accum_half blk boff bi 0 1021 h1 = inf_q6k_accum_half blk boff bi 1 1022 isum = add h0 h1 1023 mins = int 0 1024 scb = add boff 192 1025 bbase = mul bi 16 1026 for g ulong 16 1027 s = inf_i8_from_u8 get blk add scb g 1028 bs = int get inf_q8_bsums add bbase g 1029 mins = add mins mul s bs 1030 end 1031 put acc 0 isum 1032 put acc 1 mins 1033 end 1034 1035 fn inf_dot_q6k_q8 blk view uchar * boff ulong bi ulong to f32 1036 acc = new int 2 0 1037 inf_q6k_accum blk boff bi acc 1038 d = mul inf_rd_f16_le blk add boff 208 get inf_q8_d bi 1039 ret mul d f32 sub get acc 0 mul 32 get acc 1 1040 end 1041 1042 fn inf_dot_row_q8 ty uint src view uchar * res edit f32 1 to int 1043 bw = inf_ggml_block_w ty 1044 bb = inf_ggml_block_b ty 1045 if eq ty INF_GGML_Q4K 1046 else 1047 if eq ty INF_GGML_Q6K 1048 else 1049 ret INF_QUANT_UNSUPPORTED 1050 end 1051 end 1052 if ne bw INF_QK_K 1053 ret INF_QUANT_BAD_LEN 1054 end 1055 slen = len src 1056 if ne rem slen bb 0 1057 ret INF_QUANT_BAD_LEN 1058 end 1059 nblk = div slen bb 1060 if ne nblk get inf_q8_nblk 0 1061 ret INF_QUANT_BAD_LEN 1062 end 1063 acc = f32 0.0f32 1064 for bi ulong nblk bound 64 1065 if eq ty INF_GGML_Q4K 1066 acc = add acc inf_dot_q4k_q8 src mul bi INF_Q4K_BYTES bi 1067 else 1068 acc = add acc inf_dot_q6k_q8 src mul bi INF_Q6K_BYTES bi 1069 end 1070 end 1071 put res 0 acc 1072 ret INF_OK 1073 end 1074 1075 note ======================================================================== 1076 note GGUF v2/v3 bounded reader (gguf.rs) 1077 note ======================================================================== 1078 1079 fn inf_utf8_valid b view uchar * off ulong n ulong to bool 1080 i = ulong 0 1081 while lt i n bound 65536 1082 c = get b add off i 1083 if lt c uchar 0x80 1084 i = add i 1 1085 else 1086 if ge c uchar 0xc2 1087 if le c uchar 0xdf 1088 if ge add i 1 n 1089 ret false 1090 end 1091 c1 = get b add off add i 1 1092 if ne and c1 uchar 0xc0 uchar 0x80 1093 ret false 1094 end 1095 i = add i 2 1096 else 1097 if le c uchar 0xef 1098 if ge add i 2 n 1099 ret false 1100 end 1101 c1 = get b add off add i 1 1102 c2 = get b add off add i 2 1103 if ne and c1 uchar 0xc0 uchar 0x80 1104 ret false 1105 end 1106 if ne and c2 uchar 0xc0 uchar 0x80 1107 ret false 1108 end 1109 if eq c uchar 0xe0 1110 if lt c1 uchar 0xa0 1111 ret false 1112 end 1113 end 1114 if eq c uchar 0xed 1115 if ge c1 uchar 0xa0 1116 ret false 1117 end 1118 end 1119 i = add i 3 1120 else 1121 if le c uchar 0xf4 1122 if ge add i 3 n 1123 ret false 1124 end 1125 c1 = get b add off add i 1 1126 c2 = get b add off add i 2 1127 c3 = get b add off add i 3 1128 if ne and c1 uchar 0xc0 uchar 0x80 1129 ret false 1130 end 1131 if ne and c2 uchar 0xc0 uchar 0x80 1132 ret false 1133 end 1134 if ne and c3 uchar 0xc0 uchar 0x80 1135 ret false 1136 end 1137 if eq c uchar 0xf0 1138 if lt c1 uchar 0x90 1139 ret false 1140 end 1141 end 1142 if eq c uchar 0xf4 1143 if ge c1 uchar 0x90 1144 ret false 1145 end 1146 end 1147 i = add i 4 1148 else 1149 ret false 1150 end 1151 end 1152 end 1153 else 1154 ret false 1155 end 1156 end 1157 end 1158 ret true 1159 end 1160 1161 fn inf_vtype_fixed_size vt uint to ulong 1162 if eq vt INF_VT_U8 1163 ret 1 1164 end 1165 if eq vt INF_VT_I8 1166 ret 1 1167 end 1168 if eq vt INF_VT_BOOL 1169 ret 1 1170 end 1171 if eq vt INF_VT_U16 1172 ret 2 1173 end 1174 if eq vt INF_VT_I16 1175 ret 2 1176 end 1177 if eq vt INF_VT_U32 1178 ret 4 1179 end 1180 if eq vt INF_VT_I32 1181 ret 4 1182 end 1183 if eq vt INF_VT_F32 1184 ret 4 1185 end 1186 if eq vt INF_VT_U64 1187 ret 8 1188 end 1189 if eq vt INF_VT_I64 1190 ret 8 1191 end 1192 if eq vt INF_VT_F64 1193 ret 8 1194 end 1195 ret 0 1196 end 1197 1198 fn inf_slice_eq_text b view uchar * off ulong n ulong t view uchar * to bool 1199 if ne n len t 1200 ret false 1201 end 1202 for i ulong n bound 65536 1203 if ne get b add off i get t i 1204 ret false 1205 end 1206 end 1207 ret true 1208 end 1209 1210 fn inf_cur_str b view uchar * cur edit ulong 1 soff edit ulong 1 slen edit ulong 1 to int 1211 pos = get cur 0 1212 blen = len b 1213 if lt sub blen pos 8 1214 ret INF_GGUF_TRUNCATED 1215 end 1216 n = inf_rd_u64_le b pos 1217 pos = add pos 8 1218 if gt n sub blen pos 1219 ret INF_GGUF_TRUNCATED 1220 end 1221 if not inf_utf8_valid b pos n 1222 ret INF_GGUF_BAD_UTF8 1223 end 1224 put soff 0 pos 1225 put slen 0 n 1226 put cur 0 add pos n 1227 ret INF_OK 1228 end 1229 1230 fn inf_parse_kv_val b view uchar * cur edit ulong 1 idx ulong vt uint to int 1231 so = new ulong 1 0 1232 sl = new ulong 1 0 1233 pos = get cur 0 1234 blen = len b 1235 rem_b = sub blen pos 1236 if eq vt INF_VT_U8 1237 if lt rem_b 1 1238 ret INF_GGUF_TRUNCATED 1239 end 1240 put inf_kv_u64 idx ulong get b pos 1241 put cur 0 add pos 1 1242 ret INF_OK 1243 end 1244 if eq vt INF_VT_I8 1245 if lt rem_b 1 1246 ret INF_GGUF_TRUNCATED 1247 end 1248 put inf_kv_u64 idx ulong get b pos 1249 put cur 0 add pos 1 1250 ret INF_OK 1251 end 1252 if eq vt INF_VT_BOOL 1253 if lt rem_b 1 1254 ret INF_GGUF_TRUNCATED 1255 end 1256 bv = get b pos 1257 if gt bv uchar 1 1258 ret INF_GGUF_BAD_VTYPE 1259 end 1260 put inf_kv_u64 idx ulong bv 1261 put cur 0 add pos 1 1262 ret INF_OK 1263 end 1264 if eq vt INF_VT_U16 1265 if lt rem_b 2 1266 ret INF_GGUF_TRUNCATED 1267 end 1268 put inf_kv_u64 idx ulong inf_rd_u16_le b pos 1269 put cur 0 add pos 2 1270 ret INF_OK 1271 end 1272 if eq vt INF_VT_I16 1273 if lt rem_b 2 1274 ret INF_GGUF_TRUNCATED 1275 end 1276 put inf_kv_u64 idx ulong inf_rd_u16_le b pos 1277 put cur 0 add pos 2 1278 ret INF_OK 1279 end 1280 if eq vt INF_VT_U32 1281 if lt rem_b 4 1282 ret INF_GGUF_TRUNCATED 1283 end 1284 put inf_kv_u64 idx ulong inf_rd_u32_le b pos 1285 put cur 0 add pos 4 1286 ret INF_OK 1287 end 1288 if eq vt INF_VT_I32 1289 if lt rem_b 4 1290 ret INF_GGUF_TRUNCATED 1291 end 1292 put inf_kv_u64 idx ulong inf_rd_u32_le b pos 1293 put cur 0 add pos 4 1294 ret INF_OK 1295 end 1296 if eq vt INF_VT_F32 1297 if lt rem_b 4 1298 ret INF_GGUF_TRUNCATED 1299 end 1300 put inf_kv_f32 idx inf_rd_f32_le b pos 1301 put cur 0 add pos 4 1302 ret INF_OK 1303 end 1304 if eq vt INF_VT_U64 1305 if lt rem_b 8 1306 ret INF_GGUF_TRUNCATED 1307 end 1308 put inf_kv_u64 idx inf_rd_u64_le b pos 1309 put cur 0 add pos 8 1310 ret INF_OK 1311 end 1312 if eq vt INF_VT_I64 1313 if lt rem_b 8 1314 ret INF_GGUF_TRUNCATED 1315 end 1316 put inf_kv_u64 idx inf_rd_u64_le b pos 1317 put cur 0 add pos 8 1318 ret INF_OK 1319 end 1320 if eq vt INF_VT_F64 1321 if lt rem_b 8 1322 ret INF_GGUF_TRUNCATED 1323 end 1324 u = inf_rd_u64_le b pos 1325 put inf_kv_u64 idx u 1326 put inf_kv_f32 idx f32 bitcast f64 u 1327 put cur 0 add pos 8 1328 ret INF_OK 1329 end 1330 if eq vt INF_VT_STR 1331 rc = inf_cur_str b cur so sl 1332 if ne rc INF_OK 1333 ret rc 1334 end 1335 put inf_kv_voff idx get so 0 1336 put inf_kv_vlen idx get sl 0 1337 ret INF_OK 1338 end 1339 if eq vt INF_VT_ARRAY 1340 if lt rem_b 12 1341 ret INF_GGUF_TRUNCATED 1342 end 1343 ae = inf_rd_u32_le b pos 1344 if gt ae INF_VT_F64 1345 ret INF_GGUF_BAD_VTYPE 1346 end 1347 al = inf_rd_u64_le b add pos 4 1348 pos = add pos 12 1349 put cur 0 pos 1350 put inf_kv_aelem idx ae 1351 put inf_kv_alen idx al 1352 put inf_kv_voff idx pos 1353 fsz = inf_vtype_fixed_size ae 1354 if gt fsz 0 1355 if gt al div ulong 0xffffffffffffffff fsz 1356 ret INF_GGUF_OVERFLOW 1357 end 1358 tot = mul al fsz 1359 if gt tot sub blen pos 1360 ret INF_GGUF_TRUNCATED 1361 end 1362 put inf_kv_vlen idx tot 1363 put cur 0 add pos tot 1364 ret INF_OK 1365 end 1366 if eq ae INF_VT_STR 1367 if gt al div sub blen pos 8 1368 ret INF_GGUF_TOO_MANY 1369 end 1370 for k ulong al bound 65536 1371 rc = inf_cur_str b cur so sl 1372 if ne rc INF_OK 1373 ret rc 1374 end 1375 end 1376 put inf_kv_vlen idx sub get cur 0 pos 1377 ret INF_OK 1378 end 1379 ret INF_GGUF_BAD_VTYPE 1380 end 1381 ret INF_GGUF_BAD_VTYPE 1382 end 1383 1384 fn inf_finish_tensor idx ulong ty uint nelem ulong to int 1385 bw = inf_ggml_block_w ty 1386 bb = inf_ggml_block_b ty 1387 if eq bw 0 1388 ret INF_GGUF_BAD_TYPE 1389 end 1390 if ne rem get inf_ten_d0 idx bw 0 1391 ret INF_GGUF_BAD_DIMS 1392 end 1393 nblk = div nelem bw 1394 if gt nblk div ulong 0xffffffffffffffff bb 1395 ret INF_GGUF_OVERFLOW 1396 end 1397 put inf_ten_ty idx ty 1398 put inf_ten_nelem idx nelem 1399 put inf_ten_nbytes idx mul nblk bb 1400 ret INF_OK 1401 end 1402 1403 fn inf_parse_tensor b view uchar * cur edit ulong 1 idx ulong to int 1404 so = new ulong 1 0 1405 sl = new ulong 1 0 1406 rc = inf_cur_str b cur so sl 1407 if ne rc INF_OK 1408 ret rc 1409 end 1410 put inf_ten_noff idx get so 0 1411 put inf_ten_nlen idx get sl 0 1412 pos = get cur 0 1413 blen = len b 1414 if lt sub blen pos 4 1415 ret INF_GGUF_TRUNCATED 1416 end 1417 nd = inf_rd_u32_le b pos 1418 pos = add pos 4 1419 if eq nd 0 1420 ret INF_GGUF_BAD_DIMS 1421 end 1422 if gt nd 4 1423 ret INF_GGUF_BAD_DIMS 1424 end 1425 if lt sub blen pos add mul ulong nd 8 12 1426 ret INF_GGUF_TRUNCATED 1427 end 1428 put inf_ten_ndims idx nd 1429 put inf_ten_d0 idx ulong 1 1430 put inf_ten_d1 idx ulong 1 1431 put inf_ten_d2 idx ulong 1 1432 put inf_ten_d3 idx ulong 1 1433 nelem = ulong 1 1434 for d ulong ulong nd bound 4 1435 dv = inf_rd_u64_le b pos 1436 pos = add pos 8 1437 if eq d 0 1438 put inf_ten_d0 idx dv 1439 end 1440 if eq d 1 1441 put inf_ten_d1 idx dv 1442 end 1443 if eq d 2 1444 put inf_ten_d2 idx dv 1445 end 1446 if eq d 3 1447 put inf_ten_d3 idx dv 1448 end 1449 if gt dv 0 1450 if gt nelem div ulong 0xffffffffffffffff dv 1451 ret INF_GGUF_OVERFLOW 1452 end 1453 end 1454 nelem = mul nelem dv 1455 end 1456 ty = inf_rd_u32_le b pos 1457 put inf_ten_off idx inf_rd_u64_le b add pos 4 1458 put cur 0 add pos 12 1459 ret inf_finish_tensor idx ty nelem 1460 end 1461 1462 text inf_key_alignment "general.alignment" 1463 text inf_key_arch "general.architecture" 1464 text inf_val_llama "llama" 1465 text inf_key_vocab "llama.vocab_size" 1466 text inf_key_tokens "tokenizer.ggml.tokens" 1467 text inf_key_blocks "llama.block_count" 1468 text inf_key_embd "llama.embedding_length" 1469 text inf_key_heads "llama.attention.head_count" 1470 text inf_key_kv_heads "llama.attention.head_count_kv" 1471 text inf_key_ffn "llama.feed_forward_length" 1472 text inf_key_ctx "llama.context_length" 1473 text inf_key_rope_base "llama.rope.freq_base" 1474 text inf_key_rms_eps "llama.attention.layer_norm_rms_epsilon" 1475 1476 fn inf_gguf_parse_kvs b view uchar * cur edit ulong 1 nk ulong to int 1477 so = new ulong 1 0 1478 sl = new ulong 1 0 1479 blen = len b 1480 for i ulong nk bound 256 1481 rc = inf_cur_str b cur so sl 1482 if ne rc INF_OK 1483 ret rc 1484 end 1485 put inf_kv_koff i get so 0 1486 put inf_kv_klen i get sl 0 1487 pos = get cur 0 1488 if lt sub blen pos 4 1489 ret INF_GGUF_TRUNCATED 1490 end 1491 vt = inf_rd_u32_le b pos 1492 if gt vt INF_VT_F64 1493 ret INF_GGUF_BAD_VTYPE 1494 end 1495 put cur 0 add pos 4 1496 put inf_kv_vtype i vt 1497 rc = inf_parse_kv_val b cur i vt 1498 if ne rc INF_OK 1499 ret rc 1500 end 1501 end 1502 ret INF_OK 1503 end 1504 1505 fn inf_gguf_read_align b view uchar * nk ulong to long 1506 align = ulong 32 1507 for i ulong nk bound 256 1508 if inf_slice_eq_text b get inf_kv_koff i get inf_kv_klen i inf_key_alignment 1509 if ne get inf_kv_vtype i INF_VT_U32 1510 ret long INF_GGUF_BAD_ALIGN 1511 end 1512 av = get inf_kv_u64 i 1513 if eq av 0 1514 ret long INF_GGUF_BAD_ALIGN 1515 end 1516 if ne and av sub av 1 0 1517 ret long INF_GGUF_BAD_ALIGN 1518 end 1519 align = av 1520 end 1521 end 1522 ret long align 1523 end 1524 1525 fn inf_gguf_check_tensors nt ulong dlen ulong align ulong to int 1526 for i ulong nt bound 256 1527 toff = get inf_ten_off i 1528 tnb = get inf_ten_nbytes i 1529 if gt toff sub ulong 0xffffffffffffffff tnb 1530 ret INF_GGUF_OVERFLOW 1531 end 1532 if gt add toff tnb dlen 1533 ret INF_GGUF_TENSOR_OOB 1534 end 1535 if ne rem toff align 0 1536 ret INF_GGUF_TENSOR_OOB 1537 end 1538 end 1539 ret INF_OK 1540 end 1541 1542 fn inf_gguf_finish_parse b view uchar * cur edit ulong 1 g edit InfGguf to int 1543 ar = inf_gguf_read_align b get g n_kv 1544 if lt ar 0 1545 ret int ar 1546 end 1547 align = ulong ar 1548 put g alignment align 1549 nt = get g n_tensors 1550 for i ulong nt bound 256 1551 rc = inf_parse_tensor b cur i 1552 if ne rc INF_OK 1553 ret rc 1554 end 1555 end 1556 am1 = sub align 1 1557 pos = get cur 0 1558 if gt pos sub ulong 0xffffffffffffffff am1 1559 ret INF_GGUF_OVERFLOW 1560 end 1561 ds = and add pos am1 not am1 1562 if gt ds len b 1563 ret INF_GGUF_TRUNCATED 1564 end 1565 rc = inf_gguf_check_tensors nt sub len b ds align 1566 if ne rc INF_OK 1567 ret rc 1568 end 1569 put g data_start ds 1570 ret INF_OK 1571 end 1572 1573 fn inf_gguf_parse_range b view uchar * min_ver uint max_ver uint g edit InfGguf to int 1574 if lt len b 24 1575 ret INF_GGUF_TRUNCATED 1576 end 1577 if ne get b 0 uchar 71 1578 ret INF_GGUF_BAD_MAGIC 1579 end 1580 if ne get b 1 uchar 71 1581 ret INF_GGUF_BAD_MAGIC 1582 end 1583 if ne get b 2 uchar 85 1584 ret INF_GGUF_BAD_MAGIC 1585 end 1586 if ne get b 3 uchar 70 1587 ret INF_GGUF_BAD_MAGIC 1588 end 1589 ver = inf_rd_u32_le b 4 1590 if lt ver min_ver 1591 ret INF_GGUF_BAD_VERSION 1592 end 1593 if gt ver max_ver 1594 ret INF_GGUF_BAD_VERSION 1595 end 1596 put g version ver 1597 nt = inf_rd_u64_le b 8 1598 nk = inf_rd_u64_le b 16 1599 if gt nk div sub len b 24 12 1600 ret INF_GGUF_TOO_MANY 1601 end 1602 if gt nt div sub len b 24 32 1603 ret INF_GGUF_TOO_MANY 1604 end 1605 if gt nk INF_MAX_KV 1606 ret INF_GGUF_CAPACITY 1607 end 1608 if gt nt INF_MAX_TENSORS 1609 ret INF_GGUF_CAPACITY 1610 end 1611 put g n_kv nk 1612 put g n_tensors nt 1613 put g file_len len b 1614 cur = new ulong 1 24 1615 rc = inf_gguf_parse_kvs b cur nk 1616 if ne rc INF_OK 1617 ret rc 1618 end 1619 ret inf_gguf_finish_parse b cur g 1620 end 1621 1622 fn inf_gguf_parse b view uchar * g edit InfGguf to int 1623 ret inf_gguf_parse_range b 2 3 g 1624 end 1625 1626 fn inf_gguf_parse_v3 b view uchar * g edit InfGguf to int 1627 ret inf_gguf_parse_range b 3 3 g 1628 end 1629 1630 fn inf_gguf_find_kv g view InfGguf b view uchar * key view uchar * to long 1631 nk = get g n_kv 1632 for i ulong nk bound 256 1633 if inf_slice_eq_text b get inf_kv_koff i get inf_kv_klen i key 1634 ret long i 1635 end 1636 end 1637 ret long -1 1638 end 1639 1640 fn inf_gguf_kv_as_u64 idx ulong dst edit ulong 1 to bool 1641 vt = get inf_kv_vtype idx 1642 u = get inf_kv_u64 idx 1643 if eq vt INF_VT_U8 1644 put dst 0 u 1645 ret true 1646 end 1647 if eq vt INF_VT_U16 1648 put dst 0 u 1649 ret true 1650 end 1651 if eq vt INF_VT_U32 1652 put dst 0 u 1653 ret true 1654 end 1655 if eq vt INF_VT_U64 1656 put dst 0 u 1657 ret true 1658 end 1659 if eq vt INF_VT_I8 1660 if ge u 128 1661 ret false 1662 end 1663 put dst 0 u 1664 ret true 1665 end 1666 if eq vt INF_VT_I16 1667 if ge u 32768 1668 ret false 1669 end 1670 put dst 0 u 1671 ret true 1672 end 1673 if eq vt INF_VT_I32 1674 if ge u `2147483648` 1675 ret false 1676 end 1677 put dst 0 u 1678 ret true 1679 end 1680 if eq vt INF_VT_I64 1681 if ge u ulong 0x8000000000000000 1682 ret false 1683 end 1684 put dst 0 u 1685 ret true 1686 end 1687 ret false 1688 end 1689 1690 fn inf_gguf_u64_key g view InfGguf b view uchar * key view uchar * dst edit ulong 1 to int 1691 idx = inf_gguf_find_kv g b key 1692 if lt idx 0 1693 ret INF_GGUF_MISSING 1694 end 1695 if not inf_gguf_kv_as_u64 ulong idx dst 1696 ret INF_GGUF_WRONG_TYPE 1697 end 1698 ret INF_OK 1699 end 1700 1701 fn inf_gguf_u32_key g view InfGguf b view uchar * key view uchar * dst edit uint 1 to int 1702 tmp = new ulong 1 0 1703 rc = inf_gguf_u64_key g b key tmp 1704 if ne rc INF_OK 1705 ret rc 1706 end 1707 v = get tmp 0 1708 if gt v ulong 0xffffffff 1709 ret INF_GGUF_WRONG_TYPE 1710 end 1711 put dst 0 uint v 1712 ret INF_OK 1713 end 1714 1715 fn inf_gguf_f32_key g view InfGguf b view uchar * key view uchar * dst edit f32 1 to int 1716 idx = inf_gguf_find_kv g b key 1717 if lt idx 0 1718 ret INF_GGUF_MISSING 1719 end 1720 vt = get inf_kv_vtype ulong idx 1721 if eq vt INF_VT_F32 1722 put dst 0 get inf_kv_f32 ulong idx 1723 ret INF_OK 1724 end 1725 if eq vt INF_VT_F64 1726 put dst 0 get inf_kv_f32 ulong idx 1727 ret INF_OK 1728 end 1729 ret INF_GGUF_WRONG_TYPE 1730 end 1731 1732 fn inf_gguf_find_tensor g view InfGguf b view uchar * name view uchar * to long 1733 nt = get g n_tensors 1734 for i ulong nt bound 256 1735 if inf_slice_eq_text b get inf_ten_noff i get inf_ten_nlen i name 1736 ret long i 1737 end 1738 end 1739 ret long -1 1740 end 1741 1742 fn inf_gguf_tensor_row_bytes ti ulong to ulong 1743 bw = inf_ggml_block_w get inf_ten_ty ti 1744 bb = inf_ggml_block_b get inf_ten_ty ti 1745 ret mul div get inf_ten_d0 ti bw bb 1746 end 1747 1748 fn inf_gguf_llama_params g view InfGguf b view uchar * p edit InfLlamaParams to int 1749 aidx = inf_gguf_find_kv g b inf_key_arch 1750 if lt aidx 0 1751 ret INF_GGUF_MISSING 1752 end 1753 if ne get inf_kv_vtype ulong aidx INF_VT_STR 1754 ret INF_GGUF_WRONG_TYPE 1755 end 1756 if not inf_slice_eq_text b get inf_kv_voff ulong aidx get inf_kv_vlen ulong aidx inf_val_llama 1757 ret INF_GGUF_WRONG_TYPE 1758 end 1759 u32_buf = new uint 1 0 1760 f32_buf = new f32 1 0.0f32 1761 vidx = inf_gguf_find_kv g b inf_key_vocab 1762 if ge vidx 0 1763 rc = inf_gguf_u32_key g b inf_key_vocab u32_buf 1764 if ne rc INF_OK 1765 ret rc 1766 end 1767 put p vocab_size get u32_buf 0 1768 else 1769 tidx = inf_gguf_find_kv g b inf_key_tokens 1770 if lt tidx 0 1771 ret INF_GGUF_MISSING 1772 end 1773 if ne get inf_kv_vtype ulong tidx INF_VT_ARRAY 1774 ret INF_GGUF_WRONG_TYPE 1775 end 1776 put p vocab_size uint get inf_kv_alen ulong tidx 1777 end 1778 rc = inf_gguf_u32_key g b inf_key_blocks u32_buf 1779 if ne rc INF_OK 1780 ret rc 1781 end 1782 put p block_count get u32_buf 0 1783 rc = inf_gguf_u32_key g b inf_key_embd u32_buf 1784 if ne rc INF_OK 1785 ret rc 1786 end 1787 put p embedding_length get u32_buf 0 1788 rc = inf_gguf_u32_key g b inf_key_heads u32_buf 1789 if ne rc INF_OK 1790 ret rc 1791 end 1792 put p head_count get u32_buf 0 1793 rc = inf_gguf_u32_key g b inf_key_kv_heads u32_buf 1794 if ne rc INF_OK 1795 ret rc 1796 end 1797 put p head_count_kv get u32_buf 0 1798 rc = inf_gguf_u32_key g b inf_key_ffn u32_buf 1799 if ne rc INF_OK 1800 ret rc 1801 end 1802 put p feed_forward_length get u32_buf 0 1803 rc = inf_gguf_u32_key g b inf_key_ctx u32_buf 1804 if ne rc INF_OK 1805 ret rc 1806 end 1807 put p context_length get u32_buf 0 1808 rc = inf_gguf_f32_key g b inf_key_rope_base f32_buf 1809 if ne rc INF_OK 1810 ret rc 1811 end 1812 put p rope_freq_base get f32_buf 0 1813 rc = inf_gguf_f32_key g b inf_key_rms_eps f32_buf 1814 if ne rc INF_OK 1815 ret rc 1816 end 1817 put p rms_norm_eps get f32_buf 0 1818 ret INF_OK 1819 end