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

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