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_test.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_test.om` 1531 lines ยท 35025 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note aienux/crates/aienos-infer/infer_test.om: Behavioral self-test for pure-Omega aienos-infer. 3 note Compose with: si/directives/om2.om aienux/crates/aienos-infer/infer_core.om aienux/crates/aienos-infer/infer_decode.om aienux/crates/aienos-infer/infer_test.om 4 5 note ======================================================================== 6 note Test helpers: little-endian writers, PRNG, GGUF builder 7 note ======================================================================== 8 9 fn tst_put_u16 buf edit uchar * pos ulong v ushort to ulong 10 put buf pos uchar and v ushort 0xff 11 put buf add pos 1 uchar and shr v 8 ushort 0xff 12 ret add pos 2 13 end 14 15 fn tst_put_u32 buf edit uchar * pos ulong v uint to ulong 16 put buf pos uchar and v 0xff 17 put buf add pos 1 uchar and shr v 8 0xff 18 put buf add pos 2 uchar and shr v 16 0xff 19 put buf add pos 3 uchar and shr v 24 0xff 20 ret add pos 4 21 end 22 23 fn tst_put_u64 buf edit uchar * pos ulong v ulong to ulong 24 for i ulong 8 25 put buf add pos i uchar and shr v mul i 8 0xff 26 end 27 ret add pos 8 28 end 29 30 fn tst_put_f32 buf edit uchar * pos ulong v f32 to ulong 31 ret tst_put_u32 buf pos bitcast uint v 32 end 33 34 fn tst_put_bytes buf edit uchar * pos ulong s view uchar * to ulong 35 n = len s 36 for i ulong n bound 256 37 put buf add pos i get s i 38 end 39 ret add pos n 40 end 41 42 fn tst_put_str buf edit uchar * pos ulong s view uchar * to ulong 43 p = tst_put_u64 buf pos len s 44 ret tst_put_bytes buf p s 45 end 46 47 fn tst_kv_u32 buf edit uchar * pos ulong key view uchar * v uint to ulong 48 p = tst_put_str buf pos key 49 p = tst_put_u32 buf p INF_VT_U32 50 ret tst_put_u32 buf p v 51 end 52 53 fn tst_kv_f32 buf edit uchar * pos ulong key view uchar * v f32 to ulong 54 p = tst_put_str buf pos key 55 p = tst_put_u32 buf p INF_VT_F32 56 ret tst_put_f32 buf p v 57 end 58 59 fn tst_gguf_hdr buf edit uchar * ver uint nt ulong nk ulong to ulong 60 put buf 0 uchar 71 61 put buf 1 uchar 71 62 put buf 2 uchar 85 63 put buf 3 uchar 70 64 p = tst_put_u32 buf 4 ver 65 p = tst_put_u64 buf p nt 66 ret tst_put_u64 buf p nk 67 end 68 69 fn tst_meta1 meta edit ulong 5 d0 ulong ty uint off ulong to void 70 put meta 0 1 71 put meta 1 d0 72 put meta 2 0 73 put meta 3 ulong ty 74 put meta 4 off 75 end 76 77 fn tst_meta2 meta edit ulong 5 d0 ulong d1 ulong ty uint off ulong to void 78 put meta 0 2 79 put meta 1 d0 80 put meta 2 d1 81 put meta 3 ulong ty 82 put meta 4 off 83 end 84 85 fn tst_put_tensor buf edit uchar * pos ulong name view uchar * meta view ulong 5 to ulong 86 note meta: [n_dims, d0, d1, ty, off] 87 p = tst_put_str buf pos name 88 nd = get meta 0 89 p = tst_put_u32 buf p uint nd 90 if ge nd 1 91 p = tst_put_u64 buf p get meta 1 92 end 93 if ge nd 2 94 p = tst_put_u64 buf p get meta 2 95 end 96 p = tst_put_u32 buf p uint get meta 3 97 ret tst_put_u64 buf p get meta 4 98 end 99 100 fn tst_align32 buf edit uchar * pos ulong to ulong 101 p = pos 102 while ne rem p 32 0 bound 32 103 put buf p uchar 0 104 p = add p 1 105 end 106 ret p 107 end 108 109 fn tst_rng_next st edit ulong 1 to ulong 110 x = get st 0 111 x = xor x shr x 12 112 x = xor x shl x 25 113 x = xor x shr x 27 114 put st 0 x 115 ret mul x ulong 0x2545f4914f6cdd1d 116 end 117 118 fn tst_rng_byte st edit ulong 1 to uchar 119 ret uchar and shr tst_rng_next st 32 0xff 120 end 121 122 fn tst_rng_unit st edit ulong 1 to f32 123 u = shr tst_rng_next st 40 124 ret sub mul div f32 u `16777216`.0f32 2.0f32 1.0f32 125 end 126 127 fn tst_lcg st edit ulong 1 to f32 128 s = add mul get st 0 ulong `6364136223846793005` ulong `1442695040888963407` 129 put st 0 s 130 u = shr s 33 131 ret sub mul div f32 u `2147483648`.0f32 2.0f32 1.0f32 132 end 133 134 fn tst_rel a f32 b f32 to f32 135 ret div fabs sub a b fmax fabs b 1.0e-30f32 136 end 137 138 note ======================================================================== 139 note 1. Half precision tests (exit 1..15) 140 note ======================================================================== 141 142 fn test_half to int 143 if ne inf_f16_to_f32 ushort 0x0000 0.0f32 144 ret 1 145 end 146 if ne inf_f16_to_f32 ushort 0x3c00 1.0f32 147 ret 2 148 end 149 if ne inf_f16_to_f32 ushort 0xc000 -2.0f32 150 ret 3 151 end 152 if ne inf_f16_to_f32 ushort 0x3800 0.5f32 153 ret 4 154 end 155 if ne inf_f16_to_f32 ushort 0x7bff 65504.0f32 156 ret 5 157 end 158 if ne bitcast uint inf_f16_to_f32 ushort 0x0400 uint 0x38800000 159 ret 6 160 end 161 if ne bitcast uint inf_f16_to_f32 ushort 0x0001 uint 0x33800000 162 ret 7 163 end 164 if ne inf_f16_to_f32 ushort 0x03ff div 1023.0f32 `16777216`.0f32 165 ret 8 166 end 167 if ne bitcast uint inf_f16_to_f32 ushort 0x7c00 uint 0x7f800000 168 ret 9 169 end 170 if ne bitcast uint inf_f16_to_f32 ushort 0xfc00 uint 0xff800000 171 ret 10 172 end 173 nanv = inf_f16_to_f32 ushort 0x7e00 174 if eq nanv nanv 175 ret 11 176 end 177 if ne bitcast uint inf_f16_to_f32 ushort 0x8000 uint 0x80000000 178 ret 12 179 end 180 if ne inf_bf16_to_f32 ushort 0x3f80 1.0f32 181 ret 13 182 end 183 if ne inf_bf16_to_f32 ushort 0xc000 -2.0f32 184 ret 14 185 end 186 if ne inf_f32_to_f16 1.5f32 ushort 0x3e00 187 ret 15 188 end 189 ret 0 190 end 191 192 note ======================================================================== 193 note 2. Math + NN primitives + RoPE tests (exit 20..39) 194 note ======================================================================== 195 196 fn tst_dot_f32 a view f32 * b view f32 * to f32 197 n = len a 198 acc = f32 0.0f32 199 for i ulong n bound 1024 200 acc = add acc mul get a i get b i 201 end 202 ret acc 203 end 204 205 fn tst_norm2_f32 a view f32 * to f32 206 n = len a 207 acc = f32 0.0f32 208 for i ulong n bound 1024 209 v = get a i 210 acc = add acc mul v v 211 end 212 ret acc 213 end 214 215 fn test_math_core to int 216 if ne inf_sqrt -1.0f32 0.0f32 217 ret 20 218 end 219 xs = new f32 6 0.0f32 220 put xs 0 1.0e-8f32 221 put xs 1 0.25f32 222 put xs 2 1.0f32 223 put xs 3 2.0f32 224 put xs 4 1234.5f32 225 put xs 5 1.0e9f32 226 for i ulong 6 227 v = get xs i 228 if ge tst_rel inf_sqrt v fsqrt v 2.0e-7f32 229 ret 21 230 end 231 end 232 if gt fabs sub inf_exp 0.0f32 1.0f32 1.0e-6f32 233 ret 22 234 end 235 if gt fabs sub inf_exp 1.0f32 2.`7182818f32` 2.0e-6f32 236 ret 23 237 end 238 if gt fabs sub inf_ln 2.0f32 0.`69314718f32` 2.0e-6f32 239 ret 24 240 end 241 note exp(ln(x)) == x across decades 242 lx = f32 1.0e-4f32 243 for i ulong 40 244 rt = inf_exp inf_ln lx 245 if gt tst_rel rt lx 5.0e-6f32 246 ret 25 247 end 248 lx = mul lx 1.6f32 249 end 250 note Llama-3.2 rope theta scale powf(500000, -2/64) 251 ts = inf_powf 500000.0f32 div -2.0f32 64.0f32 252 if gt tst_rel ts 0.`66360124f32` 2.0e-6f32 253 ret 26 254 end 255 note sin_cos unit norm and known points 256 sc = new f32 2 0.0f32 257 ang = f32 -50.0f32 258 for i ulong 150 259 inf_sin_cos ang sc 260 s = get sc 0 261 c = get sc 1 262 if gt fabs sub add mul s s mul c c 1.0f32 1.0e-5f32 263 ret 27 264 end 265 ang = add ang 4.37f32 266 end 267 inf_sin_cos INF_FRAC_PI_2 sc 268 if gt fabs sub get sc 0 1.0f32 2.0e-5f32 269 ret 28 270 end 271 if gt fabs get sc 1 2.0e-5f32 272 ret 28 273 end 274 note silu 275 if gt fabs inf_silu 0.0f32 1.0e-6f32 276 ret 29 277 end 278 if gt fabs sub inf_silu 1.0f32 0.`7310586f32` 2.0e-6f32 279 ret 29 280 end 281 note rms_norm_inplace 282 rx = new f32 2 0.0f32 283 rw = new f32 2 0.0f32 284 put rx 0 3.0f32 285 put rx 1 4.0f32 286 put rw 0 1.0f32 287 put rw 1 2.0f32 288 inf_rms_norm_inplace rx rw 0.0f32 289 inv_r = div 1.0f32 fsqrt 12.5f32 290 if gt fabs sub get rx 0 mul 3.0f32 inv_r 1.0e-5f32 291 ret 30 292 end 293 if gt fabs sub get rx 1 mul 8.0f32 inv_r 1.0e-5f32 294 ret 30 295 end 296 note softmax shift invariance + argmax 297 sm1 = new f32 4 0.0f32 298 sm2 = new f32 4 0.0f32 299 put sm1 0 1.0f32 300 put sm1 1 3.0f32 301 put sm1 2 3.0f32 302 put sm1 3 -2.0f32 303 if ne inf_argmax sm1 4 uint 1 304 ret 31 305 end 306 for i ulong 4 307 put sm2 i add get sm1 i 100.0f32 308 end 309 inf_softmax sm1 4 310 inf_softmax sm2 4 311 ssum = f32 0.0f32 312 for i ulong 4 313 ssum = add ssum get sm1 i 314 if gt fabs sub get sm1 i get sm2 i 1.0e-5f32 315 ret 32 316 end 317 end 318 if gt fabs sub ssum 1.0f32 1.0e-5f32 319 ret 32 320 end 321 ret 0 322 end 323 324 fn tst_rot8 src view f32 8 pos ulong dst edit f32 8 to void 325 cs = new f32 8 0.0f32 326 empty = new f32 1 0.0f32 327 inf_rope_cache pos 10000.0f32 part empty 0 0 cs 328 for i ulong 8 329 put dst i get src i 330 end 331 inf_rope_apply dst 8 cs 332 end 333 334 fn test_rope to int 335 note Pair 0 rotates by position angle; pos 0 is identity 336 x = new f32 8 0.0f32 337 y = new f32 8 0.0f32 338 put x 0 1.0f32 339 tst_rot8 x 1 y 340 sc = new f32 2 0.0f32 341 inf_sin_cos 1.0f32 sc 342 if gt fabs sub get y 0 get sc 1 1.0e-5f32 343 ret 33 344 end 345 if gt fabs sub get y 1 get sc 0 1.0e-5f32 346 ret 33 347 end 348 tst_rot8 x 0 y 349 if ne get y 0 1.0f32 350 ret 34 351 end 352 if ne get y 1 0.0f32 353 ret 34 354 end 355 note Relative position invariance & norm preservation 356 seed = new ulong 1 0x5eed 357 q = new f32 8 0.0f32 358 k = new f32 8 0.0f32 359 rq = new f32 8 0.0f32 360 rk = new f32 8 0.0f32 361 for i ulong 8 362 put q i tst_lcg seed 363 put k i tst_lcg seed 364 end 365 tst_rot8 q 11 rq 366 tst_rot8 k 11 rk 367 if gt fabs sub tst_dot_f32 rq rk tst_dot_f32 q k 1.0e-4f32 368 ret 35 369 end 370 tst_rot8 q 5 rq 371 tst_rot8 k 2 rk 372 da = tst_dot_f32 rq rk 373 tst_rot8 q 13 rq 374 tst_rot8 k 10 rk 375 db = tst_dot_f32 rq rk 376 tst_rot8 q 5 rq 377 tst_rot8 k 3 rk 378 dc = tst_dot_f32 rq rk 379 if gt fabs sub da db 1.0e-4f32 380 ret 36 381 end 382 if le fabs sub da dc 1.0e-3f32 383 ret 37 384 end 385 tst_rot8 q 97 rq 386 if gt fabs sub tst_norm2_f32 rq tst_norm2_f32 q 1.0e-4f32 387 ret 38 388 end 389 ret 0 390 end 391 392 note ======================================================================== 393 note 3. Quantization tests: Q4_K, Q6_K, Q8_0 (exit 40..55) 394 note ======================================================================== 395 396 fn test_quant to int 397 note q4k_hand_block 398 b4 = new uchar 144 0 399 put b4 0 uchar 0x00 400 put b4 1 uchar 0x3c 401 put b4 2 uchar 0x00 402 put b4 3 uchar 0x3c 403 for j ulong 4 404 put b4 add 4 j uchar add j 1 405 put b4 add 8 j uchar j 406 end 407 for j ulong 4 408 sb = add j 4 409 put b4 add 12 j or uchar add sb 1 shl uchar sb 4 410 end 411 for l ulong 128 412 lo = uchar rem l 16 413 put b4 add 16 l or lo shl sub uchar 15 lo 4 414 end 415 w = new f32 256 0.0f32 416 inf_dequant_q4k_block b4 0 w 0 417 for sb ulong 8 418 sc = f32 add sb 1 419 mn = f32 sb 420 for l ulong 32 421 lo = f32 rem l 16 422 qv = lo 423 if ne rem sb 2 0 424 qv = sub 15.0f32 lo 425 end 426 want = sub mul sc qv mn 427 if ne get w add mul 32 sb l want 428 ret 40 429 end 430 end 431 end 432 note q4k_scale_extension_bits 433 b4e = new uchar 144 0 434 put b4e 0 uchar 0x00 435 put b4e 1 uchar 0x3c 436 put b4e 2 uchar 0x00 437 put b4e 3 uchar 0x3c 438 put b4e 4 shl uchar 3 6 439 put b4e 8 shl uchar 2 6 440 put b4e 12 or uchar 0x0f shl uchar 2 4 441 for l ulong 128 442 put b4e add 16 l uchar 0x11 443 end 444 inf_dequant_q4k_block b4e 0 w 0 445 if ne get w 128 29.0f32 446 ret 41 447 end 448 if ne get w 0 0.0f32 449 ret 42 450 end 451 note q6k_hand_block 452 b6 = new uchar 210 0 453 put b6 208 uchar 0x00 454 put b6 209 uchar 0x3c 455 for i ulong 16 456 put b6 add 192 i uchar 1 457 end 458 inf_dequant_q6k_block b6 0 w 0 459 for i ulong 256 460 if ne get w i -32.0f32 461 ret 43 462 end 463 end 464 put b6 0 uchar 0x5a 465 put b6 128 uchar 0xe4 466 inf_dequant_q6k_block b6 0 w 0 467 if ne get w 0 -22.0f32 468 ret 44 469 end 470 if ne get w 64 5.0f32 471 ret 44 472 end 473 if ne get w 32 -16.0f32 474 ret 44 475 end 476 if ne get w 96 16.0f32 477 ret 44 478 end 479 note q6k_scales_are_signed_and_per_16 480 b6s = new uchar 210 0 481 put b6s 208 uchar 0x00 482 put b6s 209 uchar 0x3c 483 for i ulong 16 484 sv = sub int i 8 485 if lt sv 0 486 put b6s add 192 i uchar add sv 256 487 else 488 put b6s add 192 i uchar sv 489 end 490 end 491 inf_dequant_q6k_block b6s 0 w 0 492 for p ulong 256 493 want = add mul f32 div p 16 -32.0f32 256.0f32 494 if ne get w p want 495 ret 45 496 end 497 end 498 note q8_0_hand_block + dot_row 499 b8 = new uchar 34 0 500 put b8 0 uchar 0x00 501 put b8 1 uchar 0x38 502 for l ulong 32 503 sv = sub int l 16 504 if lt sv 0 505 put b8 add 2 l uchar add sv 256 506 else 507 put b8 add 2 l uchar sv 508 end 509 end 510 w8 = new f32 32 0.0f32 511 if ne inf_dequantize INF_GGML_Q8_0 b8 w8 INF_OK 512 ret 46 513 end 514 for l ulong 32 515 want = mul 0.5f32 f32 sub int l 16 516 if ne get w8 l want 517 ret 47 518 end 519 end 520 x8 = new f32 32 1.0f32 521 res = new f32 1 0.0f32 522 if ne inf_dot_row INF_GGML_Q8_0 b8 x8 res INF_OK 523 ret 48 524 end 525 if ne get res 0 -8.0f32 526 ret 49 527 end 528 note Length & unsupported checks 529 if ne inf_dequantize INF_GGML_Q4K part b4 0 143 w INF_QUANT_BAD_LEN 530 ret 50 531 end 532 b5 = new uchar 176 0 533 if ne inf_dequantize INF_GGML_Q5K b5 w INF_QUANT_UNSUPPORTED 534 ret 51 535 end 536 b4z = new uchar 144 0 537 x256 = new f32 256 1.0f32 538 if ne inf_dot_row INF_GGML_Q4K b4z x256 res INF_OK 539 ret 52 540 end 541 if ne get res 0 0.0f32 542 ret 53 543 end 544 ret 0 545 end 546 547 note ======================================================================== 548 note 4. Dotprod Q8_K quantization & integer dot tests (exit 60..75) 549 note ======================================================================== 550 551 fn tst_fill_q4k st edit ulong 1 fill uint blk edit uchar 144 to void 552 for i ulong 144 553 put blk i tst_rng_byte st 554 end 555 if eq fill 1 556 for i ulong 12 557 put blk add 4 i uchar 0xff 558 end 559 end 560 if eq fill 2 561 for i ulong 12 562 put blk add 4 i uchar 0 563 end 564 end 565 if eq fill 3 566 for i ulong 140 567 put blk add 4 i uchar 0xff 568 end 569 end 570 if eq fill 4 571 for i ulong 140 572 put blk add 4 i uchar 0 573 end 574 end 575 d0 = mul 0.`0625f32` f32 add 1 rem ulong tst_rng_byte st 8 576 d1 = mul 0.`03125f32` f32 add 1 rem ulong tst_rng_byte st 8 577 h0 = inf_f32_to_f16 d0 578 h1 = inf_f32_to_f16 d1 579 put blk 0 uchar and h0 ushort 0xff 580 put blk 1 uchar shr h0 8 581 put blk 2 uchar and h1 ushort 0xff 582 put blk 3 uchar shr h1 8 583 end 584 585 fn tst_fill_q6k st edit ulong 1 fill uint blk edit uchar 210 to void 586 for i ulong 210 587 put blk i tst_rng_byte st 588 end 589 if eq fill 1 590 for i ulong 16 591 if eq rem i 2 0 592 put blk add 192 i uchar 127 593 else 594 put blk add 192 i uchar 0x80 595 end 596 end 597 end 598 if eq fill 2 599 for i ulong 16 600 put blk add 192 i uchar 0 601 end 602 end 603 if eq fill 3 604 for i ulong 192 605 put blk i uchar 0xff 606 end 607 end 608 if eq fill 4 609 for i ulong 192 610 put blk i uchar 0 611 end 612 end 613 d0 = mul 0.`0625f32` f32 add 1 rem ulong tst_rng_byte st 8 614 h0 = inf_f32_to_f16 d0 615 put blk 208 uchar and h0 ushort 0xff 616 put blk 209 uchar shr h0 8 617 end 618 619 fn test_dotprod to int 620 note Capability toggle 621 inf_set_cpu_dotprod false 622 if inf_cpu_dotprod 623 ret 60 624 end 625 inf_set_cpu_dotprod true 626 if not inf_cpu_dotprod 627 ret 61 628 end 629 inf_set_cpu_dotprod false 630 note quantize_matches_q8k_round_and_sums 631 rng = new ulong 1 0x123456789abcdef1 632 x = new f32 512 0.0f32 633 xr = new f32 512 0.0f32 634 for i ulong 512 635 v = mul tst_rng_unit rng 3.0f32 636 put x i v 637 put xr i v 638 end 639 if ne inf_q8_quantize x 512 INF_OK 640 ret 62 641 end 642 inf_q8k_round xr 512 643 for i ulong 512 644 if ne bitcast uint inf_q8_value i bitcast uint get xr i 645 ret 63 646 end 647 end 648 note all-zero block 649 xz = new f32 256 0.0f32 650 if ne inf_q8_quantize xz 256 INF_OK 651 ret 64 652 end 653 if ne inf_q8_value 0 0.0f32 654 ret 64 655 end 656 note bad length 657 x100 = new f32 100 0.0f32 658 if ne inf_q8_quantize x100 100 INF_QUANT_BAD_LEN 659 ret 65 660 end 661 note q4k_hand_computed_scale_application 662 bh = new uchar 144 0 663 put bh 0 uchar 0x00 664 put bh 1 uchar 0x3c 665 put bh 2 uchar 0x00 666 put bh 3 uchar 0x3c 667 for j ulong 4 668 put bh add 4 j uchar add j 1 669 put bh add 8 j uchar j 670 end 671 for j ulong 4 672 sb = add j 4 673 put bh add 12 j or uchar add sb 1 shl uchar sb 4 674 end 675 for l ulong 128 676 put bh add 16 l uchar 0x33 677 end 678 xone = new f32 256 1.0f32 679 if ne inf_q8_quantize xone 256 INF_OK 680 ret 66 681 end 682 acc = new int 2 0 683 inf_q4k_accum bh 0 0 acc 684 note sumi = 36 * 3 * 32 * -127 = -438912, mins = 28 * 32 * -127 = -113792 685 if ne get acc 0 int -438912 686 ret 67 687 end 688 if ne get acc 1 int -113792 689 ret 68 690 end 691 got_h = inf_dot_q4k_q8 bh 0 0 692 note truth = sum_{s=0..7} ((s+1)*3 - s)*32 = (108 - 28)*32 = 2560 693 if gt fabs sub got_h 2560.0f32 1.0e-2f32 694 ret 69 695 end 696 ret 0 697 end 698 699 fn test_dotprod_blocks to int 700 note Q4_K and Q6_K integer dot matches f32 dot of rounded activations across all 5 fills 701 rng = new ulong 1 0xdeadbeef0001 702 b4 = new uchar 144 0 703 b6 = new uchar 210 0 704 w = new f32 256 0.0f32 705 xa = new f32 256 0.0f32 706 for n ulong 25 707 tst_fill_q4k rng uint rem n 5 b4 708 tst_fill_q6k rng uint rem n 5 b6 709 for i ulong 256 710 put xa i mul tst_rng_unit rng 3.0f32 711 end 712 if ne inf_q8_quantize xa 256 INF_OK 713 ret 70 714 end 715 inf_q8k_round xa 256 716 inf_dequant_q4k_block b4 0 w 0 717 want4 = tst_dot_f32 w xa 718 got4 = inf_dot_q4k_q8 b4 0 0 719 if gt fabs sub got4 want4 5.0e-2f32 720 ret 71 721 end 722 inf_dequant_q6k_block b6 0 w 0 723 want6 = tst_dot_f32 w xa 724 got6 = inf_dot_q6k_q8 b6 0 0 725 if gt fabs sub got6 want6 5.0e-2f32 726 ret 72 727 end 728 end 729 note Multi-block row dot 730 res_q8 = new f32 1 0.0f32 731 res_f32 = new f32 1 0.0f32 732 if ne inf_q8_quantize xa 256 INF_OK 733 ret 73 734 end 735 if ne inf_dot_row_q8 INF_GGML_Q4K b4 res_q8 INF_OK 736 ret 73 737 end 738 if ne inf_dot_row INF_GGML_Q4K b4 xa res_f32 INF_OK 739 ret 73 740 end 741 if gt fabs sub get res_q8 0 get res_f32 0 5.0e-2f32 742 ret 74 743 end 744 if ne inf_dot_row_q8 INF_GGML_F32 b4 res_q8 INF_QUANT_UNSUPPORTED 745 ret 75 746 end 747 ret 0 748 end 749 750 note ======================================================================== 751 note 5. GGUF v2/v3 & hostile-input tests (exit 80..98) 752 note ======================================================================== 753 754 text tst_k_ab "a.b" 755 text tst_k_toks "tokens" 756 text tst_k_nope "nope" 757 text tst_s_hi "hi" 758 text tst_s_yo "yo" 759 text tst_t_w "w" 760 text tst_k_k "k" 761 762 fn tst_build_small buf edit uchar * ver uint to ulong 763 p = tst_gguf_hdr buf ver 1 2 764 p = tst_kv_u32 buf p tst_k_ab 7 765 p = tst_put_str buf p tst_k_toks 766 p = tst_put_u32 buf p INF_VT_ARRAY 767 p = tst_put_u32 buf p INF_VT_STR 768 p = tst_put_u64 buf p 2 769 p = tst_put_str buf p tst_s_hi 770 p = tst_put_str buf p tst_s_yo 771 meta = new ulong 5 0 772 put meta 0 1 773 put meta 1 256 774 put meta 3 ulong INF_GGML_Q4K 775 put meta 4 0 776 p = tst_put_tensor buf p tst_t_w meta 777 p = tst_align32 buf p 778 for i ulong 144 779 put buf add p i uchar 0 780 end 781 ret add p 144 782 end 783 784 fn test_gguf to int 785 buf = new uchar 512 0 786 g = new InfGguf 0 0 0 0 0 0 787 u32b = new uint 1 0 788 note Valid v3 789 flen = tst_build_small buf 3 790 if ne inf_gguf_parse part buf 0 flen g INF_OK 791 ret 80 792 end 793 if ne get g version 3 794 ret 81 795 end 796 if ne inf_gguf_u32_key g part buf 0 flen tst_k_ab u32b INF_OK 797 ret 82 798 end 799 if ne get u32b 0 7 800 ret 82 801 end 802 if ne inf_gguf_u32_key g part buf 0 flen tst_k_nope u32b INF_GGUF_MISSING 803 ret 83 804 end 805 ti = inf_gguf_find_tensor g part buf 0 flen tst_t_w 806 if lt ti 0 807 ret 84 808 end 809 if ne get inf_ten_nbytes ulong ti 144 810 ret 84 811 end 812 note Every truncation 0..flen-1 must fail cleanly 813 for n ulong flen bound 512 814 if eq inf_gguf_parse part buf 0 n g INF_OK 815 ret 85 816 end 817 end 818 note Valid v2 accepted by inf_gguf_parse, rejected by inf_gguf_parse_v3 819 flen2 = tst_build_small buf 2 820 if ne inf_gguf_parse part buf 0 flen2 g INF_OK 821 ret 86 822 end 823 if ne get g version 2 824 ret 86 825 end 826 if ne inf_gguf_parse_v3 part buf 0 flen2 g INF_GGUF_BAD_VERSION 827 ret 87 828 end 829 note Unsupported versions 1 and 4 830 flen1 = tst_build_small buf 1 831 if ne inf_gguf_parse part buf 0 flen1 g INF_GGUF_BAD_VERSION 832 ret 88 833 end 834 flen4 = tst_build_small buf 4 835 if ne inf_gguf_parse part buf 0 flen4 g INF_GGUF_BAD_VERSION 836 ret 88 837 end 838 note Bad magic 839 flen = tst_build_small buf 3 840 put buf 0 uchar 88 841 if ne inf_gguf_parse part buf 0 flen g INF_GGUF_BAD_MAGIC 842 ret 89 843 end 844 note TooMany counts 845 p = tst_gguf_hdr buf 3 ulong 0xffffffffffffffff 0 846 if ne inf_gguf_parse part buf 0 p g INF_GGUF_TOO_MANY 847 ret 90 848 end 849 note Misaligned tensor offset 850 meta = new ulong 5 0 851 p = tst_gguf_hdr buf 3 1 0 852 put meta 0 1 853 put meta 1 256 854 put meta 3 ulong INF_GGML_Q4K 855 put meta 4 16 856 p = tst_put_tensor buf p tst_t_w meta 857 p = tst_align32 buf p 858 p = add p 256 859 if ne inf_gguf_parse part buf 0 p g INF_GGUF_TENSOR_OOB 860 ret 91 861 end 862 note Bad alignment (0 and 3) 863 p = tst_gguf_hdr buf 3 0 1 864 p = tst_kv_u32 buf p inf_key_alignment 0 865 p = tst_align32 buf p 866 if ne inf_gguf_parse part buf 0 p g INF_GGUF_BAD_ALIGN 867 ret 92 868 end 869 p = tst_gguf_hdr buf 3 0 1 870 p = tst_kv_u32 buf p inf_key_alignment 3 871 p = tst_align32 buf p 872 if ne inf_gguf_parse part buf 0 p g INF_GGUF_BAD_ALIGN 873 ret 93 874 end 875 note Bad dims (row not multiple of block) 876 p = tst_gguf_hdr buf 3 1 0 877 put meta 0 1 878 put meta 1 255 879 put meta 3 ulong INF_GGML_Q4K 880 put meta 4 0 881 p = tst_put_tensor buf p tst_t_w meta 882 p = tst_align32 buf p 883 if ne inf_gguf_parse part buf 0 p g INF_GGUF_BAD_DIMS 884 ret 94 885 end 886 note Unknown tensor type 99 887 p = tst_gguf_hdr buf 3 1 0 888 put meta 0 1 889 put meta 1 32 890 put meta 3 99 891 put meta 4 0 892 p = tst_put_tensor buf p tst_t_w meta 893 p = tst_align32 buf p 894 if ne inf_gguf_parse part buf 0 p g INF_GGUF_BAD_TYPE 895 ret 95 896 end 897 note Unknown KV value type 77 898 p = tst_gguf_hdr buf 3 0 1 899 p = tst_put_str buf p tst_k_k 900 p = tst_put_u32 buf p 77 901 if ne inf_gguf_parse part buf 0 p g INF_GGUF_BAD_VTYPE 902 ret 96 903 end 904 note Invalid UTF-8 key 905 p = tst_gguf_hdr buf 3 0 1 906 p = tst_put_u64 buf p 2 907 put buf p uchar 0xff 908 put buf add p 1 uchar 0xfe 909 p = add p 2 910 p = tst_put_u32 buf p INF_VT_U32 911 p = tst_put_u32 buf p 0 912 if ne inf_gguf_parse part buf 0 p g INF_GGUF_BAD_UTF8 913 ret 97 914 end 915 ret 0 916 end 917 918 note ======================================================================== 919 note 6. Tokenizer tests: GPT-2 byte map, pretokenizer, BPE, chat, decode (exit 100..118) 920 note ======================================================================== 921 922 text tst_pt_hw "Hello world" 923 text tst_pt_con "it's I'M we'LL" 924 text tst_pt_dig "a1234b" 925 text tst_pt_ws "x y" 926 text tst_pt_nl "end.\n\nnext" 927 928 text tst_tok_bos "<|begin_of_text|>" 929 text tst_tok_sh "<|start_header_id|>" 930 text tst_tok_eh "<|end_header_id|>" 931 text tst_tok_eot "<|eot_id|>" 932 text tst_tok_user "user" 933 text tst_tok_asst "assistant" 934 text tst_tok_h "h" 935 text tst_tok_i "i" 936 text tst_tok_hi "hi" 937 text tst_tok_ex "!" 938 text tst_tok_hiex "hi!" 939 text tst_mrg_hi "h i" 940 text tst_mrg_hiex "hi !" 941 text tst_msg_hi "hi!" 942 text tst_msg_dec "hi!" 943 944 fn tst_put_tok_str buf edit uchar * pos ulong cp uint to ulong 945 tmp = new uchar 4 0 946 n = inf_utf8_encode cp tmp 0 947 p = tst_put_u64 buf pos n 948 for i ulong n bound 4 949 put buf add p i get tmp i 950 end 951 ret add p n 952 end 953 954 fn test_tokenizer to int 955 note GPT-2 byte map checks + full 0..255 round trip 956 if ne inf_byte_to_cp uchar 65 uint 65 957 ret 100 958 end 959 if ne inf_byte_to_cp uchar 32 uint 0x0120 960 ret 100 961 end 962 if ne inf_byte_to_cp uchar 10 uint 0x010a 963 ret 100 964 end 965 if ne inf_byte_to_cp uchar 0 uint 0x0100 966 ret 100 967 end 968 if ne inf_byte_to_cp uchar 127 uint 0x0121 969 ret 100 970 end 971 if ne inf_byte_to_cp uchar 173 uint 0x0143 972 ret 100 973 end 974 for b ulong 256 975 cp = inf_byte_to_cp uchar b 976 if ne inf_cp_to_byte cp int b 977 ret 101 978 end 979 end 980 note Pretokenizer spot checks 981 if ne inf_pretokenize_one tst_pt_hw 0 5 982 ret 102 983 end 984 if ne inf_pretokenize_one tst_pt_hw 5 6 985 ret 102 986 end 987 if ne inf_pretokenize_one tst_pt_con 0 2 988 ret 103 989 end 990 if ne inf_pretokenize_one tst_pt_con 2 2 991 ret 103 992 end 993 if ne inf_pretokenize_one tst_pt_dig 0 1 994 ret 104 995 end 996 if ne inf_pretokenize_one tst_pt_dig 1 3 997 ret 104 998 end 999 if ne inf_pretokenize_one tst_pt_dig 4 1 1000 ret 104 1001 end 1002 if ne inf_pretokenize_one tst_pt_ws 0 1 1003 ret 105 1004 end 1005 if ne inf_pretokenize_one tst_pt_ws 1 1 1006 ret 105 1007 end 1008 if ne inf_pretokenize_one tst_pt_ws 2 2 1009 ret 105 1010 end 1011 if ne inf_pretokenize_one tst_pt_nl 0 3 1012 ret 106 1013 end 1014 if ne inf_pretokenize_one tst_pt_nl 3 3 1015 ret 106 1016 end 1017 note Build synthetic GGUF with mini BPE vocabulary (14 tokens, 2 merges) 1018 note 0:<|begin_of_text|> (ctrl), 1:<|start_header_id|> (ctrl), 2:<|end_header_id|> (ctrl), 1019 note 3:<|eot_id|> (ctrl), 4:user, 5:assistant, 6:\n (U+010A), 7:\n\n (U+010A U+010A), 1020 note 8:h, 9:i, 10:hi, 11:!, 12:hi!, 13:space (U+0120) 1021 buf = new uchar 2048 0 1022 p = tst_gguf_hdr buf 3 0 5 1023 p = tst_put_str buf p inf_key_tokens 1024 p = tst_put_u32 buf p INF_VT_ARRAY 1025 p = tst_put_u32 buf p INF_VT_STR 1026 p = tst_put_u64 buf p 14 1027 p = tst_put_str buf p tst_tok_bos 1028 p = tst_put_str buf p tst_tok_sh 1029 p = tst_put_str buf p tst_tok_eh 1030 p = tst_put_str buf p tst_tok_eot 1031 p = tst_put_str buf p tst_tok_user 1032 p = tst_put_str buf p tst_tok_asst 1033 p = tst_put_tok_str buf p 0x010a 1034 note token 7: two U+010A (4 bytes: c4 8a c4 8a) 1035 p = tst_put_u64 buf p 4 1036 put buf p uchar 0xc4 1037 put buf add p 1 uchar 0x8a 1038 put buf add p 2 uchar 0xc4 1039 put buf add p 3 uchar 0x8a 1040 p = add p 4 1041 p = tst_put_str buf p tst_tok_h 1042 p = tst_put_str buf p tst_tok_i 1043 p = tst_put_str buf p tst_tok_hi 1044 p = tst_put_str buf p tst_tok_ex 1045 p = tst_put_str buf p tst_tok_hiex 1046 p = tst_put_tok_str buf p 0x0120 1047 note token_type array (14 i32s: 0..3 are 3=CONTROL, 4..13 are 1=NORMAL) 1048 p = tst_put_str buf p inf_key_tok_type 1049 p = tst_put_u32 buf p INF_VT_ARRAY 1050 p = tst_put_u32 buf p INF_VT_I32 1051 p = tst_put_u64 buf p 14 1052 for i ulong 14 1053 if lt i 4 1054 p = tst_put_u32 buf p 3 1055 else 1056 p = tst_put_u32 buf p 1 1057 end 1058 end 1059 note merges array: "h i", "hi !" 1060 p = tst_put_str buf p inf_key_tok_merges 1061 p = tst_put_u32 buf p INF_VT_ARRAY 1062 p = tst_put_u32 buf p INF_VT_STR 1063 p = tst_put_u64 buf p 2 1064 p = tst_put_str buf p tst_mrg_hi 1065 p = tst_put_str buf p tst_mrg_hiex 1066 p = tst_kv_u32 buf p inf_key_bos 0 1067 p = tst_kv_u32 buf p inf_key_eos 3 1068 p = tst_align32 buf p 1069 g = new InfGguf 0 0 0 0 0 0 1070 if ne inf_gguf_parse part buf 0 p g INF_OK 1071 ret 107 1072 end 1073 ts = new InfTokState 0 0 0 0 0 0 0 0 1074 if ne inf_tok_init g part buf 0 p ts INF_OK 1075 ret 108 1076 end 1077 if ne get ts vocab_size 14 1078 ret 109 1079 end 1080 if ne get ts eot_id 3 1081 ret 110 1082 end 1083 note Encode "hi!" -> pretokens ["hi", "!"] -> [10, 11] 1084 cnt = inf_tok_encode part buf 0 p tst_msg_hi false 1085 if ne cnt 2 1086 ret 111 1087 end 1088 if ne inf_tok_get_id 0 10 1089 ret 112 1090 end 1091 if ne inf_tok_get_id 1 11 1092 ret 112 1093 end 1094 note Encode chat for user "hi!" -> [0, 1, 4, 2, 7, 10, 11, 3, 1, 5, 2, 7] (12 tokens) 1095 ccnt = inf_tok_encode_chat part buf 0 p tst_msg_hi 1096 if ne ccnt 12 1097 ret 113 1098 end 1099 if ne inf_tok_get_id 0 0 1100 ret 114 1101 end 1102 if ne inf_tok_get_id 1 1 1103 ret 114 1104 end 1105 if ne inf_tok_get_id 2 4 1106 ret 114 1107 end 1108 if ne inf_tok_get_id 3 2 1109 ret 114 1110 end 1111 if ne inf_tok_get_id 4 7 1112 ret 114 1113 end 1114 if ne inf_tok_get_id 5 10 1115 ret 114 1116 end 1117 if ne inf_tok_get_id 6 11 1118 ret 114 1119 end 1120 if ne inf_tok_get_id 7 3 1121 ret 114 1122 end 1123 if ne inf_tok_get_id 9 5 1124 ret 114 1125 end 1126 note Decode [0, 10, 11, 3] -> skips control tokens 0 and 3 -> "hi!" 1127 ids = new uint 4 0 1128 put ids 0 0 1129 put ids 1 10 1130 put ids 2 11 1131 put ids 3 3 1132 dbuf = new uchar 64 0 1133 dlen = inf_tok_decode part buf 0 p ids dbuf 1134 if ne dlen 3 1135 ret 115 1136 end 1137 if not inf_slice_eq_text dbuf 0 3 tst_msg_dec 1138 ret 116 1139 end 1140 ret 0 1141 end 1142 1143 note ======================================================================== 1144 note 7a. Tiny synthetic F32 GQA Llama model forward + greedy decode (exit 120..132) 1145 note ======================================================================== 1146 1147 fn tst_write_f32_mat buf edit uchar * base ulong n ulong rng edit ulong 1 scale f32 to ulong 1148 p = base 1149 for i ulong n bound 4096 1150 v = mul tst_rng_unit rng scale 1151 p = tst_put_f32 buf p v 1152 end 1153 ret tst_align32 buf p 1154 end 1155 1156 fn tst_write_f32_ones buf edit uchar * base ulong n ulong to ulong 1157 p = base 1158 for i ulong n bound 1024 1159 p = tst_put_f32 buf p 1.0f32 1160 end 1161 ret tst_align32 buf p 1162 end 1163 1164 fn tst_align_off off ulong to ulong 1165 ret and add off 31 not ulong 31 1166 end 1167 1168 fn tst_build_f32_model buf edit uchar * to ulong 1169 nbuf = new uchar 64 0 1170 meta = new ulong 5 0 1171 p = tst_gguf_hdr buf 3 20 10 1172 p = tst_put_str buf p inf_key_arch 1173 p = tst_put_u32 buf p INF_VT_STR 1174 p = tst_put_str buf p inf_val_llama 1175 p = tst_kv_u32 buf p inf_key_blocks 2 1176 p = tst_kv_u32 buf p inf_key_embd 8 1177 p = tst_kv_u32 buf p inf_key_heads 4 1178 p = tst_kv_u32 buf p inf_key_kv_heads 2 1179 p = tst_kv_u32 buf p inf_key_ffn 12 1180 p = tst_kv_u32 buf p inf_key_ctx 8 1181 p = tst_kv_u32 buf p inf_key_vocab 8 1182 p = tst_kv_f32 buf p inf_key_rope_base 10000.0f32 1183 p = tst_kv_f32 buf p inf_key_rms_eps 1.0e-5f32 1184 off = ulong 0 1185 tst_meta2 meta 8 8 INF_GGML_F32 off 1186 p = tst_put_tensor buf p inf_nm_tok_embd meta 1187 off = tst_align_off add off 256 1188 for l ulong 2 1189 nlen = inf_fmt_blk_name uint l inf_sfx_attn_norm nbuf 1190 tst_meta1 meta 8 INF_GGML_F32 off 1191 p = tst_put_tensor buf p part nbuf 0 nlen meta 1192 off = tst_align_off add off 32 1193 nlen = inf_fmt_blk_name uint l inf_sfx_attn_q nbuf 1194 tst_meta2 meta 8 8 INF_GGML_F32 off 1195 p = tst_put_tensor buf p part nbuf 0 nlen meta 1196 off = tst_align_off add off 256 1197 nlen = inf_fmt_blk_name uint l inf_sfx_attn_k nbuf 1198 tst_meta2 meta 8 4 INF_GGML_F32 off 1199 p = tst_put_tensor buf p part nbuf 0 nlen meta 1200 off = tst_align_off add off 128 1201 nlen = inf_fmt_blk_name uint l inf_sfx_attn_v nbuf 1202 tst_meta2 meta 8 4 INF_GGML_F32 off 1203 p = tst_put_tensor buf p part nbuf 0 nlen meta 1204 off = tst_align_off add off 128 1205 nlen = inf_fmt_blk_name uint l inf_sfx_attn_out nbuf 1206 tst_meta2 meta 8 8 INF_GGML_F32 off 1207 p = tst_put_tensor buf p part nbuf 0 nlen meta 1208 off = tst_align_off add off 256 1209 nlen = inf_fmt_blk_name uint l inf_sfx_ffn_norm nbuf 1210 tst_meta1 meta 8 INF_GGML_F32 off 1211 p = tst_put_tensor buf p part nbuf 0 nlen meta 1212 off = tst_align_off add off 32 1213 nlen = inf_fmt_blk_name uint l inf_sfx_ffn_gate nbuf 1214 tst_meta2 meta 8 12 INF_GGML_F32 off 1215 p = tst_put_tensor buf p part nbuf 0 nlen meta 1216 off = tst_align_off add off 384 1217 nlen = inf_fmt_blk_name uint l inf_sfx_ffn_up nbuf 1218 tst_meta2 meta 8 12 INF_GGML_F32 off 1219 p = tst_put_tensor buf p part nbuf 0 nlen meta 1220 off = tst_align_off add off 384 1221 nlen = inf_fmt_blk_name uint l inf_sfx_ffn_down nbuf 1222 tst_meta2 meta 12 8 INF_GGML_F32 off 1223 p = tst_put_tensor buf p part nbuf 0 nlen meta 1224 off = tst_align_off add off 384 1225 end 1226 tst_meta1 meta 8 INF_GGML_F32 off 1227 p = tst_put_tensor buf p inf_nm_output_norm meta 1228 p = tst_align32 buf p 1229 rng = new ulong 1 0xcafebabe12345678 1230 p = tst_write_f32_mat buf p 64 rng 0.5f32 1231 for l ulong 2 1232 p = tst_write_f32_ones buf p 8 1233 p = tst_write_f32_mat buf p 64 rng 0.3f32 1234 p = tst_write_f32_mat buf p 32 rng 0.3f32 1235 p = tst_write_f32_mat buf p 32 rng 0.3f32 1236 p = tst_write_f32_mat buf p 64 rng 0.3f32 1237 p = tst_write_f32_ones buf p 8 1238 p = tst_write_f32_mat buf p 96 rng 0.3f32 1239 p = tst_write_f32_mat buf p 96 rng 0.3f32 1240 p = tst_write_f32_mat buf p 96 rng 0.3f32 1241 end 1242 ret tst_write_f32_ones buf p 8 1243 end 1244 1245 fn test_forward_f32 to int 1246 note Tiny 2-layer GQA Llama: n_embd=8, n_head=4, n_kv=2, head_dim=2, kv_dim=4, n_ff=12, vocab=8, ctx=8 1247 buf = new uchar 16384 0 1248 p = tst_build_f32_model buf 1249 note Parse, bind, init 1250 g = new InfGguf 0 0 0 0 0 0 1251 if ne inf_gguf_parse part buf 0 p g INF_OK 1252 ret 120 1253 end 1254 m = new InfModel 0 0 0 0 0 0 0.0f32 0.0f32 0 0 0 0 0 0 0 0 1255 if ne inf_model_bind g part buf 0 p m INF_OK 1256 ret 121 1257 end 1258 st = new InfDecodeState 0 0 0 0 0 1259 if ne inf_decode_init m 8 st INF_OK 1260 ret 122 1261 end 1262 note BadToken check 1263 if ne inf_decode_forward m part buf 0 p st 99 true INF_ERR_BAD_TOKEN 1264 ret 123 1265 end 1266 note Prefill [1, 2, 3] 1267 prompt = new uint 3 0 1268 put prompt 0 1 1269 put prompt 1 2 1270 put prompt 2 3 1271 if ne inf_decode_prefill m part buf 0 p st prompt INF_OK 1272 ret 124 1273 end 1274 if ne get st pos 3 1275 ret 125 1276 end 1277 note Verify logits finite and non-zero, save snapshot 1278 snap = new f32 8 0.0f32 1279 nonzero = false 1280 for i ulong 8 1281 lv = get inf_st_logits i 1282 if ne lv lv 1283 ret 126 1284 end 1285 if gt fabs lv 1.0e-6f32 1286 nonzero = true 1287 end 1288 put snap i lv 1289 end 1290 if not nonzero 1291 ret 127 1292 end 1293 top1 = inf_decode_argmax m 1294 note Reset & rerun: must be bit-for-bit identical 1295 inf_decode_reset st 1296 if ne inf_decode_prefill m part buf 0 p st prompt INF_OK 1297 ret 128 1298 end 1299 for i ulong 8 1300 if ne bitcast uint get inf_st_logits i bitcast uint get snap i 1301 ret 129 1302 end 1303 end 1304 if ne inf_decode_argmax m top1 1305 ret 129 1306 end 1307 note Greedy generation of 3 tokens 1308 ngen = inf_generate_greedy m part buf 0 p st 3 uint 999 1309 if ne ngen 3 1310 ret 130 1311 end 1312 if ne inf_gen_get_id 0 top1 1313 ret 131 1314 end 1315 note Verify ContextFull when capacity exhausted (pos is now 5: 3 prefill + 2 forward) 1316 if ne inf_decode_forward m part buf 0 p st 0 false INF_OK 1317 ret 132 1318 end 1319 if ne inf_decode_forward m part buf 0 p st 0 false INF_OK 1320 ret 132 1321 end 1322 if ne inf_decode_forward m part buf 0 p st 0 false INF_OK 1323 ret 132 1324 end 1325 if ne inf_decode_forward m part buf 0 p st 0 true INF_ERR_CONTEXT_FULL 1326 ret 133 1327 end 1328 ret 0 1329 end 1330 1331 note ======================================================================== 1332 note 7b. Quantized Q4_K / Q6_K synthetic GGUF forward parity (exit 135..145) 1333 note ======================================================================== 1334 1335 fn tst_write_q4k_mat buf edit uchar * base ulong rows ulong rng edit ulong 1 to ulong 1336 p = base 1337 blk = new uchar 144 0 1338 for r ulong rows bound 512 1339 tst_fill_q4k rng 0 blk 1340 for i ulong 144 1341 put buf add p i get blk i 1342 end 1343 p = add p 144 1344 end 1345 ret tst_align32 buf p 1346 end 1347 1348 fn tst_write_q6k_mat buf edit uchar * base ulong rows ulong rng edit ulong 1 to ulong 1349 p = base 1350 blk = new uchar 210 0 1351 for r ulong rows bound 512 1352 tst_fill_q6k rng 0 blk 1353 for i ulong 210 1354 put buf add p i get blk i 1355 end 1356 p = add p 210 1357 end 1358 ret tst_align32 buf p 1359 end 1360 1361 fn tst_build_q4k_model buf edit uchar * to ulong 1362 nbuf = new uchar 64 0 1363 meta = new ulong 5 0 1364 p = tst_gguf_hdr buf 3 11 10 1365 p = tst_put_str buf p inf_key_arch 1366 p = tst_put_u32 buf p INF_VT_STR 1367 p = tst_put_str buf p inf_val_llama 1368 p = tst_kv_u32 buf p inf_key_blocks 1 1369 p = tst_kv_u32 buf p inf_key_embd 256 1370 p = tst_kv_u32 buf p inf_key_heads 4 1371 p = tst_kv_u32 buf p inf_key_kv_heads 2 1372 p = tst_kv_u32 buf p inf_key_ffn 256 1373 p = tst_kv_u32 buf p inf_key_ctx 4 1374 p = tst_kv_u32 buf p inf_key_vocab 4 1375 p = tst_kv_f32 buf p inf_key_rope_base 500000.0f32 1376 p = tst_kv_f32 buf p inf_key_rms_eps 1.0e-5f32 1377 off = ulong 0 1378 tst_meta2 meta 256 4 INF_GGML_Q6K off 1379 p = tst_put_tensor buf p inf_nm_tok_embd meta 1380 off = tst_align_off add off 840 1381 nlen = inf_fmt_blk_name 0 inf_sfx_attn_norm nbuf 1382 tst_meta1 meta 256 INF_GGML_F32 off 1383 p = tst_put_tensor buf p part nbuf 0 nlen meta 1384 off = tst_align_off add off 1024 1385 nlen = inf_fmt_blk_name 0 inf_sfx_attn_q nbuf 1386 tst_meta2 meta 256 256 INF_GGML_Q4K off 1387 p = tst_put_tensor buf p part nbuf 0 nlen meta 1388 off = tst_align_off add off 36864 1389 nlen = inf_fmt_blk_name 0 inf_sfx_attn_k nbuf 1390 tst_meta2 meta 256 128 INF_GGML_Q4K off 1391 p = tst_put_tensor buf p part nbuf 0 nlen meta 1392 off = tst_align_off add off 18432 1393 nlen = inf_fmt_blk_name 0 inf_sfx_attn_v nbuf 1394 tst_meta2 meta 256 128 INF_GGML_Q4K off 1395 p = tst_put_tensor buf p part nbuf 0 nlen meta 1396 off = tst_align_off add off 18432 1397 nlen = inf_fmt_blk_name 0 inf_sfx_attn_out nbuf 1398 tst_meta2 meta 256 256 INF_GGML_Q4K off 1399 p = tst_put_tensor buf p part nbuf 0 nlen meta 1400 off = tst_align_off add off 36864 1401 nlen = inf_fmt_blk_name 0 inf_sfx_ffn_norm nbuf 1402 tst_meta1 meta 256 INF_GGML_F32 off 1403 p = tst_put_tensor buf p part nbuf 0 nlen meta 1404 off = tst_align_off add off 1024 1405 nlen = inf_fmt_blk_name 0 inf_sfx_ffn_gate nbuf 1406 tst_meta2 meta 256 256 INF_GGML_Q4K off 1407 p = tst_put_tensor buf p part nbuf 0 nlen meta 1408 off = tst_align_off add off 36864 1409 nlen = inf_fmt_blk_name 0 inf_sfx_ffn_up nbuf 1410 tst_meta2 meta 256 256 INF_GGML_Q4K off 1411 p = tst_put_tensor buf p part nbuf 0 nlen meta 1412 off = tst_align_off add off 36864 1413 nlen = inf_fmt_blk_name 0 inf_sfx_ffn_down nbuf 1414 tst_meta2 meta 256 256 INF_GGML_Q4K off 1415 p = tst_put_tensor buf p part nbuf 0 nlen meta 1416 off = tst_align_off add off 36864 1417 tst_meta1 meta 256 INF_GGML_F32 off 1418 p = tst_put_tensor buf p inf_nm_output_norm meta 1419 p = tst_align32 buf p 1420 rng = new ulong 1 0x9876543210fedcba 1421 p = tst_write_q6k_mat buf p 4 rng 1422 p = tst_write_f32_ones buf p 256 1423 p = tst_write_q4k_mat buf p 256 rng 1424 p = tst_write_q4k_mat buf p 128 rng 1425 p = tst_write_q4k_mat buf p 128 rng 1426 p = tst_write_q4k_mat buf p 256 rng 1427 p = tst_write_f32_ones buf p 256 1428 p = tst_write_q4k_mat buf p 256 rng 1429 p = tst_write_q4k_mat buf p 256 rng 1430 p = tst_write_q4k_mat buf p 256 rng 1431 ret tst_write_f32_ones buf p 256 1432 end 1433 1434 fn test_forward_q4k to int 1435 note 1-layer quantized model: n_embd=256, n_head=4, n_kv=2, head_dim=64, kv_dim=128, n_ff=256, vocab=4, ctx=4 1436 buf = new uchar 262144 0 1437 p = tst_build_q4k_model buf 1438 g = new InfGguf 0 0 0 0 0 0 1439 if ne inf_gguf_parse part buf 0 p g INF_OK 1440 ret 135 1441 end 1442 m = new InfModel 0 0 0 0 0 0 0.0f32 0.0f32 0 0 0 0 0 0 0 0 1443 if ne inf_model_bind g part buf 0 p m INF_OK 1444 ret 136 1445 end 1446 st = new InfDecodeState 0 0 0 0 0 1447 if ne inf_decode_init m 4 st INF_OK 1448 ret 137 1449 end 1450 note Mode 1: emulate_q8k=1, use_dotprod=0 (f32 dot on Q8_K-rounded activations) 1451 put st emulate_q8k uint 1 1452 put st use_dotprod uint 0 1453 if ne inf_decode_forward m part buf 0 p st 1 true INF_OK 1454 ret 138 1455 end 1456 l_emu = new f32 4 0.0f32 1457 for i ulong 4 1458 lv = get inf_st_logits i 1459 if ne lv lv 1460 ret 139 1461 end 1462 put l_emu i lv 1463 end 1464 top_emu = inf_decode_argmax m 1465 note Mode 2: emulate_q8k=0, use_dotprod=1 (integer Q8_K dotprod path) 1466 inf_decode_reset st 1467 put st emulate_q8k uint 0 1468 put st use_dotprod uint 1 1469 if ne inf_decode_forward m part buf 0 p st 1 true INF_OK 1470 ret 140 1471 end 1472 for i ulong 4 1473 lv = get inf_st_logits i 1474 ev = get l_emu i 1475 if gt tst_rel lv ev 5.0e-3f32 1476 ret 141 1477 end 1478 end 1479 if ne inf_decode_argmax m top_emu 1480 ret 142 1481 end 1482 ret 0 1483 end 1484 1485 note ======================================================================== 1486 note Entry point 1487 note ======================================================================== 1488 1489 fn main to int 1490 rc = test_half 1491 if ne rc 0 1492 ret rc 1493 end 1494 rc = test_math_core 1495 if ne rc 0 1496 ret rc 1497 end 1498 rc = test_rope 1499 if ne rc 0 1500 ret rc 1501 end 1502 rc = test_quant 1503 if ne rc 0 1504 ret rc 1505 end 1506 rc = test_dotprod 1507 if ne rc 0 1508 ret rc 1509 end 1510 rc = test_dotprod_blocks 1511 if ne rc 0 1512 ret rc 1513 end 1514 rc = test_gguf 1515 if ne rc 0 1516 ret rc 1517 end 1518 rc = test_tokenizer 1519 if ne rc 0 1520 ret rc 1521 end 1522 rc = test_forward_f32 1523 if ne rc 0 1524 ret rc 1525 end 1526 rc = test_forward_q4k 1527 if ne rc 0 1528 ret rc 1529 end 1530 ret 0 1531 end