char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Xuan-Son Nguyenmtmd: support DeepSeek-V4-Flash-Vision-Exp (#28133)7798007a2

char/aliceai-t5-moe
17.8 KiB441 linesraw
1#pragma once
2
3#include "../clip-graph.h"
4
5#include <map>
6#include <string>
7#include <utility>
8#include <vector>
9
10/*
11 * IMPORTANT: The mtmd module does NOT accept pull requests that are fully or predominantly AI-generated.
12 * We encourage human contributors to ensure the quality and reliability of the codebase.
13 */
14
15struct clip_graph_siglip : clip_graph {
16    clip_graph_siglip(clip_ctx * ctx, const clip_image_f32 & img) : clip_graph(ctx, img) {}
17    ggml_cgraph * build() override;
18};
19
20struct clip_graph_gemma4v : clip_graph {
21    clip_graph_gemma4v(clip_ctx * ctx, const clip_image_f32 & img) : clip_graph(ctx, img) {}
22    ggml_cgraph * build() override;
23    ggml_tensor * build_mm(ggml_tensor * w, ggml_tensor * x) const override;
24    bool support_batch() const override { return true; }
25};
26
27struct clip_graph_gemma4uv : clip_graph {
28    clip_graph_gemma4uv(clip_ctx * ctx, const clip_image_f32 & img) : clip_graph(ctx, img) {}
29    ggml_cgraph * build() override;
30};
31
32struct clip_graph_pixtral : clip_graph {
33    clip_graph_pixtral(clip_ctx * ctx, const clip_image_f32 & img) : clip_graph(ctx, img) {}
34    ggml_cgraph * build() override;
35};
36
37struct clip_graph_deepseek4v : clip_graph {
38    clip_graph_deepseek4v(clip_ctx * ctx, const clip_image_f32 & img) : clip_graph(ctx, img) {}
39    ggml_cgraph * build() override;
40};
41
42struct clip_graph_qwen2vl : clip_graph {
43    clip_graph_qwen2vl(clip_ctx * ctx, const clip_image_f32 & img) : clip_graph(ctx, img) {}
44    ggml_cgraph * build() override;
45    ggml_tensor * build_inp_with_temporal_merge();
46};
47
48struct clip_graph_qwen3vl : clip_graph_qwen2vl {
49    clip_graph_qwen3vl(clip_ctx * ctx, const clip_image_f32 & img) : clip_graph_qwen2vl(ctx, img) {}
50    ggml_cgraph * build() override;
51};
52
53struct clip_graph_minimax_m3 : clip_graph {
54    clip_graph_minimax_m3(clip_ctx * ctx, const clip_image_f32 & img) : clip_graph(ctx, img) {}
55    ggml_cgraph * build() override;
56    ggml_tensor * apply_rope(ggml_tensor * x, ggml_tensor * pos_h, ggml_tensor * pos_w);
57};
58
59struct clip_graph_mimovl : clip_graph {
60    clip_graph_mimovl(clip_ctx * ctx, const clip_image_f32 & img) : clip_graph(ctx, img) {}
61    ggml_cgraph * build() override;
62    // Force F32 mat-mul accumulation to avoid F16 overflow in the FFN down-proj
63    // when the mmproj is stored in F16 (the source weights are BF16; downcasting
64    // to F16 reduces dynamic range below the SwiGLU output magnitude on the last few layers).
65    ggml_tensor * build_mm(ggml_tensor * w, ggml_tensor * x) const override;
66};
67
68struct clip_graph_step3vl : clip_graph {
69    clip_graph_step3vl(clip_ctx * ctx, const clip_image_f32 & img) : clip_graph(ctx, img) {}
70    ggml_cgraph * build() override;
71};
72
73struct clip_graph_youtuvl : clip_graph {
74    clip_graph_youtuvl(clip_ctx * ctx, const clip_image_f32 & img) : clip_graph(ctx, img) {}
75    ggml_cgraph * build() override;
76};
77
78struct clip_graph_yasa2 : clip_graph {
79    clip_graph_yasa2(clip_ctx * ctx, const clip_image_f32 & img) : clip_graph(ctx, img) {}
80    ggml_cgraph * build() override;
81
82    ggml_tensor * layer_norm_channels(ggml_tensor * inp, ggml_tensor * w, ggml_tensor * b, float eps = 1e-6f);
83    ggml_tensor * convnext_grn(ggml_tensor * inp, ggml_tensor * w, ggml_tensor * b);
84};
85
86struct clip_graph_minicpmv : clip_graph {
87    clip_graph_minicpmv(clip_ctx * ctx, const clip_image_f32 & img) : clip_graph(ctx, img) {}
88    ggml_cgraph * build() override;
89};
90
91struct clip_graph_minicpmv4_6 : clip_graph {
92    clip_graph_minicpmv4_6(clip_ctx * ctx, const clip_image_f32 & img) : clip_graph(ctx, img) {}
93    ggml_cgraph * build() override;
94};
95
96struct clip_graph_internvl : clip_graph {
97    clip_graph_internvl(clip_ctx * ctx, const clip_image_f32 & img) : clip_graph(ctx, img) {}
98    ggml_cgraph * build() override;
99    bool support_batch() const override { return true; }
100};
101
102struct clip_graph_nemotron_v2_vl : clip_graph {
103    clip_graph_nemotron_v2_vl(clip_ctx * ctx, const clip_image_f32 & img) : clip_graph(ctx, img) {}
104    ggml_cgraph * build() override;
105};
106
107struct clip_graph_llama4 : clip_graph {
108    clip_graph_llama4(clip_ctx * ctx, const clip_image_f32 & img) : clip_graph(ctx, img) {}
109    ggml_cgraph * build() override;
110};
111
112struct clip_graph_kimivl : clip_graph {
113    clip_graph_kimivl(clip_ctx * ctx, const clip_image_f32 & img) : clip_graph(ctx, img) {}
114    ggml_cgraph * build() override;
115};
116
117struct clip_graph_paddleocr : clip_graph {
118    clip_graph_paddleocr(clip_ctx * ctx, const clip_image_f32 & img) : clip_graph(ctx, img) {}
119    ggml_cgraph * build() override;
120};
121
122struct clip_graph_dotsocr : clip_graph {
123    clip_graph_dotsocr(clip_ctx * ctx, const clip_image_f32 & img) : clip_graph(ctx, img) {}
124    ggml_cgraph * build() override;
125};
126
127struct clip_graph_dots3note_a : clip_graph {
128    clip_graph_dots3note_a(clip_ctx * ctx, const clip_image_f32 & img) : clip_graph(ctx, img) {}
129    ggml_cgraph * build() override;
130};
131
132struct clip_graph_cogvlm : clip_graph {
133    clip_graph_cogvlm(clip_ctx * ctx, const clip_image_f32 & img) : clip_graph(ctx, img) {}
134    ggml_cgraph * build() override;
135};
136
137struct clip_graph_llava : clip_graph {
138    clip_graph_llava(clip_ctx * ctx, const clip_image_f32 & img) : clip_graph(ctx, img) {}
139    ggml_cgraph * build() override;
140};
141
142struct clip_graph_whisper_enc : clip_graph {
143    clip_graph_whisper_enc(clip_ctx * ctx, const clip_image_f32 & img) : clip_graph(ctx, img) {}
144    ggml_cgraph * build() override;
145};
146
147struct clip_graph_deepseekocr : clip_graph {
148    clip_graph_deepseekocr(clip_ctx * ctx, const clip_image_f32 & img) : clip_graph(ctx, img) {}
149    ggml_cgraph * build() override;
150    ggml_tensor * build_sam(ggml_tensor * inp); // build the SAM model
151    bool support_batch() const override { return true; }
152};
153
154struct clip_graph_deepseekocr2 : clip_graph_deepseekocr {
155    clip_graph_deepseekocr2(clip_ctx * ctx, const clip_image_f32 & img) : clip_graph_deepseekocr(ctx, img) {}
156    ggml_cgraph * build() override; // reuses build_sam() from base
157    bool support_batch() const override { return true; }
158};
159
160struct clip_graph_conformer : clip_graph {
161    clip_graph_conformer(clip_ctx * ctx, const clip_image_f32 & img) : clip_graph(ctx, img) {}
162    ggml_cgraph * build() override;
163};
164
165struct clip_graph_granite_speech : clip_graph {
166    clip_graph_granite_speech(clip_ctx * ctx, const clip_image_f32 & img) : clip_graph(ctx, img) {}
167    ggml_cgraph * build() override;
168};
169
170struct clip_graph_gemma4a : clip_graph {
171    clip_graph_gemma4a(clip_ctx * ctx, const clip_image_f32 & img) : clip_graph(ctx, img) {}
172    ggml_cgraph * build() override;
173    ggml_tensor * build_mm(ggml_tensor * w, ggml_tensor * x) const override;
174};
175
176struct clip_graph_gemma4ua : clip_graph {
177    clip_graph_gemma4ua(clip_ctx * ctx, const clip_image_f32 & img) : clip_graph(ctx, img) {}
178    ggml_cgraph * build() override;
179};
180
181struct clip_graph_glm4v : clip_graph {
182    clip_graph_glm4v(clip_ctx * ctx, const clip_image_f32 & img) : clip_graph(ctx, img) {}
183    ggml_cgraph * build() override;
184};
185
186struct clip_graph_hunyuanvl : clip_graph {
187    clip_graph_hunyuanvl(clip_ctx * ctx, const clip_image_f32 & img) : clip_graph(ctx, img) {}
188    ggml_cgraph * build() override;
189};
190
191struct clip_graph_mobilenetv5 : clip_graph {
192    clip_graph_mobilenetv5(clip_ctx * ctx, const clip_image_f32 & img) : clip_graph(ctx, img) {}
193    ggml_cgraph * build() override;
194
195    ggml_tensor * rms_norm_2d(
196        ggml_tensor * inp,
197        ggml_tensor * weight,
198        float eps = 1e-6f);
199
200    ggml_tensor* pad_same_2d(
201        ggml_tensor* inp,
202        int kernel_h,
203        int kernel_w,
204        int stride_h,
205        int stride_w,
206        int dilation_h = 1,
207        int dilation_w = 1);
208
209    ggml_tensor * build_edge_residual(
210        ggml_tensor * inp,
211        const mobilenetv5_block & block,
212        int stride);
213
214    ggml_tensor * build_inverted_residual(
215        ggml_tensor * inp,
216        const mobilenetv5_block & block,
217        int stride);
218
219    ggml_tensor * build_mobilenet_attn(
220        ggml_tensor * inp,
221        const mobilenetv5_block & block);
222};
223
224struct clip_graph_qwen3a : clip_graph {
225    clip_graph_qwen3a(clip_ctx * ctx, const clip_image_f32 & img) : clip_graph(ctx, img) {}
226    ggml_cgraph * build() override;
227};
228
229struct clip_graph_mimo_audio : clip_graph {
230    clip_graph_mimo_audio(clip_ctx * ctx, const clip_image_f32 & img) : clip_graph(ctx, img) {}
231    ggml_cgraph * build() override;
232};
233
234struct clip_graph_qwen3tts_spkenc : clip_graph {
235    clip_graph_qwen3tts_spkenc(clip_ctx * ctx, const clip_image_f32 & img) : clip_graph(ctx, img) {}
236    ggml_cgraph * build() override;
237
238    ggml_tensor * conv1d_same(ggml_tensor * x, ggml_tensor * w, ggml_tensor * b, int dilation) const;
239    ggml_tensor * res2net(ggml_tensor * x, const clip_layer & layer, int dilation, int scale) const;
240    ggml_tensor * se_block(ggml_tensor * x, const clip_layer & layer) const;
241    ggml_tensor * se_res2net_block(ggml_tensor * x, const clip_layer & layer, int dilation, int scale) const;
242    ggml_tensor * attentive_stats_pool(ggml_tensor * x) const;
243};
244
245struct clip_graph_qwen3tts_gen : clip_graph {
246    clip_graph_qwen3tts_gen(clip_ctx * ctx, const clip_image_f32 & img, clip_gen_process_type gen_process, int top_k, float top_p)
247        : clip_graph(ctx, img), gen_process(gen_process), top_k(top_k), top_p(top_p) {}
248    ggml_cgraph * build() override;
249
250    // which sub-graph build() constructs, fixed at graph-build time
251    clip_gen_process_type gen_process;
252
253    // sampling params, fixed at graph-build time (GEN_CODE only)
254    int   top_k;
255    float top_p;
256
257    //
258    // code_gen: backbone hidden state + sampled code0 -> 16 RVQ codes
259    // MTP-style code predictor, one token per codebook
260    //
261    struct code_gen : clip_graph {
262        code_gen(const clip_graph & parent, int top_k, float top_p)
263            : clip_graph(parent), top_k(top_k), top_p(top_p) {}
264        ggml_cgraph * build() override { GGML_ABORT("call prefill()/step() instead"); }
265
266        int   top_k;
267        float top_p;
268
269        ggml_tensor * cache_set(ggml_tensor * cache, int row_idx, ggml_tensor * value) const;
270        ggml_tensor * do_sampling(ggml_tensor * logits, ggml_tensor * inp_rand) const;
271
272        ggml_tensor * const_i32(ggml_tensor * anchor, float value) const;
273        ggml_tensor * causal_mask_row(int64_t n_kv_pad, int pos) const;
274        ggml_tensor * project_in(ggml_tensor * cur) const;
275
276        ggml_tensor * layer_forward(
277                ggml_tensor * cur,
278                const clip_layer & layer,
279                ggml_tensor * inp_pos,
280                ggml_tensor * kq_mask,
281                ggml_tensor *& k_cache_layer,
282                ggml_tensor *& v_cache_layer,
283                int64_t n_kv_pad,
284                int pos,
285                int il) const;
286
287        void prefill(
288                std::vector<ggml_tensor *> & k_cache,
289                std::vector<ggml_tensor *> & v_cache,
290                ggml_tensor *& out_code_cache,
291                ggml_tensor * h_state,
292                ggml_tensor * code0_embd,
293                ggml_tensor * inp_rand) const;
294
295        ggml_tensor * step(
296                std::vector<ggml_tensor *> & k_cache,
297                std::vector<ggml_tensor *> & v_cache,
298                ggml_tensor * out_code_cache,
299                ggml_tensor * inp_rand,
300                int step_idx) const;
301    };
302
303    //
304    // code2wav: RVQ codes -> raw PCM (quantizer + pre_conv + pre_transformer + upsample + DAC).
305    //
306    struct code2wav : clip_graph {
307        code2wav(const clip_graph & parent) : clip_graph(parent) {}
308        ggml_cgraph * build() override { GGML_ABORT("call decode() instead"); }
309
310        // state_in: previous call's persisted state, by slot name (see list_c2w_state_slots())
311        std::map<std::string, ggml_tensor *> state_in;
312        // state_out: this call's state to persist, added to the graph outputs by build()
313        mutable std::vector<std::pair<std::string, ggml_tensor *>> state_out;
314
315        // stateful conv ops: read/update their state via state_in/state_out[state_name]
316        ggml_tensor * causal_conv1d(ggml_tensor * x, ggml_tensor * w, ggml_tensor * b, int dilation, const std::string & state_name) const;
317        ggml_tensor * causal_conv1d_dw(ggml_tensor * x, ggml_tensor * w, ggml_tensor * b, const std::string & state_name) const;
318        ggml_tensor * causal_conv_transpose1d(ggml_tensor * x, ggml_tensor * w, ggml_tensor * b, int stride, const std::string & state_name) const;
319        ggml_tensor * snake(ggml_tensor * x, ggml_tensor * alpha, ggml_tensor * beta) const;
320
321        ggml_tensor * quant_decode(ggml_tensor * inp_codes) const;
322        ggml_tensor * tfm_layer_forward(ggml_tensor * cur, const clip_layer & layer, int il) const;
323        ggml_tensor * convnext_block(ggml_tensor * x, const clip_code2wav::upsample_block & blk, const std::string & state_prefix) const;
324        ggml_tensor * dac_res_unit(ggml_tensor * x, const clip_code2wav::dac_res & res, int dilation, const std::string & state_name) const;
325
326        // inp_codes [1, n_codes] I32 -> this frame's audio samples [n_samples] F32, clamped to [-1, 1]
327        ggml_tensor * decode(ggml_tensor * inp_codes) const;
328    };
329};
330
331//
332// pocket-tts: SEANet convolution stack, shared by the voice encoder and the mimi decoder.
333// stateless unless state_in is populated: convs then pad instead of carrying left-context.
334//
335struct clip_graph_pockettts_seanet : clip_graph {
336    clip_graph_pockettts_seanet(const clip_graph & parent) : clip_graph(parent) {}
337    ggml_cgraph * build() override { GGML_ABORT("call encode()/decode() instead"); }
338
339    // per-call streaming state, keyed by slot name (see list_pockettts_state_slots)
340    std::map<std::string, ggml_tensor *> state_in;
341    mutable std::vector<std::pair<std::string, ggml_tensor *>> state_out;
342
343    ggml_tensor * conv1d(ggml_tensor * x, ggml_tensor * w, ggml_tensor * b, int stride, int dilation,
344                         bool pad_replicate = false, const std::string & state_name = "") const;
345    ggml_tensor * conv_transpose1d(ggml_tensor * x, ggml_tensor * w, ggml_tensor * b, int stride,
346                                   const std::string & state_name = "") const;
347    ggml_tensor * res_unit(ggml_tensor * x, const clip_seanet::stage & stage, int dilation,
348                           const std::string & state_prefix = "") const;
349
350    // x: [T, C] -> [T / hop, dim]
351    ggml_tensor * encode(ggml_tensor * x) const;
352    // x: [T, dim] -> [T * hop, 1], streams when state_in is populated
353    ggml_tensor * decode(ggml_tensor * x) const;
354};
355
356// mimi encoder + speaker_proj: reference waveform -> voice conditioning rows
357struct clip_graph_pockettts_spkenc : clip_graph {
358    clip_graph_pockettts_spkenc(clip_ctx * ctx, const clip_image_f32 & img) : clip_graph(ctx, img) {}
359    ggml_cgraph * build() override;
360
361    ggml_tensor * tfm_layer_forward(ggml_tensor * cur, const clip_layer & layer, ggml_tensor * inp_pos, ggml_tensor * kq_mask, int il) const;
362};
363
364//
365// pocket-tts generation:
366// GEN_CODE = flow-matching decoder + end-of-speech head, one latent per call
367// GEN_WAV  = mimi decoder, a window of latents -> PCM
368//
369struct clip_graph_pockettts_gen : clip_graph {
370    clip_graph_pockettts_gen(clip_ctx * ctx, const clip_image_f32 & img, clip_gen_process_type gen_process, int n_step, int n_frames)
371        : clip_graph(ctx, img), gen_process(gen_process), n_step(n_step), n_frames(n_frames) {}
372    ggml_cgraph * build() override;
373
374    clip_gen_process_type gen_process;
375    int n_step;   // lsd_decode steps, fixed at graph-build time
376    int n_frames; // GEN_WAV only: number of latents to decode
377
378    // AdaLN modulation: x * (1 + scale) + shift
379    ggml_tensor * modulate(ggml_tensor * x, ggml_tensor * shift, ggml_tensor * scale) const;
380    ggml_tensor * time_embed(const clip_flow_net::time_embd & te, float t) const;
381    ggml_tensor * flow_forward(ggml_tensor * cond, ggml_tensor * x, float s, float t) const;
382};
383
384// one persisted state buffer used by code2wav, see qwen3tts-gen.cpp
385struct c2w_state_slot {
386    std::string name;
387    int64_t     ne0;
388    int64_t     ne1;
389};
390std::vector<c2w_state_slot> list_c2w_state_slots(const clip_hparams & hparams, const clip_model & model);
391
392// same, for the streaming mimi decoder (pocket-tts GEN_WAV)
393std::vector<c2w_state_slot> list_pockettts_state_slots(const clip_hparams & hparams, const clip_model & model);
394
395struct clip_graph_kimik25 : clip_graph {
396    clip_graph_kimik25(clip_ctx * ctx, const clip_image_f32 & img) : clip_graph(ctx, img) {}
397    ggml_cgraph * build() override;
398
399    ggml_tensor * resize_position_embeddings_3d(uint32_t interpolation_mode);
400};
401
402struct clip_graph_parakeet : clip_graph {
403    clip_graph_parakeet(clip_ctx * ctx, const clip_image_f32 & img) : clip_graph(ctx, img) {}
404    ggml_cgraph * build() override;
405};
406
407struct clip_graph_exaone4_5 : clip_graph {
408    clip_graph_exaone4_5(clip_ctx * ctx, const clip_image_f32 & img) : clip_graph(ctx, img) {}
409    ggml_cgraph * build() override;
410};
411
412struct clip_graph_granite4_vision : clip_graph {
413    clip_graph_granite4_vision(clip_ctx * ctx, const clip_image_f32 & img)
414        : clip_graph(ctx, img),
415          anyres(img.anyres),
416          n_tiles(img.ny() / img.nx()),
417          tile_side(img.nx() / patch_size) {}
418
419    ggml_cgraph * build() override;
420
421private:
422    // the input image is a stack of tiles on the Y axis: [overview, tile(0,0), tile(0,1), ...]
423    const clip_image_f32::anyres_info anyres;
424    const int n_tiles;
425    const int tile_side; // patches per tile side
426
427    ggml_tensor * build_tile_inp();
428    ggml_tensor * gather(ggml_tensor * src, const std::string & name, int idx_len);
429    ggml_tensor * interp_down(ggml_tensor * src, int side, int new_side);
430    ggml_tensor * build_block(const qf_block & blk, ggml_tensor * h, int bid,
431                              int spatial_offset, int image_side, int window_side,
432                              int query_side, float qformer_eps);
433
434    ggml_tensor * build_newline_row(ggml_context * ctx0);
435    ggml_tensor * build_anyres_assembly(ggml_tensor * cur, int out_side);
436};
437
438struct clip_graph_muse_glimmer : clip_graph {
439    clip_graph_muse_glimmer(clip_ctx * ctx, const clip_image_f32 & img) : clip_graph(ctx, img) {}
440    ggml_cgraph * build() override;
441};