char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
62bf73d25
char/aliceai-t5-moe
1#include "models.h" 2 3// MuseGlimmer vision encoder: 50-layer ViT with 2D RoPE, sparse block-diagonal 4// window attention (every 4th + last layer global), pixel-shuffle downsample, then 5// adapter MLP + LLM's vision_projection. 6// 7// Several quantities are precomputed on host and fed as named graph inputs (filled in 8// clip.cpp set_input, PROJECTOR_TYPE_MUSE_GLIMMER branch): 9// muse_glimmer_pos_w/_h [n_tok] i32 : 1-indexed RoPE positions (sparse-permuted order) 10// muse_glimmer_sp_perm [n_tok] i32 : window grouping permutation (applied after ln_pre) 11// muse_glimmer_inv_perm [n_tok] i32 : inverse of sp_perm (applied after blocks) 12// muse_glimmer_ds_perm [n_tok] i32 : pixel-shuffle gather (original order) 13// muse_glimmer_sp_mask [n_tok, n_tok] f32 : block-diagonal window mask (sparse layers) 14ggml_cgraph * clip_graph_muse_glimmer::build () { 15const int ds = hparams .n_merge ;// downsample factor (2) 16const int sf = hparams .muse_glimmer_sparse_factor ;// 4 17const int n_tok = n_patches ; 18const int n_out = (n_patches_x /ds )* (n_patches_y /ds ); 19const float rope_base = hparams .rope_theta ;// 10000 20 21auto inp_i32 = [& ](const char * name ,int64_t n ) { 22ggml_tensor * t = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 ,n ); 23ggml_set_name (t ,name ); 24ggml_set_input (t ); 25return t ; 26 }; 27 28ggml_tensor * pos_w = inp_i32 ("muse_glimmer_pos_w" ,n_tok ); 29ggml_tensor * pos_h = inp_i32 ("muse_glimmer_pos_h" ,n_tok ); 30ggml_tensor * sp_perm = inp_i32 ("muse_glimmer_sp_perm" ,n_tok ); 31ggml_tensor * inv_perm = inp_i32 ("muse_glimmer_inv_perm" ,n_tok ); 32ggml_tensor * ds_perm = inp_i32 ("muse_glimmer_ds_perm" ,n_tok ); 33 34ggml_tensor * sp_mask = ggml_new_tensor_2d (ctx0 ,GGML_TYPE_F32 ,n_tok ,n_tok ); 35ggml_set_name (sp_mask ,"muse_glimmer_sp_mask" ); 36ggml_set_input (sp_mask ); 37 38// patchify via build_inp (conv2d over raw pixels) + bilinear-resized learned pos-emb 39ggml_tensor * x = build_inp ();// [n_embd, n_tok, 1] 40x = ggml_add (ctx0 ,x ,resize_position_embeddings (GGML_SCALE_MODE_BILINEAR )); 41cb (x ,"after_posemb" ,-1 ); 42 43// group patches into pgrid x pgrid windows (sparse attention order) 44x = ggml_get_rows (ctx0 ,x ,sp_perm ); 45cb (x ,"after_sp_perm" ,-1 ); 46 47// per-layer mask: sparse layers get sp_mask, global layers (every sf-th and last) get none 48 std::vector < ggml_tensor *> attn_mask_layers (n_layer ); 49for (int il = 0 ;il < n_layer ;++ il ) { 50const bool is_global = (il == n_layer - 1 )|| ((il + 1 ) %sf == 0 ); 51attn_mask_layers [il ]= is_global ?nullptr :sp_mask ; 52 } 53 54// 2D RoPE: first half of head_dim uses width pos, second half uses height pos 55auto add_pos = [& ](ggml_tensor * cur ,const clip_layer & ) { 56return build_rope_2d (ctx0 ,cur ,pos_w ,pos_h ,rope_base , false); 57 }; 58 59build_vit_opts opts ; 60opts .attn_mask_layers = std::move (attn_mask_layers ); 61 62// pre_ln, per-layer transformer, post_ln (all inside build_vit); reference uses exact (erf) GELU 63x = build_vit (x ,n_tok ,NORM_TYPE_NORMAL ,FFN_GELU_ERF ,nullptr ,add_pos ,opts ); 64 65// un-permute back to original grid order 66x = ggml_get_rows (ctx0 ,x ,inv_perm ); 67cb (x ,"after_inv_perm" ,-1 ); 68 69// pixel-shuffle downsample: gather f*f spatial neighbors then concat channel-outer. 70// out[c*(ds*ds)+s, o] = x[ds_perm gathered][o*(ds*ds)+s, c] 71x = ggml_get_rows (ctx0 ,x ,ds_perm );// [n_embd, n_tok], grouped 72x = ggml_reshape_3d (ctx0 ,x ,n_embd ,ds * ds ,n_out );// [c, s, o] 73x = ggml_permute (ctx0 ,x ,1 ,0 ,2 ,3 );// [s, c, o] 74x = ggml_cont (ctx0 ,x ); 75x = ggml_reshape_2d (ctx0 ,x ,n_embd * ds * ds ,n_out );// [6144, n_out] 76cb (x ,"encoder_out" ,-1 ); 77 78// adapter (6144->4096->4096, exact GELU each) + LLM vision_projection (4096->6656) 79x = build_mm (model .mm_0_w ,x ); 80x = ggml_gelu_erf (ctx0 ,x ); 81x = build_mm (model .mm_1_w ,x ); 82x = ggml_gelu_erf (ctx0 ,x ); 83x = build_mm (model .mm_2_w ,x );// [6656, n_out] 84cb (x ,"projected" ,-1 ); 85 86ggml_build_forward_expand (gf ,x ); 87return gf ; 88}