char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
7798007a2
char/aliceai-t5-moe
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 { 16clip_graph_siglip (clip_ctx * ctx ,const clip_image_f32 & img ) :clip_graph (ctx ,img ) {} 17ggml_cgraph * build ()override ; 18}; 19 20struct clip_graph_gemma4v :clip_graph { 21clip_graph_gemma4v (clip_ctx * ctx ,const clip_image_f32 & img ) :clip_graph (ctx ,img ) {} 22ggml_cgraph * build ()override ; 23ggml_tensor * build_mm (ggml_tensor * w ,ggml_tensor * x )const override ; 24bool support_batch ()const override {return true; } 25}; 26 27struct clip_graph_gemma4uv :clip_graph { 28clip_graph_gemma4uv (clip_ctx * ctx ,const clip_image_f32 & img ) :clip_graph (ctx ,img ) {} 29ggml_cgraph * build ()override ; 30}; 31 32struct clip_graph_pixtral :clip_graph { 33clip_graph_pixtral (clip_ctx * ctx ,const clip_image_f32 & img ) :clip_graph (ctx ,img ) {} 34ggml_cgraph * build ()override ; 35}; 36 37struct clip_graph_deepseek4v :clip_graph { 38clip_graph_deepseek4v (clip_ctx * ctx ,const clip_image_f32 & img ) :clip_graph (ctx ,img ) {} 39ggml_cgraph * build ()override ; 40}; 41 42struct clip_graph_qwen2vl :clip_graph { 43clip_graph_qwen2vl (clip_ctx * ctx ,const clip_image_f32 & img ) :clip_graph (ctx ,img ) {} 44ggml_cgraph * build ()override ; 45ggml_tensor * build_inp_with_temporal_merge (); 46}; 47 48struct clip_graph_qwen3vl :clip_graph_qwen2vl { 49clip_graph_qwen3vl (clip_ctx * ctx ,const clip_image_f32 & img ) :clip_graph_qwen2vl (ctx ,img ) {} 50ggml_cgraph * build ()override ; 51}; 52 53struct clip_graph_minimax_m3 :clip_graph { 54clip_graph_minimax_m3 (clip_ctx * ctx ,const clip_image_f32 & img ) :clip_graph (ctx ,img ) {} 55ggml_cgraph * build ()override ; 56ggml_tensor * apply_rope (ggml_tensor * x ,ggml_tensor * pos_h ,ggml_tensor * pos_w ); 57}; 58 59struct clip_graph_mimovl :clip_graph { 60clip_graph_mimovl (clip_ctx * ctx ,const clip_image_f32 & img ) :clip_graph (ctx ,img ) {} 61ggml_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). 65ggml_tensor * build_mm (ggml_tensor * w ,ggml_tensor * x )const override ; 66}; 67 68struct clip_graph_step3vl :clip_graph { 69clip_graph_step3vl (clip_ctx * ctx ,const clip_image_f32 & img ) :clip_graph (ctx ,img ) {} 70ggml_cgraph * build ()override ; 71}; 72 73struct clip_graph_youtuvl :clip_graph { 74clip_graph_youtuvl (clip_ctx * ctx ,const clip_image_f32 & img ) :clip_graph (ctx ,img ) {} 75ggml_cgraph * build ()override ; 76}; 77 78struct clip_graph_yasa2 :clip_graph { 79clip_graph_yasa2 (clip_ctx * ctx ,const clip_image_f32 & img ) :clip_graph (ctx ,img ) {} 80ggml_cgraph * build ()override ; 81 82ggml_tensor * layer_norm_channels (ggml_tensor * inp ,ggml_tensor * w ,ggml_tensor * b ,float eps = 1e-6f ); 83ggml_tensor * convnext_grn (ggml_tensor * inp ,ggml_tensor * w ,ggml_tensor * b ); 84}; 85 86struct clip_graph_minicpmv :clip_graph { 87clip_graph_minicpmv (clip_ctx * ctx ,const clip_image_f32 & img ) :clip_graph (ctx ,img ) {} 88ggml_cgraph * build ()override ; 89}; 90 91struct clip_graph_minicpmv4_6 :clip_graph { 92clip_graph_minicpmv4_6 (clip_ctx * ctx ,const clip_image_f32 & img ) :clip_graph (ctx ,img ) {} 93ggml_cgraph * build ()override ; 94}; 95 96struct clip_graph_internvl :clip_graph { 97clip_graph_internvl (clip_ctx * ctx ,const clip_image_f32 & img ) :clip_graph (ctx ,img ) {} 98ggml_cgraph * build ()override ; 99bool support_batch ()const override {return true; } 100}; 101 102struct clip_graph_nemotron_v2_vl :clip_graph { 103clip_graph_nemotron_v2_vl (clip_ctx * ctx ,const clip_image_f32 & img ) :clip_graph (ctx ,img ) {} 104ggml_cgraph * build ()override ; 105}; 106 107struct clip_graph_llama4 :clip_graph { 108clip_graph_llama4 (clip_ctx * ctx ,const clip_image_f32 & img ) :clip_graph (ctx ,img ) {} 109ggml_cgraph * build ()override ; 110}; 111 112struct clip_graph_kimivl :clip_graph { 113clip_graph_kimivl (clip_ctx * ctx ,const clip_image_f32 & img ) :clip_graph (ctx ,img ) {} 114ggml_cgraph * build ()override ; 115}; 116 117struct clip_graph_paddleocr :clip_graph { 118clip_graph_paddleocr (clip_ctx * ctx ,const clip_image_f32 & img ) :clip_graph (ctx ,img ) {} 119ggml_cgraph * build ()override ; 120}; 121 122struct clip_graph_dotsocr :clip_graph { 123clip_graph_dotsocr (clip_ctx * ctx ,const clip_image_f32 & img ) :clip_graph (ctx ,img ) {} 124ggml_cgraph * build ()override ; 125}; 126 127struct clip_graph_dots3note_a :clip_graph { 128clip_graph_dots3note_a (clip_ctx * ctx ,const clip_image_f32 & img ) :clip_graph (ctx ,img ) {} 129ggml_cgraph * build ()override ; 130}; 131 132struct clip_graph_cogvlm :clip_graph { 133clip_graph_cogvlm (clip_ctx * ctx ,const clip_image_f32 & img ) :clip_graph (ctx ,img ) {} 134ggml_cgraph * build ()override ; 135}; 136 137struct clip_graph_llava :clip_graph { 138clip_graph_llava (clip_ctx * ctx ,const clip_image_f32 & img ) :clip_graph (ctx ,img ) {} 139ggml_cgraph * build ()override ; 140}; 141 142struct clip_graph_whisper_enc :clip_graph { 143clip_graph_whisper_enc (clip_ctx * ctx ,const clip_image_f32 & img ) :clip_graph (ctx ,img ) {} 144ggml_cgraph * build ()override ; 145}; 146 147struct clip_graph_deepseekocr :clip_graph { 148clip_graph_deepseekocr (clip_ctx * ctx ,const clip_image_f32 & img ) :clip_graph (ctx ,img ) {} 149ggml_cgraph * build ()override ; 150ggml_tensor * build_sam (ggml_tensor * inp );// build the SAM model 151bool support_batch ()const override {return true; } 152}; 153 154struct clip_graph_deepseekocr2 :clip_graph_deepseekocr { 155clip_graph_deepseekocr2 (clip_ctx * ctx ,const clip_image_f32 & img ) :clip_graph_deepseekocr (ctx ,img ) {} 156ggml_cgraph * build ()override ;// reuses build_sam() from base 157bool support_batch ()const override {return true; } 158}; 159 160struct clip_graph_conformer :clip_graph { 161clip_graph_conformer (clip_ctx * ctx ,const clip_image_f32 & img ) :clip_graph (ctx ,img ) {} 162ggml_cgraph * build ()override ; 163}; 164 165struct clip_graph_granite_speech :clip_graph { 166clip_graph_granite_speech (clip_ctx * ctx ,const clip_image_f32 & img ) :clip_graph (ctx ,img ) {} 167ggml_cgraph * build ()override ; 168}; 169 170struct clip_graph_gemma4a :clip_graph { 171clip_graph_gemma4a (clip_ctx * ctx ,const clip_image_f32 & img ) :clip_graph (ctx ,img ) {} 172ggml_cgraph * build ()override ; 173ggml_tensor * build_mm (ggml_tensor * w ,ggml_tensor * x )const override ; 174}; 175 176struct clip_graph_gemma4ua :clip_graph { 177clip_graph_gemma4ua (clip_ctx * ctx ,const clip_image_f32 & img ) :clip_graph (ctx ,img ) {} 178ggml_cgraph * build ()override ; 179}; 180 181struct clip_graph_glm4v :clip_graph { 182clip_graph_glm4v (clip_ctx * ctx ,const clip_image_f32 & img ) :clip_graph (ctx ,img ) {} 183ggml_cgraph * build ()override ; 184}; 185 186struct clip_graph_hunyuanvl :clip_graph { 187clip_graph_hunyuanvl (clip_ctx * ctx ,const clip_image_f32 & img ) :clip_graph (ctx ,img ) {} 188ggml_cgraph * build ()override ; 189}; 190 191struct clip_graph_mobilenetv5 :clip_graph { 192clip_graph_mobilenetv5 (clip_ctx * ctx ,const clip_image_f32 & img ) :clip_graph (ctx ,img ) {} 193ggml_cgraph * build ()override ; 194 195ggml_tensor * rms_norm_2d ( 196ggml_tensor * inp , 197ggml_tensor * weight , 198float eps = 1e-6f ); 199 200ggml_tensor * pad_same_2d ( 201ggml_tensor * inp , 202int kernel_h , 203int kernel_w , 204int stride_h , 205int stride_w , 206int dilation_h = 1 , 207int dilation_w = 1 ); 208 209ggml_tensor * build_edge_residual ( 210ggml_tensor * inp , 211const mobilenetv5_block & block , 212int stride ); 213 214ggml_tensor * build_inverted_residual ( 215ggml_tensor * inp , 216const mobilenetv5_block & block , 217int stride ); 218 219ggml_tensor * build_mobilenet_attn ( 220ggml_tensor * inp , 221const mobilenetv5_block & block ); 222}; 223 224struct clip_graph_qwen3a :clip_graph { 225clip_graph_qwen3a (clip_ctx * ctx ,const clip_image_f32 & img ) :clip_graph (ctx ,img ) {} 226ggml_cgraph * build ()override ; 227}; 228 229struct clip_graph_mimo_audio :clip_graph { 230clip_graph_mimo_audio (clip_ctx * ctx ,const clip_image_f32 & img ) :clip_graph (ctx ,img ) {} 231ggml_cgraph * build ()override ; 232}; 233 234struct clip_graph_qwen3tts_spkenc :clip_graph { 235clip_graph_qwen3tts_spkenc (clip_ctx * ctx ,const clip_image_f32 & img ) :clip_graph (ctx ,img ) {} 236ggml_cgraph * build ()override ; 237 238ggml_tensor * conv1d_same (ggml_tensor * x ,ggml_tensor * w ,ggml_tensor * b ,int dilation )const ; 239ggml_tensor * res2net (ggml_tensor * x ,const clip_layer & layer ,int dilation ,int scale )const ; 240ggml_tensor * se_block (ggml_tensor * x ,const clip_layer & layer )const ; 241ggml_tensor * se_res2net_block (ggml_tensor * x ,const clip_layer & layer ,int dilation ,int scale )const ; 242ggml_tensor * attentive_stats_pool (ggml_tensor * x )const ; 243}; 244 245struct clip_graph_qwen3tts_gen :clip_graph { 246clip_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 ) {} 248ggml_cgraph * build ()override ; 249 250// which sub-graph build() constructs, fixed at graph-build time 251clip_gen_process_type gen_process ; 252 253// sampling params, fixed at graph-build time (GEN_CODE only) 254int top_k ; 255float 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// 261struct code_gen :clip_graph { 262code_gen (const clip_graph & parent ,int top_k ,float top_p ) 263 :clip_graph (parent ),top_k (top_k ),top_p (top_p ) {} 264ggml_cgraph * build ()override {GGML_ABORT ("call prefill()/step() instead" ); } 265 266int top_k ; 267float top_p ; 268 269ggml_tensor * cache_set (ggml_tensor * cache ,int row_idx ,ggml_tensor * value )const ; 270ggml_tensor * do_sampling (ggml_tensor * logits ,ggml_tensor * inp_rand )const ; 271 272ggml_tensor * const_i32 (ggml_tensor * anchor ,float value )const ; 273ggml_tensor * causal_mask_row (int64_t n_kv_pad ,int pos )const ; 274ggml_tensor * project_in (ggml_tensor * cur )const ; 275 276ggml_tensor * layer_forward ( 277ggml_tensor * cur , 278const clip_layer & layer , 279ggml_tensor * inp_pos , 280ggml_tensor * kq_mask , 281ggml_tensor *& k_cache_layer , 282ggml_tensor *& v_cache_layer , 283int64_t n_kv_pad , 284int pos , 285int il )const ; 286 287void prefill ( 288std ::vector < ggml_tensor *> & k_cache , 289std ::vector < ggml_tensor *> & v_cache , 290ggml_tensor *& out_code_cache , 291ggml_tensor * h_state , 292ggml_tensor * code0_embd , 293ggml_tensor * inp_rand )const ; 294 295ggml_tensor * step ( 296std ::vector < ggml_tensor *> & k_cache , 297std ::vector < ggml_tensor *> & v_cache , 298ggml_tensor * out_code_cache , 299ggml_tensor * inp_rand , 300int step_idx )const ; 301 }; 302 303// 304// code2wav: RVQ codes -> raw PCM (quantizer + pre_conv + pre_transformer + upsample + DAC). 305// 306struct code2wav :clip_graph { 307code2wav (const clip_graph & parent ) :clip_graph (parent ) {} 308ggml_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()) 311std ::map < std ::string ,ggml_tensor *> state_in ; 312// state_out: this call's state to persist, added to the graph outputs by build() 313mutable 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] 316ggml_tensor * causal_conv1d (ggml_tensor * x ,ggml_tensor * w ,ggml_tensor * b ,int dilation ,const std ::string & state_name )const ; 317ggml_tensor * causal_conv1d_dw (ggml_tensor * x ,ggml_tensor * w ,ggml_tensor * b ,const std ::string & state_name )const ; 318ggml_tensor * causal_conv_transpose1d (ggml_tensor * x ,ggml_tensor * w ,ggml_tensor * b ,int stride ,const std ::string & state_name )const ; 319ggml_tensor * snake (ggml_tensor * x ,ggml_tensor * alpha ,ggml_tensor * beta )const ; 320 321ggml_tensor * quant_decode (ggml_tensor * inp_codes )const ; 322ggml_tensor * tfm_layer_forward (ggml_tensor * cur ,const clip_layer & layer ,int il )const ; 323ggml_tensor * convnext_block (ggml_tensor * x ,const clip_code2wav ::upsample_block & blk ,const std ::string & state_prefix )const ; 324ggml_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] 327ggml_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 { 336clip_graph_pockettts_seanet (const clip_graph & parent ) :clip_graph (parent ) {} 337ggml_cgraph * build ()override {GGML_ABORT ("call encode()/decode() instead" ); } 338 339// per-call streaming state, keyed by slot name (see list_pockettts_state_slots) 340std ::map < std ::string ,ggml_tensor *> state_in ; 341mutable std ::vector < std ::pair < std ::string ,ggml_tensor * >>state_out ; 342 343ggml_tensor * conv1d (ggml_tensor * x ,ggml_tensor * w ,ggml_tensor * b ,int stride ,int dilation , 344bool pad_replicate = false,const std ::string & state_name = "" )const ; 345ggml_tensor * conv_transpose1d (ggml_tensor * x ,ggml_tensor * w ,ggml_tensor * b ,int stride , 346const std ::string & state_name = "" )const ; 347ggml_tensor * res_unit (ggml_tensor * x ,const clip_seanet ::stage & stage ,int dilation , 348const std ::string & state_prefix = "" )const ; 349 350// x: [T, C] -> [T / hop, dim] 351ggml_tensor * encode (ggml_tensor * x )const ; 352// x: [T, dim] -> [T * hop, 1], streams when state_in is populated 353ggml_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 { 358clip_graph_pockettts_spkenc (clip_ctx * ctx ,const clip_image_f32 & img ) :clip_graph (ctx ,img ) {} 359ggml_cgraph * build ()override ; 360 361ggml_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 { 370clip_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 ) {} 372ggml_cgraph * build ()override ; 373 374clip_gen_process_type gen_process ; 375int n_step ;// lsd_decode steps, fixed at graph-build time 376int n_frames ;// GEN_WAV only: number of latents to decode 377 378// AdaLN modulation: x * (1 + scale) + shift 379ggml_tensor * modulate (ggml_tensor * x ,ggml_tensor * shift ,ggml_tensor * scale )const ; 380ggml_tensor * time_embed (const clip_flow_net ::time_embd & te ,float t )const ; 381ggml_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 { 386std ::string name ; 387int64_t ne0 ; 388int64_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 { 396clip_graph_kimik25 (clip_ctx * ctx ,const clip_image_f32 & img ) :clip_graph (ctx ,img ) {} 397ggml_cgraph * build ()override ; 398 399ggml_tensor * resize_position_embeddings_3d (uint32_t interpolation_mode ); 400}; 401 402struct clip_graph_parakeet :clip_graph { 403clip_graph_parakeet (clip_ctx * ctx ,const clip_image_f32 & img ) :clip_graph (ctx ,img ) {} 404ggml_cgraph * build ()override ; 405}; 406 407struct clip_graph_exaone4_5 :clip_graph { 408clip_graph_exaone4_5 (clip_ctx * ctx ,const clip_image_f32 & img ) :clip_graph (ctx ,img ) {} 409ggml_cgraph * build ()override ; 410}; 411 412struct clip_graph_granite4_vision :clip_graph { 413clip_graph_granite4_vision (clip_ctx * ctx ,const clip_image_f32 & img ) 414 :clip_graph (ctx ,img ), 415anyres (img .anyres ), 416n_tiles (img .ny () /img .nx ()), 417tile_side (img .nx () /patch_size ) {} 418 419ggml_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), ...] 423const clip_image_f32 ::anyres_info anyres ; 424const int n_tiles ; 425const int tile_side ;// patches per tile side 426 427ggml_tensor * build_tile_inp (); 428ggml_tensor * gather (ggml_tensor * src ,const std ::string & name ,int idx_len ); 429ggml_tensor * interp_down (ggml_tensor * src ,int side ,int new_side ); 430ggml_tensor * build_block (const qf_block & blk ,ggml_tensor * h ,int bid , 431int spatial_offset ,int image_side ,int window_side , 432int query_side ,float qformer_eps ); 433 434ggml_tensor * build_newline_row (ggml_context * ctx0 ); 435ggml_tensor * build_anyres_assembly (ggml_tensor * cur ,int out_side ); 436}; 437 438struct clip_graph_muse_glimmer :clip_graph { 439clip_graph_muse_glimmer (clip_ctx * ctx ,const clip_image_f32 & img ) :clip_graph (ctx ,img ) {} 440ggml_cgraph * build ()override ; 441};