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_core.om

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