interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / aienux / crates / aienos-infer / infer_test.om

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