char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
b4aa7dd47
char/aliceai-t5-moe
1#include "models.h" 2 3ggml_cgraph * clip_graph_qwen3vl::build () { 4GGML_ASSERT (model .patch_bias != nullptr ); 5GGML_ASSERT (model .position_embeddings != nullptr ); 6GGML_ASSERT (model .class_embedding == nullptr ); 7 8const int batch_size = 1 ; 9const int n_pos = n_patches ; 10const int num_position_ids = n_pos * 4 ;// m-rope requires 4 dim per position 11 12norm_type norm_t = NORM_TYPE_NORMAL ; 13 14int mrope_sections [4 ]= {d_head /4 ,d_head /4 ,d_head /4 ,d_head /4 }; 15 16ggml_tensor * inp = build_inp_with_temporal_merge (); 17 18// spatial merge 19 { 20inp = ggml_permute (ctx0 ,inp ,1 ,2 ,0 ,3 );// [w, h, c, b] -> [c, w, h, b] 21inp = ggml_cont_4d ( 22ctx0 ,inp , 23n_embd * 2 ,n_patches_x /2 ,n_patches_y ,batch_size ); 24inp = ggml_reshape_4d ( 25ctx0 ,inp , 26n_embd * 2 ,n_patches_x /2 ,2 ,batch_size * (n_patches_y /2 )); 27inp = ggml_permute (ctx0 ,inp ,0 ,2 ,1 ,3 ); 28inp = ggml_cont_3d ( 29ctx0 ,inp , 30n_embd ,n_patches_x * n_patches_y ,batch_size ); 31 } 32 33// add patch bias 34if (model .patch_bias != nullptr ) { 35inp = ggml_add (ctx0 ,inp ,model .patch_bias ); 36cb (inp ,"patch_bias" ,-1 ); 37 } 38 39// calculate absolute position embedding and apply 40ggml_tensor * learned_pos_embd = resize_position_embeddings (GGML_SCALE_MODE_BILINEAR |GGML_SCALE_FLAG_ALIGN_CORNERS ); 41learned_pos_embd = ggml_cont_4d ( 42ctx0 ,learned_pos_embd , 43n_embd * 2 ,n_patches_x /2 ,n_patches_y ,batch_size ); 44learned_pos_embd = ggml_reshape_4d ( 45ctx0 ,learned_pos_embd , 46n_embd * 2 ,n_patches_x /2 ,2 ,batch_size * (n_patches_y /2 )); 47learned_pos_embd = ggml_permute (ctx0 ,learned_pos_embd ,0 ,2 ,1 ,3 ); 48learned_pos_embd = ggml_cont_3d ( 49ctx0 ,learned_pos_embd , 50n_embd ,n_patches_x * n_patches_y ,batch_size ); 51inp = ggml_add (ctx0 ,inp ,learned_pos_embd ); 52cb (inp ,"inp_pos_emb" ,-1 ); 53 54ggml_tensor * inpL = inp ; 55 56ggml_tensor * positions = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 ,num_position_ids ); 57ggml_set_name (positions ,"positions" ); 58ggml_set_input (positions ); 59 60// pre-layernorm 61if (model .pre_ln_w ) { 62inpL = build_norm (inpL ,model .pre_ln_w ,model .pre_ln_b ,norm_t ,eps ,-1 ); 63 } 64 65// deepstack features (stack along the feature dimension), [n_embd * len(deepstack_layers), n_patches_x * n_patches_y, batch_size] 66ggml_tensor * deepstack_features = nullptr ; 67const int merge_factor = hparams .n_merge > 0 ?hparams .n_merge * hparams .n_merge :4 ;// default 2x2=4 for qwen3vl 68 69// loop over layers 70for (int il = 0 ;il < n_layer ;il ++ ) { 71auto & layer = model .layers [il ]; 72 73ggml_tensor * cur = inpL ;// inpL = residual, cur = hidden_states 74 75// layernorm1 76cur = build_norm (cur ,layer .ln_1_w ,layer .ln_1_b ,norm_t ,eps ,il ); 77cb (cur ,"ln1" ,il ); 78 79// self-attention 80 { 81cur = build_mm (layer .qkv_w ,cur ); 82cur = ggml_add (ctx0 ,cur ,layer .qkv_b ); 83 84ggml_tensor * Qcur = ggml_view_3d (ctx0 ,cur ,d_head ,n_head ,n_pos , 85/* nb1 */ ggml_row_size (cur -> type ,d_head ), 86/* nb2 */ cur -> nb [1 ], 87/* offset */ 0 ); 88 89ggml_tensor * Kcur = ggml_view_3d (ctx0 ,cur ,d_head ,n_head ,n_pos , 90/* nb1 */ ggml_row_size (cur -> type ,d_head ), 91/* nb2 */ cur -> nb [1 ], 92/* offset */ ggml_row_size (cur -> type ,n_embd )); 93 94ggml_tensor * Vcur = ggml_view_3d (ctx0 ,cur ,d_head ,n_head ,n_pos , 95/* nb1 */ ggml_row_size (cur -> type ,d_head ), 96/* nb2 */ cur -> nb [1 ], 97/* offset */ ggml_row_size (cur -> type ,2 * n_embd )); 98 99cb (Qcur ,"Qcur" ,il ); 100cb (Kcur ,"Kcur" ,il ); 101cb (Vcur ,"Vcur" ,il ); 102 103// apply M-RoPE 104Qcur = ggml_rope_multi ( 105ctx0 ,Qcur ,positions ,nullptr , 106d_head /2 ,mrope_sections ,GGML_ROPE_TYPE_VISION ,32768 ,10000 ,1 ,0 ,1 ,32 ,1 ); 107Kcur = ggml_rope_multi ( 108ctx0 ,Kcur ,positions ,nullptr , 109d_head /2 ,mrope_sections ,GGML_ROPE_TYPE_VISION ,32768 ,10000 ,1 ,0 ,1 ,32 ,1 ); 110 111cb (Qcur ,"Qcur_rope" ,il ); 112cb (Kcur ,"Kcur_rope" ,il ); 113 114cur = build_attn (layer .o_w ,layer .o_b , 115Qcur ,Kcur ,Vcur ,nullptr ,kq_scale ,il ); 116cb (cur ,"attn_out" ,il ); 117 } 118 119// re-add the layer input, e.g., residual 120cur = ggml_add (ctx0 ,cur ,inpL ); 121 122inpL = cur ;// inpL = residual, cur = hidden_states 123 124cb (cur ,"ffn_inp" ,il ); 125 126// layernorm2 127cur = build_norm (cur ,layer .ln_2_w ,layer .ln_2_b ,norm_t ,eps ,il ); 128cb (cur ,"ffn_inp_normed" ,il ); 129 130// ffn 131cur = build_ffn (cur , 132layer .ff_up_w ,layer .ff_up_b , 133layer .ff_gate_w ,layer .ff_gate_b , 134layer .ff_down_w ,layer .ff_down_b , 135hparams .ffn_op ,il ); 136 137cb (cur ,"ffn_out" ,il ); 138 139// residual 2 140cur = ggml_add (ctx0 ,inpL ,cur ); 141cb (cur ,"layer_out" ,il ); 142 143if (layer .has_deepstack ()) { 144ggml_tensor * feat = ggml_reshape_3d (ctx0 ,cur ,n_embd * merge_factor ,n_pos /merge_factor ,batch_size ); 145feat = build_norm (feat ,layer .deepstack_norm_w ,layer .deepstack_norm_b ,norm_t ,eps ,il ); 146feat = build_ffn (feat , 147layer .deepstack_fc1_w ,layer .deepstack_fc1_b , 148nullptr ,nullptr , 149layer .deepstack_fc2_w ,layer .deepstack_fc2_b , 150 ffn_op_type::FFN_GELU ,il ); 151 152if (!deepstack_features ) { 153deepstack_features = feat ; 154 }else { 155// concat along the feature dimension 156deepstack_features = ggml_concat (ctx0 ,deepstack_features ,feat ,0 ); 157 } 158 } 159 160inpL = cur ; 161 } 162 163// post-layernorm 164if (model .post_ln_w ) { 165inpL = build_norm (inpL ,model .post_ln_w ,model .post_ln_b ,norm_t ,eps ,n_layer ); 166 } 167 168// multimodal projection 169ggml_tensor * embeddings = inpL ; 170embeddings = ggml_reshape_3d (ctx0 ,embeddings ,n_embd * 4 ,n_pos /4 ,batch_size ); 171 172embeddings = build_ffn (embeddings , 173model .mm_0_w ,model .mm_0_b , 174nullptr ,nullptr , 175model .mm_1_w ,model .mm_1_b , 176 ffn_op_type::FFN_GELU ,-1 ); 177 178if (deepstack_features ) { 179embeddings = ggml_concat (ctx0 ,embeddings ,deepstack_features ,0 ); 180 }// concat along the feature dimension 181 182// build the graph 183ggml_build_forward_expand (gf ,embeddings ); 184 185return gf ; 186}